From: Jonathan McDowell Date: Wed, 16 Mar 2011 03:33:26 +0000 (+0000) Subject: Make keyd background itself by default X-Git-Url: https://git.sommitrealweird.co.uk/onak.git/commitdiff_plain/8d14d66b84cc370cefe54b33de1b95bb49c90bf6 Make keyd background itself by default It's meant to be a daemon, so it should run in the background. The -f option will cause it to run in the foreground for debugging etc. --- diff --git a/keyd.c b/keyd.c index 05f9fe9..18d09bf 100644 --- a/keyd.c +++ b/keyd.c @@ -6,6 +6,7 @@ * Copyright 2004 Project Purple */ +#include #include #include #include @@ -28,6 +29,32 @@ #include "onak-conf.h" #include "parsekey.h" +void daemonize(void) +{ + pid_t pid; + + pid = fork(); + + if (pid < 0) { + logthing(LOGTHING_CRITICAL, + "Failed to fork into background: %d (%s)", + errno, + strerror(errno)); + exit(EXIT_FAILURE); + } else if (pid > 0) { + logthing(LOGTHING_INFO, "Backgrounded as pid %d.", pid); + exit(EXIT_SUCCESS); + } + + pid = setsid(); + + freopen("/dev/null", "r", stdin); + freopen("/dev/null", "w", stdout); + freopen("/dev/null", "w", stderr); + + return; +} + void iteratefunc(void *ctx, struct openpgp_publickey *key) { struct openpgp_packet_list *packets = NULL; @@ -343,13 +370,17 @@ int main(int argc, char *argv[]) fd_set rfds; char sockname[1024]; char *configfile = NULL; + bool foreground = false; int optchar; - while ((optchar = getopt(argc, argv, "c:")) != -1 ) { + while ((optchar = getopt(argc, argv, "c:f")) != -1 ) { switch (optchar) { case 'c': configfile = strdup(optarg); break; + case 'f': + foreground = true; + break; } } @@ -358,6 +389,10 @@ int main(int argc, char *argv[]) configfile = NULL; initlogthing("keyd", config.logfile); + if (!foreground) { + daemonize(); + } + catchsignals(); snprintf(sockname, 1023, "%s/%s", config.db_dir, KEYD_SOCKET);