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.
44 lines
1.4 KiB
C
44 lines
1.4 KiB
C
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
|
|
|
#include "sd-bus.h"
|
|
|
|
#include "bus-error.h"
|
|
#include "bus-locator.h"
|
|
#include "log.h"
|
|
#include "strv.h"
|
|
#include "systemctl-reset-failed.h"
|
|
#include "systemctl-trivial-method.h"
|
|
#include "systemctl-util.h"
|
|
|
|
int verb_reset_failed(int argc, char *argv[], uintptr_t data, void *userdata) {
|
|
_cleanup_strv_free_ char **names = NULL;
|
|
sd_bus *bus;
|
|
int r, q;
|
|
|
|
if (argc <= 1) /* Shortcut to trivial_method() if no argument is given */
|
|
return verb_trivial_method(argc, argv, data, userdata);
|
|
|
|
r = acquire_bus(BUS_MANAGER, &bus);
|
|
if (r < 0)
|
|
return r;
|
|
|
|
polkit_agent_open_maybe();
|
|
|
|
r = expand_unit_names(bus, strv_skip(argv, 1), NULL, &names, NULL);
|
|
if (r < 0)
|
|
return log_error_errno(r, "Failed to expand names: %m");
|
|
|
|
STRV_FOREACH(name, names) {
|
|
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
q = bus_call_method(bus, bus_systemd_mgr, "ResetFailedUnit", &error, NULL, "s", *name);
|
|
if (q < 0) {
|
|
log_error_errno(q, "Failed to reset failed state of unit %s: %s", *name, bus_error_message(&error, q));
|
|
if (r == 0)
|
|
r = q;
|
|
}
|
|
}
|
|
|
|
return r;
|
|
}
|