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