init_shared.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Stuff shared between init, reboot, halt, and poweroff
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <signal.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <getopt.h>
  26. #include <sys/reboot.h>
  27. #include <sys/syslog.h>
  28. #include "busybox.h"
  29. #include "init_shared.h"
  30. extern int kill_init(int sig)
  31. {
  32. #ifdef CONFIG_FEATURE_INITRD
  33. /* don't assume init's pid == 1 */
  34. long *pid = find_pid_by_name("init");
  35. if (!pid || *pid<=0) {
  36. pid = find_pid_by_name("linuxrc");
  37. if (!pid || *pid<=0)
  38. bb_error_msg_and_die("no process killed");
  39. }
  40. return(kill(*pid, sig));
  41. #else
  42. return(kill(1, sig));
  43. #endif
  44. }
  45. #ifndef CONFIG_INIT
  46. const char * const bb_shutdown_format = "\r%s\n";
  47. extern int bb_shutdown_system(unsigned long magic)
  48. {
  49. int pri = LOG_KERN|LOG_NOTICE|LOG_FACMASK;
  50. const char *message;
  51. /* Don't kill ourself */
  52. signal(SIGTERM,SIG_IGN);
  53. signal(SIGHUP,SIG_IGN);
  54. setpgrp();
  55. /* Allow Ctrl-Alt-Del to reboot system. */
  56. #ifndef RB_ENABLE_CAD
  57. #define RB_ENABLE_CAD 0x89abcdef
  58. #endif
  59. reboot(RB_ENABLE_CAD);
  60. openlog(bb_applet_name, 0, pri);
  61. message = "\nThe system is going down NOW !!";
  62. syslog(pri, "%s", message);
  63. printf(bb_shutdown_format, message);
  64. sync();
  65. /* Send signals to every process _except_ pid 1 */
  66. message = "Sending SIGTERM to all processes.";
  67. syslog(pri, "%s", message);
  68. printf(bb_shutdown_format, message);
  69. kill(-1, SIGTERM);
  70. sleep(1);
  71. sync();
  72. message = "Sending SIGKILL to all processes.";
  73. syslog(pri, "%s", message);
  74. printf(bb_shutdown_format, message);
  75. kill(-1, SIGKILL);
  76. sleep(1);
  77. sync();
  78. reboot(magic);
  79. return 0; /* Shrug */
  80. }
  81. #endif