]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - sys-utils/wdctl.c
wdctl: remove printing from main()
[thirdparty/util-linux.git] / sys-utils / wdctl.c
index 33012dca36a6e24251755f331a46b843412596ea..b4f2c6b56e75db9ddaca6517a3a0f9b293e8d8b1 100644 (file)
                                           other external alarm not a reboot */
 #endif
 
-/* basic output flags */
-static int no_headings;
-static int raw;
-
 struct wdflag {
        uint32_t        flag;
        const char      *name;
@@ -108,8 +104,8 @@ static struct colinfo infos[] = {
 static int columns[ARRAY_SIZE(infos) * 2];
 static int ncolumns;
 
-struct wdinfo {
-       char            *device;
+struct wd_device {
+       char            *devpath;
 
        int             timeout;
        int             timeleft;
@@ -125,6 +121,15 @@ struct wdinfo {
                        has_pretimeout : 1;
 };
 
+struct wd_control {
+       unsigned int    show_oneline : 1,
+                       show_raw : 1,
+                       hide_headings : 1,
+                       hide_flags : 1,
+                       hide_ident : 1,
+                       hide_timeouts : 1;
+};
+
 /* converts flag name to flag bit */
 static long name2bit(const char *name, size_t namesz)
 {
@@ -204,7 +209,7 @@ static void __attribute__((__noreturn__)) usage(void)
        exit(EXIT_SUCCESS);
 }
 
-static void add_flag_line(struct libscols_table *table, struct wdinfo *wd, const struct wdflag *fl)
+static void add_flag_line(struct libscols_table *table, struct wd_device *wd, const struct wdflag *fl)
 {
        int i;
        struct libscols_line *line;
@@ -232,7 +237,7 @@ static void add_flag_line(struct libscols_table *table, struct wdinfo *wd, const
                        str = wd->bstatus & fl->flag ? "1" : "0";
                        break;
                case COL_DEVICE:
-                       str = wd->device;
+                       str = wd->devpath;
                        break;
                default:
                        break;
@@ -245,7 +250,7 @@ static void add_flag_line(struct libscols_table *table, struct wdinfo *wd, const
        }
 }
 
-static int show_flags(struct wdinfo *wd, uint32_t wanted)
+static int show_flags(struct wd_control *ctl, struct wd_device *wd, uint32_t wanted)
 {
        size_t i;
        int rc = -1;
@@ -260,8 +265,8 @@ static int show_flags(struct wdinfo *wd, uint32_t wanted)
                warn(_("failed to allocate output table"));
                return -1;
        }
-       scols_table_enable_raw(table, raw);
-       scols_table_enable_noheadings(table, no_headings);
+       scols_table_enable_raw(table, ctl->show_raw);
+       scols_table_enable_noheadings(table, ctl->hide_headings);
 
        /* define columns */
        for (i = 0; i < (size_t) ncolumns; i++) {
@@ -287,7 +292,7 @@ static int show_flags(struct wdinfo *wd, uint32_t wanted)
        }
 
        if (flags)
-               warnx(_("%s: unknown flags 0x%x\n"), wd->device, flags);
+               warnx(_("%s: unknown flags 0x%x\n"), wd->devpath, flags);
 
        scols_print_table(table);
        rc = 0;
@@ -301,25 +306,25 @@ done:
  *
  * Don't use err() or exit() here!
  */
-static int set_watchdog(struct wdinfo *wd, int timeout)
+static int set_watchdog(struct wd_device *wd, int timeout)
 {
        int fd;
        sigset_t sigs, oldsigs;
        int rc = 0;
 
-       assert(wd->device);
+       assert(wd->devpath);
 
        sigemptyset(&oldsigs);
        sigfillset(&sigs);
        sigprocmask(SIG_BLOCK, &sigs, &oldsigs);
 
-       fd = open(wd->device, O_WRONLY|O_CLOEXEC);
+       fd = open(wd->devpath, O_WRONLY|O_CLOEXEC);
 
        if (fd < 0) {
                if (errno == EBUSY)
                        warnx(_("%s: watchdog already in use, terminating."),
-                                       wd->device);
-               warn(_("cannot open %s"), wd->device);
+                                       wd->devpath);
+               warn(_("cannot open %s"), wd->devpath);
                return -1;
        }
 
@@ -331,7 +336,7 @@ static int set_watchdog(struct wdinfo *wd, int timeout)
                if (write(fd, &v, 1) >= 0)
                        break;
                if (errno != EINTR) {
-                       warn(_("%s: failed to disarm watchdog"), wd->device);
+                       warn(_("%s: failed to disarm watchdog"), wd->devpath);
                        break;
                }
                /* Let's try hard, since if we don't get this right
@@ -340,10 +345,10 @@ static int set_watchdog(struct wdinfo *wd, int timeout)
 
        if (ioctl(fd, WDIOC_SETTIMEOUT, &timeout) != 0) {
                rc = errno;
-               warn(_("cannot set timeout for %s"), wd->device);
+               warn(_("cannot set timeout for %s"), wd->devpath);
        }
 
-       if (close_fd(fd))
+       if (close(fd))
                warn(_("write failed"));
        sigprocmask(SIG_SETMASK, &oldsigs, NULL);
        printf(P_("Timeout has been set to %d second.\n",
@@ -358,29 +363,29 @@ static int set_watchdog(struct wdinfo *wd, int timeout)
  *
  * Don't use err() or exit() here!
  */
-static int read_watchdog(struct wdinfo *wd)
+static int read_watchdog(struct wd_device *wd)
 {
        int fd;
        sigset_t sigs, oldsigs;
 
-       assert(wd->device);
+       assert(wd->devpath);
 
        sigemptyset(&oldsigs);
        sigfillset(&sigs);
        sigprocmask(SIG_BLOCK, &sigs, &oldsigs);
 
-       fd = open(wd->device, O_WRONLY|O_CLOEXEC);
+       fd = open(wd->devpath, O_WRONLY|O_CLOEXEC);
 
        if (fd < 0) {
                if (errno == EBUSY)
                        warnx(_("%s: watchdog already in use, terminating."),
-                                       wd->device);
-               warn(_("cannot open %s"), wd->device);
+                                       wd->devpath);
+               warn(_("cannot open %s"), wd->devpath);
                return -1;
        }
 
        if (ioctl(fd, WDIOC_GETSUPPORT, &wd->ident) < 0)
-               warn(_("%s: failed to get information about watchdog"), wd->device);
+               warn(_("%s: failed to get information about watchdog"), wd->devpath);
        else {
                ioctl(fd, WDIOC_GETSTATUS, &wd->status);
                ioctl(fd, WDIOC_GETBOOTSTATUS, &wd->bstatus);
@@ -401,32 +406,44 @@ static int read_watchdog(struct wdinfo *wd)
                if (write(fd, &v, 1) >= 0)
                        break;
                if (errno != EINTR) {
-                       warn(_("%s: failed to disarm watchdog"), wd->device);
+                       warn(_("%s: failed to disarm watchdog"), wd->devpath);
                        break;
                }
                /* Let's try hard, since if we don't get this right
                 * the machine might end up rebooting. */
        }
 
-       if (close_fd(fd))
+       if (close(fd))
                warn(_("write failed"));
        sigprocmask(SIG_SETMASK, &oldsigs, NULL);
 
        return 0;
 }
 
-static void print_oneline(struct wdinfo *wd, uint32_t wanted,
-               int noident, int notimeouts, int noflags)
+static void show_timeouts(struct wd_device *wd)
+{
+       if (wd->has_timeout)
+               printf(P_("%-14s %2i second\n", "%-14s %2i seconds\n", wd->timeout),
+                         _("Timeout:"), wd->timeout);
+       if (wd->has_pretimeout)
+               printf(P_("%-14s %2i second\n", "%-14s %2i seconds\n", wd->pretimeout),
+                         _("Pre-timeout:"), wd->pretimeout);
+       if (wd->has_timeleft)
+               printf(P_("%-14s %2i second\n", "%-14s %2i seconds\n", wd->timeleft),
+                         _("Timeleft:"), wd->timeleft);
+}
+
+static void print_oneline(struct wd_control *ctl, struct wd_device *wd, uint32_t wanted)
 {
-       printf("%s:", wd->device);
+       printf("%s:", wd->devpath);
 
-       if (!noident) {
+       if (!ctl->hide_ident) {
                printf(" VERSION=\"%x\"", wd->ident.firmware_version);
 
                printf(" IDENTITY=");
                fputs_quoted((char *) wd->ident.identity, stdout);
        }
-       if (!notimeouts) {
+       if (!ctl->hide_timeouts) {
                if (wd->has_timeout)
                        printf(" TIMEOUT=\"%i\"", wd->timeout);
                if (wd->has_pretimeout)
@@ -435,7 +452,7 @@ static void print_oneline(struct wdinfo *wd, uint32_t wanted,
                        printf(" TIMELEFT=\"%i\"", wd->timeleft);
        }
 
-       if (!noflags) {
+       if (!ctl->hide_flags) {
                size_t i;
                uint32_t flags = wd->ident.options;
 
@@ -459,24 +476,35 @@ static void print_oneline(struct wdinfo *wd, uint32_t wanted,
        fputc('\n', stdout);
 }
 
-static void show_timeouts(struct wdinfo *wd)
+static void print_device(struct wd_control *ctl, struct wd_device *wd, uint32_t wanted)
 {
-       if (wd->has_timeout)
-               printf(P_("%-14s %2i second\n", "%-14s %2i seconds\n", wd->timeout),
-                         _("Timeout:"), wd->timeout);
-       if (wd->has_pretimeout)
-               printf(P_("%-14s %2i second\n", "%-14s %2i seconds\n", wd->pretimeout),
-                         _("Pre-timeout:"), wd->pretimeout);
-       if (wd->has_timeleft)
-               printf(P_("%-14s %2i second\n", "%-14s %2i seconds\n", wd->timeleft),
-                         _("Timeleft:"), wd->timeleft);
+       /* NAME=value one line output */
+       if (ctl->show_oneline) {
+               print_oneline(ctl, wd, wanted);
+               return;
+       }
+
+       /* pretty output */
+       if (!ctl->hide_ident) {
+               printf("%-15s%s\n", _("Device:"), wd->devpath);
+               printf("%-15s%s [%s %x]\n",
+                               _("Identity:"),
+                               wd->ident.identity,
+                               _("version"),
+                               wd->ident.firmware_version);
+       }
+       if (!ctl->hide_timeouts)
+               show_timeouts(wd);
+
+       if (!ctl->hide_flags)
+               show_flags(ctl, wd, wanted);
 }
 
 int main(int argc, char *argv[])
 {
-       struct wdinfo wd;
+       struct wd_device wd;
+       struct wd_control ctl = { .hide_headings = 0 };
        int c, res = EXIT_SUCCESS, count = 0;
-       char noflags = 0, noident = 0, notimeouts = 0, oneline = 0;
        uint32_t wanted = 0;
        int timeout = 0;
 
@@ -505,7 +533,7 @@ int main(int argc, char *argv[])
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        while ((c = getopt_long(argc, argv,
                                "d:f:hFnITo:s:OrVx", long_opts, NULL)) != -1) {
@@ -527,33 +555,33 @@ int main(int argc, char *argv[])
                        if (string_to_bitmask(optarg, (unsigned long *) &wanted, name2bit) != 0)
                                return EXIT_FAILURE;
                        break;
-               case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       return EXIT_SUCCESS;
-               case 'h':
-                       usage();
                case 'F':
-                       noflags = 1;
+                       ctl.hide_flags = 1;
                        break;
                case 'I':
-                       noident = 1;
+                       ctl.hide_ident = 1;
                        break;
                case 'T':
-                       notimeouts = 1;
+                       ctl.hide_timeouts = 1;
                        break;
                case 'n':
-                       no_headings = 1;
+                       ctl.hide_headings = 1;
                        break;
                case 'r':
-                       raw = 1;
+                       ctl.show_raw = 1;
                        break;
                case 'O':
-                       oneline = 1;
+                       ctl.show_oneline = 1;
                        break;
                case 'x':
-                       noident = 1;
-                       notimeouts = 1;
+                       ctl.hide_ident = 1;
+                       ctl.hide_timeouts = 1;
                        break;
+
+               case 'h':
+                       usage();
+               case 'V':
+                       print_version(EXIT_SUCCESS);
                default:
                        errtryhelp(EXIT_FAILURE);
                }
@@ -573,9 +601,9 @@ int main(int argc, char *argv[])
                memset(&wd, 0, sizeof(wd));
 
                if (optind == argc)
-                       wd.device = _PATH_WATCHDOG_DEV;
+                       wd.devpath = _PATH_WATCHDOG_DEV;
                else
-                       wd.device = argv[optind++];
+                       wd.devpath = argv[optind++];
 
                if (count)
                        fputc('\n', stdout);
@@ -594,24 +622,8 @@ int main(int argc, char *argv[])
                        continue;
                }
 
-               if (oneline) {
-                       print_oneline(&wd, wanted, noident, notimeouts, noflags);
-                       continue;
-               }
+               print_device(&ctl, &wd, wanted);
 
-               /* pretty output */
-               if (!noident) {
-                       printf("%-15s%s\n", _("Device:"), wd.device);
-                       printf("%-15s%s [%s %x]\n",
-                                       _("Identity:"),
-                                       wd.ident.identity,
-                                       _("version"),
-                                       wd.ident.firmware_version);
-               }
-               if (!notimeouts)
-                       show_timeouts(&wd);
-               if (!noflags)
-                       show_flags(&wd, wanted);
        } while (optind < argc);
 
        return res;