int listen_port = 4822; /* Default port */
int opt;
+ char* pidfile = NULL;
+
/* Daemon Process */
pid_t daemon_pid;
#endif
/* Parse arguments */
- while ((opt = getopt(argc, argv, "l:")) != -1) {
+ while ((opt = getopt(argc, argv, "l:p:")) != -1) {
if (opt == 'l') {
listen_port = atoi(optarg);
if (listen_port <= 0) {
exit(EXIT_FAILURE);
}
}
+ else if (opt == 'p') {
+ pidfile = strdup(optarg);
+ }
else {
- fprintf(stderr, "USAGE: %s [-l LISTENPORT]\n", argv[0]);
+ fprintf(stderr, "USAGE: %s [-l LISTENPORT] [-p PIDFILE]\n", argv[0]);
exit(EXIT_FAILURE);
}
}
exit(EXIT_FAILURE);
}
- /* If parent, exit */
+ /* If parent, write PID file and exit */
else if (daemon_pid != 0) {
+
+ if (pidfile != NULL) {
+
+ /* Attempt to open pidfile and write PID */
+ FILE* pidf = fopen(pidfile, "w");
+ if (pidf) {
+ fprintf(pidf, "%d\n", daemon_pid);
+ fclose(pidf);
+ }
+
+ /* Warn on failure */
+ else {
+ fprintf(stderr, "WARNING: Could not write PID file: %s\n", lasterror());
+ exit(EXIT_FAILURE);
+ }
+
+ }
+
exit(EXIT_SUCCESS);
}
#else