Convert log calls to ulog() api
[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 check_dbglvl(void)
35 {
36         FILE *fp = fopen("/tmp/debug_level", "r");
37         int lvl = 0;
38
39         if (!fp)
40                 return;
41         fscanf(fp, "%d", &lvl);
42         fclose(fp);
43         unlink("/tmp/debug_level");
44
45         if (lvl > 0 && lvl < 5)
46                 debug = lvl;
47 }
48
49 static void
50 spawn_procd(struct uloop_process *proc, int ret)
51 {
52         char *wdt_fd = watchdog_fd();
53         char *argv[] = { "/sbin/procd", NULL};
54         struct stat s;
55         char dbg[2];
56
57         if (plugd_proc.pid > 0)
58                 kill(plugd_proc.pid, SIGKILL);
59
60         if (!stat("/tmp/sysupgrade", &s))
61                 while (true)
62                         sleep(1);
63
64         unsetenv("INITRAMFS");
65         unsetenv("PREINIT");
66         DEBUG(2, "Exec to real procd now\n");
67         if (wdt_fd)
68                 setenv("WDTFD", wdt_fd, 1);
69         check_dbglvl();
70         if (debug > 0) {
71                 snprintf(dbg, 2, "%d", debug);
72                 setenv("DBGLVL", dbg, 1);
73         }
74
75         execvp(argv[0], argv);
76 }
77
78 static void
79 plugd_proc_cb(struct uloop_process *proc, int ret)
80 {
81         proc->pid = 0;
82 }
83
84 void
85 preinit(void)
86 {
87         char *init[] = { "/bin/sh", "/etc/preinit", NULL };
88         char *plug[] = { "/sbin/procd", "-h", "/etc/hotplug-preinit.json", NULL };
89
90         LOG("- preinit -\n");
91
92         plugd_proc.cb = plugd_proc_cb;
93         plugd_proc.pid = fork();
94         if (!plugd_proc.pid) {
95                 execvp(plug[0], plug);
96                 ERROR("Failed to start plugd\n");
97                 exit(-1);
98         }
99         if (plugd_proc.pid <= 0) {
100                 ERROR("Failed to start new plugd instance\n");
101                 return;
102         }
103         uloop_process_add(&plugd_proc);
104
105         setenv("PREINIT", "1", 1);
106
107         preinit_proc.cb = spawn_procd;
108         preinit_proc.pid = fork();
109         if (!preinit_proc.pid) {
110                 execvp(init[0], init);
111                 ERROR("Failed to start preinit\n");
112                 exit(-1);
113         }
114         if (preinit_proc.pid <= 0) {
115                 ERROR("Failed to start new preinit instance\n");
116                 return;
117         }
118         uloop_process_add(&preinit_proc);
119
120         DEBUG(4, "Launched preinit instance, pid=%d\n", (int) preinit_proc.pid);
121 }