]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
suricata: avoid concurrent run in daemon mode
authorEric Leblond <eric@regit.org>
Fri, 5 Oct 2012 07:35:48 +0000 (09:35 +0200)
committerVictor Julien <victor@inliniac.net>
Fri, 9 Nov 2012 15:06:46 +0000 (16:06 +0100)
This patch creates a pid file per default and use it to avoid to be
able to run two Suricata. Separate pid file have to be provided to
be able to do it.

src/Makefile.am
src/suricata.c
src/suricata.h
src/util-pidfile.c
src/util-pidfile.h

index 7b1d31ebf4d7e1ffe2bd6e11ea2163e1766275c3..7812c37a07fde40546311d336feed06e8a91f7d0 100644 (file)
@@ -381,6 +381,7 @@ endif
 
 
 #suricata_CFLAGS = -Wall -fno-strict-aliasing 
+AM_CFLAGS = -DLOCAL_STATE_DIR=\"$(localstatedir)\"
 
 if BUILD_UNITTESTS
 check-am:
index 349381938ad774fc3f9ec216071bcb36bcffe479..92d52b86071605ae2b3495e5075d86413afbb89e 100644 (file)
@@ -1656,17 +1656,21 @@ int main(int argc, char **argv)
     TmModuleRunInit();
 
     if (daemon == 1) {
-        Daemonize();
         if (pid_filename == NULL) {
             if (ConfGet("pid-file", &pid_filename) == 1) {
                 SCLogInfo("Use pid file %s from config file.", pid_filename);
+           } else {
+                pid_filename = DEFAULT_PID_FILENAME;
            }
         }
-        if (pid_filename != NULL) {
-            if (SCPidfileCreate(pid_filename) != 0) {
-                pid_filename = NULL;
-                exit(EXIT_FAILURE);
-            }
+        if (SCPidfileTestRunning(pid_filename) != 0) {
+            pid_filename = NULL;
+            exit(EXIT_FAILURE);
+        }
+        Daemonize();
+        if (SCPidfileCreate(pid_filename) != 0) {
+            pid_filename = NULL;
+            exit(EXIT_FAILURE);
         }
     } else {
         if (pid_filename != NULL) {
index 4db7d15d6cb5e147ed53417d17ef7a49111863f5..2b98a96fb7623246a2d760171510dcb7032061b1 100644 (file)
@@ -81,6 +81,9 @@
 
 #define DEFAULT_CONF_FILE CONFIG_DIR "/suricata.yaml"
 
+#define DEFAULT_PID_DIR LOCAL_STATE_DIR "/run/"
+#define DEFAULT_PID_BASENAME "suricata.pid"
+#define DEFAULT_PID_FILENAME DEFAULT_PID_DIR DEFAULT_PID_BASENAME
 
 /* runtime engine control flags */
 #define SURICATA_STOP    0x01   /**< gracefully stop the engine: process all
index 50f57132c0d34eb3b78baca5fd037ad79945adfc..8b6b5d518536a6332dbc7cc8c6342086bf6cabd0 100644 (file)
@@ -85,3 +85,40 @@ void SCPidfileRemove(const char *pid_filename) {
     }
 }
 
+/**
+ * \brief Check a pid file (used at the startup)
+ *        This commonly needed by the init scripts
+ *
+ * \param pointer to the name of the pid file to write (optarg)
+ *
+ * \retval 0 if succes
+ * \retval -1 on failure
+ */
+int SCPidfileTestRunning(const char *pid_filename)
+{
+    if (access(pid_filename, F_OK) == 0) {
+        /* Check if the existing process is still alive. */
+        pid_t pidv;
+        FILE *pf;
+
+        pf = fopen(pid_filename, "r");
+        if (pf == NULL) {
+            SCLogError(SC_ERR_INITIALIZATION,
+                    "pid file '%s' exists and can not be read. Aborting!",
+                    pid_filename);
+            return -1;
+        }
+
+        if (fscanf(pf, "%d", &pidv) == 1 && kill(pidv, 0) == 0) {
+            fclose(pf);
+            SCLogError(SC_ERR_INITIALIZATION,
+                    "pid file '%s' exists. Is Suricata already running? Aborting!",
+                    pid_filename);
+            return -1;
+        }
+
+        if (pf != NULL)
+            fclose(pf);
+    }
+    return 0;
+}
index 1757e00cfd17bcfc7a536b3ac359a05a7a8bacea..ef071f4e8424011691db7e5cf07b7798819b9217 100644 (file)
@@ -27,6 +27,7 @@
 
 int SCPidfileCreate(const char *);
 void SCPidfileRemove(const char *);
+int SCPidfileTestRunning(const char *pid_filename);
 
 #endif /* __UTIL_PID_H__ */