watchdog-test.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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:Tn:N";
  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. {"gettimeout", no_argument, NULL, 'T'},
  28. {"pretimeout", required_argument, NULL, 'n'},
  29. {"getpretimeout", no_argument, NULL, 'N'},
  30. {NULL, no_argument, NULL, 0x0}
  31. };
  32. /*
  33. * This function simply sends an IOCTL to the driver, which in turn ticks
  34. * the PC Watchdog card to reset its internal timer so it doesn't trigger
  35. * a computer reset.
  36. */
  37. static void keep_alive(void)
  38. {
  39. int dummy;
  40. int ret;
  41. ret = ioctl(fd, WDIOC_KEEPALIVE, &dummy);
  42. if (!ret)
  43. printf(".");
  44. }
  45. /*
  46. * The main program. Run the program with "-d" to disable the card,
  47. * or "-e" to enable the card.
  48. */
  49. static void term(int sig)
  50. {
  51. int ret = write(fd, &v, 1);
  52. close(fd);
  53. if (ret < 0)
  54. printf("\nStopping watchdog ticks failed (%d)...\n", errno);
  55. else
  56. printf("\nStopping watchdog ticks...\n");
  57. exit(0);
  58. }
  59. static void usage(char *progname)
  60. {
  61. printf("Usage: %s [options]\n", progname);
  62. printf(" -b, --bootstatus Get last boot status (Watchdog/POR)\n");
  63. printf(" -d, --disable Turn off the watchdog timer\n");
  64. printf(" -e, --enable Turn on the watchdog timer\n");
  65. printf(" -h, --help Print the help message\n");
  66. printf(" -p, --pingrate=P Set ping rate to P seconds (default %d)\n", DEFAULT_PING_RATE);
  67. printf(" -t, --timeout=T Set timeout to T seconds\n");
  68. printf(" -T, --gettimeout Get the timeout\n");
  69. printf(" -n, --pretimeout=T Set the pretimeout to T seconds\n");
  70. printf(" -N, --getpretimeout Get the pretimeout\n");
  71. printf("\n");
  72. printf("Parameters are parsed left-to-right in real-time.\n");
  73. printf("Example: %s -d -t 10 -p 5 -e\n", progname);
  74. printf("Example: %s -t 12 -T -n 7 -N\n", progname);
  75. }
  76. int main(int argc, char *argv[])
  77. {
  78. int flags;
  79. unsigned int ping_rate = DEFAULT_PING_RATE;
  80. int ret;
  81. int c;
  82. int oneshot = 0;
  83. setbuf(stdout, NULL);
  84. fd = open("/dev/watchdog", O_WRONLY);
  85. if (fd == -1) {
  86. if (errno == ENOENT)
  87. printf("Watchdog device not enabled.\n");
  88. else if (errno == EACCES)
  89. printf("Run watchdog as root.\n");
  90. else
  91. printf("Watchdog device open failed %s\n",
  92. strerror(errno));
  93. exit(-1);
  94. }
  95. while ((c = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
  96. switch (c) {
  97. case 'b':
  98. flags = 0;
  99. oneshot = 1;
  100. ret = ioctl(fd, WDIOC_GETBOOTSTATUS, &flags);
  101. if (!ret)
  102. printf("Last boot is caused by: %s.\n", (flags != 0) ?
  103. "Watchdog" : "Power-On-Reset");
  104. else
  105. printf("WDIOC_GETBOOTSTATUS error '%s'\n", strerror(errno));
  106. break;
  107. case 'd':
  108. flags = WDIOS_DISABLECARD;
  109. ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
  110. if (!ret)
  111. printf("Watchdog card disabled.\n");
  112. else {
  113. printf("WDIOS_DISABLECARD error '%s'\n", strerror(errno));
  114. oneshot = 1;
  115. }
  116. break;
  117. case 'e':
  118. flags = WDIOS_ENABLECARD;
  119. ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
  120. if (!ret)
  121. printf("Watchdog card enabled.\n");
  122. else {
  123. printf("WDIOS_ENABLECARD error '%s'\n", strerror(errno));
  124. oneshot = 1;
  125. }
  126. break;
  127. case 'p':
  128. ping_rate = strtoul(optarg, NULL, 0);
  129. if (!ping_rate)
  130. ping_rate = DEFAULT_PING_RATE;
  131. printf("Watchdog ping rate set to %u seconds.\n", ping_rate);
  132. break;
  133. case 't':
  134. flags = strtoul(optarg, NULL, 0);
  135. ret = ioctl(fd, WDIOC_SETTIMEOUT, &flags);
  136. if (!ret)
  137. printf("Watchdog timeout set to %u seconds.\n", flags);
  138. else {
  139. printf("WDIOC_SETTIMEOUT error '%s'\n", strerror(errno));
  140. oneshot = 1;
  141. }
  142. break;
  143. case 'T':
  144. oneshot = 1;
  145. ret = ioctl(fd, WDIOC_GETTIMEOUT, &flags);
  146. if (!ret)
  147. printf("WDIOC_GETTIMEOUT returns %u seconds.\n", flags);
  148. else
  149. printf("WDIOC_GETTIMEOUT error '%s'\n", strerror(errno));
  150. break;
  151. case 'n':
  152. flags = strtoul(optarg, NULL, 0);
  153. ret = ioctl(fd, WDIOC_SETPRETIMEOUT, &flags);
  154. if (!ret)
  155. printf("Watchdog pretimeout set to %u seconds.\n", flags);
  156. else {
  157. printf("WDIOC_SETPRETIMEOUT error '%s'\n", strerror(errno));
  158. oneshot = 1;
  159. }
  160. break;
  161. case 'N':
  162. oneshot = 1;
  163. ret = ioctl(fd, WDIOC_GETPRETIMEOUT, &flags);
  164. if (!ret)
  165. printf("WDIOC_GETPRETIMEOUT returns %u seconds.\n", flags);
  166. else
  167. printf("WDIOC_GETPRETIMEOUT error '%s'\n", strerror(errno));
  168. break;
  169. default:
  170. usage(argv[0]);
  171. goto end;
  172. }
  173. }
  174. if (oneshot)
  175. goto end;
  176. printf("Watchdog Ticking Away!\n");
  177. signal(SIGINT, term);
  178. while (1) {
  179. keep_alive();
  180. sleep(ping_rate);
  181. }
  182. end:
  183. ret = write(fd, &v, 1);
  184. if (ret < 0)
  185. printf("Stopping watchdog ticks failed (%d)...\n", errno);
  186. close(fd);
  187. return 0;
  188. }