Skip to content

Commit

Permalink
shared/verbs: split out helper to find verbs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
keszybz committed May 5, 2020
1 parent d8b065e commit 9321e23
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
43 changes: 17 additions & 26 deletions src/shared/verbs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;

Expand All @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/shared/verbs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 9321e23

Please sign in to comment.