1 /* Copyright (c) 2008, 2009, 2010 Nicira Networks, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
24 #include <sys/ioctl.h>
25 #include <linux/types.h>
26 #include <linux/watchdog.h>
29 /* Default values for the interval and timer. In seconds. */
30 #define DEFAULT_INTERVAL 1
31 #define DEFAULT_TIMEOUT 30
35 /* The WDT is automatically enabled when /dev/watchdog is opened. If we
36 * do not send the magic value to the device first before exiting, the
37 * system will reboot. This function allows the program to exit without
47 /* Writing the magic value "V" to the device is an indication that
48 * the device is about to be closed. This causes the watchdog to be
49 * disabled after the call to close.
51 if (write(fd, "V", 1) != 1) {
52 fprintf(stderr, "Couldn't write magic val: %d\n", errno);
60 /* If we receive a SIGINT, cleanup first, which will disable the
64 sighandler(int signum)
67 signal(signum, SIG_DFL);
74 struct sigaction action;
76 action.sa_handler = sighandler;
77 sigemptyset(&action.sa_mask);
80 if (sigaction(SIGINT, &action, NULL) != 0) {
81 fprintf(stderr, "Problem setting up SIGINT handler...\n");
83 if (sigaction(SIGTERM, &action, NULL) != 0) {
84 fprintf(stderr, "Problem setting up SIGTERM handler...\n");
89 /* Print information on the WDT hardware */
93 struct watchdog_info ident;
95 if (ioctl(fd, WDIOC_GETSUPPORT, &ident) == -1) {
96 fprintf(stderr, "Couldn't get version: %d\n", errno);
100 printf("identity: %s, ver: %d, opt: %#x\n", ident.identity,
101 ident.firmware_version, ident.options);
106 print_help(char *progname)
108 printf("%s: Watchdog timer utility\n", progname);
109 printf("usage: %s [OPTIONS]\n\n", progname);
110 printf("Options:\n");
111 printf(" -t, --timeout=SECS expiration time of WDT (default: %d)\n",
113 printf(" -i, --interval=SECS interval to send keep-alives (default: %d)\n",
115 printf(" -d, --disable disable the WDT and exit\n");
116 printf(" -h, --help display this help message\n");
117 printf(" -v, --verbose enable verbose printing\n");
118 printf(" -V, --version display version information of WDT and exit\n");
122 int main(int argc, char *argv[])
127 int interval = DEFAULT_INTERVAL;
128 int timeout = DEFAULT_TIMEOUT;
129 static struct option const longopts[] =
131 {"timeout", required_argument, NULL, 't'},
132 {"interval", required_argument, NULL, 'i'},
133 {"disable", no_argument, NULL, 'd'},
134 {"help", no_argument, NULL, 'h'},
135 {"verbose", no_argument, NULL, 'v'},
136 {"version", no_argument, NULL, 'V'},
142 fd = open("/dev/watchdog", O_RDWR);
144 fprintf(stderr, "Couldn't open watchdog device: %s\n", strerror(errno));
148 while ((optc = getopt_long(argc, argv, "t:i:dh?vV", longopts, NULL)) != -1) {
151 timeout = strtol(optarg, NULL, 10);
153 fprintf(stderr, "Invalid timeout: %s\n", optarg);
159 interval = strtol(optarg, NULL, 10);
161 fprintf(stderr, "Invalid interval: %s\n", optarg);
167 arg = WDIOS_DISABLECARD;
168 if (ioctl(fd, WDIOC_SETOPTIONS, &arg) == -1) {
169 fprintf(stderr, "Couldn't disable: %d\n", errno);
202 /* Sanity-check the arguments */
204 fprintf(stderr, "Illegal argument: %s\n", argv[0]);
210 printf("timeout: %d, interval: %d\n", timeout, interval);
213 /* Prevent the interval being greater than the timeout, since it
214 * will always cause a reboot.
216 if (interval > timeout) {
217 fprintf(stderr, "Interval greater than timeout: %d > %d\n",
222 /* Always set the timeout */
223 if (ioctl(fd, WDIOC_SETTIMEOUT, &timeout) == -1) {
224 fprintf(stderr, "Couldn't set timeout: %d\n", errno);
228 /* Loop and send a keep-alive every "interval" seconds */
231 if (ioctl(fd, WDIOC_GETTIMELEFT, &arg) == -1) {
232 fprintf(stderr, "Couldn't get time left: %d\n", errno);
235 printf("Sending keep alive, time remaining: %d\n", arg);
238 /* Send a keep-alive. The argument is ignored */
239 if (ioctl(fd, WDIOC_KEEPALIVE, &arg) == -1) {
240 fprintf(stderr, "Couldn't keepalive: %d\n", errno);
247 /* Never directly reached... */