watchdog.c 1.4 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 "busybox.h"
  11. #define OPT_FOREGROUND 0x01
  12. #define OPT_TIMER 0x02
  13. /* Watchdog file descriptor */
  14. static int fd;
  15. static void watchdog_shutdown(int ATTRIBUTE_UNUSED unused)
  16. {
  17. write(fd, "V", 1); /* Magic, see watchdog-api.txt in kernel */
  18. close(fd);
  19. exit(0);
  20. }
  21. int watchdog_main(int argc, char **argv)
  22. {
  23. unsigned opts;
  24. unsigned timer_duration = 30; /* Userspace timer duration, in seconds */
  25. char *t_arg;
  26. opts = getopt32(argc, argv, "Ft:", &t_arg);
  27. if (opts & OPT_TIMER)
  28. timer_duration = xatou(t_arg);
  29. /* We're only interested in the watchdog device .. */
  30. if (optind < argc - 1 || argc == 1)
  31. bb_show_usage();
  32. #ifdef BB_NOMMU
  33. if (!(opts & OPT_FOREGROUND))
  34. vfork_daemon_rexec(0, 1, argc, argv, "-F");
  35. #else
  36. xdaemon(0, 1);
  37. #endif
  38. signal(SIGHUP, watchdog_shutdown);
  39. signal(SIGINT, watchdog_shutdown);
  40. fd = xopen(argv[argc - 1], O_WRONLY);
  41. while (1) {
  42. /*
  43. * Make sure we clear the counter before sleeping, as the counter value
  44. * is undefined at this point -- PFM
  45. */
  46. write(fd, "\0", 1);
  47. sleep(timer_duration);
  48. }
  49. watchdog_shutdown(0);
  50. return EXIT_SUCCESS;
  51. }