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.
17 #include <sys/types.h>
18 #include <sys/mount.h>
21 #include <libubox/uloop.h>
22 #include <libubox/utils.h>
30 #include "../watchdog.h"
31 #include "../sysupgrade.h"
33 static struct uloop_process preinit_proc
;
34 static struct uloop_process plugd_proc
;
39 FILE *fp
= fopen("/tmp/debug_level", "r");
44 if (fscanf(fp
, "%d", &lvl
) == EOF
)
45 ERROR("failed to read debug level: %m\n");
47 unlink("/tmp/debug_level");
49 if (lvl
> 0 && lvl
< 5)
54 check_sysupgrade(void)
56 char *prefix
= NULL
, *path
= NULL
, *command
= NULL
;
62 FILE *sysupgrade
= fopen("/tmp/sysupgrade", "r");
67 if (getdelim(&prefix
, &n
, 0, sysupgrade
) < 0)
70 if (getdelim(&path
, &n
, 0, sysupgrade
) < 0)
73 if (getdelim(&command
, &n
, 0, sysupgrade
) < 0)
78 sysupgrade_exec_upgraded(prefix
, path
, NULL
, command
, NULL
);
91 spawn_procd(struct uloop_process
*proc
, int ret
)
93 char *wdt_fd
= watchdog_fd();
94 char *argv
[] = { "/sbin/procd", NULL
};
97 if (plugd_proc
.pid
> 0)
98 kill(plugd_proc
.pid
, SIGKILL
);
101 unlink("/tmp/.preinit");
105 DEBUG(2, "Exec to real procd now\n");
107 setenv("WDTFD", wdt_fd
, 1);
110 snprintf(dbg
, 2, "%d", debug
);
111 setenv("DBGLVL", dbg
, 1);
114 execvp(argv
[0], argv
);
118 plugd_proc_cb(struct uloop_process
*proc
, int ret
)
126 char *init
[] = { "/bin/sh", "/etc/preinit", NULL
};
127 char *plug
[] = { "/sbin/procd", "-h", "/etc/hotplug-preinit.json", NULL
};
130 LOG("- preinit -\n");
132 plugd_proc
.cb
= plugd_proc_cb
;
133 plugd_proc
.pid
= fork();
134 if (!plugd_proc
.pid
) {
135 execvp(plug
[0], plug
);
136 ERROR("Failed to start plugd: %m\n");
139 if (plugd_proc
.pid
<= 0) {
140 ERROR("Failed to start new plugd instance: %m\n");
143 uloop_process_add(&plugd_proc
);
145 setenv("PREINIT", "1", 1);
147 fd
= creat("/tmp/.preinit", 0600);
150 ERROR("Failed to create sentinel file: %m\n");
154 preinit_proc
.cb
= spawn_procd
;
155 preinit_proc
.pid
= fork();
156 if (!preinit_proc
.pid
) {
157 execvp(init
[0], init
);
158 ERROR("Failed to start preinit: %m\n");
161 if (preinit_proc
.pid
<= 0) {
162 ERROR("Failed to start new preinit instance: %m\n");
165 uloop_process_add(&preinit_proc
);
167 DEBUG(4, "Launched preinit instance, pid=%d\n", (int) preinit_proc
.pid
);