]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/wdctl.c
wdctl: gettextize one forgotten word
[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 "nls.h"
30 #include "c.h"
31 #include "closestream.h"
32 #include "optutils.h"
33 #include "pathnames.h"
34 #include "strutils.h"
35 #include "tt.h"
36
37 struct wdflag {
38 uint32_t flag;
39 const char *name;
40 const char *description;
41 };
42
43 static const struct wdflag wdflags[] = {
44 { WDIOF_CARDRESET, "CARDRESET", N_("Card previously reset the CPU") },
45 { WDIOF_EXTERN1, "EXTERN1", N_("External relay 1") },
46 { WDIOF_EXTERN2, "EXTERN2", N_("External relay 2") },
47 { WDIOF_FANFAULT, "FANFAULT", N_("Fan failed") },
48 { WDIOF_KEEPALIVEPING, "KEEPALIVEPING", N_("Keep alive ping reply") },
49 { WDIOF_MAGICCLOSE, "MAGICCLOSE", N_("Supports magic close char") },
50 { WDIOF_OVERHEAT, "OVERHEAT", N_("Reset due to CPU overheat") },
51 { WDIOF_POWEROVER, "POWEROVER", N_("Power over voltage") },
52 { WDIOF_POWERUNDER, "POWERUNDER", N_("Power bad/power fault") },
53 { WDIOF_PRETIMEOUT, "PRETIMEOUT", N_("Pretimeout (in seconds)") },
54 { WDIOF_SETTIMEOUT, "SETTIMEOUT", N_("Set timeout (in seconds)") }
55 };
56
57
58 /* column names */
59 struct colinfo {
60 const char *name; /* header */
61 double whint; /* width hint (N < 1 is in percent of termwidth) */
62 int flags; /* TT_FL_* */
63 const char *help;
64 };
65
66 enum { COL_FLAG, COL_DESC, COL_STATUS, COL_BSTATUS, COL_DEVICE };
67
68 /* columns descriptions */
69 static struct colinfo infos[] = {
70 [COL_FLAG] = { "FLAG", 14, 0, N_("flag name") },
71 [COL_DESC] = { "DESCRIPTION", 0.1, TT_FL_TRUNC, N_("flag description") },
72 [COL_STATUS] = { "STATUS", 1, TT_FL_RIGHT, N_("flag status") },
73 [COL_BSTATUS] = { "BOOT-STATUS", 1, TT_FL_RIGHT, N_("flag boot status") },
74 [COL_DEVICE] = { "DEVICE", 0.1, 0, N_("watchdog device name") }
75
76 };
77
78 #define NCOLS ARRAY_SIZE(infos)
79 static int columns[NCOLS], ncolumns;
80
81 struct wdinfo {
82 char *device;
83
84 int timeout;
85 int timeleft;
86 int pretimeout;
87
88 uint32_t status;
89 uint32_t bstatus;
90
91 struct watchdog_info ident;
92
93 unsigned int has_timeout : 1,
94 has_timeleft : 1,
95 has_pretimeout : 1;
96 };
97
98 /* converts flag name to flag bit */
99 static long name2bit(const char *name, size_t namesz)
100 {
101 size_t i;
102
103 for (i = 0; i < ARRAY_SIZE(wdflags); i++) {
104 const char *cn = wdflags[i].name;
105 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
106 return wdflags[i].flag;
107 }
108 warnx(_("unknown flag: %s"), name);
109 return -1;
110 }
111
112 static int column2id(const char *name, size_t namesz)
113 {
114 size_t i;
115
116 for (i = 0; i < NCOLS; i++) {
117 const char *cn = infos[i].name;
118 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
119 return i;
120 }
121 warnx(_("unknown column: %s"), name);
122 return -1;
123 }
124
125 static int get_column_id(int num)
126 {
127 assert(ARRAY_SIZE(columns) == NCOLS);
128 assert(num < ncolumns);
129 assert(columns[num] < (int) NCOLS);
130
131 return columns[num];
132 }
133
134 static struct colinfo *get_column_info(unsigned num)
135 {
136 return &infos[ get_column_id(num) ];
137 }
138
139 static void usage(FILE *out)
140 {
141 size_t i;
142
143 fputs(USAGE_HEADER, out);
144 fprintf(out,
145 _(" %s [options] [<device> ...]\n"), program_invocation_short_name);
146
147 fputs(USAGE_OPTIONS, out);
148
149 fputs(_(" -f, --flags <list> print selected flags only\n"
150 " -F, --noflags don't print information about flags\n"
151 " -I, --noident don't print watchdog identity information\n"
152 " -n, --noheadings don't print headings for flags table\n"
153 " -O, --oneline print all information on one line\n"
154 " -o, --output <list> output columns of the flags\n"
155 " -r, --raw use raw output format for flags table\n"
156 " -T, --notimeouts don't print watchdog timeouts\n"
157 " -s, --settimeout <sec> set watchdog timeout\n"
158 " -x, --flags-only print only flags table (same as -I -T)\n"), out);
159
160 fputs(USAGE_SEPARATOR, out);
161 fputs(USAGE_HELP, out);
162 fputs(USAGE_VERSION, out);
163 fputs(USAGE_SEPARATOR, out);
164
165 fprintf(out, _("The default device is %s.\n"), _PATH_WATCHDOG_DEV);
166 fputs(USAGE_SEPARATOR, out);
167
168 fputs(_("Available columns:\n"), out);
169 for (i = 0; i < ARRAY_SIZE(infos); i++)
170 fprintf(out, " %13s %s\n", infos[i].name, _(infos[i].help));
171
172 fprintf(out, USAGE_MAN_TAIL("wdctl(8)"));
173
174 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
175 }
176
177 static void add_flag_line(struct tt *tt, struct wdinfo *wd, const struct wdflag *fl)
178 {
179 int i;
180 struct tt_line *line;
181
182 line = tt_add_line(tt, NULL);
183 if (!line) {
184 warn(_("failed to add line to output"));
185 return;
186 }
187
188 for (i = 0; i < ncolumns; i++) {
189 const char *str = NULL;
190
191 switch (get_column_id(i)) {
192 case COL_FLAG:
193 str = fl->name;
194 break;
195 case COL_DESC:
196 str = fl->description;
197 break;
198 case COL_STATUS:
199 str = wd->status & fl->flag ? "1" : "0";
200 break;
201 case COL_BSTATUS:
202 str = wd->bstatus & fl->flag ? "1" : "0";
203 break;
204 case COL_DEVICE:
205 str = wd->device;
206 break;
207 default:
208 break;
209 }
210
211 if (str)
212 tt_line_set_data(line, i, str);
213 }
214 }
215
216 static int show_flags(struct wdinfo *wd, int tt_flags, uint32_t wanted)
217 {
218 size_t i;
219 int rc = -1;
220 struct tt *tt;
221 uint32_t flags;
222
223 /* create output table */
224 tt = tt_new_table(tt_flags);
225 if (!tt) {
226 warn(_("failed to initialize output table"));
227 return -1;
228 }
229
230 /* define columns */
231 for (i = 0; i < (size_t) ncolumns; i++) {
232 struct colinfo *col = get_column_info(i);
233
234 if (!tt_define_column(tt, col->name, col->whint, col->flags)) {
235 warnx(_("failed to initialize output column"));
236 goto done;
237 }
238 }
239
240 /* fill-in table with data
241 * -- one line for each supported flag (option) */
242 flags = wd->ident.options;
243
244 for (i = 0; i < ARRAY_SIZE(wdflags); i++) {
245 if (wanted && !(wanted & wdflags[i].flag))
246 ; /* ignore */
247 else if (flags & wdflags[i].flag)
248 add_flag_line(tt, wd, &wdflags[i]);
249
250 flags &= ~wdflags[i].flag;
251 }
252
253 if (flags)
254 warnx(_("%s: unknown flags 0x%x\n"), wd->device, flags);
255
256 tt_print_table(tt);
257 rc = 0;
258 done:
259 tt_free_table(tt);
260 return rc;
261 }
262 /*
263 * Warning: successfully opened watchdog has to be properly closed with magic
264 * close character otherwise the machine will be rebooted!
265 *
266 * Don't use err() or exit() here!
267 */
268 static int set_watchdog(struct wdinfo *wd, int timeout)
269 {
270 int fd;
271 sigset_t sigs, oldsigs;
272 int rc = 0;
273
274 assert(wd->device);
275
276 sigemptyset(&oldsigs);
277 sigfillset(&sigs);
278 sigprocmask(SIG_BLOCK, &sigs, &oldsigs);
279
280 fd = open(wd->device, O_WRONLY|O_CLOEXEC);
281
282 if (fd < 0) {
283 if (errno == EBUSY)
284 warnx(_("%s: watchdog already in use, terminating."),
285 wd->device);
286 warn(_("cannot open %s"), wd->device);
287 return -1;
288 }
289
290 for (;;) {
291 /* We just opened this to query the state, not to arm
292 * it hence use the magic close character */
293 static const char v = 'V';
294
295 if (write(fd, &v, 1) >= 0)
296 break;
297 if (errno != EINTR) {
298 warn(_("%s: failed to disarm watchdog"), wd->device);
299 break;
300 }
301 /* Let's try hard, since if we don't get this right
302 * the machine might end up rebooting. */
303 }
304
305 if (ioctl(fd, WDIOC_SETTIMEOUT, &timeout) != 0) {
306 rc = errno;
307 warn(_("cannot set timeout for %s"), wd->device);
308 }
309
310 close(fd);
311 sigprocmask(SIG_SETMASK, &oldsigs, NULL);
312 printf("Set timeout to %d seconds\n", timeout);
313
314 return rc;
315 }
316
317 /*
318 * Warning: successfully opened watchdog has to be properly closed with magic
319 * close character otherwise the machine will be rebooted!
320 *
321 * Don't use err() or exit() here!
322 */
323 static int read_watchdog(struct wdinfo *wd)
324 {
325 int fd;
326 sigset_t sigs, oldsigs;
327
328 assert(wd->device);
329
330 sigemptyset(&oldsigs);
331 sigfillset(&sigs);
332 sigprocmask(SIG_BLOCK, &sigs, &oldsigs);
333
334 fd = open(wd->device, O_WRONLY|O_CLOEXEC);
335
336 if (fd < 0) {
337 if (errno == EBUSY)
338 warnx(_("%s: watchdog already in use, terminating."),
339 wd->device);
340 warn(_("cannot open %s"), wd->device);
341 return -1;
342 }
343
344 if (ioctl(fd, WDIOC_GETSUPPORT, &wd->ident) < 0)
345 warn(_("%s: failed to get information about watchdog"), wd->device);
346 else {
347 ioctl(fd, WDIOC_GETSTATUS, &wd->status);
348 ioctl(fd, WDIOC_GETBOOTSTATUS, &wd->bstatus);
349
350 if (ioctl(fd, WDIOC_GETTIMEOUT, &wd->timeout) >= 0)
351 wd->has_timeout = 1;
352 if (ioctl(fd, WDIOC_GETPRETIMEOUT, &wd->pretimeout) >= 0)
353 wd->has_pretimeout = 1;
354 if (ioctl(fd, WDIOC_GETTIMELEFT, &wd->timeleft) >= 0)
355 wd->has_timeleft = 1;
356 }
357
358 for (;;) {
359 /* We just opened this to query the state, not to arm
360 * it hence use the magic close character */
361 static const char v = 'V';
362
363 if (write(fd, &v, 1) >= 0)
364 break;
365 if (errno != EINTR) {
366 warn(_("%s: failed to disarm watchdog"), wd->device);
367 break;
368 }
369 /* Let's try hard, since if we don't get this right
370 * the machine might end up rebooting. */
371 }
372
373 close(fd);
374 sigprocmask(SIG_SETMASK, &oldsigs, NULL);
375
376 return 0;
377 }
378
379 static void print_oneline(struct wdinfo *wd, uint32_t wanted,
380 int noident, int notimeouts, int noflags)
381 {
382 printf("%s:", wd->device);
383
384 if (!noident) {
385 printf(" VERSION=\"%x\"", wd->ident.firmware_version);
386
387 printf(" IDENTITY=");
388 tt_fputs_quoted((char *) wd->ident.identity, stdout);
389 }
390 if (!notimeouts) {
391 if (wd->has_timeout)
392 printf(" TIMEOUT=\"%i\"", wd->timeout);
393 if (wd->has_pretimeout)
394 printf(" PRETIMEOUT=\"%i\"", wd->pretimeout);
395 if (wd->has_timeleft)
396 printf(" TIMELEFT=\"%i\"", wd->timeleft);
397 }
398
399 if (!noflags) {
400 size_t i;
401 uint32_t flags = wd->ident.options;
402
403 for (i = 0; i < ARRAY_SIZE(wdflags); i++) {
404 const struct wdflag *fl;
405
406 if ((wanted && !(wanted & wdflags[i].flag)) ||
407 !(flags & wdflags[i].flag))
408 continue;
409
410 fl= &wdflags[i];
411
412 printf(" %s=\"%s\"", fl->name,
413 wd->status & fl->flag ? "1" : "0");
414 printf(" %s_BOOT=\"%s\"", fl->name,
415 wd->bstatus & fl->flag ? "1" : "0");
416
417 }
418 }
419
420 fputc('\n', stdout);
421 }
422
423 static void show_timeouts(struct wdinfo *wd)
424 {
425 if (wd->has_timeout)
426 printf(_("%-15s%2i seconds\n"), _("Timeout:"), wd->timeout);
427 if (wd->has_pretimeout)
428 printf(_("%-15s%2i seconds\n"), _("Pre-timeout:"), wd->pretimeout);
429 if (wd->has_timeleft)
430 printf(_("%-15s%2i seconds\n"), _("Timeleft:"), wd->timeleft);
431 }
432
433 int main(int argc, char *argv[])
434 {
435 struct wdinfo wd;
436 int c, tt_flags = 0, res = EXIT_SUCCESS, count = 0;
437 char noflags = 0, noident = 0, notimeouts = 0, oneline = 0;
438 uint32_t wanted = 0;
439 int timeout = 0;
440
441 static const struct option long_opts[] = {
442 { "flags", required_argument, NULL, 'f' },
443 { "flags-only", no_argument, NULL, 'x' },
444 { "help", no_argument, NULL, 'h' },
445 { "noflags", no_argument, NULL, 'F' },
446 { "noheadings", no_argument, NULL, 'n' },
447 { "noident", no_argument, NULL, 'I' },
448 { "notimeouts", no_argument, NULL, 'T' },
449 { "settimeout", required_argument, NULL, 's' },
450 { "output", required_argument, NULL, 'o' },
451 { "oneline", no_argument, NULL, 'O' },
452 { "raw", no_argument, NULL, 'r' },
453 { "version", no_argument, NULL, 'V' },
454 { NULL, 0, NULL, 0 }
455 };
456
457 static const ul_excl_t excl[] = { /* rows and cols in in ASCII order */
458 { 'F','f' }, /* noflags,flags*/
459 { 0 }
460 };
461 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
462
463 setlocale(LC_ALL, "");
464 bindtextdomain(PACKAGE, LOCALEDIR);
465 textdomain(PACKAGE);
466 atexit(close_stdout);
467
468 while ((c = getopt_long(argc, argv,
469 "d:f:hFnITo:s:OrVx", long_opts, NULL)) != -1) {
470
471 err_exclusive_options(c, long_opts, excl, excl_st);
472
473 switch(c) {
474 case 'o':
475 ncolumns = string_to_idarray(optarg,
476 columns, ARRAY_SIZE(columns),
477 column2id);
478 if (ncolumns < 0)
479 return EXIT_FAILURE;
480 break;
481 case 's':
482 timeout = strtos32_or_err(optarg, _("invalid timeout argument"));
483 break;
484 case 'f':
485 if (string_to_bitmask(optarg, (unsigned long *) &wanted, name2bit) != 0)
486 return EXIT_FAILURE;
487 break;
488 case 'V':
489 printf(UTIL_LINUX_VERSION);
490 return EXIT_SUCCESS;
491 case 'h':
492 usage(stdout);
493 case 'F':
494 noflags = 1;
495 break;
496 case 'I':
497 noident = 1;
498 break;
499 case 'T':
500 notimeouts = 1;
501 break;
502 case 'n':
503 tt_flags |= TT_FL_NOHEADINGS;
504 break;
505 case 'r':
506 tt_flags |= TT_FL_RAW;
507 break;
508 case 'O':
509 oneline = 1;
510 break;
511 case 'x':
512 noident = 1;
513 notimeouts = 1;
514 break;
515
516 case '?':
517 default:
518 usage(stderr);
519 }
520 }
521
522 if (!ncolumns) {
523 /* default columns */
524 columns[ncolumns++] = COL_FLAG;
525 columns[ncolumns++] = COL_DESC;
526 columns[ncolumns++] = COL_STATUS;
527 columns[ncolumns++] = COL_BSTATUS;
528 }
529
530 do {
531 int rc;
532
533 memset(&wd, 0, sizeof(wd));
534
535 if (optind == argc)
536 wd.device = _PATH_WATCHDOG_DEV;
537 else
538 wd.device = argv[optind++];
539
540 if (count)
541 fputc('\n', stdout);
542 count++;
543
544 if (timeout) {
545 rc = set_watchdog(&wd, timeout);
546 if (rc) {
547 res = EXIT_FAILURE;
548 }
549 }
550
551 rc = read_watchdog(&wd);
552 if (rc) {
553 res = EXIT_FAILURE;
554 continue;
555 }
556
557 if (oneline) {
558 print_oneline(&wd, wanted, noident, notimeouts, noflags);
559 continue;
560 }
561
562 /* pretty output */
563 if (!noident) {
564 printf("%-15s%s\n", _("Device:"), wd.device);
565 printf(_("%-15s%s [version %x]\n"),
566 _("Identity:"),
567 wd.ident.identity,
568 wd.ident.firmware_version);
569 }
570 if (!notimeouts)
571 show_timeouts(&wd);
572 if (!noflags)
573 show_flags(&wd, tt_flags, wanted);
574 } while (optind < argc);
575
576 return res;
577 }