diff options
author | Omar Polo <op@omarpolo.com> | 2021-03-19 19:21:29 +0000 |
---|---|---|
committer | Omar Polo <op@omarpolo.com> | 2021-03-19 19:21:29 +0000 |
commit | bc99d868bc3745dcc65add06cd3f9b9ec3575cb5 (patch) | |
tree | be26811257addb94ad763b63aa940a84964e9676 /utils.c | |
parent | 1fbac5ba7c6c04d59d1c1199cd9f57638967a504 (diff) |
refactoring: imsg everywhere
use imsg to handle ALL kinds of IPC in gmid. This simplifies and shorten the
code, and makes everything more uniform too.
Diffstat (limited to 'utils.c')
-rw-r--r-- | utils.c | 48 |
1 files changed, 30 insertions, 18 deletions
@@ -24,24 +24,6 @@ #include <openssl/x509_vfy.h> #include <openssl/x509v3.h> -static sigset_t set; - -void -block_signals(void) -{ - sigset_t new; - - sigemptyset(&new); - sigaddset(&new, SIGHUP); - sigprocmask(SIG_BLOCK, &new, &set); -} - -void -unblock_signals(void) -{ - sigprocmask(SIG_SETMASK, &set, NULL); -} - int starts_with(const char *str, const char *prefix) { @@ -248,3 +230,33 @@ end: X509_STORE_CTX_free(ctx); return ret; } + +void +dispatch_imsg(struct imsgbuf *ibuf, imsg_handlerfn **handlers, size_t size) +{ + struct imsg imsg; + size_t datalen, i; + ssize_t n; + + if ((n = imsg_read(ibuf)) == -1) { + if (errno == EAGAIN || errno == EWOULDBLOCK) + return; + _exit(1); + } + + if (n == 0) + _exit(1); + + for (;;) { + if ((n = imsg_get(ibuf, &imsg)) == -1) + _exit(1); + if (n == 0) + return; + datalen = imsg.hdr.len - IMSG_HEADER_SIZE; + i = imsg.hdr.type; + if (i > (size / sizeof(imsg_handlerfn*)) || handlers[i] == NULL) + abort(); + handlers[i](ibuf, &imsg, datalen); + imsg_free(&imsg); + } +} |