diff --git a/nano/nano_node/entry.cpp b/nano/nano_node/entry.cpp index c86228a04b..417a4bbb41 100644 --- a/nano/nano_node/entry.cpp +++ b/nano/nano_node/entry.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #ifdef _WIN32 @@ -55,7 +56,38 @@ class address_library_pair bool operator< (const address_library_pair & other) const; bool operator== (const address_library_pair & other) const; }; - +std::filesystem::path pid_file; +void remove_pid_file () +{ + std::error_code ec; + std::filesystem::remove (pid_file, ec); + if (ec) + { + std::cerr << "Unable to remove pid file: " << ec.message (); + } +} +void register_pid_file () +{ + std::error_code ec; + auto pid = boost::this_process::get_id (); + std::filesystem::create_directories (pid_file.parent_path (), ec); + if (ec) + { + std::cerr << "Unable to access PID file path" << std::endl; + return; + } + std::ofstream pid_file_stream (pid_file, std::ios::out | std::ios::trunc); + if (pid_file_stream.is_open ()) + { + pid_file_stream << std::to_string (pid) << std::endl; + pid_file_stream.close (); + std::atexit (remove_pid_file); + } + else + { + std::cerr << "Unable to open PID file for writing." << std::endl; + } +} } int main (int argc, char * const * argv) @@ -113,7 +145,8 @@ int main (int argc, char * const * argv) ("count", boost::program_options::value (), "Defines for various commands") ("pow_sleep_interval", boost::program_options::value (), "Defines the amount to sleep inbetween each pow calculation attempt") ("address_column", boost::program_options::value (), "Defines which column the addresses are located, 0 indexed (check --debug_output_last_backtrace_dump output)") - ("silent", "Silent command execution"); + ("silent", "Silent command execution") + ("pid_file", boost::program_options::value (), "If present, node will write its process id to the specified file and delete the file upon exit"); // clang-format on nano::add_node_options (description); nano::add_node_flag_options (description); @@ -141,6 +174,12 @@ int main (int argc, char * const * argv) } } + if (auto existing = vm.find ("pid_file"); existing != vm.end ()) + { + pid_file = existing->second.as (); + register_pid_file (); + } + nano::network_params network_params{ nano::network_constants::active_network }; auto data_path_it = vm.find ("data_path"); std::filesystem::path data_path ((data_path_it != vm.end ()) ? std::filesystem::path (data_path_it->second.as ()) : nano::working_path ());