/*- * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Donn Seeley at Berkeley Software Design, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved. * @(#)init.c 8.1 (Berkeley) 7/15/93 * $FreeBSD: src/sbin/init/init.c,v 1.38.2.8 2001/10/22 11:27:32 des Exp $ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "pathnames.h" static int setctty(const char *); static void runcom(char **); /* * The mother of all processes. */ int main(int argc __unused, char **argv) { /* Dispose of random users. */ if (getuid() != 0) errx(1, "%s", strerror(EPERM)); /* Init is not allowed to die, it would make the kernel panic */ signal(SIGTERM, SIG_IGN); runcom(argv); return 1; } static int setctty(const char *name) { int fd; revoke(name); if ((fd = open(name, O_RDWR)) == -1) { exit(1); } if (login_tty(fd) == -1) { exit(1); } return fd; } static void runcom(char **argv_orig) { pid_t pid, wpid; int status, error; const char *argv[4]; struct sigaction sa; if ((pid = fork()) == 0) { sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = SIG_IGN; sigaction(SIGTSTP, &sa, NULL); sigaction(SIGHUP, &sa, NULL); setctty(_PATH_CONSOLE); argv[0] = "sh"; argv[1] = _PATH_RUNCOM; argv[2] = "autoboot" ; argv[3] = NULL; sigprocmask(SIG_SETMASK, &sa.sa_mask, NULL); execv(_PATH_BSHELL, __DECONST(char **, argv)); exit(1); /* force single user mode */ } do { wpid = waitpid(-1, &status, WUNTRACED); } while (wpid != pid); error = chdir(_PATH_NEWROOT); if (error) goto chroot_failed; error = chroot_kernel(_PATH_NEWROOT); if (error) goto chroot_failed; error = chroot(_PATH_NEWROOT); if (error) goto chroot_failed; execv("/sbin/init", __DECONST(char **, argv_orig)); /* We failed to exec /sbin/init in the chroot, sleep forever */ chroot_failed: while(1) { sleep(3); }; }