| 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[], uintptr_t _data, 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 |
if (arg_kill_subgroup && arg_kill_value_set) |
| 24 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--kill-subgroup= and --kill-value= may not be combined."); |
| 25 |
|
| 26 |
r = acquire_bus(BUS_MANAGER, &bus); |
| 27 |
if (r < 0) |
| 28 |
return r; |
| 29 |
|
| 30 |
if (arg_wait) { |
| 31 |
r = bus_wait_for_units_new(bus, &w); |
| 32 |
if (r < 0) |
| 33 |
return log_error_errno(r, "Failed to allocate unit watch context: %m"); |
| 34 |
} |
| 35 |
|
| 36 |
polkit_agent_open_maybe(); |
| 37 |
|
| 38 |
kill_whom = arg_kill_whom ?: arg_kill_subgroup ? "cgroup" : "all"; |
| 39 |
|
| 40 |
/* --fail was specified */ |
| 41 |
if (streq(arg_job_mode(), "fail")) |
| 42 |
kill_whom = strjoina(kill_whom, "-fail"); |
| 43 |
|
| 44 |
r = expand_unit_names(bus, strv_skip(argv, 1), NULL, &names, NULL); |
| 45 |
if (r < 0) |
| 46 |
return log_error_errno(r, "Failed to expand names: %m"); |
| 47 |
|
| 48 |
STRV_FOREACH(name, names) { |
| 49 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 50 |
|
| 51 |
if (arg_kill_value_set) |
| 52 |
q = bus_call_method( |
| 53 |
bus, |
| 54 |
bus_systemd_mgr, |
| 55 |
"QueueSignalUnit", |
| 56 |
&error, |
| 57 |
NULL, |
| 58 |
"ssii", *name, kill_whom, arg_signal, arg_kill_value); |
| 59 |
else if (arg_kill_subgroup) |
| 60 |
q = bus_call_method( |
| 61 |
bus, |
| 62 |
bus_systemd_mgr, |
| 63 |
"KillUnitSubgroup", |
| 64 |
&error, |
| 65 |
NULL, |
| 66 |
"sssi", *name, kill_whom, arg_kill_subgroup, arg_signal); |
| 67 |
else |
| 68 |
q = bus_call_method( |
| 69 |
bus, |
| 70 |
bus_systemd_mgr, |
| 71 |
"KillUnit", |
| 72 |
&error, |
| 73 |
NULL, |
| 74 |
"ssi", *name, kill_whom, arg_signal); |
| 75 |
if (q < 0) { |
| 76 |
RET_GATHER(r, log_error_errno(q, "Failed to kill unit %s: %s", *name, bus_error_message(&error, q))); |
| 77 |
continue; |
| 78 |
} |
| 79 |
|
| 80 |
if (w) { |
| 81 |
q = bus_wait_for_units_add_unit(w, *name, BUS_WAIT_FOR_INACTIVE|BUS_WAIT_NO_JOB, NULL, NULL); |
| 82 |
if (q < 0) |
| 83 |
RET_GATHER(r, log_error_errno(q, "Failed to watch unit %s: %m", *name)); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
if (w) { |
| 88 |
q = bus_wait_for_units_run(w); |
| 89 |
if (q < 0) |
| 90 |
return log_error_errno(q, "Failed to wait for units: %m"); |
| 91 |
if (q == BUS_WAIT_FAILURE) |
| 92 |
RET_GATHER(r, -EIO); |
| 93 |
} |
| 94 |
|
| 95 |
return r; |
| 96 |
} |