]> git.ipfire.org Git - people/ms/systemd.git/blame - dbus-unit.c
manager: if wanted dependencies are invalid, don't fail
[people/ms/systemd.git] / dbus-unit.c
CommitLineData
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
27static 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
63static 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
80static 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
97static int bus_unit_append_load_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
98 Unit *u = data;
99 const char *state;
100
101 assert(m);
102 assert(i);
103 assert(property);
104 assert(u);
105
106 state = unit_load_state_to_string(u->meta.load_state);
107
108 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
109 return -ENOMEM;
110
111 return 0;
112}
113
114static int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
115 Unit *u = data;
116 const char *state;
117
118 assert(m);
119 assert(i);
120 assert(property);
121 assert(u);
122
123 state = unit_active_state_to_string(unit_active_state(u));
124
125 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
126 return -ENOMEM;
127
128 return 0;
129}
130
38131695
LP
131static int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
132 Unit *u = data;
133 dbus_bool_t b;
134
135 assert(m);
136 assert(i);
137 assert(property);
138 assert(u);
139
140 b = unit_can_reload(u);
141
142 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
143 return -ENOMEM;
144
145 return 0;
146}
147
148static int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *property, void *data) {
149 Unit *u = data;
150 dbus_bool_t b;
151
152 assert(m);
153 assert(i);
154 assert(property);
155 assert(u);
156
157 b = unit_can_start(u);
158
159 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
160 return -ENOMEM;
161
162 return 0;
163}
164
165static int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) {
166 Unit *u = data;
167 DBusMessageIter sub;
168 char *p;
169
170 assert(m);
171 assert(i);
172 assert(property);
173 assert(u);
174
175 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
176 return -ENOMEM;
177
178 if (u->meta.job) {
179
180 if (!(p = job_dbus_path(u->meta.job)))
181 return -ENOMEM;
182
183 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) ||
86ad3bc1 184 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
38131695
LP
185 free(p);
186 return -ENOMEM;
187 }
188 } else {
189 uint32_t id = 0;
190
191 /* No job, so let's fill in some placeholder
192 * data. Since we need to fill in a valid path we
193 * simple point to ourselves. */
194
195 if (!(p = unit_dbus_path(u)))
196 return -ENOMEM;
197
198 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) ||
86ad3bc1 199 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
38131695
LP
200 free(p);
201 return -ENOMEM;
202 }
203 }
204
205 free(p);
206
207 if (!dbus_message_iter_close_container(i, &sub))
208 return -ENOMEM;
209
210 return 0;
211}
212
ea430986
LP
213static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusMessage *message) {
214
215 const BusProperty properties[] = {
38131695
LP
216 { "org.freedesktop.systemd1.Unit", "Id", bus_unit_append_id, "s", u },
217 { "org.freedesktop.systemd1.Unit", "Description", bus_unit_append_description, "s", u },
218 { "org.freedesktop.systemd1.Unit", "LoadState", bus_unit_append_load_state, "s", u },
219 { "org.freedesktop.systemd1.Unit", "ActiveState", bus_unit_append_active_state, "s", u },
6be1e7d5 220 { "org.freedesktop.systemd1.Unit", "FragmentPath", bus_property_append_string, "s", u->meta.fragment_path },
38131695
LP
221 { "org.freedesktop.systemd1.Unit", "ActiveEnterTimestamp", bus_property_append_uint64, "t", &u->meta.active_enter_timestamp },
222 { "org.freedesktop.systemd1.Unit", "ActiveExitTimestamp", bus_property_append_uint64, "t", &u->meta.active_exit_timestamp },
223 { "org.freedesktop.systemd1.Unit", "CanReload", bus_unit_append_can_reload, "b", u },
224 { "org.freedesktop.systemd1.Unit", "CanStart", bus_unit_append_can_start, "b", u },
86ad3bc1 225 { "org.freedesktop.systemd1.Unit", "Job", bus_unit_append_job, "(uo)", u },
ea430986
LP
226 { NULL, NULL, NULL, NULL, NULL }
227 };
228
b548631a
LP
229 DBusMessage *reply = NULL;
230 Manager *m = u->meta.manager;
231 DBusError error;
232 JobType job_type = _JOB_TYPE_INVALID;
233
234 dbus_error_init(&error);
235
236 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start"))
237 job_type = JOB_START;
238 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop"))
239 job_type = JOB_STOP;
240 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload"))
241 job_type = JOB_RELOAD;
242 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart"))
243 job_type = JOB_RESTART;
244 else
245 return bus_default_message_handler(u->meta.manager, message, introspection, properties);
246
247 if (job_type != _JOB_TYPE_INVALID) {
248 const char *smode;
249 JobMode mode;
250 Job *j;
251 int r;
252 char *path;
253
254 if (!dbus_message_get_args(
255 message,
256 &error,
257 DBUS_TYPE_STRING, &smode,
258 DBUS_TYPE_INVALID))
259 return bus_send_error_reply(m, message, &error, -EINVAL);
260
261 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID)
262 return bus_send_error_reply(m, message, NULL, -EINVAL);
263
264 if ((r = manager_add_job(m, job_type, u, mode, true, &j)) < 0)
265 return bus_send_error_reply(m, message, NULL, r);
266
267 if (!(reply = dbus_message_new_method_return(message)))
268 goto oom;
269
270 if (!(path = job_dbus_path(j)))
271 goto oom;
272
273 if (!dbus_message_append_args(
274 reply,
275 DBUS_TYPE_OBJECT_PATH, &path,
276 DBUS_TYPE_INVALID))
277 goto oom;
278 }
279
280 if (reply) {
281 if (!dbus_connection_send(m->bus, reply, NULL))
282 goto oom;
283
284 dbus_message_unref(reply);
285 }
286
287 return DBUS_HANDLER_RESULT_HANDLED;
288
289oom:
290 if (reply)
291 dbus_message_unref(reply);
292
293 dbus_error_free(&error);
294
295 return DBUS_HANDLER_RESULT_NEED_MEMORY;
ea430986
LP
296}
297
298static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
299 Manager *m = data;
300 Unit *u;
301 int r;
302
303 assert(connection);
304 assert(message);
305 assert(m);
306
307 log_debug("Got D-Bus request: %s.%s() on %s",
308 dbus_message_get_interface(message),
309 dbus_message_get_member(message),
310 dbus_message_get_path(message));
311
312 if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
313
314 if (r == -ENOMEM)
315 return DBUS_HANDLER_RESULT_NEED_MEMORY;
316
317 if (r == -ENOENT)
318 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
319
320 return bus_send_error_reply(m, message, NULL, r);
321 }
322
323 return bus_unit_message_dispatch(u, message);
324}
325
326const DBusObjectPathVTable bus_unit_vtable = {
327 .message_function = bus_unit_message_handler
328};
c1e1601e
LP
329
330void bus_unit_send_change_signal(Unit *u) {
331 char *p = NULL;
332 DBusMessage *m = NULL;
333
334 assert(u);
335 assert(u->meta.in_dbus_queue);
336
337 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
338 u->meta.in_dbus_queue = false;
339
340 if (set_isempty(u->meta.manager->subscribed))
341 return;
342
343 if (!(p = unit_dbus_path(u)))
344 goto oom;
345
346 if (u->meta.sent_dbus_new_signal) {
347 /* Send a change signal */
348
349 if (!(m = dbus_message_new_signal(p, "org.freedesktop.systemd1.Unit", "Changed")))
350 goto oom;
351 } else {
352 const char *id;
353 /* Send a new signal */
354
355 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "UnitNew")))
356 goto oom;
357
358 id = unit_id(u);
359 if (!dbus_message_append_args(m,
360 DBUS_TYPE_STRING, &id,
361 DBUS_TYPE_OBJECT_PATH, &p,
362 DBUS_TYPE_INVALID))
363 goto oom;
364 }
365
366 if (!dbus_connection_send(u->meta.manager->bus, m, NULL))
367 goto oom;
368
369 free(p);
370 dbus_message_unref(m);
371
372 u->meta.sent_dbus_new_signal = true;
373
374 return;
375
376oom:
377 free(p);
378
379 if (m)
380 dbus_message_unref(m);
381
382 log_error("Failed to allocate unit change/new signal.");
383}
384
385void bus_unit_send_removed_signal(Unit *u) {
386 char *p = NULL;
387 DBusMessage *m = NULL;
388 const char *id;
389
390 assert(u);
391
392 if (set_isempty(u->meta.manager->subscribed) || !u->meta.sent_dbus_new_signal)
393 return;
394
395 if (!(p = unit_dbus_path(u)))
396 goto oom;
397
398 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "UnitRemoved")))
399 goto oom;
400
401 id = unit_id(u);
402 if (!dbus_message_append_args(m,
403 DBUS_TYPE_STRING, &id,
404 DBUS_TYPE_OBJECT_PATH, &p,
405 DBUS_TYPE_INVALID))
406 goto oom;
407
408 if (!dbus_connection_send(u->meta.manager->bus, m, NULL))
409 goto oom;
410
411 free(p);
412 dbus_message_unref(m);
413
414 return;
415
416oom:
417 free(p);
418
419 if (m)
420 dbus_message_unref(m);
421
422 log_error("Failed to allocate unit remove signal.");
423}