watchdog.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 Fischer <busybox@busybox.net>
  7. *
  8. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  9. */
  10. #include "libbb.h"
  11. #define OPT_FOREGROUND 0x01
  12. #define OPT_TIMER 0x02
  13. static void watchdog_shutdown(int sig UNUSED_PARAM)
  14. {
  15. static const char V = 'V';
  16. write(3, &V, 1); /* Magic, see watchdog-api.txt in kernel */
  17. if (ENABLE_FEATURE_CLEAN_UP)
  18. close(3);
  19. exit(EXIT_SUCCESS);
  20. }
  21. int watchdog_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  22. int watchdog_main(int argc, char **argv)
  23. {
  24. unsigned opts;
  25. unsigned timer_duration = 30000; /* Userspace timer duration, in milliseconds */
  26. char *t_arg;
  27. opt_complementary = "=1"; /* must have 1 argument */
  28. opts = getopt32(argv, "Ft:", &t_arg);
  29. if (opts & OPT_TIMER) {
  30. static const struct suffix_mult suffixes[] = {
  31. { "ms", 1 },
  32. { "", 1000 },
  33. { }
  34. };
  35. timer_duration = xatou_sfx(t_arg, suffixes);
  36. }
  37. if (!(opts & OPT_FOREGROUND)) {
  38. bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
  39. }
  40. bb_signals(BB_FATAL_SIGS, watchdog_shutdown);
  41. /* Use known fd # - avoid needing global 'int fd' */
  42. xmove_fd(xopen(argv[argc - 1], O_WRONLY), 3);
  43. // TODO?
  44. // if (!(opts & OPT_TIMER)) {
  45. // if (ioctl(fd, WDIOC_GETTIMEOUT, &timer_duration) == 0)
  46. // timer_duration *= 500;
  47. // else
  48. // timer_duration = 30000;
  49. // }
  50. while (1) {
  51. /*
  52. * Make sure we clear the counter before sleeping, as the counter value
  53. * is undefined at this point -- PFM
  54. */
  55. write(3, "", 1); /* write zero byte */
  56. usleep(timer_duration * 1000L);
  57. }
  58. return EXIT_SUCCESS; /* - not reached, but gcc 4.2.1 is too dumb! */
  59. }