Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / sbin / mkinitrd / mini_init / oinit.c
1 /*-
2  * Copyright (c) 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Donn Seeley at Berkeley Software Design, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * @(#) Copyright (c) 1991, 1993 The Regents of the University of California.  All rights reserved.
37  * @(#)init.c   8.1 (Berkeley) 7/15/93
38  * $FreeBSD: src/sbin/init/init.c,v 1.38.2.8 2001/10/22 11:27:32 des Exp $
39  * $DragonFly: src/sbin/init/init.c,v 1.10 2007/11/25 18:10:07 swildner Exp $
40  */
41
42 #include <sys/param.h>
43 #include <sys/ioctl.h>
44 #include <sys/mount.h>
45 #include <sys/sysctl.h>
46 #include <sys/wait.h>
47 #include <sys/stat.h>
48
49 #include <db.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <libutil.h>
53 #include <paths.h>
54 #include <signal.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <syslog.h>
59 #include <time.h>
60 #include <ttyent.h>
61 #include <unistd.h>
62 #include <sys/reboot.h>
63 #include <err.h>
64
65 #ifdef __STDC__
66 #include <stdarg.h>
67 #else
68 #include <varargs.h>
69 #endif
70
71 #include "pathnames.h"
72
73 static int      setctty(const char *);
74 static void     runcom(char **);
75
76 /*
77  * The mother of all processes.
78  */
79 int
80 main(int argc __unused, char **argv)
81 {
82         /* Dispose of random users. */
83         if (getuid() != 0)
84                 errx(1, "%s", strerror(EPERM));
85
86         runcom(argv);
87         return 1;
88 }
89
90 static int
91 setctty(const char *name)
92 {
93         int fd;
94
95         revoke(name);
96         if ((fd = open(name, O_RDWR)) == -1) {
97                 exit(1);
98         }
99
100         if (login_tty(fd) == -1) {
101                 exit(1);
102         }
103
104         return fd;
105 }
106
107
108 static void
109 runcom(char **argv_orig)
110 {
111         pid_t pid, wpid;
112         int status, fd, error;
113         const char *argv[4];
114         struct sigaction sa;
115
116         fd = -1;
117
118         if ((pid = fork()) == 0) {
119                 sigemptyset(&sa.sa_mask);
120                 sa.sa_flags = 0;
121                 sa.sa_handler = SIG_IGN;
122                 sigaction(SIGTSTP, &sa, NULL);
123                 sigaction(SIGHUP, &sa, NULL);
124
125                 setctty(_PATH_CONSOLE);
126
127                 argv[0] = "sh";
128                 argv[1] = _PATH_RUNCOM;
129                 argv[2] = "autoboot" ;
130                 argv[3] = 0;
131
132                 sigprocmask(SIG_SETMASK, &sa.sa_mask, NULL);
133
134                 execv(_PATH_BSHELL, __DECONST(char **, argv));
135                 exit(1);        /* force single user mode */
136         }
137
138         do {
139                 wpid = waitpid(-1, &status, WUNTRACED);
140         } while (wpid != pid);
141
142         error = chroot_kernel("/new_root");
143         if (error)
144                 exit(1);
145
146         error = chroot("/new_root");
147         if (error)
148                 exit(1);
149
150         execv("/sbin/init", __DECONST(char **, argv_orig));
151 }
152