watchdog.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. write(3, "V", 1); /* Magic, see watchdog-api.txt in kernel */
  17. if (ENABLE_FEATURE_CLEAN_UP)
  18. close(3);
  19. exit(0);
  20. }
  21. int watchdog_main(int argc, char **argv);
  22. int watchdog_main(int argc, char **argv)
  23. {
  24. unsigned opts;
  25. unsigned timer_duration = 30; /* Userspace timer duration, in seconds */
  26. char *t_arg;
  27. opt_complementary = "=1"; /* must have 1 argument */
  28. opts = getopt32(argc, argv, "Ft:", &t_arg);
  29. if (opts & OPT_TIMER)
  30. timer_duration = xatou(t_arg);
  31. if (!(opts & OPT_FOREGROUND)) {
  32. bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
  33. }
  34. signal(SIGHUP, watchdog_shutdown);
  35. signal(SIGINT, watchdog_shutdown);
  36. /* Use known fd # - avoid needing global 'int fd' */
  37. xmove_fd(xopen(argv[argc - 1], O_WRONLY), 3);
  38. while (1) {
  39. /*
  40. * Make sure we clear the counter before sleeping, as the counter value
  41. * is undefined at this point -- PFM
  42. */
  43. write(3, "", 1);
  44. sleep(timer_duration);
  45. }
  46. watchdog_shutdown(0);
  47. /* return EXIT_SUCCESS; */
  48. }