84e62b0d4af1179b20f57d6a85b5c92710e9e793
[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 #define _GNU_SOURCE
15
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <sys/mount.h>
19 #include <fcntl.h>
20
21 #include <libubox/uloop.h>
22 #include <libubox/utils.h>
23 #include <libubus.h>
24
25 #include <stdio.h>
26
27 #include <unistd.h>
28
29 #include "init.h"
30 #include "../watchdog.h"
31 #include "../sysupgrade.h"
32
33 static struct uloop_process preinit_proc;
34 static struct uloop_process plugd_proc;
35
36 static void
37 check_dbglvl(void)
38 {
39 FILE *fp = fopen("/tmp/debug_level", "r");
40 int lvl = 0;
41
42 if (!fp)
43 return;
44 if (fscanf(fp, "%d", &lvl) == EOF)
45 ERROR("failed to read debug level: %m\n");
46 fclose(fp);
47 unlink("/tmp/debug_level");
48
49 if (lvl > 0 && lvl < 5)
50 debug = lvl;
51 }
52
53 static void
54 check_sysupgrade(void)
55 {
56 char *prefix = NULL, *path = NULL, *command = NULL;
57 size_t n;
58
59 if (chdir("/"))
60 return;
61
62 FILE *sysupgrade = fopen("/tmp/sysupgrade", "r");
63 if (!sysupgrade)
64 return;
65
66 n = 0;
67 if (getdelim(&prefix, &n, 0, sysupgrade) < 0)
68 goto fail;
69 n = 0;
70 if (getdelim(&path, &n, 0, sysupgrade) < 0)
71 goto fail;
72 n = 0;
73 if (getdelim(&command, &n, 0, sysupgrade) < 0)
74 goto fail;
75
76 fclose(sysupgrade);
77
78 sysupgrade_exec_upgraded(prefix, path, NULL, command, NULL);
79
80 while (true)
81 sleep(1);
82
83 fail:
84 fclose(sysupgrade);
85 free(prefix);
86 free(path);
87 free(command);
88 }
89
90 static void
91 spawn_procd(struct uloop_process *proc, int ret)
92 {
93 char *wdt_fd = watchdog_fd();
94 char *argv[] = { "/sbin/procd", NULL};
95 char dbg[2];
96
97 if (plugd_proc.pid > 0)
98 kill(plugd_proc.pid, SIGKILL);
99
100 unsetenv("INITRAMFS");
101 unsetenv("PREINIT");
102 unlink("/tmp/.preinit");
103
104 check_sysupgrade();
105
106 DEBUG(2, "Exec to real procd now\n");
107 if (wdt_fd)
108 setenv("WDTFD", wdt_fd, 1);
109 check_dbglvl();
110 if (debug > 0) {
111 snprintf(dbg, 2, "%d", debug);
112 setenv("DBGLVL", dbg, 1);
113 }
114
115 execvp(argv[0], argv);
116 }
117
118 static void
119 plugd_proc_cb(struct uloop_process *proc, int ret)
120 {
121 proc->pid = 0;
122 }
123
124 void
125 preinit(void)
126 {
127 char *init[] = { "/bin/sh", "/etc/preinit", NULL };
128 char *plug[] = { "/sbin/procd", "-h", "/etc/hotplug-preinit.json", NULL };
129 int fd;
130
131 LOG("- preinit -\n");
132
133 plugd_proc.cb = plugd_proc_cb;
134 plugd_proc.pid = fork();
135 if (!plugd_proc.pid) {
136 execvp(plug[0], plug);
137 ERROR("Failed to start plugd: %m\n");
138 exit(-1);
139 }
140 if (plugd_proc.pid <= 0) {
141 ERROR("Failed to start new plugd instance: %m\n");
142 return;
143 }
144 uloop_process_add(&plugd_proc);
145
146 setenv("PREINIT", "1", 1);
147
148 fd = creat("/tmp/.preinit", 0600);
149
150 if (fd < 0)
151 ERROR("Failed to create sentinel file: %m\n");
152 else
153 close(fd);
154
155 preinit_proc.cb = spawn_procd;
156 preinit_proc.pid = fork();
157 if (!preinit_proc.pid) {
158 execvp(init[0], init);
159 ERROR("Failed to start preinit: %m\n");
160 exit(-1);
161 }
162 if (preinit_proc.pid <= 0) {
163 ERROR("Failed to start new preinit instance: %m\n");
164 return;
165 }
166 uloop_process_add(&preinit_proc);
167
168 DEBUG(4, "Launched preinit instance, pid=%d\n", (int) preinit_proc.pid);
169 }