#suricata_CFLAGS = -Wall -fno-strict-aliasing
+AM_CFLAGS = -DLOCAL_STATE_DIR=\"$(localstatedir)\"
if BUILD_UNITTESTS
check-am:
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) {
#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
}
}
+/**
+ * \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;
+}
int SCPidfileCreate(const char *);
void SCPidfileRemove(const char *);
+int SCPidfileTestRunning(const char *pid_filename);
#endif /* __UTIL_PID_H__ */