watchdog.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. *
  7. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  8. */
  9. #include <stdio.h>
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <stdlib.h>
  13. #include <signal.h>
  14. #include "busybox.h"
  15. /* Userspace timer duration, in seconds */
  16. static unsigned int timer_duration = 30;
  17. /* Watchdog file descriptor */
  18. static int fd;
  19. static void watchdog_shutdown(int ATTRIBUTE_UNUSED unused)
  20. {
  21. write(fd, "V", 1); /* Magic */
  22. close(fd);
  23. exit(0);
  24. }
  25. int watchdog_main(int argc, char **argv)
  26. {
  27. char *t_arg;
  28. unsigned long flags;
  29. flags = bb_getopt_ulflags(argc, argv, "t:", &t_arg);
  30. if (flags & 1)
  31. timer_duration = bb_xgetlarg(t_arg, 10, 0, INT_MAX);
  32. /* We're only interested in the watchdog device .. */
  33. if (optind < argc - 1 || argc == 1)
  34. bb_show_usage();
  35. if (daemon(0, 1) < 0)
  36. bb_perror_msg_and_die("Failed forking watchdog daemon");
  37. signal(SIGHUP, watchdog_shutdown);
  38. signal(SIGINT, watchdog_shutdown);
  39. fd = bb_xopen(argv[argc - 1], O_WRONLY);
  40. while (1) {
  41. /*
  42. * Make sure we clear the counter before sleeping, as the counter value
  43. * is undefined at this point -- PFM
  44. */
  45. write(fd, "\0", 1);
  46. sleep(timer_duration);
  47. }
  48. watchdog_shutdown(0);
  49. return EXIT_SUCCESS;
  50. }