Skip to content

Instantly share code, notes, and snippets.

@kiler129
Created October 31, 2020 02:13
Show Gist options
  • Save kiler129/00d666d54a1ab3537ed8c64298b86dd2 to your computer and use it in GitHub Desktop.
Save kiler129/00d666d54a1ab3537ed8c64298b86dd2 to your computer and use it in GitHub Desktop.
Simple process watchdog & restarter
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char* argv[]) {
char *args[]={argv[0], "UDP4-LISTEN:123,fork", "UDP:example.com:123", NULL};
setuid(0);
pid_t pid;
int exCode;
while(1) {
pid = fork();
if (pid == 0) {
printf("(Re)spawning..\n");
execvp("/usr/bin/socat", args);
exit(1);
} else {
waitpid(pid, &exCode, 0);
if (WIFEXITED(exCode)) {
printf("Crashed, exit: %d\n", WEXITSTATUS(exCode));
usleep(1000 * 1000);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment