Debian Codegraph @

monero/src/daemon/daemon.cpp

raw symbols SCIP
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 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31 #include <memory>
32 #include <stdexcept>
33 #include <boost/algorithm/string/split.hpp>
34 #include "misc_log_ex.h"
35 #include "daemon/daemon.h"
36 #include "rpc/daemon_handler.h"
37 #include "rpc/zmq_pub.h"
38 #include "rpc/zmq_server.h"
39
40 #include "common/password.h"
41 #include "common/util.h"
42 #include "cryptonote_basic/events.h"
43 #include "daemon/core.h"
44 #include "daemon/p2p.h"
45 #include "daemon/protocol.h"
46 #include "daemon/rpc.h"
47 #include "daemon/command_server.h"
48 #include "daemon/command_line_args.h"
49 #include "net/net_ssl.h"
50 #include "version.h"
51
52 using namespace epee;
53
54 #include <functional>
55
56 #undef MONERO_DEFAULT_LOG_CATEGORY
57 #define MONERO_DEFAULT_LOG_CATEGORY "daemon"
58
59 namespace daemonize {
60
61 struct zmq_internals
62 {
63 explicit zmq_internals(t_core& core, t_p2p& p2p)
64 : rpc_handler{core.get(), p2p.get()}
65 , server{rpc_handler}
66 {}
67
68 cryptonote::rpc::DaemonHandler rpc_handler;
69 cryptonote::rpc::ZmqServer server;
70 };
71
72 struct t_internals {
73 private:
74 t_protocol protocol;
75 public:
76 t_core core;
77 t_p2p p2p;
78 std::vector<std::unique_ptr<t_rpc>> rpcs;
79 std::unique_ptr<zmq_internals> zmq;
80
81 t_internals(
82 boost::program_options::variables_map const & vm
83 )
84 : core{vm}
85 , protocol{vm, core, command_line::get_arg(vm, cryptonote::arg_offline)}
86 , p2p{vm, protocol}
87 , zmq{nullptr}
88 {
89 // Handle circular dependencies
90 protocol.set_p2p_endpoint(p2p.get());
91 core.set_protocol(protocol.get());
92
93 const auto restricted = command_line::get_arg(vm, cryptonote::core_rpc_server::arg_restricted_rpc);
94 const auto main_rpc_port = command_line::get_arg(vm, cryptonote::core_rpc_server::arg_rpc_bind_port);
95 const auto restricted_rpc_port_arg = cryptonote::core_rpc_server::arg_rpc_restricted_bind_port;
96 const bool has_restricted_rpc_port_arg = !command_line::is_arg_defaulted(vm, restricted_rpc_port_arg);
97 rpcs.emplace_back(new t_rpc{vm, core, p2p, restricted, main_rpc_port, "core", !has_restricted_rpc_port_arg});
98
99 if(has_restricted_rpc_port_arg)
100 {
101 auto restricted_rpc_port = command_line::get_arg(vm, restricted_rpc_port_arg);
102 rpcs.emplace_back(new t_rpc{vm, core, p2p, true, restricted_rpc_port, "restricted", true});
103 }
104
105 if (!command_line::get_arg(vm, daemon_args::arg_zmq_rpc_disabled))
106 {
107 zmq.reset(new zmq_internals{core, p2p});
108
109 const std::string zmq_port = command_line::get_arg(vm, daemon_args::arg_zmq_rpc_bind_port);
110 const std::string zmq_address = command_line::get_arg(vm, daemon_args::arg_zmq_rpc_bind_ip);
111
112 if (!zmq->server.init_rpc(zmq_address, zmq_port))
113 throw std::runtime_error{"Failed to add TCP socket(" + zmq_address + ":" + zmq_port + ") to ZMQ RPC Server"};
114
115 std::shared_ptr<cryptonote::listener::zmq_pub> shared;
116 const std::vector<std::string> zmq_pub = command_line::get_arg(vm, daemon_args::arg_zmq_pub);
117 if (!zmq_pub.empty() && !(shared = zmq->server.init_pub(epee::to_span(zmq_pub))))
118 throw std::runtime_error{"Failed to initialize zmq_pub"};
119
120 if (shared)
121 {
122 core.get().get_blockchain_storage().set_txpool_notify(cryptonote::listener::zmq_pub::txpool_add{shared});
123 core.get().get_blockchain_storage().add_block_notify(cryptonote::listener::zmq_pub::chain_main{shared});
124 core.get().get_blockchain_storage().add_miner_notify(cryptonote::listener::zmq_pub::miner_data{shared});
125 }
126 }
127 }
128 };
129
130 void t_daemon::init_options(boost::program_options::options_description & option_spec)
131 {
132 t_core::init_options(option_spec);
133 t_p2p::init_options(option_spec);
134 t_rpc::init_options(option_spec);
135 }
136
137 t_daemon::t_daemon(
138 boost::program_options::variables_map const & vm,
139 uint16_t public_rpc_port
140 )
141 : mp_internals{new t_internals{vm}},
142 public_rpc_port(public_rpc_port)
143 {
144 }
145
146 t_daemon::~t_daemon() = default;
147
148 // MSVC is brain-dead and can't default this...
149 t_daemon::t_daemon(t_daemon && other)
150 {
151 if (this != &other)
152 {
153 mp_internals = std::move(other.mp_internals);
154 other.mp_internals.reset(nullptr);
155 public_rpc_port = other.public_rpc_port;
156 }
157 }
158
159 // or this
160 t_daemon & t_daemon::operator=(t_daemon && other)
161 {
162 if (this != &other)
163 {
164 mp_internals = std::move(other.mp_internals);
165 other.mp_internals.reset(nullptr);
166 public_rpc_port = other.public_rpc_port;
167 }
168 return *this;
169 }
170
171 bool t_daemon::run(bool interactive)
172 {
173 if (nullptr == mp_internals)
174 {
175 throw std::runtime_error{"Can't run stopped daemon"};
176 }
177
178 std::atomic<bool> stop(false), shutdown(false);
179 boost::thread stop_thread = boost::thread([&stop, &shutdown, this] {
180 while (!stop)
181 epee::misc_utils::sleep_no_w(100);
182 if (shutdown)
183 this->stop_p2p();
184 });
185 epee::misc_utils::auto_scope_leave_caller scope_exit_handler = epee::misc_utils::create_scope_leave_handler([&](){
186 stop = true;
187 stop_thread.join();
188 });
189 tools::signal_handler::install([&stop, &shutdown](int){ stop = shutdown = true; });
190
191 try
192 {
193 if (!mp_internals->core.run())
194 return false;
195
196 for(auto& rpc: mp_internals->rpcs)
197 rpc->run();
198
199 std::unique_ptr<daemonize::t_command_server> rpc_commands;
200 if (interactive && mp_internals->rpcs.size())
201 {
202 // The first three variables are not used when the fourth is false
203 rpc_commands.reset(new daemonize::t_command_server(0, 0, boost::none, epee::net_utils::ssl_support_t::e_ssl_support_disabled, false, mp_internals->rpcs.front()->get_server()));
204 rpc_commands->start_handling(std::bind(&daemonize::t_daemon::stop_p2p, this));
205 }
206
207 if (mp_internals->zmq)
208 mp_internals->zmq->server.run();
209 else
210 MINFO("ZMQ server disabled");
211
212 if (public_rpc_port > 0)
213 {
214 MGINFO("Public RPC port " << public_rpc_port << " will be advertised to other peers over P2P");
215 mp_internals->p2p.get().set_rpc_port(public_rpc_port);
216 }
217
218 mp_internals->p2p.run(); // blocks until p2p goes down
219
220 if (rpc_commands)
221 rpc_commands->stop_handling();
222
223 if (mp_internals->zmq)
224 mp_internals->zmq->server.stop();
225
226 for(auto& rpc : mp_internals->rpcs)
227 rpc->stop();
228 MGINFO("Node stopped.");
229 return true;
230 }
231 catch (std::exception const & ex)
232 {
233 MFATAL("Uncaught exception! " << ex.what());
234 return false;
235 }
236 catch (...)
237 {
238 MFATAL("Uncaught exception!");
239 return false;
240 }
241 }
242
243 void t_daemon::stop()
244 {
245 if (nullptr == mp_internals)
246 {
247 throw std::runtime_error{"Can't stop stopped daemon"};
248 }
249 mp_internals->p2p.stop();
250 for(auto& rpc : mp_internals->rpcs)
251 rpc->stop();
252
253 mp_internals.reset(nullptr); // Ensure resources are cleaned up before we return
254 }
255
256 void t_daemon::stop_p2p()
257 {
258 if (nullptr == mp_internals)
259 {
260 throw std::runtime_error{"Can't send stop signal to a stopped daemon"};
261 }
262 mp_internals->p2p.get().send_stop_signal();
263 }
264
265 } // namespace daemonize