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