blob: c6bd9a68306bef345edc54ac8466384fd2a7ed1b [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 Hoemann7514c392018-09-26 15:23:09 -060022static const char sopts[] = "bdehp:t:Tn:N";
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'},
Eugeniu Rosca749fb262017-07-01 14:57:26 +020033 {NULL, no_argument, NULL, 0x0}
34};
Randy Dunlap56fb9e52006-05-21 20:58:10 -070035
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 Chandrasingheb7ed6982009-09-22 16:43:42 -070041static void keep_alive(void)
Randy Dunlap56fb9e52006-05-21 20:58:10 -070042{
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020043 int dummy;
44 int ret;
Randy Dunlap56fb9e52006-05-21 20:58:10 -070045
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020046 ret = ioctl(fd, WDIOC_KEEPALIVE, &dummy);
47 if (!ret)
48 printf(".");
Randy Dunlap56fb9e52006-05-21 20:58:10 -070049}
50
51/*
52 * The main program. Run the program with "-d" to disable the card,
53 * or "-e" to enable the card.
54 */
Devendra Nagacad19fa2012-05-17 15:07:48 +053055
Randy Dunlap4b1c2f42012-07-23 10:46:11 -070056static void term(int sig)
Devendra Nagacad19fa2012-05-17 15:07:48 +053057{
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020058 int ret = write(fd, &v, 1);
Arnd Bergmann9dd8d5f2016-07-19 17:41:22 +020059
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020060 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 Nagacad19fa2012-05-17 15:07:48 +053066}
67
Eugeniu Rosca749fb262017-07-01 14:57:26 +020068static void usage(char *progname)
69{
70 printf("Usage: %s [options]\n", progname);
Eugeniu Roscaa3d6d792017-07-01 14:57:30 +020071 printf(" -b, --bootstatus Get last boot status (Watchdog/POR)\n");
Eugeniu Rosca749fb262017-07-01 14:57:26 +020072 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 Roscaf8f92c02017-07-01 14:57:29 +020075 printf(" -p, --pingrate=P Set ping rate to P seconds (default %d)\n", DEFAULT_PING_RATE);
Eugeniu Rosca749fb262017-07-01 14:57:26 +020076 printf(" -t, --timeout=T Set timeout to T seconds\n");
Jerry Hoemann7514c392018-09-26 15:23:09 -060077 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 Rosca749fb262017-07-01 14:57:26 +020080 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 Hoemann7514c392018-09-26 15:23:09 -060083 printf("Example: %s -t 12 -T -n 7 -N\n", progname);
Eugeniu Rosca749fb262017-07-01 14:57:26 +020084}
85
Randy Dunlap56fb9e52006-05-21 20:58:10 -070086int main(int argc, char *argv[])
87{
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020088 int flags;
Eugeniu Roscaf8f92c02017-07-01 14:57:29 +020089 unsigned int ping_rate = DEFAULT_PING_RATE;
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020090 int ret;
Eugeniu Rosca749fb262017-07-01 14:57:26 +020091 int c;
Eugeniu Roscaa3d6d792017-07-01 14:57:30 +020092 int oneshot = 0;
James Hogandfc33382010-04-05 11:31:29 +010093
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020094 setbuf(stdout, NULL);
Timur Tabiee279c22016-06-21 18:00:14 -050095
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020096 fd = open("/dev/watchdog", O_WRONLY);
Randy Dunlap56fb9e52006-05-21 20:58:10 -070097
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020098 if (fd == -1) {
Shuah Khan (Samsung OSG)9a244222018-09-26 13:07:11 -060099 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 Rosca0c528da2017-07-01 14:57:25 +0200106 exit(-1);
107 }
Randy Dunlap56fb9e52006-05-21 20:58:10 -0700108
Eugeniu Rosca749fb262017-07-01 14:57:26 +0200109 while ((c = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
110 switch (c) {
Eugeniu Roscaa3d6d792017-07-01 14:57:30 +0200111 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 Hoemann04d5e4b2018-09-26 15:23:08 -0600119 printf("WDIOC_GETBOOTSTATUS error '%s'\n", strerror(errno));
Eugeniu Roscaa3d6d792017-07-01 14:57:30 +0200120 break;
Eugeniu Rosca749fb262017-07-01 14:57:26 +0200121 case 'd':
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200122 flags = WDIOS_DISABLECARD;
123 ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
124 if (!ret)
125 printf("Watchdog card disabled.\n");
Shuah Khan (Samsung OSG)d89d08f2018-09-26 16:37:43 -0600126 else {
Jerry Hoemann04d5e4b2018-09-26 15:23:08 -0600127 printf("WDIOS_DISABLECARD error '%s'\n", strerror(errno));
Shuah Khan (Samsung OSG)d89d08f2018-09-26 16:37:43 -0600128 oneshot = 1;
129 }
Eugeniu Rosca749fb262017-07-01 14:57:26 +0200130 break;
131 case 'e':
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200132 flags = WDIOS_ENABLECARD;
133 ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
134 if (!ret)
135 printf("Watchdog card enabled.\n");
Shuah Khan (Samsung OSG)d89d08f2018-09-26 16:37:43 -0600136 else {
Jerry Hoemann04d5e4b2018-09-26 15:23:08 -0600137 printf("WDIOS_ENABLECARD error '%s'\n", strerror(errno));
Shuah Khan (Samsung OSG)d89d08f2018-09-26 16:37:43 -0600138 oneshot = 1;
139 }
Eugeniu Rosca749fb262017-07-01 14:57:26 +0200140 break;
141 case 'p':
142 ping_rate = strtoul(optarg, NULL, 0);
Eugeniu Roscaf8f92c02017-07-01 14:57:29 +0200143 if (!ping_rate)
144 ping_rate = DEFAULT_PING_RATE;
Eugeniu Rosca749fb262017-07-01 14:57:26 +0200145 printf("Watchdog ping rate set to %u seconds.\n", ping_rate);
146 break;
147 case 't':
Eugeniu Rosca7a5e10d2017-07-01 14:57:27 +0200148 flags = strtoul(optarg, NULL, 0);
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200149 ret = ioctl(fd, WDIOC_SETTIMEOUT, &flags);
150 if (!ret)
151 printf("Watchdog timeout set to %u seconds.\n", flags);
Shuah Khan (Samsung OSG)d89d08f2018-09-26 16:37:43 -0600152 else {
Jerry Hoemann04d5e4b2018-09-26 15:23:08 -0600153 printf("WDIOC_SETTIMEOUT error '%s'\n", strerror(errno));
Shuah Khan (Samsung OSG)d89d08f2018-09-26 16:37:43 -0600154 oneshot = 1;
155 }
Eugeniu Rosca749fb262017-07-01 14:57:26 +0200156 break;
Jerry Hoemann7514c392018-09-26 15:23:09 -0600157 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)d89d08f2018-09-26 16:37:43 -0600170 else {
Jerry Hoemann7514c392018-09-26 15:23:09 -0600171 printf("WDIOC_SETPRETIMEOUT error '%s'\n", strerror(errno));
Shuah Khan (Samsung OSG)d89d08f2018-09-26 16:37:43 -0600172 oneshot = 1;
173 }
Jerry Hoemann7514c392018-09-26 15:23:09 -0600174 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 Rosca749fb262017-07-01 14:57:26 +0200183 default:
184 usage(argv[0]);
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200185 goto end;
186 }
187 }
Randy Dunlap56fb9e52006-05-21 20:58:10 -0700188
Eugeniu Roscaa3d6d792017-07-01 14:57:30 +0200189 if (oneshot)
190 goto end;
191
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200192 printf("Watchdog Ticking Away!\n");
Timur Tabif15d7112015-06-29 11:46:17 -0500193
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200194 signal(SIGINT, term);
Devendra Nagacad19fa2012-05-17 15:07:48 +0530195
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200196 while (1) {
197 keep_alive();
198 sleep(ping_rate);
199 }
Devendra Naga3c2a6182012-05-14 23:42:02 +0530200end:
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200201 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 Dunlap56fb9e52006-05-21 20:58:10 -0700206}