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
+7 -7
View File
@@ -407,7 +407,7 @@ static int table_add_uid_map(
return n_added;
}
static int verb_display_user(int argc, char *argv[], void *userdata) {
static int verb_display_user(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(table_unrefp) Table *table = NULL;
bool draw_separator = false;
int ret = 0, r;
@@ -750,7 +750,7 @@ static int add_unavailable_gid(Table *table, uid_t start, uid_t end) {
return 2;
}
static int verb_display_group(int argc, char *argv[], void *userdata) {
static int verb_display_group(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(table_unrefp) Table *table = NULL;
bool draw_separator = false;
int ret = 0, r;
@@ -951,7 +951,7 @@ static int show_membership(const char *user, const char *group, Table *table) {
return 0;
}
static int verb_display_memberships(int argc, char *argv[], void *userdata) {
static int verb_display_memberships(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(table_unrefp) Table *table = NULL;
int ret = 0, r;
@@ -1047,7 +1047,7 @@ static int verb_display_memberships(int argc, char *argv[], void *userdata) {
return ret;
}
static int verb_display_services(int argc, char *argv[], void *userdata) {
static int verb_display_services(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(table_unrefp) Table *t = NULL;
_cleanup_closedir_ DIR *d = NULL;
int r;
@@ -1114,7 +1114,7 @@ static int verb_display_services(int argc, char *argv[], void *userdata) {
return 0;
}
static int verb_ssh_authorized_keys(int argc, char *argv[], void *userdata) {
static int verb_ssh_authorized_keys(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(user_record_unrefp) UserRecord *ur = NULL;
char **chain_invocation;
int r;
@@ -1497,7 +1497,7 @@ static int load_credential_one(
return 0;
}
static int verb_load_credentials(int argc, char *argv[], void *userdata) {
static int verb_load_credentials(int argc, char *argv[], uintptr_t _data, void *userdata) {
int r;
_cleanup_close_ int credential_dir_fd = open_credentials_dir();
@@ -1590,7 +1590,7 @@ static int help(void) {
return 0;
}
static int verb_help(int argc, char *argv[], void *userdata) {
static int verb_help(int argc, char *argv[], uintptr_t _data, void *userdata) {
return help();
}