fiss

Friedel's Initialization and Service Supervision
Log | Files | Refs | LICENSE

commit fc3a280bd094568b5caee4c7d5d400875e33e654
parent fdade2facf5fd2164bca13d324c68468c8abbfdc
Author: Friedel Schön <[email protected]>
Date:   Sat, 27 May 2023 13:05:38 +0200

fixing error handling in creation for supervise; supervise->control == 0

Diffstat:
Msrc/register.c | 50++++++++++++++++++++++++++++++++------------------
Msrc/supervise.c | 1+
2 files changed, 33 insertions(+), 18 deletions(-)

diff --git a/src/register.c b/src/register.c @@ -23,49 +23,59 @@ static int fd_set_flag(int fd, int flags) { return 0; } -static void init_supervise(service_t* s) { +static int init_supervise(service_t* s) { int fd; struct stat st; - if (fstatat(s->dir, "supervise", &st, 0) == -1) - mkdirat(s->dir, "supervise", 0744); - - if (fstatat(s->dir, "supervise/ok", &st, 0) == -1) - mkfifoat(s->dir, "supervise/ok", 0644); - - if (fstatat(s->dir, "supervise/control", &st, 0) == -1) - mkfifoat(s->dir, "supervise/control", 0644); - + if (fstatat(s->dir, "supervise", &st, 0) == -1 && mkdirat(s->dir, "supervise", 0744) == -1) { + print_error("warning: cannot create directory supervise: %s\ntrying to remove supervise...\n"); + if (unlinkat(s->dir, "supervise", 0) == -1 || mkdirat(s->dir, "supervise", 0744) == -1) { + print_error("warning: cannot create directory supervise: %s\n"); + return -1; + } + } + if (fstatat(s->dir, "supervise/ok", &st, 0) == -1 && mkfifoat(s->dir, "supervise/ok", 0644) == -1) { + print_error("cannot create fifo at supervise/ok: %s\n"); + return -1; + } + if (fstatat(s->dir, "supervise/control", &st, 0) == -1 && mkfifoat(s->dir, "supervise/control", 0644) == -1) { + print_error("cannot create fifo at supervise/control: %s\n"); + return -1; + } if (openat(s->dir, "supervise/ok", O_RDONLY | O_NONBLOCK) == -1) { print_error("cannot open supervise/ok: %s\n"); - return; + return -1; } if ((s->control = openat(s->dir, "supervise/control", O_RDONLY | O_NONBLOCK)) == -1) { print_error("cannot open supervise/ok: %s\n"); - return; + return -1; } - fd_set_flag(s->control, O_NONBLOCK); + if (fd_set_flag(s->control, O_NONBLOCK)) { + print_error("cannot set supervise/control non-blocking: %s\n"); + } if ((fd = openat(s->dir, "supervise/lock", O_CREAT | O_WRONLY, 0644)) == -1) { print_error("cannot create supervise/lock: %s\n"); - return; + return -1; } close(fd); if ((fd = openat(s->dir, "supervise/runlevel", O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1) { print_error("cannot create supervise/runlevel: %s\n"); - return; + return -1; } if (write(fd, runlevel, strlen(runlevel)) == -1) { print_error("cannot write to supervise/runlevel: %s\n"); close(fd); - return; + return -1; } close(fd); + + return 0; } service_t* service_register(int dir, const char* name, bool is_log_service) { @@ -92,12 +102,16 @@ service_t* service_register(int dir, const char* name, bool is_log_service) { if ((s->dir = openat(dir, name, O_DIRECTORY)) == -1) { print_error("error: cannot open '%s': %s\n", name); + services_size--; return NULL; } - strncpy(s->name, name, sizeof(s->name)); + if (init_supervise(s) == -1) { + services_size--; + return NULL; + } - init_supervise(s); + strncpy(s->name, name, sizeof(s->name)); s->status_change = time(NULL); service_update_status(s); diff --git a/src/supervise.c b/src/supervise.c @@ -75,6 +75,7 @@ static void control_sockets(void) { for (int i = 0; i < services_size; i++) { s = &services[i]; + printf("%s reading from %d\n", s->name, s->control); while (read(s->control, &chr, 1) == 1) { printf("handling '%c' from %s\n", chr, s->name); if (read_signo) {