fix hotplug
[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
33         unsetenv("INITRAMFS");
34         unsetenv("PREINIT");
35         DEBUG(1, "Exec to real procd now\n");
36         if (wdt_fd)
37                 setenv("WDTFD", wdt_fd, 1);
38         execvp(argv[0], argv);
39 }
40
41 void procd_preinit(void)
42 {
43         char *argv[] = { "/bin/sh", "/etc/preinit", NULL };
44
45         LOG("- preinit -\n");
46
47         setenv("PREINIT", "1", 1);
48         preinit.cb = spawn_procd;
49         preinit.pid = fork();
50         if (!preinit.pid) {
51                 execvp(argv[0], argv);
52                 ERROR("Failed to start preinit\n");
53                 exit(-1);
54         }
55
56         if (preinit.pid <= 0) {
57                 ERROR("Failed to start new preinit instance\n");
58                 return;
59         }
60
61         uloop_process_add(&preinit);
62         DEBUG(2, "Launched preinit instance, pid=%d\n", (int) preinit.pid);
63 }