main.c 1.9 KB

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