Debian Codegraph @

src/systemctl/systemctl-trivial-method.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-bus.h"
4
5 #include "bus-error.h"
6 #include "bus-locator.h"
7 #include "log.h"
8 #include "string-util.h"
9 #include "systemctl.h"
10 #include "systemctl-trivial-method.h"
11 #include "systemctl-util.h"
12
13 /* A generic implementation for cases we just need to invoke a simple method call on the Manager object. */
14
15 int verb_trivial_method(int argc, char *argv[], uintptr_t _data, void *userdata) {
16 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
17 const char *method;
18 sd_bus *bus;
19 int r;
20
21 if (arg_dry_run)
22 return 0;
23
24 r = acquire_bus(BUS_MANAGER, &bus);
25 if (r < 0)
26 return r;
27
28 polkit_agent_open_maybe();
29
30 method =
31 streq(argv[0], "clear-jobs") ||
32 streq(argv[0], "cancel") ? "ClearJobs" :
33 streq(argv[0], "reset-failed") ? "ResetFailed" :
34 streq(argv[0], "halt") ? "Halt" :
35 streq(argv[0], "reboot") ? "Reboot" :
36 streq(argv[0], "kexec") ? "KExec" :
37 streq(argv[0], "soft-reboot") ? "SoftReboot" :
38 streq(argv[0], "exit") ? "Exit" :
39 /* poweroff */ "PowerOff";
40
41 r = bus_call_method(bus, bus_systemd_mgr, method, &error, NULL, NULL);
42 if (r < 0 && arg_action == ACTION_SYSTEMCTL)
43 return log_error_errno(r, "Failed to execute operation: %s", bus_error_message(&error, r));
44
45 /* Note that for the legacy commands (i.e. those with action != ACTION_SYSTEMCTL) we support
46 * fallbacks to the old ways of doing things, hence don't log any error in that case here. */
47
48 return r < 0 ? r : 0;
49 }