Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
minimal init daemon by rich felker, author of musl libc
#define _XOPEN_SOURCE 700
#include <signal.h>
#include <unistd.h>
int main()
{
sigset_t set;
int status;
if (getpid() != 1) return 1;
sigfillset(&set);
sigprocmask(SIG_BLOCK, &set, 0);
if (fork()) for (;;) wait(&status);
sigprocmask(SIG_UNBLOCK, &set, 0);
setsid();
setpgid(0, 0);
return execve("/etc/rc", (char *[]){ "rc", 0 }, (char *[]){ 0 });
}
@furrybrackets
Copy link

furrybrackets commented Aug 14, 2022

For those confused, all this code does is:

  1. Get the process ID of the current process.
  2. If the process ID is not 1, return 1 (error).
  3. Fill the signal set with all current signals.
  4. Block all signals via the SIG_BLOCK constant.
  5. If a fork is successful, wait for the child to exit.
  6. Unblock all signals.
  7. Set the session ID of the process to the process ID.
  8. Execute the /etc/rc script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment