From 91a7ca50f3f8a2c7bb01113fa3849cb5e153a70f Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Fri, 19 Jun 2020 19:11:48 +0200 Subject: Add support for async DNS --- main.c | 86 ++++++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 52 insertions(+), 34 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index e748ff6..b2accc1 100644 --- a/main.c +++ b/main.c @@ -255,7 +255,7 @@ cfg_read(struct cfg *cfg) while (true) { int key; const char *keyname; - union cfg_value value; + struct cfg_value value; if (!config_parse_line(cfg, path, &pos, mcfg_key_map, &key, &keyname, &value)) @@ -276,6 +276,7 @@ cfg_read(struct cfg *cfg) cfg->igmp_iface = xstrdup(value.str); if (!cfg->igmp_iface) perrordie("xstrdup"); + break; case MCFG_KEY_INVALID: @@ -291,40 +292,46 @@ const struct { } debug_category_str[] = { { .name = "config", - .val = DBG_CFG + .val = DBG_CFG, },{ .name = "refcount", - .val = DBG_REF + .val = DBG_REF, },{ .name = "malloc", - .val = DBG_MALLOC + .val = DBG_MALLOC, },{ .name = "announce", - .val = DBG_ANN + .val = DBG_ANN, },{ - .name = "signals", - .val = DBG_SIG + .name = "signal", + .val = DBG_SIG, },{ .name = "uring", - .val = DBG_UR + .val = DBG_UR, },{ .name = "server", - .val = DBG_SRV + .val = DBG_SRV, },{ .name = "proxy", - .val = DBG_PROXY + .val = DBG_PROXY, },{ .name = "rcon", - .val = DBG_RCON + .val = DBG_RCON, },{ .name = "idle", - .val = DBG_IDLE + .val = DBG_IDLE, },{ .name = "igmp", - .val = DBG_IGMP + .val = DBG_IGMP, + },{ + .name = "systemd", + .val = DBG_SYSD, + },{ + .name = "dns", + .val = DBG_DNS, },{ .name = NULL, - .val = 0 + .val = 0, } }; @@ -561,24 +568,19 @@ dump_tree(struct cfg *cfg) debug(DBG_REF, "\n\n\n\n"); } +static struct dns_async *hack_dns = NULL; + static void signalfd_read(struct cfg *cfg, struct uring_task *task, int res) { struct signalfd_ev *sev = container_of(task, struct signalfd_ev, task); struct server *server, *stmp; - static int count = 0; if (task->dead) { debug(DBG_SIG, "task dead\n"); return; } - count++; - if (count > 5) { - error("Max signal count exceeded, force quitting\n"); - exit(EXIT_FAILURE); - } - if (res != sizeof(sev->buf)) die("error in signalfd (%i)", res); @@ -586,7 +588,7 @@ signalfd_read(struct cfg *cfg, struct uring_task *task, int res) verbose("got a signal to quit\n"); sd_notifyf(0, "STOPPING=1\nSTATUS=Received signal, exiting"); exit(EXIT_SUCCESS); - } else { + } else if (sev->buf < 10000) { verbose("got a signal to dump tree\n"); sd_notifyf(0, "STOPPING=1\nSTATUS=Received signal, exiting"); dump_tree(cfg); @@ -598,25 +600,45 @@ signalfd_read(struct cfg *cfg, struct uring_task *task, int res) server_delete(cfg, server); uring_delete(cfg); return; + } else { + debug(DBG_DNS, "DNS lookup complete, dns: %p, dns->cb: %p\n", + hack_dns, + hack_dns ? hack_dns->callback : NULL); + + if (!hack_dns || !hack_dns->callback) { + error("DNS callback not set\n"); + goto out; + } + + if (hack_dns && hack_dns->callback) + hack_dns->callback(hack_dns); } +out: uring_read(cfg, &sev->task, &sev->buf, sizeof(sev->buf), signalfd_read); } static int hack_efd = -1; static void -hack_handler(int signum) +hack_handler(int signum, siginfo_t *info, void *ucontext) { uint64_t val; - static int count = 0; switch (signum) { + case SIGUSR1: + debug(DBG_SIG, "Got a SIGUSR1\n"); + if (info->si_code != SI_ASYNCNL || info->si_signo != SIGUSR1 || !info->si_ptr) { + debug(DBG_SIG, "unexpected values in siginfo\n"); + return; + } + debug(DBG_SIG, "SIGUSR1 struct dns_async: %p\n", info->si_ptr); + hack_dns = info->si_ptr; + val = 10000; + break; case SIGINT: debug(DBG_SIG, "Got a SIGINT\n"); val = 1000; - if (count > 3) - dump_tree(cfghack); break; case SIGHUP: debug(DBG_SIG, "Got a SIGHUP\n"); @@ -632,13 +654,7 @@ hack_handler(int signum) break; } - if (count > 5) { - error("Max signal count exceeded, force quitting\n"); - exit(EXIT_FAILURE); - } - write(hack_efd, &val, sizeof(val)); - count++; } static void @@ -664,14 +680,16 @@ signalfd_init(struct cfg *cfg) struct sigaction action; sigfillset(&action.sa_mask); - action.sa_handler = hack_handler; - action.sa_flags = 0; + action.sa_sigaction = hack_handler; + action.sa_flags = SA_SIGINFO; sigaction(SIGINT, &action, NULL); sigaction(SIGHUP, &action, NULL); sigaction(SIGTERM, &action, NULL); + sigaction(SIGUSR1, &action, NULL); action.sa_handler = SIG_IGN; + action.sa_flags = 0; sigaction(SIGPIPE, &action, NULL); sfd = eventfd(0, EFD_CLOEXEC); -- cgit v1.2.3