X-Git-Url: https://git.sommitrealweird.co.uk/onak.git/blobdiff_plain/78808f320d39f8c22622feb613706325222cfd59..0fac914c0876bf3cf78c12ea0b9b44880fbdffa4:/keyd.c diff --git a/keyd.c b/keyd.c index 7b8a18f..919b61b 100644 --- a/keyd.c +++ b/keyd.c @@ -2,11 +2,13 @@ * keyd.c - key retrieval daemon * * Jonathan McDowell - * + * * Copyright 2004 Project Purple */ +#include #include +#include #include #include #include @@ -27,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; @@ -41,7 +69,7 @@ void iteratefunc(void *ctx, struct openpgp_publickey *key) storebuf.buffer = malloc(8192); logthing(LOGTHING_TRACE, - "Iterating over 0x%016llX.", + "Iterating over 0x%016" PRIX64 ".", get_keyid(key)); flatten_publickey(key, @@ -97,7 +125,7 @@ int sock_init(const char *sockname) int sock_do(int fd) { - int cmd = KEYD_CMD_UNKNOWN; + uint32_t cmd = KEYD_CMD_UNKNOWN; ssize_t bytes = 0; ssize_t count = 0; int ret = 0; @@ -124,6 +152,8 @@ int sock_do(int fd) case KEYD_CMD_VERSION: cmd = KEYD_REPLY_OK; write(fd, &cmd, sizeof(cmd)); + cmd = sizeof(keyd_version); + write(fd, &cmd, sizeof(cmd)); write(fd, &keyd_version, sizeof(keyd_version)); break; case KEYD_CMD_GET: @@ -136,7 +166,8 @@ int sock_do(int fd) storebuf.offset = 0; if (ret == 0) { logthing(LOGTHING_INFO, - "Fetching 0x%llX, result: %d", + "Fetching 0x%" PRIX64 + ", result: %d", keyid, config.dbbackend-> fetch_key(keyid, &key, false)); @@ -266,7 +297,8 @@ int sock_do(int fd) } if (ret == 0) { logthing(LOGTHING_INFO, - "Deleting 0x%llX, result: %d", + "Deleting 0x%" PRIX64 + ", result: %d", keyid, config.dbbackend->delete_key( keyid, false)); @@ -281,6 +313,8 @@ int sock_do(int fd) } if (ret == 0) { keyid = config.dbbackend->getfullkeyid(keyid); + cmd = sizeof(keyid); + write(fd, &cmd, sizeof(cmd)); write(fd, &keyid, sizeof(keyid)); } break; @@ -293,9 +327,14 @@ int sock_do(int fd) write(fd, &bytes, sizeof(bytes)); break; case KEYD_CMD_CLOSE: + cmd = KEYD_REPLY_OK; + write(fd, &cmd, sizeof(cmd)); ret = 1; break; case KEYD_CMD_QUIT: + cmd = KEYD_REPLY_OK; + write(fd, &cmd, sizeof(cmd)); + ret = 1; trytocleanup(); break; default: @@ -311,7 +350,8 @@ int sock_do(int fd) int sock_close(int fd) { - return shutdown(fd, SHUT_RDWR); + shutdown(fd, SHUT_RDWR); + return close(fd); } int sock_accept(int fd) @@ -340,9 +380,30 @@ int main(int argc, char *argv[]) int fd = -1; fd_set rfds; char sockname[1024]; + char *configfile = NULL; + bool foreground = false; + int optchar; + + while ((optchar = getopt(argc, argv, "c:f")) != -1 ) { + switch (optchar) { + case 'c': + configfile = strdup(optarg); + break; + case 'f': + foreground = true; + break; + } + } - readconfig(NULL); + readconfig(configfile); + free(configfile); + configfile = NULL; initlogthing("keyd", config.logfile); + config.use_keyd = false; + + if (!foreground) { + daemonize(); + } catchsignals(); @@ -368,6 +429,6 @@ int main(int argc, char *argv[]) cleanuplogthing(); cleanupconfig(); - + return(EXIT_SUCCESS); }