Created
October 31, 2020 02:13
-
-
Save kiler129/00d666d54a1ab3537ed8c64298b86dd2 to your computer and use it in GitHub Desktop.
Simple process watchdog & restarter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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