]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Merge pull request #80 in SNORT/snort3 from crc/daemon to master
authorRuss Combs (rucombs) <rucombs@cisco.com>
Thu, 15 Oct 2015 16:16:09 +0000 (12:16 -0400)
committerRuss Combs (rucombs) <rucombs@cisco.com>
Thu, 15 Oct 2015 16:16:09 +0000 (12:16 -0400)
Squashed commit of the following:

commit 4cc0fa54863c63fc7ff18495a890e65f2e90af60
Author: russ <russ@localhost.localdomain>
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
src/log/messages.cc
src/main/snort.cc
src/main/snort_config.cc
src/parser/cmd_line.cc
src/parser/config_file.cc
src/utils/util.cc

index 15b1e9852a566ed3560df692ecf7283a2f91b299..da23bd4f228bf79b957d556c2595b4e28fd4d796 100644 (file)
@@ -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();
 }
index ee0476cc25cc1b7b29c1fee5c7b944412dd9d439..b9178a0c93260fea0c29fd1b7bd5878f07b46226 100644 (file)
@@ -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';
index f12ebe514061486fb8c4e2905e2fd57765fa2a5e..c261ccaf7bb86aa42d2f1407f393610b5d767f39 100644 (file)
@@ -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() )
index ad412895f59680c4f35687ef9dec677b996447f6..5b46f2d727dcf5f3d69b9eb8409cbc9b895175c1 100644 (file)
@@ -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
index fcbf485fdb07564f6dc2bbb05ad5da98d652805c..2434b5bbe059f59e0b99a8df48bd26ea4f91e13f 100644 (file)
@@ -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))
     {
index 37b3a71f83c40f120836768863ec5fe295c24ae4..22ab4f344764f048c4304122043368ca6bc35ff9 100644 (file)
@@ -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;
 }
index f066f911b7ee49919f6e021d34ef0eb2575c8c26..6b015052b00813b072c5c1e18d08e3fe03037834 100644 (file)
@@ -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());
 }
 
 /****************************************************************************