watchdog-test.c (6574B)
1// SPDX-License-Identifier: GPL-2.0 2/* 3 * Watchdog Driver Test Program 4 */ 5 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> 17 18#define DEFAULT_PING_RATE 1 19 20int fd; 21const char v = 'V'; 22static const char sopts[] = "bdehp:t:Tn:NLf:i"; 23static 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 {"gettimeout", no_argument, NULL, 'T'}, 31 {"pretimeout", required_argument, NULL, 'n'}, 32 {"getpretimeout", no_argument, NULL, 'N'}, 33 {"gettimeleft", no_argument, NULL, 'L'}, 34 {"file", required_argument, NULL, 'f'}, 35 {"info", no_argument, NULL, 'i'}, 36 {NULL, no_argument, NULL, 0x0} 37}; 38 39/* 40 * This function simply sends an IOCTL to the driver, which in turn ticks 41 * the PC Watchdog card to reset its internal timer so it doesn't trigger 42 * a computer reset. 43 */ 44static void keep_alive(void) 45{ 46 int dummy; 47 int ret; 48 49 ret = ioctl(fd, WDIOC_KEEPALIVE, &dummy); 50 if (!ret) 51 printf("."); 52} 53 54/* 55 * The main program. Run the program with "-d" to disable the card, 56 * or "-e" to enable the card. 57 */ 58 59static void term(int sig) 60{ 61 int ret = write(fd, &v, 1); 62 63 close(fd); 64 if (ret < 0) 65 printf("\nStopping watchdog ticks failed (%d)...\n", errno); 66 else 67 printf("\nStopping watchdog ticks...\n"); 68 exit(0); 69} 70 71static void usage(char *progname) 72{ 73 printf("Usage: %s [options]\n", progname); 74 printf(" -f, --file\t\tOpen watchdog device file\n"); 75 printf("\t\t\tDefault is /dev/watchdog\n"); 76 printf(" -i, --info\t\tShow watchdog_info\n"); 77 printf(" -b, --bootstatus\tGet last boot status (Watchdog/POR)\n"); 78 printf(" -d, --disable\t\tTurn off the watchdog timer\n"); 79 printf(" -e, --enable\t\tTurn on the watchdog timer\n"); 80 printf(" -h, --help\t\tPrint the help message\n"); 81 printf(" -p, --pingrate=P\tSet ping rate to P seconds (default %d)\n", 82 DEFAULT_PING_RATE); 83 printf(" -t, --timeout=T\tSet timeout to T seconds\n"); 84 printf(" -T, --gettimeout\tGet the timeout\n"); 85 printf(" -n, --pretimeout=T\tSet the pretimeout to T seconds\n"); 86 printf(" -N, --getpretimeout\tGet the pretimeout\n"); 87 printf(" -L, --gettimeleft\tGet the time left until timer expires\n"); 88 printf("\n"); 89 printf("Parameters are parsed left-to-right in real-time.\n"); 90 printf("Example: %s -d -t 10 -p 5 -e\n", progname); 91 printf("Example: %s -t 12 -T -n 7 -N\n", progname); 92} 93 94int main(int argc, char *argv[]) 95{ 96 int flags; 97 unsigned int ping_rate = DEFAULT_PING_RATE; 98 int ret; 99 int c; 100 int oneshot = 0; 101 char *file = "/dev/watchdog"; 102 struct watchdog_info info; 103 104 setbuf(stdout, NULL); 105 106 while ((c = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) { 107 if (c == 'f') 108 file = optarg; 109 } 110 111 fd = open(file, O_WRONLY); 112 113 if (fd == -1) { 114 if (errno == ENOENT) 115 printf("Watchdog device (%s) not found.\n", file); 116 else if (errno == EACCES) 117 printf("Run watchdog as root.\n"); 118 else 119 printf("Watchdog device open failed %s\n", 120 strerror(errno)); 121 exit(-1); 122 } 123 124 /* 125 * Validate that `file` is a watchdog device 126 */ 127 ret = ioctl(fd, WDIOC_GETSUPPORT, &info); 128 if (ret) { 129 printf("WDIOC_GETSUPPORT error '%s'\n", strerror(errno)); 130 close(fd); 131 exit(ret); 132 } 133 134 optind = 0; 135 136 while ((c = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) { 137 switch (c) { 138 case 'b': 139 flags = 0; 140 oneshot = 1; 141 ret = ioctl(fd, WDIOC_GETBOOTSTATUS, &flags); 142 if (!ret) 143 printf("Last boot is caused by: %s.\n", (flags != 0) ? 144 "Watchdog" : "Power-On-Reset"); 145 else 146 printf("WDIOC_GETBOOTSTATUS error '%s'\n", strerror(errno)); 147 break; 148 case 'd': 149 flags = WDIOS_DISABLECARD; 150 ret = ioctl(fd, WDIOC_SETOPTIONS, &flags); 151 if (!ret) 152 printf("Watchdog card disabled.\n"); 153 else { 154 printf("WDIOS_DISABLECARD error '%s'\n", strerror(errno)); 155 oneshot = 1; 156 } 157 break; 158 case 'e': 159 flags = WDIOS_ENABLECARD; 160 ret = ioctl(fd, WDIOC_SETOPTIONS, &flags); 161 if (!ret) 162 printf("Watchdog card enabled.\n"); 163 else { 164 printf("WDIOS_ENABLECARD error '%s'\n", strerror(errno)); 165 oneshot = 1; 166 } 167 break; 168 case 'p': 169 ping_rate = strtoul(optarg, NULL, 0); 170 if (!ping_rate) 171 ping_rate = DEFAULT_PING_RATE; 172 printf("Watchdog ping rate set to %u seconds.\n", ping_rate); 173 break; 174 case 't': 175 flags = strtoul(optarg, NULL, 0); 176 ret = ioctl(fd, WDIOC_SETTIMEOUT, &flags); 177 if (!ret) 178 printf("Watchdog timeout set to %u seconds.\n", flags); 179 else { 180 printf("WDIOC_SETTIMEOUT error '%s'\n", strerror(errno)); 181 oneshot = 1; 182 } 183 break; 184 case 'T': 185 oneshot = 1; 186 ret = ioctl(fd, WDIOC_GETTIMEOUT, &flags); 187 if (!ret) 188 printf("WDIOC_GETTIMEOUT returns %u seconds.\n", flags); 189 else 190 printf("WDIOC_GETTIMEOUT error '%s'\n", strerror(errno)); 191 break; 192 case 'n': 193 flags = strtoul(optarg, NULL, 0); 194 ret = ioctl(fd, WDIOC_SETPRETIMEOUT, &flags); 195 if (!ret) 196 printf("Watchdog pretimeout set to %u seconds.\n", flags); 197 else { 198 printf("WDIOC_SETPRETIMEOUT error '%s'\n", strerror(errno)); 199 oneshot = 1; 200 } 201 break; 202 case 'N': 203 oneshot = 1; 204 ret = ioctl(fd, WDIOC_GETPRETIMEOUT, &flags); 205 if (!ret) 206 printf("WDIOC_GETPRETIMEOUT returns %u seconds.\n", flags); 207 else 208 printf("WDIOC_GETPRETIMEOUT error '%s'\n", strerror(errno)); 209 break; 210 case 'L': 211 oneshot = 1; 212 ret = ioctl(fd, WDIOC_GETTIMELEFT, &flags); 213 if (!ret) 214 printf("WDIOC_GETTIMELEFT returns %u seconds.\n", flags); 215 else 216 printf("WDIOC_GETTIMELEFT error '%s'\n", strerror(errno)); 217 break; 218 case 'f': 219 /* Handled above */ 220 break; 221 case 'i': 222 /* 223 * watchdog_info was obtained as part of file open 224 * validation. So we just show it here. 225 */ 226 oneshot = 1; 227 printf("watchdog_info:\n"); 228 printf(" identity:\t\t%s\n", info.identity); 229 printf(" firmware_version:\t%u\n", 230 info.firmware_version); 231 printf(" options:\t\t%08x\n", info.options); 232 break; 233 234 default: 235 usage(argv[0]); 236 goto end; 237 } 238 } 239 240 if (oneshot) 241 goto end; 242 243 printf("Watchdog Ticking Away!\n"); 244 245 signal(SIGINT, term); 246 247 while (1) { 248 keep_alive(); 249 sleep(ping_rate); 250 } 251end: 252 ret = write(fd, &v, 1); 253 if (ret < 0) 254 printf("Stopping watchdog ticks failed (%d)...\n", errno); 255 close(fd); 256 return 0; 257}