1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "bus-error.h"
4 #include "bus-locator.h"
5 #include "systemctl-set-property.h"
6 #include "systemctl-util.h"
7 #include "systemctl.h"
8
set_property_one(sd_bus * bus,const char * name,char ** properties)9 static int set_property_one(sd_bus *bus, const char *name, char **properties) {
10 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
11 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
12 int r;
13
14 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "SetUnitProperties");
15 if (r < 0)
16 return bus_log_create_error(r);
17
18 UnitType t = unit_name_to_type(name);
19 if (t < 0)
20 return log_error_errno(t, "Invalid unit type: %s", name);
21
22 r = sd_bus_message_append(m, "sb", name, arg_runtime);
23 if (r < 0)
24 return bus_log_create_error(r);
25
26 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, "(sv)");
27 if (r < 0)
28 return bus_log_create_error(r);
29
30 r = bus_append_unit_property_assignment_many(m, t, properties);
31 if (r < 0)
32 return r;
33
34 r = sd_bus_message_close_container(m);
35 if (r < 0)
36 return bus_log_create_error(r);
37
38 r = sd_bus_call(bus, m, 0, &error, NULL);
39 if (r < 0)
40 return log_error_errno(r, "Failed to set unit properties on %s: %s",
41 name, bus_error_message(&error, r));
42
43 return 0;
44 }
45
verb_set_property(int argc,char * argv[],void * userdata)46 int verb_set_property(int argc, char *argv[], void *userdata) {
47 sd_bus *bus;
48 _cleanup_strv_free_ char **names = NULL;
49 int r, k;
50
51 r = acquire_bus(BUS_MANAGER, &bus);
52 if (r < 0)
53 return r;
54
55 polkit_agent_open_maybe();
56
57 r = expand_unit_names(bus, STRV_MAKE(argv[1]), NULL, &names, NULL);
58 if (r < 0)
59 return log_error_errno(r, "Failed to expand '%s' into names: %m", argv[1]);
60
61 r = 0;
62 STRV_FOREACH(name, names) {
63 k = set_property_one(bus, *name, strv_skip(argv, 2));
64 if (k < 0 && r >= 0)
65 r = k;
66 }
67 return r;
68 }
69