watchdog-test.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 <signal.h>
  10. #include <sys/ioctl.h>
  11. #include <linux/types.h>
  12. #include <linux/watchdog.h>
  13. int fd;
  14. /*
  15. * This function simply sends an IOCTL to the driver, which in turn ticks
  16. * the PC Watchdog card to reset its internal timer so it doesn't trigger
  17. * a computer reset.
  18. */
  19. static void keep_alive(void)
  20. {
  21. int dummy;
  22. ioctl(fd, WDIOC_KEEPALIVE, &dummy);
  23. }
  24. /*
  25. * The main program. Run the program with "-d" to disable the card,
  26. * or "-e" to enable the card.
  27. */
  28. static void term(int sig)
  29. {
  30. close(fd);
  31. fprintf(stderr, "Stopping watchdog ticks...\n");
  32. exit(0);
  33. }
  34. int main(int argc, char *argv[])
  35. {
  36. int flags;
  37. fd = open("/dev/watchdog", O_WRONLY);
  38. if (fd == -1) {
  39. fprintf(stderr, "Watchdog device not enabled.\n");
  40. fflush(stderr);
  41. exit(-1);
  42. }
  43. if (argc > 1) {
  44. if (!strncasecmp(argv[1], "-d", 2)) {
  45. flags = WDIOS_DISABLECARD;
  46. ioctl(fd, WDIOC_SETOPTIONS, &flags);
  47. fprintf(stderr, "Watchdog card disabled.\n");
  48. fflush(stderr);
  49. goto end;
  50. } else if (!strncasecmp(argv[1], "-e", 2)) {
  51. flags = WDIOS_ENABLECARD;
  52. ioctl(fd, WDIOC_SETOPTIONS, &flags);
  53. fprintf(stderr, "Watchdog card enabled.\n");
  54. fflush(stderr);
  55. goto end;
  56. } else {
  57. fprintf(stderr, "-d to disable, -e to enable.\n");
  58. fprintf(stderr, "run by itself to tick the card.\n");
  59. fflush(stderr);
  60. goto end;
  61. }
  62. } else {
  63. fprintf(stderr, "Watchdog Ticking Away!\n");
  64. fflush(stderr);
  65. }
  66. signal(SIGINT, term);
  67. while(1) {
  68. keep_alive();
  69. sleep(1);
  70. }
  71. end:
  72. close(fd);
  73. return 0;
  74. }