blob: c2333c78cf04dbd2fe94e356b24dc8500fe5cad9 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Randy Dunlap56fb9e52006-05-21 20:58:10 -07002/*
3 * Watchdog Driver Test Program
4 */
5
Arnd Bergmann9dd8d5f2016-07-19 17:41:22 +02006#include <errno.h>
Randy Dunlap56fb9e52006-05-21 20:58:10 -07007#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <unistd.h>
11#include <fcntl.h>
Devendra Nagacad19fa2012-05-17 15:07:48 +053012#include <signal.h>
Eugeniu Rosca749fb262017-07-01 14:57:26 +020013#include <getopt.h>
Randy Dunlap56fb9e52006-05-21 20:58:10 -070014#include <sys/ioctl.h>
15#include <linux/types.h>
16#include <linux/watchdog.h>
17
Eugeniu Roscaf8f92c02017-07-01 14:57:29 +020018#define DEFAULT_PING_RATE 1
19
Randy Dunlap56fb9e52006-05-21 20:58:10 -070020int fd;
Timur Tabi5a2d3de2016-06-21 18:00:15 -050021const char v = 'V';
Jerry Hoemann6059c6b2018-11-30 10:35:41 -070022static const char sopts[] = "bdehp:t:Tn:NL";
Eugeniu Rosca749fb262017-07-01 14:57:26 +020023static const struct option lopts[] = {
Eugeniu Roscaa3d6d792017-07-01 14:57:30 +020024 {"bootstatus", no_argument, NULL, 'b'},
Eugeniu Rosca749fb262017-07-01 14:57:26 +020025 {"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 Hoemann7514c392018-09-26 15:23:09 -060030 {"gettimeout", no_argument, NULL, 'T'},
31 {"pretimeout", required_argument, NULL, 'n'},
32 {"getpretimeout", no_argument, NULL, 'N'},
Jerry Hoemann6059c6b2018-11-30 10:35:41 -070033 {"gettimeleft", no_argument, NULL, 'L'},
Eugeniu Rosca749fb262017-07-01 14:57:26 +020034 {NULL, no_argument, NULL, 0x0}
35};
Randy Dunlap56fb9e52006-05-21 20:58:10 -070036
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 Chandrasingheb7ed6982009-09-22 16:43:42 -070042static void keep_alive(void)
Randy Dunlap56fb9e52006-05-21 20:58:10 -070043{
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020044 int dummy;
45 int ret;
Randy Dunlap56fb9e52006-05-21 20:58:10 -070046
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020047 ret = ioctl(fd, WDIOC_KEEPALIVE, &dummy);
48 if (!ret)
49 printf(".");
Randy Dunlap56fb9e52006-05-21 20:58:10 -070050}
51
52/*
53 * The main program. Run the program with "-d" to disable the card,
54 * or "-e" to enable the card.
55 */
Devendra Nagacad19fa2012-05-17 15:07:48 +053056
Randy Dunlap4b1c2f42012-07-23 10:46:11 -070057static void term(int sig)
Devendra Nagacad19fa2012-05-17 15:07:48 +053058{
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020059 int ret = write(fd, &v, 1);
Arnd Bergmann9dd8d5f2016-07-19 17:41:22 +020060
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020061 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 Nagacad19fa2012-05-17 15:07:48 +053067}
68
Eugeniu Rosca749fb262017-07-01 14:57:26 +020069static void usage(char *progname)
70{
71 printf("Usage: %s [options]\n", progname);
Eugeniu Roscaa3d6d792017-07-01 14:57:30 +020072 printf(" -b, --bootstatus Get last boot status (Watchdog/POR)\n");
Eugeniu Rosca749fb262017-07-01 14:57:26 +020073 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 Roscaf8f92c02017-07-01 14:57:29 +020076 printf(" -p, --pingrate=P Set ping rate to P seconds (default %d)\n", DEFAULT_PING_RATE);
Eugeniu Rosca749fb262017-07-01 14:57:26 +020077 printf(" -t, --timeout=T Set timeout to T seconds\n");
Jerry Hoemann7514c392018-09-26 15:23:09 -060078 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");
Colin Ian King0ca29232018-12-13 09:40:15 +000081 printf(" -L, --gettimeleft Get the time left until timer expires\n");
Eugeniu Rosca749fb262017-07-01 14:57:26 +020082 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 Hoemann7514c392018-09-26 15:23:09 -060085 printf("Example: %s -t 12 -T -n 7 -N\n", progname);
Eugeniu Rosca749fb262017-07-01 14:57:26 +020086}
87
Randy Dunlap56fb9e52006-05-21 20:58:10 -070088int main(int argc, char *argv[])
89{
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020090 int flags;
Eugeniu Roscaf8f92c02017-07-01 14:57:29 +020091 unsigned int ping_rate = DEFAULT_PING_RATE;
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020092 int ret;
Eugeniu Rosca749fb262017-07-01 14:57:26 +020093 int c;
Eugeniu Roscaa3d6d792017-07-01 14:57:30 +020094 int oneshot = 0;
James Hogandfc33382010-04-05 11:31:29 +010095
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020096 setbuf(stdout, NULL);
Timur Tabiee279c22016-06-21 18:00:14 -050097
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020098 fd = open("/dev/watchdog", O_WRONLY);
Randy Dunlap56fb9e52006-05-21 20:58:10 -070099
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200100 if (fd == -1) {
Shuah Khan (Samsung OSG)9a244222018-09-26 13:07:11 -0600101 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 Rosca0c528da2017-07-01 14:57:25 +0200108 exit(-1);
109 }
Randy Dunlap56fb9e52006-05-21 20:58:10 -0700110
Eugeniu Rosca749fb262017-07-01 14:57:26 +0200111 while ((c = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
112 switch (c) {
Eugeniu Roscaa3d6d792017-07-01 14:57:30 +0200113 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 Hoemann04d5e4b2018-09-26 15:23:08 -0600121 printf("WDIOC_GETBOOTSTATUS error '%s'\n", strerror(errno));
Eugeniu Roscaa3d6d792017-07-01 14:57:30 +0200122 break;
Eugeniu Rosca749fb262017-07-01 14:57:26 +0200123 case 'd':
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200124 flags = WDIOS_DISABLECARD;
125 ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
126 if (!ret)
127 printf("Watchdog card disabled.\n");
Shuah Khan (Samsung OSG)d89d08f2018-09-26 16:37:43 -0600128 else {
Jerry Hoemann04d5e4b2018-09-26 15:23:08 -0600129 printf("WDIOS_DISABLECARD error '%s'\n", strerror(errno));
Shuah Khan (Samsung OSG)d89d08f2018-09-26 16:37:43 -0600130 oneshot = 1;
131 }
Eugeniu Rosca749fb262017-07-01 14:57:26 +0200132 break;
133 case 'e':
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200134 flags = WDIOS_ENABLECARD;
135 ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
136 if (!ret)
137 printf("Watchdog card enabled.\n");
Shuah Khan (Samsung OSG)d89d08f2018-09-26 16:37:43 -0600138 else {
Jerry Hoemann04d5e4b2018-09-26 15:23:08 -0600139 printf("WDIOS_ENABLECARD error '%s'\n", strerror(errno));
Shuah Khan (Samsung OSG)d89d08f2018-09-26 16:37:43 -0600140 oneshot = 1;
141 }
Eugeniu Rosca749fb262017-07-01 14:57:26 +0200142 break;
143 case 'p':
144 ping_rate = strtoul(optarg, NULL, 0);
Eugeniu Roscaf8f92c02017-07-01 14:57:29 +0200145 if (!ping_rate)
146 ping_rate = DEFAULT_PING_RATE;
Eugeniu Rosca749fb262017-07-01 14:57:26 +0200147 printf("Watchdog ping rate set to %u seconds.\n", ping_rate);
148 break;
149 case 't':
Eugeniu Rosca7a5e10d2017-07-01 14:57:27 +0200150 flags = strtoul(optarg, NULL, 0);
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200151 ret = ioctl(fd, WDIOC_SETTIMEOUT, &flags);
152 if (!ret)
153 printf("Watchdog timeout set to %u seconds.\n", flags);
Shuah Khan (Samsung OSG)d89d08f2018-09-26 16:37:43 -0600154 else {
Jerry Hoemann04d5e4b2018-09-26 15:23:08 -0600155 printf("WDIOC_SETTIMEOUT error '%s'\n", strerror(errno));
Shuah Khan (Samsung OSG)d89d08f2018-09-26 16:37:43 -0600156 oneshot = 1;
157 }
Eugeniu Rosca749fb262017-07-01 14:57:26 +0200158 break;
Jerry Hoemann7514c392018-09-26 15:23:09 -0600159 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)d89d08f2018-09-26 16:37:43 -0600172 else {
Jerry Hoemann7514c392018-09-26 15:23:09 -0600173 printf("WDIOC_SETPRETIMEOUT error '%s'\n", strerror(errno));
Shuah Khan (Samsung OSG)d89d08f2018-09-26 16:37:43 -0600174 oneshot = 1;
175 }
Jerry Hoemann7514c392018-09-26 15:23:09 -0600176 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 Hoemann6059c6b2018-11-30 10:35:41 -0700185 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 Rosca749fb262017-07-01 14:57:26 +0200194 default:
195 usage(argv[0]);
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200196 goto end;
197 }
198 }
Randy Dunlap56fb9e52006-05-21 20:58:10 -0700199
Eugeniu Roscaa3d6d792017-07-01 14:57:30 +0200200 if (oneshot)
201 goto end;
202
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200203 printf("Watchdog Ticking Away!\n");
Timur Tabif15d7112015-06-29 11:46:17 -0500204
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200205 signal(SIGINT, term);
Devendra Nagacad19fa2012-05-17 15:07:48 +0530206
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200207 while (1) {
208 keep_alive();
209 sleep(ping_rate);
210 }
Devendra Naga3c2a6182012-05-14 23:42:02 +0530211end:
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200212 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 Dunlap56fb9e52006-05-21 20:58:10 -0700217}