2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
16 #include <sys/types.h>
18 #include <sys/reboot.h>
20 #include <libubox/uloop.h>
32 #if defined(WITH_SELINUX)
33 #include <selinux/selinux.h>
34 #include <selinux/restorecon.h>
35 #include <selinux/avc.h>
38 #include "../utils/utils.h"
40 #include "../watchdog.h"
42 unsigned int debug
= 0;
45 signal_shutdown(int signal
, siginfo_t
*siginfo
, void *data
)
47 fprintf(stderr
, "reboot\n");
56 static struct sigaction sa_shutdown
= {
57 .sa_sigaction
= signal_shutdown
,
58 .sa_flags
= SA_SIGINFO
68 res
= get_cmdline_val("init_debug", line
, sizeof(line
));
70 r
= strtol(line
, NULL
, 10);
71 if ((r
!= LONG_MIN
) && (r
!= LONG_MAX
))
76 #if defined(WITH_SELINUX)
81 int enforce
= selinux_status_getenforce();
83 /* is SELinux already initialized? */
84 if (getenv("SELINUX_INIT")) {
85 /* have initramfs permissions already been restored? */
86 if (!getenv("INITRAMFS") || getenv("SELINUX_RESTORECON")) {
87 unsetenv("SELINUX_INIT");
88 unsetenv("SELINUX_RESTORECON");
91 /* Second call (initramfs only): restore filesystem labels */
92 const char *exclude_list
[] = { "/dev/console", "/proc", "/sys", 0 };
93 selinux_restorecon_set_exclude_list(exclude_list
);
94 ret
= selinux_restorecon("/", SELINUX_RESTORECON_RECURSE
| SELINUX_RESTORECON_MASS_RELABEL
);
95 putenv("SELINUX_RESTORECON=1");
97 /* First call: load policy */
98 ret
= selinux_init_load_policy(&enforce
);
99 putenv("SELINUX_INIT=1");
103 execv(argv
[0], argv
);
106 fprintf(stderr
, "Cannot load SELinux policy, but system in enforcing mode. Halting.\n");
121 main(int argc
, char **argv
)
125 ulog_open(ULOG_KMSG
, LOG_DAEMON
, "init");
127 sigaction(SIGTERM
, &sa_shutdown
, NULL
);
128 sigaction(SIGUSR1
, &sa_shutdown
, NULL
);
129 sigaction(SIGUSR2
, &sa_shutdown
, NULL
);
130 sigaction(SIGPWR
, &sa_shutdown
, NULL
);
140 char *kmod
[] = { "/sbin/kmodloader", "/etc/modules-boot.d/", NULL
};
143 patch_stdio("/dev/null");
145 execvp(kmod
[0], kmod
);
146 ERROR("Failed to start kmodloader: %m\n");
150 ERROR("Failed to start kmodloader instance: %m\n");
152 const struct timespec req
= {0, 10 * 1000 * 1000};
155 for (i
= 0; i
< 1200; i
++) {
156 if (waitpid(pid
, NULL
, WNOHANG
) > 0)
158 nanosleep(&req
, NULL
);