watchdog.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini watchdog implementation for busybox
  4. *
  5. * Copyright (C) 2003 Paul Mundt <lethal@linux-sh.org>
  6. * Copyright (C) 2006 Bernhard Reutner-Fischer <busybox@busybox.net>
  7. * Copyright (C) 2008 Darius Augulis <augulis.darius@gmail.com>
  8. *
  9. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  10. */
  11. #include "libbb.h"
  12. #include "linux/types.h" /* for __u32 */
  13. #include "linux/watchdog.h"
  14. #define OPT_FOREGROUND (1 << 0)
  15. #define OPT_STIMER (1 << 1)
  16. #define OPT_HTIMER (1 << 2)
  17. static void watchdog_shutdown(int sig UNUSED_PARAM)
  18. {
  19. static const char V = 'V';
  20. write(3, &V, 1); /* Magic, see watchdog-api.txt in kernel */
  21. if (ENABLE_FEATURE_CLEAN_UP)
  22. close(3);
  23. exit(EXIT_SUCCESS);
  24. }
  25. int watchdog_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  26. int watchdog_main(int argc, char **argv)
  27. {
  28. static const struct suffix_mult suffixes[] = {
  29. { "ms", 1 },
  30. { "", 1000 },
  31. { "", 0 }
  32. };
  33. unsigned opts;
  34. unsigned stimer_duration; /* how often to restart */
  35. unsigned htimer_duration = 60000; /* reboots after N ms if not restarted */
  36. char *st_arg;
  37. char *ht_arg;
  38. opt_complementary = "=1"; /* must have exactly 1 argument */
  39. opts = getopt32(argv, "Ft:T:", &st_arg, &ht_arg);
  40. /* We need to daemonize *before* opening the watchdog as many drivers
  41. * will only allow one process at a time to do so. Since daemonizing
  42. * is not perfect (child may run before parent finishes exiting), we
  43. * can't rely on parent exiting before us (let alone *cleanly* releasing
  44. * the watchdog fd -- something else that may not even be allowed).
  45. */
  46. if (!(opts & OPT_FOREGROUND))
  47. bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
  48. if (opts & OPT_HTIMER)
  49. htimer_duration = xatou_sfx(ht_arg, suffixes);
  50. stimer_duration = htimer_duration / 2;
  51. if (opts & OPT_STIMER)
  52. stimer_duration = xatou_sfx(st_arg, suffixes);
  53. bb_signals(BB_FATAL_SIGS, watchdog_shutdown);
  54. /* Use known fd # - avoid needing global 'int fd' */
  55. xmove_fd(xopen(argv[argc - 1], O_WRONLY), 3);
  56. /* WDIOC_SETTIMEOUT takes seconds, not milliseconds */
  57. htimer_duration = htimer_duration / 1000;
  58. #ifndef WDIOC_SETTIMEOUT
  59. # error WDIOC_SETTIMEOUT is not defined, cannot compile watchdog applet
  60. #else
  61. # if defined WDIOC_SETOPTIONS && defined WDIOS_ENABLECARD
  62. {
  63. static const int enable = WDIOS_ENABLECARD;
  64. ioctl_or_warn(3, WDIOC_SETOPTIONS, (void*) &enable);
  65. }
  66. # endif
  67. ioctl_or_warn(3, WDIOC_SETTIMEOUT, &htimer_duration);
  68. #endif
  69. #if 0
  70. ioctl_or_warn(3, WDIOC_GETTIMEOUT, &htimer_duration);
  71. printf("watchdog: SW timer is %dms, HW timer is %ds\n",
  72. stimer_duration, htimer_duration * 1000);
  73. #endif
  74. while (1) {
  75. /*
  76. * Make sure we clear the counter before sleeping,
  77. * as the counter value is undefined at this point -- PFM
  78. */
  79. write(3, "", 1); /* write zero byte */
  80. usleep(stimer_duration * 1000L);
  81. }
  82. return EXIT_SUCCESS; /* - not reached, but gcc 4.2.1 is too dumb! */
  83. }