Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Watchdog Driver Test Program |
| 3 | */ |
| 4 | |
Arnd Bergmann | 9dd8d5f | 2016-07-19 17:41:22 +0200 | [diff] [blame] | 5 | #include <errno.h> |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | #include <unistd.h> |
| 10 | #include <fcntl.h> |
Devendra Naga | cad19fa | 2012-05-17 15:07:48 +0530 | [diff] [blame] | 11 | #include <signal.h> |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 12 | #include <sys/ioctl.h> |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/watchdog.h> |
| 15 | |
| 16 | int fd; |
Timur Tabi | 5a2d3de | 2016-06-21 18:00:15 -0500 | [diff] [blame] | 17 | const char v = 'V'; |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * This function simply sends an IOCTL to the driver, which in turn ticks |
| 21 | * the PC Watchdog card to reset its internal timer so it doesn't trigger |
| 22 | * a computer reset. |
| 23 | */ |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 24 | static void keep_alive(void) |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 25 | { |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame^] | 26 | int dummy; |
| 27 | int ret; |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 28 | |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame^] | 29 | ret = ioctl(fd, WDIOC_KEEPALIVE, &dummy); |
| 30 | if (!ret) |
| 31 | printf("."); |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /* |
| 35 | * The main program. Run the program with "-d" to disable the card, |
| 36 | * or "-e" to enable the card. |
| 37 | */ |
Devendra Naga | cad19fa | 2012-05-17 15:07:48 +0530 | [diff] [blame] | 38 | |
Randy Dunlap | 4b1c2f4 | 2012-07-23 10:46:11 -0700 | [diff] [blame] | 39 | static void term(int sig) |
Devendra Naga | cad19fa | 2012-05-17 15:07:48 +0530 | [diff] [blame] | 40 | { |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame^] | 41 | int ret = write(fd, &v, 1); |
Arnd Bergmann | 9dd8d5f | 2016-07-19 17:41:22 +0200 | [diff] [blame] | 42 | |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame^] | 43 | close(fd); |
| 44 | if (ret < 0) |
| 45 | printf("\nStopping watchdog ticks failed (%d)...\n", errno); |
| 46 | else |
| 47 | printf("\nStopping watchdog ticks...\n"); |
| 48 | exit(0); |
Devendra Naga | cad19fa | 2012-05-17 15:07:48 +0530 | [diff] [blame] | 49 | } |
| 50 | |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 51 | int main(int argc, char *argv[]) |
| 52 | { |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame^] | 53 | int flags; |
| 54 | unsigned int ping_rate = 1; |
| 55 | int ret; |
| 56 | int i; |
James Hogan | dfc3338 | 2010-04-05 11:31:29 +0100 | [diff] [blame] | 57 | |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame^] | 58 | setbuf(stdout, NULL); |
Timur Tabi | ee279c2 | 2016-06-21 18:00:14 -0500 | [diff] [blame] | 59 | |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame^] | 60 | fd = open("/dev/watchdog", O_WRONLY); |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 61 | |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame^] | 62 | if (fd == -1) { |
| 63 | printf("Watchdog device not enabled.\n"); |
| 64 | exit(-1); |
| 65 | } |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 66 | |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame^] | 67 | for (i = 1; i < argc; i++) { |
| 68 | if (!strncasecmp(argv[i], "-d", 2)) { |
| 69 | flags = WDIOS_DISABLECARD; |
| 70 | ret = ioctl(fd, WDIOC_SETOPTIONS, &flags); |
| 71 | if (!ret) |
| 72 | printf("Watchdog card disabled.\n"); |
| 73 | } else if (!strncasecmp(argv[i], "-e", 2)) { |
| 74 | flags = WDIOS_ENABLECARD; |
| 75 | ret = ioctl(fd, WDIOC_SETOPTIONS, &flags); |
| 76 | if (!ret) |
| 77 | printf("Watchdog card enabled.\n"); |
| 78 | } else if (!strncasecmp(argv[i], "-t", 2) && argv[2]) { |
| 79 | flags = atoi(argv[i + 1]); |
| 80 | ret = ioctl(fd, WDIOC_SETTIMEOUT, &flags); |
| 81 | if (!ret) |
| 82 | printf("Watchdog timeout set to %u seconds.\n", flags); |
| 83 | i++; |
| 84 | } else if (!strncasecmp(argv[i], "-p", 2) && argv[2]) { |
| 85 | ping_rate = strtoul(argv[i + 1], NULL, 0); |
| 86 | printf("Watchdog ping rate set to %u seconds.\n", ping_rate); |
| 87 | i++; |
| 88 | } else { |
| 89 | printf("-d to disable, -e to enable, -t <n> to set " |
| 90 | "the timeout,\n-p <n> to set the ping rate, and "); |
| 91 | printf("run by itself to tick the card.\n"); |
| 92 | printf("Parameters are parsed left-to-right in real-time.\n"); |
| 93 | printf("Example: %s -d -t 10 -p 5 -e\n", argv[0]); |
| 94 | goto end; |
| 95 | } |
| 96 | } |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 97 | |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame^] | 98 | printf("Watchdog Ticking Away!\n"); |
Timur Tabi | f15d711 | 2015-06-29 11:46:17 -0500 | [diff] [blame] | 99 | |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame^] | 100 | signal(SIGINT, term); |
Devendra Naga | cad19fa | 2012-05-17 15:07:48 +0530 | [diff] [blame] | 101 | |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame^] | 102 | while (1) { |
| 103 | keep_alive(); |
| 104 | sleep(ping_rate); |
| 105 | } |
Devendra Naga | 3c2a618 | 2012-05-14 23:42:02 +0530 | [diff] [blame] | 106 | end: |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame^] | 107 | ret = write(fd, &v, 1); |
| 108 | if (ret < 0) |
| 109 | printf("Stopping watchdog ticks failed (%d)...\n", errno); |
| 110 | close(fd); |
| 111 | return 0; |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 112 | } |