From cf87432e410cbe2ee61e90b8f4c2e8d2a75f09ca Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Fri, 5 Jun 2020 16:27:31 +0200 Subject: Split config into generic and dir watching parts --- cfgdir.c | 369 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 369 insertions(+) create mode 100644 cfgdir.c (limited to 'cfgdir.c') diff --git a/cfgdir.c b/cfgdir.c new file mode 100644 index 0000000..34283f4 --- /dev/null +++ b/cfgdir.c @@ -0,0 +1,369 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "main.h" +#include "uring.h" +#include "cfgdir.h" +#include "config.h" +#include "server.h" + +enum scfg_keys { + SCFG_KEY_INVALID = 0, + SCFG_KEY_TYPE, + SCFG_KEY_NAME, + SCFG_KEY_PORT, + SCFG_KEY_LOCAL, + SCFG_KEY_REMOTE, +}; + +struct cfg_key_value_map scfg_key_map[] = { + { + .key_name = "type", + .key_value = SCFG_KEY_TYPE, + .value_type = CFG_VAL_TYPE_STRING, + }, { + .key_name = "name", + .key_value = SCFG_KEY_NAME, + .value_type = CFG_VAL_TYPE_STRING, + }, { + .key_name = "port", + .key_value = SCFG_KEY_PORT, + .value_type = CFG_VAL_TYPE_UINT16, + }, { + .key_name = "local", + .key_value = SCFG_KEY_LOCAL, + .value_type = CFG_VAL_TYPE_ADDRS, + }, { + .key_name = "remote", + .key_value = SCFG_KEY_REMOTE, + .value_type = CFG_VAL_TYPE_ADDRS, + }, { + .key_name = NULL, + .key_value = SCFG_KEY_INVALID, + .value_type = CFG_VAL_TYPE_INVALID, + } +}; + +static void +scfg_parse(struct cfg *cfg, struct server *scfg) +{ + char *pos = &scfg->buf[0]; + + if (!config_parse_header(cfg, "server", &pos)) + return; + + while (true) { + int key; + union cfg_value value; + + if (!config_parse_line(cfg, &pos, scfg_key_map, &key, &value)) + break; + + if (key == SCFG_KEY_INVALID) + break; + printf("Got a key-value pair: %i = something\n", key); + + switch (key) { + + case SCFG_KEY_TYPE: + if (!strcmp(value.str, "proxy")) { + if (!server_set_type(cfg, scfg, SERVER_TYPE_PROXY)) + return; + } else if (!strcmp(value.str, "announce")) { + if (!server_set_type(cfg, scfg, SERVER_TYPE_ANNOUNCE)) + return; + } + break; + + case SCFG_KEY_NAME: + if (!server_set_pretty_name(cfg, scfg, value.str)) + return; + break; + + case SCFG_KEY_PORT: + if (!server_set_port(cfg, scfg, value.uint16)) + return; + break; + + case SCFG_KEY_LOCAL: { + struct sockaddr_in46 *addr, *tmp; + + list_for_each_entry_safe(addr, tmp, &value.addr_list, list) { + list_del(&addr->list); + server_add_local(cfg, scfg, addr); + } + break; + } + + case SCFG_KEY_REMOTE: { + struct sockaddr_in46 *addr, *tmp; + + list_for_each_entry_safe(addr, tmp, &value.addr_list, list) { + list_del(&addr->list); + server_add_remote(cfg, scfg, addr); + } + break; + } + + case SCFG_KEY_INVALID: + default: + break; + } + } + + //printf("Cfg:\n%s\n\n", pos); +} + +static void +scfg_read_cb(struct cfg *cfg, struct uring_task *task, int res) +{ + struct server *scfg = container_of(task, struct server, task); + + printf("Asked to parse server cfg %s (bytes %i)\n", scfg->name, res); + + if (res < 0) { + perrordie("read"); + } else if (res > 0) { + scfg->len += res; + if (scfg->len + 1 >= sizeof(scfg->buf)) { + fprintf(stderr, "Server config too large\n"); + server_delete(cfg, scfg); + return; + } + + uring_read(cfg, &scfg->task, scfg->buf + scfg->len, sizeof(scfg->buf) - scfg->len, scfg->len, scfg_read_cb); + return; + } else { + /* EOF */ + scfg->buf[scfg->len] = '\0'; + uring_task_close_fd(cfg, &scfg->task); + scfg_parse(cfg, scfg); + server_commit(cfg, scfg); + } +} + +static void +scfg_open_cb(struct cfg *cfg, struct uring_task *task, int res) +{ + struct server *scfg = container_of(task, struct server, task); + + if (res < 0) { + fprintf(stderr, "Open failed\n"); + server_delete(cfg, scfg); + return; + } + + printf("Asked to read server cfg %s (fd %i)\n", scfg->name, res); + uring_task_set_fd(&scfg->task, res); + scfg->len = 0; + uring_read(cfg, &scfg->task, scfg->buf, sizeof(scfg->buf), 0, scfg_read_cb); +} + +static bool +scfg_valid_filename(const char *name) +{ + const char *suffix; + + if (!name) + return false; + if (name[0] == '\0') + return false; + if (name[0] == '.') + return false; + if ((suffix = strrchr(name, '.')) == NULL) + return false; + if (strcmp(suffix, ".server")) + return false; + + return true; +} + +struct inotify_ev { + struct uring_task task; + char buf[4096] __attribute__((aligned(__alignof__(struct inotify_event)))); +}; + +static void +inotify_free(struct uring_task *task) +{ + struct inotify_ev *iev = container_of(task, struct inotify_ev, task); + struct cfg *cfg = container_of(task->parent, struct cfg, task); + + fprintf(stderr, "%s called\n", __func__); + if (!iev || !cfg) + die("%s: iev or cfg is NULL!?\n", __func__); + + free(iev); + cfg->iev = NULL; + uring_task_put(cfg, &cfg->task); +} + +static void +inotify_event_dump(const struct inotify_event *event) +{ + printf("Event:\n"); + printf(" * WD : %i\n", event->wd); + printf(" * Cookie : %" PRIu32 "\n", event->cookie); + printf(" * Length : %" PRIu32 "\n", event->len); + printf(" * Name : %s\n", event->name); + printf(" * Mask : %" PRIu32 "\n", event->mask); + if (event->mask & IN_ACCESS) + printf("\tIN_ACCESS\n"); + else if(event->mask & IN_MODIFY) + printf("\tIN_MODIFY\n"); + else if(event->mask & IN_ATTRIB) + printf("\tIN_ATTRIB\n"); + else if(event->mask & IN_CLOSE_WRITE) + printf("\tIN_CLOSE_WRITE\n"); + else if(event->mask & IN_CLOSE_NOWRITE) + printf("\tIN_CLOSE_NOWRITE\n"); + else if(event->mask & IN_OPEN) + printf("\tIN_OPEN\n"); + else if(event->mask & IN_MOVED_FROM) + printf("\tIN_MOVED_FROM\n"); + else if(event->mask & IN_MOVED_TO) + printf("\tIN_MOVED_TO\n"); + else if(event->mask & IN_CREATE) + printf("\tIN_CREATE\n"); + else if(event->mask & IN_DELETE) + printf("\tIN_DELETE\n"); + else if(event->mask & IN_DELETE_SELF) + printf("\tIN_DELETE_SELF\n"); + else if(event->mask & IN_MOVE_SELF) + printf("\tIN_MOVE_SELF\n"); + else if(event->mask & IN_UNMOUNT) + printf("\tIN_UNMOUNT\n"); + else if(event->mask & IN_Q_OVERFLOW) + printf("\tIN_Q_OVERFLOW\n"); + else if(event->mask & IN_IGNORED) + printf("\tIN_IGNORED\n"); + printf("\n"); +} + +static void +inotify_cb(struct cfg *cfg, struct uring_task *task, int res) +{ + struct inotify_ev *iev = container_of(task, struct inotify_ev, task); + const struct inotify_event *event; + char *ptr; + struct server *scfg; + + fprintf(stderr, "%s: ret is %i (ref %u)\n", __func__, res, task->refcount); + + if (task->dead) { + fprintf(stderr, "%s: task is dead\n", __func__); + uring_task_put(cfg, task); + return; + } + + if (res <= 0) + perrordie("inotify_read"); + + for (ptr = iev->buf; ptr < iev->buf + res; ptr += sizeof(struct inotify_event) + event->len) { + event = (const struct inotify_event *)ptr; + + if (debuglvl > 0) + inotify_event_dump(event); + + if (event->mask & (IN_IGNORED | IN_MOVE_SELF | IN_DELETE_SELF | IN_UNMOUNT)) + die("Configuration directory gone, exiting\n"); + + if (event->mask & IN_Q_OVERFLOW) { + error("inotify queue overflow!\n"); + continue; + } + + if (!scfg_valid_filename(event->name)) + continue; + + if (event->mask & (IN_MOVED_FROM | IN_DELETE)) + server_delete_by_name(cfg, event->name); + else if (event->mask & (IN_MOVED_TO | IN_CREATE | IN_CLOSE_WRITE)) { + scfg = server_new(cfg, event->name); + uring_openat(cfg, &scfg->task, event->name, scfg_open_cb); + } else + error("inotify: weird, unknown event: 0x%08x\n", event->mask); + } + + uring_read(cfg, &iev->task, iev->buf, sizeof(iev->buf), 0, inotify_cb); +} + +void +cfgdir_refdump(struct inotify_ev *iev) +{ + uring_task_refdump(&iev->task); +} + +void +cfgdir_delete(struct cfg *cfg) +{ + if (!cfg->iev) { + fprintf(stderr, "%s called with no iev!\n", __func__); + return; + } + + fprintf(stderr, "%s called, closing fd %i\n", __func__, cfg->iev->task.fd); + uring_cancel(cfg, &cfg->iev->task); + cfg->iev = NULL; +} + +void +cfgdir_init(struct cfg *cfg) +{ + int ifd; + int iwd; + struct inotify_ev *iev; + DIR *cfgdir; + struct dirent *dent; + struct server *scfg; + + iev = malloc(sizeof(*iev)); + if (!iev) + perrordie("malloc"); + + ifd = inotify_init1(IN_CLOEXEC); + if (ifd < 0) + perrordie("inotify_init1"); + + /* ln = IN_CREATE, cp/vi/mv = IN_CREATE, IN_OPEN, IN_CLOSE_WRITE */ + iwd = inotify_add_watch(ifd, ".", + IN_CLOSE_WRITE | IN_DELETE | IN_CREATE | + IN_DELETE_SELF | IN_MOVE_SELF | IN_MOVED_TO | + IN_MOVED_FROM | IN_DONT_FOLLOW | + IN_EXCL_UNLINK | IN_ONLYDIR ); + if (iwd < 0) + perrordie("inotify_add_watch"); + + uring_task_init(&iev->task, "iev", &cfg->task, inotify_free); + uring_task_set_fd(&iev->task, ifd); + cfg->iev = iev; + uring_read(cfg, &iev->task, iev->buf, sizeof(iev->buf), 0, inotify_cb); + + cfgdir = opendir("."); + if (!cfgdir) + perrordie("opendir"); + + while ((dent = readdir(cfgdir)) != NULL) { + if (dent->d_type != DT_REG && dent->d_type != DT_UNKNOWN) + continue; + if (!scfg_valid_filename(dent->d_name)) + continue; + + scfg = server_new(cfg, dent->d_name); + uring_openat(cfg, &scfg->task, dent->d_name, scfg_open_cb); + } + + closedir(cfgdir); +} + -- cgit v1.2.3