reboot.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /* reboot.c - Restart, halt or powerdown the system.
  2. *
  3. * Copyright 2013 Elie De Brauwer <eliedebrauwer@gmail.com>
  4. USE_REBOOT(NEWTOY(reboot, "d:fn", TOYFLAG_SBIN|TOYFLAG_NEEDROOT))
  5. USE_REBOOT(OLDTOY(halt, reboot, TOYFLAG_SBIN|TOYFLAG_NEEDROOT))
  6. USE_REBOOT(OLDTOY(poweroff, reboot, TOYFLAG_SBIN|TOYFLAG_NEEDROOT))
  7. config REBOOT
  8. bool "reboot"
  9. default y
  10. help
  11. usage: reboot/halt/poweroff [-fn] [-d DELAY]
  12. Restart, halt, or power off the system.
  13. -d Wait DELAY before proceeding (in seconds or m/h/d suffix: -d 1.5m = 90s)
  14. -f Force reboot (don't signal init, reboot directly)
  15. -n Don't sync filesystems before reboot
  16. */
  17. #define FOR_reboot
  18. #include "toys.h"
  19. #include <sys/reboot.h>
  20. GLOBALS(
  21. char *d;
  22. )
  23. void reboot_main(void)
  24. {
  25. struct timespec ts;
  26. int types[] = {RB_AUTOBOOT, RB_HALT_SYSTEM, RB_POWER_OFF},
  27. sigs[] = {SIGTERM, SIGUSR1, SIGUSR2}, idx;
  28. if (TT.d) {
  29. xparsetimespec(TT.d, &ts);
  30. nanosleep(&ts, NULL);
  31. }
  32. if (!FLAG(n)) sync();
  33. idx = stridx("hp", *toys.which->name)+1;
  34. if (FLAG(f)) toys.exitval = reboot(types[idx]);
  35. else toys.exitval = kill(1, sigs[idx]);
  36. }