watchdog-test.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. printf("Watchdog device not enabled.\n");
  80. exit(-1);
  81. }
  82. while ((c = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
  83. switch (c) {
  84. case 'b':
  85. flags = 0;
  86. oneshot = 1;
  87. ret = ioctl(fd, WDIOC_GETBOOTSTATUS, &flags);
  88. if (!ret)
  89. printf("Last boot is caused by: %s.\n", (flags != 0) ?
  90. "Watchdog" : "Power-On-Reset");
  91. else
  92. printf("WDIOC_GETBOOTSTATUS errno '%s'\n", strerror(errno));
  93. break;
  94. case 'd':
  95. flags = WDIOS_DISABLECARD;
  96. ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
  97. if (!ret)
  98. printf("Watchdog card disabled.\n");
  99. else
  100. printf("WDIOS_DISABLECARD errno '%s'\n", strerror(errno));
  101. break;
  102. case 'e':
  103. flags = WDIOS_ENABLECARD;
  104. ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
  105. if (!ret)
  106. printf("Watchdog card enabled.\n");
  107. else
  108. printf("WDIOS_ENABLECARD errno '%s'\n", strerror(errno));
  109. break;
  110. case 'p':
  111. ping_rate = strtoul(optarg, NULL, 0);
  112. if (!ping_rate)
  113. ping_rate = DEFAULT_PING_RATE;
  114. printf("Watchdog ping rate set to %u seconds.\n", ping_rate);
  115. break;
  116. case 't':
  117. flags = strtoul(optarg, NULL, 0);
  118. ret = ioctl(fd, WDIOC_SETTIMEOUT, &flags);
  119. if (!ret)
  120. printf("Watchdog timeout set to %u seconds.\n", flags);
  121. else
  122. printf("WDIOC_SETTIMEOUT errno '%s'\n", strerror(errno));
  123. break;
  124. default:
  125. usage(argv[0]);
  126. goto end;
  127. }
  128. }
  129. if (oneshot)
  130. goto end;
  131. printf("Watchdog Ticking Away!\n");
  132. signal(SIGINT, term);
  133. while (1) {
  134. keep_alive();
  135. sleep(ping_rate);
  136. }
  137. end:
  138. ret = write(fd, &v, 1);
  139. if (ret < 0)
  140. printf("Stopping watchdog ticks failed (%d)...\n", errno);
  141. close(fd);
  142. return 0;
  143. }