From 360f9109e002b98f12af3b2237c7d980b5b596b6 Mon Sep 17 00:00:00 2001 From: "Russ Combs (rucombs)" Date: Thu, 15 Oct 2015 12:16:09 -0400 Subject: [PATCH] Merge pull request #80 in SNORT/snort3 from crc/daemon to master Squashed commit of the following: commit 4cc0fa54863c63fc7ff18495a890e65f2e90af60 Author: russ Date: Tue Oct 13 07:57:23 2015 -0400 decouple -D, -M, and -q fix daemonization don't create pid file unless requested remove pid lock file --- src/helpers/process.cc | 140 +++++++++++++------------------------- src/log/messages.cc | 6 +- src/main/snort.cc | 6 +- src/main/snort_config.cc | 7 -- src/parser/cmd_line.cc | 8 --- src/parser/config_file.cc | 7 -- src/utils/util.cc | 6 +- 7 files changed, 57 insertions(+), 123 deletions(-) diff --git a/src/helpers/process.cc b/src/helpers/process.cc index 15b1e9852..da23bd4f2 100644 --- a/src/helpers/process.cc +++ b/src/helpers/process.cc @@ -306,123 +306,79 @@ static void signal_waiting_parent(void) } } +static void snuff_stdio() +{ + errno = 0; + bool err = false; + + err = close(0) || err; + err = close(1) || err; + err = close(2) || err; + + /* redirect stdin/stdout/stderr to /dev/null */ + const char* file = "/dev/null"; + err = open(file, O_RDWR) || err; /* stdin, fd 0 */ + + err = dup(0) || err; /* stdout, fd 0 => fd 1 */ + err = dup(0) || err; /* stderr, fd 0 => fd 2 */ + + if ( err ) + perror("daemonization errors"); +} + /* All threads need to be created after daemonizing. If created in * the parent thread, when it goes away, so will all of the threads. * The child does not "inherit" threads created in the parent. */ void daemonize() { - int exit_val = 0; pid_t cpid; - if (SnortConfig::daemon_restart()) + if ( SnortConfig::daemon_restart() ) return; - LogMessage("Initializing daemon mode\n"); - /* Don't daemonize if we've already daemonized and * received a SIGNAL_SNORT_RELOAD. */ - if (getppid() != 1) - { - /* Register signal handler that parent can trap signal */ - add_signal(SIGNAL_SNORT_CHILD_READY, child_ready_handler, 1); + if ( getppid() == 1 ) + return; - if (errno != 0) - errno = 0; + LogMessage("Initializing daemon mode\n"); - /* now fork the child */ - printf("Spawning daemon child...\n"); - cpid = fork(); + /* Register signal handler so that parent can trap signal */ + add_signal(SIGNAL_SNORT_CHILD_READY, child_ready_handler, 1); - if (cpid > 0) - { - /* Continue waiting until receiving signal from child */ - int status; - /* Parent */ - printf("My daemon child %d lives...\n", cpid); - - /* Don't exit quite yet. Wait for the child - * to signal that is there and created the PID - * file. - */ - do - { -#ifdef DEBUG - printf("Parent waiting for child...\n"); -#endif - sleep(1); - } - while ( !child_ready_signal ); - - if (waitpid(cpid, &status, WNOHANG) == cpid) - { - if (WIFEXITED(status)) - { - LogMessage("Child exited unexpectedly\n"); - exit_val = -1; - } - else if (WIFSIGNALED(status)) - { - LogMessage("Child terminated unexpectedly\n"); - exit_val = -2; - } - } -#ifdef DEBUG - printf("Child terminated unexpectedly (%d)\n", status); -#endif - printf("Daemon parent exiting (%d)\n", exit_val); + if (errno != 0) + errno = 0; - exit(exit_val); /* parent */ - } + printf("Forking snort process\n"); + cpid = fork(); - if (cpid < 0) - { - /* Daemonizing failed... */ - perror("fork"); - exit(1); - } + if ( cpid < 0 ) + { + perror("Process fork failed"); + exit(1); } - /* Child */ - setsid(); - - errno = 0; - bool err = false; - - err = close(0) || err; - err = close(1) || err; - err = close(2) || err; - -#ifdef DEBUG - /* redirect stdin/stdout/stderr to a file */ - const int mode = S_IWUSR | S_IRUSR | S_IRGRP; - const char* file = "/tmp/snort.debug"; - err = open(file, O_CREAT | O_RDWR, mode) || err; /* stdin, fd 0 */ - - /* Change ownership to that which we will drop privileges to */ - if ((snort_conf->user_id != -1) || (snort_conf->group_id != -1)) + if ( cpid > 0 ) { - uid_t user_id = getuid(); - gid_t group_id = getgid(); + /* Parent */ + printf("Child process is %d\n", cpid); - if (snort_conf->user_id != -1) - user_id = snort_conf->user_id; - if (snort_conf->group_id != -1) - group_id = snort_conf->group_id; + while ( !child_ready_signal ) + { + printf("Waiting for child ready signal\n"); + sleep(1); + } - err = chown(file, user_id, group_id) || err; + printf("Parent process exiting\n"); + exit(0); } -#else - /* redirect stdin/stdout/stderr to /dev/null */ - const char* file = "/dev/null"; - err = open(file, O_RDWR) || err; /* stdin, fd 0 */ -#endif - err = dup(0) || err; /* stdout, fd 0 => fd 1 */ - err = dup(0) || err; /* stderr, fd 0 => fd 2 */ + /* Child */ + setsid(); - if ( err ) - perror("daemonization errors"); + if ( SnortConfig::log_quiet() or SnortConfig::log_syslog() ) + snuff_stdio(); signal_waiting_parent(); } diff --git a/src/log/messages.cc b/src/log/messages.cc index ee0476cc2..b9178a0c9 100644 --- a/src/log/messages.cc +++ b/src/log/messages.cc @@ -70,19 +70,19 @@ void LogMessage(const char* format,...) char buf[STD_BUF+1]; va_list ap; - if (snort_conf == NULL) + if ( !snort_conf ) { va_start(ap, format); vfprintf(stdout, format, ap); va_end(ap); return; } - if (SnortConfig::log_quiet() && !SnortConfig::daemon_mode() && !SnortConfig::log_syslog()) + if ( SnortConfig::log_quiet() ) return; va_start(ap, format); - if (SnortConfig::daemon_mode() || SnortConfig::log_syslog()) + if ( SnortConfig::log_syslog() ) { vsnprintf(buf, STD_BUF, format, ap); buf[STD_BUF] = '\0'; diff --git a/src/main/snort.cc b/src/main/snort.cc index f12ebe514..c261ccaf7 100644 --- a/src/main/snort.cc +++ b/src/main/snort.cc @@ -336,12 +336,8 @@ void Snort::init(int argc, char** argv) // opening iface void Snort::unprivileged_init() { - /* create the PID file */ - if ( !SnortConfig::read_mode() && - (SnortConfig::daemon_mode() || SnortConfig::create_pid_file())) - { + if ( SnortConfig::create_pid_file() ) CreatePidFile(snort_main_thread_pid); - } /* Drop the Chrooted Settings */ if ( !snort_conf->chroot_dir.empty() ) diff --git a/src/main/snort_config.cc b/src/main/snort_config.cc index ad412895f..5b46f2d72 100644 --- a/src/main/snort_config.cc +++ b/src/main/snort_config.cc @@ -315,13 +315,6 @@ void SnortConfig::merge(SnortConfig* cmd_line) output_flags |= cmd_line->output_flags; logging_flags |= cmd_line->logging_flags; - if ((cmd_line->run_flags & RUN_FLAG__TEST) && - (run_flags & RUN_FLAG__DAEMON)) - { - /* Just ignore deamon setting in conf file */ - run_flags &= ~RUN_FLAG__DAEMON; - } - stdin_rules = cmd_line->stdin_rules; // only set by cmd_line to override other conf output settings diff --git a/src/parser/cmd_line.cc b/src/parser/cmd_line.cc index fcbf485fd..2434b5bbe 100644 --- a/src/parser/cmd_line.cc +++ b/src/parser/cmd_line.cc @@ -40,14 +40,6 @@ using namespace std; static void check_flags(SnortConfig* sc) { - if ((sc->run_flags & RUN_FLAG__TEST) && - (sc->run_flags & RUN_FLAG__DAEMON)) - { - FatalError("Cannot use test mode and daemon mode together.\n" - "To verify configuration, run first in test " - "mode and then restart in daemon mode.\n"); - } - if ((sc->run_flags & RUN_FLAG__INLINE) && (sc->run_flags & RUN_FLAG__INLINE_TEST)) { diff --git a/src/parser/config_file.cc b/src/parser/config_file.cc index 37b3a71f8..22ab4f344 100644 --- a/src/parser/config_file.cc +++ b/src/parser/config_file.cc @@ -233,7 +233,6 @@ void ConfigDaemon(SnortConfig* sc, const char*) { DebugMessage(DEBUG_INIT, "Daemon mode flag set\n"); sc->run_flags |= RUN_FLAG__DAEMON; - sc->logging_flags |= LOGGING_FLAG__QUIET; } void ConfigDecodeDataLink(SnortConfig* sc, const char*) @@ -583,8 +582,6 @@ void config_syslog(SnortConfig* sc, const char*) if (syslog_configured) return; - /* If daemon or logging to syslog use "snort" as identifier and - * start logging there now */ openlog("snort", LOG_PID | LOG_CONS, LOG_DAEMON); sc->logging_flags |= LOGGING_FLAG__SYSLOG; @@ -598,10 +595,6 @@ void config_daemon(SnortConfig* sc, const char* val) if (daemon_configured) return; - /* If daemon or logging to syslog use "snort" as identifier and - * start logging there now */ - openlog("snort", LOG_PID | LOG_CONS, LOG_DAEMON); - ConfigDaemon(sc, val); daemon_configured = true; } diff --git a/src/utils/util.cc b/src/utils/util.cc index f066f911b..6b015052b 100644 --- a/src/utils/util.cc +++ b/src/utils/util.cc @@ -251,9 +251,11 @@ void CreatePidFile(pid_t pid) snort_conf->pid_filename = snort_conf->log_dir; snort_conf->pid_filename += "/snort.pid"; + std::string pid_lockfilename; + if ( !SnortConfig::no_lock_pid_file() ) { - std::string pid_lockfilename = snort_conf->pid_filename; + pid_lockfilename = snort_conf->pid_filename; pid_lockfilename += ".lck"; int lock_fd; @@ -296,6 +298,8 @@ void CreatePidFile(pid_t pid) snort_conf->pid_filename.c_str(), error); snort_conf->pid_filename.clear(); } + if ( !pid_lockfilename.empty() ) + unlink(pid_lockfilename.c_str()); } /**************************************************************************** -- 2.47.3