init.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #include <sys/wait.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <sys/reboot.h>
  18. #include <libubox/uloop.h>
  19. #include <libubus.h>
  20. #include <limits.h>
  21. #include <stdlib.h>
  22. #include <fcntl.h>
  23. #include <getopt.h>
  24. #include <libgen.h>
  25. #include <regex.h>
  26. #include <unistd.h>
  27. #include <stdio.h>
  28. #if defined(WITH_SELINUX)
  29. #include <selinux/selinux.h>
  30. #include <selinux/restorecon.h>
  31. #include <selinux/avc.h>
  32. #endif
  33. #include "../utils/utils.h"
  34. #include "init.h"
  35. #include "../watchdog.h"
  36. unsigned int debug = 0;
  37. static void
  38. signal_shutdown(int signal, siginfo_t *siginfo, void *data)
  39. {
  40. fprintf(stderr, "reboot\n");
  41. fflush(stderr);
  42. sync();
  43. sleep(2);
  44. reboot(RB_AUTOBOOT);
  45. while (1)
  46. ;
  47. }
  48. static struct sigaction sa_shutdown = {
  49. .sa_sigaction = signal_shutdown,
  50. .sa_flags = SA_SIGINFO
  51. };
  52. static void
  53. cmdline(void)
  54. {
  55. char line[20];
  56. char* res;
  57. long r;
  58. res = get_cmdline_val("init_debug", line, sizeof(line));
  59. if (res != NULL) {
  60. r = strtol(line, NULL, 10);
  61. if ((r != LONG_MIN) && (r != LONG_MAX))
  62. debug = (int) r;
  63. }
  64. }
  65. #if defined(WITH_SELINUX)
  66. static int
  67. selinux(char **argv)
  68. {
  69. int ret;
  70. int enforce = selinux_status_getenforce();
  71. /* is SELinux already initialized? */
  72. if (getenv("SELINUX_INIT")) {
  73. /* have initramfs permissions already been restored? */
  74. if (!getenv("INITRAMFS") || getenv("SELINUX_RESTORECON")) {
  75. unsetenv("SELINUX_INIT");
  76. unsetenv("SELINUX_RESTORECON");
  77. return 0;
  78. }
  79. /* Second call (initramfs only): restore filesystem labels */
  80. const char *exclude_list[] = { "/dev/console", "/proc", "/sys", 0 };
  81. selinux_restorecon_set_exclude_list(exclude_list);
  82. ret = selinux_restorecon("/", SELINUX_RESTORECON_RECURSE | SELINUX_RESTORECON_MASS_RELABEL);
  83. putenv("SELINUX_RESTORECON=1");
  84. } else {
  85. /* First call: load policy */
  86. ret = selinux_init_load_policy(&enforce);
  87. putenv("SELINUX_INIT=1");
  88. }
  89. if (ret == 0)
  90. execv(argv[0], argv);
  91. if (enforce > 0) {
  92. fprintf(stderr, "Cannot load SELinux policy, but system in enforcing mode. Halting.\n");
  93. return 1;
  94. }
  95. return 0;
  96. }
  97. #else
  98. static int
  99. selinux(char **argv)
  100. {
  101. return 0;
  102. }
  103. #endif
  104. int
  105. main(int argc, char **argv)
  106. {
  107. pid_t pid;
  108. ulog_open(ULOG_KMSG, LOG_DAEMON, "init");
  109. sigaction(SIGTERM, &sa_shutdown, NULL);
  110. sigaction(SIGUSR1, &sa_shutdown, NULL);
  111. sigaction(SIGUSR2, &sa_shutdown, NULL);
  112. sigaction(SIGPWR, &sa_shutdown, NULL);
  113. if (selinux(argv))
  114. exit(-1);
  115. early();
  116. cmdline();
  117. watchdog_init(1);
  118. pid = fork();
  119. if (!pid) {
  120. char *kmod[] = { "/sbin/kmodloader", "/etc/modules-boot.d/", NULL };
  121. if (debug < 3)
  122. patch_stdio("/dev/null");
  123. execvp(kmod[0], kmod);
  124. ERROR("Failed to start kmodloader: %m\n");
  125. exit(EXIT_FAILURE);
  126. }
  127. if (pid <= 0) {
  128. ERROR("Failed to start kmodloader instance: %m\n");
  129. } else {
  130. const struct timespec req = {0, 10 * 1000 * 1000};
  131. int i;
  132. for (i = 0; i < 1200; i++) {
  133. if (waitpid(pid, NULL, WNOHANG) > 0)
  134. break;
  135. nanosleep(&req, NULL);
  136. watchdog_ping();
  137. }
  138. }
  139. uloop_init();
  140. preinit();
  141. uloop_run();
  142. return 0;
  143. }