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