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