3
0

watchdog.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/watchdog.h"
  13. #define OPT_FOREGROUND (1 << 0)
  14. #define OPT_STIMER (1 << 1)
  15. #define OPT_HTIMER (1 << 2)
  16. static void watchdog_shutdown(int sig UNUSED_PARAM)
  17. {
  18. static const char V = 'V';
  19. write(3, &V, 1); /* Magic, see watchdog-api.txt in kernel */
  20. if (ENABLE_FEATURE_CLEAN_UP)
  21. close(3);
  22. exit(EXIT_SUCCESS);
  23. }
  24. int watchdog_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  25. int watchdog_main(int argc, char **argv)
  26. {
  27. static const struct suffix_mult suffixes[] = {
  28. { "ms", 1 },
  29. { "", 1000 },
  30. { }
  31. };
  32. unsigned opts;
  33. unsigned stimer_duration; /* how often to restart */
  34. unsigned htimer_duration = 60000; /* reboots after N ms if not restarted */
  35. char *st_arg;
  36. char *ht_arg;
  37. opt_complementary = "=1"; /* must have exactly 1 argument */
  38. opts = getopt32(argv, "Ft:T:", &st_arg, &ht_arg);
  39. if (opts & OPT_HTIMER)
  40. htimer_duration = xatou_sfx(ht_arg, suffixes);
  41. stimer_duration = htimer_duration / 2;
  42. if (opts & OPT_STIMER)
  43. stimer_duration = xatou_sfx(st_arg, suffixes);
  44. bb_signals(BB_FATAL_SIGS, watchdog_shutdown);
  45. /* Use known fd # - avoid needing global 'int fd' */
  46. xmove_fd(xopen(argv[argc - 1], O_WRONLY), 3);
  47. /* WDIOC_SETTIMEOUT takes seconds, not milliseconds */
  48. htimer_duration = htimer_duration / 1000;
  49. ioctl_or_warn(3, WDIOC_SETTIMEOUT, &htimer_duration);
  50. #if 0
  51. ioctl_or_warn(3, WDIOC_GETTIMEOUT, &htimer_duration);
  52. printf("watchdog: SW timer is %dms, HW timer is %dms\n",
  53. stimer_duration, htimer_duration * 1000);
  54. #endif
  55. if (!(opts & OPT_FOREGROUND)) {
  56. bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
  57. }
  58. while (1) {
  59. /*
  60. * Make sure we clear the counter before sleeping, as the counter value
  61. * is undefined at this point -- PFM
  62. */
  63. write(3, "", 1); /* write zero byte */
  64. usleep(stimer_duration * 1000L);
  65. }
  66. return EXIT_SUCCESS; /* - not reached, but gcc 4.2.1 is too dumb! */
  67. }