]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/systemctl/systemctl-kill.c
man/systemd-sysext: list ephemeral/ephemeral-import in the list of options
[thirdparty/systemd.git] / src / systemctl / systemctl-kill.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
daf71ef6 2
0bbd8e18
DDM
3#include "sd-bus.h"
4
daf71ef6
LP
5#include "bus-error.h"
6#include "bus-locator.h"
6be4dab0 7#include "bus-wait-for-units.h"
0bbd8e18
DDM
8#include "errno-util.h"
9#include "log.h"
10#include "string-util.h"
11#include "strv.h"
1cf40697 12#include "systemctl.h"
daf71ef6
LP
13#include "systemctl-kill.h"
14#include "systemctl-util.h"
daf71ef6 15
32baf64d 16int verb_kill(int argc, char *argv[], void *userdata) {
6be4dab0 17 _cleanup_(bus_wait_for_units_freep) BusWaitForUnits *w = NULL;
daf71ef6 18 _cleanup_strv_free_ char **names = NULL;
d06e6199 19 const char *kill_whom;
daf71ef6
LP
20 sd_bus *bus;
21 int r, q;
22
23 r = acquire_bus(BUS_MANAGER, &bus);
24 if (r < 0)
25 return r;
26
6be4dab0
DDM
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
daf71ef6
LP
33 polkit_agent_open_maybe();
34
d06e6199 35 kill_whom = arg_kill_whom ?: "all";
daf71ef6
LP
36
37 /* --fail was specified */
a88f9dba 38 if (streq(arg_job_mode(), "fail"))
d06e6199 39 kill_whom = strjoina(kill_whom, "-fail");
daf71ef6
LP
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
d06e6199
LP
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);
daf71ef6 64 if (q < 0) {
6be4dab0
DDM
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));
daf71ef6
LP
73 }
74 }
75
6be4dab0
DDM
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
daf71ef6
LP
84 return r;
85}