halt.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Poweroff reboot and halt, oh my.
  4. *
  5. * Copyright 2006 by Rob Landley <rob@landley.net>
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. //config:config HALT
  10. //config: bool "halt (4 kb)"
  11. //config: default y
  12. //config: help
  13. //config: Stop all processes and halt the system.
  14. //config:
  15. //config:config POWEROFF
  16. //config: bool "poweroff (4 kb)"
  17. //config: default y
  18. //config: help
  19. //config: Stop all processes and power off the system.
  20. //config:
  21. //config:config REBOOT
  22. //config: bool "reboot (4 kb)"
  23. //config: default y
  24. //config: help
  25. //config: Stop all processes and reboot the system.
  26. //config:
  27. //config:config FEATURE_WAIT_FOR_INIT
  28. //config: bool "Before signaling init, make sure it is ready for it"
  29. //config: default y
  30. //config: depends on HALT || POWEROFF || REBOOT
  31. //config: help
  32. //config: In rare cases, poweroff may be commanded by firmware to OS
  33. //config: even before init process exists. On Linux, this spawns
  34. //config: "/sbin/poweroff" very early. This option adds code
  35. //config: which checks that init is ready to receive poweroff
  36. //config: commands. Code size increase of ~80 bytes.
  37. //config:
  38. //config:config FEATURE_CALL_TELINIT
  39. //config: bool "Call telinit on shutdown and reboot"
  40. //config: default y
  41. //config: depends on (HALT || POWEROFF || REBOOT) && !INIT
  42. //config: help
  43. //config: Call an external program (normally telinit) to facilitate
  44. //config: a switch to a proper runlevel.
  45. //config:
  46. //config: This option is only available if you selected halt and friends,
  47. //config: but did not select init.
  48. //config:
  49. //config:config TELINIT_PATH
  50. //config: string "Path to telinit executable"
  51. //config: default "/sbin/telinit"
  52. //config: depends on FEATURE_CALL_TELINIT
  53. //config: help
  54. //config: When busybox halt and friends have to call external telinit
  55. //config: to facilitate proper shutdown, this path is to be used when
  56. //config: locating telinit executable.
  57. //applet:IF_HALT(APPLET(halt, BB_DIR_SBIN, BB_SUID_DROP))
  58. // APPLET_ODDNAME:name main location suid_type help
  59. //applet:IF_POWEROFF(APPLET_ODDNAME(poweroff, halt, BB_DIR_SBIN, BB_SUID_DROP, poweroff))
  60. //applet:IF_REBOOT( APPLET_ODDNAME(reboot, halt, BB_DIR_SBIN, BB_SUID_DROP, reboot))
  61. //kbuild:lib-$(CONFIG_HALT) += halt.o
  62. //kbuild:lib-$(CONFIG_POWEROFF) += halt.o
  63. //kbuild:lib-$(CONFIG_REBOOT) += halt.o
  64. //usage:#define halt_trivial_usage
  65. //usage: "[-d DELAY] [-n] [-f]" IF_FEATURE_WTMP(" [-w]")
  66. //usage:#define halt_full_usage "\n\n"
  67. //usage: "Halt the system\n"
  68. //usage: "\n -d SEC Delay interval"
  69. //usage: "\n -n Do not sync"
  70. //usage: "\n -f Force (don't go through init)"
  71. //usage: IF_FEATURE_WTMP(
  72. //usage: "\n -w Only write a wtmp record"
  73. //usage: )
  74. //usage:
  75. //usage:#define poweroff_trivial_usage
  76. //usage: "[-d DELAY] [-n] [-f]"
  77. //usage:#define poweroff_full_usage "\n\n"
  78. //usage: "Halt and shut off power\n"
  79. //usage: "\n -d SEC Delay interval"
  80. //usage: "\n -n Do not sync"
  81. //usage: "\n -f Force (don't go through init)"
  82. //usage:
  83. //usage:#define reboot_trivial_usage
  84. //usage: "[-d DELAY] [-n] [-f]"
  85. //usage:#define reboot_full_usage "\n\n"
  86. //usage: "Reboot the system\n"
  87. //usage: "\n -d SEC Delay interval"
  88. //usage: "\n -n Do not sync"
  89. //usage: "\n -f Force (don't go through init)"
  90. #include "libbb.h"
  91. #include "reboot.h"
  92. #if ENABLE_FEATURE_WTMP
  93. #include <sys/utsname.h>
  94. static void write_wtmp(void)
  95. {
  96. struct utmpx utmp;
  97. struct utsname uts;
  98. /* "man utmp" says wtmp file should *not* be created automagically */
  99. /*if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) {
  100. close(creat(bb_path_wtmp_file, 0664));
  101. }*/
  102. memset(&utmp, 0, sizeof(utmp));
  103. utmp.ut_tv.tv_sec = time(NULL);
  104. strcpy(utmp.ut_user, "shutdown"); /* it is wide enough */
  105. utmp.ut_type = RUN_LVL;
  106. utmp.ut_id[0] = '~'; utmp.ut_id[1] = '~'; /* = strcpy(utmp.ut_id, "~~"); */
  107. utmp.ut_line[0] = '~'; utmp.ut_line[1] = '~'; /* = strcpy(utmp.ut_line, "~~"); */
  108. uname(&uts);
  109. safe_strncpy(utmp.ut_host, uts.release, sizeof(utmp.ut_host));
  110. updwtmpx(bb_path_wtmp_file, &utmp);
  111. }
  112. #else
  113. #define write_wtmp() ((void)0)
  114. #endif
  115. #if ENABLE_FEATURE_WAIT_FOR_INIT
  116. /* In Linux, "poweroff" may be spawned even before init.
  117. * For example, with ACPI:
  118. * linux/drivers/acpi/bus.c:
  119. * static void sb_notify_work(struct work_struct *dummy)
  120. * orderly_poweroff(true);
  121. * linux/kernel/reboot.c:
  122. * static int run_cmd(const char *cmd)
  123. * ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  124. * poweroff_cmd[] = "/sbin/poweroff";
  125. * static int __orderly_poweroff(bool force)
  126. * ret = run_cmd(poweroff_cmd);
  127. *
  128. * We want to make sure init exists and listens to signals.
  129. */
  130. static int init_was_not_there(void)
  131. {
  132. enum { initial = 5 }; /* 5 seconds should be plenty for timeout */
  133. int cnt = initial - 1;
  134. /* Just existence of PID 1 does not mean it installed
  135. * the handlers already.
  136. */
  137. #if 0
  138. while (kill(1, 0) != 0 && --cnt >= 0)
  139. sleep(1);
  140. #endif
  141. /* ... so let's wait for some evidence a usual startup event,
  142. * mounting of /proc, happened. By that time init should be ready
  143. * for signals.
  144. */
  145. while (access("/proc/meminfo", F_OK) != 0 && --cnt >= 0)
  146. sleep(1);
  147. /* Does it look like init wasn't there? */
  148. return (cnt != initial - 1);
  149. }
  150. #else
  151. /* Assume it's always there */
  152. # define init_was_not_there() 0
  153. #endif
  154. int halt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  155. int halt_main(int argc UNUSED_PARAM, char **argv)
  156. {
  157. static const int magic[] = {
  158. RB_HALT_SYSTEM,
  159. RB_POWER_OFF,
  160. RB_AUTOBOOT
  161. };
  162. static const smallint signals[] = { SIGUSR1, SIGUSR2, SIGTERM };
  163. int delay = 0;
  164. int which, flags, rc;
  165. /* Figure out which applet we're running */
  166. if (ENABLE_HALT && !ENABLE_POWEROFF && !ENABLE_REBOOT)
  167. which = 0;
  168. else
  169. if (!ENABLE_HALT && ENABLE_POWEROFF && !ENABLE_REBOOT)
  170. which = 1;
  171. else
  172. if (!ENABLE_HALT && !ENABLE_POWEROFF && ENABLE_REBOOT)
  173. which = 2;
  174. else
  175. for (which = 0; "hpr"[which] != applet_name[0]; which++)
  176. continue;
  177. /* Parse and handle arguments */
  178. /* We support -w even if !ENABLE_FEATURE_WTMP,
  179. * in order to not break scripts.
  180. * -i (shut down network interfaces) is ignored.
  181. */
  182. flags = getopt32(argv, "d:+nfwi", &delay);
  183. sleep(delay);
  184. write_wtmp();
  185. if (flags & 8) /* -w */
  186. return EXIT_SUCCESS;
  187. if (!(flags & 2)) /* no -n */
  188. sync();
  189. /* Perform action. */
  190. rc = 1;
  191. if (!(flags & 4)) { /* no -f */
  192. //TODO: I tend to think that signalling linuxrc is wrong
  193. // pity original author didn't comment on it...
  194. if (ENABLE_LINUXRC) {
  195. /* talk to linuxrc */
  196. /* bbox init/linuxrc assumed */
  197. pid_t *pidlist = find_pid_by_name("linuxrc");
  198. if (pidlist[0] > 0)
  199. rc = kill(pidlist[0], signals[which]);
  200. if (ENABLE_FEATURE_CLEAN_UP)
  201. free(pidlist);
  202. }
  203. if (rc) {
  204. /* talk to init */
  205. if (!ENABLE_FEATURE_CALL_TELINIT) {
  206. /* bbox init assumed */
  207. rc = kill(1, signals[which]);
  208. if (init_was_not_there())
  209. rc = kill(1, signals[which]);
  210. } else {
  211. /* SysV style init assumed */
  212. /* runlevels:
  213. * 0 == shutdown
  214. * 6 == reboot */
  215. execlp(CONFIG_TELINIT_PATH,
  216. CONFIG_TELINIT_PATH,
  217. which == 2 ? "6" : "0",
  218. (char *)NULL
  219. );
  220. bb_perror_msg_and_die("can't execute '%s'",
  221. CONFIG_TELINIT_PATH);
  222. }
  223. }
  224. } else {
  225. rc = reboot(magic[which]);
  226. }
  227. if (rc)
  228. bb_perror_nomsg_and_die();
  229. return rc;
  230. }