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>
26 #include <linux/watchdog.h>
27 #include <sys/types.h>
31 #include <libsmartcols.h>
36 #include "closestream.h"
38 #include "pathnames.h"
40 #include "carefulputc.h"
46 #ifndef WDIOC_SETPRETIMEOUT
47 # define WDIOC_SETPRETIMEOUT _IOWR(WATCHDOG_IOCTL_BASE, 8, int)
48 # define WDIOC_GETPRETIMEOUT _IOR(WATCHDOG_IOCTL_BASE, 9, int)
49 # define WDIOC_GETTIMELEFT _IOR(WATCHDOG_IOCTL_BASE, 10, int)
50 # define WDIOF_POWEROVER 0x0040 /* Power over voltage */
51 # define WDIOF_SETTIMEOUT 0x0080 /* Set timeout (in seconds) */
52 # define WDIOF_MAGICCLOSE 0x0100 /* Supports magic close char */
53 # define WDIOF_PRETIMEOUT 0x0200 /* Pretimeout (in seconds), get/set */
54 # define WDIOF_KEEPALIVEPING 0x8000 /* Keep alive ping reply */
60 #ifndef WDIOF_ALARMONLY
61 # define WDIOF_ALARMONLY 0x0400 /* Watchdog triggers a management or
62 other external alarm not a reboot */
68 const char *description
;
71 static const struct wdflag wdflags
[] = {
72 { WDIOF_CARDRESET
, "CARDRESET", N_("Card previously reset the CPU") },
73 { WDIOF_EXTERN1
, "EXTERN1", N_("External relay 1") },
74 { WDIOF_EXTERN2
, "EXTERN2", N_("External relay 2") },
75 { WDIOF_FANFAULT
, "FANFAULT", N_("Fan failed") },
76 { WDIOF_KEEPALIVEPING
, "KEEPALIVEPING", N_("Keep alive ping reply") },
77 { WDIOF_MAGICCLOSE
, "MAGICCLOSE", N_("Supports magic close char") },
78 { WDIOF_OVERHEAT
, "OVERHEAT", N_("Reset due to CPU overheat") },
79 { WDIOF_POWEROVER
, "POWEROVER", N_("Power over voltage") },
80 { WDIOF_POWERUNDER
, "POWERUNDER", N_("Power bad/power fault") },
81 { WDIOF_PRETIMEOUT
, "PRETIMEOUT", N_("Pretimeout (in seconds)") },
82 { WDIOF_SETTIMEOUT
, "SETTIMEOUT", N_("Set timeout (in seconds)") },
83 { WDIOF_ALARMONLY
, "ALARMONLY", N_("Not trigger reboot") }
89 const char *name
; /* header */
90 double whint
; /* width hint (N < 1 is in percent of termwidth) */
91 int flags
; /* SCOLS_FL_* */
95 enum { COL_FLAG
, COL_DESC
, COL_STATUS
, COL_BSTATUS
, COL_DEVICE
};
97 /* columns descriptions */
98 static struct colinfo infos
[] = {
99 [COL_FLAG
] = { "FLAG", 14, 0, N_("flag name") },
100 [COL_DESC
] = { "DESCRIPTION", 0.1, SCOLS_FL_TRUNC
, N_("flag description") },
101 [COL_STATUS
] = { "STATUS", 1, SCOLS_FL_RIGHT
, N_("flag status") },
102 [COL_BSTATUS
] = { "BOOT-STATUS", 1, SCOLS_FL_RIGHT
, N_("flag boot status") },
103 [COL_DEVICE
] = { "DEVICE", 0.1, 0, N_("watchdog device name") }
107 static int columns
[ARRAY_SIZE(infos
) * 2];
121 struct watchdog_info ident
;
123 unsigned int has_timeout
: 1,
130 unsigned int show_oneline
: 1,
138 /* converts flag name to flag bit */
139 static long name2bit(const char *name
, size_t namesz
)
143 for (i
= 0; i
< ARRAY_SIZE(wdflags
); i
++) {
144 const char *cn
= wdflags
[i
].name
;
145 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
146 return wdflags
[i
].flag
;
148 warnx(_("unknown flag: %s"), name
);
152 static int column2id(const char *name
, size_t namesz
)
156 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
157 const char *cn
= infos
[i
].name
;
158 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
161 warnx(_("unknown column: %s"), name
);
165 static int get_column_id(int num
)
167 assert(num
< ncolumns
);
168 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
173 static struct colinfo
*get_column_info(unsigned num
)
175 return &infos
[ get_column_id(num
) ];
178 /* We preffer cdev /dev/watchdog0 as this device has node in
179 * /sys/class/watchdog/. The old miscdev /dev/watchdog is fallback for old
182 static const char *get_default_device(void)
185 static const char *devs
[] = {
191 for (p
= devs
; *p
; p
++) {
192 if (access(*p
, F_OK
) == 0)
199 static void __attribute__((__noreturn__
)) usage(void)
203 const char *dflt
= get_default_device();
205 fputs(USAGE_HEADER
, out
);
207 _(" %s [options] [<device> ...]\n"), program_invocation_short_name
);
209 fputs(USAGE_SEPARATOR
, out
);
210 fputs(_("Show the status of the hardware watchdog.\n"), out
);
212 fputs(USAGE_OPTIONS
, out
);
213 fputs(_(" -f, --flags <list> print selected flags only\n"
214 " -F, --noflags don't print information about flags\n"
215 " -I, --noident don't print watchdog identity information\n"
216 " -n, --noheadings don't print headings for flags table\n"
217 " -O, --oneline print all information on one line\n"
218 " -o, --output <list> output columns of the flags\n"
219 " -r, --raw use raw output format for flags table\n"
220 " -T, --notimeouts don't print watchdog timeouts\n"
221 " -s, --settimeout <sec> set watchdog timeout\n"
222 " -x, --flags-only print only flags table (same as -I -T)\n"), out
);
224 fputs(USAGE_SEPARATOR
, out
);
225 printf(USAGE_HELP_OPTIONS(24));
226 fputs(USAGE_SEPARATOR
, out
);
229 fprintf(out
, _("The default device is %s.\n"), dflt
);
231 fprintf(out
, _("No default device is available.\n"));
233 fputs(USAGE_COLUMNS
, out
);
234 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
235 fprintf(out
, " %13s %s\n", infos
[i
].name
, _(infos
[i
].help
));
237 printf(USAGE_MAN_TAIL("wdctl(8)"));
242 static void add_flag_line(struct libscols_table
*table
, struct wd_device
*wd
, const struct wdflag
*fl
)
245 struct libscols_line
*line
;
247 line
= scols_table_new_line(table
, NULL
);
249 warn(_("failed to allocate output line"));
253 for (i
= 0; i
< ncolumns
; i
++) {
254 const char *str
= NULL
;
256 switch (get_column_id(i
)) {
261 str
= fl
->description
;
264 str
= wd
->status
& fl
->flag
? "1" : "0";
267 str
= wd
->bstatus
& fl
->flag
? "1" : "0";
276 if (str
&& scols_line_set_data(line
, i
, str
)) {
277 warn(_("failed to add output data"));
283 static int show_flags(struct wd_control
*ctl
, struct wd_device
*wd
, uint32_t wanted
)
287 struct libscols_table
*table
;
290 /* information about supported bits is probably missing in /sys */
291 if (!wd
->ident
.options
)
296 /* create output table */
297 table
= scols_new_table();
299 warn(_("failed to allocate output table"));
302 scols_table_enable_raw(table
, ctl
->show_raw
);
303 scols_table_enable_noheadings(table
, ctl
->hide_headings
);
306 for (i
= 0; i
< (size_t) ncolumns
; i
++) {
307 struct colinfo
*col
= get_column_info(i
);
309 if (!scols_table_new_column(table
, col
->name
, col
->whint
, col
->flags
)) {
310 warnx(_("failed to allocate output column"));
315 /* fill-in table with data
316 * -- one line for each supported flag (option) */
317 flags
= wd
->ident
.options
;
319 for (i
= 0; i
< ARRAY_SIZE(wdflags
); i
++) {
320 if (wanted
&& !(wanted
& wdflags
[i
].flag
))
322 else if (flags
& wdflags
[i
].flag
)
323 add_flag_line(table
, wd
, &wdflags
[i
]);
325 flags
&= ~wdflags
[i
].flag
;
329 warnx(_("%s: unknown flags 0x%x\n"), wd
->devpath
, flags
);
331 scols_print_table(table
);
334 scols_unref_table(table
);
338 * Warning: successfully opened watchdog has to be properly closed with magic
339 * close character otherwise the machine will be rebooted!
341 * Don't use err() or exit() here!
343 static int set_watchdog(struct wd_device
*wd
, int timeout
)
346 sigset_t sigs
, oldsigs
;
351 sigemptyset(&oldsigs
);
353 sigprocmask(SIG_BLOCK
, &sigs
, &oldsigs
);
355 fd
= open(wd
->devpath
, O_WRONLY
|O_CLOEXEC
);
359 warnx(_("%s: watchdog already in use, terminating."),
361 warn(_("cannot open %s"), wd
->devpath
);
366 /* We just opened this to query the state, not to arm
367 * it hence use the magic close character */
368 static const char v
= 'V';
370 if (write(fd
, &v
, 1) >= 0)
372 if (errno
!= EINTR
) {
373 warn(_("%s: failed to disarm watchdog"), wd
->devpath
);
376 /* Let's try hard, since if we don't get this right
377 * the machine might end up rebooting. */
380 if (ioctl(fd
, WDIOC_SETTIMEOUT
, &timeout
) != 0) {
382 warn(_("cannot set timeout for %s"), wd
->devpath
);
386 warn(_("write failed"));
387 sigprocmask(SIG_SETMASK
, &oldsigs
, NULL
);
388 printf(P_("Timeout has been set to %d second.\n",
389 "Timeout has been set to %d seconds.\n", timeout
), timeout
);
395 * Warning: successfully opened watchdog has to be properly closed with magic
396 * close character otherwise the machine will be rebooted!
398 * Don't use err() or exit() here!
400 static int read_watchdog_from_device(struct wd_device
*wd
)
403 sigset_t sigs
, oldsigs
;
407 sigemptyset(&oldsigs
);
409 sigprocmask(SIG_BLOCK
, &sigs
, &oldsigs
);
411 fd
= open(wd
->devpath
, O_WRONLY
|O_CLOEXEC
);
416 if (ioctl(fd
, WDIOC_GETSUPPORT
, &wd
->ident
) < 0)
417 warn(_("%s: failed to get information about watchdog"), wd
->devpath
);
419 ioctl(fd
, WDIOC_GETSTATUS
, &wd
->status
);
420 ioctl(fd
, WDIOC_GETBOOTSTATUS
, &wd
->bstatus
);
422 if (ioctl(fd
, WDIOC_GETTIMEOUT
, &wd
->timeout
) >= 0)
424 if (ioctl(fd
, WDIOC_GETPRETIMEOUT
, &wd
->pretimeout
) >= 0)
425 wd
->has_pretimeout
= 1;
426 if (ioctl(fd
, WDIOC_GETTIMELEFT
, &wd
->timeleft
) >= 0)
427 wd
->has_timeleft
= 1;
431 /* We just opened this to query the state, not to arm
432 * it hence use the magic close character */
433 static const char v
= 'V';
435 if (write(fd
, &v
, 1) >= 0)
437 if (errno
!= EINTR
) {
438 warn(_("%s: failed to disarm watchdog"), wd
->devpath
);
441 /* Let's try hard, since if we don't get this right
442 * the machine might end up rebooting. */
446 warn(_("write failed"));
447 sigprocmask(SIG_SETMASK
, &oldsigs
, NULL
);
452 /* Returns: <0 error, 0 success, 1 unssuported */
453 static int read_watchdog_from_sysfs(struct wd_device
*wd
)
455 struct path_cxt
*sys
;
459 rc
= stat(wd
->devpath
, &st
);
463 sys
= ul_new_path(_PATH_SYS_DEVCHAR
"/%u:%u",
464 major(st
.st_rdev
), minor(st
.st_rdev
));
468 if (ul_path_get_dirfd(sys
) < 0)
469 goto nosysfs
; /* device not in /sys */
471 if (ul_path_access(sys
, F_OK
, "identity") != 0)
472 goto nosysfs
; /* no info in /sys (old miscdev?) */
474 ul_path_read_buffer(sys
, (char *) wd
->ident
.identity
, sizeof(wd
->ident
.identity
), "identity");
476 ul_path_scanf(sys
, "status", "%x", &wd
->status
);
477 ul_path_read_u32(sys
, &wd
->bstatus
, "bootstatus");
479 if (ul_path_read_s32(sys
, &wd
->nowayout
, "nowayout") == 0)
480 wd
->has_nowayout
= 1;
481 if (ul_path_read_s32(sys
, &wd
->timeout
, "timeout") == 0)
483 if (ul_path_read_s32(sys
, &wd
->pretimeout
, "pretimeout") == 0)
484 wd
->has_pretimeout
= 1;
485 if (ul_path_read_s32(sys
, &wd
->timeleft
, "timeleft") == 0)
486 wd
->has_timeleft
= 1;
495 static int read_watchdog(struct wd_device
*wd
)
497 int rc
= read_watchdog_from_device(wd
);
499 if (rc
== -EBUSY
|| rc
== -EACCES
|| rc
== -EPERM
)
500 rc
= read_watchdog_from_sysfs(wd
);
503 warn(_("cannot read information about %s"), wd
->devpath
);
510 static void show_timeouts(struct wd_device
*wd
)
513 printf(P_("%-14s %2i second\n", "%-14s %2i seconds\n", wd
->timeout
),
514 _("Timeout:"), wd
->timeout
);
515 if (wd
->has_pretimeout
)
516 printf(P_("%-14s %2i second\n", "%-14s %2i seconds\n", wd
->pretimeout
),
517 _("Pre-timeout:"), wd
->pretimeout
);
518 if (wd
->has_timeleft
)
519 printf(P_("%-14s %2i second\n", "%-14s %2i seconds\n", wd
->timeleft
),
520 _("Timeleft:"), wd
->timeleft
);
523 static void print_oneline(struct wd_control
*ctl
, struct wd_device
*wd
, uint32_t wanted
)
525 printf("%s:", wd
->devpath
);
527 if (!ctl
->hide_ident
) {
528 printf(" VERSION=\"%x\"", wd
->ident
.firmware_version
);
530 printf(" IDENTITY=");
531 fputs_quoted((char *) wd
->ident
.identity
, stdout
);
533 if (!ctl
->hide_timeouts
) {
535 printf(" TIMEOUT=\"%i\"", wd
->timeout
);
536 if (wd
->has_pretimeout
)
537 printf(" PRETIMEOUT=\"%i\"", wd
->pretimeout
);
538 if (wd
->has_timeleft
)
539 printf(" TIMELEFT=\"%i\"", wd
->timeleft
);
542 if (!ctl
->hide_flags
) {
544 uint32_t flags
= wd
->ident
.options
;
546 for (i
= 0; i
< ARRAY_SIZE(wdflags
); i
++) {
547 const struct wdflag
*fl
;
549 if ((wanted
&& !(wanted
& wdflags
[i
].flag
)) ||
550 !(flags
& wdflags
[i
].flag
))
555 printf(" %s=\"%s\"", fl
->name
,
556 wd
->status
& fl
->flag
? "1" : "0");
557 printf(" %s_BOOT=\"%s\"", fl
->name
,
558 wd
->bstatus
& fl
->flag
? "1" : "0");
566 static void print_device(struct wd_control
*ctl
, struct wd_device
*wd
, uint32_t wanted
)
568 /* NAME=value one line output */
569 if (ctl
->show_oneline
) {
570 print_oneline(ctl
, wd
, wanted
);
575 if (!ctl
->hide_ident
) {
576 printf("%-15s%s\n", _("Device:"), wd
->devpath
);
577 printf("%-15s%s [%s %x]\n",
581 wd
->ident
.firmware_version
);
583 if (!ctl
->hide_timeouts
)
586 if (!ctl
->hide_flags
)
587 show_flags(ctl
, wd
, wanted
);
590 int main(int argc
, char *argv
[])
593 struct wd_control ctl
= { .hide_headings
= 0 };
594 int c
, res
= EXIT_SUCCESS
, count
= 0;
597 const char *dflt_device
= NULL
;
599 static const struct option long_opts
[] = {
600 { "flags", required_argument
, NULL
, 'f' },
601 { "flags-only", no_argument
, NULL
, 'x' },
602 { "help", no_argument
, NULL
, 'h' },
603 { "noflags", no_argument
, NULL
, 'F' },
604 { "noheadings", no_argument
, NULL
, 'n' },
605 { "noident", no_argument
, NULL
, 'I' },
606 { "notimeouts", no_argument
, NULL
, 'T' },
607 { "settimeout", required_argument
, NULL
, 's' },
608 { "output", required_argument
, NULL
, 'o' },
609 { "oneline", no_argument
, NULL
, 'O' },
610 { "raw", no_argument
, NULL
, 'r' },
611 { "version", no_argument
, NULL
, 'V' },
615 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
616 { 'F','f' }, /* noflags,flags*/
619 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
621 setlocale(LC_ALL
, "");
622 bindtextdomain(PACKAGE
, LOCALEDIR
);
624 close_stdout_atexit();
626 while ((c
= getopt_long(argc
, argv
,
627 "d:f:hFnITo:s:OrVx", long_opts
, NULL
)) != -1) {
629 err_exclusive_options(c
, long_opts
, excl
, excl_st
);
633 ncolumns
= string_to_idarray(optarg
,
634 columns
, ARRAY_SIZE(columns
),
640 timeout
= strtos32_or_err(optarg
, _("invalid timeout argument"));
643 if (string_to_bitmask(optarg
, (unsigned long *) &wanted
, name2bit
) != 0)
653 ctl
.hide_timeouts
= 1;
656 ctl
.hide_headings
= 1;
662 ctl
.show_oneline
= 1;
666 ctl
.hide_timeouts
= 1;
672 print_version(EXIT_SUCCESS
);
674 errtryhelp(EXIT_FAILURE
);
679 /* default columns */
680 columns
[ncolumns
++] = COL_FLAG
;
681 columns
[ncolumns
++] = COL_DESC
;
682 columns
[ncolumns
++] = COL_STATUS
;
683 columns
[ncolumns
++] = COL_BSTATUS
;
686 /* Device no specified, use default. */
687 if (optind
== argc
) {
688 dflt_device
= get_default_device();
690 err(EXIT_FAILURE
, _("No default device is available."));
696 memset(&wd
, 0, sizeof(wd
));
697 wd
.devpath
= dflt_device
? dflt_device
: argv
[optind
++];
704 rc
= set_watchdog(&wd
, timeout
);
710 rc
= read_watchdog(&wd
);
716 print_device(&ctl
, &wd
, wanted
);
718 } while (optind
< argc
);