]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
uuidd: add command-line option values struct
authorSami Kerola <kerolasa@iki.fi>
Fri, 20 Nov 2020 22:17:22 +0000 (22:17 +0000)
committerSami Kerola <kerolasa@iki.fi>
Mon, 23 Nov 2020 21:34:40 +0000 (21:34 +0000)
The struct will help seeing where the variable values are coming from.  This
will also help upcoming refactoring.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
misc-utils/uuidd.c

index 8328bd57e7978f3d8295b9f4acb74c1b42e6ab8c..a915455345fd8966a9c6fb0ebce0f13c1e76863d 100644 (file)
@@ -55,6 +55,16 @@ struct uuidd_cxt_t {
                        no_sock: 1;
 };
 
+struct uuidd_options_t {
+       const char      *pidfile_path;
+       const char      *socket_path;
+       int             num;
+       int             do_type;
+       unsigned int    do_kill:1,
+                       no_pid:1,
+                       s_flag:1;
+};
+
 static void __attribute__((__noreturn__)) usage(void)
 {
        FILE *out = stdout;
@@ -534,18 +544,12 @@ static void __attribute__ ((__noreturn__)) unexpected_size(int size)
 
 int main(int argc, char **argv)
 {
-       const char      *socket_path = UUIDD_SOCKET_PATH;
-       const char      *pidfile_path = NULL;
        const char      *err_context = NULL;
-       char            buf[1024], *cp;
-       char            str[UUID_STR_LEN];
-       uuid_t          uu;
-       int             i, c, ret;
-       int             do_type = 0, do_kill = 0, num = 0;
-       int             no_pid = 0;
-       int             s_flag = 0;
+       char            *cp;
+       int             c, ret;
 
        struct uuidd_cxt_t uuidd_cxt = { .timeout = 0 };
+       struct uuidd_options_t uuidd_opts = { .socket_path = UUIDD_SOCKET_PATH };
 
        static const struct option longopts[] = {
                {"pid", required_argument, NULL, 'p'},
@@ -586,17 +590,17 @@ int main(int argc, char **argv)
                        uuidd_cxt.debug = 1;
                        break;
                case 'k':
-                       do_kill = 1;
+                       uuidd_opts.do_kill = 1;
                        break;
                case 'n':
-                       num = strtou32_or_err(optarg,
+                       uuidd_opts.num = strtou32_or_err(optarg,
                                                _("failed to parse --uuids"));
                        break;
                case 'p':
-                       pidfile_path = optarg;
+                       uuidd_opts.pidfile_path = optarg;
                        break;
                case 'P':
-                       no_pid = 1;
+                       uuidd_opts.no_pid = 1;
                        break;
                case 'F':
                        uuidd_cxt.no_fork = 1;
@@ -605,7 +609,7 @@ int main(int argc, char **argv)
 #ifdef HAVE_LIBSYSTEMD
                        uuidd_cxt.no_sock = 1;
                        uuidd_cxt.no_fork = 1;
-                       no_pid = 1;
+                       uuidd_opts.no_pid = 1;
 #else
                        errx(EXIT_FAILURE, _("uuidd has been built without "
                                             "support for socket activation"));
@@ -615,14 +619,14 @@ int main(int argc, char **argv)
                        uuidd_cxt.quiet = 1;
                        break;
                case 'r':
-                       do_type = UUIDD_OP_RANDOM_UUID;
+                       uuidd_opts.do_type = UUIDD_OP_RANDOM_UUID;
                        break;
                case 's':
-                       socket_path = optarg;
-                       s_flag = 1;
+                       uuidd_opts.socket_path = optarg;
+                       uuidd_opts.s_flag = 1;
                        break;
                case 't':
-                       do_type = UUIDD_OP_TIME_UUID;
+                       uuidd_opts.do_type = UUIDD_OP_TIME_UUID;
                        break;
                case 'T':
                        uuidd_cxt.timeout = strtou32_or_err(optarg,
@@ -638,48 +642,59 @@ int main(int argc, char **argv)
                }
        }
 
-       if (strlen(socket_path) >= sizeof(((struct sockaddr_un *)0)->sun_path))
-               errx(EXIT_FAILURE, _("socket name too long: %s"), socket_path);
+       if (strlen(uuidd_opts.socket_path) >= sizeof(((struct sockaddr_un *)0)->sun_path))
+               errx(EXIT_FAILURE, _("socket name too long: %s"), uuidd_opts.socket_path);
 
-       if (!no_pid && !pidfile_path)
-               pidfile_path = UUIDD_PIDFILE_PATH;
+       if (!uuidd_opts.no_pid && !uuidd_opts.pidfile_path)
+               uuidd_opts.pidfile_path = UUIDD_PIDFILE_PATH;
 
        /* custom socket path and socket-activation make no sense */
-       if (s_flag && uuidd_cxt.no_sock && !uuidd_cxt.quiet)
+       if (uuidd_opts.s_flag && uuidd_cxt.no_sock && !uuidd_cxt.quiet)
                warnx(_("Both --socket-activation and --socket specified. "
                        "Ignoring --socket."));
 
-       if (num && do_type) {
-               ret = call_daemon(socket_path, do_type + 2, buf,
-                                 sizeof(buf), &num, &err_context);
+       if (uuidd_opts.num && uuidd_opts.do_type) {
+               char buf[1024];
+               char str[UUID_STR_LEN];
+
+               ret = call_daemon(uuidd_opts.socket_path, uuidd_opts.do_type + 2, buf,
+                                 sizeof(buf), &uuidd_opts.num, &err_context);
+
                if (ret < 0)
                        err(EXIT_FAILURE, _("error calling uuidd daemon (%s)"),
                                        err_context ? : _("unexpected error"));
 
-               if (do_type == UUIDD_OP_TIME_UUID) {
-                       if (ret != sizeof(uu) + sizeof(num))
+               if (uuidd_opts.do_type == UUIDD_OP_TIME_UUID) {
+                       if (ret != sizeof(uuid_t) + sizeof(uuidd_opts.num))
                                unexpected_size(ret);
 
                        uuid_unparse((unsigned char *) buf, str);
 
                        printf(P_("%s and %d subsequent UUID\n",
-                                 "%s and %d subsequent UUIDs\n", num - 1),
-                              str, num - 1);
+                                 "%s and %d subsequent UUIDs\n", uuidd_opts.num - 1),
+                              str, uuidd_opts.num - 1);
                } else {
+                       int i;
+
                        printf(_("List of UUIDs:\n"));
                        cp = buf + 4;
-                       if (ret != (int) (sizeof(num) + num * sizeof(uu)))
+                       if (ret != (int) (sizeof(uuidd_opts.num) + uuidd_opts.num * sizeof(uuid_t)))
                                unexpected_size(ret);
-                       for (i = 0; i < num; i++, cp += UUID_LEN) {
+                       for (i = 0; i < uuidd_opts.num; i++, cp += UUID_LEN) {
                                uuid_unparse((unsigned char *) cp, str);
                                printf("\t%s\n", str);
                        }
                }
                return EXIT_SUCCESS;
        }
-       if (do_type) {
-               ret = call_daemon(socket_path, do_type, (char *) &uu,
+
+       if (uuidd_opts.do_type) {
+               uuid_t uu;
+               char str[UUID_STR_LEN];
+
+               ret = call_daemon(uuidd_opts.socket_path, uuidd_opts.do_type, (char *) &uu,
                                  sizeof(uu), 0, &err_context);
+
                if (ret < 0)
                        err(EXIT_FAILURE, _("error calling uuidd daemon (%s)"),
                                        err_context ? : _("unexpected error"));
@@ -692,8 +707,10 @@ int main(int argc, char **argv)
                return EXIT_SUCCESS;
        }
 
-       if (do_kill) {
-               ret = call_daemon(socket_path, UUIDD_OP_GETPID, buf, sizeof(buf), 0, NULL);
+       if (uuidd_opts.do_kill) {
+               char buf[16];
+
+               ret = call_daemon(uuidd_opts.socket_path, UUIDD_OP_GETPID, buf, sizeof(buf), 0, NULL);
                if (0 < ret) {
                        pid_t pid;
 
@@ -711,6 +728,6 @@ int main(int argc, char **argv)
                return EXIT_SUCCESS;
        }
 
-       server_loop(socket_path, pidfile_path, &uuidd_cxt);
+       server_loop(uuidd_opts.socket_path, uuidd_opts.pidfile_path, &uuidd_cxt);
        return EXIT_SUCCESS;
 }