]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Unix/ventoy_unix_src/DragonFly/oinit.c
grub2 boot mode for UEFI
[Ventoy.git] / Unix / ventoy_unix_src / DragonFly / 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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)init.c 8.1 (Berkeley) 7/15/93
34 * $FreeBSD: src/sbin/init/init.c,v 1.38.2.8 2001/10/22 11:27:32 des Exp $
35 */
36
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/mount.h>
40 #include <sys/sysctl.h>
41 #include <sys/wait.h>
42 #include <sys/stat.h>
43
44 #include <db.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <libutil.h>
48 #include <paths.h>
49 #include <signal.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <syslog.h>
54 #include <time.h>
55 #include <ttyent.h>
56 #include <unistd.h>
57 #include <sys/reboot.h>
58 #include <sys/mount.h>
59 #include <err.h>
60
61 #include <stdarg.h>
62 #include <vtutil.h>
63
64 int boot_verbose = 0;
65
66 int prepare_dmtable(void);
67 int mount_cd9660(char *dev, char *dir);
68 int mount_null(const char *src, const char *dst);
69 int mount_tmpfs(int argc, char *argv[]);
70
71 static int setctty(const char *name)
72 {
73 int fd;
74
75 revoke(name);
76 if ((fd = open(name, O_RDWR)) == -1) {
77 exit(1);
78 }
79
80 if (login_tty(fd) == -1) {
81 exit(1);
82 }
83
84 return fd;
85 }
86
87 static void ventoy_init(char **argv_orig)
88 {
89 pid_t pid, wpid;
90 int status, error;
91 char arg0[MAXPATHLEN];
92 char arg1[MAXPATHLEN];
93 char arg2[MAXPATHLEN];
94 char *argv[8];
95 struct sigaction sa;
96
97 /* step1: mount tmpfs */
98 vdebug("[VTOY] step 1: mount tmpfs ...");
99 strcpy(arg0, "mount_tmpfs");
100 strcpy(arg1, "tmpfs");
101 strcpy(arg2, "/tmp");
102 argv[0] = arg0;
103 argv[1] = arg1;
104 argv[2] = arg2;
105 argv[3] = NULL;
106 error = mount_tmpfs(3, argv);
107 vdebug(" %d\n", error);
108
109 /* step 2: prepare dmtable */
110 vdebug("[VTOY] step 2: prepare device-mapper table...\n");
111 (void)prepare_dmtable();
112
113 /* step 3: create device mapper */
114 vdebug("[VTOY] step 3: create device-mapper ...\n");
115 if ((pid = fork()) == 0) {
116 sigemptyset(&sa.sa_mask);
117 sa.sa_flags = 0;
118 sa.sa_handler = SIG_IGN;
119 sigaction(SIGTSTP, &sa, NULL);
120 sigaction(SIGHUP, &sa, NULL);
121
122 argv[0] = "dmsetup";
123 argv[1] = "create";
124 argv[2] = "ventoy";
125 argv[3] = "/tmp/dmtable";
126 argv[4] = "--readonly";
127 argv[5] = NULL;
128
129 sigprocmask(SIG_SETMASK, &sa.sa_mask, NULL);
130 execv("/sbin/dmsetup", __DECONST(char **, argv));
131 exit(1); /* force single user mode */
132 }
133
134 do {
135 wpid = waitpid(-1, &status, WUNTRACED);
136 } while (wpid != pid);
137
138 /* step 4: mount iso */
139 vdebug("[VTOY] step 4: mount device-mapper ...");
140 strcpy(arg0, "/dev/mapper/ventoy");
141 strcpy(arg1, "/new_root");
142 error = mount_cd9660(arg0, arg1);
143 vdebug(" %d\n", error);
144
145 /* step 5: mount devfs */
146 vdebug("[VTOY] step 5: mount devfs ...");
147 strcpy(arg0, "/dev");
148 strcpy(arg1, "/new_root/dev");
149 mount_null(arg0, arg1);
150 vdebug(" %d\n", error);
151
152 /* step 6: umount tmpfs */
153 error = unmount("/tmp", 0);
154 vdebug("[VTOY] step 6: unmount tmpfs %d\n", error);
155
156 /* step 7: swich_root */
157 vdebug("[VTOY] step 7: switch root ...\n");
158 error = chdir("/new_root");
159 if (error)
160 {
161 printf("[VTOY] chdir /new_root failed %d\n", error);
162 goto chroot_failed;
163 }
164
165 error = chroot_kernel("/new_root");
166 if (error)
167 {
168 printf("[VTOY] chroot_kernel /new_root failed %d\n", error);
169 goto chroot_failed;
170 }
171
172 error = chroot("/new_root");
173 if (error)
174 {
175 printf("[VTOY] chroot /new_root failed %d\n", error);
176 goto chroot_failed;
177 }
178
179 vdebug("[VTOY] step 8: now run /sbin/init ...\n");
180 execv("/sbin/init", __DECONST(char **, argv_orig));
181
182 /* We failed to exec /sbin/init in the chroot, sleep forever */
183 chroot_failed:
184 printf("[VTOY] ################### DEAD ################\n");
185 while(1) {
186 sleep(3);
187 };
188 }
189
190 int main(int argc __unused, char **argv)
191 {
192 size_t varsize = sizeof(int);
193
194 /* Dispose of random users. */
195 if (getuid() != 0)
196 errx(1, "%s", strerror(EPERM));
197
198 /* Init is not allowed to die, it would make the kernel panic */
199 signal(SIGTERM, SIG_IGN);
200
201 setctty(_PATH_CONSOLE);
202
203 sysctlbyname("debug.bootverbose", &boot_verbose, &varsize, NULL, 0);
204
205 vdebug("======= Ventoy Init Start ========\n");
206
207 ventoy_init(argv);
208 return 1;
209 }
210