2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2010-05-24 05:25:33 +02:00
|
|
|
|
2017-12-11 16:10:58 +01:00
|
|
|
#include "alloc-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "bus-util.h"
|
2015-11-16 22:09:36 +01:00
|
|
|
#include "dbus-path.h"
|
2018-01-01 18:25:57 +01:00
|
|
|
#include "dbus-util.h"
|
2017-12-11 16:10:58 +01:00
|
|
|
#include "list.h"
|
2013-11-19 21:12:59 +01:00
|
|
|
#include "path.h"
|
2017-12-11 16:10:58 +01:00
|
|
|
#include "path-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
|
|
|
#include "unit.h"
|
2013-11-19 21:12:59 +01:00
|
|
|
|
|
|
|
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, path_result, PathResult);
|
|
|
|
|
|
|
|
static int property_get_paths(
|
|
|
|
sd_bus *bus,
|
|
|
|
const char *path,
|
|
|
|
const char *interface,
|
|
|
|
const char *property,
|
|
|
|
sd_bus_message *reply,
|
2013-11-21 19:34:37 +01:00
|
|
|
void *userdata,
|
|
|
|
sd_bus_error *error) {
|
2013-11-19 21:12:59 +01:00
|
|
|
|
|
|
|
Path *p = userdata;
|
2010-07-04 04:23:48 +02:00
|
|
|
PathSpec *k;
|
2013-11-19 21:12:59 +01:00
|
|
|
int r;
|
2010-07-04 04:23:48 +02:00
|
|
|
|
2013-11-19 21:12:59 +01:00
|
|
|
assert(bus);
|
|
|
|
assert(reply);
|
2010-07-04 04:23:48 +02:00
|
|
|
assert(p);
|
|
|
|
|
2013-11-19 21:12:59 +01:00
|
|
|
r = sd_bus_message_open_container(reply, 'a', "(ss)");
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2010-07-04 04:23:48 +02:00
|
|
|
|
|
|
|
LIST_FOREACH(spec, k, p->specs) {
|
2013-11-19 21:12:59 +01:00
|
|
|
r = sd_bus_message_append(reply, "(ss)", path_type_to_string(k->type), k->path);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2010-07-04 04:23:48 +02:00
|
|
|
}
|
|
|
|
|
2013-11-19 21:12:59 +01:00
|
|
|
return sd_bus_message_close_container(reply);
|
2010-07-04 04:23:48 +02:00
|
|
|
}
|
|
|
|
|
2013-11-19 21:12:59 +01:00
|
|
|
const sd_bus_vtable bus_path_vtable[] = {
|
|
|
|
SD_BUS_VTABLE_START(0),
|
2018-05-10 18:41:06 +02:00
|
|
|
SD_BUS_PROPERTY("Unit", "s", bus_property_get_triggered_unit, 0, SD_BUS_VTABLE_PROPERTY_CONST),
|
2013-12-22 02:24:05 +01:00
|
|
|
SD_BUS_PROPERTY("Paths", "a(ss)", property_get_paths, 0, SD_BUS_VTABLE_PROPERTY_CONST),
|
|
|
|
SD_BUS_PROPERTY("MakeDirectory", "b", bus_property_get_bool, offsetof(Path, make_directory), SD_BUS_VTABLE_PROPERTY_CONST),
|
|
|
|
SD_BUS_PROPERTY("DirectoryMode", "u", bus_property_get_mode, offsetof(Path, directory_mode), SD_BUS_VTABLE_PROPERTY_CONST),
|
2013-11-19 21:12:59 +01:00
|
|
|
SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Path, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
|
|
|
|
SD_BUS_VTABLE_END
|
2012-01-16 00:23:59 +01:00
|
|
|
};
|
2017-12-11 16:10:58 +01:00
|
|
|
|
|
|
|
static int bus_path_set_transient_property(
|
|
|
|
Path *p,
|
|
|
|
const char *name,
|
|
|
|
sd_bus_message *message,
|
|
|
|
UnitWriteFlags flags,
|
|
|
|
sd_bus_error *error) {
|
|
|
|
|
|
|
|
Unit *u = UNIT(p);
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(p);
|
|
|
|
assert(name);
|
|
|
|
assert(message);
|
|
|
|
|
|
|
|
flags |= UNIT_PRIVATE;
|
|
|
|
|
2018-01-01 18:25:57 +01:00
|
|
|
if (streq(name, "MakeDirectory"))
|
|
|
|
return bus_set_transient_bool(u, name, &p->make_directory, message, flags, error);
|
2017-12-11 16:10:58 +01:00
|
|
|
|
2018-01-01 18:25:57 +01:00
|
|
|
if (streq(name, "DirectoryMode"))
|
|
|
|
return bus_set_transient_mode_t(u, name, &p->directory_mode, message, flags, error);
|
2017-12-11 16:10:58 +01:00
|
|
|
|
2018-01-01 18:25:57 +01:00
|
|
|
if (streq(name, "Paths")) {
|
|
|
|
const char *type_name, *path;
|
|
|
|
bool empty = true;
|
|
|
|
|
|
|
|
r = sd_bus_message_enter_container(message, 'a', "(ss)");
|
2017-12-11 16:10:58 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-01-01 18:25:57 +01:00
|
|
|
while ((r = sd_bus_message_read(message, "(ss)", &type_name, &path)) > 0) {
|
|
|
|
PathType t;
|
|
|
|
|
|
|
|
t = path_type_from_string(type_name);
|
|
|
|
if (t < 0)
|
|
|
|
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown path type: %s", type_name);
|
|
|
|
|
|
|
|
if (isempty(path))
|
|
|
|
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path in %s is empty", type_name);
|
|
|
|
|
|
|
|
if (!path_is_absolute(path))
|
|
|
|
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path in %s is not absolute: %s", type_name, path);
|
2017-12-11 16:10:58 +01:00
|
|
|
|
2018-01-01 18:25:57 +01:00
|
|
|
if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
|
2017-12-11 16:10:58 +01:00
|
|
|
_cleanup_free_ char *k;
|
|
|
|
PathSpec *s;
|
|
|
|
|
2018-01-01 18:25:57 +01:00
|
|
|
k = strdup(path);
|
2017-12-11 16:10:58 +01:00
|
|
|
if (!k)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2018-05-31 16:39:31 +02:00
|
|
|
path_simplify(k, false);
|
|
|
|
|
2017-12-11 16:10:58 +01:00
|
|
|
s = new0(PathSpec, 1);
|
|
|
|
if (!s)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
s->unit = u;
|
2018-05-31 16:39:31 +02:00
|
|
|
s->path = TAKE_PTR(k);
|
2018-01-01 18:25:57 +01:00
|
|
|
s->type = t;
|
2017-12-11 16:10:58 +01:00
|
|
|
s->inotify_fd = -1;
|
|
|
|
|
|
|
|
LIST_PREPEND(spec, p->specs, s);
|
|
|
|
|
2018-01-01 18:25:57 +01:00
|
|
|
unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s", type_name, path);
|
2017-12-11 16:10:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-01 18:25:57 +01:00
|
|
|
empty = false;
|
|
|
|
}
|
2017-12-11 16:10:58 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-01-01 18:25:57 +01:00
|
|
|
r = sd_bus_message_exit_container(message);
|
2017-12-11 16:10:58 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-01-01 18:25:57 +01:00
|
|
|
if (!UNIT_WRITE_FLAGS_NOOP(flags) && empty) {
|
|
|
|
path_free_specs(p);
|
|
|
|
unit_write_settingf(u, flags, name, "PathExists=");
|
2017-12-11 16:10:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bus_path_set_property(
|
|
|
|
Unit *u,
|
|
|
|
const char *name,
|
|
|
|
sd_bus_message *message,
|
|
|
|
UnitWriteFlags mode,
|
|
|
|
sd_bus_error *error) {
|
|
|
|
|
|
|
|
Path *p = PATH(u);
|
|
|
|
|
|
|
|
assert(p);
|
|
|
|
assert(name);
|
|
|
|
assert(message);
|
|
|
|
|
|
|
|
if (u->transient && u->load_state == UNIT_STUB)
|
|
|
|
return bus_path_set_transient_property(p, name, message, mode, error);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|