- 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(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 */
- if ((device_open(a->terminal, O_RDWR)) < 0) {
- if (stat(a->terminal, &sb) != 0) {
- message(LOG | CONSOLE, "device '%s' does not exist.",
- a->terminal);
- _exit(1);
- }
- message(LOG | CONSOLE, "Bummer, can't open %s", a->terminal);
- _exit(1);
- }
-
- /* Make sure the terminal will act fairly normal for us */
- set_term(0);
- /* 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);
-
- /* Wait for child to exit */
- while ((tmp_pid = waitpid(pid, &junk, 0)) != pid) {
- if (tmp_pid == -1 && errno == ECHILD) {
- break;
- }
- /* FIXME handle other errors */
- }
-
- /* See if stealing the controlling tty back is necessary */
- pgrp = tcgetpgrp(0);
- if (pgrp != 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);
- }
- while ((tmp_pid = waitpid(pid, &junk, 0)) != pid) {
- if (tmp_pid < 0 && errno == ECHILD)
- break;
- }
- _exit(0);
- }