]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/dbus-path.c
Merge pull request #7620 from keszybz/two-docs
[thirdparty/systemd.git] / src / core / dbus-path.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6
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.
11
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.
16
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/>.
19 ***/
20
21 #include "alloc-util.h"
22 #include "bus-util.h"
23 #include "dbus-path.h"
24 #include "list.h"
25 #include "path.h"
26 #include "path-util.h"
27 #include "string-util.h"
28 #include "unit.h"
29
30 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, path_result, PathResult);
31
32 static int property_get_paths(
33 sd_bus *bus,
34 const char *path,
35 const char *interface,
36 const char *property,
37 sd_bus_message *reply,
38 void *userdata,
39 sd_bus_error *error) {
40
41 Path *p = userdata;
42 PathSpec *k;
43 int r;
44
45 assert(bus);
46 assert(reply);
47 assert(p);
48
49 r = sd_bus_message_open_container(reply, 'a', "(ss)");
50 if (r < 0)
51 return r;
52
53 LIST_FOREACH(spec, k, p->specs) {
54 r = sd_bus_message_append(reply, "(ss)", path_type_to_string(k->type), k->path);
55 if (r < 0)
56 return r;
57 }
58
59 return sd_bus_message_close_container(reply);
60 }
61
62 static int property_get_unit(
63 sd_bus *bus,
64 const char *path,
65 const char *interface,
66 const char *property,
67 sd_bus_message *reply,
68 void *userdata,
69 sd_bus_error *error) {
70
71 Unit *p = userdata, *trigger;
72
73 assert(bus);
74 assert(reply);
75 assert(p);
76
77 trigger = UNIT_TRIGGER(p);
78
79 return sd_bus_message_append(reply, "s", trigger ? trigger->id : "");
80 }
81
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),
89 SD_BUS_VTABLE_END
90 };
91
92 static int bus_path_set_transient_property(
93 Path *p,
94 const char *name,
95 sd_bus_message *message,
96 UnitWriteFlags flags,
97 sd_bus_error *error) {
98
99 Unit *u = UNIT(p);
100 int r;
101
102 assert(p);
103 assert(name);
104 assert(message);
105
106 flags |= UNIT_PRIVATE;
107
108 if (STR_IN_SET(name, "PathExists", "PathExistsGlob", "PathChanged", "PathModified", "DirectoryNotEmpty")) {
109 const char *str;
110 PathType b;
111
112 b = path_type_from_string(name);
113 if (b < 0)
114 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown path type");
115
116 r = sd_bus_message_read(message, "s", &str);
117 if (r < 0)
118 return r;
119
120 if (!isempty(str) && !path_is_absolute(str))
121 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path is not absolute");
122
123 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
124 if (isempty(str)) {
125 path_free_specs(p);
126 unit_write_settingf(u, flags, name, "%s=", name);
127 } else {
128 _cleanup_free_ char *k;
129 PathSpec *s;
130
131 k = strdup(str);
132 if (!k)
133 return -ENOMEM;
134
135 s = new0(PathSpec, 1);
136 if (!s)
137 return -ENOMEM;
138
139 s->unit = u;
140 s->path = path_kill_slashes(k);
141 k = NULL;
142 s->type = b;
143 s->inotify_fd = -1;
144
145 LIST_PREPEND(spec, p->specs, s);
146
147 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s", name, str);
148 }
149 }
150
151 return 1;
152
153 } else if (streq(name, "MakeDirectory")) {
154 int b;
155
156 r = sd_bus_message_read(message, "b", &b);
157 if (r < 0)
158 return r;
159
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));
163 }
164
165 return 1;
166
167 } else if (streq(name, "DirectoryMode")) {
168 mode_t m;
169
170 r = sd_bus_message_read(message, "u", &m);
171 if (r < 0)
172 return r;
173
174 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
175 p->directory_mode = m;
176 unit_write_settingf(u, flags, name, "%s=%040o", name, m);
177 }
178
179 return 1;
180
181 } else if (streq(name, "Unit")) {
182 /* not implemented yet */
183 return 0;
184 }
185
186 return 0;
187 }
188
189 int bus_path_set_property(
190 Unit *u,
191 const char *name,
192 sd_bus_message *message,
193 UnitWriteFlags mode,
194 sd_bus_error *error) {
195
196 Path *p = PATH(u);
197
198 assert(p);
199 assert(name);
200 assert(message);
201
202 if (u->transient && u->load_state == UNIT_STUB)
203 return bus_path_set_transient_property(p, name, message, mode, error);
204
205 return 0;
206 }