wdctl: default to /dev/watchdog0

Let's use miscdev /dev/watchdog as fallback only. We need (if possible)
cdev /dev/watchdog0 as this device has entry in /sys/class/watchdog.

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2019-05-31 14:49:23 +02:00
parent 5d628f37b7
commit 8c8df42146
2 changed files with 36 additions and 10 deletions

View File

@ -167,9 +167,6 @@
/* deprecated */ /* deprecated */
#define _PATH_RAWDEVCTL_OLD "/dev/rawctl" #define _PATH_RAWDEVCTL_OLD "/dev/rawctl"
/* wdctl path */
#define _PATH_WATCHDOG_DEV "/dev/watchdog"
/* ipc paths */ /* ipc paths */
#define _PATH_PROC_SYSV_MSG "/proc/sysvipc/msg" #define _PATH_PROC_SYSV_MSG "/proc/sysvipc/msg"
#define _PATH_PROC_SYSV_SEM "/proc/sysvipc/sem" #define _PATH_PROC_SYSV_SEM "/proc/sysvipc/sem"

View File

@ -105,7 +105,7 @@ static int columns[ARRAY_SIZE(infos) * 2];
static int ncolumns; static int ncolumns;
struct wd_device { struct wd_device {
char *devpath; const char *devpath;
int timeout; int timeout;
int timeleft; int timeleft;
@ -170,10 +170,32 @@ static struct colinfo *get_column_info(unsigned num)
return &infos[ get_column_id(num) ]; return &infos[ get_column_id(num) ];
} }
/* We preffer cdev /dev/watchdog0 as this device has node in
* /sys/class/watchdog/. The old miscdev /dev/watchdog is fallback for old
* systemds only.
*/
static const char *get_default_device(void)
{
const char **p;
static const char *devs[] = {
"/dev/watchdog0",
"/dev/watchdog",
NULL
};
for (p = devs; *p; p++) {
if (access(*p, F_OK) == 0)
return *p;
}
return NULL;
}
static void __attribute__((__noreturn__)) usage(void) static void __attribute__((__noreturn__)) usage(void)
{ {
FILE *out = stdout; FILE *out = stdout;
size_t i; size_t i;
const char *dflt = get_default_device();
fputs(USAGE_HEADER, out); fputs(USAGE_HEADER, out);
fprintf(out, fprintf(out,
@ -198,7 +220,10 @@ static void __attribute__((__noreturn__)) usage(void)
printf(USAGE_HELP_OPTIONS(24)); printf(USAGE_HELP_OPTIONS(24));
fputs(USAGE_SEPARATOR, out); fputs(USAGE_SEPARATOR, out);
fprintf(out, _("The default device is %s.\n"), _PATH_WATCHDOG_DEV); if (dflt)
fprintf(out, _("The default device is %s.\n"), dflt);
else
fprintf(out, _("No default device is available.\n"));
fputs(USAGE_COLUMNS, out); fputs(USAGE_COLUMNS, out);
for (i = 0; i < ARRAY_SIZE(infos); i++) for (i = 0; i < ARRAY_SIZE(infos); i++)
@ -507,6 +532,7 @@ int main(int argc, char *argv[])
int c, res = EXIT_SUCCESS, count = 0; int c, res = EXIT_SUCCESS, count = 0;
uint32_t wanted = 0; uint32_t wanted = 0;
int timeout = 0; int timeout = 0;
const char *dflt_device = NULL;
static const struct option long_opts[] = { static const struct option long_opts[] = {
{ "flags", required_argument, NULL, 'f' }, { "flags", required_argument, NULL, 'f' },
@ -595,15 +621,18 @@ int main(int argc, char *argv[])
columns[ncolumns++] = COL_BSTATUS; columns[ncolumns++] = COL_BSTATUS;
} }
/* Device no specified, use default. */
if (optind == argc) {
dflt_device = get_default_device();
if (!dflt_device)
err(EXIT_FAILURE, _("No default device is available."));
}
do { do {
int rc; int rc;
memset(&wd, 0, sizeof(wd)); memset(&wd, 0, sizeof(wd));
wd.devpath = dflt_device ? dflt_device : argv[optind++];
if (optind == argc)
wd.devpath = _PATH_WATCHDOG_DEV;
else
wd.devpath = argv[optind++];
if (count) if (count)
fputc('\n', stdout); fputc('\n', stdout);