]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/wdctl.c
misc: consolidate smartcols error messages
[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(FILE *out)
169 {
170 size_t i;
171
172 fputs(USAGE_HEADER, out);
173 fprintf(out,
174 _(" %s [options] [<device> ...]\n"), program_invocation_short_name);
175
176 fputs(USAGE_SEPARATOR, out);
177 fputs(_("Show the status of the hardware watchdog.\n"), out);
178
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);
190
191 fputs(USAGE_SEPARATOR, out);
192 fputs(USAGE_HELP, out);
193 fputs(USAGE_VERSION, out);
194 fputs(USAGE_SEPARATOR, out);
195
196 fprintf(out, _("The default device is %s.\n"), _PATH_WATCHDOG_DEV);
197 fputs(USAGE_SEPARATOR, out);
198
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));
202
203 fprintf(out, USAGE_MAN_TAIL("wdctl(8)"));
204
205 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
206 }
207
208 static void add_flag_line(struct libscols_table *table, struct wdinfo *wd, const struct wdflag *fl)
209 {
210 int i;
211 struct libscols_line *line;
212
213 line = scols_table_new_line(table, NULL);
214 if (!line) {
215 warn(_("failed to allocate output line"));
216 return;
217 }
218
219 for (i = 0; i < ncolumns; i++) {
220 const char *str = NULL;
221
222 switch (get_column_id(i)) {
223 case COL_FLAG:
224 str = fl->name;
225 break;
226 case COL_DESC:
227 str = fl->description;
228 break;
229 case COL_STATUS:
230 str = wd->status & fl->flag ? "1" : "0";
231 break;
232 case COL_BSTATUS:
233 str = wd->bstatus & fl->flag ? "1" : "0";
234 break;
235 case COL_DEVICE:
236 str = wd->device;
237 break;
238 default:
239 break;
240 }
241
242 if (str && scols_line_set_data(line, i, str)) {
243 warn(_("failed to add output data"));
244 break;
245 }
246 }
247 }
248
249 static int show_flags(struct wdinfo *wd, uint32_t wanted)
250 {
251 size_t i;
252 int rc = -1;
253 struct libscols_table *table;
254 uint32_t flags;
255
256 scols_init_debug(0);
257
258 /* create output table */
259 table = scols_new_table();
260 if (!table) {
261 warn(_("failed to allocate output table"));
262 return -1;
263 }
264 scols_table_enable_raw(table, raw);
265 scols_table_enable_noheadings(table, no_headings);
266
267 /* define columns */
268 for (i = 0; i < (size_t) ncolumns; i++) {
269 struct colinfo *col = get_column_info(i);
270
271 if (!scols_table_new_column(table, col->name, col->whint, col->flags)) {
272 warnx(_("failed to allocate output column"));
273 goto done;
274 }
275 }
276
277 /* fill-in table with data
278 * -- one line for each supported flag (option) */
279 flags = wd->ident.options;
280
281 for (i = 0; i < ARRAY_SIZE(wdflags); i++) {
282 if (wanted && !(wanted & wdflags[i].flag))
283 ; /* ignore */
284 else if (flags & wdflags[i].flag)
285 add_flag_line(table, wd, &wdflags[i]);
286
287 flags &= ~wdflags[i].flag;
288 }
289
290 if (flags)
291 warnx(_("%s: unknown flags 0x%x\n"), wd->device, flags);
292
293 scols_print_table(table);
294 rc = 0;
295 done:
296 scols_unref_table(table);
297 return rc;
298 }
299 /*
300 * Warning: successfully opened watchdog has to be properly closed with magic
301 * close character otherwise the machine will be rebooted!
302 *
303 * Don't use err() or exit() here!
304 */
305 static int set_watchdog(struct wdinfo *wd, int timeout)
306 {
307 int fd;
308 sigset_t sigs, oldsigs;
309 int rc = 0;
310
311 assert(wd->device);
312
313 sigemptyset(&oldsigs);
314 sigfillset(&sigs);
315 sigprocmask(SIG_BLOCK, &sigs, &oldsigs);
316
317 fd = open(wd->device, O_WRONLY|O_CLOEXEC);
318
319 if (fd < 0) {
320 if (errno == EBUSY)
321 warnx(_("%s: watchdog already in use, terminating."),
322 wd->device);
323 warn(_("cannot open %s"), wd->device);
324 return -1;
325 }
326
327 for (;;) {
328 /* We just opened this to query the state, not to arm
329 * it hence use the magic close character */
330 static const char v = 'V';
331
332 if (write(fd, &v, 1) >= 0)
333 break;
334 if (errno != EINTR) {
335 warn(_("%s: failed to disarm watchdog"), wd->device);
336 break;
337 }
338 /* Let's try hard, since if we don't get this right
339 * the machine might end up rebooting. */
340 }
341
342 if (ioctl(fd, WDIOC_SETTIMEOUT, &timeout) != 0) {
343 rc = errno;
344 warn(_("cannot set timeout for %s"), wd->device);
345 }
346
347 if (close_fd(fd))
348 warn(_("write failed"));
349 sigprocmask(SIG_SETMASK, &oldsigs, NULL);
350 printf(P_("Timeout has been set to %d second.\n",
351 "Timeout has been set to %d seconds.\n", timeout), timeout);
352
353 return rc;
354 }
355
356 /*
357 * Warning: successfully opened watchdog has to be properly closed with magic
358 * close character otherwise the machine will be rebooted!
359 *
360 * Don't use err() or exit() here!
361 */
362 static int read_watchdog(struct wdinfo *wd)
363 {
364 int fd;
365 sigset_t sigs, oldsigs;
366
367 assert(wd->device);
368
369 sigemptyset(&oldsigs);
370 sigfillset(&sigs);
371 sigprocmask(SIG_BLOCK, &sigs, &oldsigs);
372
373 fd = open(wd->device, O_WRONLY|O_CLOEXEC);
374
375 if (fd < 0) {
376 if (errno == EBUSY)
377 warnx(_("%s: watchdog already in use, terminating."),
378 wd->device);
379 warn(_("cannot open %s"), wd->device);
380 return -1;
381 }
382
383 if (ioctl(fd, WDIOC_GETSUPPORT, &wd->ident) < 0)
384 warn(_("%s: failed to get information about watchdog"), wd->device);
385 else {
386 ioctl(fd, WDIOC_GETSTATUS, &wd->status);
387 ioctl(fd, WDIOC_GETBOOTSTATUS, &wd->bstatus);
388
389 if (ioctl(fd, WDIOC_GETTIMEOUT, &wd->timeout) >= 0)
390 wd->has_timeout = 1;
391 if (ioctl(fd, WDIOC_GETPRETIMEOUT, &wd->pretimeout) >= 0)
392 wd->has_pretimeout = 1;
393 if (ioctl(fd, WDIOC_GETTIMELEFT, &wd->timeleft) >= 0)
394 wd->has_timeleft = 1;
395 }
396
397 for (;;) {
398 /* We just opened this to query the state, not to arm
399 * it hence use the magic close character */
400 static const char v = 'V';
401
402 if (write(fd, &v, 1) >= 0)
403 break;
404 if (errno != EINTR) {
405 warn(_("%s: failed to disarm watchdog"), wd->device);
406 break;
407 }
408 /* Let's try hard, since if we don't get this right
409 * the machine might end up rebooting. */
410 }
411
412 if (close_fd(fd))
413 warn(_("write failed"));
414 sigprocmask(SIG_SETMASK, &oldsigs, NULL);
415
416 return 0;
417 }
418
419 static void print_oneline(struct wdinfo *wd, uint32_t wanted,
420 int noident, int notimeouts, int noflags)
421 {
422 printf("%s:", wd->device);
423
424 if (!noident) {
425 printf(" VERSION=\"%x\"", wd->ident.firmware_version);
426
427 printf(" IDENTITY=");
428 fputs_quoted((char *) wd->ident.identity, stdout);
429 }
430 if (!notimeouts) {
431 if (wd->has_timeout)
432 printf(" TIMEOUT=\"%i\"", wd->timeout);
433 if (wd->has_pretimeout)
434 printf(" PRETIMEOUT=\"%i\"", wd->pretimeout);
435 if (wd->has_timeleft)
436 printf(" TIMELEFT=\"%i\"", wd->timeleft);
437 }
438
439 if (!noflags) {
440 size_t i;
441 uint32_t flags = wd->ident.options;
442
443 for (i = 0; i < ARRAY_SIZE(wdflags); i++) {
444 const struct wdflag *fl;
445
446 if ((wanted && !(wanted & wdflags[i].flag)) ||
447 !(flags & wdflags[i].flag))
448 continue;
449
450 fl= &wdflags[i];
451
452 printf(" %s=\"%s\"", fl->name,
453 wd->status & fl->flag ? "1" : "0");
454 printf(" %s_BOOT=\"%s\"", fl->name,
455 wd->bstatus & fl->flag ? "1" : "0");
456
457 }
458 }
459
460 fputc('\n', stdout);
461 }
462
463 static void show_timeouts(struct wdinfo *wd)
464 {
465 if (wd->has_timeout)
466 printf(P_("%-14s %2i second\n", "%-14s %2i seconds\n", wd->timeout),
467 _("Timeout:"), wd->timeout);
468 if (wd->has_pretimeout)
469 printf(P_("%-14s %2i second\n", "%-14s %2i seconds\n", wd->pretimeout),
470 _("Pre-timeout:"), wd->pretimeout);
471 if (wd->has_timeleft)
472 printf(P_("%-14s %2i second\n", "%-14s %2i seconds\n", wd->timeleft),
473 _("Timeleft:"), wd->timeleft);
474 }
475
476 int main(int argc, char *argv[])
477 {
478 struct wdinfo wd;
479 int c, res = EXIT_SUCCESS, count = 0;
480 char noflags = 0, noident = 0, notimeouts = 0, oneline = 0;
481 uint32_t wanted = 0;
482 int timeout = 0;
483
484 static const struct option long_opts[] = {
485 { "flags", required_argument, NULL, 'f' },
486 { "flags-only", no_argument, NULL, 'x' },
487 { "help", no_argument, NULL, 'h' },
488 { "noflags", no_argument, NULL, 'F' },
489 { "noheadings", no_argument, NULL, 'n' },
490 { "noident", no_argument, NULL, 'I' },
491 { "notimeouts", no_argument, NULL, 'T' },
492 { "settimeout", required_argument, NULL, 's' },
493 { "output", required_argument, NULL, 'o' },
494 { "oneline", no_argument, NULL, 'O' },
495 { "raw", no_argument, NULL, 'r' },
496 { "version", no_argument, NULL, 'V' },
497 { NULL, 0, NULL, 0 }
498 };
499
500 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
501 { 'F','f' }, /* noflags,flags*/
502 { 0 }
503 };
504 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
505
506 setlocale(LC_ALL, "");
507 bindtextdomain(PACKAGE, LOCALEDIR);
508 textdomain(PACKAGE);
509 atexit(close_stdout);
510
511 while ((c = getopt_long(argc, argv,
512 "d:f:hFnITo:s:OrVx", long_opts, NULL)) != -1) {
513
514 err_exclusive_options(c, long_opts, excl, excl_st);
515
516 switch(c) {
517 case 'o':
518 ncolumns = string_to_idarray(optarg,
519 columns, ARRAY_SIZE(columns),
520 column2id);
521 if (ncolumns < 0)
522 return EXIT_FAILURE;
523 break;
524 case 's':
525 timeout = strtos32_or_err(optarg, _("invalid timeout argument"));
526 break;
527 case 'f':
528 if (string_to_bitmask(optarg, (unsigned long *) &wanted, name2bit) != 0)
529 return EXIT_FAILURE;
530 break;
531 case 'V':
532 printf(UTIL_LINUX_VERSION);
533 return EXIT_SUCCESS;
534 case 'h':
535 usage(stdout);
536 case 'F':
537 noflags = 1;
538 break;
539 case 'I':
540 noident = 1;
541 break;
542 case 'T':
543 notimeouts = 1;
544 break;
545 case 'n':
546 no_headings = 1;
547 break;
548 case 'r':
549 raw = 1;
550 break;
551 case 'O':
552 oneline = 1;
553 break;
554 case 'x':
555 noident = 1;
556 notimeouts = 1;
557 break;
558 default:
559 errtryhelp(EXIT_FAILURE);
560 }
561 }
562
563 if (!ncolumns) {
564 /* default columns */
565 columns[ncolumns++] = COL_FLAG;
566 columns[ncolumns++] = COL_DESC;
567 columns[ncolumns++] = COL_STATUS;
568 columns[ncolumns++] = COL_BSTATUS;
569 }
570
571 do {
572 int rc;
573
574 memset(&wd, 0, sizeof(wd));
575
576 if (optind == argc)
577 wd.device = _PATH_WATCHDOG_DEV;
578 else
579 wd.device = argv[optind++];
580
581 if (count)
582 fputc('\n', stdout);
583 count++;
584
585 if (timeout) {
586 rc = set_watchdog(&wd, timeout);
587 if (rc) {
588 res = EXIT_FAILURE;
589 }
590 }
591
592 rc = read_watchdog(&wd);
593 if (rc) {
594 res = EXIT_FAILURE;
595 continue;
596 }
597
598 if (oneline) {
599 print_oneline(&wd, wanted, noident, notimeouts, noflags);
600 continue;
601 }
602
603 /* pretty output */
604 if (!noident) {
605 printf("%-15s%s\n", _("Device:"), wd.device);
606 printf("%-15s%s [%s %x]\n",
607 _("Identity:"),
608 wd.ident.identity,
609 _("version"),
610 wd.ident.firmware_version);
611 }
612 if (!notimeouts)
613 show_timeouts(&wd);
614 if (!noflags)
615 show_flags(&wd, wanted);
616 } while (optind < argc);
617
618 return res;
619 }