]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/dbus-manager.c
manager: measure startup times
[thirdparty/systemd.git] / src / dbus-manager.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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-manager.h"
27 #include "strv.h"
28 #include "bus-errors.h"
29
30 #define BUS_MANAGER_INTERFACE \
31 " <interface name=\"org.freedesktop.systemd1.Manager\">\n" \
32 " <method name=\"GetUnit\">\n" \
33 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
34 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
35 " </method>\n" \
36 " <method name=\"GetUnitByPID\">\n" \
37 " <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n" \
38 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
39 " </method>\n" \
40 " <method name=\"LoadUnit\">\n" \
41 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
42 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
43 " </method>\n" \
44 " <method name=\"StartUnit\">\n" \
45 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
46 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
47 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
48 " </method>\n" \
49 " <method name=\"StopUnit\">\n" \
50 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
51 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
52 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
53 " </method>\n" \
54 " <method name=\"ReloadUnit\">\n" \
55 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
56 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
57 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
58 " </method>\n" \
59 " <method name=\"RestartUnit\">\n" \
60 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
61 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
62 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
63 " </method>\n" \
64 " <method name=\"TryRestartUnit\">\n" \
65 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
66 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
67 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
68 " </method>\n" \
69 " <method name=\"ReloadOrRestartUnit\">\n" \
70 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
71 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
72 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
73 " </method>\n" \
74 " <method name=\"ReloadOrTryRestartUnit\">\n" \
75 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
76 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
77 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
78 " </method>\n" \
79 " <method name=\"ResetFailedUnit\">\n" \
80 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
81 " </method>\n" \
82 " <method name=\"GetJob\">\n" \
83 " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
84 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
85 " </method>\n" \
86 " <method name=\"ClearJobs\"/>\n" \
87 " <method name=\"ResetFailed\"/>\n" \
88 " <method name=\"ListUnits\">\n" \
89 " <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
90 " </method>\n" \
91 " <method name=\"ListJobs\">\n" \
92 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
93 " </method>\n" \
94 " <method name=\"Subscribe\"/>\n" \
95 " <method name=\"Unsubscribe\"/>\n" \
96 " <method name=\"Dump\"/>\n" \
97 " <method name=\"CreateSnapshot\">\n" \
98 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
99 " <arg name=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \
100 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
101 " </method>\n" \
102 " <method name=\"Reload\"/>\n" \
103 " <method name=\"Reexecute\"/>\n" \
104 " <method name=\"Exit\"/>\n" \
105 " <method name=\"SetEnvironment\">\n" \
106 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
107 " </method>\n" \
108 " <method name=\"UnsetEnvironment\">\n" \
109 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
110 " </method>\n" \
111 " <signal name=\"UnitNew\">\n" \
112 " <arg name=\"id\" type=\"s\"/>\n" \
113 " <arg name=\"unit\" type=\"o\"/>\n" \
114 " </signal>\n" \
115 " <signal name=\"UnitRemoved\">\n" \
116 " <arg name=\"id\" type=\"s\"/>\n" \
117 " <arg name=\"unit\" type=\"o\"/>\n" \
118 " </signal>\n" \
119 " <signal name=\"JobNew\">\n" \
120 " <arg name=\"id\" type=\"u\"/>\n" \
121 " <arg name=\"job\" type=\"o\"/>\n" \
122 " </signal>\n" \
123 " <signal name=\"JobRemoved\">\n" \
124 " <arg name=\"id\" type=\"u\"/>\n" \
125 " <arg name=\"job\" type=\"o\"/>\n" \
126 " <arg name=\"success\" type=\"b\"/>\n" \
127 " </signal>" \
128 " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
129 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
130 " <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
131 " <property name=\"LogLevel\" type=\"s\" access=\"read\"/>\n" \
132 " <property name=\"LogTarget\" type=\"s\" access=\"read\"/>\n" \
133 " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
134 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
135 " <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \
136 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
137 " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
138 " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
139 " <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
140 " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
141 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
142 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n" \
143 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
144 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
145 " <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
146 " <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n" \
147 " </interface>\n"
148
149 #define INTROSPECTION_BEGIN \
150 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
151 "<node>\n" \
152 BUS_MANAGER_INTERFACE \
153 BUS_PROPERTIES_INTERFACE \
154 BUS_PEER_INTERFACE \
155 BUS_INTROSPECTABLE_INTERFACE
156
157 #define INTROSPECTION_END \
158 "</node>\n"
159
160 const char bus_manager_interface[] = BUS_MANAGER_INTERFACE;
161
162 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
163
164 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
165 const char *t;
166
167 assert(m);
168 assert(i);
169 assert(property);
170
171 t = log_target_to_string(log_get_target());
172
173 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
174 return -ENOMEM;
175
176 return 0;
177 }
178
179 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
180 const char *t;
181
182 assert(m);
183 assert(i);
184 assert(property);
185
186 t = log_level_to_string(log_get_max_level());
187
188 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
189 return -ENOMEM;
190
191 return 0;
192 }
193
194 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
195 uint32_t u;
196
197 assert(m);
198 assert(i);
199 assert(property);
200
201 u = hashmap_size(m->units);
202
203 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
204 return -ENOMEM;
205
206 return 0;
207 }
208
209 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
210 uint32_t u;
211
212 assert(m);
213 assert(i);
214 assert(property);
215
216 u = hashmap_size(m->jobs);
217
218 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
219 return -ENOMEM;
220
221 return 0;
222 }
223
224 static const char *message_get_sender_with_fallback(DBusMessage *m) {
225 const char *s;
226
227 assert(m);
228
229 if ((s = dbus_message_get_sender(m)))
230 return s;
231
232 /* When the message came in from a direct connection the
233 * message will have no sender. We fix that here. */
234
235 return ":no-sender";
236 }
237
238 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
239 Manager *m = data;
240
241 const BusProperty properties[] = {
242 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
243 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
244 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime },
245 { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64, "t", &m->finish_timestamp.realtime },
246 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL },
247 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL },
248 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL },
249 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL },
250 { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32, "u", &m->n_installed_jobs },
251 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
252 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn },
253 { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status },
254 { "org.freedesktop.systemd1.Manager", "SysVConsole", bus_property_append_bool, "b", &m->sysv_console },
255 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path },
256 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path },
257 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path },
258 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket },
259 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
260 { "org.freedesktop.systemd1.Manager", "MountAuto", bus_property_append_bool, "b", &m->mount_auto },
261 { "org.freedesktop.systemd1.Manager", "SwapAuto", bus_property_append_bool, "b", &m->swap_auto },
262 { NULL, NULL, NULL, NULL, NULL }
263 };
264
265 int r;
266 DBusError error;
267 DBusMessage *reply = NULL;
268 char * path = NULL;
269 JobType job_type = _JOB_TYPE_INVALID;
270 bool reload_if_possible = false;
271
272 assert(connection);
273 assert(message);
274 assert(m);
275
276 dbus_error_init(&error);
277
278 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
279 const char *name;
280 Unit *u;
281
282 if (!dbus_message_get_args(
283 message,
284 &error,
285 DBUS_TYPE_STRING, &name,
286 DBUS_TYPE_INVALID))
287 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
288
289 if (!(u = manager_get_unit(m, name))) {
290 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
291 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
292 }
293
294 if (!(reply = dbus_message_new_method_return(message)))
295 goto oom;
296
297 if (!(path = unit_dbus_path(u)))
298 goto oom;
299
300 if (!dbus_message_append_args(
301 reply,
302 DBUS_TYPE_OBJECT_PATH, &path,
303 DBUS_TYPE_INVALID))
304 goto oom;
305 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
306 Unit *u;
307 uint32_t pid;
308
309 if (!dbus_message_get_args(
310 message,
311 &error,
312 DBUS_TYPE_UINT32, &pid,
313 DBUS_TYPE_INVALID))
314 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
315
316 if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) {
317 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
318 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
319 }
320
321 if (!(reply = dbus_message_new_method_return(message)))
322 goto oom;
323
324 if (!(path = unit_dbus_path(u)))
325 goto oom;
326
327 if (!dbus_message_append_args(
328 reply,
329 DBUS_TYPE_OBJECT_PATH, &path,
330 DBUS_TYPE_INVALID))
331 goto oom;
332 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
333 const char *name;
334 Unit *u;
335
336 if (!dbus_message_get_args(
337 message,
338 &error,
339 DBUS_TYPE_STRING, &name,
340 DBUS_TYPE_INVALID))
341 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
342
343 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 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 = unit_dbus_path(u)))
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 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
359 job_type = JOB_START;
360 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
361 job_type = JOB_STOP;
362 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
363 job_type = JOB_RELOAD;
364 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
365 job_type = JOB_RESTART;
366 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
367 job_type = JOB_TRY_RESTART;
368 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
369 reload_if_possible = true;
370 job_type = JOB_RESTART;
371 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
372 reload_if_possible = true;
373 job_type = JOB_TRY_RESTART;
374 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
375 uint32_t id;
376 Job *j;
377
378 if (!dbus_message_get_args(
379 message,
380 &error,
381 DBUS_TYPE_UINT32, &id,
382 DBUS_TYPE_INVALID))
383 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
384
385 if (!(j = manager_get_job(m, id))) {
386 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
387 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
388 }
389
390 if (!(reply = dbus_message_new_method_return(message)))
391 goto oom;
392
393 if (!(path = job_dbus_path(j)))
394 goto oom;
395
396 if (!dbus_message_append_args(
397 reply,
398 DBUS_TYPE_OBJECT_PATH, &path,
399 DBUS_TYPE_INVALID))
400 goto oom;
401
402 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
403
404 manager_clear_jobs(m);
405
406 if (!(reply = dbus_message_new_method_return(message)))
407 goto oom;
408
409 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
410
411 manager_reset_failed(m);
412
413 if (!(reply = dbus_message_new_method_return(message)))
414 goto oom;
415
416 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
417 const char *name;
418 Unit *u;
419
420 if (!dbus_message_get_args(
421 message,
422 &error,
423 DBUS_TYPE_STRING, &name,
424 DBUS_TYPE_INVALID))
425 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
426
427 if (!(u = manager_get_unit(m, name))) {
428 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
429 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
430 }
431
432 unit_reset_failed(u);
433
434 if (!(reply = dbus_message_new_method_return(message)))
435 goto oom;
436
437 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
438 DBusMessageIter iter, sub;
439 Iterator i;
440 Unit *u;
441 const char *k;
442
443 if (!(reply = dbus_message_new_method_return(message)))
444 goto oom;
445
446 dbus_message_iter_init_append(reply, &iter);
447
448 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
449 goto oom;
450
451 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
452 char *u_path, *j_path;
453 const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
454 DBusMessageIter sub2;
455 uint32_t job_id;
456 Unit *f;
457
458 if (k != u->meta.id)
459 continue;
460
461 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
462 goto oom;
463
464 description = unit_description(u);
465 load_state = unit_load_state_to_string(u->meta.load_state);
466 active_state = unit_active_state_to_string(unit_active_state(u));
467 sub_state = unit_sub_state_to_string(u);
468
469 f = unit_following(u);
470 following = f ? f->meta.id : "";
471
472 if (!(u_path = unit_dbus_path(u)))
473 goto oom;
474
475 if (u->meta.job) {
476 job_id = (uint32_t) u->meta.job->id;
477
478 if (!(j_path = job_dbus_path(u->meta.job))) {
479 free(u_path);
480 goto oom;
481 }
482
483 sjob_type = job_type_to_string(u->meta.job->type);
484 } else {
485 job_id = 0;
486 j_path = u_path;
487 sjob_type = "";
488 }
489
490 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
491 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
492 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
493 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
494 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
495 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
496 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
497 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
498 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
499 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
500 free(u_path);
501 if (u->meta.job)
502 free(j_path);
503 goto oom;
504 }
505
506 free(u_path);
507 if (u->meta.job)
508 free(j_path);
509
510 if (!dbus_message_iter_close_container(&sub, &sub2))
511 goto oom;
512 }
513
514 if (!dbus_message_iter_close_container(&iter, &sub))
515 goto oom;
516
517 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
518 DBusMessageIter iter, sub;
519 Iterator i;
520 Job *j;
521
522 if (!(reply = dbus_message_new_method_return(message)))
523 goto oom;
524
525 dbus_message_iter_init_append(reply, &iter);
526
527 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
528 goto oom;
529
530 HASHMAP_FOREACH(j, m->jobs, i) {
531 char *u_path, *j_path;
532 const char *state, *type;
533 uint32_t id;
534 DBusMessageIter sub2;
535
536 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
537 goto oom;
538
539 id = (uint32_t) j->id;
540 state = job_state_to_string(j->state);
541 type = job_type_to_string(j->type);
542
543 if (!(j_path = job_dbus_path(j)))
544 goto oom;
545
546 if (!(u_path = unit_dbus_path(j->unit))) {
547 free(j_path);
548 goto oom;
549 }
550
551 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
552 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
553 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
554 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
555 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
556 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
557 free(j_path);
558 free(u_path);
559 goto oom;
560 }
561
562 free(j_path);
563 free(u_path);
564
565 if (!dbus_message_iter_close_container(&sub, &sub2))
566 goto oom;
567 }
568
569 if (!dbus_message_iter_close_container(&iter, &sub))
570 goto oom;
571
572 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
573 char *client;
574 Set *s;
575
576 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
577 if (!(s = set_new(string_hash_func, string_compare_func)))
578 goto oom;
579
580 if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
581 set_free(s);
582 goto oom;
583 }
584 }
585
586 if (!(client = strdup(message_get_sender_with_fallback(message))))
587 goto oom;
588
589 if ((r = set_put(s, client)) < 0) {
590 free(client);
591 return bus_send_error_reply(m, connection, message, NULL, r);
592 }
593
594 if (!(reply = dbus_message_new_method_return(message)))
595 goto oom;
596
597 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
598 char *client;
599
600 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
601 dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
602 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
603 }
604
605 free(client);
606
607 if (!(reply = dbus_message_new_method_return(message)))
608 goto oom;
609
610 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
611 FILE *f;
612 char *dump = NULL;
613 size_t size;
614
615 if (!(reply = dbus_message_new_method_return(message)))
616 goto oom;
617
618 if (!(f = open_memstream(&dump, &size)))
619 goto oom;
620
621 manager_dump_units(m, f, NULL);
622 manager_dump_jobs(m, f, NULL);
623
624 if (ferror(f)) {
625 fclose(f);
626 free(dump);
627 goto oom;
628 }
629
630 fclose(f);
631
632 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
633 free(dump);
634 goto oom;
635 }
636
637 free(dump);
638 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
639 const char *name;
640 dbus_bool_t cleanup;
641 Snapshot *s;
642
643 if (!dbus_message_get_args(
644 message,
645 &error,
646 DBUS_TYPE_STRING, &name,
647 DBUS_TYPE_BOOLEAN, &cleanup,
648 DBUS_TYPE_INVALID))
649 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
650
651 if (name && name[0] == 0)
652 name = NULL;
653
654 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
655 return bus_send_error_reply(m, connection, message, &error, r);
656
657 if (!(reply = dbus_message_new_method_return(message)))
658 goto oom;
659
660 if (!(path = unit_dbus_path(UNIT(s))))
661 goto oom;
662
663 if (!dbus_message_append_args(
664 reply,
665 DBUS_TYPE_OBJECT_PATH, &path,
666 DBUS_TYPE_INVALID))
667 goto oom;
668
669 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
670 char *introspection = NULL;
671 FILE *f;
672 Iterator i;
673 Unit *u;
674 Job *j;
675 const char *k;
676 size_t size;
677
678 if (!(reply = dbus_message_new_method_return(message)))
679 goto oom;
680
681 /* We roll our own introspection code here, instead of
682 * relying on bus_default_message_handler() because we
683 * need to generate our introspection string
684 * dynamically. */
685
686 if (!(f = open_memstream(&introspection, &size)))
687 goto oom;
688
689 fputs(INTROSPECTION_BEGIN, f);
690
691 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
692 char *p;
693
694 if (k != u->meta.id)
695 continue;
696
697 if (!(p = bus_path_escape(k))) {
698 fclose(f);
699 free(introspection);
700 goto oom;
701 }
702
703 fprintf(f, "<node name=\"unit/%s\"/>", p);
704 free(p);
705 }
706
707 HASHMAP_FOREACH(j, m->jobs, i)
708 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
709
710 fputs(INTROSPECTION_END, f);
711
712 if (ferror(f)) {
713 fclose(f);
714 free(introspection);
715 goto oom;
716 }
717
718 fclose(f);
719
720 if (!introspection)
721 goto oom;
722
723 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
724 free(introspection);
725 goto oom;
726 }
727
728 free(introspection);
729
730 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
731
732 assert(!m->queued_message);
733
734 /* Instead of sending the reply back right away, we
735 * just remember that we need to and then send it
736 * after the reload is finished. That way the caller
737 * knows when the reload finished. */
738
739 if (!(m->queued_message = dbus_message_new_method_return(message)))
740 goto oom;
741
742 m->queued_message_connection = connection;
743 m->exit_code = MANAGER_RELOAD;
744
745 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
746
747 if (!(reply = dbus_message_new_method_return(message)))
748 goto oom;
749
750 m->exit_code = MANAGER_REEXECUTE;
751
752 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
753
754 if (m->running_as == MANAGER_SYSTEM) {
755 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for session managers.");
756 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
757 }
758
759 if (!(reply = dbus_message_new_method_return(message)))
760 goto oom;
761
762 m->exit_code = MANAGER_EXIT;
763
764 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
765 char **l = NULL, **e = NULL;
766
767 if ((r = bus_parse_strv(message, &l)) < 0) {
768 if (r == -ENOMEM)
769 goto oom;
770
771 return bus_send_error_reply(m, connection, message, NULL, r);
772 }
773
774 e = strv_env_merge(2, m->environment, l);
775 strv_free(l);
776
777 if (!e)
778 goto oom;
779
780 if (!(reply = dbus_message_new_method_return(message))) {
781 strv_free(e);
782 goto oom;
783 }
784
785 strv_free(m->environment);
786 m->environment = e;
787
788 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
789 char **l = NULL, **e = NULL;
790
791 if ((r = bus_parse_strv(message, &l)) < 0) {
792 if (r == -ENOMEM)
793 goto oom;
794
795 return bus_send_error_reply(m, connection, message, NULL, r);
796 }
797
798 e = strv_env_delete(m->environment, 1, l);
799 strv_free(l);
800
801 if (!e)
802 goto oom;
803
804 if (!(reply = dbus_message_new_method_return(message)))
805 goto oom;
806
807 strv_free(m->environment);
808 m->environment = e;
809
810 } else
811 return bus_default_message_handler(m, connection, message, NULL, properties);
812
813 if (job_type != _JOB_TYPE_INVALID) {
814 const char *name, *smode;
815 JobMode mode;
816 Job *j;
817 Unit *u;
818
819 if (!dbus_message_get_args(
820 message,
821 &error,
822 DBUS_TYPE_STRING, &name,
823 DBUS_TYPE_STRING, &smode,
824 DBUS_TYPE_INVALID))
825 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
826
827 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
828 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
829 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
830 }
831
832 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
833 return bus_send_error_reply(m, connection, message, &error, r);
834
835 if (reload_if_possible && unit_can_reload(u)) {
836 if (job_type == JOB_RESTART)
837 job_type = JOB_RELOAD_OR_START;
838 else if (job_type == JOB_TRY_RESTART)
839 job_type = JOB_RELOAD;
840 }
841
842 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
843 (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
844 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
845 (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
846 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
847 return bus_send_error_reply(m, connection, message, &error, -EPERM);
848 }
849
850 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
851 return bus_send_error_reply(m, connection, message, &error, r);
852
853 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
854 goto oom;
855
856 j->bus = connection;
857
858 if (!(reply = dbus_message_new_method_return(message)))
859 goto oom;
860
861 if (!(path = job_dbus_path(j)))
862 goto oom;
863
864 if (!dbus_message_append_args(
865 reply,
866 DBUS_TYPE_OBJECT_PATH, &path,
867 DBUS_TYPE_INVALID))
868 goto oom;
869 }
870
871 free(path);
872
873 if (reply) {
874 if (!dbus_connection_send(connection, reply, NULL))
875 goto oom;
876
877 dbus_message_unref(reply);
878 }
879
880 return DBUS_HANDLER_RESULT_HANDLED;
881
882 oom:
883 free(path);
884
885 if (reply)
886 dbus_message_unref(reply);
887
888 dbus_error_free(&error);
889
890 return DBUS_HANDLER_RESULT_NEED_MEMORY;
891 }
892
893 const DBusObjectPathVTable bus_manager_vtable = {
894 .message_function = bus_manager_message_handler
895 };