From: Eric Leblond Date: Fri, 5 Oct 2012 07:35:48 +0000 (+0200) Subject: suricata: avoid concurrent run in daemon mode X-Git-Tag: suricata-1.4beta3~24 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3061452c5e7fb3f0a6ba412ce43f0960bafaa512;p=thirdparty%2Fsuricata.git suricata: avoid concurrent run in daemon mode 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. --- diff --git a/src/Makefile.am b/src/Makefile.am index 7b1d31ebf4..7812c37a07 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -381,6 +381,7 @@ endif #suricata_CFLAGS = -Wall -fno-strict-aliasing +AM_CFLAGS = -DLOCAL_STATE_DIR=\"$(localstatedir)\" if BUILD_UNITTESTS check-am: diff --git a/src/suricata.c b/src/suricata.c index 349381938a..92d52b8607 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -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) { diff --git a/src/suricata.h b/src/suricata.h index 4db7d15d6c..2b98a96fb7 100644 --- a/src/suricata.h +++ b/src/suricata.h @@ -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 diff --git a/src/util-pidfile.c b/src/util-pidfile.c index 50f57132c0..8b6b5d5185 100644 --- a/src/util-pidfile.c +++ b/src/util-pidfile.c @@ -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; +} diff --git a/src/util-pidfile.h b/src/util-pidfile.h index 1757e00cfd..ef071f4e84 100644 --- a/src/util-pidfile.h +++ b/src/util-pidfile.h @@ -27,6 +27,7 @@ int SCPidfileCreate(const char *); void SCPidfileRemove(const char *); +int SCPidfileTestRunning(const char *pid_filename); #endif /* __UTIL_PID_H__ */