tree-wide: extend verbs functions with extra per-verb data parameter

We often have a pattern where the same verb function is used for
multiple actions. This leads to an antipattern where we figure out what
action needs to be taken from argv[0] multiple times: often once in
parse_argv() to figure out what options are allowed, then once again
implicitly in dispatch_verb(), and then again in the action verb itself.
Let's allow passing a parameter into the verb to simplify this.

This matches a pattern we have in conf-parser.h, where we have both
void *userdata (more global) and void *data (per-config item). Here,
I opted for uintptr_t userdata. It seems that most of the time we'll
want to just pass an enum value. This works OK with no casts. I also
tried a void* and union. In both cases, much more boilerplate is needed:
either a cast or a macro to help avoid compiler warnings. uintptr_t
seems generic enough to cover foreseeable usecases with no fuss.

This is a noop refactoring. See next commit for an example.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-03-18 10:28:47 +00:00
committed by Luca Boccassi
parent 9e8babc8ab
commit 31ddd87da0
216 changed files with 745 additions and 573 deletions
+6 -6
View File
@@ -1178,7 +1178,7 @@ static int do_add(
return context_execute(c);
}
static int verb_add(int argc, char *argv[], void *userdata) {
static int verb_add(int argc, char *argv[], uintptr_t _data, void *userdata) {
const char *version, *kernel;
char **initrds;
int r;
@@ -1207,7 +1207,7 @@ static int verb_add(int argc, char *argv[], void *userdata) {
return do_add(&c, version, kernel, initrds);
}
static int verb_add_all(int argc, char *argv[], void *userdata) {
static int verb_add_all(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_close_ int fd = -EBADF;
size_t n = 0;
int ret = 0, r;
@@ -1294,10 +1294,10 @@ static int run_as_installkernel(int argc, char *argv[]) {
if (optind + 2 > argc)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "'installkernel' command requires at least two arguments.");
return verb_add(3, STRV_MAKE("add", argv[optind], argv[optind+1]), /* userdata= */ NULL);
return verb_add(3, STRV_MAKE("add", argv[optind], argv[optind+1]), /* data= */ 0, /* userdata= */ NULL);
}
static int verb_remove(int argc, char *argv[], void *userdata) {
static int verb_remove(int argc, char *argv[], uintptr_t _data, void *userdata) {
int r;
assert(argc >= 2);
@@ -1333,7 +1333,7 @@ static int verb_remove(int argc, char *argv[], void *userdata) {
return context_execute(&c);
}
static int verb_inspect(int argc, char *argv[], void *userdata) {
static int verb_inspect(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(table_unrefp) Table *t = NULL;
_cleanup_free_ char *vmlinuz = NULL;
const char *version, *kernel;
@@ -1446,7 +1446,7 @@ static int verb_inspect(int argc, char *argv[], void *userdata) {
return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, /* show_header= */ false);
}
static int verb_list(int argc, char *argv[], void *userdata) {
static int verb_list(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_close_ int fd = -EBADF;
int r;