watchdog-test.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Watchdog Driver Test Program
  3. */
  4. #include <errno.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #include <signal.h>
  11. #include <sys/ioctl.h>
  12. #include <linux/types.h>
  13. #include <linux/watchdog.h>
  14. int fd;
  15. const char v = 'V';
  16. /*
  17. * This function simply sends an IOCTL to the driver, which in turn ticks
  18. * the PC Watchdog card to reset its internal timer so it doesn't trigger
  19. * a computer reset.
  20. */
  21. static void keep_alive(void)
  22. {
  23. int dummy;
  24. printf(".");
  25. ioctl(fd, WDIOC_KEEPALIVE, &dummy);
  26. }
  27. /*
  28. * The main program. Run the program with "-d" to disable the card,
  29. * or "-e" to enable the card.
  30. */
  31. static void term(int sig)
  32. {
  33. int ret = write(fd, &v, 1);
  34. close(fd);
  35. if (ret < 0)
  36. printf("\nStopping watchdog ticks failed (%d)...\n", errno);
  37. else
  38. printf("\nStopping watchdog ticks...\n");
  39. exit(0);
  40. }
  41. int main(int argc, char *argv[])
  42. {
  43. int flags;
  44. unsigned int ping_rate = 1;
  45. int ret;
  46. setbuf(stdout, NULL);
  47. fd = open("/dev/watchdog", O_WRONLY);
  48. if (fd == -1) {
  49. printf("Watchdog device not enabled.\n");
  50. exit(-1);
  51. }
  52. if (argc > 1) {
  53. if (!strncasecmp(argv[1], "-d", 2)) {
  54. flags = WDIOS_DISABLECARD;
  55. ioctl(fd, WDIOC_SETOPTIONS, &flags);
  56. printf("Watchdog card disabled.\n");
  57. goto end;
  58. } else if (!strncasecmp(argv[1], "-e", 2)) {
  59. flags = WDIOS_ENABLECARD;
  60. ioctl(fd, WDIOC_SETOPTIONS, &flags);
  61. printf("Watchdog card enabled.\n");
  62. goto end;
  63. } else if (!strncasecmp(argv[1], "-t", 2) && argv[2]) {
  64. flags = atoi(argv[2]);
  65. ioctl(fd, WDIOC_SETTIMEOUT, &flags);
  66. printf("Watchdog timeout set to %u seconds.\n", flags);
  67. goto end;
  68. } else if (!strncasecmp(argv[1], "-p", 2) && argv[2]) {
  69. ping_rate = strtoul(argv[2], NULL, 0);
  70. printf("Watchdog ping rate set to %u seconds.\n", ping_rate);
  71. } else {
  72. printf("-d to disable, -e to enable, -t <n> to set " \
  73. "the timeout,\n-p <n> to set the ping rate, and \n");
  74. printf("run by itself to tick the card.\n");
  75. goto end;
  76. }
  77. }
  78. printf("Watchdog Ticking Away!\n");
  79. signal(SIGINT, term);
  80. while(1) {
  81. keep_alive();
  82. sleep(ping_rate);
  83. }
  84. end:
  85. ret = write(fd, &v, 1);
  86. if (ret < 0)
  87. printf("Stopping watchdog ticks failed (%d)...\n", errno);
  88. close(fd);
  89. return 0;
  90. }