add PID file handling to clients (including signal handling to remove the files)

This commit is contained in:
marschap
2008-01-06 17:03:51 +00:00
parent 7db002742e
commit c68e1aca72
12 changed files with 185 additions and 28 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ if DARWIN
AM_LDFLAGS = -framework CoreFoundation -framework IOKit
endif
AM_CPPFLAGS = -I$(top_srcdir) -DSYSCONFDIR=\"$(sysconfdir)\"
AM_CPPFLAGS = -I$(top_srcdir) -DSYSCONFDIR=\"$(sysconfdir)\" -DPIDFILEDIR=\"$(pidfiledir)\"
EXTRA_DIST = $(sysconf_DATA)
+3
View File
@@ -17,6 +17,9 @@ ReportToSyslog=false
# run in foreground [default: false; legal: true, false]
#Foreground=true
# PidFile location when running as daemon [default: /var/run/lcdproc.pid]
#PidFile=/var/run/lcdproc.pid
# slow down initial announcement of modes (in 1/100s)
#delay=2
+26 -5
View File
@@ -70,11 +70,15 @@ static int process_configfile(char *cfgfile);
#if !defined(SYSCONFDIR)
# define SYSCONFDIR "/etc"
#endif
#if !defined(PIDFILEDIR)
# define PIDFILEDIR "/var/run"
#endif
#define UNSET_INT -1
#define UNSET_STR "\01"
#define DEFAULT_SERVER "127.0.0.1"
#define DEFAULT_CONFIGFILE SYSCONFDIR "/lcdproc.conf"
#define DEFAULT_PIDFILE PIDFILEDIR "/lcdproc.pid"
#define DEFAULT_REPORTDEST RPT_DEST_STDERR
#define DEFAULT_REPORTLEVEL RPT_WARNING
@@ -111,6 +115,7 @@ int port = LCDPORT;
int foreground = FALSE;
static int report_level = UNSET_INT;
static int report_dest = UNSET_INT;
char *pidfile = NULL;
char *configfile = NULL;
char *displayname = NULL;
@@ -243,7 +248,7 @@ main(int argc, char **argv)
/* Read config file*/
cfgresult = process_configfile(configfile);
if (cfgresult < 0) {
fprintf(stderr, "Error reading config file");
fprintf(stderr, "Error reading config file\n");
exit(EXIT_FAILURE);
}
@@ -303,9 +308,22 @@ main(int argc, char **argv)
if (foreground != TRUE) {
if (daemon(1,0) != 0) {
fprintf(stderr, "Error: daemonize failed");
fprintf(stderr, "Error: daemonize failed\n");
return(EXIT_FAILURE);
}
if (pidfile != NULL) {
FILE *pidf = fopen(pidfile, "w");
if (pidf) {
fprintf(pidf, "%d\n", (int) getpid());
fclose(pidf);
} else {
fprintf(stderr, "Error creating pidfile %s: %s\n",
pidfile, strerror(errno));
return(EXIT_FAILURE);
}
}
}
// Init the status gatherers...
@@ -314,10 +332,8 @@ main(int argc, char **argv)
// And spew stuff!
main_loop();
// Clean up
// Clean up & exit
exit_program(EXIT_SUCCESS);
return(0);
}
@@ -372,6 +388,9 @@ process_configfile(char *configfile)
if (foreground != TRUE) {
foreground = config_get_bool(progname, "Foreground", 0, FALSE);
}
if (pidfile == NULL) {
pidfile = strdup(config_get_string(progname, "PidFile", 0, DEFAULT_PIDFILE));
}
if (islow < 0) {
islow = config_get_int(progname, "Delay", 0, -1);
}
@@ -455,6 +474,8 @@ exit_program(int val)
Quit = 1;
sock_close(sock);
mode_close();
if ((foreground != TRUE) && (pidfile != NULL))
unlink(pidfile);
exit(val);
}