watchdog-test.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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:NLf:i";
  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. {"gettimeleft", no_argument, NULL, 'L'},
  31. {"file", required_argument, NULL, 'f'},
  32. {"info", no_argument, NULL, 'i'},
  33. {NULL, no_argument, NULL, 0x0}
  34. };
  35. /*
  36. * This function simply sends an IOCTL to the driver, which in turn ticks
  37. * the PC Watchdog card to reset its internal timer so it doesn't trigger
  38. * a computer reset.
  39. */
  40. static void keep_alive(void)
  41. {
  42. int dummy;
  43. int ret;
  44. ret = ioctl(fd, WDIOC_KEEPALIVE, &dummy);
  45. if (!ret)
  46. printf(".");
  47. }
  48. /*
  49. * The main program. Run the program with "-d" to disable the card,
  50. * or "-e" to enable the card.
  51. */
  52. static void term(int sig)
  53. {
  54. int ret = write(fd, &v, 1);
  55. close(fd);
  56. if (ret < 0)
  57. printf("\nStopping watchdog ticks failed (%d)...\n", errno);
  58. else
  59. printf("\nStopping watchdog ticks...\n");
  60. exit(0);
  61. }
  62. static void usage(char *progname)
  63. {
  64. printf("Usage: %s [options]\n", progname);
  65. printf(" -f, --file\t\tOpen watchdog device file\n");
  66. printf("\t\t\tDefault is /dev/watchdog\n");
  67. printf(" -i, --info\t\tShow watchdog_info\n");
  68. printf(" -b, --bootstatus\tGet last boot status (Watchdog/POR)\n");
  69. printf(" -d, --disable\t\tTurn off the watchdog timer\n");
  70. printf(" -e, --enable\t\tTurn on the watchdog timer\n");
  71. printf(" -h, --help\t\tPrint the help message\n");
  72. printf(" -p, --pingrate=P\tSet ping rate to P seconds (default %d)\n",
  73. DEFAULT_PING_RATE);
  74. printf(" -t, --timeout=T\tSet timeout to T seconds\n");
  75. printf(" -T, --gettimeout\tGet the timeout\n");
  76. printf(" -n, --pretimeout=T\tSet the pretimeout to T seconds\n");
  77. printf(" -N, --getpretimeout\tGet the pretimeout\n");
  78. printf(" -L, --gettimeleft\tGet the time left until timer expires\n");
  79. printf("\n");
  80. printf("Parameters are parsed left-to-right in real-time.\n");
  81. printf("Example: %s -d -t 10 -p 5 -e\n", progname);
  82. printf("Example: %s -t 12 -T -n 7 -N\n", progname);
  83. }
  84. int main(int argc, char *argv[])
  85. {
  86. int flags;
  87. unsigned int ping_rate = DEFAULT_PING_RATE;
  88. int ret;
  89. int c;
  90. int oneshot = 0;
  91. char *file = "/dev/watchdog";
  92. struct watchdog_info info;
  93. setbuf(stdout, NULL);
  94. while ((c = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
  95. if (c == 'f')
  96. file = optarg;
  97. }
  98. fd = open(file, O_WRONLY);
  99. if (fd == -1) {
  100. if (errno == ENOENT)
  101. printf("Watchdog device (%s) not found.\n", file);
  102. else if (errno == EACCES)
  103. printf("Run watchdog as root.\n");
  104. else
  105. printf("Watchdog device open failed %s\n",
  106. strerror(errno));
  107. exit(-1);
  108. }
  109. /*
  110. * Validate that `file` is a watchdog device
  111. */
  112. ret = ioctl(fd, WDIOC_GETSUPPORT, &info);
  113. if (ret) {
  114. printf("WDIOC_GETSUPPORT error '%s'\n", strerror(errno));
  115. close(fd);
  116. exit(ret);
  117. }
  118. optind = 0;
  119. while ((c = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
  120. switch (c) {
  121. case 'b':
  122. flags = 0;
  123. oneshot = 1;
  124. ret = ioctl(fd, WDIOC_GETBOOTSTATUS, &flags);
  125. if (!ret)
  126. printf("Last boot is caused by: %s.\n", (flags != 0) ?
  127. "Watchdog" : "Power-On-Reset");
  128. else
  129. printf("WDIOC_GETBOOTSTATUS error '%s'\n", strerror(errno));
  130. break;
  131. case 'd':
  132. flags = WDIOS_DISABLECARD;
  133. ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
  134. if (!ret)
  135. printf("Watchdog card disabled.\n");
  136. else {
  137. printf("WDIOS_DISABLECARD error '%s'\n", strerror(errno));
  138. oneshot = 1;
  139. }
  140. break;
  141. case 'e':
  142. flags = WDIOS_ENABLECARD;
  143. ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
  144. if (!ret)
  145. printf("Watchdog card enabled.\n");
  146. else {
  147. printf("WDIOS_ENABLECARD error '%s'\n", strerror(errno));
  148. oneshot = 1;
  149. }
  150. break;
  151. case 'p':
  152. ping_rate = strtoul(optarg, NULL, 0);
  153. if (!ping_rate)
  154. ping_rate = DEFAULT_PING_RATE;
  155. printf("Watchdog ping rate set to %u seconds.\n", ping_rate);
  156. break;
  157. case 't':
  158. flags = strtoul(optarg, NULL, 0);
  159. ret = ioctl(fd, WDIOC_SETTIMEOUT, &flags);
  160. if (!ret)
  161. printf("Watchdog timeout set to %u seconds.\n", flags);
  162. else {
  163. printf("WDIOC_SETTIMEOUT error '%s'\n", strerror(errno));
  164. oneshot = 1;
  165. }
  166. break;
  167. case 'T':
  168. oneshot = 1;
  169. ret = ioctl(fd, WDIOC_GETTIMEOUT, &flags);
  170. if (!ret)
  171. printf("WDIOC_GETTIMEOUT returns %u seconds.\n", flags);
  172. else
  173. printf("WDIOC_GETTIMEOUT error '%s'\n", strerror(errno));
  174. break;
  175. case 'n':
  176. flags = strtoul(optarg, NULL, 0);
  177. ret = ioctl(fd, WDIOC_SETPRETIMEOUT, &flags);
  178. if (!ret)
  179. printf("Watchdog pretimeout set to %u seconds.\n", flags);
  180. else {
  181. printf("WDIOC_SETPRETIMEOUT error '%s'\n", strerror(errno));
  182. oneshot = 1;
  183. }
  184. break;
  185. case 'N':
  186. oneshot = 1;
  187. ret = ioctl(fd, WDIOC_GETPRETIMEOUT, &flags);
  188. if (!ret)
  189. printf("WDIOC_GETPRETIMEOUT returns %u seconds.\n", flags);
  190. else
  191. printf("WDIOC_GETPRETIMEOUT error '%s'\n", strerror(errno));
  192. break;
  193. case 'L':
  194. oneshot = 1;
  195. ret = ioctl(fd, WDIOC_GETTIMELEFT, &flags);
  196. if (!ret)
  197. printf("WDIOC_GETTIMELEFT returns %u seconds.\n", flags);
  198. else
  199. printf("WDIOC_GETTIMELEFT error '%s'\n", strerror(errno));
  200. break;
  201. case 'f':
  202. /* Handled above */
  203. break;
  204. case 'i':
  205. /*
  206. * watchdog_info was obtained as part of file open
  207. * validation. So we just show it here.
  208. */
  209. oneshot = 1;
  210. printf("watchdog_info:\n");
  211. printf(" identity:\t\t%s\n", info.identity);
  212. printf(" firmware_version:\t%u\n",
  213. info.firmware_version);
  214. printf(" options:\t\t%08x\n", info.options);
  215. break;
  216. default:
  217. usage(argv[0]);
  218. goto end;
  219. }
  220. }
  221. if (oneshot)
  222. goto end;
  223. printf("Watchdog Ticking Away!\n");
  224. signal(SIGINT, term);
  225. while (1) {
  226. keep_alive();
  227. sleep(ping_rate);
  228. }
  229. end:
  230. ret = write(fd, &v, 1);
  231. if (ret < 0)
  232. printf("Stopping watchdog ticks failed (%d)...\n", errno);
  233. close(fd);
  234. return 0;
  235. }