]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/wdctl.c
wdct: mark usage() as noreturn [coverity scan]
[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 initialize 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)
243 scols_line_set_data(line, i, str);
244 }
245 }
246
247 static int show_flags(struct wdinfo *wd, uint32_t wanted)
248 {
249 size_t i;
250 int rc = -1;
251 struct libscols_table *table;
252 uint32_t flags;
253
254 scols_init_debug(0);
255
256 /* create output table */
257 table = scols_new_table();
258 if (!table) {
259 warn(_("failed to initialize output table"));
260 return -1;
261 }
262 scols_table_enable_raw(table, raw);
263 scols_table_enable_noheadings(table, no_headings);
264
265 /* define columns */
266 for (i = 0; i < (size_t) ncolumns; i++) {
267 struct colinfo *col = get_column_info(i);
268
269 if (!scols_table_new_column(table, col->name, col->whint, col->flags)) {
270 warnx(_("failed to initialize output column"));
271 goto done;
272 }
273 }
274
275 /* fill-in table with data
276 * -- one line for each supported flag (option) */
277 flags = wd->ident.options;
278
279 for (i = 0; i < ARRAY_SIZE(wdflags); i++) {
280 if (wanted && !(wanted & wdflags[i].flag))
281 ; /* ignore */
282 else if (flags & wdflags[i].flag)
283 add_flag_line(table, wd, &wdflags[i]);
284
285 flags &= ~wdflags[i].flag;
286 }
287
288 if (flags)
289 warnx(_("%s: unknown flags 0x%x\n"), wd->device, flags);
290
291 scols_print_table(table);
292 rc = 0;
293 done:
294 scols_unref_table(table);
295 return rc;
296 }
297 /*
298 * Warning: successfully opened watchdog has to be properly closed with magic
299 * close character otherwise the machine will be rebooted!
300 *
301 * Don't use err() or exit() here!
302 */
303 static int set_watchdog(struct wdinfo *wd, int timeout)
304 {
305 int fd;
306 sigset_t sigs, oldsigs;
307 int rc = 0;
308
309 assert(wd->device);
310
311 sigemptyset(&oldsigs);
312 sigfillset(&sigs);
313 sigprocmask(SIG_BLOCK, &sigs, &oldsigs);
314
315 fd = open(wd->device, O_WRONLY|O_CLOEXEC);
316
317 if (fd < 0) {
318 if (errno == EBUSY)
319 warnx(_("%s: watchdog already in use, terminating."),
320 wd->device);
321 warn(_("cannot open %s"), wd->device);
322 return -1;
323 }
324
325 for (;;) {
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';
329
330 if (write(fd, &v, 1) >= 0)
331 break;
332 if (errno != EINTR) {
333 warn(_("%s: failed to disarm watchdog"), wd->device);
334 break;
335 }
336 /* Let's try hard, since if we don't get this right
337 * the machine might end up rebooting. */
338 }
339
340 if (ioctl(fd, WDIOC_SETTIMEOUT, &timeout) != 0) {
341 rc = errno;
342 warn(_("cannot set timeout for %s"), wd->device);
343 }
344
345 if (close_fd(fd))
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);
350
351 return rc;
352 }
353
354 /*
355 * Warning: successfully opened watchdog has to be properly closed with magic
356 * close character otherwise the machine will be rebooted!
357 *
358 * Don't use err() or exit() here!
359 */
360 static int read_watchdog(struct wdinfo *wd)
361 {
362 int fd;
363 sigset_t sigs, oldsigs;
364
365 assert(wd->device);
366
367 sigemptyset(&oldsigs);
368 sigfillset(&sigs);
369 sigprocmask(SIG_BLOCK, &sigs, &oldsigs);
370
371 fd = open(wd->device, O_WRONLY|O_CLOEXEC);
372
373 if (fd < 0) {
374 if (errno == EBUSY)
375 warnx(_("%s: watchdog already in use, terminating."),
376 wd->device);
377 warn(_("cannot open %s"), wd->device);
378 return -1;
379 }
380
381 if (ioctl(fd, WDIOC_GETSUPPORT, &wd->ident) < 0)
382 warn(_("%s: failed to get information about watchdog"), wd->device);
383 else {
384 ioctl(fd, WDIOC_GETSTATUS, &wd->status);
385 ioctl(fd, WDIOC_GETBOOTSTATUS, &wd->bstatus);
386
387 if (ioctl(fd, WDIOC_GETTIMEOUT, &wd->timeout) >= 0)
388 wd->has_timeout = 1;
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;
393 }
394
395 for (;;) {
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';
399
400 if (write(fd, &v, 1) >= 0)
401 break;
402 if (errno != EINTR) {
403 warn(_("%s: failed to disarm watchdog"), wd->device);
404 break;
405 }
406 /* Let's try hard, since if we don't get this right
407 * the machine might end up rebooting. */
408 }
409
410 if (close_fd(fd))
411 warn(_("write failed"));
412 sigprocmask(SIG_SETMASK, &oldsigs, NULL);
413
414 return 0;
415 }
416
417 static void print_oneline(struct wdinfo *wd, uint32_t wanted,
418 int noident, int notimeouts, int noflags)
419 {
420 printf("%s:", wd->device);
421
422 if (!noident) {
423 printf(" VERSION=\"%x\"", wd->ident.firmware_version);
424
425 printf(" IDENTITY=");
426 fputs_quoted((char *) wd->ident.identity, stdout);
427 }
428 if (!notimeouts) {
429 if (wd->has_timeout)
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);
435 }
436
437 if (!noflags) {
438 size_t i;
439 uint32_t flags = wd->ident.options;
440
441 for (i = 0; i < ARRAY_SIZE(wdflags); i++) {
442 const struct wdflag *fl;
443
444 if ((wanted && !(wanted & wdflags[i].flag)) ||
445 !(flags & wdflags[i].flag))
446 continue;
447
448 fl= &wdflags[i];
449
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");
454
455 }
456 }
457
458 fputc('\n', stdout);
459 }
460
461 static void show_timeouts(struct wdinfo *wd)
462 {
463 if (wd->has_timeout)
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);
472 }
473
474 int main(int argc, char *argv[])
475 {
476 struct wdinfo wd;
477 int c, res = EXIT_SUCCESS, count = 0;
478 char noflags = 0, noident = 0, notimeouts = 0, oneline = 0;
479 uint32_t wanted = 0;
480 int timeout = 0;
481
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' },
495 { NULL, 0, NULL, 0 }
496 };
497
498 static const ul_excl_t excl[] = { /* rows and cols in in ASCII order */
499 { 'F','f' }, /* noflags,flags*/
500 { 0 }
501 };
502 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
503
504 setlocale(LC_ALL, "");
505 bindtextdomain(PACKAGE, LOCALEDIR);
506 textdomain(PACKAGE);
507 atexit(close_stdout);
508
509 while ((c = getopt_long(argc, argv,
510 "d:f:hFnITo:s:OrVx", long_opts, NULL)) != -1) {
511
512 err_exclusive_options(c, long_opts, excl, excl_st);
513
514 switch(c) {
515 case 'o':
516 ncolumns = string_to_idarray(optarg,
517 columns, ARRAY_SIZE(columns),
518 column2id);
519 if (ncolumns < 0)
520 return EXIT_FAILURE;
521 break;
522 case 's':
523 timeout = strtos32_or_err(optarg, _("invalid timeout argument"));
524 break;
525 case 'f':
526 if (string_to_bitmask(optarg, (unsigned long *) &wanted, name2bit) != 0)
527 return EXIT_FAILURE;
528 break;
529 case 'V':
530 printf(UTIL_LINUX_VERSION);
531 return EXIT_SUCCESS;
532 case 'h':
533 usage(stdout);
534 case 'F':
535 noflags = 1;
536 break;
537 case 'I':
538 noident = 1;
539 break;
540 case 'T':
541 notimeouts = 1;
542 break;
543 case 'n':
544 no_headings = 1;
545 break;
546 case 'r':
547 raw = 1;
548 break;
549 case 'O':
550 oneline = 1;
551 break;
552 case 'x':
553 noident = 1;
554 notimeouts = 1;
555 break;
556
557 case '?':
558 default:
559 usage(stderr);
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 }