]>
Commit | Line | Data |
---|---|---|
ea430986 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
ea430986 LP |
22 | #include <errno.h> |
23 | ||
24 | #include "dbus.h" | |
25 | #include "log.h" | |
26 | ||
27 | static const char introspection[] = | |
28 | DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE | |
29 | "<node>" | |
ea430986 | 30 | " <interface name=\"org.freedesktop.systemd1.Unit\">" |
b548631a LP |
31 | " <method name=\"Start\">" |
32 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>" | |
33 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>" | |
34 | " </method>" | |
35 | " <method name=\"Stop\">" | |
36 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>" | |
37 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>" | |
38 | " </method>" | |
39 | " <method name=\"Restart\">" | |
40 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>" | |
41 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>" | |
42 | " </method>" | |
43 | " <method name=\"Reload\">" | |
44 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>" | |
45 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>" | |
46 | " </method>" | |
c1e1601e | 47 | " <signal name=\"Changed\"/>" |
ea430986 LP |
48 | " <property name=\"Id\" type=\"s\" access=\"read\"/>" |
49 | " <property name=\"Description\" type=\"s\" access=\"read\"/>" | |
50 | " <property name=\"LoadState\" type=\"s\" access=\"read\"/>" | |
51 | " <property name=\"ActiveState\" type=\"s\" access=\"read\"/>" | |
6be1e7d5 | 52 | " <property name=\"FragmentPath\" type=\"s\" access=\"read\"/>" |
38131695 LP |
53 | " <property name=\"ActiveEnterTimestamp\" type=\"t\" access=\"read\"/>" |
54 | " <property name=\"ActiveExitTimestamp\" type=\"t\" access=\"read\"/>" | |
55 | " <property name=\"CanReload\" type=\"b\" access=\"read\"/>" | |
56 | " <property name=\"CanStart\" type=\"b\" access=\"read\"/>" | |
57 | " <property name=\"Job\" type=\"(uo)\" access=\"read\"/>" | |
ea430986 LP |
58 | " </interface>" |
59 | BUS_PROPERTIES_INTERFACE | |
60 | BUS_INTROSPECTABLE_INTERFACE | |
61 | "</node>"; | |
62 | ||
63 | static int bus_unit_append_id(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
64 | Unit *u = data; | |
65 | const char *id; | |
66 | ||
67 | assert(m); | |
68 | assert(i); | |
69 | assert(property); | |
70 | assert(u); | |
71 | ||
72 | id = unit_id(u); | |
73 | ||
c401a1e0 | 74 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &id)) |
ea430986 LP |
75 | return -ENOMEM; |
76 | ||
77 | return 0; | |
78 | } | |
79 | ||
80 | static int bus_unit_append_description(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
81 | Unit *u = data; | |
82 | const char *d; | |
83 | ||
84 | assert(m); | |
85 | assert(i); | |
86 | assert(property); | |
87 | assert(u); | |
88 | ||
89 | d = unit_description(u); | |
90 | ||
91 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d)) | |
92 | return -ENOMEM; | |
93 | ||
94 | return 0; | |
95 | } | |
96 | ||
6f4706b7 | 97 | DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_load_state, unit_load_state, UnitLoadState); |
ea430986 LP |
98 | |
99 | static int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
100 | Unit *u = data; | |
101 | const char *state; | |
102 | ||
103 | assert(m); | |
104 | assert(i); | |
105 | assert(property); | |
106 | assert(u); | |
107 | ||
108 | state = unit_active_state_to_string(unit_active_state(u)); | |
109 | ||
110 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state)) | |
111 | return -ENOMEM; | |
112 | ||
113 | return 0; | |
114 | } | |
115 | ||
38131695 LP |
116 | static int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
117 | Unit *u = data; | |
118 | dbus_bool_t b; | |
119 | ||
120 | assert(m); | |
121 | assert(i); | |
122 | assert(property); | |
123 | assert(u); | |
124 | ||
125 | b = unit_can_reload(u); | |
126 | ||
127 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b)) | |
128 | return -ENOMEM; | |
129 | ||
130 | return 0; | |
131 | } | |
132 | ||
133 | static int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
134 | Unit *u = data; | |
135 | dbus_bool_t b; | |
136 | ||
137 | assert(m); | |
138 | assert(i); | |
139 | assert(property); | |
140 | assert(u); | |
141 | ||
142 | b = unit_can_start(u); | |
143 | ||
144 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b)) | |
145 | return -ENOMEM; | |
146 | ||
147 | return 0; | |
148 | } | |
149 | ||
150 | static int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
151 | Unit *u = data; | |
152 | DBusMessageIter sub; | |
153 | char *p; | |
154 | ||
155 | assert(m); | |
156 | assert(i); | |
157 | assert(property); | |
158 | assert(u); | |
159 | ||
160 | if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub)) | |
161 | return -ENOMEM; | |
162 | ||
163 | if (u->meta.job) { | |
164 | ||
165 | if (!(p = job_dbus_path(u->meta.job))) | |
166 | return -ENOMEM; | |
167 | ||
168 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) || | |
86ad3bc1 | 169 | !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) { |
38131695 LP |
170 | free(p); |
171 | return -ENOMEM; | |
172 | } | |
173 | } else { | |
174 | uint32_t id = 0; | |
175 | ||
176 | /* No job, so let's fill in some placeholder | |
177 | * data. Since we need to fill in a valid path we | |
178 | * simple point to ourselves. */ | |
179 | ||
180 | if (!(p = unit_dbus_path(u))) | |
181 | return -ENOMEM; | |
182 | ||
183 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) || | |
86ad3bc1 | 184 | !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) { |
38131695 LP |
185 | free(p); |
186 | return -ENOMEM; | |
187 | } | |
188 | } | |
189 | ||
190 | free(p); | |
191 | ||
192 | if (!dbus_message_iter_close_container(i, &sub)) | |
193 | return -ENOMEM; | |
194 | ||
195 | return 0; | |
196 | } | |
197 | ||
ea430986 LP |
198 | static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusMessage *message) { |
199 | ||
200 | const BusProperty properties[] = { | |
38131695 LP |
201 | { "org.freedesktop.systemd1.Unit", "Id", bus_unit_append_id, "s", u }, |
202 | { "org.freedesktop.systemd1.Unit", "Description", bus_unit_append_description, "s", u }, | |
6f4706b7 | 203 | { "org.freedesktop.systemd1.Unit", "LoadState", bus_unit_append_load_state, "s", &u->meta.load_state }, |
38131695 | 204 | { "org.freedesktop.systemd1.Unit", "ActiveState", bus_unit_append_active_state, "s", u }, |
6be1e7d5 | 205 | { "org.freedesktop.systemd1.Unit", "FragmentPath", bus_property_append_string, "s", u->meta.fragment_path }, |
38131695 LP |
206 | { "org.freedesktop.systemd1.Unit", "ActiveEnterTimestamp", bus_property_append_uint64, "t", &u->meta.active_enter_timestamp }, |
207 | { "org.freedesktop.systemd1.Unit", "ActiveExitTimestamp", bus_property_append_uint64, "t", &u->meta.active_exit_timestamp }, | |
208 | { "org.freedesktop.systemd1.Unit", "CanReload", bus_unit_append_can_reload, "b", u }, | |
209 | { "org.freedesktop.systemd1.Unit", "CanStart", bus_unit_append_can_start, "b", u }, | |
86ad3bc1 | 210 | { "org.freedesktop.systemd1.Unit", "Job", bus_unit_append_job, "(uo)", u }, |
ea430986 LP |
211 | { NULL, NULL, NULL, NULL, NULL } |
212 | }; | |
213 | ||
b548631a LP |
214 | DBusMessage *reply = NULL; |
215 | Manager *m = u->meta.manager; | |
216 | DBusError error; | |
217 | JobType job_type = _JOB_TYPE_INVALID; | |
218 | ||
219 | dbus_error_init(&error); | |
220 | ||
221 | if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start")) | |
222 | job_type = JOB_START; | |
223 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop")) | |
224 | job_type = JOB_STOP; | |
225 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload")) | |
226 | job_type = JOB_RELOAD; | |
227 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart")) | |
228 | job_type = JOB_RESTART; | |
229 | else | |
230 | return bus_default_message_handler(u->meta.manager, message, introspection, properties); | |
231 | ||
232 | if (job_type != _JOB_TYPE_INVALID) { | |
233 | const char *smode; | |
234 | JobMode mode; | |
235 | Job *j; | |
236 | int r; | |
237 | char *path; | |
238 | ||
239 | if (!dbus_message_get_args( | |
240 | message, | |
241 | &error, | |
242 | DBUS_TYPE_STRING, &smode, | |
243 | DBUS_TYPE_INVALID)) | |
244 | return bus_send_error_reply(m, message, &error, -EINVAL); | |
245 | ||
246 | if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) | |
247 | return bus_send_error_reply(m, message, NULL, -EINVAL); | |
248 | ||
249 | if ((r = manager_add_job(m, job_type, u, mode, true, &j)) < 0) | |
250 | return bus_send_error_reply(m, message, NULL, r); | |
251 | ||
252 | if (!(reply = dbus_message_new_method_return(message))) | |
253 | goto oom; | |
254 | ||
255 | if (!(path = job_dbus_path(j))) | |
256 | goto oom; | |
257 | ||
258 | if (!dbus_message_append_args( | |
259 | reply, | |
260 | DBUS_TYPE_OBJECT_PATH, &path, | |
261 | DBUS_TYPE_INVALID)) | |
262 | goto oom; | |
263 | } | |
264 | ||
265 | if (reply) { | |
f278026d | 266 | if (!dbus_connection_send(m->api_bus, reply, NULL)) |
b548631a LP |
267 | goto oom; |
268 | ||
269 | dbus_message_unref(reply); | |
270 | } | |
271 | ||
272 | return DBUS_HANDLER_RESULT_HANDLED; | |
273 | ||
274 | oom: | |
275 | if (reply) | |
276 | dbus_message_unref(reply); | |
277 | ||
278 | dbus_error_free(&error); | |
279 | ||
280 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
ea430986 LP |
281 | } |
282 | ||
283 | static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage *message, void *data) { | |
284 | Manager *m = data; | |
285 | Unit *u; | |
286 | int r; | |
287 | ||
288 | assert(connection); | |
289 | assert(message); | |
290 | assert(m); | |
291 | ||
292 | log_debug("Got D-Bus request: %s.%s() on %s", | |
293 | dbus_message_get_interface(message), | |
294 | dbus_message_get_member(message), | |
295 | dbus_message_get_path(message)); | |
296 | ||
297 | if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) { | |
298 | ||
299 | if (r == -ENOMEM) | |
300 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
301 | ||
302 | if (r == -ENOENT) | |
303 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | |
304 | ||
305 | return bus_send_error_reply(m, message, NULL, r); | |
306 | } | |
307 | ||
308 | return bus_unit_message_dispatch(u, message); | |
309 | } | |
310 | ||
311 | const DBusObjectPathVTable bus_unit_vtable = { | |
312 | .message_function = bus_unit_message_handler | |
313 | }; | |
c1e1601e LP |
314 | |
315 | void bus_unit_send_change_signal(Unit *u) { | |
316 | char *p = NULL; | |
317 | DBusMessage *m = NULL; | |
318 | ||
319 | assert(u); | |
320 | assert(u->meta.in_dbus_queue); | |
321 | ||
322 | LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta); | |
323 | u->meta.in_dbus_queue = false; | |
324 | ||
325 | if (set_isempty(u->meta.manager->subscribed)) | |
326 | return; | |
327 | ||
328 | if (!(p = unit_dbus_path(u))) | |
329 | goto oom; | |
330 | ||
331 | if (u->meta.sent_dbus_new_signal) { | |
332 | /* Send a change signal */ | |
333 | ||
334 | if (!(m = dbus_message_new_signal(p, "org.freedesktop.systemd1.Unit", "Changed"))) | |
335 | goto oom; | |
336 | } else { | |
337 | const char *id; | |
338 | /* Send a new signal */ | |
339 | ||
340 | if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "UnitNew"))) | |
341 | goto oom; | |
342 | ||
343 | id = unit_id(u); | |
344 | if (!dbus_message_append_args(m, | |
345 | DBUS_TYPE_STRING, &id, | |
346 | DBUS_TYPE_OBJECT_PATH, &p, | |
347 | DBUS_TYPE_INVALID)) | |
348 | goto oom; | |
349 | } | |
350 | ||
f278026d | 351 | if (!dbus_connection_send(u->meta.manager->api_bus, m, NULL)) |
c1e1601e LP |
352 | goto oom; |
353 | ||
354 | free(p); | |
355 | dbus_message_unref(m); | |
356 | ||
357 | u->meta.sent_dbus_new_signal = true; | |
358 | ||
359 | return; | |
360 | ||
361 | oom: | |
362 | free(p); | |
363 | ||
364 | if (m) | |
365 | dbus_message_unref(m); | |
366 | ||
367 | log_error("Failed to allocate unit change/new signal."); | |
368 | } | |
369 | ||
370 | void bus_unit_send_removed_signal(Unit *u) { | |
371 | char *p = NULL; | |
372 | DBusMessage *m = NULL; | |
373 | const char *id; | |
374 | ||
375 | assert(u); | |
376 | ||
377 | if (set_isempty(u->meta.manager->subscribed) || !u->meta.sent_dbus_new_signal) | |
378 | return; | |
379 | ||
380 | if (!(p = unit_dbus_path(u))) | |
381 | goto oom; | |
382 | ||
383 | if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "UnitRemoved"))) | |
384 | goto oom; | |
385 | ||
386 | id = unit_id(u); | |
387 | if (!dbus_message_append_args(m, | |
388 | DBUS_TYPE_STRING, &id, | |
389 | DBUS_TYPE_OBJECT_PATH, &p, | |
390 | DBUS_TYPE_INVALID)) | |
391 | goto oom; | |
392 | ||
f278026d | 393 | if (!dbus_connection_send(u->meta.manager->api_bus, m, NULL)) |
c1e1601e LP |
394 | goto oom; |
395 | ||
396 | free(p); | |
397 | dbus_message_unref(m); | |
398 | ||
399 | return; | |
400 | ||
401 | oom: | |
402 | free(p); | |
403 | ||
404 | if (m) | |
405 | dbus_message_unref(m); | |
406 | ||
407 | log_error("Failed to allocate unit remove signal."); | |
408 | } |