c015ebd28d91c8ece7907b42214468e43ee36b85
[project/procd.git] / preinit.c
1 /*
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <sys/mount.h>
18
19 #include <unistd.h>
20 #include <unistd.h>
21
22 #include "procd.h"
23 #include "hotplug.h"
24 #include "watchdog.h"
25
26 static struct uloop_process preinit;
27
28 static void spawn_procd(struct uloop_process *proc, int ret)
29 {
30 char *wdt_fd = watchdog_fd();
31 char *argv[] = { "/sbin/procd", NULL };
32 struct stat s;
33
34 if (!stat("/tmp/sysupgrade", &s))
35 while (true)
36 sleep(1);
37
38 unsetenv("INITRAMFS");
39 unsetenv("PREINIT");
40 DEBUG(1, "Exec to real procd now\n");
41 if (wdt_fd)
42 setenv("WDTFD", wdt_fd, 1);
43 execvp(argv[0], argv);
44 }
45
46 void procd_preinit(void)
47 {
48 char *argv[] = { "/bin/sh", "/etc/preinit", NULL };
49
50 LOG("- preinit -\n");
51
52 setenv("PREINIT", "1", 1);
53 preinit.cb = spawn_procd;
54 preinit.pid = fork();
55 if (!preinit.pid) {
56 execvp(argv[0], argv);
57 ERROR("Failed to start preinit\n");
58 exit(-1);
59 }
60
61 if (preinit.pid <= 0) {
62 ERROR("Failed to start new preinit instance\n");
63 return;
64 }
65
66 uloop_process_add(&preinit);
67 DEBUG(2, "Launched preinit instance, pid=%d\n", (int) preinit.pid);
68 }