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
+5 -5
View File
@@ -46,7 +46,7 @@ static int parse_interfaces(sd_netlink **rtnl, char *argv[], OrderedSet **ret) {
return 0;
}
int verb_link_up_down(int argc, char *argv[], void *userdata) {
int verb_link_up_down(int argc, char *argv[], uintptr_t _data, void *userdata) {
int r, ret = 0;
bool up = streq_ptr(argv[0], "up");
@@ -75,7 +75,7 @@ int verb_link_up_down(int argc, char *argv[], void *userdata) {
return ret;
}
int verb_link_delete(int argc, char *argv[], void *userdata) {
int verb_link_delete(int argc, char *argv[], uintptr_t _data, void *userdata) {
int r, ret = 0;
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
@@ -104,7 +104,7 @@ int verb_link_delete(int argc, char *argv[], void *userdata) {
return ret;
}
int verb_link_bus_simple_method(int argc, char *argv[], void *userdata) {
int verb_link_bus_simple_method(int argc, char *argv[], uintptr_t _data, void *userdata) {
int r, ret = 0;
typedef struct LinkBusAction {
@@ -159,7 +159,7 @@ int verb_link_bus_simple_method(int argc, char *argv[], void *userdata) {
return ret;
}
int verb_reload(int argc, char *argv[], void *userdata) {
int verb_reload(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
@@ -177,7 +177,7 @@ int verb_reload(int argc, char *argv[], void *userdata) {
return 0;
}
int verb_persistent_storage(int argc, char *argv[], void *userdata) {
int verb_persistent_storage(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *vl = NULL;
bool ready;
int r;