watchdog-test.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Watchdog Driver Test Program
  4. */
  5. #include <errno.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <signal.h>
  12. #include <getopt.h>
  13. #include <sys/ioctl.h>
  14. #include <linux/types.h>
  15. #include <linux/watchdog.h>
  16. #define DEFAULT_PING_RATE 1
  17. int fd;
  18. const char v = 'V';
  19. static const char sopts[] = "bdehp:t:";
  20. static const struct option lopts[] = {
  21. {"bootstatus", no_argument, NULL, 'b'},
  22. {"disable", no_argument, NULL, 'd'},
  23. {"enable", no_argument, NULL, 'e'},
  24. {"help", no_argument, NULL, 'h'},
  25. {"pingrate", required_argument, NULL, 'p'},
  26. {"timeout", required_argument, NULL, 't'},
  27. {NULL, no_argument, NULL, 0x0}
  28. };
  29. /*
  30. * This function simply sends an IOCTL to the driver, which in turn ticks
  31. * the PC Watchdog card to reset its internal timer so it doesn't trigger
  32. * a computer reset.
  33. */
  34. static void keep_alive(void)
  35. {
  36. int dummy;
  37. int ret;
  38. ret = ioctl(fd, WDIOC_KEEPALIVE, &dummy);
  39. if (!ret)
  40. printf(".");
  41. }
  42. /*
  43. * The main program. Run the program with "-d" to disable the card,
  44. * or "-e" to enable the card.
  45. */
  46. static void term(int sig)
  47. {
  48. int ret = write(fd, &v, 1);
  49. close(fd);
  50. if (ret < 0)
  51. printf("\nStopping watchdog ticks failed (%d)...\n", errno);
  52. else
  53. printf("\nStopping watchdog ticks...\n");
  54. exit(0);
  55. }
  56. static void usage(char *progname)
  57. {
  58. printf("Usage: %s [options]\n", progname);
  59. printf(" -b, --bootstatus Get last boot status (Watchdog/POR)\n");
  60. printf(" -d, --disable Turn off the watchdog timer\n");
  61. printf(" -e, --enable Turn on the watchdog timer\n");
  62. printf(" -h, --help Print the help message\n");
  63. printf(" -p, --pingrate=P Set ping rate to P seconds (default %d)\n", DEFAULT_PING_RATE);
  64. printf(" -t, --timeout=T Set timeout to T seconds\n");
  65. printf("\n");
  66. printf("Parameters are parsed left-to-right in real-time.\n");
  67. printf("Example: %s -d -t 10 -p 5 -e\n", progname);
  68. }
  69. int main(int argc, char *argv[])
  70. {
  71. int flags;
  72. unsigned int ping_rate = DEFAULT_PING_RATE;
  73. int ret;
  74. int c;
  75. int oneshot = 0;
  76. setbuf(stdout, NULL);
  77. fd = open("/dev/watchdog", O_WRONLY);
  78. if (fd == -1) {
  79. if (errno == ENOENT)
  80. printf("Watchdog device not enabled.\n");
  81. else if (errno == EACCES)
  82. printf("Run watchdog as root.\n");
  83. else
  84. printf("Watchdog device open failed %s\n",
  85. strerror(errno));
  86. exit(-1);
  87. }
  88. while ((c = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
  89. switch (c) {
  90. case 'b':
  91. flags = 0;
  92. oneshot = 1;
  93. ret = ioctl(fd, WDIOC_GETBOOTSTATUS, &flags);
  94. if (!ret)
  95. printf("Last boot is caused by: %s.\n", (flags != 0) ?
  96. "Watchdog" : "Power-On-Reset");
  97. else
  98. printf("WDIOC_GETBOOTSTATUS error '%s'\n", strerror(errno));
  99. break;
  100. case 'd':
  101. flags = WDIOS_DISABLECARD;
  102. ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
  103. if (!ret)
  104. printf("Watchdog card disabled.\n");
  105. else
  106. printf("WDIOS_DISABLECARD error '%s'\n", strerror(errno));
  107. break;
  108. case 'e':
  109. flags = WDIOS_ENABLECARD;
  110. ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
  111. if (!ret)
  112. printf("Watchdog card enabled.\n");
  113. else
  114. printf("WDIOS_ENABLECARD error '%s'\n", strerror(errno));
  115. break;
  116. case 'p':
  117. ping_rate = strtoul(optarg, NULL, 0);
  118. if (!ping_rate)
  119. ping_rate = DEFAULT_PING_RATE;
  120. printf("Watchdog ping rate set to %u seconds.\n", ping_rate);
  121. break;
  122. case 't':
  123. flags = strtoul(optarg, NULL, 0);
  124. ret = ioctl(fd, WDIOC_SETTIMEOUT, &flags);
  125. if (!ret)
  126. printf("Watchdog timeout set to %u seconds.\n", flags);
  127. else
  128. printf("WDIOC_SETTIMEOUT error '%s'\n", strerror(errno));
  129. break;
  130. default:
  131. usage(argv[0]);
  132. goto end;
  133. }
  134. }
  135. if (oneshot)
  136. goto end;
  137. printf("Watchdog Ticking Away!\n");
  138. signal(SIGINT, term);
  139. while (1) {
  140. keep_alive();
  141. sleep(ping_rate);
  142. }
  143. end:
  144. ret = write(fd, &v, 1);
  145. if (ret < 0)
  146. printf("Stopping watchdog ticks failed (%d)...\n", errno);
  147. close(fd);
  148. return 0;
  149. }