preinit: define _GNU_SOURCE
[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
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 spawn_procd(struct uloop_process *proc, int ret)
54 {
55 char *wdt_fd = watchdog_fd();
56 char *argv[] = { "/sbin/procd", NULL};
57 struct stat s;
58 char dbg[2];
59
60 if (plugd_proc.pid > 0)
61 kill(plugd_proc.pid, SIGKILL);
62
63 if (!stat("/tmp/sysupgrade", &s))
64 while (true)
65 sleep(1);
66
67 unsetenv("INITRAMFS");
68 unsetenv("PREINIT");
69 unlink("/tmp/.preinit");
70 DEBUG(2, "Exec to real procd now\n");
71 if (wdt_fd)
72 setenv("WDTFD", wdt_fd, 1);
73 check_dbglvl();
74 if (debug > 0) {
75 snprintf(dbg, 2, "%d", debug);
76 setenv("DBGLVL", dbg, 1);
77 }
78
79 execvp(argv[0], argv);
80 }
81
82 static void
83 plugd_proc_cb(struct uloop_process *proc, int ret)
84 {
85 proc->pid = 0;
86 }
87
88 void
89 preinit(void)
90 {
91 char *init[] = { "/bin/sh", "/etc/preinit", NULL };
92 char *plug[] = { "/sbin/procd", "-h", "/etc/hotplug-preinit.json", NULL };
93 int fd;
94
95 LOG("- preinit -\n");
96
97 plugd_proc.cb = plugd_proc_cb;
98 plugd_proc.pid = fork();
99 if (!plugd_proc.pid) {
100 execvp(plug[0], plug);
101 ERROR("Failed to start plugd\n");
102 exit(-1);
103 }
104 if (plugd_proc.pid <= 0) {
105 ERROR("Failed to start new plugd instance\n");
106 return;
107 }
108 uloop_process_add(&plugd_proc);
109
110 setenv("PREINIT", "1", 1);
111
112 fd = creat("/tmp/.preinit", 0600);
113
114 if (fd < 0)
115 ERROR("Failed to create sentinel file\n");
116 else
117 close(fd);
118
119 preinit_proc.cb = spawn_procd;
120 preinit_proc.pid = fork();
121 if (!preinit_proc.pid) {
122 execvp(init[0], init);
123 ERROR("Failed to start preinit\n");
124 exit(-1);
125 }
126 if (preinit_proc.pid <= 0) {
127 ERROR("Failed to start new preinit instance\n");
128 return;
129 }
130 uloop_process_add(&preinit_proc);
131
132 DEBUG(4, "Launched preinit instance, pid=%d\n", (int) preinit_proc.pid);
133 }