3
0

watchdog.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 GPLv2 or later, see file LICENSE in this source tree.
  10. */
  11. //usage:#define watchdog_trivial_usage
  12. //usage: "[-t N[ms]] [-T N[ms]] [-F] DEV"
  13. //usage:#define watchdog_full_usage "\n\n"
  14. //usage: "Periodically write to watchdog device DEV\n"
  15. //usage: "\n -T N Reboot after N seconds if not reset (default 60)"
  16. //usage: "\n -t N Reset every N seconds (default 30)"
  17. //usage: "\n -F Run in foreground"
  18. //usage: "\n"
  19. //usage: "\nUse 500ms to specify period in milliseconds"
  20. #include "libbb.h"
  21. #include "linux/types.h" /* for __u32 */
  22. #include "linux/watchdog.h"
  23. #define OPT_FOREGROUND (1 << 0)
  24. #define OPT_STIMER (1 << 1)
  25. #define OPT_HTIMER (1 << 2)
  26. static void watchdog_shutdown(int sig UNUSED_PARAM)
  27. {
  28. static const char V = 'V';
  29. remove_pidfile(CONFIG_PID_FILE_PATH "/watchdog.pid");
  30. write(3, &V, 1); /* Magic, see watchdog-api.txt in kernel */
  31. if (ENABLE_FEATURE_CLEAN_UP)
  32. close(3);
  33. _exit(EXIT_SUCCESS);
  34. }
  35. int watchdog_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  36. int watchdog_main(int argc, char **argv)
  37. {
  38. static const struct suffix_mult suffixes[] = {
  39. { "ms", 1 },
  40. { "", 1000 },
  41. { "", 0 }
  42. };
  43. unsigned opts;
  44. unsigned stimer_duration; /* how often to restart */
  45. unsigned htimer_duration = 60000; /* reboots after N ms if not restarted */
  46. char *st_arg;
  47. char *ht_arg;
  48. opt_complementary = "=1"; /* must have exactly 1 argument */
  49. opts = getopt32(argv, "Ft:T:", &st_arg, &ht_arg);
  50. /* We need to daemonize *before* opening the watchdog as many drivers
  51. * will only allow one process at a time to do so. Since daemonizing
  52. * is not perfect (child may run before parent finishes exiting), we
  53. * can't rely on parent exiting before us (let alone *cleanly* releasing
  54. * the watchdog fd -- something else that may not even be allowed).
  55. */
  56. if (!(opts & OPT_FOREGROUND))
  57. bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
  58. if (opts & OPT_HTIMER)
  59. htimer_duration = xatou_sfx(ht_arg, suffixes);
  60. stimer_duration = htimer_duration / 2;
  61. if (opts & OPT_STIMER)
  62. stimer_duration = xatou_sfx(st_arg, suffixes);
  63. bb_signals(BB_FATAL_SIGS, watchdog_shutdown);
  64. /* Use known fd # - avoid needing global 'int fd' */
  65. xmove_fd(xopen(argv[argc - 1], O_WRONLY), 3);
  66. /* WDIOC_SETTIMEOUT takes seconds, not milliseconds */
  67. htimer_duration = htimer_duration / 1000;
  68. #ifndef WDIOC_SETTIMEOUT
  69. # error WDIOC_SETTIMEOUT is not defined, cannot compile watchdog applet
  70. #else
  71. # if defined WDIOC_SETOPTIONS && defined WDIOS_ENABLECARD
  72. {
  73. static const int enable = WDIOS_ENABLECARD;
  74. ioctl_or_warn(3, WDIOC_SETOPTIONS, (void*) &enable);
  75. }
  76. # endif
  77. ioctl_or_warn(3, WDIOC_SETTIMEOUT, &htimer_duration);
  78. #endif
  79. #if 0
  80. ioctl_or_warn(3, WDIOC_GETTIMEOUT, &htimer_duration);
  81. printf("watchdog: SW timer is %dms, HW timer is %ds\n",
  82. stimer_duration, htimer_duration * 1000);
  83. #endif
  84. write_pidfile(CONFIG_PID_FILE_PATH "/watchdog.pid");
  85. while (1) {
  86. /*
  87. * Make sure we clear the counter before sleeping,
  88. * as the counter value is undefined at this point -- PFM
  89. */
  90. write(3, "", 1); /* write zero byte */
  91. usleep(stimer_duration * 1000L);
  92. }
  93. return EXIT_SUCCESS; /* - not reached, but gcc 4.2.1 is too dumb! */
  94. }