| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "bus-get-properties.h" |
| 5 |
#include "dbus-path.h" |
| 6 |
#include "dbus-util.h" |
| 7 |
#include "list.h" |
| 8 |
#include "path.h" |
| 9 |
#include "path-util.h" |
| 10 |
#include "string-util.h" |
| 11 |
#include "unit.h" |
| 12 |
|
| 13 |
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, path_result, PathResult); |
| 14 |
|
| 15 |
static int property_get_paths( |
| 16 |
sd_bus *bus, |
| 17 |
const char *path, |
| 18 |
const char *interface, |
| 19 |
const char *property, |
| 20 |
sd_bus_message *reply, |
| 21 |
void *userdata, |
| 22 |
sd_bus_error *reterr_error) { |
| 23 |
|
| 24 |
Path *p = ASSERT_PTR(userdata); |
| 25 |
int r; |
| 26 |
|
| 27 |
assert(bus); |
| 28 |
assert(reply); |
| 29 |
|
| 30 |
r = sd_bus_message_open_container(reply, 'a', "(ss)"); |
| 31 |
if (r < 0) |
| 32 |
return r; |
| 33 |
|
| 34 |
LIST_FOREACH(spec, k, p->specs) { |
| 35 |
r = sd_bus_message_append(reply, "(ss)", path_type_to_string(k->type), k->path); |
| 36 |
if (r < 0) |
| 37 |
return r; |
| 38 |
} |
| 39 |
|
| 40 |
return sd_bus_message_close_container(reply); |
| 41 |
} |
| 42 |
|
| 43 |
const sd_bus_vtable bus_path_vtable[] = { |
| 44 |
SD_BUS_VTABLE_START(0), |
| 45 |
SD_BUS_PROPERTY("Unit", "s", bus_property_get_triggered_unit, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 46 |
SD_BUS_PROPERTY("Paths", "a(ss)", property_get_paths, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 47 |
SD_BUS_PROPERTY("MakeDirectory", "b", bus_property_get_bool, offsetof(Path, make_directory), SD_BUS_VTABLE_PROPERTY_CONST), |
| 48 |
SD_BUS_PROPERTY("DirectoryMode", "u", bus_property_get_mode, offsetof(Path, directory_mode), SD_BUS_VTABLE_PROPERTY_CONST), |
| 49 |
SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Path, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 50 |
SD_BUS_PROPERTY("TriggerLimitIntervalUSec", "t", bus_property_get_usec, offsetof(Path, trigger_limit.interval), SD_BUS_VTABLE_PROPERTY_CONST), |
| 51 |
SD_BUS_PROPERTY("TriggerLimitBurst", "u", bus_property_get_unsigned, offsetof(Path, trigger_limit.burst), SD_BUS_VTABLE_PROPERTY_CONST), |
| 52 |
SD_BUS_VTABLE_END |
| 53 |
}; |
| 54 |
|
| 55 |
static int bus_path_set_transient_property( |
| 56 |
Path *p, |
| 57 |
const char *name, |
| 58 |
sd_bus_message *message, |
| 59 |
UnitWriteFlags flags, |
| 60 |
sd_bus_error *reterr_error) { |
| 61 |
|
| 62 |
Unit *u = UNIT(p); |
| 63 |
int r; |
| 64 |
|
| 65 |
assert(p); |
| 66 |
assert(name); |
| 67 |
assert(message); |
| 68 |
|
| 69 |
flags |= UNIT_PRIVATE; |
| 70 |
|
| 71 |
if (streq(name, "MakeDirectory")) |
| 72 |
return bus_set_transient_bool(u, name, &p->make_directory, message, flags, reterr_error); |
| 73 |
|
| 74 |
if (streq(name, "DirectoryMode")) |
| 75 |
return bus_set_transient_mode_t(u, name, &p->directory_mode, message, flags, reterr_error); |
| 76 |
|
| 77 |
if (streq(name, "Paths")) { |
| 78 |
const char *type_name, *path; |
| 79 |
bool empty = true; |
| 80 |
|
| 81 |
r = sd_bus_message_enter_container(message, 'a', "(ss)"); |
| 82 |
if (r < 0) |
| 83 |
return r; |
| 84 |
|
| 85 |
while ((r = sd_bus_message_read(message, "(ss)", &type_name, &path)) > 0) { |
| 86 |
PathType t; |
| 87 |
|
| 88 |
t = path_type_from_string(type_name); |
| 89 |
if (t < 0) |
| 90 |
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Unknown path type: %s", type_name); |
| 91 |
|
| 92 |
if (isempty(path)) |
| 93 |
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Path in %s is empty", type_name); |
| 94 |
|
| 95 |
if (!path_is_absolute(path)) |
| 96 |
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Path in %s is not absolute: %s", type_name, path); |
| 97 |
|
| 98 |
if (!UNIT_WRITE_FLAGS_NOOP(flags)) { |
| 99 |
_cleanup_free_ char *k = NULL; |
| 100 |
|
| 101 |
r = path_simplify_alloc(path, &k); |
| 102 |
if (r < 0) |
| 103 |
return r; |
| 104 |
|
| 105 |
PathSpec *s = new(PathSpec, 1); |
| 106 |
if (!s) |
| 107 |
return -ENOMEM; |
| 108 |
|
| 109 |
*s = (PathSpec) { |
| 110 |
.unit = u, |
| 111 |
.path = TAKE_PTR(k), |
| 112 |
.type = t, |
| 113 |
.inotify_fd = -EBADF, |
| 114 |
}; |
| 115 |
|
| 116 |
LIST_PREPEND(spec, p->specs, s); |
| 117 |
|
| 118 |
unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s", type_name, path); |
| 119 |
} |
| 120 |
|
| 121 |
empty = false; |
| 122 |
} |
| 123 |
if (r < 0) |
| 124 |
return r; |
| 125 |
|
| 126 |
r = sd_bus_message_exit_container(message); |
| 127 |
if (r < 0) |
| 128 |
return r; |
| 129 |
|
| 130 |
if (!UNIT_WRITE_FLAGS_NOOP(flags) && empty) { |
| 131 |
path_free_specs(p); |
| 132 |
unit_write_settingf(u, flags, name, "PathExists="); |
| 133 |
} |
| 134 |
|
| 135 |
return 1; |
| 136 |
} |
| 137 |
|
| 138 |
if (streq(name, "TriggerLimitBurst")) |
| 139 |
return bus_set_transient_unsigned(u, name, &p->trigger_limit.burst, message, flags, reterr_error); |
| 140 |
|
| 141 |
if (streq(name, "TriggerLimitIntervalUSec")) |
| 142 |
return bus_set_transient_usec(u, name, &p->trigger_limit.interval, message, flags, reterr_error); |
| 143 |
|
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
int bus_path_set_property( |
| 148 |
Unit *u, |
| 149 |
const char *name, |
| 150 |
sd_bus_message *message, |
| 151 |
UnitWriteFlags flags, |
| 152 |
sd_bus_error *reterr_error) { |
| 153 |
|
| 154 |
Path *p = PATH(u); |
| 155 |
|
| 156 |
assert(p); |
| 157 |
assert(name); |
| 158 |
assert(message); |
| 159 |
|
| 160 |
if (u->transient && u->load_state == UNIT_STUB) |
| 161 |
return bus_path_set_transient_property(p, name, message, flags, reterr_error); |
| 162 |
|
| 163 |
return 0; |
| 164 |
} |