]>
Commit | Line | Data |
---|---|---|
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 "bus-wait-for-units.h" | |
8 | #include "errno-util.h" | |
9 | #include "log.h" | |
10 | #include "string-util.h" | |
11 | #include "strv.h" | |
12 | #include "systemctl.h" | |
13 | #include "systemctl-kill.h" | |
14 | #include "systemctl-util.h" | |
15 | ||
16 | int verb_kill(int argc, char *argv[], void *userdata) { | |
17 | _cleanup_(bus_wait_for_units_freep) BusWaitForUnits *w = NULL; | |
18 | _cleanup_strv_free_ char **names = NULL; | |
19 | const char *kill_whom; | |
20 | sd_bus *bus; | |
21 | int r, q; | |
22 | ||
23 | r = acquire_bus(BUS_MANAGER, &bus); | |
24 | if (r < 0) | |
25 | return r; | |
26 | ||
27 | if (arg_wait) { | |
28 | r = bus_wait_for_units_new(bus, &w); | |
29 | if (r < 0) | |
30 | return log_error_errno(r, "Failed to allocate unit watch context: %m"); | |
31 | } | |
32 | ||
33 | polkit_agent_open_maybe(); | |
34 | ||
35 | kill_whom = arg_kill_whom ?: "all"; | |
36 | ||
37 | /* --fail was specified */ | |
38 | if (streq(arg_job_mode(), "fail")) | |
39 | kill_whom = strjoina(kill_whom, "-fail"); | |
40 | ||
41 | r = expand_unit_names(bus, strv_skip(argv, 1), NULL, &names, NULL); | |
42 | if (r < 0) | |
43 | return log_error_errno(r, "Failed to expand names: %m"); | |
44 | ||
45 | STRV_FOREACH(name, names) { | |
46 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
47 | ||
48 | if (arg_kill_value_set) | |
49 | q = bus_call_method( | |
50 | bus, | |
51 | bus_systemd_mgr, | |
52 | "QueueSignalUnit", | |
53 | &error, | |
54 | NULL, | |
55 | "ssii", *name, kill_whom, arg_signal, arg_kill_value); | |
56 | else | |
57 | q = bus_call_method( | |
58 | bus, | |
59 | bus_systemd_mgr, | |
60 | "KillUnit", | |
61 | &error, | |
62 | NULL, | |
63 | "ssi", *name, kill_whom, arg_signal); | |
64 | if (q < 0) { | |
65 | RET_GATHER(r, log_error_errno(q, "Failed to kill unit %s: %s", *name, bus_error_message(&error, q))); | |
66 | continue; | |
67 | } | |
68 | ||
69 | if (w) { | |
70 | q = bus_wait_for_units_add_unit(w, *name, BUS_WAIT_FOR_INACTIVE|BUS_WAIT_NO_JOB, NULL, NULL); | |
71 | if (q < 0) | |
72 | RET_GATHER(r, log_error_errno(q, "Failed to watch unit %s: %m", *name)); | |
73 | } | |
74 | } | |
75 | ||
76 | if (w) { | |
77 | q = bus_wait_for_units_run(w); | |
78 | if (q < 0) | |
79 | return log_error_errno(q, "Failed to wait for units: %m"); | |
80 | if (q == BUS_WAIT_FAILURE) | |
81 | RET_GATHER(r, -EIO); | |
82 | } | |
83 | ||
84 | return r; | |
85 | } |