]> the.earth.li Git - onak.git/blobdiff - keyd.c
Make keyd internal functions static
[onak.git] / keyd.c
diff --git a/keyd.c b/keyd.c
index f3966db96dcdbafde6b92b341566b1199da791a7..982d893c7eadaddf0a11543486c5e541797333b0 100644 (file)
--- a/keyd.c
+++ b/keyd.c
@@ -48,7 +48,7 @@
 
 static struct keyd_stats *stats;
 
-void daemonize(void)
+static void daemonize(void)
 {
        pid_t pid;
 
@@ -65,16 +65,40 @@ void daemonize(void)
                exit(EXIT_SUCCESS);
        }
 
-       pid = setsid();
+       if (setsid() == -1) {
+               logthing(LOGTHING_CRITICAL,
+                       "Couldn't set process group leader: %d (%s)",
+                       errno,
+                       strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 
-       freopen("/dev/null", "r", stdin);
-       freopen("/dev/null", "w", stdout);
-       freopen("/dev/null", "w", stderr);
+       if (!freopen("/dev/null", "r", stdin)) {
+               logthing(LOGTHING_CRITICAL,
+                       "Couldn't reopen stdin to NULL: %d (%s)",
+                       errno,
+                       strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+       if (!freopen("/dev/null", "w", stdout)) {
+               logthing(LOGTHING_CRITICAL,
+                       "Couldn't reopen stdout to NULL: %d (%s)",
+                       errno,
+                       strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+       if (!freopen("/dev/null", "w", stderr)) {
+               logthing(LOGTHING_CRITICAL,
+                       "Couldn't reopen stderr to NULL: %d (%s)",
+                       errno,
+                       strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 
        return;
 }
 
-void iteratefunc(void *ctx, struct openpgp_publickey *key)
+static void iteratefunc(void *ctx, struct openpgp_publickey *key)
 {
        struct openpgp_packet_list *packets = NULL;
        struct openpgp_packet_list *list_end = NULL;
@@ -119,7 +143,7 @@ void iteratefunc(void *ctx, struct openpgp_publickey *key)
        return;
 }
 
-int sock_init(const char *sockname)
+static int sock_init(const char *sockname)
 {
        struct sockaddr_un sock;
        int                fd = -1;
@@ -139,25 +163,29 @@ int sock_init(const char *sockname)
 
        if (ret != -1) {
                ret = listen(fd, 5);
+               if (ret == -1) {
+                       close(fd);
+                       fd = -1;
+               }
        }
-       
+
        return fd;
 }
 
-int sock_do(struct onak_dbctx *dbctx, int fd)
+static int sock_do(struct onak_dbctx *dbctx, int fd)
 {
        uint32_t cmd = KEYD_CMD_UNKNOWN;
        ssize_t  bytes = 0;
        ssize_t  count = 0;
        int      ret = 0;
        uint64_t keyid = 0;
-       uint8_t  fp[MAX_FINGERPRINT_LEN];
        char     *search = NULL;
        struct openpgp_publickey *key = NULL;
        struct openpgp_packet_list *packets = NULL;
        struct openpgp_packet_list *list_end = NULL;
        struct buffer_ctx storebuf;
        struct skshash hash;
+       struct openpgp_fingerprint fingerprint;
 
        /*
         * Get the command from the client.
@@ -238,7 +266,8 @@ int sock_do(struct onak_dbctx *dbctx, int fd)
                        if (bytes > MAX_FINGERPRINT_LEN) {
                                ret = 1;
                        } else {
-                               read(fd, fp, bytes);
+                               fingerprint.length = bytes;
+                               read(fd, fingerprint.fp, bytes);
                        }
                        storebuf.offset = 0;
                        if (ret == 0) {
@@ -246,7 +275,7 @@ int sock_do(struct onak_dbctx *dbctx, int fd)
                                                "Fetching by fingerprint"
                                                ", result: %d",
                                                dbctx->fetch_key_fp(dbctx,
-                                                       fp, bytes,
+                                                       &fingerprint,
                                                        &key, false));
                                if (key != NULL) {
                                        storebuf.size = 8192;
@@ -483,13 +512,13 @@ int sock_do(struct onak_dbctx *dbctx, int fd)
        return(ret);
 }
 
-int sock_close(int fd)
+static int sock_close(int fd)
 {
        shutdown(fd, SHUT_RDWR);
        return close(fd);
 }
 
-int sock_accept(int fd)
+static int sock_accept(int fd)
 {
        struct sockaddr_un sock;
        socklen_t          socklen;
@@ -535,6 +564,9 @@ int main(int argc, char *argv[])
        while ((optchar = getopt(argc, argv, "c:fh")) != -1 ) {
                switch (optchar) {
                case 'c':
+                       if (configfile != NULL) {
+                               free(configfile);
+                       }
                        configfile = strdup(optarg);
                        break;
                case 'f':