1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include "alloc-util.h"
23 #include "dbus-path.h"
26 #include "path-util.h"
27 #include "string-util.h"
30 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result
, path_result
, PathResult
);
32 static int property_get_paths(
35 const char *interface
,
37 sd_bus_message
*reply
,
39 sd_bus_error
*error
) {
49 r
= sd_bus_message_open_container(reply
, 'a', "(ss)");
53 LIST_FOREACH(spec
, k
, p
->specs
) {
54 r
= sd_bus_message_append(reply
, "(ss)", path_type_to_string(k
->type
), k
->path
);
59 return sd_bus_message_close_container(reply
);
62 static int property_get_unit(
65 const char *interface
,
67 sd_bus_message
*reply
,
69 sd_bus_error
*error
) {
71 Unit
*p
= userdata
, *trigger
;
77 trigger
= UNIT_TRIGGER(p
);
79 return sd_bus_message_append(reply
, "s", trigger
? trigger
->id
: "");
82 const sd_bus_vtable bus_path_vtable
[] = {
83 SD_BUS_VTABLE_START(0),
84 SD_BUS_PROPERTY("Unit", "s", property_get_unit
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
85 SD_BUS_PROPERTY("Paths", "a(ss)", property_get_paths
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
86 SD_BUS_PROPERTY("MakeDirectory", "b", bus_property_get_bool
, offsetof(Path
, make_directory
), SD_BUS_VTABLE_PROPERTY_CONST
),
87 SD_BUS_PROPERTY("DirectoryMode", "u", bus_property_get_mode
, offsetof(Path
, directory_mode
), SD_BUS_VTABLE_PROPERTY_CONST
),
88 SD_BUS_PROPERTY("Result", "s", property_get_result
, offsetof(Path
, result
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
92 static int bus_path_set_transient_property(
95 sd_bus_message
*message
,
97 sd_bus_error
*error
) {
106 flags
|= UNIT_PRIVATE
;
108 if (STR_IN_SET(name
, "PathExists", "PathExistsGlob", "PathChanged", "PathModified", "DirectoryNotEmpty")) {
112 b
= path_type_from_string(name
);
114 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown path type");
116 r
= sd_bus_message_read(message
, "s", &str
);
120 if (!isempty(str
) && !path_is_absolute(str
))
121 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path is not absolute");
123 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
126 unit_write_settingf(u
, flags
, name
, "%s=", name
);
128 _cleanup_free_
char *k
;
135 s
= new0(PathSpec
, 1);
140 s
->path
= path_kill_slashes(k
);
145 LIST_PREPEND(spec
, p
->specs
, s
);
147 unit_write_settingf(u
, flags
|UNIT_ESCAPE_SPECIFIERS
, name
, "%s=%s", name
, str
);
153 } else if (streq(name
, "MakeDirectory")) {
156 r
= sd_bus_message_read(message
, "b", &b
);
160 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
161 p
->make_directory
= b
;
162 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, yes_no(b
));
167 } else if (streq(name
, "DirectoryMode")) {
170 r
= sd_bus_message_read(message
, "u", &m
);
174 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
175 p
->directory_mode
= m
;
176 unit_write_settingf(u
, flags
, name
, "%s=%040o", name
, m
);
181 } else if (streq(name
, "Unit")) {
182 /* not implemented yet */
189 int bus_path_set_property(
192 sd_bus_message
*message
,
194 sd_bus_error
*error
) {
202 if (u
->transient
&& u
->load_state
== UNIT_STUB
)
203 return bus_path_set_transient_property(p
, name
, message
, mode
, error
);