make the service running trigger be queued directly after the service was startetd
[project/procd.git] / initd / 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 <libubox/uloop.h>
20 #include <libubox/utils.h>
21 #include <libubus.h>
22
23 #include <stdio.h>
24
25 #include <unistd.h>
26
27 #include "init.h"
28 #include "../watchdog.h"
29
30 static struct uloop_process preinit_proc;
31 static struct uloop_process plugd_proc;
32
33 static void
34 spawn_procd(struct uloop_process *proc, int ret)
35 {
36         char *wdt_fd = watchdog_fd();
37         char *argv[] = { "/sbin/procd", NULL };
38         struct stat s;
39
40         if (plugd_proc.pid > 0)
41                 kill(plugd_proc.pid, SIGKILL);
42
43         if (!stat("/tmp/sysupgrade", &s))
44                 while (true)
45                         sleep(1);
46
47         unsetenv("INITRAMFS");
48         unsetenv("PREINIT");
49         DEBUG(2, "Exec to real procd now\n");
50         if (wdt_fd)
51                 setenv("WDTFD", wdt_fd, 1);
52         execvp(argv[0], argv);
53 }
54
55 static void
56 plugd_proc_cb(struct uloop_process *proc, int ret)
57 {
58         proc->pid = 0;
59 }
60
61 void
62 preinit(void)
63 {
64         char *init[] = { "/bin/sh", "/etc/preinit", NULL };
65         char *plug[] = { "/sbin/procd", "-h", "/etc/hotplug-preinit.json", NULL };
66
67         LOG("- preinit -\n");
68
69         plugd_proc.cb = plugd_proc_cb;
70         plugd_proc.pid = fork();
71         if (!plugd_proc.pid) {
72                 execvp(plug[0], plug);
73                 ERROR("Failed to start plugd\n");
74                 exit(-1);
75         }
76         if (plugd_proc.pid <= 0) {
77                 ERROR("Failed to start new plugd instance\n");
78                 return;
79         }
80         uloop_process_add(&plugd_proc);
81
82         setenv("PREINIT", "1", 1);
83
84         preinit_proc.cb = spawn_procd;
85         preinit_proc.pid = fork();
86         if (!preinit_proc.pid) {
87                 execvp(init[0], init);
88                 ERROR("Failed to start preinit\n");
89                 exit(-1);
90         }
91         if (preinit_proc.pid <= 0) {
92                 ERROR("Failed to start new preinit instance\n");
93                 return;
94         }
95         uloop_process_add(&preinit_proc);
96
97         DEBUG(4, "Launched preinit instance, pid=%d\n", (int) preinit_proc.pid);
98 }