From 8fb6a2de79540d85567c40e0e5e8d42a612f3af2 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 5 Apr 2012 23:31:41 +0200 Subject: wdctl: add watchdog status tool For: util-linux This patch adds a tiny tool "wdctl" which may be used to query the status of Linux watchdog devices (/dev/watchdog). This will simply query all fields that may be queried from the device and print them nicely formatted. Signed-off-by: Lennart Poettering --- sys-utils/wdctl.c | 180 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 sys-utils/wdctl.c (limited to 'sys-utils/wdctl.c') diff --git a/sys-utils/wdctl.c b/sys-utils/wdctl.c new file mode 100644 index 000000000..179aff207 --- /dev/null +++ b/sys-utils/wdctl.c @@ -0,0 +1,180 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: t -*-*/ + +/* + * wdctl(8) - show hardware watchdog status + * + * Copyright (C) 2012 Lennart Poettering + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include +#include + +#include "nls.h" +#include "c.h" + +static const struct { + uint32_t flag; + const char *name; +} flag_names[] = { + { WDIOF_OVERHEAT, N_("OVERHEAT: Reset due to CPU overheat") }, + { WDIOF_FANFAULT, N_("FANFAULT: Fan failed") }, + { WDIOF_EXTERN1, N_("EXTERN1: External relay 1") }, + { WDIOF_EXTERN2, N_("EXTERN2: External relay 2") }, + { WDIOF_POWERUNDER, N_("POWERUNDER: Power bad/power fault") }, + { WDIOF_CARDRESET, N_("CARDRESET: Card previously reset the CPU") }, + { WDIOF_POWEROVER, N_("POWEROVER: Power over voltage") }, + { WDIOF_SETTIMEOUT, N_("SETTIMEOUT: Set timeout (in seconds)") }, + { WDIOF_MAGICCLOSE, N_("MAGICCLOSE: Supports magic close char") }, + { WDIOF_PRETIMEOUT, N_("PRETIMEOUT: Pretimeout (in seconds)") }, + { WDIOF_KEEPALIVEPING, N_("KEEPALIVEPING: Keep alive ping reply") } +}; + +static void usage(int status) +{ + FILE *out = status == EXIT_SUCCESS ? stdout : stderr; + + fputs(USAGE_HEADER, out); + fprintf(out, + _(" %s [options]\n"), program_invocation_short_name); + + fputs(USAGE_OPTIONS, out); + fputs(_(" -d, --device= device to use (/dev/watchdog)\n"), out); + + fputs(USAGE_SEPARATOR, out); + fputs(USAGE_HELP, out); + fputs(USAGE_VERSION, out); + fprintf(out, USAGE_MAN_TAIL("wdctl(1)")); + + exit(status); +} + +static void print_options(uint32_t options) +{ + unsigned i; + + if (options == 0) { + puts(_("\tNo flags set.")); + return; + } + + for (i = 0; i < ARRAY_SIZE(flag_names); i++) { + if (options & flag_names[i].flag) + printf("\t%s\n", _(flag_names[i].name)); + options &= ~flag_names[i].flag; + } + + if (options) + printf(_("\tUnknown flags 0x%x\n"), options); +} + +int main(int argc, char *argv[]) +{ + static const struct option longopts[] = { + { "help", no_argument, 0, 'h' }, + { "version", no_argument, 0, 'V'}, + { "device", required_argument, 0, 'd' }, + { NULL, 0, 0, 0 } + }; + + int c, status, sec, fd; + const char *device = "/dev/watchdog"; + struct watchdog_info ident; + + setlocale(LC_MESSAGES, ""); + bindtextdomain(PACKAGE, LOCALEDIR); + textdomain(PACKAGE); + + while((c = getopt_long(argc, argv, "hVd:", longopts, NULL)) != -1) { + + switch(c) { + case 'h': + usage(EXIT_SUCCESS); + case 'V': + printf(UTIL_LINUX_VERSION); + return EXIT_SUCCESS; + case 'd': + device = optarg; + break; + default: + usage(EXIT_FAILURE); + } + } + + if (optind < argc) + usage(EXIT_FAILURE); + + fd = open(device, O_WRONLY|O_CLOEXEC); + if (fd < 0) { + if (errno == EBUSY) + errx(EXIT_FAILURE, _("Watchdog already in use, terminating.")); + + err(EXIT_FAILURE, _("Failed to open watchdog device")); + } + + if (ioctl(fd, WDIOC_GETSUPPORT, &ident) >= 0) { + printf(_("Identity:\n\t%s\n" + "Firmware version:\n\t%x\n" + "Supported options:\n"), + ident.identity, + ident.firmware_version); + print_options(ident.options); + } + + if (ioctl(fd, WDIOC_GETSTATUS, &status) >= 0) { + puts(_("Status:")); + print_options((uint32_t) status); + } + + if (ioctl(fd, WDIOC_GETBOOTSTATUS, &status) >= 0) { + puts(_("Boot status:")); + print_options((uint32_t) status); + } + + if (ioctl(fd, WDIOC_GETTIMEOUT, &sec) >= 0) + printf(_("Timeout:\n\t%is\n"), sec); + + if (ioctl(fd, WDIOC_GETPRETIMEOUT, &sec) >= 0) + printf(_("Pre-Timeout:\n\t%is\n"), sec); + + if (ioctl(fd, WDIOC_GETTIMELEFT, &sec) >= 0) + printf(_("Time Left:\n\t%is\n"), sec); + + for (;;) { + /* We just opened this to query the state, not to arm + * it hence use the magic close character */ + + static const char v = 'V'; + + if (write(fd, &v, 1) >= 0) + break; + + if (errno != EINTR) { + warn(_("Failed to disarm watchdog")); + break; + } + + /* Let's try hard, since if we don't get this right + * the machine might end up rebooting. */ + } + + close(fd); + + return 0; +} -- cgit v1.2.3-55-g7522