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