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 <linux/watchdog.h>
22 #include <sys/ioctl.h>
31 #include "closestream.h"
32 #include "pathnames.h"
39 const char *description
;
42 static const struct wdflag wdflags
[] = {
43 { WDIOF_CARDRESET
, "CARDRESET", N_("Card previously reset the CPU") },
44 { WDIOF_EXTERN1
, "EXTERN1", N_("External relay 1") },
45 { WDIOF_EXTERN2
, "EXTERN2", N_("External relay 2") },
46 { WDIOF_FANFAULT
, "FANFAULT", N_("Fan failed") },
47 { WDIOF_KEEPALIVEPING
, "KEEPALIVEPING", N_("Keep alive ping reply") },
48 { WDIOF_MAGICCLOSE
, "MAGICCLOSE", N_("Supports magic close char") },
49 { WDIOF_OVERHEAT
, "OVERHEAT", N_("Reset due to CPU overheat") },
50 { WDIOF_POWEROVER
, "POWEROVER", N_("Power over voltage") },
51 { WDIOF_POWERUNDER
, "POWERUNDER", N_("Power bad/power fault") },
52 { WDIOF_PRETIMEOUT
, "PRETIMEOUT", N_("Pretimeout (in seconds)") },
53 { WDIOF_SETTIMEOUT
, "SETTIMEOUT", N_("Set timeout (in seconds)") }
59 const char *name
; /* header */
60 double whint
; /* width hint (N < 1 is in percent of termwidth) */
61 int flags
; /* TT_FL_* */
65 enum { COL_FLAG
, COL_DESC
, COL_STATUS
, COL_BSTATUS
, COL_DEVICE
};
67 /* columns descriptions */
68 static struct colinfo infos
[] = {
69 [COL_FLAG
] = { "FLAG", 14, 0, N_("flag name") },
70 [COL_DESC
] = { "DESCRIPTION", 0.1, TT_FL_TRUNC
, N_("flag description") },
71 [COL_STATUS
] = { "STATUS", 1, TT_FL_RIGHT
, N_("flag status") },
72 [COL_BSTATUS
] = { "BOOT-STATUS", 1, TT_FL_RIGHT
, N_("flag boot status") },
73 [COL_DEVICE
] = { "DEVICE", 0.1, 0, N_("watchdog device name") }
77 #define NCOLS ARRAY_SIZE(infos)
78 static int columns
[NCOLS
], ncolumns
;
90 struct watchdog_info ident
;
92 unsigned int has_timeout
: 1,
97 /* converts flag name to flag bit */
98 static long name2bit(const char *name
, size_t namesz
)
102 for (i
= 0; i
< ARRAY_SIZE(wdflags
); i
++) {
103 const char *cn
= wdflags
[i
].name
;
104 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
105 return wdflags
[i
].flag
;
107 warnx(_("unknown flag: %s"), name
);
111 static int column2id(const char *name
, size_t namesz
)
115 for (i
= 0; i
< NCOLS
; i
++) {
116 const char *cn
= infos
[i
].name
;
117 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
120 warnx(_("unknown column: %s"), name
);
124 static int get_column_id(int num
)
126 assert(ARRAY_SIZE(columns
) == NCOLS
);
127 assert(num
< ncolumns
);
128 assert(columns
[num
] < (int) NCOLS
);
133 static struct colinfo
*get_column_info(unsigned num
)
135 return &infos
[ get_column_id(num
) ];
138 static void usage(FILE *out
)
142 fputs(USAGE_HEADER
, out
);
144 _(" %s [options] [<device> ...]\n"), program_invocation_short_name
);
146 fputs(USAGE_OPTIONS
, out
);
148 fputs(_(" -f, --flags <list> print selected flags only\n"
149 " -F, --noflags don't print information about flags\n"
150 " -I, --noident don't print watchdog identity information\n"
151 " -n, --noheadings don't print headings for flags table\n"
152 " -O, --oneline print all information on one line\n"
153 " -o, --output <list> output columns of the flags\n"
154 " -r, --raw use raw output format for flags table\n"
155 " -T, --notimeouts don't print watchdog timeouts\n"
156 " -x, --flags-only print only flags table (same as -I -T)\n"), out
);
158 fputs(USAGE_SEPARATOR
, out
);
159 fputs(USAGE_HELP
, out
);
160 fputs(USAGE_VERSION
, out
);
161 fputs(USAGE_SEPARATOR
, out
);
163 fprintf(out
, _("The default device is %s.\n"), _PATH_WATCHDOG_DEV
);
164 fputs(USAGE_SEPARATOR
, out
);
166 fputs(_("Available columns:\n"), out
);
167 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
168 fprintf(out
, " %13s %s\n", infos
[i
].name
, _(infos
[i
].help
));
170 fprintf(out
, USAGE_MAN_TAIL("wdctl(1)"));
172 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
175 static void add_flag_line(struct tt
*tt
, struct wdinfo
*wd
, const struct wdflag
*fl
)
178 struct tt_line
*line
;
180 line
= tt_add_line(tt
, NULL
);
182 warn(_("failed to add line to output"));
186 for (i
= 0; i
< ncolumns
; i
++) {
187 const char *str
= NULL
;
189 switch (get_column_id(i
)) {
194 str
= fl
->description
;
197 str
= wd
->status
& fl
->flag
? "1" : "0";
200 str
= wd
->bstatus
& fl
->flag
? "1" : "0";
210 tt_line_set_data(line
, i
, str
);
214 static int show_flags(struct wdinfo
*wd
, int tt_flags
, uint32_t wanted
)
221 /* create output table */
222 tt
= tt_new_table(tt_flags
);
224 warn(_("failed to initialize output table"));
229 for (i
= 0; i
< (size_t) ncolumns
; i
++) {
230 struct colinfo
*col
= get_column_info(i
);
232 if (!tt_define_column(tt
, col
->name
, col
->whint
, col
->flags
)) {
233 warnx(_("failed to initialize output column"));
238 /* fill-in table with data
239 * -- one line for each supported flag (option) */
240 flags
= wd
->ident
.options
;
242 for (i
= 0; i
< ARRAY_SIZE(wdflags
); i
++) {
243 if (wanted
&& !(wanted
& wdflags
[i
].flag
))
245 else if (flags
& wdflags
[i
].flag
)
246 add_flag_line(tt
, wd
, &wdflags
[i
]);
248 flags
&= ~wdflags
[i
].flag
;
252 warnx(_("%s: unknown flags 0x%x\n"), wd
->device
, flags
);
262 * Warning: successfully opened watchdog has to be properly closed with magic
263 * close character otherwise the machine will be rebooted!
265 * Don't use err() or exit() here!
267 static int read_watchdog(struct wdinfo
*wd
)
270 sigset_t sigs
, oldsigs
;
274 sigemptyset(&oldsigs
);
276 sigprocmask(SIG_BLOCK
, &sigs
, &oldsigs
);
278 fd
= open(wd
->device
, O_WRONLY
|O_CLOEXEC
);
282 warnx(_("%s: watchdog already in use, terminating."),
284 warn(_("%s: open failed"), wd
->device
);
288 if (ioctl(fd
, WDIOC_GETSUPPORT
, &wd
->ident
) < 0)
289 warn(_("%s: failed to get information about watchdog"), wd
->device
);
291 ioctl(fd
, WDIOC_GETSTATUS
, &wd
->status
);
292 ioctl(fd
, WDIOC_GETBOOTSTATUS
, &wd
->bstatus
);
294 if (ioctl(fd
, WDIOC_GETTIMEOUT
, &wd
->timeout
) >= 0)
296 if (ioctl(fd
, WDIOC_GETPRETIMEOUT
, &wd
->pretimeout
) >= 0)
297 wd
->has_pretimeout
= 1;
298 if (ioctl(fd
, WDIOC_GETTIMELEFT
, &wd
->timeleft
) >= 0)
299 wd
->has_timeleft
= 1;
303 /* We just opened this to query the state, not to arm
304 * it hence use the magic close character */
305 static const char v
= 'V';
307 if (write(fd
, &v
, 1) >= 0)
309 if (errno
!= EINTR
) {
310 warn(_("%s: failed to disarm watchdog"), wd
->device
);
313 /* Let's try hard, since if we don't get this right
314 * the machine might end up rebooting. */
318 sigprocmask(SIG_SETMASK
, &oldsigs
, NULL
);
323 static void print_oneline(struct wdinfo
*wd
, uint32_t wanted
,
324 int noident
, int notimeouts
, int noflags
)
326 printf("%s:", wd
->device
);
329 printf(" VERSION=\"%x\"", wd
->ident
.firmware_version
);
331 printf(" IDENTITY=");
332 tt_fputs_quoted((char *) wd
->ident
.identity
, stdout
);
336 printf(" TIMEOUT=\"%i\"", wd
->timeout
);
337 if (wd
->has_pretimeout
)
338 printf(" PRETIMEOUT=\"%i\"", wd
->pretimeout
);
339 if (wd
->has_timeleft
)
340 printf(" TIMELEFT=\"%i\"", wd
->timeleft
);
345 uint32_t flags
= wd
->ident
.options
;
347 for (i
= 0; i
< ARRAY_SIZE(wdflags
); i
++) {
348 const struct wdflag
*fl
;
350 if ((wanted
&& !(wanted
& wdflags
[i
].flag
)) ||
351 !(flags
& wdflags
[i
].flag
))
356 printf(" %s=\"%s\"", fl
->name
,
357 wd
->status
& fl
->flag
? "1" : "0");
358 printf(" %s_BOOT=\"%s\"", fl
->name
,
359 wd
->bstatus
& fl
->flag
? "1" : "0");
367 static void show_timeouts(struct wdinfo
*wd
)
370 printf(_("%-15s%2i seconds\n"), _("Timeout:"), wd
->timeout
);
371 if (wd
->has_pretimeout
)
372 printf(_("%-15s%2i seconds\n"), _("Pre-timeout:"), wd
->pretimeout
);
373 if (wd
->has_timeleft
)
374 printf(_("%-15s%2i seconds\n"), _("Timeleft:"), wd
->timeleft
);
377 int main(int argc
, char *argv
[])
380 int c
, tt_flags
= 0, res
= EXIT_SUCCESS
, count
= 0;
381 char noflags
= 0, noident
= 0, notimeouts
= 0, oneline
= 0;
384 static const struct option long_opts
[] = {
385 { "flags", required_argument
, NULL
, 'f' },
386 { "flags-only", no_argument
, NULL
, 'x' },
387 { "help", no_argument
, NULL
, 'h' },
388 { "noflags", no_argument
, NULL
, 'F' },
389 { "noheadings", no_argument
, NULL
, 'n' },
390 { "noident", no_argument
, NULL
, 'I' },
391 { "notimeouts", no_argument
, NULL
, 'T' },
392 { "output", required_argument
, NULL
, 'o' },
393 { "oneline", no_argument
, NULL
, 'O' },
394 { "raw", no_argument
, NULL
, 'r' },
395 { "version", no_argument
, NULL
, 'V' },
399 setlocale(LC_ALL
, "");
400 bindtextdomain(PACKAGE
, LOCALEDIR
);
402 atexit(close_stdout
);
404 while ((c
= getopt_long(argc
, argv
,
405 "d:f:hFnITo:OrVx", long_opts
, NULL
)) != -1) {
408 ncolumns
= string_to_idarray(optarg
,
409 columns
, ARRAY_SIZE(columns
),
415 if (string_to_bitmask(optarg
, (unsigned long *) &wanted
, name2bit
) != 0)
419 printf(UTIL_LINUX_VERSION
);
433 tt_flags
|= TT_FL_NOHEADINGS
;
436 tt_flags
|= TT_FL_RAW
;
452 if (wanted
&& noflags
)
453 errx(EXIT_FAILURE
, _("--flags and --noflags are mutually exclusive"));
456 /* default columns */
457 columns
[ncolumns
++] = COL_FLAG
;
458 columns
[ncolumns
++] = COL_DESC
;
459 columns
[ncolumns
++] = COL_STATUS
;
460 columns
[ncolumns
++] = COL_BSTATUS
;
466 memset(&wd
, 0, sizeof(wd
));
469 wd
.device
= _PATH_WATCHDOG_DEV
;
471 wd
.device
= argv
[optind
++];
477 rc
= read_watchdog(&wd
);
484 print_oneline(&wd
, wanted
, noident
, notimeouts
, noflags
);
490 printf("%-15s%s\n", _("Device:"), wd
.device
);
491 printf(_("%-15s%s [version %x]\n"),
494 wd
.ident
.firmware_version
);
499 show_flags(&wd
, tt_flags
, wanted
);
500 } while (optind
< argc
);