// sleepyd.c -- sleeps, and restarts self if given the HUP signal #include #include #include #include #include #include // 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); }