Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / selftests / watchdog / watchdog-test.c
blobf1c6e025cbe549a3eb2f083aa363080ec2db8126
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Watchdog Driver Test Program
4 */
6 #include <errno.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <getopt.h>
14 #include <sys/ioctl.h>
15 #include <linux/types.h>
16 #include <linux/watchdog.h>
18 #define DEFAULT_PING_RATE 1
20 int fd;
21 const char v = 'V';
22 static const char sopts[] = "bdehp:t:";
23 static const struct option lopts[] = {
24 {"bootstatus", no_argument, NULL, 'b'},
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'},
30 {NULL, no_argument, NULL, 0x0}
34 * This function simply sends an IOCTL to the driver, which in turn ticks
35 * the PC Watchdog card to reset its internal timer so it doesn't trigger
36 * a computer reset.
38 static void keep_alive(void)
40 int dummy;
41 int ret;
43 ret = ioctl(fd, WDIOC_KEEPALIVE, &dummy);
44 if (!ret)
45 printf(".");
49 * The main program. Run the program with "-d" to disable the card,
50 * or "-e" to enable the card.
53 static void term(int sig)
55 int ret = write(fd, &v, 1);
57 close(fd);
58 if (ret < 0)
59 printf("\nStopping watchdog ticks failed (%d)...\n", errno);
60 else
61 printf("\nStopping watchdog ticks...\n");
62 exit(0);
65 static void usage(char *progname)
67 printf("Usage: %s [options]\n", progname);
68 printf(" -b, --bootstatus Get last boot status (Watchdog/POR)\n");
69 printf(" -d, --disable Turn off the watchdog timer\n");
70 printf(" -e, --enable Turn on the watchdog timer\n");
71 printf(" -h, --help Print the help message\n");
72 printf(" -p, --pingrate=P Set ping rate to P seconds (default %d)\n", DEFAULT_PING_RATE);
73 printf(" -t, --timeout=T Set timeout to T seconds\n");
74 printf("\n");
75 printf("Parameters are parsed left-to-right in real-time.\n");
76 printf("Example: %s -d -t 10 -p 5 -e\n", progname);
79 int main(int argc, char *argv[])
81 int flags;
82 unsigned int ping_rate = DEFAULT_PING_RATE;
83 int ret;
84 int c;
85 int oneshot = 0;
87 setbuf(stdout, NULL);
89 fd = open("/dev/watchdog", O_WRONLY);
91 if (fd == -1) {
92 if (errno == ENOENT)
93 printf("Watchdog device not enabled.\n");
94 else if (errno == EACCES)
95 printf("Run watchdog as root.\n");
96 else
97 printf("Watchdog device open failed %s\n",
98 strerror(errno));
99 exit(-1);
102 while ((c = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
103 switch (c) {
104 case 'b':
105 flags = 0;
106 oneshot = 1;
107 ret = ioctl(fd, WDIOC_GETBOOTSTATUS, &flags);
108 if (!ret)
109 printf("Last boot is caused by: %s.\n", (flags != 0) ?
110 "Watchdog" : "Power-On-Reset");
111 else
112 printf("WDIOC_GETBOOTSTATUS error '%s'\n", strerror(errno));
113 break;
114 case 'd':
115 flags = WDIOS_DISABLECARD;
116 ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
117 if (!ret)
118 printf("Watchdog card disabled.\n");
119 else
120 printf("WDIOS_DISABLECARD error '%s'\n", strerror(errno));
121 break;
122 case 'e':
123 flags = WDIOS_ENABLECARD;
124 ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
125 if (!ret)
126 printf("Watchdog card enabled.\n");
127 else
128 printf("WDIOS_ENABLECARD error '%s'\n", strerror(errno));
129 break;
130 case 'p':
131 ping_rate = strtoul(optarg, NULL, 0);
132 if (!ping_rate)
133 ping_rate = DEFAULT_PING_RATE;
134 printf("Watchdog ping rate set to %u seconds.\n", ping_rate);
135 break;
136 case 't':
137 flags = strtoul(optarg, NULL, 0);
138 ret = ioctl(fd, WDIOC_SETTIMEOUT, &flags);
139 if (!ret)
140 printf("Watchdog timeout set to %u seconds.\n", flags);
141 else
142 printf("WDIOC_SETTIMEOUT error '%s'\n", strerror(errno));
143 break;
144 default:
145 usage(argv[0]);
146 goto end;
150 if (oneshot)
151 goto end;
153 printf("Watchdog Ticking Away!\n");
155 signal(SIGINT, term);
157 while (1) {
158 keep_alive();
159 sleep(ping_rate);
161 end:
162 ret = write(fd, &v, 1);
163 if (ret < 0)
164 printf("Stopping watchdog ticks failed (%d)...\n", errno);
165 close(fd);
166 return 0;