Loading etc/norns.conf.in +1 −1 Original line number Original line Diff line number Diff line Loading @@ -19,7 +19,7 @@ global_settings: [ pidfile: "@localstatedir@/urd.pid", pidfile: "@localstatedir@/urd.pid", # address to bind to # address to bind to bind_address: "127.0.0.1", bind_address: "127.0.0.1:42000", # incoming port for remote connections # incoming port for remote connections remote_port: 42000, remote_port: 42000, Loading src/config/settings.cpp +45 −25 Original line number Original line Diff line number Diff line Loading @@ -58,7 +58,7 @@ settings::settings(const std::string& progname, uint32_t dry_run_duration, uint32_t dry_run_duration, const bfs::path& global_socket, const bfs::path& global_socket, const bfs::path& control_socket, const bfs::path& control_socket, const std::string& bind_address, const std::string& configured_address, uint32_t remote_port, uint32_t remote_port, const bfs::path& pidfile, const bfs::path& pidfile, uint32_t workers, uint32_t workers, Loading @@ -76,7 +76,8 @@ settings::settings(const std::string& progname, m_dry_run_duration(dry_run_duration), m_dry_run_duration(dry_run_duration), m_global_socket(global_socket), m_global_socket(global_socket), m_control_socket(control_socket), m_control_socket(control_socket), m_bind_address(bind_address), m_configured_address(configured_address), m_lookup_address(), m_remote_port(remote_port), m_remote_port(remote_port), m_daemon_pidfile(pidfile), m_daemon_pidfile(pidfile), m_workers_in_pool(workers), m_workers_in_pool(workers), Loading @@ -97,7 +98,8 @@ settings::load_defaults() { m_dry_run_duration = defaults::dry_run_duration; m_dry_run_duration = defaults::dry_run_duration; m_global_socket = defaults::global_socket; m_global_socket = defaults::global_socket; m_control_socket = defaults::control_socket; m_control_socket = defaults::control_socket; m_bind_address = defaults::bind_address; m_configured_address = defaults::bind_address; m_lookup_address.clear(); m_remote_port = defaults::remote_port; m_remote_port = defaults::remote_port; m_daemon_pidfile = defaults::pidfile; m_daemon_pidfile = defaults::pidfile; m_workers_in_pool = defaults::workers_in_pool; m_workers_in_pool = defaults::workers_in_pool; Loading Loading @@ -134,7 +136,9 @@ settings::load_from_file(const bfs::path& filename) { m_dry_run_duration = defaults::dry_run_duration; m_dry_run_duration = defaults::dry_run_duration; m_global_socket = gsettings.get_as<bfs::path>(keywords::global_socket); m_global_socket = gsettings.get_as<bfs::path>(keywords::global_socket); m_control_socket = gsettings.get_as<bfs::path>(keywords::control_socket); m_control_socket = gsettings.get_as<bfs::path>(keywords::control_socket); m_bind_address = gsettings.get_as<std::string>(keywords::bind_address); m_configured_address = gsettings.get_as<std::string>(keywords::bind_address); m_lookup_address.clear(); m_remote_port = gsettings.get_as<uint32_t>(keywords::remote_port); m_remote_port = gsettings.get_as<uint32_t>(keywords::remote_port); m_daemon_pidfile = gsettings.get_as<bfs::path>(keywords::pidfile); m_daemon_pidfile = gsettings.get_as<bfs::path>(keywords::pidfile); m_workers_in_pool = gsettings.get_as<uint32_t>(keywords::workers); m_workers_in_pool = gsettings.get_as<uint32_t>(keywords::workers); Loading @@ -159,18 +163,24 @@ settings::load_from_file(const bfs::path& filename) { std::string std::string settings::to_string() const { settings::to_string() const { std::string str = std::string("settings {\n") + std::string str = std::string("settings {\n") + " m_progname: " + m_progname + ",\n" + " m_progname: " + m_progname + ",\n" + " m_daemonize: " + (m_daemonize ? "true" : "false") + ",\n" + " m_daemonize: " + (m_daemonize ? "true" : "false") + ",\n" + " m_use_syslog: " + (m_use_syslog ? "true" : "false") + ",\n" + " m_use_syslog: " + (m_use_syslog ? "true" : "false") + ",\n" + " m_use_console: " + (m_use_console ? "true" : "false") + ",\n" + " m_use_console: " + (m_use_console ? "true" : "false") + ",\n" + " m_log_file: " + m_log_file.string() + ",\n" + " m_log_file: " + m_log_file.string() + ",\n" + " m_log_file_max_size: " + std::to_string(m_log_file_max_size) + ",\n" + " m_log_file_max_size: " + std::to_string(m_log_file_max_size) + ",\n" + " m_dry_run: " + (m_dry_run ? "true" : "false") + ",\n" + " m_dry_run: " + (m_dry_run ? "true" : "false") + ",\n" + " m_dry_run_duration: " + std::to_string(m_dry_run_duration) + ",\n" + " m_dry_run_duration: " + std::to_string(m_dry_run_duration) + ",\n" + " m_global_socket: " + m_global_socket.string() + ",\n" + " m_global_socket: " + m_global_socket.string() + ",\n" + " m_control_socket: " + m_control_socket.string() + ",\n" + " m_control_socket: " + m_control_socket.string() + ",\n" + " m_bind_address: " + m_bind_address + ",\n" + " m_configured_address: " + m_configured_address + ",\n" + " m_lookup_address: " + (m_lookup_address.empty() ? m_lookup_address : "{undetermined}") + ",\n" + " m_remote_port: " + std::to_string(m_remote_port) + ",\n" + " m_remote_port: " + std::to_string(m_remote_port) + ",\n" + " m_pidfile: " + m_daemon_pidfile.string() + ",\n" + " m_pidfile: " + m_daemon_pidfile.string() + ",\n" + " m_workers: " + std::to_string(m_workers_in_pool) + ",\n" + " m_workers: " + std::to_string(m_workers_in_pool) + ",\n" + Loading Loading @@ -283,13 +293,23 @@ settings::control_socket(const bfs::path& control_socket) { } } std::string std::string settings::bind_address() const { settings::configured_address() const { return m_bind_address; return m_configured_address; } } void void settings::bind_address(const std::string& bind_address) { settings::configured_address(const std::string& configured_address) { m_bind_address = bind_address; m_configured_address = configured_address; } std::string settings::lookup_address() const { return m_lookup_address; } void settings::lookup_address(const std::string& lookup_address) { m_lookup_address = lookup_address; } } in_port_t in_port_t Loading src/config/settings.hpp +11 −4 Original line number Original line Diff line number Diff line Loading @@ -114,7 +114,7 @@ struct settings { uint32_t dry_run_duration, uint32_t dry_run_duration, const bfs::path& global_socket, const bfs::path& global_socket, const bfs::path& control_socket, const bfs::path& control_socket, const std::string& bind_address, const std::string& configured_address, uint32_t remote_port, uint32_t remote_port, const bfs::path& pidfile, const bfs::path& pidfile, uint32_t workers, uint32_t workers, Loading Loading @@ -205,10 +205,16 @@ struct settings { control_socket(const bfs::path& control_socket); control_socket(const bfs::path& control_socket); std::string std::string bind_address() const; configured_address() const; void void bind_address(const std::string& bind_address); configured_address(const std::string& configured_address); std::string lookup_address() const; void lookup_address(const std::string& lookup_address); in_port_t in_port_t remote_port() const; remote_port() const; Loading Loading @@ -262,7 +268,8 @@ struct settings { uint32_t m_dry_run_duration; uint32_t m_dry_run_duration; bfs::path m_global_socket; bfs::path m_global_socket; bfs::path m_control_socket; bfs::path m_control_socket; std::string m_bind_address; std::string m_configured_address; std::string m_lookup_address; in_port_t m_remote_port; in_port_t m_remote_port; bfs::path m_daemon_pidfile; bfs::path m_daemon_pidfile; uint32_t m_workers_in_pool; uint32_t m_workers_in_pool; Loading src/urd.cpp +87 −44 Original line number Original line Diff line number Diff line Loading @@ -63,8 +63,67 @@ #include "hermes.hpp" #include "hermes.hpp" #include "rpcs.hpp" #include "rpcs.hpp" #include "context.hpp" #include "context.hpp" #include "utils/file-handle.hpp" #include "urd.hpp" #include "urd.hpp" namespace { void write_pidfile(const bfs::path& pidfile) { /* Check if daemon already exists: * First instance of the daemon will lock the file so that other * instances understand that an instance is already running. */ norns::utils::file_handle fh( ::open(pidfile.c_str(), O_RDWR | O_CREAT, 0640)); if(!fh) { LOGGER_ERRNO("Failed to create daemon lock file"); exit(EXIT_FAILURE); } if(::lockf(fh.native(), F_TLOCK, 0) < 0) { LOGGER_ERRNO("Failed to acquire lock on pidfile"); LOGGER_ERROR("Another instance of this daemon may already be running"); exit(EXIT_FAILURE); } /* record pid in lockfile */ std::string pid(std::to_string(::getpid())); ssize_t n = static_cast<ssize_t>(pid.length()); if(::write(fh.native(), pid.c_str(), n) != n) { LOGGER_ERRNO("Failed to write pidfile"); exit(EXIT_FAILURE); } } bool append_to_pidfile(const bfs::path& pidfile, const std::string& entry) { const std::string tabbed_entry("\n" + entry + "\n"); norns::utils::file_handle fh( ::open(pidfile.c_str(), O_WRONLY | O_APPEND, 0640)); if(!fh) { return false; } ssize_t n = static_cast<ssize_t>(tabbed_entry.length()); if(::write(fh.native(), tabbed_entry.c_str(), n) != n) { return false; } return true; } } namespace norns { namespace norns { urd::urd() : urd::urd() : Loading Loading @@ -164,29 +223,8 @@ pid_t urd::daemonize() { * First instance of the daemon will lock the file so that other * First instance of the daemon will lock the file so that other * instances understand that an instance is already running. * instances understand that an instance is already running. */ */ int pfd; ::write_pidfile(m_settings->pidfile()); if((pfd = open(m_settings->pidfile().c_str(), O_RDWR | O_CREAT, 0640)) == -1) { LOGGER_ERRNO("Failed to create daemon lock file"); exit(EXIT_FAILURE); } if(lockf(pfd, F_TLOCK, 0) < 0) { LOGGER_ERRNO("Failed to acquire lock on pidfile"); LOGGER_ERROR("Another instance of this daemon may already be running"); exit(EXIT_FAILURE); } /* record pid in lockfile */ std::string pidstr(std::to_string(getpid())); if(write(pfd, pidstr.c_str(), pidstr.length()) != static_cast<ssize_t>(pidstr.length())) { LOGGER_ERRNO("Failed to write pidfile"); exit(EXIT_FAILURE); } close(pfd); close(dev_null); close(dev_null); /* Manage signals */ /* Manage signals */ Loading Loading @@ -1130,15 +1168,10 @@ void urd::init_event_handlers() { ::umask(old_mask); ::umask(old_mask); try { try { const std::string bind_address = m_settings->bind_address() + ":" + std::to_string(m_settings->remote_port()); m_network_service = m_network_service = std::make_shared<hermes::async_engine>( std::make_shared<hermes::async_engine>( hermes::transport::ofi_tcp, hermes::transport::ofi_tcp, bind_address, m_settings->configured_address(), true); true); } } catch(const std::exception& e) { catch(const std::exception& e) { Loading @@ -1147,20 +1180,6 @@ void urd::init_event_handlers() { exit(EXIT_FAILURE); exit(EXIT_FAILURE); } } #if 0 // setup socket for remote connections try { m_ipc_service->register_endpoint(m_settings->remote_port()); } catch(const std::exception& e) { LOGGER_ERROR("Failed to create socket for remote connections: {}", e.what()); teardown(); exit(EXIT_FAILURE); } #endif LOGGER_INFO(" * Installing message handlers..."); LOGGER_INFO(" * Installing message handlers..."); /* user-level functionalities */ /* user-level functionalities */ Loading Loading @@ -1466,7 +1485,8 @@ void urd::print_greeting() { LOGGER_INFO("{}", gsep); LOGGER_INFO("{}", gsep); } } void urd::print_configuration() { void urd::print_configuration() { LOGGER_INFO(""); LOGGER_INFO(""); LOGGER_INFO("[[ Configuration ]]"); LOGGER_INFO("[[ Configuration ]]"); LOGGER_INFO(" - running as daemon?: {}", (m_settings->daemonize() ? "yes" : "no")); LOGGER_INFO(" - running as daemon?: {}", (m_settings->daemonize() ? "yes" : "no")); Loading @@ -1485,11 +1505,25 @@ void urd::print_configuration() { LOGGER_INFO(" - control socket: {}", m_settings->control_socket()); LOGGER_INFO(" - control socket: {}", m_settings->control_socket()); LOGGER_INFO(" - global socket: {}", m_settings->global_socket()); LOGGER_INFO(" - global socket: {}", m_settings->global_socket()); LOGGER_INFO(" - staging directory: {}", m_settings->staging_directory()); LOGGER_INFO(" - staging directory: {}", m_settings->staging_directory()); LOGGER_INFO(" - port for remote requests: {}", m_settings->remote_port()); LOGGER_INFO(" - bind address: {}", m_settings->configured_address()); LOGGER_INFO(" - workers: {}", m_settings->workers_in_pool()); LOGGER_INFO(" - workers: {}", m_settings->workers_in_pool()); LOGGER_INFO(""); LOGGER_INFO(""); } } void urd::store_runtime_configuration() { const std::string addr(m_network_service->self_address()); m_settings->lookup_address(addr); if(!::append_to_pidfile(m_settings->pidfile(), addr)) { LOGGER_ERRNO("Failed to append entry to pidfile"); exit(EXIT_FAILURE); } } void urd::print_farewell() { void urd::print_farewell() { const char farewell[] = "Stopping {} daemon (pid {})"; const char farewell[] = "Stopping {} daemon (pid {})"; const auto fsep = std::string(sizeof(farewell) - 4 + const auto fsep = std::string(sizeof(farewell) - 4 + Loading Loading @@ -1570,6 +1604,11 @@ int urd::run() { teardown(); teardown(); return EXIT_SUCCESS; return EXIT_SUCCESS; } } else { /* keep a pidfile even in foreground mode so that we can detect * concurrent daemons with the same configuration */ ::write_pidfile(m_settings->pidfile()); } // print useful information // print useful information print_greeting(); print_greeting(); Loading @@ -1587,12 +1626,16 @@ int urd::run() { // everything is set up // everything is set up load_transfer_plugins(); load_transfer_plugins(); // store runtime values of several settings in m_settings store_runtime_configuration(); // start the listener for remote transfers // start the listener for remote transfers // N.B. This call returns immediately // N.B. This call returns immediately m_network_service->run(); m_network_service->run(); LOGGER_INFO(""); LOGGER_INFO(""); LOGGER_INFO("[[ Start up successful, awaiting requests... ]]"); LOGGER_INFO("[[ Start up successful, awaiting requests... ]]"); LOGGER_INFO("[[ (public address: {}) ]]", m_settings->lookup_address()); // N.B. This call blocks here, which means that everything after it // N.B. This call blocks here, which means that everything after it // will only run when a shutdown command is received // will only run when a shutdown command is received Loading src/urd.hpp +4 −0 Original line number Original line Diff line number Diff line Loading @@ -103,6 +103,10 @@ private: void load_transfer_plugins(); void load_transfer_plugins(); void load_default_namespaces(); void load_default_namespaces(); void check_configuration(); void check_configuration(); void store_runtime_configuration(); void print_greeting(); void print_greeting(); void print_configuration(); void print_configuration(); void print_farewell(); void print_farewell(); Loading Loading
etc/norns.conf.in +1 −1 Original line number Original line Diff line number Diff line Loading @@ -19,7 +19,7 @@ global_settings: [ pidfile: "@localstatedir@/urd.pid", pidfile: "@localstatedir@/urd.pid", # address to bind to # address to bind to bind_address: "127.0.0.1", bind_address: "127.0.0.1:42000", # incoming port for remote connections # incoming port for remote connections remote_port: 42000, remote_port: 42000, Loading
src/config/settings.cpp +45 −25 Original line number Original line Diff line number Diff line Loading @@ -58,7 +58,7 @@ settings::settings(const std::string& progname, uint32_t dry_run_duration, uint32_t dry_run_duration, const bfs::path& global_socket, const bfs::path& global_socket, const bfs::path& control_socket, const bfs::path& control_socket, const std::string& bind_address, const std::string& configured_address, uint32_t remote_port, uint32_t remote_port, const bfs::path& pidfile, const bfs::path& pidfile, uint32_t workers, uint32_t workers, Loading @@ -76,7 +76,8 @@ settings::settings(const std::string& progname, m_dry_run_duration(dry_run_duration), m_dry_run_duration(dry_run_duration), m_global_socket(global_socket), m_global_socket(global_socket), m_control_socket(control_socket), m_control_socket(control_socket), m_bind_address(bind_address), m_configured_address(configured_address), m_lookup_address(), m_remote_port(remote_port), m_remote_port(remote_port), m_daemon_pidfile(pidfile), m_daemon_pidfile(pidfile), m_workers_in_pool(workers), m_workers_in_pool(workers), Loading @@ -97,7 +98,8 @@ settings::load_defaults() { m_dry_run_duration = defaults::dry_run_duration; m_dry_run_duration = defaults::dry_run_duration; m_global_socket = defaults::global_socket; m_global_socket = defaults::global_socket; m_control_socket = defaults::control_socket; m_control_socket = defaults::control_socket; m_bind_address = defaults::bind_address; m_configured_address = defaults::bind_address; m_lookup_address.clear(); m_remote_port = defaults::remote_port; m_remote_port = defaults::remote_port; m_daemon_pidfile = defaults::pidfile; m_daemon_pidfile = defaults::pidfile; m_workers_in_pool = defaults::workers_in_pool; m_workers_in_pool = defaults::workers_in_pool; Loading Loading @@ -134,7 +136,9 @@ settings::load_from_file(const bfs::path& filename) { m_dry_run_duration = defaults::dry_run_duration; m_dry_run_duration = defaults::dry_run_duration; m_global_socket = gsettings.get_as<bfs::path>(keywords::global_socket); m_global_socket = gsettings.get_as<bfs::path>(keywords::global_socket); m_control_socket = gsettings.get_as<bfs::path>(keywords::control_socket); m_control_socket = gsettings.get_as<bfs::path>(keywords::control_socket); m_bind_address = gsettings.get_as<std::string>(keywords::bind_address); m_configured_address = gsettings.get_as<std::string>(keywords::bind_address); m_lookup_address.clear(); m_remote_port = gsettings.get_as<uint32_t>(keywords::remote_port); m_remote_port = gsettings.get_as<uint32_t>(keywords::remote_port); m_daemon_pidfile = gsettings.get_as<bfs::path>(keywords::pidfile); m_daemon_pidfile = gsettings.get_as<bfs::path>(keywords::pidfile); m_workers_in_pool = gsettings.get_as<uint32_t>(keywords::workers); m_workers_in_pool = gsettings.get_as<uint32_t>(keywords::workers); Loading @@ -159,18 +163,24 @@ settings::load_from_file(const bfs::path& filename) { std::string std::string settings::to_string() const { settings::to_string() const { std::string str = std::string("settings {\n") + std::string str = std::string("settings {\n") + " m_progname: " + m_progname + ",\n" + " m_progname: " + m_progname + ",\n" + " m_daemonize: " + (m_daemonize ? "true" : "false") + ",\n" + " m_daemonize: " + (m_daemonize ? "true" : "false") + ",\n" + " m_use_syslog: " + (m_use_syslog ? "true" : "false") + ",\n" + " m_use_syslog: " + (m_use_syslog ? "true" : "false") + ",\n" + " m_use_console: " + (m_use_console ? "true" : "false") + ",\n" + " m_use_console: " + (m_use_console ? "true" : "false") + ",\n" + " m_log_file: " + m_log_file.string() + ",\n" + " m_log_file: " + m_log_file.string() + ",\n" + " m_log_file_max_size: " + std::to_string(m_log_file_max_size) + ",\n" + " m_log_file_max_size: " + std::to_string(m_log_file_max_size) + ",\n" + " m_dry_run: " + (m_dry_run ? "true" : "false") + ",\n" + " m_dry_run: " + (m_dry_run ? "true" : "false") + ",\n" + " m_dry_run_duration: " + std::to_string(m_dry_run_duration) + ",\n" + " m_dry_run_duration: " + std::to_string(m_dry_run_duration) + ",\n" + " m_global_socket: " + m_global_socket.string() + ",\n" + " m_global_socket: " + m_global_socket.string() + ",\n" + " m_control_socket: " + m_control_socket.string() + ",\n" + " m_control_socket: " + m_control_socket.string() + ",\n" + " m_bind_address: " + m_bind_address + ",\n" + " m_configured_address: " + m_configured_address + ",\n" + " m_lookup_address: " + (m_lookup_address.empty() ? m_lookup_address : "{undetermined}") + ",\n" + " m_remote_port: " + std::to_string(m_remote_port) + ",\n" + " m_remote_port: " + std::to_string(m_remote_port) + ",\n" + " m_pidfile: " + m_daemon_pidfile.string() + ",\n" + " m_pidfile: " + m_daemon_pidfile.string() + ",\n" + " m_workers: " + std::to_string(m_workers_in_pool) + ",\n" + " m_workers: " + std::to_string(m_workers_in_pool) + ",\n" + Loading Loading @@ -283,13 +293,23 @@ settings::control_socket(const bfs::path& control_socket) { } } std::string std::string settings::bind_address() const { settings::configured_address() const { return m_bind_address; return m_configured_address; } } void void settings::bind_address(const std::string& bind_address) { settings::configured_address(const std::string& configured_address) { m_bind_address = bind_address; m_configured_address = configured_address; } std::string settings::lookup_address() const { return m_lookup_address; } void settings::lookup_address(const std::string& lookup_address) { m_lookup_address = lookup_address; } } in_port_t in_port_t Loading
src/config/settings.hpp +11 −4 Original line number Original line Diff line number Diff line Loading @@ -114,7 +114,7 @@ struct settings { uint32_t dry_run_duration, uint32_t dry_run_duration, const bfs::path& global_socket, const bfs::path& global_socket, const bfs::path& control_socket, const bfs::path& control_socket, const std::string& bind_address, const std::string& configured_address, uint32_t remote_port, uint32_t remote_port, const bfs::path& pidfile, const bfs::path& pidfile, uint32_t workers, uint32_t workers, Loading Loading @@ -205,10 +205,16 @@ struct settings { control_socket(const bfs::path& control_socket); control_socket(const bfs::path& control_socket); std::string std::string bind_address() const; configured_address() const; void void bind_address(const std::string& bind_address); configured_address(const std::string& configured_address); std::string lookup_address() const; void lookup_address(const std::string& lookup_address); in_port_t in_port_t remote_port() const; remote_port() const; Loading Loading @@ -262,7 +268,8 @@ struct settings { uint32_t m_dry_run_duration; uint32_t m_dry_run_duration; bfs::path m_global_socket; bfs::path m_global_socket; bfs::path m_control_socket; bfs::path m_control_socket; std::string m_bind_address; std::string m_configured_address; std::string m_lookup_address; in_port_t m_remote_port; in_port_t m_remote_port; bfs::path m_daemon_pidfile; bfs::path m_daemon_pidfile; uint32_t m_workers_in_pool; uint32_t m_workers_in_pool; Loading
src/urd.cpp +87 −44 Original line number Original line Diff line number Diff line Loading @@ -63,8 +63,67 @@ #include "hermes.hpp" #include "hermes.hpp" #include "rpcs.hpp" #include "rpcs.hpp" #include "context.hpp" #include "context.hpp" #include "utils/file-handle.hpp" #include "urd.hpp" #include "urd.hpp" namespace { void write_pidfile(const bfs::path& pidfile) { /* Check if daemon already exists: * First instance of the daemon will lock the file so that other * instances understand that an instance is already running. */ norns::utils::file_handle fh( ::open(pidfile.c_str(), O_RDWR | O_CREAT, 0640)); if(!fh) { LOGGER_ERRNO("Failed to create daemon lock file"); exit(EXIT_FAILURE); } if(::lockf(fh.native(), F_TLOCK, 0) < 0) { LOGGER_ERRNO("Failed to acquire lock on pidfile"); LOGGER_ERROR("Another instance of this daemon may already be running"); exit(EXIT_FAILURE); } /* record pid in lockfile */ std::string pid(std::to_string(::getpid())); ssize_t n = static_cast<ssize_t>(pid.length()); if(::write(fh.native(), pid.c_str(), n) != n) { LOGGER_ERRNO("Failed to write pidfile"); exit(EXIT_FAILURE); } } bool append_to_pidfile(const bfs::path& pidfile, const std::string& entry) { const std::string tabbed_entry("\n" + entry + "\n"); norns::utils::file_handle fh( ::open(pidfile.c_str(), O_WRONLY | O_APPEND, 0640)); if(!fh) { return false; } ssize_t n = static_cast<ssize_t>(tabbed_entry.length()); if(::write(fh.native(), tabbed_entry.c_str(), n) != n) { return false; } return true; } } namespace norns { namespace norns { urd::urd() : urd::urd() : Loading Loading @@ -164,29 +223,8 @@ pid_t urd::daemonize() { * First instance of the daemon will lock the file so that other * First instance of the daemon will lock the file so that other * instances understand that an instance is already running. * instances understand that an instance is already running. */ */ int pfd; ::write_pidfile(m_settings->pidfile()); if((pfd = open(m_settings->pidfile().c_str(), O_RDWR | O_CREAT, 0640)) == -1) { LOGGER_ERRNO("Failed to create daemon lock file"); exit(EXIT_FAILURE); } if(lockf(pfd, F_TLOCK, 0) < 0) { LOGGER_ERRNO("Failed to acquire lock on pidfile"); LOGGER_ERROR("Another instance of this daemon may already be running"); exit(EXIT_FAILURE); } /* record pid in lockfile */ std::string pidstr(std::to_string(getpid())); if(write(pfd, pidstr.c_str(), pidstr.length()) != static_cast<ssize_t>(pidstr.length())) { LOGGER_ERRNO("Failed to write pidfile"); exit(EXIT_FAILURE); } close(pfd); close(dev_null); close(dev_null); /* Manage signals */ /* Manage signals */ Loading Loading @@ -1130,15 +1168,10 @@ void urd::init_event_handlers() { ::umask(old_mask); ::umask(old_mask); try { try { const std::string bind_address = m_settings->bind_address() + ":" + std::to_string(m_settings->remote_port()); m_network_service = m_network_service = std::make_shared<hermes::async_engine>( std::make_shared<hermes::async_engine>( hermes::transport::ofi_tcp, hermes::transport::ofi_tcp, bind_address, m_settings->configured_address(), true); true); } } catch(const std::exception& e) { catch(const std::exception& e) { Loading @@ -1147,20 +1180,6 @@ void urd::init_event_handlers() { exit(EXIT_FAILURE); exit(EXIT_FAILURE); } } #if 0 // setup socket for remote connections try { m_ipc_service->register_endpoint(m_settings->remote_port()); } catch(const std::exception& e) { LOGGER_ERROR("Failed to create socket for remote connections: {}", e.what()); teardown(); exit(EXIT_FAILURE); } #endif LOGGER_INFO(" * Installing message handlers..."); LOGGER_INFO(" * Installing message handlers..."); /* user-level functionalities */ /* user-level functionalities */ Loading Loading @@ -1466,7 +1485,8 @@ void urd::print_greeting() { LOGGER_INFO("{}", gsep); LOGGER_INFO("{}", gsep); } } void urd::print_configuration() { void urd::print_configuration() { LOGGER_INFO(""); LOGGER_INFO(""); LOGGER_INFO("[[ Configuration ]]"); LOGGER_INFO("[[ Configuration ]]"); LOGGER_INFO(" - running as daemon?: {}", (m_settings->daemonize() ? "yes" : "no")); LOGGER_INFO(" - running as daemon?: {}", (m_settings->daemonize() ? "yes" : "no")); Loading @@ -1485,11 +1505,25 @@ void urd::print_configuration() { LOGGER_INFO(" - control socket: {}", m_settings->control_socket()); LOGGER_INFO(" - control socket: {}", m_settings->control_socket()); LOGGER_INFO(" - global socket: {}", m_settings->global_socket()); LOGGER_INFO(" - global socket: {}", m_settings->global_socket()); LOGGER_INFO(" - staging directory: {}", m_settings->staging_directory()); LOGGER_INFO(" - staging directory: {}", m_settings->staging_directory()); LOGGER_INFO(" - port for remote requests: {}", m_settings->remote_port()); LOGGER_INFO(" - bind address: {}", m_settings->configured_address()); LOGGER_INFO(" - workers: {}", m_settings->workers_in_pool()); LOGGER_INFO(" - workers: {}", m_settings->workers_in_pool()); LOGGER_INFO(""); LOGGER_INFO(""); } } void urd::store_runtime_configuration() { const std::string addr(m_network_service->self_address()); m_settings->lookup_address(addr); if(!::append_to_pidfile(m_settings->pidfile(), addr)) { LOGGER_ERRNO("Failed to append entry to pidfile"); exit(EXIT_FAILURE); } } void urd::print_farewell() { void urd::print_farewell() { const char farewell[] = "Stopping {} daemon (pid {})"; const char farewell[] = "Stopping {} daemon (pid {})"; const auto fsep = std::string(sizeof(farewell) - 4 + const auto fsep = std::string(sizeof(farewell) - 4 + Loading Loading @@ -1570,6 +1604,11 @@ int urd::run() { teardown(); teardown(); return EXIT_SUCCESS; return EXIT_SUCCESS; } } else { /* keep a pidfile even in foreground mode so that we can detect * concurrent daemons with the same configuration */ ::write_pidfile(m_settings->pidfile()); } // print useful information // print useful information print_greeting(); print_greeting(); Loading @@ -1587,12 +1626,16 @@ int urd::run() { // everything is set up // everything is set up load_transfer_plugins(); load_transfer_plugins(); // store runtime values of several settings in m_settings store_runtime_configuration(); // start the listener for remote transfers // start the listener for remote transfers // N.B. This call returns immediately // N.B. This call returns immediately m_network_service->run(); m_network_service->run(); LOGGER_INFO(""); LOGGER_INFO(""); LOGGER_INFO("[[ Start up successful, awaiting requests... ]]"); LOGGER_INFO("[[ Start up successful, awaiting requests... ]]"); LOGGER_INFO("[[ (public address: {}) ]]", m_settings->lookup_address()); // N.B. This call blocks here, which means that everything after it // N.B. This call blocks here, which means that everything after it // will only run when a shutdown command is received // will only run when a shutdown command is received Loading
src/urd.hpp +4 −0 Original line number Original line Diff line number Diff line Loading @@ -103,6 +103,10 @@ private: void load_transfer_plugins(); void load_transfer_plugins(); void load_default_namespaces(); void load_default_namespaces(); void check_configuration(); void check_configuration(); void store_runtime_configuration(); void print_greeting(); void print_greeting(); void print_configuration(); void print_configuration(); void print_farewell(); void print_farewell(); Loading