- int fd;
- int tried_devcons = 0;
- int tried_vtprimary = 0;
- struct serial_struct sr;
- char *s;
-
- if ((s = getenv("TERM")) != NULL) {
- snprintf(termType,sizeof(termType)-1,"TERM=%s",s);
- }
-
- if ((s = getenv("CONSOLE")) != NULL) {
- snprintf(console, sizeof(console)-1, "%s",s);
- }
-#if #cpu(sparc)
- /* sparc kernel supports console=tty[ab] parameter which is also
- * passed to init, so catch it here */
- else if ((s = getenv("console")) != NULL) {
- /* remap tty[ab] to /dev/ttyS[01] */
- if (strcmp( s, "ttya" )==0)
- snprintf(console, sizeof(console)-1, "%s", SERIAL_CON0);
- else if (strcmp( s, "ttyb" )==0)
- snprintf(console, sizeof(console)-1, "%s", SERIAL_CON1);
- }
-#endif
- else {
- struct vt_stat vt;
-
- /* 2.2 kernels: identify the real console backend and try to use it */
- if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
- /* this is a serial console */
- snprintf(console, sizeof(console)-1, "/dev/ttyS%d", sr.line);
- }
- else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
- /* this is linux virtual tty */
- snprintf(console, sizeof(console)-1, "/dev/tty%d", vt.v_active);
- } else {
- snprintf(console, sizeof(console)-1, "%s", _PATH_CONSOLE);
- tried_devcons++;
- }
- }
-
- while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
- /* Can't open selected console -- try /dev/console */
- if (!tried_devcons) {
- tried_devcons++;
- snprintf(console, sizeof(console)-1, "%s", _PATH_CONSOLE);
- continue;
- }
- /* Can't open selected console -- try vt1 */
- if (!tried_vtprimary) {
- tried_vtprimary++;
- snprintf(console, sizeof(console)-1, "%s", VT_PRIMARY);
- continue;
- }
- break;
- }
- if (fd < 0) {
- /* Perhaps we should panic here? */
- snprintf(console, sizeof(console)-1, "/dev/null");
- } else {
- /* check for serial console and disable logging to tty3 & running a
- * shell to tty2 */
- if (ioctl(0,TIOCGSERIAL,&sr) == 0) {
- message(LOG|CONSOLE, "serial console detected. Disabling virtual terminals.\r\n" );
- log = NULL;
- secondConsole = NULL;
- }
- close(fd);
- }
- message(LOG, "console=%s\n", console );
-}
+ int i;
+ pid_t pid;
+ char *s, *tmpCmd, *cmdpath;
+ char *cmd[INIT_BUFFS_SIZE];
+ char buf[INIT_BUFFS_SIZE + 6]; /* INIT_BUFFS_SIZE+strlen("exec ")+1 */
+ sigset_t nmask, omask;
+
+ /* Block sigchild while forking (why?) */
+ sigemptyset(&nmask);
+ sigaddset(&nmask, SIGCHLD);
+ sigprocmask(SIG_BLOCK, &nmask, &omask);
+ pid = fork();
+ sigprocmask(SIG_SETMASK, &omask, NULL);
+
+ if (pid < 0)
+ message(L_LOG | L_CONSOLE, "Can't fork");
+ if (pid)
+ return pid;
+
+ /* Child */
+
+ /* 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();