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=\"ResetMaintenanceUnit\">\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=\"ResetMaintenance\"/>\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" \
146 #define INTROSPECTION_BEGIN \
147 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
149 BUS_MANAGER_INTERFACE \
150 BUS_PROPERTIES_INTERFACE \
151 BUS_INTROSPECTABLE_INTERFACE
153 #define INTROSPECTION_END \
156 const char bus_manager_interface
[] = BUS_MANAGER_INTERFACE
;
158 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as
, manager_running_as
, ManagerRunningAs
);
160 static int bus_manager_append_log_target(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
167 t
= log_target_to_string(log_get_target());
169 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
))
175 static int bus_manager_append_log_level(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
182 t
= log_level_to_string(log_get_max_level());
184 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
))
190 static int bus_manager_append_n_names(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
197 u
= hashmap_size(m
->units
);
199 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT32
, &u
))
205 static int bus_manager_append_n_jobs(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
212 u
= hashmap_size(m
->jobs
);
214 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT32
, &u
))
220 static const char *message_get_sender_with_fallback(DBusMessage
*m
) {
225 if ((s
= dbus_message_get_sender(m
)))
228 /* When the message came in from a direct connection the
229 * message will have no sender. We fix that here. */
234 static DBusHandlerResult
bus_manager_message_handler(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
237 const BusProperty properties
[] = {
238 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string
, "s", PACKAGE_STRING
},
239 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as
, "s", &m
->running_as
},
240 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64
, "t", &m
->startup_timestamp
.realtime
},
241 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level
, "s", NULL
},
242 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target
, "s", NULL
},
243 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names
, "u", NULL
},
244 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs
, "u", NULL
},
245 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv
, "as", m
->environment
},
246 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool
, "b", &m
->confirm_spawn
},
247 { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool
, "b", &m
->show_status
},
248 { "org.freedesktop.systemd1.Manager", "SysVConsole", bus_property_append_bool
, "b", &m
->sysv_console
},
249 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv
, "as", m
->lookup_paths
.unit_path
},
250 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv
, "as", m
->lookup_paths
.sysvinit_path
},
251 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv
, "as", m
->lookup_paths
.sysvrcnd_path
},
252 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string
, "s", m
->notify_socket
},
253 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string
, "s", m
->cgroup_hierarchy
},
254 { NULL
, NULL
, NULL
, NULL
, NULL
}
259 DBusMessage
*reply
= NULL
;
261 JobType job_type
= _JOB_TYPE_INVALID
;
262 bool reload_if_possible
= false;
268 dbus_error_init(&error
);
270 if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "GetUnit")) {
274 if (!dbus_message_get_args(
277 DBUS_TYPE_STRING
, &name
,
279 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
281 if (!(u
= manager_get_unit(m
, name
))) {
282 dbus_set_error(&error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s is not loaded.", name
);
283 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOENT
);
286 if (!(reply
= dbus_message_new_method_return(message
)))
289 if (!(path
= unit_dbus_path(u
)))
292 if (!dbus_message_append_args(
294 DBUS_TYPE_OBJECT_PATH
, &path
,
297 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
301 if (!dbus_message_get_args(
304 DBUS_TYPE_UINT32
, &pid
,
306 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
308 if (!(u
= cgroup_unit_by_pid(m
, (pid_t
) pid
))) {
309 dbus_set_error(&error
, BUS_ERROR_NO_SUCH_UNIT
, "No unit for PID %lu is loaded.", (unsigned long) pid
);
310 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOENT
);
313 if (!(reply
= dbus_message_new_method_return(message
)))
316 if (!(path
= unit_dbus_path(u
)))
319 if (!dbus_message_append_args(
321 DBUS_TYPE_OBJECT_PATH
, &path
,
324 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
328 if (!dbus_message_get_args(
331 DBUS_TYPE_STRING
, &name
,
333 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
335 if ((r
= manager_load_unit(m
, name
, NULL
, &error
, &u
)) < 0)
336 return bus_send_error_reply(m
, connection
, message
, &error
, r
);
338 if (!(reply
= dbus_message_new_method_return(message
)))
341 if (!(path
= unit_dbus_path(u
)))
344 if (!dbus_message_append_args(
346 DBUS_TYPE_OBJECT_PATH
, &path
,
350 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "StartUnit"))
351 job_type
= JOB_START
;
352 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "StopUnit"))
354 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
355 job_type
= JOB_RELOAD
;
356 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "RestartUnit"))
357 job_type
= JOB_RESTART
;
358 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
359 job_type
= JOB_TRY_RESTART
;
360 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
361 reload_if_possible
= true;
362 job_type
= JOB_RESTART
;
363 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
364 reload_if_possible
= true;
365 job_type
= JOB_TRY_RESTART
;
366 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "GetJob")) {
370 if (!dbus_message_get_args(
373 DBUS_TYPE_UINT32
, &id
,
375 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
377 if (!(j
= manager_get_job(m
, id
))) {
378 dbus_set_error(&error
, BUS_ERROR_NO_SUCH_JOB
, "Job %u does not exist.", (unsigned) id
);
379 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOENT
);
382 if (!(reply
= dbus_message_new_method_return(message
)))
385 if (!(path
= job_dbus_path(j
)))
388 if (!dbus_message_append_args(
390 DBUS_TYPE_OBJECT_PATH
, &path
,
394 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
396 manager_clear_jobs(m
);
398 if (!(reply
= dbus_message_new_method_return(message
)))
401 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ResetMaintenance")) {
403 manager_reset_maintenance(m
);
405 if (!(reply
= dbus_message_new_method_return(message
)))
408 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ResetMaintenanceUnit")) {
412 if (!dbus_message_get_args(
415 DBUS_TYPE_STRING
, &name
,
417 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
419 if (!(u
= manager_get_unit(m
, name
))) {
420 dbus_set_error(&error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s is not loaded.", name
);
421 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOENT
);
424 unit_reset_maintenance(u
);
426 if (!(reply
= dbus_message_new_method_return(message
)))
429 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ListUnits")) {
430 DBusMessageIter iter
, sub
;
435 if (!(reply
= dbus_message_new_method_return(message
)))
438 dbus_message_iter_init_append(reply
, &iter
);
440 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(ssssssouso)", &sub
))
443 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
444 char *u_path
, *j_path
;
445 const char *description
, *load_state
, *active_state
, *sub_state
, *sjob_type
, *following
;
446 DBusMessageIter sub2
;
453 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_STRUCT
, NULL
, &sub2
))
456 description
= unit_description(u
);
457 load_state
= unit_load_state_to_string(u
->meta
.load_state
);
458 active_state
= unit_active_state_to_string(unit_active_state(u
));
459 sub_state
= unit_sub_state_to_string(u
);
461 f
= unit_following(u
);
462 following
= f
? f
->meta
.id
: "";
464 if (!(u_path
= unit_dbus_path(u
)))
468 job_id
= (uint32_t) u
->meta
.job
->id
;
470 if (!(j_path
= job_dbus_path(u
->meta
.job
))) {
475 sjob_type
= job_type_to_string(u
->meta
.job
->type
);
482 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &u
->meta
.id
) ||
483 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &description
) ||
484 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &load_state
) ||
485 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &active_state
) ||
486 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &sub_state
) ||
487 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &following
) ||
488 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u_path
) ||
489 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &job_id
) ||
490 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &sjob_type
) ||
491 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &j_path
)) {
502 if (!dbus_message_iter_close_container(&sub
, &sub2
))
506 if (!dbus_message_iter_close_container(&iter
, &sub
))
509 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ListJobs")) {
510 DBusMessageIter iter
, sub
;
514 if (!(reply
= dbus_message_new_method_return(message
)))
517 dbus_message_iter_init_append(reply
, &iter
);
519 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(usssoo)", &sub
))
522 HASHMAP_FOREACH(j
, m
->jobs
, i
) {
523 char *u_path
, *j_path
;
524 const char *state
, *type
;
526 DBusMessageIter sub2
;
528 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_STRUCT
, NULL
, &sub2
))
531 id
= (uint32_t) j
->id
;
532 state
= job_state_to_string(j
->state
);
533 type
= job_type_to_string(j
->type
);
535 if (!(j_path
= job_dbus_path(j
)))
538 if (!(u_path
= unit_dbus_path(j
->unit
))) {
543 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &id
) ||
544 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &j
->unit
->meta
.id
) ||
545 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &type
) ||
546 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &state
) ||
547 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &j_path
) ||
548 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u_path
)) {
557 if (!dbus_message_iter_close_container(&sub
, &sub2
))
561 if (!dbus_message_iter_close_container(&iter
, &sub
))
564 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Subscribe")) {
568 if (!(s
= BUS_CONNECTION_SUBSCRIBED(m
, connection
))) {
569 if (!(s
= set_new(string_hash_func
, string_compare_func
)))
572 if (!(dbus_connection_set_data(connection
, m
->subscribed_data_slot
, s
, NULL
))) {
578 if (!(client
= strdup(message_get_sender_with_fallback(message
))))
581 if ((r
= set_put(s
, client
)) < 0) {
583 return bus_send_error_reply(m
, connection
, message
, NULL
, r
);
586 if (!(reply
= dbus_message_new_method_return(message
)))
589 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
592 if (!(client
= set_remove(BUS_CONNECTION_SUBSCRIBED(m
, connection
), (char*) message_get_sender_with_fallback(message
)))) {
593 dbus_set_error(&error
, BUS_ERROR_NOT_SUBSCRIBED
, "Client is not subscribed.");
594 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOENT
);
599 if (!(reply
= dbus_message_new_method_return(message
)))
602 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Dump")) {
607 if (!(reply
= dbus_message_new_method_return(message
)))
610 if (!(f
= open_memstream(&dump
, &size
)))
613 manager_dump_units(m
, f
, NULL
);
614 manager_dump_jobs(m
, f
, NULL
);
624 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &dump
, DBUS_TYPE_INVALID
)) {
630 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
635 if (!dbus_message_get_args(
638 DBUS_TYPE_STRING
, &name
,
639 DBUS_TYPE_BOOLEAN
, &cleanup
,
641 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
643 if (name
&& name
[0] == 0)
646 if ((r
= snapshot_create(m
, name
, cleanup
, &error
, &s
)) < 0)
647 return bus_send_error_reply(m
, connection
, message
, &error
, r
);
649 if (!(reply
= dbus_message_new_method_return(message
)))
652 if (!(path
= unit_dbus_path(UNIT(s
))))
655 if (!dbus_message_append_args(
657 DBUS_TYPE_OBJECT_PATH
, &path
,
661 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", "Introspect")) {
662 char *introspection
= NULL
;
670 if (!(reply
= dbus_message_new_method_return(message
)))
673 /* We roll our own introspection code here, instead of
674 * relying on bus_default_message_handler() because we
675 * need to generate our introspection string
678 if (!(f
= open_memstream(&introspection
, &size
)))
681 fputs(INTROSPECTION_BEGIN
, f
);
683 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
689 if (!(p
= bus_path_escape(k
))) {
695 fprintf(f
, "<node name=\"unit/%s\"/>", p
);
699 HASHMAP_FOREACH(j
, m
->jobs
, i
)
700 fprintf(f
, "<node name=\"job/%lu\"/>", (unsigned long) j
->id
);
702 fputs(INTROSPECTION_END
, f
);
715 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &introspection
, DBUS_TYPE_INVALID
)) {
722 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Reload")) {
724 assert(!m
->queued_message
);
726 /* Instead of sending the reply back right away, we
727 * just remember that we need to and then send it
728 * after the reload is finished. That way the caller
729 * knows when the reload finished. */
731 if (!(m
->queued_message
= dbus_message_new_method_return(message
)))
734 m
->queued_message_connection
= connection
;
735 m
->exit_code
= MANAGER_RELOAD
;
737 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Reexecute")) {
739 if (!(reply
= dbus_message_new_method_return(message
)))
742 m
->exit_code
= MANAGER_REEXECUTE
;
744 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Exit")) {
746 if (m
->running_as
== MANAGER_SYSTEM
) {
747 dbus_set_error(&error
, BUS_ERROR_NOT_SUPPORTED
, "Exit is only supported for session managers.");
748 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOTSUP
);
751 if (!(reply
= dbus_message_new_method_return(message
)))
754 m
->exit_code
= MANAGER_EXIT
;
756 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
757 char **l
= NULL
, **e
= NULL
;
759 if ((r
= bus_parse_strv(message
, &l
)) < 0) {
763 return bus_send_error_reply(m
, connection
, message
, NULL
, r
);
766 e
= strv_env_merge(2, m
->environment
, l
);
772 if (!(reply
= dbus_message_new_method_return(message
))) {
777 strv_free(m
->environment
);
780 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
781 char **l
= NULL
, **e
= NULL
;
783 if ((r
= bus_parse_strv(message
, &l
)) < 0) {
787 return bus_send_error_reply(m
, connection
, message
, NULL
, r
);
790 e
= strv_env_delete(m
->environment
, 1, l
);
796 if (!(reply
= dbus_message_new_method_return(message
)))
799 strv_free(m
->environment
);
803 return bus_default_message_handler(m
, connection
, message
, NULL
, properties
);
805 if (job_type
!= _JOB_TYPE_INVALID
) {
806 const char *name
, *smode
;
811 if (!dbus_message_get_args(
814 DBUS_TYPE_STRING
, &name
,
815 DBUS_TYPE_STRING
, &smode
,
817 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
819 if ((mode
= job_mode_from_string(smode
)) == _JOB_MODE_INVALID
) {
820 dbus_set_error(&error
, BUS_ERROR_INVALID_JOB_MODE
, "Job mode %s is invalid.", smode
);
821 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
824 if ((r
= manager_load_unit(m
, name
, NULL
, &error
, &u
)) < 0)
825 return bus_send_error_reply(m
, connection
, message
, &error
, r
);
827 if (reload_if_possible
&& unit_can_reload(u
)) {
828 if (job_type
== JOB_RESTART
)
829 job_type
= JOB_RELOAD_OR_START
;
830 else if (job_type
== JOB_TRY_RESTART
)
831 job_type
= JOB_RELOAD
;
834 if ((job_type
== JOB_START
&& u
->meta
.refuse_manual_start
) ||
835 (job_type
== JOB_STOP
&& u
->meta
.refuse_manual_stop
) ||
836 ((job_type
== JOB_RESTART
|| job_type
== JOB_TRY_RESTART
) &&
837 (u
->meta
.refuse_manual_start
|| u
->meta
.refuse_manual_stop
))) {
838 dbus_set_error(&error
, BUS_ERROR_ONLY_BY_DEPENDENCY
, "Operation refused, may be requested by dependency only.");
839 return bus_send_error_reply(m
, connection
, message
, &error
, -EPERM
);
842 if ((r
= manager_add_job(m
, job_type
, u
, mode
, true, &error
, &j
)) < 0)
843 return bus_send_error_reply(m
, connection
, message
, &error
, r
);
845 if (!(j
->bus_client
= strdup(message_get_sender_with_fallback(message
))))
850 if (!(reply
= dbus_message_new_method_return(message
)))
853 if (!(path
= job_dbus_path(j
)))
856 if (!dbus_message_append_args(
858 DBUS_TYPE_OBJECT_PATH
, &path
,
866 if (!dbus_connection_send(connection
, reply
, NULL
))
869 dbus_message_unref(reply
);
872 return DBUS_HANDLER_RESULT_HANDLED
;
878 dbus_message_unref(reply
);
880 dbus_error_free(&error
);
882 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
885 const DBusObjectPathVTable bus_manager_vtable
= {
886 .message_function
= bus_manager_message_handler