]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/verbs: split out helper to find verbs
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 30 Apr 2020 12:20:31 +0000 (14:20 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 5 May 2020 16:04:55 +0000 (18:04 +0200)
It will be used later, but I think it makes the code clearer anyway.

Also change the message about ignoring to include the name for default
verbs.

src/shared/verbs.c
src/shared/verbs.h

index 06d89b4736375e45462144ee58db7f707e69c78a..d2744b6918f4c2ba86b5fe4f4419ff6532635256 100644 (file)
@@ -45,10 +45,19 @@ bool running_in_chroot_or_offline(void) {
         return r > 0;
 }
 
+const Verb* verbs_find_verb(const char *name, const Verb verbs[]) {
+        for (size_t i = 0; verbs[i].dispatch; i++)
+                if (streq_ptr(name, verbs[i].verb) ||
+                    (!name && FLAGS_SET(verbs[i].flags, VERB_DEFAULT)))
+                        return &verbs[i];
+
+        /* At the end of the list? */
+        return NULL;
+}
+
 int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata) {
         const Verb *verb;
         const char *name;
-        unsigned i;
         int left;
 
         assert(verbs);
@@ -62,31 +71,16 @@ int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata) {
         optind = 0;
         name = argv[0];
 
-        for (i = 0;; i++) {
-                bool found;
-
-                /* At the end of the list? */
-                if (!verbs[i].dispatch) {
-                        if (name)
-                                log_error("Unknown command verb %s.", name);
-                        else
-                                log_error("Command verb required.");
-                        return -EINVAL;
-                }
-
+        verb = verbs_find_verb(name, verbs);
+        if (!verb) {
                 if (name)
-                        found = streq(name, verbs[i].verb);
+                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+                                               "Unknown command verb %s.", name);
                 else
-                        found = verbs[i].flags & VERB_DEFAULT;
-
-                if (found) {
-                        verb = &verbs[i];
-                        break;
-                }
+                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+                                               "Command verb required.");
         }
 
-        assert(verb);
-
         if (!name)
                 left = 1;
 
@@ -101,10 +95,7 @@ int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata) {
                                        "Too many arguments.");
 
         if ((verb->flags & VERB_ONLINE_ONLY) && running_in_chroot_or_offline()) {
-                if (name)
-                        log_info("Running in chroot, ignoring request: %s", name);
-                else
-                        log_info("Running in chroot, ignoring request.");
+                log_info("Running in chroot, ignoring command '%s'", name ?: verb->verb);
                 return 0;
         }
 
index fd35a2e0dddd63b5c07c2466bbf585e4f45144ff..b6a1afcdee630de970a1acf205633a629e8427f4 100644 (file)
@@ -19,4 +19,5 @@ typedef struct {
 
 bool running_in_chroot_or_offline(void);
 
+const Verb* verbs_find_verb(const char *name, const Verb verbs[]);
 int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata);