signal.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/reboot.h>
  15. #include <sys/types.h>
  16. #include <unistd.h>
  17. #include "procd.h"
  18. static void do_reboot(void)
  19. {
  20. LOG("reboot\n");
  21. fflush(stderr);
  22. sync();
  23. sleep(2);
  24. reboot(RB_AUTOBOOT);
  25. while (1)
  26. ;
  27. }
  28. static void signal_shutdown(int signal, siginfo_t *siginfo, void *data)
  29. {
  30. int event = 0;
  31. char *msg = NULL;
  32. #ifndef DISABLE_INIT
  33. switch(signal) {
  34. case SIGINT:
  35. case SIGTERM:
  36. event = RB_AUTOBOOT;
  37. msg = "reboot";
  38. break;
  39. case SIGUSR1:
  40. case SIGUSR2:
  41. case SIGPWR:
  42. event = RB_POWER_OFF;
  43. msg = "poweroff";
  44. break;
  45. }
  46. #endif
  47. DEBUG(1, "Triggering %s\n", msg);
  48. if (event)
  49. procd_shutdown(event);
  50. }
  51. struct sigaction sa_shutdown = {
  52. .sa_sigaction = signal_shutdown,
  53. .sa_flags = SA_SIGINFO
  54. };
  55. static void signal_crash(int signal, siginfo_t *siginfo, void *data)
  56. {
  57. ERROR("Rebooting as procd has crashed\n");
  58. do_reboot();
  59. }
  60. struct sigaction sa_crash = {
  61. .sa_sigaction = signal_crash,
  62. .sa_flags = SA_SIGINFO
  63. };
  64. static void signal_dummy(int signal, siginfo_t *siginfo, void *data)
  65. {
  66. ERROR("Got unexpected signal %d\n", signal);
  67. }
  68. struct sigaction sa_dummy = {
  69. .sa_sigaction = signal_dummy,
  70. .sa_flags = SA_SIGINFO
  71. };
  72. void procd_signal(void)
  73. {
  74. signal(SIGPIPE, SIG_IGN);
  75. if (getpid() != 1)
  76. return;
  77. sigaction(SIGTERM, &sa_shutdown, NULL);
  78. sigaction(SIGINT, &sa_shutdown, NULL);
  79. sigaction(SIGUSR1, &sa_shutdown, NULL);
  80. sigaction(SIGUSR2, &sa_shutdown, NULL);
  81. sigaction(SIGPWR, &sa_shutdown, NULL);
  82. sigaction(SIGSEGV, &sa_crash, NULL);
  83. sigaction(SIGBUS, &sa_crash, NULL);
  84. sigaction(SIGHUP, &sa_dummy, NULL);
  85. sigaction(SIGKILL, &sa_dummy, NULL);
  86. sigaction(SIGSTOP, &sa_dummy, NULL);
  87. #ifndef DISABLE_INIT
  88. reboot(RB_DISABLE_CAD);
  89. #endif
  90. }