watchdog-test.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Watchdog Driver Test Program
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <fcntl.h>
  9. #include <sys/ioctl.h>
  10. #include <linux/types.h>
  11. #include <linux/watchdog.h>
  12. int fd;
  13. /*
  14. * This function simply sends an IOCTL to the driver, which in turn ticks
  15. * the PC Watchdog card to reset its internal timer so it doesn't trigger
  16. * a computer reset.
  17. */
  18. static void keep_alive(void)
  19. {
  20. int dummy;
  21. ioctl(fd, WDIOC_KEEPALIVE, &dummy);
  22. }
  23. /*
  24. * The main program. Run the program with "-d" to disable the card,
  25. * or "-e" to enable the card.
  26. */
  27. int main(int argc, char *argv[])
  28. {
  29. int flags;
  30. fd = open("/dev/watchdog", O_WRONLY);
  31. if (fd == -1) {
  32. fprintf(stderr, "Watchdog device not enabled.\n");
  33. fflush(stderr);
  34. exit(-1);
  35. }
  36. if (argc > 1) {
  37. if (!strncasecmp(argv[1], "-d", 2)) {
  38. flags = WDIOS_DISABLECARD;
  39. ioctl(fd, WDIOC_SETOPTIONS, &flags);
  40. fprintf(stderr, "Watchdog card disabled.\n");
  41. fflush(stderr);
  42. exit(0);
  43. } else if (!strncasecmp(argv[1], "-e", 2)) {
  44. flags = WDIOS_ENABLECARD;
  45. ioctl(fd, WDIOC_SETOPTIONS, &flags);
  46. fprintf(stderr, "Watchdog card enabled.\n");
  47. fflush(stderr);
  48. exit(0);
  49. } else {
  50. fprintf(stderr, "-d to disable, -e to enable.\n");
  51. fprintf(stderr, "run by itself to tick the card.\n");
  52. fflush(stderr);
  53. exit(0);
  54. }
  55. } else {
  56. fprintf(stderr, "Watchdog Ticking Away!\n");
  57. fflush(stderr);
  58. }
  59. while(1) {
  60. keep_alive();
  61. sleep(1);
  62. }
  63. }