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.
pull/184/merge
Eric Leblond 14 years ago committed by Victor Julien
parent 24d10de8af
commit 3061452c5e

@ -381,6 +381,7 @@ endif
#suricata_CFLAGS = -Wall -fno-strict-aliasing
AM_CFLAGS = -DLOCAL_STATE_DIR=\"$(localstatedir)\"
if BUILD_UNITTESTS
check-am:

@ -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) {

@ -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

@ -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;
}

@ -27,6 +27,7 @@
int SCPidfileCreate(const char *);
void SCPidfileRemove(const char *);
int SCPidfileTestRunning(const char *pid_filename);
#endif /* __UTIL_PID_H__ */

Loading…
Cancel
Save