1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "bus-error.h"
4 #include "bus-locator.h"
5 #include "systemctl-kill.h"
6 #include "systemctl-util.h"
7 #include "systemctl.h"
8 
verb_kill(int argc,char * argv[],void * userdata)9 int verb_kill(int argc, char *argv[], void *userdata) {
10         _cleanup_strv_free_ char **names = NULL;
11         char *kill_who = NULL;
12         sd_bus *bus;
13         int r, q;
14 
15         r = acquire_bus(BUS_MANAGER, &bus);
16         if (r < 0)
17                 return r;
18 
19         polkit_agent_open_maybe();
20 
21         if (!arg_kill_who)
22                 arg_kill_who = "all";
23 
24         /* --fail was specified */
25         if (streq(arg_job_mode(), "fail"))
26                 kill_who = strjoina(arg_kill_who, "-fail");
27 
28         r = expand_unit_names(bus, strv_skip(argv, 1), NULL, &names, NULL);
29         if (r < 0)
30                 return log_error_errno(r, "Failed to expand names: %m");
31 
32         STRV_FOREACH(name, names) {
33                 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
34 
35                 q = bus_call_method(
36                                 bus,
37                                 bus_systemd_mgr,
38                                 "KillUnit",
39                                 &error,
40                                 NULL,
41                                 "ssi", *name, kill_who ? kill_who : arg_kill_who, arg_signal);
42                 if (q < 0) {
43                         log_error_errno(q, "Failed to kill unit %s: %s", *name, bus_error_message(&error, q));
44                         if (r == 0)
45                                 r = q;
46                 }
47         }
48 
49         return r;
50 }
51