Blob Blame History Raw
diff -ur a/server/daemon_init.c b/server/daemon_init.c
--- a/server/daemon_init.c	2014-06-23 15:56:09.000000000 -0400
+++ b/server/daemon_init.c	2016-06-27 11:46:20.838477025 -0400
@@ -29,14 +29,14 @@
 /*
  * This should ultimately go in a header file.
  */
-void daemon_init(const char *prog, int nofork);
+void daemon_init(const char *prog, const char *pid_file, int nofork);
 void daemon_cleanup(void);
-int check_process_running(const char *prog, pid_t * pid);
+int check_process_running(const char *cmd, const char *pid_file, pid_t * pid);
 
 /*
  * Local prototypes.
  */
-static void update_pidfile(const char *prog);
+static void update_pidfile(const char *filename);
 static int setup_sigmask(void);
 static char pid_filename[PATH_MAX];
 
@@ -99,12 +99,10 @@
 
 
 int
-check_process_running(const char *prog, pid_t * pid)
+check_process_running(const char *cmd, const char *filename, pid_t * pid)
 {
 	pid_t oldpid;
 	FILE *fp;
-	char filename[PATH_MAX];
-	char *cmd;
 	int ret;
 	struct stat st;
 
@@ -114,11 +112,6 @@
 	 * Now see if there is a pidfile associated with this cmd in /var/run
 	 */
 	fp = NULL;
-	memset(filename, 0, PATH_MAX);
-
-	cmd = basename((char *)prog);
-	snprintf(filename, sizeof (filename), "/var/run/%s.pid", cmd);
-
 	ret = stat(filename, &st);
 	if ((ret < 0) || (!st.st_size))
 		return 0;
@@ -146,15 +139,11 @@
 
 
 static void
-update_pidfile(const char *prog)
+update_pidfile(const char *filename)
 {
 	FILE *fp = NULL;
-	char *cmd;
-
-	memset(pid_filename, 0, PATH_MAX);
 
-	cmd = basename((char *)prog);
-	snprintf(pid_filename, sizeof (pid_filename), "/var/run/%s.pid", cmd);
+	strncpy(pid_filename, filename, PATH_MAX);
 
 	fp = fopen(pid_filename, "w");
 	if (fp == NULL) {
@@ -197,19 +186,11 @@
 
 
 void
-daemon_init(const char *prog, int nofork)
+daemon_init(const char *prog, const char *pid_file, int nofork)
 {
-	uid_t uid;
 	pid_t pid;
 
-	uid = getuid();
-	if (uid) {
-		syslog(LOG_ERR,
-			"daemon_init: Sorry, only root wants to run this.\n");
-		exit(1);
-	}
-
-	if (check_process_running(prog, &pid) && (pid != getpid())) {
+	if (check_process_running(prog, pid_file, &pid) && (pid != getpid())) {
 		syslog(LOG_ERR,
 			"daemon_init: Process \"%s\" already running.\n",
 			prog);
@@ -226,7 +207,7 @@
 		exit(1);
 	}
 
-	update_pidfile(prog);
+	update_pidfile(pid_file);
 }
 
 
diff -ur a/server/main.c b/server/main.c
--- a/server/main.c	2014-06-23 15:56:09.000000000 -0400
+++ b/server/main.c	2016-06-27 11:39:55.345859796 -0400
@@ -4,6 +4,7 @@
 #include <unistd.h>
 #include <signal.h>
 #include <sys/types.h>
+#include <sys/param.h>
 
 /* Local includes */
 #include <stdint.h>
@@ -16,7 +17,7 @@
 
 /* configure.c */
 int do_configure(config_object_t *config, const char *filename);
-int daemon_init(const char *prog, int nofork);
+int daemon_init(const char *prog, const char *pid_file, int nofork);
 int daemon_cleanup(void);
 
 
@@ -29,6 +30,8 @@
 	printf("  -d <level>       Set debugging level to <level>.\n");
 	printf("  -c               Configuration mode.\n");
 	printf("  -l               List plugins.\n");
+	printf("  -w               Wait for initialization.\n");
+	printf("  -p <file>        Use <file> to record the active process id.\n");
 }
 
 
@@ -47,6 +50,7 @@
 	char listener_name[80];
 	char backend_name[80];
 	const char *config_file = DEFAULT_CONFIG_FILE;
+	char *pid_file = NULL;
 	config_object_t *config = NULL;
 	map_object_t *map = NULL;
 	const listener_plugin_t *lp;
@@ -64,7 +68,7 @@
 		return -1;
 	}
 
-	while ((opt = getopt(argc, argv, "Ff:d:cwlh")) != EOF) {
+	while ((opt = getopt(argc, argv, "Ff:d:cwlhp:")) != EOF) {
 		switch(opt) {
 		case 'F':
 			printf("Background mode disabled\n");
@@ -74,6 +78,10 @@
 			printf("Using %s\n", optarg);
 			config_file = optarg;
 			break;
+		case 'p':
+			printf("Using %s\n", optarg);
+			pid_file = optarg;
+			break;
 		case 'd':
 			debug_set = atoi(optarg);
 			break;
@@ -190,7 +198,13 @@
 		return 1;
 	}
 
-	daemon_init(basename(argv[0]), foreground);
+        if(pid_file == NULL) {
+            pid_file = malloc(PATH_MAX);
+            memset(pid_file, 0, PATH_MAX);
+            snprintf(pid_file, PATH_MAX, "/var/run/%s.pid", basename(argv[0]));
+        }
+        
+	daemon_init(basename(argv[0]), pid_file, foreground);
 	signal(SIGINT, exit_handler);
 	signal(SIGTERM, exit_handler);
 	signal(SIGQUIT, exit_handler);