3
0

watchdog.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 ATTRIBUTE_UNUSED sig) ATTRIBUTE_NORETURN;
  14. static void watchdog_shutdown(int ATTRIBUTE_UNUSED sig)
  15. {
  16. static const char V = 'V';
  17. write(3, &V, 1); /* Magic, see watchdog-api.txt in kernel */
  18. if (ENABLE_FEATURE_CLEAN_UP)
  19. close(3);
  20. exit(0);
  21. }
  22. int watchdog_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  23. int watchdog_main(int argc, char **argv)
  24. {
  25. unsigned opts;
  26. unsigned timer_duration = 30000; /* Userspace timer duration, in milliseconds */
  27. char *t_arg;
  28. opt_complementary = "=1"; /* must have 1 argument */
  29. opts = getopt32(argv, "Ft:", &t_arg);
  30. if (opts & OPT_TIMER) {
  31. static const struct suffix_mult suffixes[] = {
  32. { "ms", 1 },
  33. { "", 1000 },
  34. { }
  35. };
  36. timer_duration = xatou_sfx(t_arg, suffixes);
  37. }
  38. if (!(opts & OPT_FOREGROUND)) {
  39. bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
  40. }
  41. signal(SIGHUP, watchdog_shutdown);
  42. signal(SIGINT, watchdog_shutdown);
  43. /* Use known fd # - avoid needing global 'int fd' */
  44. xmove_fd(xopen(argv[argc - 1], O_WRONLY), 3);
  45. while (1) {
  46. /*
  47. * Make sure we clear the counter before sleeping, as the counter value
  48. * is undefined at this point -- PFM
  49. */
  50. write(3, "", 1); /* write zero byte */
  51. usleep(timer_duration * 1000L);
  52. }
  53. return EXIT_SUCCESS; /* - not reached, but gcc 4.2.1 is too dumb! */
  54. }