2 * wdctl(8) - show hardware watchdog status
4 * Copyright (C) 2012 Lennart Poettering
5 * Copyright (C) 2012 Karel Zak <kzak@redhat.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <sys/ioctl.h>
27 #include <linux/watchdog.h>
29 #include <libsmartcols.h>
34 #include "closestream.h"
36 #include "pathnames.h"
38 #include "carefulputc.h"
43 #ifndef WDIOC_SETPRETIMEOUT
44 # define WDIOC_SETPRETIMEOUT _IOWR(WATCHDOG_IOCTL_BASE, 8, int)
45 # define WDIOC_GETPRETIMEOUT _IOR(WATCHDOG_IOCTL_BASE, 9, int)
46 # define WDIOC_GETTIMELEFT _IOR(WATCHDOG_IOCTL_BASE, 10, int)
47 # define WDIOF_POWEROVER 0x0040 /* Power over voltage */
48 # define WDIOF_SETTIMEOUT 0x0080 /* Set timeout (in seconds) */
49 # define WDIOF_MAGICCLOSE 0x0100 /* Supports magic close char */
50 # define WDIOF_PRETIMEOUT 0x0200 /* Pretimeout (in seconds), get/set */
51 # define WDIOF_KEEPALIVEPING 0x8000 /* Keep alive ping reply */
57 #ifndef WDIOF_ALARMONLY
58 # define WDIOF_ALARMONLY 0x0400 /* Watchdog triggers a management or
59 other external alarm not a reboot */
62 /* basic output flags */
63 static int no_headings
;
69 const char *description
;
72 static const struct wdflag wdflags
[] = {
73 { WDIOF_CARDRESET
, "CARDRESET", N_("Card previously reset the CPU") },
74 { WDIOF_EXTERN1
, "EXTERN1", N_("External relay 1") },
75 { WDIOF_EXTERN2
, "EXTERN2", N_("External relay 2") },
76 { WDIOF_FANFAULT
, "FANFAULT", N_("Fan failed") },
77 { WDIOF_KEEPALIVEPING
, "KEEPALIVEPING", N_("Keep alive ping reply") },
78 { WDIOF_MAGICCLOSE
, "MAGICCLOSE", N_("Supports magic close char") },
79 { WDIOF_OVERHEAT
, "OVERHEAT", N_("Reset due to CPU overheat") },
80 { WDIOF_POWEROVER
, "POWEROVER", N_("Power over voltage") },
81 { WDIOF_POWERUNDER
, "POWERUNDER", N_("Power bad/power fault") },
82 { WDIOF_PRETIMEOUT
, "PRETIMEOUT", N_("Pretimeout (in seconds)") },
83 { WDIOF_SETTIMEOUT
, "SETTIMEOUT", N_("Set timeout (in seconds)") },
84 { WDIOF_ALARMONLY
, "ALARMONLY", N_("Not trigger reboot") }
90 const char *name
; /* header */
91 double whint
; /* width hint (N < 1 is in percent of termwidth) */
92 int flags
; /* SCOLS_FL_* */
96 enum { COL_FLAG
, COL_DESC
, COL_STATUS
, COL_BSTATUS
, COL_DEVICE
};
98 /* columns descriptions */
99 static struct colinfo infos
[] = {
100 [COL_FLAG
] = { "FLAG", 14, 0, N_("flag name") },
101 [COL_DESC
] = { "DESCRIPTION", 0.1, SCOLS_FL_TRUNC
, N_("flag description") },
102 [COL_STATUS
] = { "STATUS", 1, SCOLS_FL_RIGHT
, N_("flag status") },
103 [COL_BSTATUS
] = { "BOOT-STATUS", 1, SCOLS_FL_RIGHT
, N_("flag boot status") },
104 [COL_DEVICE
] = { "DEVICE", 0.1, 0, N_("watchdog device name") }
108 static int columns
[ARRAY_SIZE(infos
) * 2];
121 struct watchdog_info ident
;
123 unsigned int has_timeout
: 1,
128 /* converts flag name to flag bit */
129 static long name2bit(const char *name
, size_t namesz
)
133 for (i
= 0; i
< ARRAY_SIZE(wdflags
); i
++) {
134 const char *cn
= wdflags
[i
].name
;
135 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
136 return wdflags
[i
].flag
;
138 warnx(_("unknown flag: %s"), name
);
142 static int column2id(const char *name
, size_t namesz
)
146 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
147 const char *cn
= infos
[i
].name
;
148 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
151 warnx(_("unknown column: %s"), name
);
155 static int get_column_id(int num
)
157 assert(num
< ncolumns
);
158 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
163 static struct colinfo
*get_column_info(unsigned num
)
165 return &infos
[ get_column_id(num
) ];
168 static void __attribute__ ((__noreturn__
)) usage(FILE *out
)
172 fputs(USAGE_HEADER
, out
);
174 _(" %s [options] [<device> ...]\n"), program_invocation_short_name
);
176 fputs(USAGE_SEPARATOR
, out
);
177 fputs(_("Show the status of the hardware watchdog.\n"), out
);
179 fputs(USAGE_OPTIONS
, out
);
180 fputs(_(" -f, --flags <list> print selected flags only\n"
181 " -F, --noflags don't print information about flags\n"
182 " -I, --noident don't print watchdog identity information\n"
183 " -n, --noheadings don't print headings for flags table\n"
184 " -O, --oneline print all information on one line\n"
185 " -o, --output <list> output columns of the flags\n"
186 " -r, --raw use raw output format for flags table\n"
187 " -T, --notimeouts don't print watchdog timeouts\n"
188 " -s, --settimeout <sec> set watchdog timeout\n"
189 " -x, --flags-only print only flags table (same as -I -T)\n"), out
);
191 fputs(USAGE_SEPARATOR
, out
);
192 fputs(USAGE_HELP
, out
);
193 fputs(USAGE_VERSION
, out
);
194 fputs(USAGE_SEPARATOR
, out
);
196 fprintf(out
, _("The default device is %s.\n"), _PATH_WATCHDOG_DEV
);
197 fputs(USAGE_SEPARATOR
, out
);
199 fputs(_("Available columns:\n"), out
);
200 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
201 fprintf(out
, " %13s %s\n", infos
[i
].name
, _(infos
[i
].help
));
203 fprintf(out
, USAGE_MAN_TAIL("wdctl(8)"));
205 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
208 static void add_flag_line(struct libscols_table
*table
, struct wdinfo
*wd
, const struct wdflag
*fl
)
211 struct libscols_line
*line
;
213 line
= scols_table_new_line(table
, NULL
);
215 warn(_("failed to initialize output line"));
219 for (i
= 0; i
< ncolumns
; i
++) {
220 const char *str
= NULL
;
222 switch (get_column_id(i
)) {
227 str
= fl
->description
;
230 str
= wd
->status
& fl
->flag
? "1" : "0";
233 str
= wd
->bstatus
& fl
->flag
? "1" : "0";
243 scols_line_set_data(line
, i
, str
);
247 static int show_flags(struct wdinfo
*wd
, uint32_t wanted
)
251 struct libscols_table
*table
;
256 /* create output table */
257 table
= scols_new_table();
259 warn(_("failed to initialize output table"));
262 scols_table_enable_raw(table
, raw
);
263 scols_table_enable_noheadings(table
, no_headings
);
266 for (i
= 0; i
< (size_t) ncolumns
; i
++) {
267 struct colinfo
*col
= get_column_info(i
);
269 if (!scols_table_new_column(table
, col
->name
, col
->whint
, col
->flags
)) {
270 warnx(_("failed to initialize output column"));
275 /* fill-in table with data
276 * -- one line for each supported flag (option) */
277 flags
= wd
->ident
.options
;
279 for (i
= 0; i
< ARRAY_SIZE(wdflags
); i
++) {
280 if (wanted
&& !(wanted
& wdflags
[i
].flag
))
282 else if (flags
& wdflags
[i
].flag
)
283 add_flag_line(table
, wd
, &wdflags
[i
]);
285 flags
&= ~wdflags
[i
].flag
;
289 warnx(_("%s: unknown flags 0x%x\n"), wd
->device
, flags
);
291 scols_print_table(table
);
294 scols_unref_table(table
);
298 * Warning: successfully opened watchdog has to be properly closed with magic
299 * close character otherwise the machine will be rebooted!
301 * Don't use err() or exit() here!
303 static int set_watchdog(struct wdinfo
*wd
, int timeout
)
306 sigset_t sigs
, oldsigs
;
311 sigemptyset(&oldsigs
);
313 sigprocmask(SIG_BLOCK
, &sigs
, &oldsigs
);
315 fd
= open(wd
->device
, O_WRONLY
|O_CLOEXEC
);
319 warnx(_("%s: watchdog already in use, terminating."),
321 warn(_("cannot open %s"), wd
->device
);
326 /* We just opened this to query the state, not to arm
327 * it hence use the magic close character */
328 static const char v
= 'V';
330 if (write(fd
, &v
, 1) >= 0)
332 if (errno
!= EINTR
) {
333 warn(_("%s: failed to disarm watchdog"), wd
->device
);
336 /* Let's try hard, since if we don't get this right
337 * the machine might end up rebooting. */
340 if (ioctl(fd
, WDIOC_SETTIMEOUT
, &timeout
) != 0) {
342 warn(_("cannot set timeout for %s"), wd
->device
);
346 warn(_("write failed"));
347 sigprocmask(SIG_SETMASK
, &oldsigs
, NULL
);
348 printf(P_("Timeout has been set to %d second.\n",
349 "Timeout has been set to %d seconds.\n", timeout
), timeout
);
355 * Warning: successfully opened watchdog has to be properly closed with magic
356 * close character otherwise the machine will be rebooted!
358 * Don't use err() or exit() here!
360 static int read_watchdog(struct wdinfo
*wd
)
363 sigset_t sigs
, oldsigs
;
367 sigemptyset(&oldsigs
);
369 sigprocmask(SIG_BLOCK
, &sigs
, &oldsigs
);
371 fd
= open(wd
->device
, O_WRONLY
|O_CLOEXEC
);
375 warnx(_("%s: watchdog already in use, terminating."),
377 warn(_("cannot open %s"), wd
->device
);
381 if (ioctl(fd
, WDIOC_GETSUPPORT
, &wd
->ident
) < 0)
382 warn(_("%s: failed to get information about watchdog"), wd
->device
);
384 ioctl(fd
, WDIOC_GETSTATUS
, &wd
->status
);
385 ioctl(fd
, WDIOC_GETBOOTSTATUS
, &wd
->bstatus
);
387 if (ioctl(fd
, WDIOC_GETTIMEOUT
, &wd
->timeout
) >= 0)
389 if (ioctl(fd
, WDIOC_GETPRETIMEOUT
, &wd
->pretimeout
) >= 0)
390 wd
->has_pretimeout
= 1;
391 if (ioctl(fd
, WDIOC_GETTIMELEFT
, &wd
->timeleft
) >= 0)
392 wd
->has_timeleft
= 1;
396 /* We just opened this to query the state, not to arm
397 * it hence use the magic close character */
398 static const char v
= 'V';
400 if (write(fd
, &v
, 1) >= 0)
402 if (errno
!= EINTR
) {
403 warn(_("%s: failed to disarm watchdog"), wd
->device
);
406 /* Let's try hard, since if we don't get this right
407 * the machine might end up rebooting. */
411 warn(_("write failed"));
412 sigprocmask(SIG_SETMASK
, &oldsigs
, NULL
);
417 static void print_oneline(struct wdinfo
*wd
, uint32_t wanted
,
418 int noident
, int notimeouts
, int noflags
)
420 printf("%s:", wd
->device
);
423 printf(" VERSION=\"%x\"", wd
->ident
.firmware_version
);
425 printf(" IDENTITY=");
426 fputs_quoted((char *) wd
->ident
.identity
, stdout
);
430 printf(" TIMEOUT=\"%i\"", wd
->timeout
);
431 if (wd
->has_pretimeout
)
432 printf(" PRETIMEOUT=\"%i\"", wd
->pretimeout
);
433 if (wd
->has_timeleft
)
434 printf(" TIMELEFT=\"%i\"", wd
->timeleft
);
439 uint32_t flags
= wd
->ident
.options
;
441 for (i
= 0; i
< ARRAY_SIZE(wdflags
); i
++) {
442 const struct wdflag
*fl
;
444 if ((wanted
&& !(wanted
& wdflags
[i
].flag
)) ||
445 !(flags
& wdflags
[i
].flag
))
450 printf(" %s=\"%s\"", fl
->name
,
451 wd
->status
& fl
->flag
? "1" : "0");
452 printf(" %s_BOOT=\"%s\"", fl
->name
,
453 wd
->bstatus
& fl
->flag
? "1" : "0");
461 static void show_timeouts(struct wdinfo
*wd
)
464 printf(P_("%-14s %2i second\n", "%-14s %2i seconds\n", wd
->timeout
),
465 _("Timeout:"), wd
->timeout
);
466 if (wd
->has_pretimeout
)
467 printf(P_("%-14s %2i second\n", "%-14s %2i seconds\n", wd
->pretimeout
),
468 _("Pre-timeout:"), wd
->pretimeout
);
469 if (wd
->has_timeleft
)
470 printf(P_("%-14s %2i second\n", "%-14s %2i seconds\n", wd
->timeleft
),
471 _("Timeleft:"), wd
->timeleft
);
474 int main(int argc
, char *argv
[])
477 int c
, res
= EXIT_SUCCESS
, count
= 0;
478 char noflags
= 0, noident
= 0, notimeouts
= 0, oneline
= 0;
482 static const struct option long_opts
[] = {
483 { "flags", required_argument
, NULL
, 'f' },
484 { "flags-only", no_argument
, NULL
, 'x' },
485 { "help", no_argument
, NULL
, 'h' },
486 { "noflags", no_argument
, NULL
, 'F' },
487 { "noheadings", no_argument
, NULL
, 'n' },
488 { "noident", no_argument
, NULL
, 'I' },
489 { "notimeouts", no_argument
, NULL
, 'T' },
490 { "settimeout", required_argument
, NULL
, 's' },
491 { "output", required_argument
, NULL
, 'o' },
492 { "oneline", no_argument
, NULL
, 'O' },
493 { "raw", no_argument
, NULL
, 'r' },
494 { "version", no_argument
, NULL
, 'V' },
498 static const ul_excl_t excl
[] = { /* rows and cols in in ASCII order */
499 { 'F','f' }, /* noflags,flags*/
502 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
504 setlocale(LC_ALL
, "");
505 bindtextdomain(PACKAGE
, LOCALEDIR
);
507 atexit(close_stdout
);
509 while ((c
= getopt_long(argc
, argv
,
510 "d:f:hFnITo:s:OrVx", long_opts
, NULL
)) != -1) {
512 err_exclusive_options(c
, long_opts
, excl
, excl_st
);
516 ncolumns
= string_to_idarray(optarg
,
517 columns
, ARRAY_SIZE(columns
),
523 timeout
= strtos32_or_err(optarg
, _("invalid timeout argument"));
526 if (string_to_bitmask(optarg
, (unsigned long *) &wanted
, name2bit
) != 0)
530 printf(UTIL_LINUX_VERSION
);
557 errtryhelp(EXIT_FAILURE
);
562 /* default columns */
563 columns
[ncolumns
++] = COL_FLAG
;
564 columns
[ncolumns
++] = COL_DESC
;
565 columns
[ncolumns
++] = COL_STATUS
;
566 columns
[ncolumns
++] = COL_BSTATUS
;
572 memset(&wd
, 0, sizeof(wd
));
575 wd
.device
= _PATH_WATCHDOG_DEV
;
577 wd
.device
= argv
[optind
++];
584 rc
= set_watchdog(&wd
, timeout
);
590 rc
= read_watchdog(&wd
);
597 print_oneline(&wd
, wanted
, noident
, notimeouts
, noflags
);
603 printf("%-15s%s\n", _("Device:"), wd
.device
);
604 printf("%-15s%s [%s %x]\n",
608 wd
.ident
.firmware_version
);
613 show_flags(&wd
, wanted
);
614 } while (optind
< argc
);