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