1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
26 #include "dbus-manager.h"
28 #include "bus-errors.h"
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" \
36 " <method name=\"GetUnitByPID\">\n" \
37 " <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n" \
38 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
40 " <method name=\"LoadUnit\">\n" \
41 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
42 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\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" \
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" \
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" \
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" \
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" \
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" \
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" \
79 " <method name=\"ResetFailedUnit\">\n" \
80 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
82 " <method name=\"GetJob\">\n" \
83 " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
84 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\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" \
91 " <method name=\"ListJobs\">\n" \
92 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\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" \
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" \
108 " <method name=\"UnsetEnvironment\">\n" \
109 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
111 " <signal name=\"UnitNew\">\n" \
112 " <arg name=\"id\" type=\"s\"/>\n" \
113 " <arg name=\"unit\" type=\"o\"/>\n" \
115 " <signal name=\"UnitRemoved\">\n" \
116 " <arg name=\"id\" type=\"s\"/>\n" \
117 " <arg name=\"unit\" type=\"o\"/>\n" \
119 " <signal name=\"JobNew\">\n" \
120 " <arg name=\"id\" type=\"u\"/>\n" \
121 " <arg name=\"job\" type=\"o\"/>\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" \
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=\"Environment\" type=\"as\" access=\"read\"/>\n" \
136 " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
137 " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
138 " <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
139 " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
140 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
141 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n" \
142 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
143 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
144 " <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
145 " <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n" \
148 #define INTROSPECTION_BEGIN \
149 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
151 BUS_MANAGER_INTERFACE \
152 BUS_PROPERTIES_INTERFACE \
154 BUS_INTROSPECTABLE_INTERFACE
156 #define INTROSPECTION_END \
159 const char bus_manager_interface[] = BUS_MANAGER_INTERFACE;
161 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
163 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
170 t = log_target_to_string(log_get_target());
172 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
178 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
185 t = log_level_to_string(log_get_max_level());
187 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
193 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
200 u = hashmap_size(m->units);
202 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
208 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
215 u = hashmap_size(m->jobs);
217 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
223 static const char *message_get_sender_with_fallback(DBusMessage *m) {
228 if ((s = dbus_message_get_sender(m)))
231 /* When the message came in from a direct connection the
232 * message will have no sender. We fix that here. */
237 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
240 const BusProperty properties[] = {
241 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
242 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
243 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime },
244 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL },
245 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL },
246 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL },
247 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL },
248 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
249 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn },
250 { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status },
251 { "org.freedesktop.systemd1.Manager", "SysVConsole", bus_property_append_bool, "b", &m->sysv_console },
252 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path },
253 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path },
254 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path },
255 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket },
256 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
257 { "org.freedesktop.systemd1.Manager", "MountAuto", bus_property_append_bool, "b", &m->mount_auto },
258 { "org.freedesktop.systemd1.Manager", "SwapAuto", bus_property_append_bool, "b", &m->swap_auto },
259 { NULL, NULL, NULL, NULL, NULL }
264 DBusMessage *reply = NULL;
266 JobType job_type = _JOB_TYPE_INVALID;
267 bool reload_if_possible = false;
273 dbus_error_init(&error);
275 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
279 if (!dbus_message_get_args(
282 DBUS_TYPE_STRING, &name,
284 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
286 if (!(u = manager_get_unit(m, name))) {
287 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
288 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
291 if (!(reply = dbus_message_new_method_return(message)))
294 if (!(path = unit_dbus_path(u)))
297 if (!dbus_message_append_args(
299 DBUS_TYPE_OBJECT_PATH, &path,
302 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
306 if (!dbus_message_get_args(
309 DBUS_TYPE_UINT32, &pid,
311 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
313 if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) {
314 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
315 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
318 if (!(reply = dbus_message_new_method_return(message)))
321 if (!(path = unit_dbus_path(u)))
324 if (!dbus_message_append_args(
326 DBUS_TYPE_OBJECT_PATH, &path,
329 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
333 if (!dbus_message_get_args(
336 DBUS_TYPE_STRING, &name,
338 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
340 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
341 return bus_send_error_reply(m, connection, message, &error, r);
343 if (!(reply = dbus_message_new_method_return(message)))
346 if (!(path = unit_dbus_path(u)))
349 if (!dbus_message_append_args(
351 DBUS_TYPE_OBJECT_PATH, &path,
355 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
356 job_type = JOB_START;
357 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
359 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
360 job_type = JOB_RELOAD;
361 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
362 job_type = JOB_RESTART;
363 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
364 job_type = JOB_TRY_RESTART;
365 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
366 reload_if_possible = true;
367 job_type = JOB_RESTART;
368 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
369 reload_if_possible = true;
370 job_type = JOB_TRY_RESTART;
371 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
375 if (!dbus_message_get_args(
378 DBUS_TYPE_UINT32, &id,
380 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
382 if (!(j = manager_get_job(m, id))) {
383 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
384 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
387 if (!(reply = dbus_message_new_method_return(message)))
390 if (!(path = job_dbus_path(j)))
393 if (!dbus_message_append_args(
395 DBUS_TYPE_OBJECT_PATH, &path,
399 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
401 manager_clear_jobs(m);
403 if (!(reply = dbus_message_new_method_return(message)))
406 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
408 manager_reset_failed(m);
410 if (!(reply = dbus_message_new_method_return(message)))
413 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
417 if (!dbus_message_get_args(
420 DBUS_TYPE_STRING, &name,
422 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
424 if (!(u = manager_get_unit(m, name))) {
425 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
426 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
429 unit_reset_failed(u);
431 if (!(reply = dbus_message_new_method_return(message)))
434 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
435 DBusMessageIter iter, sub;
440 if (!(reply = dbus_message_new_method_return(message)))
443 dbus_message_iter_init_append(reply, &iter);
445 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
448 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
449 char *u_path, *j_path;
450 const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
451 DBusMessageIter sub2;
458 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
461 description = unit_description(u);
462 load_state = unit_load_state_to_string(u->meta.load_state);
463 active_state = unit_active_state_to_string(unit_active_state(u));
464 sub_state = unit_sub_state_to_string(u);
466 f = unit_following(u);
467 following = f ? f->meta.id : "";
469 if (!(u_path = unit_dbus_path(u)))
473 job_id = (uint32_t) u->meta.job->id;
475 if (!(j_path = job_dbus_path(u->meta.job))) {
480 sjob_type = job_type_to_string(u->meta.job->type);
487 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
488 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
489 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
490 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
491 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
492 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
493 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
494 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
495 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
496 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
507 if (!dbus_message_iter_close_container(&sub, &sub2))
511 if (!dbus_message_iter_close_container(&iter, &sub))
514 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
515 DBusMessageIter iter, sub;
519 if (!(reply = dbus_message_new_method_return(message)))
522 dbus_message_iter_init_append(reply, &iter);
524 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
527 HASHMAP_FOREACH(j, m->jobs, i) {
528 char *u_path, *j_path;
529 const char *state, *type;
531 DBusMessageIter sub2;
533 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
536 id = (uint32_t) j->id;
537 state = job_state_to_string(j->state);
538 type = job_type_to_string(j->type);
540 if (!(j_path = job_dbus_path(j)))
543 if (!(u_path = unit_dbus_path(j->unit))) {
548 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
549 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
550 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
551 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
552 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
553 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
562 if (!dbus_message_iter_close_container(&sub, &sub2))
566 if (!dbus_message_iter_close_container(&iter, &sub))
569 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
573 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
574 if (!(s = set_new(string_hash_func, string_compare_func)))
577 if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
583 if (!(client = strdup(message_get_sender_with_fallback(message))))
586 if ((r = set_put(s, client)) < 0) {
588 return bus_send_error_reply(m, connection, message, NULL, r);
591 if (!(reply = dbus_message_new_method_return(message)))
594 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
597 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
598 dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
599 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
604 if (!(reply = dbus_message_new_method_return(message)))
607 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
612 if (!(reply = dbus_message_new_method_return(message)))
615 if (!(f = open_memstream(&dump, &size)))
618 manager_dump_units(m, f, NULL);
619 manager_dump_jobs(m, f, NULL);
629 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
635 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
640 if (!dbus_message_get_args(
643 DBUS_TYPE_STRING, &name,
644 DBUS_TYPE_BOOLEAN, &cleanup,
646 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
648 if (name && name[0] == 0)
651 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
652 return bus_send_error_reply(m, connection, message, &error, r);
654 if (!(reply = dbus_message_new_method_return(message)))
657 if (!(path = unit_dbus_path(UNIT(s))))
660 if (!dbus_message_append_args(
662 DBUS_TYPE_OBJECT_PATH, &path,
666 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
667 char *introspection = NULL;
675 if (!(reply = dbus_message_new_method_return(message)))
678 /* We roll our own introspection code here, instead of
679 * relying on bus_default_message_handler() because we
680 * need to generate our introspection string
683 if (!(f = open_memstream(&introspection, &size)))
686 fputs(INTROSPECTION_BEGIN, f);
688 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
694 if (!(p = bus_path_escape(k))) {
700 fprintf(f, "<node name=\"unit/%s\"/>", p);
704 HASHMAP_FOREACH(j, m->jobs, i)
705 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
707 fputs(INTROSPECTION_END, f);
720 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
727 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
729 assert(!m->queued_message);
731 /* Instead of sending the reply back right away, we
732 * just remember that we need to and then send it
733 * after the reload is finished. That way the caller
734 * knows when the reload finished. */
736 if (!(m->queued_message = dbus_message_new_method_return(message)))
739 m->queued_message_connection = connection;
740 m->exit_code = MANAGER_RELOAD;
742 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
744 if (!(reply = dbus_message_new_method_return(message)))
747 m->exit_code = MANAGER_REEXECUTE;
749 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
751 if (m->running_as == MANAGER_SYSTEM) {
752 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for session managers.");
753 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
756 if (!(reply = dbus_message_new_method_return(message)))
759 m->exit_code = MANAGER_EXIT;
761 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
762 char **l = NULL, **e = NULL;
764 if ((r = bus_parse_strv(message, &l)) < 0) {
768 return bus_send_error_reply(m, connection, message, NULL, r);
771 e = strv_env_merge(2, m->environment, l);
777 if (!(reply = dbus_message_new_method_return(message))) {
782 strv_free(m->environment);
785 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
786 char **l = NULL, **e = NULL;
788 if ((r = bus_parse_strv(message, &l)) < 0) {
792 return bus_send_error_reply(m, connection, message, NULL, r);
795 e = strv_env_delete(m->environment, 1, l);
801 if (!(reply = dbus_message_new_method_return(message)))
804 strv_free(m->environment);
808 return bus_default_message_handler(m, connection, message, NULL, properties);
810 if (job_type != _JOB_TYPE_INVALID) {
811 const char *name, *smode;
816 if (!dbus_message_get_args(
819 DBUS_TYPE_STRING, &name,
820 DBUS_TYPE_STRING, &smode,
822 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
824 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
825 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
826 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
829 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
830 return bus_send_error_reply(m, connection, message, &error, r);
832 if (reload_if_possible && unit_can_reload(u)) {
833 if (job_type == JOB_RESTART)
834 job_type = JOB_RELOAD_OR_START;
835 else if (job_type == JOB_TRY_RESTART)
836 job_type = JOB_RELOAD;
839 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
840 (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
841 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
842 (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
843 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
844 return bus_send_error_reply(m, connection, message, &error, -EPERM);
847 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
848 return bus_send_error_reply(m, connection, message, &error, r);
850 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
855 if (!(reply = dbus_message_new_method_return(message)))
858 if (!(path = job_dbus_path(j)))
861 if (!dbus_message_append_args(
863 DBUS_TYPE_OBJECT_PATH, &path,
871 if (!dbus_connection_send(connection, reply, NULL))
874 dbus_message_unref(reply);
877 return DBUS_HANDLER_RESULT_HANDLED;
883 dbus_message_unref(reply);
885 dbus_error_free(&error);
887 return DBUS_HANDLER_RESULT_NEED_MEMORY;
890 const DBusObjectPathVTable bus_manager_vtable = {
891 .message_function = bus_manager_message_handler