#include <unistd.h> /* fork(), exec(), getuid() */
#include <sys/types.h> /* wait(), getuid() */
#include <sys/wait.h> /* wait() */
#include <sys/prctl.h> /* prctl() */
#include <stdlib.h> /* exit() */
#include <stdarg.h> /* va_list, va_start(), vprintf(), va_end() */
#include <stdio.h> /* vprintf(), putchar(), fflush(), perror() */
#include <errno.h> /* errno */
#include <string.h> /* strcpy() */
/* specify your stage2 here */
#define STAGE2 "/sbin/stage2"
/* if we fail to start the above, then we start the below instead. */
#define SHELL "/bin/sh"
#define TAG_WAIT "IDFK what this used to be..."
void say (const char *restrict const fmt, ...) {
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
putchar('\n');
fflush(stdout);
va_end(args);
}
int spawn (const char *path, char *const name) {
if (fork()) {
int status;
if (0 > wait(&status) && ECHILD != errno) perror(name);
return 0;
} else {
execl(path, name, NULL);
perror(name);
exit(0);
}
}
int main (int argc, char **argv) {
argc = argc; /* stfu gcc */
if (getuid()) {
say("must be superuser");
exit(0);
}
if (fork()) {
int status;
strcpy(argv[0], "death");
prctl(PR_SET_NAME, argv[0]);
while (0 < wait(&status));
if (ECHILD != errno) perror(TAG_WAIT);
else say("%s exiting", TAG_WAIT);
} else {
strcpy(argv[0], "life");
prctl(PR_SET_NAME, argv[0]);
for (;;) {
spawn(STAGE2, "stage2");
spawn(SHELL, "sh");
}
}
}