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