diff --git a/cmd/pktvisord/main.cpp b/cmd/pktvisord/main.cpp index 4ef8876ba..85b7d9238 100644 --- a/cmd/pktvisord/main.cpp +++ b/cmd/pktvisord/main.cpp @@ -90,7 +90,8 @@ void initialize_geo(const docopt::value &city, const docopt::value &asn) // adapted from LPI becomeDaemon() int daemonize() { - switch (fork()) { + + switch (auto pid = fork()) { case -1: return -1; case 0: @@ -98,53 +99,37 @@ int daemonize() break; default: // while parent terminates + spdlog::get("pktvisor-daemon")->info("daemonized to PID {}", pid); _exit(EXIT_SUCCESS); } // Become leader of new session if (setsid() == -1) { + spdlog::get("pktvisor-daemon")->error("setsid() fail"); return -1; } - // Ensure we are not session leader - switch (auto pid = fork()) { - case -1: - return -1; - case 0: - break; - default: - std::cerr << "pktvisord running at PID " << pid << std::endl; - _exit(EXIT_SUCCESS); - } - // Clear file mode creation mask umask(0); // Change to root directory chdir("/"); - int maxfd, fd; - maxfd = sysconf(_SC_OPEN_MAX); - // Limit is indeterminate... - if (maxfd == -1) { - maxfd = 8192; // so take a guess - } - - for (fd = 0; fd < maxfd; fd++) { - close(fd); - } // Reopen standard fd's to /dev/null close(STDIN_FILENO); - fd = open("/dev/null", O_RDWR); + int fd = open("/dev/null", O_RDWR); if (fd != STDIN_FILENO) { + spdlog::get("pktvisor-daemon")->error("open() fail"); return -1; } if (dup2(STDIN_FILENO, STDOUT_FILENO) != STDOUT_FILENO) { + spdlog::get("pktvisor-daemon")->error("dup2 fail (STDOUT)"); return -1; } if (dup2(STDIN_FILENO, STDERR_FILENO) != STDERR_FILENO) { + spdlog::get("pktvisor-daemon")->error("dup2 fail (STDERR)"); return -1; } @@ -160,13 +145,16 @@ int main(int argc, char *argv[]) VISOR_VERSION); // version string if (args["-d"].asBool()) { + auto dlogger = spdlog::stderr_color_st("pktvisor-daemon"); + dlogger->flush_on(spdlog::level::info); if (daemonize()) { - std::cerr << "failed to daemonize" << std::endl; + dlogger->error("failed to daemonize"); exit(EXIT_FAILURE); } } std::shared_ptr logger; + spdlog::flush_on(spdlog::level::err); if (args["--log-file"]) { try { logger = spdlog::basic_logger_mt("pktvisor", args["--log-file"].asString());