729978ea2f2af790653baaee5ae788f05a88a177
[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 #include <fcntl.h>
19
20 #include <libubox/uloop.h>
21 #include <libubox/utils.h>
22 #include <libubus.h>
23
24 #include <stdio.h>
25
26 #include <unistd.h>
27
28 #include "init.h"
29 #include "../watchdog.h"
30
31 static struct uloop_process preinit_proc;
32 static struct uloop_process plugd_proc;
33
34 static void
35 check_dbglvl(void)
36 {
37 FILE *fp = fopen("/tmp/debug_level", "r");
38 int lvl = 0;
39
40 if (!fp)
41 return;
42 if (fscanf(fp, "%d", &lvl) == EOF)
43 ERROR("failed to read debug level\n");
44 fclose(fp);
45 unlink("/tmp/debug_level");
46
47 if (lvl > 0 && lvl < 5)
48 debug = lvl;
49 }
50
51 static void
52 spawn_procd(struct uloop_process *proc, int ret)
53 {
54 char *wdt_fd = watchdog_fd();
55 char *argv[] = { "/sbin/procd", NULL};
56 struct stat s;
57 char dbg[2];
58
59 if (plugd_proc.pid > 0)
60 kill(plugd_proc.pid, SIGKILL);
61
62 if (!stat("/tmp/sysupgrade", &s))
63 while (true)
64 sleep(1);
65
66 unsetenv("INITRAMFS");
67 unsetenv("PREINIT");
68 unlink("/tmp/.preinit");
69 DEBUG(2, "Exec to real procd now\n");
70 if (wdt_fd)
71 setenv("WDTFD", wdt_fd, 1);
72 check_dbglvl();
73 if (debug > 0) {
74 snprintf(dbg, 2, "%d", debug);
75 setenv("DBGLVL", dbg, 1);
76 }
77
78 execvp(argv[0], argv);
79 }
80
81 static void
82 plugd_proc_cb(struct uloop_process *proc, int ret)
83 {
84 proc->pid = 0;
85 }
86
87 void
88 preinit(void)
89 {
90 char *init[] = { "/bin/sh", "/etc/preinit", NULL };
91 char *plug[] = { "/sbin/procd", "-h", "/etc/hotplug-preinit.json", NULL };
92 int fd;
93
94 LOG("- preinit -\n");
95
96 plugd_proc.cb = plugd_proc_cb;
97 plugd_proc.pid = fork();
98 if (!plugd_proc.pid) {
99 execvp(plug[0], plug);
100 ERROR("Failed to start plugd\n");
101 exit(-1);
102 }
103 if (plugd_proc.pid <= 0) {
104 ERROR("Failed to start new plugd instance\n");
105 return;
106 }
107 uloop_process_add(&plugd_proc);
108
109 setenv("PREINIT", "1", 1);
110
111 fd = creat("/tmp/.preinit", 0600);
112
113 if (fd < 0)
114 ERROR("Failed to create sentinel file\n");
115 else
116 close(fd);
117
118 preinit_proc.cb = spawn_procd;
119 preinit_proc.pid = fork();
120 if (!preinit_proc.pid) {
121 execvp(init[0], init);
122 ERROR("Failed to start preinit\n");
123 exit(-1);
124 }
125 if (preinit_proc.pid <= 0) {
126 ERROR("Failed to start new preinit instance\n");
127 return;
128 }
129 uloop_process_add(&preinit_proc);
130
131 DEBUG(4, "Launched preinit instance, pid=%d\n", (int) preinit_proc.pid);
132 }