Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Watchdog Driver Test Program |
| 4 | */ |
| 5 | |
Arnd Bergmann | 9dd8d5f | 2016-07-19 17:41:22 +0200 | [diff] [blame] | 6 | #include <errno.h> |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <unistd.h> |
| 11 | #include <fcntl.h> |
Devendra Naga | cad19fa | 2012-05-17 15:07:48 +0530 | [diff] [blame] | 12 | #include <signal.h> |
Eugeniu Rosca | 749fb26 | 2017-07-01 14:57:26 +0200 | [diff] [blame] | 13 | #include <getopt.h> |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 14 | #include <sys/ioctl.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/watchdog.h> |
| 17 | |
Eugeniu Rosca | f8f92c0 | 2017-07-01 14:57:29 +0200 | [diff] [blame] | 18 | #define DEFAULT_PING_RATE 1 |
| 19 | |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 20 | int fd; |
Timur Tabi | 5a2d3de | 2016-06-21 18:00:15 -0500 | [diff] [blame] | 21 | const char v = 'V'; |
Jerry Hoemann | 7514c39 | 2018-09-26 15:23:09 -0600 | [diff] [blame] | 22 | static const char sopts[] = "bdehp:t:Tn:N"; |
Eugeniu Rosca | 749fb26 | 2017-07-01 14:57:26 +0200 | [diff] [blame] | 23 | static const struct option lopts[] = { |
Eugeniu Rosca | a3d6d79 | 2017-07-01 14:57:30 +0200 | [diff] [blame] | 24 | {"bootstatus", no_argument, NULL, 'b'}, |
Eugeniu Rosca | 749fb26 | 2017-07-01 14:57:26 +0200 | [diff] [blame] | 25 | {"disable", no_argument, NULL, 'd'}, |
| 26 | {"enable", no_argument, NULL, 'e'}, |
| 27 | {"help", no_argument, NULL, 'h'}, |
| 28 | {"pingrate", required_argument, NULL, 'p'}, |
| 29 | {"timeout", required_argument, NULL, 't'}, |
Jerry Hoemann | 7514c39 | 2018-09-26 15:23:09 -0600 | [diff] [blame] | 30 | {"gettimeout", no_argument, NULL, 'T'}, |
| 31 | {"pretimeout", required_argument, NULL, 'n'}, |
| 32 | {"getpretimeout", no_argument, NULL, 'N'}, |
Eugeniu Rosca | 749fb26 | 2017-07-01 14:57:26 +0200 | [diff] [blame] | 33 | {NULL, no_argument, NULL, 0x0} |
| 34 | }; |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * This function simply sends an IOCTL to the driver, which in turn ticks |
| 38 | * the PC Watchdog card to reset its internal timer so it doesn't trigger |
| 39 | * a computer reset. |
| 40 | */ |
Ladinu Chandrasinghe | b7ed698 | 2009-09-22 16:43:42 -0700 | [diff] [blame] | 41 | static void keep_alive(void) |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 42 | { |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame] | 43 | int dummy; |
| 44 | int ret; |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 45 | |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame] | 46 | ret = ioctl(fd, WDIOC_KEEPALIVE, &dummy); |
| 47 | if (!ret) |
| 48 | printf("."); |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /* |
| 52 | * The main program. Run the program with "-d" to disable the card, |
| 53 | * or "-e" to enable the card. |
| 54 | */ |
Devendra Naga | cad19fa | 2012-05-17 15:07:48 +0530 | [diff] [blame] | 55 | |
Randy Dunlap | 4b1c2f4 | 2012-07-23 10:46:11 -0700 | [diff] [blame] | 56 | static void term(int sig) |
Devendra Naga | cad19fa | 2012-05-17 15:07:48 +0530 | [diff] [blame] | 57 | { |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame] | 58 | int ret = write(fd, &v, 1); |
Arnd Bergmann | 9dd8d5f | 2016-07-19 17:41:22 +0200 | [diff] [blame] | 59 | |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame] | 60 | close(fd); |
| 61 | if (ret < 0) |
| 62 | printf("\nStopping watchdog ticks failed (%d)...\n", errno); |
| 63 | else |
| 64 | printf("\nStopping watchdog ticks...\n"); |
| 65 | exit(0); |
Devendra Naga | cad19fa | 2012-05-17 15:07:48 +0530 | [diff] [blame] | 66 | } |
| 67 | |
Eugeniu Rosca | 749fb26 | 2017-07-01 14:57:26 +0200 | [diff] [blame] | 68 | static void usage(char *progname) |
| 69 | { |
| 70 | printf("Usage: %s [options]\n", progname); |
Eugeniu Rosca | a3d6d79 | 2017-07-01 14:57:30 +0200 | [diff] [blame] | 71 | printf(" -b, --bootstatus Get last boot status (Watchdog/POR)\n"); |
Eugeniu Rosca | 749fb26 | 2017-07-01 14:57:26 +0200 | [diff] [blame] | 72 | printf(" -d, --disable Turn off the watchdog timer\n"); |
| 73 | printf(" -e, --enable Turn on the watchdog timer\n"); |
| 74 | printf(" -h, --help Print the help message\n"); |
Eugeniu Rosca | f8f92c0 | 2017-07-01 14:57:29 +0200 | [diff] [blame] | 75 | printf(" -p, --pingrate=P Set ping rate to P seconds (default %d)\n", DEFAULT_PING_RATE); |
Eugeniu Rosca | 749fb26 | 2017-07-01 14:57:26 +0200 | [diff] [blame] | 76 | printf(" -t, --timeout=T Set timeout to T seconds\n"); |
Jerry Hoemann | 7514c39 | 2018-09-26 15:23:09 -0600 | [diff] [blame] | 77 | printf(" -T, --gettimeout Get the timeout\n"); |
| 78 | printf(" -n, --pretimeout=T Set the pretimeout to T seconds\n"); |
| 79 | printf(" -N, --getpretimeout Get the pretimeout\n"); |
Eugeniu Rosca | 749fb26 | 2017-07-01 14:57:26 +0200 | [diff] [blame] | 80 | printf("\n"); |
| 81 | printf("Parameters are parsed left-to-right in real-time.\n"); |
| 82 | printf("Example: %s -d -t 10 -p 5 -e\n", progname); |
Jerry Hoemann | 7514c39 | 2018-09-26 15:23:09 -0600 | [diff] [blame] | 83 | printf("Example: %s -t 12 -T -n 7 -N\n", progname); |
Eugeniu Rosca | 749fb26 | 2017-07-01 14:57:26 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 86 | int main(int argc, char *argv[]) |
| 87 | { |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame] | 88 | int flags; |
Eugeniu Rosca | f8f92c0 | 2017-07-01 14:57:29 +0200 | [diff] [blame] | 89 | unsigned int ping_rate = DEFAULT_PING_RATE; |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame] | 90 | int ret; |
Eugeniu Rosca | 749fb26 | 2017-07-01 14:57:26 +0200 | [diff] [blame] | 91 | int c; |
Eugeniu Rosca | a3d6d79 | 2017-07-01 14:57:30 +0200 | [diff] [blame] | 92 | int oneshot = 0; |
James Hogan | dfc3338 | 2010-04-05 11:31:29 +0100 | [diff] [blame] | 93 | |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame] | 94 | setbuf(stdout, NULL); |
Timur Tabi | ee279c2 | 2016-06-21 18:00:14 -0500 | [diff] [blame] | 95 | |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame] | 96 | fd = open("/dev/watchdog", O_WRONLY); |
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 | if (fd == -1) { |
Shuah Khan (Samsung OSG) | 9a24422 | 2018-09-26 13:07:11 -0600 | [diff] [blame] | 99 | if (errno == ENOENT) |
| 100 | printf("Watchdog device not enabled.\n"); |
| 101 | else if (errno == EACCES) |
| 102 | printf("Run watchdog as root.\n"); |
| 103 | else |
| 104 | printf("Watchdog device open failed %s\n", |
| 105 | strerror(errno)); |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame] | 106 | exit(-1); |
| 107 | } |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 108 | |
Eugeniu Rosca | 749fb26 | 2017-07-01 14:57:26 +0200 | [diff] [blame] | 109 | while ((c = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) { |
| 110 | switch (c) { |
Eugeniu Rosca | a3d6d79 | 2017-07-01 14:57:30 +0200 | [diff] [blame] | 111 | case 'b': |
| 112 | flags = 0; |
| 113 | oneshot = 1; |
| 114 | ret = ioctl(fd, WDIOC_GETBOOTSTATUS, &flags); |
| 115 | if (!ret) |
| 116 | printf("Last boot is caused by: %s.\n", (flags != 0) ? |
| 117 | "Watchdog" : "Power-On-Reset"); |
| 118 | else |
Jerry Hoemann | 04d5e4b | 2018-09-26 15:23:08 -0600 | [diff] [blame] | 119 | printf("WDIOC_GETBOOTSTATUS error '%s'\n", strerror(errno)); |
Eugeniu Rosca | a3d6d79 | 2017-07-01 14:57:30 +0200 | [diff] [blame] | 120 | break; |
Eugeniu Rosca | 749fb26 | 2017-07-01 14:57:26 +0200 | [diff] [blame] | 121 | case 'd': |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame] | 122 | flags = WDIOS_DISABLECARD; |
| 123 | ret = ioctl(fd, WDIOC_SETOPTIONS, &flags); |
| 124 | if (!ret) |
| 125 | printf("Watchdog card disabled.\n"); |
Shuah Khan (Samsung OSG) | d89d08f | 2018-09-26 16:37:43 -0600 | [diff] [blame] | 126 | else { |
Jerry Hoemann | 04d5e4b | 2018-09-26 15:23:08 -0600 | [diff] [blame] | 127 | printf("WDIOS_DISABLECARD error '%s'\n", strerror(errno)); |
Shuah Khan (Samsung OSG) | d89d08f | 2018-09-26 16:37:43 -0600 | [diff] [blame] | 128 | oneshot = 1; |
| 129 | } |
Eugeniu Rosca | 749fb26 | 2017-07-01 14:57:26 +0200 | [diff] [blame] | 130 | break; |
| 131 | case 'e': |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame] | 132 | flags = WDIOS_ENABLECARD; |
| 133 | ret = ioctl(fd, WDIOC_SETOPTIONS, &flags); |
| 134 | if (!ret) |
| 135 | printf("Watchdog card enabled.\n"); |
Shuah Khan (Samsung OSG) | d89d08f | 2018-09-26 16:37:43 -0600 | [diff] [blame] | 136 | else { |
Jerry Hoemann | 04d5e4b | 2018-09-26 15:23:08 -0600 | [diff] [blame] | 137 | printf("WDIOS_ENABLECARD error '%s'\n", strerror(errno)); |
Shuah Khan (Samsung OSG) | d89d08f | 2018-09-26 16:37:43 -0600 | [diff] [blame] | 138 | oneshot = 1; |
| 139 | } |
Eugeniu Rosca | 749fb26 | 2017-07-01 14:57:26 +0200 | [diff] [blame] | 140 | break; |
| 141 | case 'p': |
| 142 | ping_rate = strtoul(optarg, NULL, 0); |
Eugeniu Rosca | f8f92c0 | 2017-07-01 14:57:29 +0200 | [diff] [blame] | 143 | if (!ping_rate) |
| 144 | ping_rate = DEFAULT_PING_RATE; |
Eugeniu Rosca | 749fb26 | 2017-07-01 14:57:26 +0200 | [diff] [blame] | 145 | printf("Watchdog ping rate set to %u seconds.\n", ping_rate); |
| 146 | break; |
| 147 | case 't': |
Eugeniu Rosca | 7a5e10d | 2017-07-01 14:57:27 +0200 | [diff] [blame] | 148 | flags = strtoul(optarg, NULL, 0); |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame] | 149 | ret = ioctl(fd, WDIOC_SETTIMEOUT, &flags); |
| 150 | if (!ret) |
| 151 | printf("Watchdog timeout set to %u seconds.\n", flags); |
Shuah Khan (Samsung OSG) | d89d08f | 2018-09-26 16:37:43 -0600 | [diff] [blame] | 152 | else { |
Jerry Hoemann | 04d5e4b | 2018-09-26 15:23:08 -0600 | [diff] [blame] | 153 | printf("WDIOC_SETTIMEOUT error '%s'\n", strerror(errno)); |
Shuah Khan (Samsung OSG) | d89d08f | 2018-09-26 16:37:43 -0600 | [diff] [blame] | 154 | oneshot = 1; |
| 155 | } |
Eugeniu Rosca | 749fb26 | 2017-07-01 14:57:26 +0200 | [diff] [blame] | 156 | break; |
Jerry Hoemann | 7514c39 | 2018-09-26 15:23:09 -0600 | [diff] [blame] | 157 | case 'T': |
| 158 | oneshot = 1; |
| 159 | ret = ioctl(fd, WDIOC_GETTIMEOUT, &flags); |
| 160 | if (!ret) |
| 161 | printf("WDIOC_GETTIMEOUT returns %u seconds.\n", flags); |
| 162 | else |
| 163 | printf("WDIOC_GETTIMEOUT error '%s'\n", strerror(errno)); |
| 164 | break; |
| 165 | case 'n': |
| 166 | flags = strtoul(optarg, NULL, 0); |
| 167 | ret = ioctl(fd, WDIOC_SETPRETIMEOUT, &flags); |
| 168 | if (!ret) |
| 169 | printf("Watchdog pretimeout set to %u seconds.\n", flags); |
Shuah Khan (Samsung OSG) | d89d08f | 2018-09-26 16:37:43 -0600 | [diff] [blame] | 170 | else { |
Jerry Hoemann | 7514c39 | 2018-09-26 15:23:09 -0600 | [diff] [blame] | 171 | printf("WDIOC_SETPRETIMEOUT error '%s'\n", strerror(errno)); |
Shuah Khan (Samsung OSG) | d89d08f | 2018-09-26 16:37:43 -0600 | [diff] [blame] | 172 | oneshot = 1; |
| 173 | } |
Jerry Hoemann | 7514c39 | 2018-09-26 15:23:09 -0600 | [diff] [blame] | 174 | break; |
| 175 | case 'N': |
| 176 | oneshot = 1; |
| 177 | ret = ioctl(fd, WDIOC_GETPRETIMEOUT, &flags); |
| 178 | if (!ret) |
| 179 | printf("WDIOC_GETPRETIMEOUT returns %u seconds.\n", flags); |
| 180 | else |
| 181 | printf("WDIOC_GETPRETIMEOUT error '%s'\n", strerror(errno)); |
| 182 | break; |
Eugeniu Rosca | 749fb26 | 2017-07-01 14:57:26 +0200 | [diff] [blame] | 183 | default: |
| 184 | usage(argv[0]); |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame] | 185 | goto end; |
| 186 | } |
| 187 | } |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 188 | |
Eugeniu Rosca | a3d6d79 | 2017-07-01 14:57:30 +0200 | [diff] [blame] | 189 | if (oneshot) |
| 190 | goto end; |
| 191 | |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame] | 192 | printf("Watchdog Ticking Away!\n"); |
Timur Tabi | f15d711 | 2015-06-29 11:46:17 -0500 | [diff] [blame] | 193 | |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame] | 194 | signal(SIGINT, term); |
Devendra Naga | cad19fa | 2012-05-17 15:07:48 +0530 | [diff] [blame] | 195 | |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame] | 196 | while (1) { |
| 197 | keep_alive(); |
| 198 | sleep(ping_rate); |
| 199 | } |
Devendra Naga | 3c2a618 | 2012-05-14 23:42:02 +0530 | [diff] [blame] | 200 | end: |
Eugeniu Rosca | 0c528da | 2017-07-01 14:57:25 +0200 | [diff] [blame] | 201 | ret = write(fd, &v, 1); |
| 202 | if (ret < 0) |
| 203 | printf("Stopping watchdog ticks failed (%d)...\n", errno); |
| 204 | close(fd); |
| 205 | return 0; |
Randy Dunlap | 56fb9e5 | 2006-05-21 20:58:10 -0700 | [diff] [blame] | 206 | } |