]> git.sommitrealweird.co.uk Git - onak.git/commitdiff
Make keyd background itself by default
authorJonathan McDowell <noodles@earth.li>
Wed, 16 Mar 2011 03:33:26 +0000 (03:33 +0000)
committerJonathan McDowell <noodles@earth.li>
Wed, 16 Mar 2011 03:33:26 +0000 (03:33 +0000)
  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.

keyd.c

diff --git a/keyd.c b/keyd.c
index 05f9fe9b025742dbad205741d4f881002b446770..18d09bf515fd5533c74a52e9f59e62042d45a094 100644 (file)
--- a/keyd.c
+++ b/keyd.c
@@ -6,6 +6,7 @@
  * Copyright 2004 Project Purple
  */
 
+#include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
 #include <stdio.h>
 #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);