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