watchdog-test.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. 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. fd = open("/dev/watchdog", O_WRONLY);
  30. if (fd == -1) {
  31. fprintf(stderr, "Watchdog device not enabled.\n");
  32. fflush(stderr);
  33. exit(-1);
  34. }
  35. if (argc > 1) {
  36. if (!strncasecmp(argv[1], "-d", 2)) {
  37. ioctl(fd, WDIOC_SETOPTIONS, WDIOS_DISABLECARD);
  38. fprintf(stderr, "Watchdog card disabled.\n");
  39. fflush(stderr);
  40. exit(0);
  41. } else if (!strncasecmp(argv[1], "-e", 2)) {
  42. ioctl(fd, WDIOC_SETOPTIONS, WDIOS_ENABLECARD);
  43. fprintf(stderr, "Watchdog card enabled.\n");
  44. fflush(stderr);
  45. exit(0);
  46. } else {
  47. fprintf(stderr, "-d to disable, -e to enable.\n");
  48. fprintf(stderr, "run by itself to tick the card.\n");
  49. fflush(stderr);
  50. exit(0);
  51. }
  52. } else {
  53. fprintf(stderr, "Watchdog Ticking Away!\n");
  54. fflush(stderr);
  55. }
  56. while(1) {
  57. keep_alive();
  58. sleep(1);
  59. }
  60. }