forked from pcercuei/mininit
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinit.c
More file actions
377 lines (304 loc) · 8.23 KB
/
init.c
File metadata and controls
377 lines (304 loc) · 8.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#ifndef MS_MOVE
#define MS_MOVE 8192
#endif
#include "debug.h"
#include "loop.h"
/* Split the passed buffer as a list of parameters. */
static int __mkparam (char *buf, char **paramv, int maxparam, const char delim)
{
int paramc;
paramv[0] = strtok(buf, &delim);
if (!paramv[0])
return 0;
for (paramc=1; ; paramc++) {
paramv[paramc] = strtok(NULL, &delim);
if (!paramv[paramc]) break;
}
return paramc;
}
static int __read_text_file (const char *fn, char *buf, size_t len)
{
int fd, r;
fd = open(fn, O_RDONLY);
if (fd < 0) {
ERROR("Unable to open \'%s\'.\n", fn);
return fd;
}
r = read(fd, buf, len-1);
close(fd);
if (r < 0) {
ERROR("Unable to read \'%s\'.\n", fn);
return r;
}
/* Skip the last \n */
if (r && (buf[r-1] == '\n'))
buf[r-1] = '\0';
buf[r] = '\0';
return 0;
}
static int __mount (
const char *source,
const char *target,
const char *type,
unsigned long flags)
{
int nb;
char cbuf[4096];
char * tokens[64];
if (type || (flags & MS_MOVE))
return mount(source, target, type, flags, NULL);
/* The filesystem is unknown.
* We will try each filesystem supported by the kernel. */
if (__read_text_file("/proc/filesystems", cbuf, sizeof(cbuf)))
return -1;
nb = __mkparam(cbuf, tokens, sizeof(tokens)/sizeof(tokens[0]), '\n');
while (nb--) {
/* note: the possible filesystems all start with a
* tabulation in that file, except ubifs */
if (!strncmp(tokens[nb], "nodev\tubifs", 11))
tokens[nb] += 5;
else if (*tokens[nb] != '\t')
continue;
/* skip the tabulation */
tokens[nb]++;
if (!mount(source, target, tokens[nb], flags, NULL))
return 0;
}
DEBUG("Failed attempt to mount %s on %s\n", source, target);
return -1;
}
static int __multi_mount (
char *source,
const char *target,
const char *type,
unsigned long flags,
int retries)
{
size_t try;
char c, *s, *t;
for (try = 0; try < retries; usleep(100000), try++) {
for (c = ',', s = source; c == ','; *s++ = c) {
for (t = s; *s != ',' && *s != '\0'; s++);
c = *s;
*s = '\0';
if (!__mount(t, target, type, flags)) {
INFO("%s mounted on %s\n", t, target);
*s = c;
return 0;
}
}
}
ERROR("Cannot mount %s on %s\n", source, target);
return -1;
}
static int __losetup (
const char *loop,
const char *file)
{
int loopfd, filefd, res;
filefd = open(file, O_RDONLY);
if (filefd < 0) {
ERROR("losetup: cannot open \'%s\'.\n", file);
return -1;
}
loopfd = open(loop, O_RDONLY);
if (loopfd < 0) {
ERROR("losetup: cannot open \'%s\'.\n", file);
close(filefd);
return -1;
}
res = losetup(loopfd, filefd, file);
if (res < 0)
ERROR("Cannot setup loop device \'%s\'.\n", loop);
close(loopfd);
close(filefd);
return res;
}
int main(int argc, char **argv)
{
char loop_dev[11] = "/dev/loop0";
const char *inits [] = {
"/sbin/init",
"/etc/init",
"/bin/init",
"/bin/sh",
NULL,
};
int fd, boot = 0, is_backup = 0;
char sbuf [256];
char cbuf[4096];
int paramc;
char * paramv[64];
size_t i;
INFO("\n\n\nOpenDingux min-init 1.1.0 "
"by Ignacio Garcia Perez <iggarpe@gmail.com> "
"and Paul Cercueil <paul@crapouillou.net>\n");
DEBUG("Mounting /proc\n");
if ( mount(NULL, "/proc", "proc", 0, NULL) ) {
ERROR("Unable to mount /proc\n");
return -1;
}
DEBUG("Reading kernel command line\n");
if (__read_text_file("/proc/cmdline", cbuf, sizeof(cbuf)))
return -1;
DEBUG("Command line read: %s\n", cbuf);
/* paramv[0] and paramv[paramc] are reserved */
paramc = 1 + __mkparam(cbuf, paramv+1, sizeof(paramv)/sizeof(paramv[0]) -2, ' ');
/* Process "boot" parameter
* (only one, allow comma-separated list).
* Note that we specify 20 retries (2 seconds), just in case it is
* a hotplug device which takes some time to detect and initialize. */
for (i=1; i<paramc; i++) {
if (!strcmp(paramv[i], "rootfs_bak"))
is_backup = 1;
if (strncmp(paramv[i], "boot=", 5))
continue;
if ( __multi_mount(paramv[i]+5, "/boot", NULL, 0, 20) )
return -1;
boot = 1;
if (boot && is_backup)
break;
}
if (!boot)
WARNING("\'boot\' parameter not found.\n");
/* Check for a modules fs update */
if (!access("/boot/update_m.bin", R_OK | W_OK)) {
DEBUG("Modules update found!\n");
rename("/boot/modules.squashfs", "/boot/modules.squashfs.bak");
rename("/boot/modules.squashfs.sha1", "/boot/modules.squashfs.bak.sha1");
rename("/boot/update_m.bin", "/boot/modules.squashfs");
rename("/boot/update_m.bin.sha1", "/boot/modules.squashfs.sha1");
}
/* Process "loop" parameter (only one) */
for (i=1; i<paramc; i++) {
char old[128], *name;
if (strncmp(paramv[i], "loop", 4)
|| paramv[i][5] != '=') continue;
/* update the loop device filename if needed */
loop_dev[9] = paramv[i][4];
name = paramv[i] + 6;
sprintf(old, "%s.bak", name);
/* Check for a rootfs update */
if (boot && !access("/boot/update_r.bin", R_OK | W_OK)) {
char sha1_file[128];
sprintf(sha1_file, "%s.sha1", name);
DEBUG("RootFS update found!\n");
/* If rootfs_bak was not passed, or the backup is not available,
* make a backup of the current rootfs before the update */
if (!is_backup || access(old, F_OK)) {
char old_sha1_file[128];
sprintf(old_sha1_file, "%s.sha1", old);
rename(name, old);
rename(sha1_file, old_sha1_file);
}
rename("/boot/update_r.bin", name);
rename("/boot/update_r.bin.sha1", sha1_file);
sync();
}
if (is_backup && !access(old, F_OK))
name = old;
DEBUG("Setting up loopback: \'%s\' associated to \'%s\'.\n",
loop_dev, name);
__losetup(loop_dev, name);
break;
}
/* Process "root" parameter (only one, allow comma-separated list).
* Note that we specify 20 retries (2 seconds), just in case it is
* a hotplug device which takes some time to detect and initialize. */
for (i=1; i<paramc; i++) {
if (strncmp(paramv[i], "root=", 5))
continue;
if ( __multi_mount(paramv[i]+5, "/root", NULL, MS_RDONLY, 20) )
return -1;
break;
}
/* TODO: try with real-root-dev */
if (i >= paramc) {
ERROR("\'root\' parameter not found.\n");
return -1;
}
/* Move the /boot mountpoint so that it is visible
* on the new filesystem tree */
if (boot) {
DEBUG("Moving \'/boot\' mountpoint\n");
if ( mount("/boot", "/root/boot", NULL, MS_MOVE, NULL) ) {
ERROR("Unable to move the \'/boot\' mountpoint.\n");
return -1;
}
/* Remount /boot readonly */
DEBUG("Remounting \'/root/boot\' read-only\n");
if ( mount("/root/boot", "/root/boot", NULL,
MS_REMOUNT | MS_RDONLY, NULL) ) {
ERROR("Unable to remount \'/root/boot\' read-only.\n");
return -1;
}
}
/* Now let's switch to the new root */
DEBUG("Switching root\n");
if (chdir("/root") < 0) {
ERROR("Unable to change to \'/root\' directory.\n");
return -1;
}
/* Re-open the console device at the new location
* (must already exist). */
fd = open("/root/dev/console", O_RDWR, 0);
if (fd < 0) {
ERROR("Unable to re-open console.\n");
return -1;
}
if ((dup2(fd, 0) != 0)
|| (dup2(fd, 1) != 1)
|| (dup2(fd, 2) != 2)) {
ERROR("Unable to duplicate console handles.\n");
return -1;
}
if (fd > 2) close(fd);
/* Keep the old root open until the chroot is done */
fd = open("/", O_RDONLY, 0);
/* Unmount the previously mounted /proc filesystem. */
umount("/proc");
/* Do the root switch */
if ( mount(".", "/", NULL, MS_MOVE, NULL) ) {
ERROR("Unable to switch to the new root.\n");
close(fd);
return -1;
}
if ((chroot(".") < 0) || (chdir("/") < 0)) {
ERROR("\'chroot\' failed.\n");
close(fd);
return -1;
}
/* Release the old root */
close(fd);
/* Prepare paramv[0] which is the init program itself */
for (i=1; i<paramc; i++) {
if (strncmp(paramv[i], "init=", 5))
continue;
strcpy(sbuf, paramv[i]+5);
break;
}
/* If no 'init=' is found on the command line, we try to
* locate the init program. */
if (i >= paramc) {
for (i=0; inits[i] && access(inits[i], X_OK)<0; i++);
if (!inits[i]) {
ERROR("Unable to find the \'init\' executable.\n");
return -1;
}
strcpy(sbuf, inits[i]);
}
paramv[0] = sbuf;
/* Execute the 'init' executable */
execv(paramv[0], paramv);
ERROR("exec or init failed.\n");
return 0;
}