💾 Archived View for thrig.me › tech › sleepyd.c captured on 2023-12-28 at 17:20:24.
⬅️ Previous capture (2023-05-24)
-=-=-=-=-=-=-
// sleepyd.c -- sleeps, and restarts self if given the HUP signal #include <err.h> #include <limits.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> // an exec from a signal handler may be problematic (especially if // signal masks and a fork is involved) so instead we only flag that a // restart needs to happen volatile sig_atomic_t Restart_Self; void handler(int sig) { Restart_Self = 1; } int main(int argc, char *argv[]) { // probably a daemon should have this //if (chdir("/") == -1) err(1, "chdir"); #ifdef __OpenBSD__ if (pledge("exec stdio", NULL) == -1) err(1, "pledge failed"); #endif signal(SIGHUP, handler); fprintf(stderr, "sleepy time for %d\n", getpid()); while (1) { sleep(INT_MAX); if (Restart_Self) { execv(*argv, argv); err(1, "execv failed '%s'", *argv); } } exit(EXIT_FAILURE); }