- if ((pid = fork()) == 0) {
-
- /* Clean up */
- close(0);
- close(1);
- close(2);
- sigprocmask(SIG_SETMASK, &omask, NULL);
-
- /* Reset signal handlers that were set by the parent process */
- signal(SIGUSR1, SIG_DFL);
- signal(SIGUSR2, SIG_DFL);
- signal(SIGINT, SIG_DFL);
- signal(SIGTERM, SIG_DFL);
- signal(SIGHUP, SIG_DFL);
- signal(SIGQUIT, SIG_DFL);
- signal(SIGCONT, SIG_DFL);
- signal(SIGSTOP, SIG_DFL);
- signal(SIGTSTP, SIG_DFL);
-
- /* Create a new session and make ourself the process
- * group leader */
- setsid();
-
- /* Open the new terminal device */
- open_new_terminal(a->terminal, 1);
-
- /* Make sure the terminal will act fairly normal for us */
- set_term();
- /* Setup stdout, stderr for the new process so
- * they point to the supplied terminal */
- dup(0);
- dup(0);
-
- /* If the init Action requires us to wait, then force the
- * supplied terminal to be the controlling tty. */
- if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
-
- /* Now fork off another process to just hang around */
- if ((pid = fork()) < 0) {
- message(LOG | CONSOLE, "Can't fork!");
- _exit(1);
- }
-
- if (pid > 0) {
-
- /* We are the parent -- wait till the child is done */
- signal(SIGINT, SIG_IGN);
- signal(SIGTSTP, SIG_IGN);
- signal(SIGQUIT, SIG_IGN);
- signal(SIGCHLD, SIG_DFL);
-
- waitfor(NULL, pid);
- /* See if stealing the controlling tty back is necessary */
- if (tcgetpgrp(0) != getpid())
- _exit(0);
-
- /* Use a temporary process to steal the controlling tty. */
- if ((pid = fork()) < 0) {
- message(LOG | CONSOLE, "Can't fork!");
- _exit(1);
- }
- if (pid == 0) {
- setsid();
- ioctl(0, TIOCSCTTY, 1);
- _exit(0);
- }
- waitfor(NULL, pid);
- _exit(0);
- }