init: fix signal handling

The only signals we handle are SIGINT and SIGCHLD, so block all others
and unblock them in the child process before we start a rurnlevel.
This commit is contained in:
William Hubbs 2017-04-13 12:54:18 -05:00
parent 05738bfce1
commit 4694900190

View File

@ -39,6 +39,7 @@ static const char *rc_default_runlevel = "default";
static pid_t do_openrc(const char *runlevel) static pid_t do_openrc(const char *runlevel)
{ {
pid_t pid; pid_t pid;
sigset_t signals;
pid = fork(); pid = fork();
switch(pid) { switch(pid) {
@ -47,6 +48,9 @@ static pid_t do_openrc(const char *runlevel)
break; break;
case 0: case 0:
setsid(); setsid();
/* unblock all signals */
sigemptyset(&signals);
sigprocmask(SIG_SETMASK, &signals, NULL);
printf("Starting %s runlevel\n", runlevel); printf("Starting %s runlevel\n", runlevel);
execl("/sbin/openrc", "/sbin/openrc", runlevel, NULL); execl("/sbin/openrc", "/sbin/openrc", runlevel, NULL);
perror("exec"); perror("exec");
@ -135,11 +139,14 @@ int main(int argc, char **argv)
int count; int count;
FILE *fifo; FILE *fifo;
bool reexec = false; bool reexec = false;
sigset_t signals;
struct sigaction sa; struct sigaction sa;
if (getpid() != 1) if (getpid() != 1)
return 1; return 1;
printf("OpenRC init version %s starting\n", VERSION);
if (argc > 1) if (argc > 1)
default_runlevel = argv[1]; default_runlevel = argv[1];
else else
@ -148,15 +155,22 @@ int main(int argc, char **argv)
if (default_runlevel && strcmp(default_runlevel, "reexec") == 0) if (default_runlevel && strcmp(default_runlevel, "reexec") == 0)
reexec = true; reexec = true;
printf("OpenRC init version %s starting\n", VERSION); /* block all signals we do not handle */
if (! reexec) sigfillset(&signals);
init(default_runlevel); sigdelset(&signals, SIGCHLD);
sigdelset(&signals, SIGINT);
sigprocmask(SIG_SETMASK, &signals, NULL);
/* install signal handler */
memset(&sa, 0, sizeof(sa)); memset(&sa, 0, sizeof(sa));
sa.sa_handler = signal_handler; sa.sa_handler = signal_handler;
sigaction(SIGCHLD, &sa, NULL); sigaction(SIGCHLD, &sa, NULL);
sigaction(SIGINT, &sa, NULL); sigaction(SIGINT, &sa, NULL);
reboot(RB_DISABLE_CAD); reboot(RB_DISABLE_CAD);
if (! reexec)
init(default_runlevel);
if (mkfifo(RC_INIT_FIFO, 0600) == -1 && errno != EEXIST) if (mkfifo(RC_INIT_FIFO, 0600) == -1 && errno != EEXIST)
perror("mkfifo"); perror("mkfifo");