From 4b2ea32eacaeb5d266bc357d89d265912141fb9d Mon Sep 17 00:00:00 2001 From: Lorenzo Fontana Date: Thu, 18 Jul 2019 22:43:17 +0000 Subject: [PATCH] fix: do the inspector after forking for daemon mode Signed-off-by: Lorenzo Fontana --- userspace/falco/falco.cpp | 114 +++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/userspace/falco/falco.cpp b/userspace/falco/falco.cpp index 715a9aba492..d628039c5ae 100644 --- a/userspace/falco/falco.cpp +++ b/userspace/falco/falco.cpp @@ -915,6 +915,63 @@ int falco_init(int argc, char **argv) goto exit; } + // If daemonizing, do it here so any init errors will + // be returned in the foreground process. + if (daemon && !g_daemonized) { + pid_t pid, sid; + + pid = fork(); + if (pid < 0) { + // error + falco_logger::log(LOG_ERR, "Could not fork. Exiting.\n"); + result = EXIT_FAILURE; + goto exit; + } else if (pid > 0) { + // parent. Write child pid to pidfile and exit + std::ofstream pidfile; + pidfile.open(pidfilename); + + if (!pidfile.good()) + { + falco_logger::log(LOG_ERR, "Could not write pid to pid file " + pidfilename + ". Exiting.\n"); + result = EXIT_FAILURE; + goto exit; + } + pidfile << pid; + pidfile.close(); + goto exit; + } + // if here, child. + + // Become own process group. + sid = setsid(); + if (sid < 0) { + falco_logger::log(LOG_ERR, "Could not set session id. Exiting.\n"); + result = EXIT_FAILURE; + goto exit; + } + + // Set umask so no files are world anything or group writable. + umask(027); + + // Change working directory to '/' + if ((chdir("/")) < 0) { + falco_logger::log(LOG_ERR, "Could not change working directory to '/'. Exiting.\n"); + result = EXIT_FAILURE; + goto exit; + } + + // Close stdin, stdout, stderr and reopen to /dev/null + close(0); + close(1); + close(2); + open("/dev/null", O_RDONLY); + open("/dev/null", O_RDWR); + open("/dev/null", O_RDWR); + + g_daemonized = true; + } + if (trace_filename.size()) { // Try to open the trace file as a sysdig @@ -979,63 +1036,6 @@ int falco_init(int argc, char **argv) inspector->start_dropping_mode(1); } - // If daemonizing, do it here so any init errors will - // be returned in the foreground process. - if (daemon && !g_daemonized) { - pid_t pid, sid; - - pid = fork(); - if (pid < 0) { - // error - falco_logger::log(LOG_ERR, "Could not fork. Exiting.\n"); - result = EXIT_FAILURE; - goto exit; - } else if (pid > 0) { - // parent. Write child pid to pidfile and exit - std::ofstream pidfile; - pidfile.open(pidfilename); - - if (!pidfile.good()) - { - falco_logger::log(LOG_ERR, "Could not write pid to pid file " + pidfilename + ". Exiting.\n"); - result = EXIT_FAILURE; - goto exit; - } - pidfile << pid; - pidfile.close(); - goto exit; - } - // if here, child. - - // Become own process group. - sid = setsid(); - if (sid < 0) { - falco_logger::log(LOG_ERR, "Could not set session id. Exiting.\n"); - result = EXIT_FAILURE; - goto exit; - } - - // Set umask so no files are world anything or group writable. - umask(027); - - // Change working directory to '/' - if ((chdir("/")) < 0) { - falco_logger::log(LOG_ERR, "Could not change working directory to '/'. Exiting.\n"); - result = EXIT_FAILURE; - goto exit; - } - - // Close stdin, stdout, stderr and reopen to /dev/null - close(0); - close(1); - close(2); - open("/dev/null", O_RDONLY); - open("/dev/null", O_RDWR); - open("/dev/null", O_RDWR); - - g_daemonized = true; - } - if(outfile != "") { inspector->setup_cycle_writer(outfile, rollover_mb, duration_seconds, file_limit, event_limit, compress);