1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
8 #include "alloc-util.h"
10 #include "dbus-path.h"
11 #include "dbus-util.h"
14 #include "path-util.h"
15 #include "string-util.h"
18 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result
, path_result
, PathResult
);
20 static int property_get_paths(
23 const char *interface
,
25 sd_bus_message
*reply
,
27 sd_bus_error
*error
) {
37 r
= sd_bus_message_open_container(reply
, 'a', "(ss)");
41 LIST_FOREACH(spec
, k
, p
->specs
) {
42 r
= sd_bus_message_append(reply
, "(ss)", path_type_to_string(k
->type
), k
->path
);
47 return sd_bus_message_close_container(reply
);
50 const sd_bus_vtable bus_path_vtable
[] = {
51 SD_BUS_VTABLE_START(0),
52 SD_BUS_PROPERTY("Unit", "s", bus_property_get_triggered_unit
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
53 SD_BUS_PROPERTY("Paths", "a(ss)", property_get_paths
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
54 SD_BUS_PROPERTY("MakeDirectory", "b", bus_property_get_bool
, offsetof(Path
, make_directory
), SD_BUS_VTABLE_PROPERTY_CONST
),
55 SD_BUS_PROPERTY("DirectoryMode", "u", bus_property_get_mode
, offsetof(Path
, directory_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
56 SD_BUS_PROPERTY("Result", "s", property_get_result
, offsetof(Path
, result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
60 static int bus_path_set_transient_property(
63 sd_bus_message
*message
,
65 sd_bus_error
*error
) {
74 flags
|= UNIT_PRIVATE
;
76 if (streq(name
, "MakeDirectory"))
77 return bus_set_transient_bool(u
, name
, &p
->make_directory
, message
, flags
, error
);
79 if (streq(name
, "DirectoryMode"))
80 return bus_set_transient_mode_t(u
, name
, &p
->directory_mode
, message
, flags
, error
);
82 if (streq(name
, "Paths")) {
83 const char *type_name
, *path
;
86 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
90 while ((r
= sd_bus_message_read(message
, "(ss)", &type_name
, &path
)) > 0) {
93 t
= path_type_from_string(type_name
);
95 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown path type: %s", type_name
);
98 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path in %s is empty", type_name
);
100 if (!path_is_absolute(path
))
101 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path in %s is not absolute: %s", type_name
, path
);
103 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
104 _cleanup_free_
char *k
;
111 path_simplify(k
, false);
113 s
= new0(PathSpec
, 1);
118 s
->path
= TAKE_PTR(k
);
122 LIST_PREPEND(spec
, p
->specs
, s
);
124 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
, "%s=%s", type_name
, path
);
132 r
= sd_bus_message_exit_container(message
);
136 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && empty
) {
138 unit_write_settingf(u
, flags
, name
, "PathExists=");
147 int bus_path_set_property(
150 sd_bus_message
*message
,
152 sd_bus_error
*error
) {
160 if (u
->transient
&& u
->load_state
== UNIT_STUB
)
161 return bus_path_set_transient_property(p
, name
, message
, mode
, error
);