3
0

halt.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 GPL version 2, see file LICENSE in this tarball for details.
  8. */
  9. #include "busybox.h"
  10. #include <sys/reboot.h>
  11. int halt_main(int argc, char *argv[])
  12. {
  13. static const int magic[] = {
  14. #ifdef RB_HALT_SYSTEM
  15. RB_HALT_SYSTEM,
  16. #elif defined RB_HALT
  17. RB_HALT,
  18. #endif
  19. #ifdef RB_POWER_OFF
  20. RB_POWER_OFF,
  21. #elif defined RB_POWERDOWN
  22. RB_POWERDOWN,
  23. #endif
  24. RB_AUTOBOOT
  25. };
  26. static const int signals[] = { SIGUSR1, SIGUSR2, SIGTERM };
  27. char *delay;
  28. int which, flags, rc = 1;
  29. /* Figure out which applet we're running */
  30. for (which = 0; "hpr"[which] != *applet_name; which++);
  31. /* Parse and handle arguments */
  32. flags = getopt32(argc, argv, "d:nf", &delay);
  33. if (flags & 1) sleep(xatou(delay));
  34. if (!(flags & 2)) sync();
  35. /* Perform action. */
  36. if (ENABLE_INIT && !(flags & 4)) {
  37. if (ENABLE_FEATURE_INITRD) {
  38. pid_t *pidlist = find_pid_by_name("linuxrc");
  39. if (pidlist[0] > 0)
  40. rc = kill(pidlist[0], signals[which]);
  41. if (ENABLE_FEATURE_CLEAN_UP)
  42. free(pidlist);
  43. }
  44. if (rc)
  45. rc = kill(1, signals[which]);
  46. } else
  47. rc = reboot(magic[which]);
  48. if (rc)
  49. bb_error_msg("no");
  50. return rc;
  51. }