0001
0002
0003
0004
0005
0006 #include <errno.h>
0007 #include <stdio.h>
0008 #include <stdlib.h>
0009 #include <string.h>
0010 #include <unistd.h>
0011 #include <fcntl.h>
0012 #include <signal.h>
0013 #include <getopt.h>
0014 #include <sys/ioctl.h>
0015 #include <linux/types.h>
0016 #include <linux/watchdog.h>
0017
0018 #define DEFAULT_PING_RATE 1
0019
0020 int fd;
0021 const char v = 'V';
0022 static const char sopts[] = "bdehp:t:Tn:NLf:i";
0023 static const struct option lopts[] = {
0024 {"bootstatus", no_argument, NULL, 'b'},
0025 {"disable", no_argument, NULL, 'd'},
0026 {"enable", no_argument, NULL, 'e'},
0027 {"help", no_argument, NULL, 'h'},
0028 {"pingrate", required_argument, NULL, 'p'},
0029 {"timeout", required_argument, NULL, 't'},
0030 {"gettimeout", no_argument, NULL, 'T'},
0031 {"pretimeout", required_argument, NULL, 'n'},
0032 {"getpretimeout", no_argument, NULL, 'N'},
0033 {"gettimeleft", no_argument, NULL, 'L'},
0034 {"file", required_argument, NULL, 'f'},
0035 {"info", no_argument, NULL, 'i'},
0036 {NULL, no_argument, NULL, 0x0}
0037 };
0038
0039
0040
0041
0042
0043
0044 static void keep_alive(void)
0045 {
0046 int dummy;
0047 int ret;
0048
0049 ret = ioctl(fd, WDIOC_KEEPALIVE, &dummy);
0050 if (!ret)
0051 printf(".");
0052 }
0053
0054
0055
0056
0057
0058
0059 static void term(int sig)
0060 {
0061 int ret = write(fd, &v, 1);
0062
0063 close(fd);
0064 if (ret < 0)
0065 printf("\nStopping watchdog ticks failed (%d)...\n", errno);
0066 else
0067 printf("\nStopping watchdog ticks...\n");
0068 exit(0);
0069 }
0070
0071 static void usage(char *progname)
0072 {
0073 printf("Usage: %s [options]\n", progname);
0074 printf(" -f, --file\t\tOpen watchdog device file\n");
0075 printf("\t\t\tDefault is /dev/watchdog\n");
0076 printf(" -i, --info\t\tShow watchdog_info\n");
0077 printf(" -b, --bootstatus\tGet last boot status (Watchdog/POR)\n");
0078 printf(" -d, --disable\t\tTurn off the watchdog timer\n");
0079 printf(" -e, --enable\t\tTurn on the watchdog timer\n");
0080 printf(" -h, --help\t\tPrint the help message\n");
0081 printf(" -p, --pingrate=P\tSet ping rate to P seconds (default %d)\n",
0082 DEFAULT_PING_RATE);
0083 printf(" -t, --timeout=T\tSet timeout to T seconds\n");
0084 printf(" -T, --gettimeout\tGet the timeout\n");
0085 printf(" -n, --pretimeout=T\tSet the pretimeout to T seconds\n");
0086 printf(" -N, --getpretimeout\tGet the pretimeout\n");
0087 printf(" -L, --gettimeleft\tGet the time left until timer expires\n");
0088 printf("\n");
0089 printf("Parameters are parsed left-to-right in real-time.\n");
0090 printf("Example: %s -d -t 10 -p 5 -e\n", progname);
0091 printf("Example: %s -t 12 -T -n 7 -N\n", progname);
0092 }
0093
0094 int main(int argc, char *argv[])
0095 {
0096 int flags;
0097 unsigned int ping_rate = DEFAULT_PING_RATE;
0098 int ret;
0099 int c;
0100 int oneshot = 0;
0101 char *file = "/dev/watchdog";
0102 struct watchdog_info info;
0103
0104 setbuf(stdout, NULL);
0105
0106 while ((c = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
0107 if (c == 'f')
0108 file = optarg;
0109 }
0110
0111 fd = open(file, O_WRONLY);
0112
0113 if (fd == -1) {
0114 if (errno == ENOENT)
0115 printf("Watchdog device (%s) not found.\n", file);
0116 else if (errno == EACCES)
0117 printf("Run watchdog as root.\n");
0118 else
0119 printf("Watchdog device open failed %s\n",
0120 strerror(errno));
0121 exit(-1);
0122 }
0123
0124
0125
0126
0127 ret = ioctl(fd, WDIOC_GETSUPPORT, &info);
0128 if (ret) {
0129 printf("WDIOC_GETSUPPORT error '%s'\n", strerror(errno));
0130 close(fd);
0131 exit(ret);
0132 }
0133
0134 optind = 0;
0135
0136 while ((c = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
0137 switch (c) {
0138 case 'b':
0139 flags = 0;
0140 oneshot = 1;
0141 ret = ioctl(fd, WDIOC_GETBOOTSTATUS, &flags);
0142 if (!ret)
0143 printf("Last boot is caused by: %s.\n", (flags != 0) ?
0144 "Watchdog" : "Power-On-Reset");
0145 else
0146 printf("WDIOC_GETBOOTSTATUS error '%s'\n", strerror(errno));
0147 break;
0148 case 'd':
0149 flags = WDIOS_DISABLECARD;
0150 ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
0151 if (!ret)
0152 printf("Watchdog card disabled.\n");
0153 else {
0154 printf("WDIOS_DISABLECARD error '%s'\n", strerror(errno));
0155 oneshot = 1;
0156 }
0157 break;
0158 case 'e':
0159 flags = WDIOS_ENABLECARD;
0160 ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
0161 if (!ret)
0162 printf("Watchdog card enabled.\n");
0163 else {
0164 printf("WDIOS_ENABLECARD error '%s'\n", strerror(errno));
0165 oneshot = 1;
0166 }
0167 break;
0168 case 'p':
0169 ping_rate = strtoul(optarg, NULL, 0);
0170 if (!ping_rate)
0171 ping_rate = DEFAULT_PING_RATE;
0172 printf("Watchdog ping rate set to %u seconds.\n", ping_rate);
0173 break;
0174 case 't':
0175 flags = strtoul(optarg, NULL, 0);
0176 ret = ioctl(fd, WDIOC_SETTIMEOUT, &flags);
0177 if (!ret)
0178 printf("Watchdog timeout set to %u seconds.\n", flags);
0179 else {
0180 printf("WDIOC_SETTIMEOUT error '%s'\n", strerror(errno));
0181 oneshot = 1;
0182 }
0183 break;
0184 case 'T':
0185 oneshot = 1;
0186 ret = ioctl(fd, WDIOC_GETTIMEOUT, &flags);
0187 if (!ret)
0188 printf("WDIOC_GETTIMEOUT returns %u seconds.\n", flags);
0189 else
0190 printf("WDIOC_GETTIMEOUT error '%s'\n", strerror(errno));
0191 break;
0192 case 'n':
0193 flags = strtoul(optarg, NULL, 0);
0194 ret = ioctl(fd, WDIOC_SETPRETIMEOUT, &flags);
0195 if (!ret)
0196 printf("Watchdog pretimeout set to %u seconds.\n", flags);
0197 else {
0198 printf("WDIOC_SETPRETIMEOUT error '%s'\n", strerror(errno));
0199 oneshot = 1;
0200 }
0201 break;
0202 case 'N':
0203 oneshot = 1;
0204 ret = ioctl(fd, WDIOC_GETPRETIMEOUT, &flags);
0205 if (!ret)
0206 printf("WDIOC_GETPRETIMEOUT returns %u seconds.\n", flags);
0207 else
0208 printf("WDIOC_GETPRETIMEOUT error '%s'\n", strerror(errno));
0209 break;
0210 case 'L':
0211 oneshot = 1;
0212 ret = ioctl(fd, WDIOC_GETTIMELEFT, &flags);
0213 if (!ret)
0214 printf("WDIOC_GETTIMELEFT returns %u seconds.\n", flags);
0215 else
0216 printf("WDIOC_GETTIMELEFT error '%s'\n", strerror(errno));
0217 break;
0218 case 'f':
0219
0220 break;
0221 case 'i':
0222
0223
0224
0225
0226 oneshot = 1;
0227 printf("watchdog_info:\n");
0228 printf(" identity:\t\t%s\n", info.identity);
0229 printf(" firmware_version:\t%u\n",
0230 info.firmware_version);
0231 printf(" options:\t\t%08x\n", info.options);
0232 break;
0233
0234 default:
0235 usage(argv[0]);
0236 goto end;
0237 }
0238 }
0239
0240 if (oneshot)
0241 goto end;
0242
0243 printf("Watchdog Ticking Away!\n");
0244
0245 signal(SIGINT, term);
0246
0247 while (1) {
0248 keep_alive();
0249 sleep(ping_rate);
0250 }
0251 end:
0252 ret = write(fd, &v, 1);
0253 if (ret < 0)
0254 printf("Stopping watchdog ticks failed (%d)...\n", errno);
0255 close(fd);
0256 return 0;
0257 }