]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/wdctl.c
642db85d221d93592f48e0c1eae3877733a2f143
[thirdparty/util-linux.git] / sys-utils / wdctl.c
1 /*
2 * wdctl(8) - show hardware watchdog status
3 *
4 * Copyright (C) 2012 Lennart Poettering
5 * Copyright (C) 2012 Karel Zak <kzak@redhat.com>
6 *
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
10 * later version.
11 *
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.
16 *
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.
20 */
21 #include <sys/ioctl.h>
22 #include <getopt.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <assert.h>
27 #include <linux/watchdog.h>
28
29 #include <libsmartcols.h>
30
31 #include "nls.h"
32 #include "c.h"
33 #include "xalloc.h"
34 #include "closestream.h"
35 #include "optutils.h"
36 #include "pathnames.h"
37 #include "strutils.h"
38 #include "carefulputc.h"
39
40 /*
41 * since 2.6.18
42 */
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 */
52 #endif
53
54 /*
55 * since 3.5
56 */
57 #ifndef WDIOF_ALARMONLY
58 # define WDIOF_ALARMONLY 0x0400 /* Watchdog triggers a management or
59 other external alarm not a reboot */
60 #endif
61
62 /* basic output flags */
63 static int no_headings;
64 static int raw;
65
66 struct wdflag {
67 uint32_t flag;
68 const char *name;
69 const char *description;
70 };
71
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") }
85 };
86
87
88 /* column names */
89 struct colinfo {
90 const char *name; /* header */
91 double whint; /* width hint (N < 1 is in percent of termwidth) */
92 int flags; /* SCOLS_FL_* */
93 const char *help;
94 };
95
96 enum { COL_FLAG, COL_DESC, COL_STATUS, COL_BSTATUS, COL_DEVICE };
97
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") }
105
106 };
107
108 static int columns[ARRAY_SIZE(infos) * 2];
109 static int ncolumns;
110
111 struct wdinfo {
112 char *device;
113
114 int timeout;
115 int timeleft;
116 int pretimeout;
117
118 uint32_t status;
119 uint32_t bstatus;
120
121 struct watchdog_info ident;
122
123 unsigned int has_timeout : 1,
124 has_timeleft : 1,
125 has_pretimeout : 1;
126 };
127
128 /* converts flag name to flag bit */
129 static long name2bit(const char *name, size_t namesz)
130 {
131 size_t i;
132
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;
137 }
138 warnx(_("unknown flag: %s"), name);
139 return -1;
140 }
141
142 static int column2id(const char *name, size_t namesz)
143 {
144 size_t i;
145
146 for (i = 0; i < ARRAY_SIZE(infos); i++) {
147 const char *cn = infos[i].name;
148 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
149 return i;
150 }
151 warnx(_("unknown column: %s"), name);
152 return -1;
153 }
154
155 static int get_column_id(int num)
156 {
157 assert(num < ncolumns);
158 assert(columns[num] < (int) ARRAY_SIZE(infos));
159
160 return columns[num];
161 }
162
163 static struct colinfo *get_column_info(unsigned num)
164 {
165 return &infos[ get_column_id(num) ];
166 }
167
168 static void __attribute__((__noreturn__)) usage(void)
169 {
170 FILE *out = stdout;
171 size_t i;
172
173 fputs(USAGE_HEADER, out);
174 fprintf(out,
175 _(" %s [options] [<device> ...]\n"), program_invocation_short_name);
176
177 fputs(USAGE_SEPARATOR, out);
178 fputs(_("Show the status of the hardware watchdog.\n"), out);
179
180 fputs(USAGE_OPTIONS, out);
181 fputs(_(" -f, --flags <list> print selected flags only\n"
182 " -F, --noflags don't print information about flags\n"
183 " -I, --noident don't print watchdog identity information\n"
184 " -n, --noheadings don't print headings for flags table\n"
185 " -O, --oneline print all information on one line\n"
186 " -o, --output <list> output columns of the flags\n"
187 " -r, --raw use raw output format for flags table\n"
188 " -T, --notimeouts don't print watchdog timeouts\n"
189 " -s, --settimeout <sec> set watchdog timeout\n"
190 " -x, --flags-only print only flags table (same as -I -T)\n"), out);
191
192 fputs(USAGE_SEPARATOR, out);
193 printf(USAGE_HELP_OPTIONS(24));
194 fputs(USAGE_SEPARATOR, out);
195
196 fprintf(out, _("The default device is %s.\n"), _PATH_WATCHDOG_DEV);
197
198 fputs(USAGE_COLUMNS, out);
199 for (i = 0; i < ARRAY_SIZE(infos); i++)
200 fprintf(out, " %13s %s\n", infos[i].name, _(infos[i].help));
201
202 printf(USAGE_MAN_TAIL("wdctl(8)"));
203
204 exit(EXIT_SUCCESS);
205 }
206
207 static void add_flag_line(struct libscols_table *table, struct wdinfo *wd, const struct wdflag *fl)
208 {
209 int i;
210 struct libscols_line *line;
211
212 line = scols_table_new_line(table, NULL);
213 if (!line) {
214 warn(_("failed to allocate output line"));
215 return;
216 }
217
218 for (i = 0; i < ncolumns; i++) {
219 const char *str = NULL;
220
221 switch (get_column_id(i)) {
222 case COL_FLAG:
223 str = fl->name;
224 break;
225 case COL_DESC:
226 str = fl->description;
227 break;
228 case COL_STATUS:
229 str = wd->status & fl->flag ? "1" : "0";
230 break;
231 case COL_BSTATUS:
232 str = wd->bstatus & fl->flag ? "1" : "0";
233 break;
234 case COL_DEVICE:
235 str = wd->device;
236 break;
237 default:
238 break;
239 }
240
241 if (str && scols_line_set_data(line, i, str)) {
242 warn(_("failed to add output data"));
243 break;
244 }
245 }
246 }
247
248 static int show_flags(struct wdinfo *wd, uint32_t wanted)
249 {
250 size_t i;
251 int rc = -1;
252 struct libscols_table *table;
253 uint32_t flags;
254
255 scols_init_debug(0);
256
257 /* create output table */
258 table = scols_new_table();
259 if (!table) {
260 warn(_("failed to allocate output table"));
261 return -1;
262 }
263 scols_table_enable_raw(table, raw);
264 scols_table_enable_noheadings(table, no_headings);
265
266 /* define columns */
267 for (i = 0; i < (size_t) ncolumns; i++) {
268 struct colinfo *col = get_column_info(i);
269
270 if (!scols_table_new_column(table, col->name, col->whint, col->flags)) {
271 warnx(_("failed to allocate output column"));
272 goto done;
273 }
274 }
275
276 /* fill-in table with data
277 * -- one line for each supported flag (option) */
278 flags = wd->ident.options;
279
280 for (i = 0; i < ARRAY_SIZE(wdflags); i++) {
281 if (wanted && !(wanted & wdflags[i].flag))
282 ; /* ignore */
283 else if (flags & wdflags[i].flag)
284 add_flag_line(table, wd, &wdflags[i]);
285
286 flags &= ~wdflags[i].flag;
287 }
288
289 if (flags)
290 warnx(_("%s: unknown flags 0x%x\n"), wd->device, flags);
291
292 scols_print_table(table);
293 rc = 0;
294 done:
295 scols_unref_table(table);
296 return rc;
297 }
298 /*
299 * Warning: successfully opened watchdog has to be properly closed with magic
300 * close character otherwise the machine will be rebooted!
301 *
302 * Don't use err() or exit() here!
303 */
304 static int set_watchdog(struct wdinfo *wd, int timeout)
305 {
306 int fd;
307 sigset_t sigs, oldsigs;
308 int rc = 0;
309
310 assert(wd->device);
311
312 sigemptyset(&oldsigs);
313 sigfillset(&sigs);
314 sigprocmask(SIG_BLOCK, &sigs, &oldsigs);
315
316 fd = open(wd->device, O_WRONLY|O_CLOEXEC);
317
318 if (fd < 0) {
319 if (errno == EBUSY)
320 warnx(_("%s: watchdog already in use, terminating."),
321 wd->device);
322 warn(_("cannot open %s"), wd->device);
323 return -1;
324 }
325
326 for (;;) {
327 /* We just opened this to query the state, not to arm
328 * it hence use the magic close character */
329 static const char v = 'V';
330
331 if (write(fd, &v, 1) >= 0)
332 break;
333 if (errno != EINTR) {
334 warn(_("%s: failed to disarm watchdog"), wd->device);
335 break;
336 }
337 /* Let's try hard, since if we don't get this right
338 * the machine might end up rebooting. */
339 }
340
341 if (ioctl(fd, WDIOC_SETTIMEOUT, &timeout) != 0) {
342 rc = errno;
343 warn(_("cannot set timeout for %s"), wd->device);
344 }
345
346 if (close(fd))
347 warn(_("write failed"));
348 sigprocmask(SIG_SETMASK, &oldsigs, NULL);
349 printf(P_("Timeout has been set to %d second.\n",
350 "Timeout has been set to %d seconds.\n", timeout), timeout);
351
352 return rc;
353 }
354
355 /*
356 * Warning: successfully opened watchdog has to be properly closed with magic
357 * close character otherwise the machine will be rebooted!
358 *
359 * Don't use err() or exit() here!
360 */
361 static int read_watchdog(struct wdinfo *wd)
362 {
363 int fd;
364 sigset_t sigs, oldsigs;
365
366 assert(wd->device);
367
368 sigemptyset(&oldsigs);
369 sigfillset(&sigs);
370 sigprocmask(SIG_BLOCK, &sigs, &oldsigs);
371
372 fd = open(wd->device, O_WRONLY|O_CLOEXEC);
373
374 if (fd < 0) {
375 if (errno == EBUSY)
376 warnx(_("%s: watchdog already in use, terminating."),
377 wd->device);
378 warn(_("cannot open %s"), wd->device);
379 return -1;
380 }
381
382 if (ioctl(fd, WDIOC_GETSUPPORT, &wd->ident) < 0)
383 warn(_("%s: failed to get information about watchdog"), wd->device);
384 else {
385 ioctl(fd, WDIOC_GETSTATUS, &wd->status);
386 ioctl(fd, WDIOC_GETBOOTSTATUS, &wd->bstatus);
387
388 if (ioctl(fd, WDIOC_GETTIMEOUT, &wd->timeout) >= 0)
389 wd->has_timeout = 1;
390 if (ioctl(fd, WDIOC_GETPRETIMEOUT, &wd->pretimeout) >= 0)
391 wd->has_pretimeout = 1;
392 if (ioctl(fd, WDIOC_GETTIMELEFT, &wd->timeleft) >= 0)
393 wd->has_timeleft = 1;
394 }
395
396 for (;;) {
397 /* We just opened this to query the state, not to arm
398 * it hence use the magic close character */
399 static const char v = 'V';
400
401 if (write(fd, &v, 1) >= 0)
402 break;
403 if (errno != EINTR) {
404 warn(_("%s: failed to disarm watchdog"), wd->device);
405 break;
406 }
407 /* Let's try hard, since if we don't get this right
408 * the machine might end up rebooting. */
409 }
410
411 if (close(fd))
412 warn(_("write failed"));
413 sigprocmask(SIG_SETMASK, &oldsigs, NULL);
414
415 return 0;
416 }
417
418 static void print_oneline(struct wdinfo *wd, uint32_t wanted,
419 int noident, int notimeouts, int noflags)
420 {
421 printf("%s:", wd->device);
422
423 if (!noident) {
424 printf(" VERSION=\"%x\"", wd->ident.firmware_version);
425
426 printf(" IDENTITY=");
427 fputs_quoted((char *) wd->ident.identity, stdout);
428 }
429 if (!notimeouts) {
430 if (wd->has_timeout)
431 printf(" TIMEOUT=\"%i\"", wd->timeout);
432 if (wd->has_pretimeout)
433 printf(" PRETIMEOUT=\"%i\"", wd->pretimeout);
434 if (wd->has_timeleft)
435 printf(" TIMELEFT=\"%i\"", wd->timeleft);
436 }
437
438 if (!noflags) {
439 size_t i;
440 uint32_t flags = wd->ident.options;
441
442 for (i = 0; i < ARRAY_SIZE(wdflags); i++) {
443 const struct wdflag *fl;
444
445 if ((wanted && !(wanted & wdflags[i].flag)) ||
446 !(flags & wdflags[i].flag))
447 continue;
448
449 fl= &wdflags[i];
450
451 printf(" %s=\"%s\"", fl->name,
452 wd->status & fl->flag ? "1" : "0");
453 printf(" %s_BOOT=\"%s\"", fl->name,
454 wd->bstatus & fl->flag ? "1" : "0");
455
456 }
457 }
458
459 fputc('\n', stdout);
460 }
461
462 static void show_timeouts(struct wdinfo *wd)
463 {
464 if (wd->has_timeout)
465 printf(P_("%-14s %2i second\n", "%-14s %2i seconds\n", wd->timeout),
466 _("Timeout:"), wd->timeout);
467 if (wd->has_pretimeout)
468 printf(P_("%-14s %2i second\n", "%-14s %2i seconds\n", wd->pretimeout),
469 _("Pre-timeout:"), wd->pretimeout);
470 if (wd->has_timeleft)
471 printf(P_("%-14s %2i second\n", "%-14s %2i seconds\n", wd->timeleft),
472 _("Timeleft:"), wd->timeleft);
473 }
474
475 int main(int argc, char *argv[])
476 {
477 struct wdinfo wd;
478 int c, res = EXIT_SUCCESS, count = 0;
479 char noflags = 0, noident = 0, notimeouts = 0, oneline = 0;
480 uint32_t wanted = 0;
481 int timeout = 0;
482
483 static const struct option long_opts[] = {
484 { "flags", required_argument, NULL, 'f' },
485 { "flags-only", no_argument, NULL, 'x' },
486 { "help", no_argument, NULL, 'h' },
487 { "noflags", no_argument, NULL, 'F' },
488 { "noheadings", no_argument, NULL, 'n' },
489 { "noident", no_argument, NULL, 'I' },
490 { "notimeouts", no_argument, NULL, 'T' },
491 { "settimeout", required_argument, NULL, 's' },
492 { "output", required_argument, NULL, 'o' },
493 { "oneline", no_argument, NULL, 'O' },
494 { "raw", no_argument, NULL, 'r' },
495 { "version", no_argument, NULL, 'V' },
496 { NULL, 0, NULL, 0 }
497 };
498
499 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
500 { 'F','f' }, /* noflags,flags*/
501 { 0 }
502 };
503 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
504
505 setlocale(LC_ALL, "");
506 bindtextdomain(PACKAGE, LOCALEDIR);
507 textdomain(PACKAGE);
508 atexit(close_stdout);
509
510 while ((c = getopt_long(argc, argv,
511 "d:f:hFnITo:s:OrVx", long_opts, NULL)) != -1) {
512
513 err_exclusive_options(c, long_opts, excl, excl_st);
514
515 switch(c) {
516 case 'o':
517 ncolumns = string_to_idarray(optarg,
518 columns, ARRAY_SIZE(columns),
519 column2id);
520 if (ncolumns < 0)
521 return EXIT_FAILURE;
522 break;
523 case 's':
524 timeout = strtos32_or_err(optarg, _("invalid timeout argument"));
525 break;
526 case 'f':
527 if (string_to_bitmask(optarg, (unsigned long *) &wanted, name2bit) != 0)
528 return EXIT_FAILURE;
529 break;
530 case 'V':
531 printf(UTIL_LINUX_VERSION);
532 return EXIT_SUCCESS;
533 case 'h':
534 usage();
535 case 'F':
536 noflags = 1;
537 break;
538 case 'I':
539 noident = 1;
540 break;
541 case 'T':
542 notimeouts = 1;
543 break;
544 case 'n':
545 no_headings = 1;
546 break;
547 case 'r':
548 raw = 1;
549 break;
550 case 'O':
551 oneline = 1;
552 break;
553 case 'x':
554 noident = 1;
555 notimeouts = 1;
556 break;
557 default:
558 errtryhelp(EXIT_FAILURE);
559 }
560 }
561
562 if (!ncolumns) {
563 /* default columns */
564 columns[ncolumns++] = COL_FLAG;
565 columns[ncolumns++] = COL_DESC;
566 columns[ncolumns++] = COL_STATUS;
567 columns[ncolumns++] = COL_BSTATUS;
568 }
569
570 do {
571 int rc;
572
573 memset(&wd, 0, sizeof(wd));
574
575 if (optind == argc)
576 wd.device = _PATH_WATCHDOG_DEV;
577 else
578 wd.device = argv[optind++];
579
580 if (count)
581 fputc('\n', stdout);
582 count++;
583
584 if (timeout) {
585 rc = set_watchdog(&wd, timeout);
586 if (rc) {
587 res = EXIT_FAILURE;
588 }
589 }
590
591 rc = read_watchdog(&wd);
592 if (rc) {
593 res = EXIT_FAILURE;
594 continue;
595 }
596
597 if (oneline) {
598 print_oneline(&wd, wanted, noident, notimeouts, noflags);
599 continue;
600 }
601
602 /* pretty output */
603 if (!noident) {
604 printf("%-15s%s\n", _("Device:"), wd.device);
605 printf("%-15s%s [%s %x]\n",
606 _("Identity:"),
607 wd.ident.identity,
608 _("version"),
609 wd.ident.firmware_version);
610 }
611 if (!notimeouts)
612 show_timeouts(&wd);
613 if (!noflags)
614 show_flags(&wd, wanted);
615 } while (optind < argc);
616
617 return res;
618 }