2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
16 #include <sys/types.h>
17 #include <sys/mount.h>
19 #include <libubox/uloop.h>
20 #include <libubox/utils.h>
28 #include "../watchdog.h"
30 static struct uloop_process preinit_proc;
31 static struct uloop_process plugd_proc;
36 FILE *fp = fopen("/tmp/debug_level", "r");
41 if (fscanf(fp, "%d", &lvl) == EOF)
42 ERROR("failed to read debug level\n");
44 unlink("/tmp/debug_level");
46 if (lvl > 0 && lvl < 5)
51 spawn_procd(struct uloop_process *proc, int ret)
53 char *wdt_fd = watchdog_fd();
54 char *argv[] = { "/sbin/procd", NULL};
58 if (plugd_proc.pid > 0)
59 kill(plugd_proc.pid, SIGKILL);
61 if (!stat("/tmp/sysupgrade", &s))
65 unsetenv("INITRAMFS");
67 DEBUG(2, "Exec to real procd now\n");
69 setenv("WDTFD", wdt_fd, 1);
72 snprintf(dbg, 2, "%d", debug);
73 setenv("DBGLVL", dbg, 1);
76 execvp(argv[0], argv);
80 plugd_proc_cb(struct uloop_process *proc, int ret)
88 char *init[] = { "/bin/sh", "/etc/preinit", NULL };
89 char *plug[] = { "/sbin/procd", "-h", "/etc/hotplug-preinit.json", NULL };
93 plugd_proc.cb = plugd_proc_cb;
94 plugd_proc.pid = fork();
95 if (!plugd_proc.pid) {
96 execvp(plug[0], plug);
97 ERROR("Failed to start plugd\n");
100 if (plugd_proc.pid <= 0) {
101 ERROR("Failed to start new plugd instance\n");
104 uloop_process_add(&plugd_proc);
106 setenv("PREINIT", "1", 1);
108 preinit_proc.cb = spawn_procd;
109 preinit_proc.pid = fork();
110 if (!preinit_proc.pid) {
111 execvp(init[0], init);
112 ERROR("Failed to start preinit\n");
115 if (preinit_proc.pid <= 0) {
116 ERROR("Failed to start new preinit instance\n");
119 uloop_process_add(&preinit_proc);
121 DEBUG(4, "Launched preinit instance, pid=%d\n", (int) preinit_proc.pid);