1 /* Copyright (c) 2008, 2009 Nicira Networks, Inc.
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 * In addition, as a special exception, Nicira Networks gives permission
17 * to link the code of its release of vswitchd with the OpenSSL project's
18 * "OpenSSL" library (or with modified versions of it that use the same
19 * license as the "OpenSSL" library), and distribute the linked
20 * executables. You must obey the GNU General Public License in all
21 * respects for all of the code used other than "OpenSSL". If you modify
22 * this file, you may extend this exception to your version of the file,
23 * but you are not obligated to do so. If you do not wish to do so,
24 * delete this exception statement from your version.
36 #include <sys/ioctl.h>
37 #include <linux/types.h>
38 #include <linux/watchdog.h>
41 /* Default values for the interval and timer. In seconds. */
42 #define DEFAULT_INTERVAL 1
43 #define DEFAULT_TIMEOUT 30
47 /* The WDT is automatically enabled when /dev/watchdog is opened. If we
48 * do not send the magic value to the device first before exiting, the
49 * system will reboot. This function allows the program to exit without
59 /* Writing the magic value "V" to the device is an indication that
60 * the device is about to be closed. This causes the watchdog to be
61 * disabled after the call to close.
63 if (write(fd, "V", 1) != 1) {
64 fprintf(stderr, "Couldn't write magic val: %d\n", errno);
72 /* If we receive a SIGINT, cleanup first, which will disable the
76 sighandler(int signum)
79 signal(signum, SIG_DFL);
86 struct sigaction action;
88 action.sa_handler = sighandler;
89 sigemptyset(&action.sa_mask);
92 if (sigaction(SIGINT, &action, NULL) != 0) {
93 fprintf(stderr, "Problem setting up SIGINT handler...\n");
95 if (sigaction(SIGTERM, &action, NULL) != 0) {
96 fprintf(stderr, "Problem setting up SIGTERM handler...\n");
101 /* Print information on the WDT hardware */
105 struct watchdog_info ident;
107 if (ioctl(fd, WDIOC_GETSUPPORT, &ident) == -1) {
108 fprintf(stderr, "Couldn't get version: %d\n", errno);
112 printf("identity: %s, ver: %d, opt: %#x\n", ident.identity,
113 ident.firmware_version, ident.options);
118 print_help(char *progname)
120 printf("%s: Watchdog timer utility\n", progname);
121 printf("usage: %s [OPTIONS]\n\n", progname);
122 printf("Options:\n");
123 printf(" -t, --timeout=SECS expiration time of WDT (default: %d)\n",
125 printf(" -i, --interval=SECS interval to send keep-alives (default: %d)\n",
127 printf(" -d, --disable disable the WDT and exit\n");
128 printf(" -h, --help display this help message\n");
129 printf(" -v, --verbose enable verbose printing\n");
130 printf(" -V, --version display version information of WDT and exit\n");
134 int main(int argc, char *argv[])
139 int interval = DEFAULT_INTERVAL;
140 int timeout = DEFAULT_TIMEOUT;
141 static struct option const longopts[] =
143 {"timeout", required_argument, NULL, 't'},
144 {"interval", required_argument, NULL, 'i'},
145 {"disable", no_argument, NULL, 'd'},
146 {"help", no_argument, NULL, 'h'},
147 {"verbose", no_argument, NULL, 'v'},
148 {"version", no_argument, NULL, 'V'},
154 fd = open("/dev/watchdog", O_RDWR);
156 fprintf(stderr, "Couldn't open watchdog device: %s\n", strerror(errno));
160 while ((optc = getopt_long(argc, argv, "t:i:dh?vV", longopts, NULL)) != -1) {
163 timeout = strtol(optarg, NULL, 10);
165 fprintf(stderr, "Invalid timeout: %s\n", optarg);
171 interval = strtol(optarg, NULL, 10);
173 fprintf(stderr, "Invalid interval: %s\n", optarg);
179 arg = WDIOS_DISABLECARD;
180 if (ioctl(fd, WDIOC_SETOPTIONS, &arg) == -1) {
181 fprintf(stderr, "Couldn't disable: %d\n", errno);
214 /* Sanity-check the arguments */
216 fprintf(stderr, "Illegal argument: %s\n", argv[0]);
222 printf("timeout: %d, interval: %d\n", timeout, interval);
225 /* Prevent the interval being greater than the timeout, since it
226 * will always cause a reboot.
228 if (interval > timeout) {
229 fprintf(stderr, "Interval greater than timeout: %d > %d\n",
234 /* Always set the timeout */
235 if (ioctl(fd, WDIOC_SETTIMEOUT, &timeout) == -1) {
236 fprintf(stderr, "Couldn't set timeout: %d\n", errno);
240 /* Loop and send a keep-alive every "interval" seconds */
243 if (ioctl(fd, WDIOC_GETTIMELEFT, &arg) == -1) {
244 fprintf(stderr, "Couldn't get time left: %d\n", errno);
247 printf("Sending keep alive, time remaining: %d\n", arg);
250 /* Send a keep-alive. The argument is ignored */
251 if (ioctl(fd, WDIOC_KEEPALIVE, &arg) == -1) {
252 fprintf(stderr, "Couldn't keepalive: %d\n", errno);
259 /* Never directly reached... */