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