};
struct wd_control {
+ /* set */
+ int timeout; /* --settimeout */
+ int pretimeout; /* --setpretimeout */
+ unsigned int set_timeout : 1,
+ set_pretimeout : 1;
+
+ /* output */
unsigned int show_oneline : 1,
show_raw : 1,
hide_headings : 1,
hide_timeouts : 1;
};
+#define want_set(_ctl) ((_ctl)->set_timeout || (_ctl)->set_pretimeout)
+
/* converts flag name to flag bit */
static long name2bit(const char *name, size_t namesz)
{
exit(EXIT_SUCCESS);
}
+static struct path_cxt *get_sysfs(struct wd_device *wd)
+{
+ struct path_cxt *sys;
+ struct stat st;
+
+ if (wd->no_sysfs)
+ return NULL;
+ if (wd->sysfs)
+ return wd->sysfs;
+ if (stat(wd->devpath, &st) != 0)
+ goto nosysfs;
+
+ sys = ul_new_path(_PATH_SYS_DEVCHAR "/%u:%u",
+ major(st.st_rdev), minor(st.st_rdev));
+ if (!sys)
+ return NULL;
+
+ if (ul_path_get_dirfd(sys) < 0)
+ goto nosysfs; /* device not in /sys */
+
+ if (ul_path_access(sys, F_OK, "identity") != 0)
+ goto nosysfs; /* no info in /sys (old miscdev?) */
+
+ wd->sysfs = sys;
+ return sys;
+nosysfs:
+ wd->no_sysfs = 1;
+ return NULL;
+}
+
static void add_flag_line(struct libscols_table *table, struct wd_device *wd, const struct wdflag *fl)
{
int i;
return rc;
}
-enum {
- WDCTL_SET_TIMEOUT = (1 << 1),
- WDCTL_SET_PRETIMEOUT = (1 << 2)
-};
-
/*
* Warning: successfully opened watchdog has to be properly closed with magic
* close character otherwise the machine will be rebooted!
*
* Don't use err() or exit() here!
*/
-static int set_watchdog(struct wd_device *wd, int timeout, int pretimeout,
- unsigned int flags)
+static int set_watchdog(struct wd_control *ctl, struct wd_device *wd)
{
int fd;
sigset_t sigs, oldsigs;
int rc = 0;
+ assert(wd);
assert(wd->devpath);
- assert(flags);
+ assert(ctl);
sigemptyset(&oldsigs);
sigfillset(&sigs);
* the machine might end up rebooting. */
}
- if ((flags & WDCTL_SET_TIMEOUT) &&
- ioctl(fd, WDIOC_SETTIMEOUT, &timeout) != 0) {
- rc += errno;
- warn(_("cannot set timeout for %s"), wd->devpath);
+ if (ctl->set_timeout) {
+ if (ioctl(fd, WDIOC_SETTIMEOUT, &ctl->timeout) != 0) {
+ rc += errno;
+ warn(_("cannot set timeout for %s"), wd->devpath);
+ } else
+ printf(P_("Timeout has been set to %d second.\n",
+ "Timeout has been set to %d seconds.\n",
+ ctl->timeout), ctl->timeout);
}
- if ((flags & WDCTL_SET_PRETIMEOUT) &&
- ioctl(fd, WDIOC_SETPRETIMEOUT, &pretimeout) != 0) {
- rc += errno;
- warn(_("cannot set pretimeout for %s"), wd->devpath);
+ if (ctl->set_pretimeout) {
+ if (ioctl(fd, WDIOC_SETPRETIMEOUT, &ctl->pretimeout) != 0) {
+ rc += errno;
+ warn(_("cannot set pretimeout for %s"), wd->devpath);
+ } else
+ printf(P_("Pre-timeout has been set to %d second.\n",
+ "Pre-timeout has been set to %d seconds.\n",
+ ctl->pretimeout), ctl->pretimeout);
}
if (close(fd))
warn(_("write failed"));
- sigprocmask(SIG_SETMASK, &oldsigs, NULL);
- printf(P_("Timeout has been set to %d second.\n",
- "Timeout has been set to %d seconds.\n", timeout), timeout);
+ sigprocmask(SIG_SETMASK, &oldsigs, NULL);
return rc;
}
return 0;
}
-static struct path_cxt *get_sysfs(struct wd_device *wd)
-{
- struct path_cxt *sys;
- struct stat st;
-
- if (wd->no_sysfs)
- return NULL;
- if (wd->sysfs)
- return wd->sysfs;
- if (stat(wd->devpath, &st) != 0)
- goto nosysfs;
-
- sys = ul_new_path(_PATH_SYS_DEVCHAR "/%u:%u",
- major(st.st_rdev), minor(st.st_rdev));
- if (!sys)
- return NULL;
-
- if (ul_path_get_dirfd(sys) < 0)
- goto nosysfs; /* device not in /sys */
-
- if (ul_path_access(sys, F_OK, "identity") != 0)
- goto nosysfs; /* no info in /sys (old miscdev?) */
-
- wd->sysfs = sys;
- return sys;
-nosysfs:
- wd->no_sysfs = 1;
- return NULL;
-}
/* Returns: <0 error, 0 success, 1 unssuported */
static int read_watchdog_from_sysfs(struct wd_device *wd)
}
read_governors(wd);
-
- ul_unref_path(wd->sysfs);
- wd->sysfs = NULL;
return 0;
}
struct wd_device wd;
struct wd_control ctl = { .hide_headings = 0 };
int c, res = EXIT_SUCCESS, count = 0;
- unsigned int wantset = 0;
uint32_t wanted = 0;
- int timeout = 0, pretimeout = 0;
const char *dflt_device = NULL;
static const struct option long_opts[] = {
return EXIT_FAILURE;
break;
case 's':
- timeout = strtos32_or_err(optarg, _("invalid timeout argument"));
- wantset |= WDCTL_SET_TIMEOUT;
+ ctl.timeout = strtos32_or_err(optarg, _("invalid timeout argument"));
+ ctl.set_timeout = 1;
break;
case 'p':
- pretimeout = strtos32_or_err(optarg, _("invalid pretimeout argument"));
- wantset |= WDCTL_SET_PRETIMEOUT;
+ ctl.pretimeout = strtos32_or_err(optarg, _("invalid pretimeout argument"));
+ ctl.set_pretimeout = 1;
break;
case 'f':
if (string_to_bitmask(optarg, (unsigned long *) &wanted, name2bit) != 0)
fputc('\n', stdout);
count++;
- if (wantset) {
- rc = set_watchdog(&wd, timeout, pretimeout, wantset);
+ if (want_set(&ctl)) {
+ rc = set_watchdog(&ctl, &wd);
if (rc) {
res = EXIT_FAILURE;
}
}
print_device(&ctl, &wd, wanted);
-
+ ul_unref_path(wd.sysfs);
} while (optind < argc);
return res;