]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/dbus-unit.c
device: do not merge devices
[thirdparty/systemd.git] / src / 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"
4139c1b2 26#include "dbus-unit.h"
398ef8ba 27#include "bus-errors.h"
ea430986 28
4288f619
LP
29const char bus_unit_interface[] = BUS_UNIT_INTERFACE;
30
4139c1b2
LP
31int bus_unit_append_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
32 char *t;
33 Iterator j;
34 DBusMessageIter sub;
35 Unit *u = data;
36
37 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
38 return -ENOMEM;
39
40 SET_FOREACH(t, u->meta.names, j)
41 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t))
42 return -ENOMEM;
43
44 if (!dbus_message_iter_close_container(i, &sub))
45 return -ENOMEM;
46
47 return 0;
48}
49
8fe914ec
LP
50int bus_unit_append_following(Manager *m, DBusMessageIter *i, const char *property, void *data) {
51 Unit *u = data;
52 const char *d;
53
54 assert(m);
55 assert(i);
56 assert(property);
57 assert(u);
58
59 d = u->meta.following ? u->meta.following->meta.id : "";
60
61 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
62 return -ENOMEM;
63
64 return 0;
65}
66
5301be81
LP
67int bus_unit_append_dependencies(Manager *m, DBusMessageIter *i, const char *property, void *data) {
68 Unit *u;
69 Iterator j;
70 DBusMessageIter sub;
71 Set *s = data;
72
73 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
74 return -ENOMEM;
75
76 SET_FOREACH(u, s, j)
77 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &u->meta.id))
78 return -ENOMEM;
79
80 if (!dbus_message_iter_close_container(i, &sub))
81 return -ENOMEM;
82
83 return 0;
84}
85
4139c1b2 86int bus_unit_append_description(Manager *m, DBusMessageIter *i, const char *property, void *data) {
ea430986
LP
87 Unit *u = data;
88 const char *d;
89
90 assert(m);
91 assert(i);
92 assert(property);
93 assert(u);
94
95 d = unit_description(u);
96
97 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
98 return -ENOMEM;
99
100 return 0;
101}
102
6f4706b7 103DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_load_state, unit_load_state, UnitLoadState);
ea430986 104
4139c1b2 105int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
ea430986
LP
106 Unit *u = data;
107 const char *state;
108
109 assert(m);
110 assert(i);
111 assert(property);
112 assert(u);
113
114 state = unit_active_state_to_string(unit_active_state(u));
115
116 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
117 return -ENOMEM;
118
119 return 0;
120}
121
4139c1b2 122int bus_unit_append_sub_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
10a94420
LP
123 Unit *u = data;
124 const char *state;
125
126 assert(m);
127 assert(i);
128 assert(property);
129 assert(u);
130
131 state = unit_sub_state_to_string(u);
132
133 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
134 return -ENOMEM;
135
136 return 0;
137}
138
4139c1b2 139int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *property, void *data) {
38131695
LP
140 Unit *u = data;
141 dbus_bool_t b;
142
143 assert(m);
144 assert(i);
145 assert(property);
146 assert(u);
147
064f51fa
LP
148 b = unit_can_start(u) &&
149 !u->meta.only_by_dependency;
38131695
LP
150
151 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
152 return -ENOMEM;
153
154 return 0;
155}
156
4139c1b2 157int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
38131695
LP
158 Unit *u = data;
159 dbus_bool_t b;
160
161 assert(m);
162 assert(i);
163 assert(property);
164 assert(u);
165
4139c1b2 166 b = unit_can_reload(u);
38131695
LP
167
168 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
169 return -ENOMEM;
170
171 return 0;
172}
173
4139c1b2 174int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) {
38131695
LP
175 Unit *u = data;
176 DBusMessageIter sub;
177 char *p;
178
179 assert(m);
180 assert(i);
181 assert(property);
182 assert(u);
183
184 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
185 return -ENOMEM;
186
187 if (u->meta.job) {
188
189 if (!(p = job_dbus_path(u->meta.job)))
190 return -ENOMEM;
191
192 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) ||
86ad3bc1 193 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
38131695
LP
194 free(p);
195 return -ENOMEM;
196 }
197 } else {
198 uint32_t id = 0;
199
200 /* No job, so let's fill in some placeholder
201 * data. Since we need to fill in a valid path we
202 * simple point to ourselves. */
203
204 if (!(p = unit_dbus_path(u)))
205 return -ENOMEM;
206
207 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) ||
86ad3bc1 208 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
38131695
LP
209 free(p);
210 return -ENOMEM;
211 }
212 }
213
214 free(p);
215
216 if (!dbus_message_iter_close_container(i, &sub))
217 return -ENOMEM;
218
219 return 0;
220}
221
4139c1b2
LP
222int bus_unit_append_default_cgroup(Manager *m, DBusMessageIter *i, const char *property, void *data) {
223 Unit *u = data;
224 char *t;
225 CGroupBonding *cgb;
226 bool success;
ea430986 227
4139c1b2
LP
228 assert(m);
229 assert(i);
230 assert(property);
231 assert(u);
232
233 if ((cgb = unit_get_default_cgroup(u))) {
234 if (!(t = cgroup_bonding_to_string(cgb)))
235 return -ENOMEM;
236 } else
237 t = (char*) "";
ea430986 238
4139c1b2
LP
239 success = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t);
240
241 if (cgb)
242 free(t);
243
244 return success ? 0 : -ENOMEM;
245}
246
247int bus_unit_append_cgroups(Manager *m, DBusMessageIter *i, const char *property, void *data) {
248 Unit *u = data;
249 CGroupBonding *cgb;
250 DBusMessageIter sub;
251
252 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
253 return -ENOMEM;
254
255 LIST_FOREACH(by_unit, cgb, u->meta.cgroup_bondings) {
256 char *t;
257 bool success;
258
259 if (!(t = cgroup_bonding_to_string(cgb)))
260 return -ENOMEM;
261
262 success = dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t);
263 free(t);
264
265 if (!success)
266 return -ENOMEM;
267 }
268
269 if (!dbus_message_iter_close_container(i, &sub))
270 return -ENOMEM;
271
272 return 0;
273}
274
45fb0699
LP
275int bus_unit_append_need_daemon_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
276 Unit *u = data;
277 dbus_bool_t b;
278
279 assert(m);
280 assert(i);
281 assert(property);
282 assert(u);
283
284 b = unit_need_daemon_reload(u);
285
286 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
287 return -ENOMEM;
288
289 return 0;
290}
291
5e8d1c9a 292static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *connection, DBusMessage *message) {
b548631a
LP
293 DBusMessage *reply = NULL;
294 Manager *m = u->meta.manager;
295 DBusError error;
296 JobType job_type = _JOB_TYPE_INVALID;
c87eba54 297 char *path = NULL;
6f28c033 298 bool reload_if_possible = false;
b548631a
LP
299
300 dbus_error_init(&error);
301
302 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start"))
303 job_type = JOB_START;
304 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop"))
305 job_type = JOB_STOP;
306 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload"))
307 job_type = JOB_RELOAD;
308 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart"))
309 job_type = JOB_RESTART;
9a1ac7b9
LP
310 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "TryRestart"))
311 job_type = JOB_TRY_RESTART;
6f28c033
LP
312 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrRestart")) {
313 reload_if_possible = true;
314 job_type = JOB_RESTART;
315 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrTryRestart")) {
316 reload_if_possible = true;
317 job_type = JOB_TRY_RESTART;
5632e374
LP
318 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ResetMaintenance")) {
319
320 unit_reset_maintenance(u);
321
322 if (!(reply = dbus_message_new_method_return(message)))
323 goto oom;
324
6f28c033 325 } else if (UNIT_VTABLE(u)->bus_message_handler)
5e8d1c9a 326 return UNIT_VTABLE(u)->bus_message_handler(u, connection, message);
b548631a 327 else
4139c1b2 328 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
b548631a
LP
329
330 if (job_type != _JOB_TYPE_INVALID) {
331 const char *smode;
332 JobMode mode;
333 Job *j;
334 int r;
b548631a 335
398ef8ba
LP
336 if (job_type == JOB_START && u->meta.only_by_dependency) {
337 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Unit may be activated by dependency only.");
338 return bus_send_error_reply(m, connection, message, &error, -EPERM);
339 }
bc0f8771 340
b548631a
LP
341 if (!dbus_message_get_args(
342 message,
343 &error,
344 DBUS_TYPE_STRING, &smode,
345 DBUS_TYPE_INVALID))
5e8d1c9a 346 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
b548631a 347
6f28c033
LP
348 if (reload_if_possible && unit_can_reload(u)) {
349 if (job_type == JOB_RESTART)
350 job_type = JOB_RELOAD_OR_START;
351 else if (job_type == JOB_TRY_RESTART)
352 job_type = JOB_RELOAD;
353 }
354
398ef8ba
LP
355 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
356 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
357 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
358 }
b548631a 359
398ef8ba
LP
360 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
361 return bus_send_error_reply(m, connection, message, &error, r);
b548631a
LP
362
363 if (!(reply = dbus_message_new_method_return(message)))
364 goto oom;
365
366 if (!(path = job_dbus_path(j)))
367 goto oom;
368
369 if (!dbus_message_append_args(
370 reply,
371 DBUS_TYPE_OBJECT_PATH, &path,
372 DBUS_TYPE_INVALID))
373 goto oom;
374 }
375
c87eba54
LP
376 free(path);
377
b548631a 378 if (reply) {
5e8d1c9a 379 if (!dbus_connection_send(connection, reply, NULL))
b548631a
LP
380 goto oom;
381
382 dbus_message_unref(reply);
383 }
384
385 return DBUS_HANDLER_RESULT_HANDLED;
386
387oom:
c87eba54
LP
388 free(path);
389
b548631a
LP
390 if (reply)
391 dbus_message_unref(reply);
392
393 dbus_error_free(&error);
394
395 return DBUS_HANDLER_RESULT_NEED_MEMORY;
ea430986
LP
396}
397
5e8d1c9a 398static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
ea430986
LP
399 Manager *m = data;
400 Unit *u;
401 int r;
402
403 assert(connection);
404 assert(message);
405 assert(m);
406
ea430986
LP
407 if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
408
409 if (r == -ENOMEM)
410 return DBUS_HANDLER_RESULT_NEED_MEMORY;
411
412 if (r == -ENOENT)
413 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
414
5e8d1c9a 415 return bus_send_error_reply(m, connection, message, NULL, r);
ea430986
LP
416 }
417
5e8d1c9a 418 return bus_unit_message_dispatch(u, connection, message);
ea430986
LP
419}
420
421const DBusObjectPathVTable bus_unit_vtable = {
422 .message_function = bus_unit_message_handler
423};
c1e1601e
LP
424
425void bus_unit_send_change_signal(Unit *u) {
426 char *p = NULL;
427 DBusMessage *m = NULL;
428
429 assert(u);
c1e1601e 430
c0bd0cf7
LP
431 if (u->meta.in_dbus_queue) {
432 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
433 u->meta.in_dbus_queue = false;
434 }
c1e1601e 435
a567261a 436 if (!bus_has_subscriber(u->meta.manager)) {
94b6dfa2 437 u->meta.sent_dbus_new_signal = true;
c1e1601e 438 return;
94b6dfa2 439 }
c1e1601e
LP
440
441 if (!(p = unit_dbus_path(u)))
442 goto oom;
443
444 if (u->meta.sent_dbus_new_signal) {
445 /* Send a change signal */
446
447 if (!(m = dbus_message_new_signal(p, "org.freedesktop.systemd1.Unit", "Changed")))
448 goto oom;
449 } else {
c1e1601e
LP
450 /* Send a new signal */
451
701cc384 452 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
c1e1601e
LP
453 goto oom;
454
c1e1601e 455 if (!dbus_message_append_args(m,
9e2f7c11 456 DBUS_TYPE_STRING, &u->meta.id,
c1e1601e
LP
457 DBUS_TYPE_OBJECT_PATH, &p,
458 DBUS_TYPE_INVALID))
459 goto oom;
460 }
461
5e8d1c9a 462 if (bus_broadcast(u->meta.manager, m) < 0)
c1e1601e
LP
463 goto oom;
464
465 free(p);
466 dbus_message_unref(m);
467
468 u->meta.sent_dbus_new_signal = true;
469
470 return;
471
472oom:
473 free(p);
474
475 if (m)
476 dbus_message_unref(m);
477
478 log_error("Failed to allocate unit change/new signal.");
479}
480
481void bus_unit_send_removed_signal(Unit *u) {
482 char *p = NULL;
483 DBusMessage *m = NULL;
c1e1601e
LP
484
485 assert(u);
486
a567261a 487 if (!bus_has_subscriber(u->meta.manager))
c1e1601e
LP
488 return;
489
7535cc78
LP
490 if (!u->meta.sent_dbus_new_signal)
491 bus_unit_send_change_signal(u);
492
c1e1601e
LP
493 if (!(p = unit_dbus_path(u)))
494 goto oom;
495
701cc384 496 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
c1e1601e
LP
497 goto oom;
498
c1e1601e 499 if (!dbus_message_append_args(m,
9e2f7c11 500 DBUS_TYPE_STRING, &u->meta.id,
c1e1601e
LP
501 DBUS_TYPE_OBJECT_PATH, &p,
502 DBUS_TYPE_INVALID))
503 goto oom;
504
5e8d1c9a 505 if (bus_broadcast(u->meta.manager, m) < 0)
c1e1601e
LP
506 goto oom;
507
508 free(p);
509 dbus_message_unref(m);
510
511 return;
512
513oom:
514 free(p);
515
516 if (m)
517 dbus_message_unref(m);
518
519 log_error("Failed to allocate unit remove signal.");
520}