Debian Codegraph @

src/daemonizer/posix_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/scoped_message_writer.h"
32 #include "common/util.h"
33 #include "daemonizer/posix_fork.h"
34
35 #include <boost/filesystem/operations.hpp>
36 #include <boost/filesystem/path.hpp>
37
38 namespace daemonizer
39 {
40 namespace
41 {
42 const command_line::arg_descriptor<bool> arg_detach = {
43 "detach"
44 , "Run as daemon"
45 };
46 const command_line::arg_descriptor<std::string> arg_pidfile = {
47 "pidfile"
48 , "File path to write the daemon's PID to (optional, requires --detach)"
49 };
50 const command_line::arg_descriptor<bool> arg_non_interactive = {
51 "non-interactive"
52 , "Run non-interactive"
53 };
54 }
55
56 inline void init_options(
57 boost::program_options::options_description & hidden_options
58 , boost::program_options::options_description & normal_options
59 )
60 {
61 command_line::add_arg(normal_options, arg_detach);
62 command_line::add_arg(normal_options, arg_pidfile);
63 command_line::add_arg(normal_options, arg_non_interactive);
64 }
65
66 inline boost::filesystem::path get_default_data_dir()
67 {
68 return boost::filesystem::absolute(tools::get_default_data_dir());
69 }
70
71 inline boost::filesystem::path get_relative_path_base(
72 boost::program_options::variables_map const & vm
73 )
74 {
75 return boost::filesystem::current_path();
76 }
77
78 template <typename T_executor>
79 inline bool daemonize(
80 int argc, char const * argv[]
81 , T_executor && executor // universal ref
82 , boost::program_options::variables_map const & vm
83 )
84 {
85 if (command_line::has_arg(vm, arg_detach))
86 {
87 tools::success_msg_writer() << "Forking to background...";
88 std::string pidfile;
89 if (command_line::has_arg(vm, arg_pidfile))
90 {
91 pidfile = command_line::get_arg(vm, arg_pidfile);
92 }
93 posix::fork(pidfile);
94 auto daemon = executor.create_daemon(vm);
95 return daemon.run();
96 }
97 else if (command_line::has_arg(vm, arg_non_interactive))
98 {
99 return executor.run_non_interactive(vm);
100 }
101 else
102 {
103 //LOG_PRINT_L0("Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL);
104 return executor.run_interactive(vm);
105 }
106 }
107 }