Debian Codegraph @

src/daemonizer/windows_daemonizer.inl

raw
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #pragma once
30
31 #include "common/util.h"
32 #include "daemonizer/windows_service.h"
33 #include "daemonizer/windows_service_runner.h"
34 #include "cryptonote_core/cryptonote_core.h"
35
36 #include <shlobj.h>
37 #include <boost/filesystem/operations.hpp>
38 #include <boost/filesystem/path.hpp>
39
40 namespace daemonizer
41 {
42 namespace
43 {
44 const command_line::arg_descriptor<bool> arg_install_service = {
45 "install-service"
46 , "Install Windows service"
47 };
48 const command_line::arg_descriptor<bool> arg_uninstall_service = {
49 "uninstall-service"
50 , "Uninstall Windows service"
51 };
52 const command_line::arg_descriptor<bool> arg_start_service = {
53 "start-service"
54 , "Start Windows service"
55 };
56 const command_line::arg_descriptor<bool> arg_stop_service = {
57 "stop-service"
58 , "Stop Windows service"
59 };
60 const command_line::arg_descriptor<bool> arg_is_service = {
61 "run-as-service"
62 , "Hidden -- true if running as windows service"
63 };
64 const command_line::arg_descriptor<bool> arg_non_interactive = {
65 "non-interactive"
66 , "Run non-interactive"
67 };
68
69 std::string get_argument_string(int argc, char const * argv[])
70 {
71 std::string result = "";
72 for (int i = 1; i < argc; ++i)
73 {
74 result += " " + std::string{argv[i]};
75 }
76 return result;
77 }
78 }
79
80 inline void init_options(
81 boost::program_options::options_description & hidden_options
82 , boost::program_options::options_description & normal_options
83 )
84 {
85 command_line::add_arg(normal_options, arg_install_service);
86 command_line::add_arg(normal_options, arg_uninstall_service);
87 command_line::add_arg(normal_options, arg_start_service);
88 command_line::add_arg(normal_options, arg_stop_service);
89 command_line::add_arg(hidden_options, arg_is_service);
90 command_line::add_arg(hidden_options, arg_non_interactive);
91 }
92
93 inline boost::filesystem::path get_default_data_dir()
94 {
95 bool admin;
96 if (!windows::check_admin(admin))
97 {
98 admin = false;
99 }
100 if (admin)
101 {
102 return boost::filesystem::absolute(
103 tools::get_special_folder_path(CSIDL_COMMON_APPDATA, true) + "\\" + CRYPTONOTE_NAME
104 );
105 }
106 else
107 {
108 return boost::filesystem::absolute(
109 tools::get_special_folder_path(CSIDL_APPDATA, true) + "\\" + CRYPTONOTE_NAME
110 );
111 }
112 }
113
114 inline boost::filesystem::path get_relative_path_base(
115 boost::program_options::variables_map const & vm
116 )
117 {
118 if (command_line::has_arg(vm, arg_is_service))
119 {
120 if (command_line::has_arg(vm, cryptonote::arg_data_dir))
121 {
122 return command_line::get_arg(vm, cryptonote::arg_data_dir);
123 }
124 else
125 {
126 return tools::get_default_data_dir();
127 }
128 }
129 else
130 {
131 return boost::filesystem::current_path();
132 }
133 }
134
135 template <typename T_executor>
136 inline bool daemonize(
137 int argc, char const * argv[]
138 , T_executor && executor // universal ref
139 , boost::program_options::variables_map const & vm
140 )
141 {
142 std::string arguments = get_argument_string(argc, argv);
143
144 if (command_line::has_arg(vm, arg_is_service))
145 {
146 // TODO - Set the service status here for return codes
147 windows::t_service_runner<typename T_executor::t_daemon>::run(
148 executor.name()
149 , executor.create_daemon(vm)
150 );
151 return true;
152 }
153 else if (command_line::has_arg(vm, arg_install_service))
154 {
155 if (windows::ensure_admin(arguments))
156 {
157 arguments += " --run-as-service";
158 return windows::install_service(executor.name(), arguments);
159 }
160 }
161 else if (command_line::has_arg(vm, arg_uninstall_service))
162 {
163 if (windows::ensure_admin(arguments))
164 {
165 return windows::uninstall_service(executor.name());
166 }
167 }
168 else if (command_line::has_arg(vm, arg_start_service))
169 {
170 if (windows::ensure_admin(arguments))
171 {
172 return windows::start_service(executor.name());
173 }
174 }
175 else if (command_line::has_arg(vm, arg_stop_service))
176 {
177 if (windows::ensure_admin(arguments))
178 {
179 return windows::stop_service(executor.name());
180 }
181 }
182 else // interactive
183 {
184 //LOG_PRINT_L0("Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL);
185 if (command_line::has_arg(vm, arg_non_interactive))
186 return executor.run_non_interactive(vm);
187 else
188 return executor.run_interactive(vm);
189 }
190
191 return false;
192 }
193 }