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_BEGIN \
31 " <interface name=\"org.freedesktop.systemd1.Manager\">\n"
33 #define BUS_MANAGER_INTERFACE_METHODS \
34 " <method name=\"GetUnit\">\n" \
35 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
36 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
38 " <method name=\"GetUnitByPID\">\n" \
39 " <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n" \
40 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
42 " <method name=\"LoadUnit\">\n" \
43 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
44 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
46 " <method name=\"StartUnit\">\n" \
47 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
48 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
49 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
51 " <method name=\"StartUnitReplace\">\n" \
52 " <arg name=\"old_unit\" type=\"s\" direction=\"in\"/>\n" \
53 " <arg name=\"new_unit\" type=\"s\" direction=\"in\"/>\n" \
54 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
55 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
57 " <method name=\"StopUnit\">\n" \
58 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
59 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
60 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
62 " <method name=\"ReloadUnit\">\n" \
63 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
64 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
65 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
67 " <method name=\"RestartUnit\">\n" \
68 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
69 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
70 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
72 " <method name=\"TryRestartUnit\">\n" \
73 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
74 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
75 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
77 " <method name=\"ReloadOrRestartUnit\">\n" \
78 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
79 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
80 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
82 " <method name=\"ReloadOrTryRestartUnit\">\n" \
83 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
84 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
85 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
87 " <method name=\"KillUnit\">\n" \
88 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
89 " <arg name=\"who\" type=\"s\" direction=\"in\"/>\n" \
90 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
91 " <arg name=\"signal\" type=\"i\" direction=\"in\"/>\n" \
93 " <method name=\"ResetFailedUnit\">\n" \
94 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
96 " <method name=\"GetJob\">\n" \
97 " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
98 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
100 " <method name=\"ClearJobs\"/>\n" \
101 " <method name=\"ResetFailed\"/>\n" \
102 " <method name=\"ListUnits\">\n" \
103 " <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
105 " <method name=\"ListJobs\">\n" \
106 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
108 " <method name=\"Subscribe\"/>\n" \
109 " <method name=\"Unsubscribe\"/>\n" \
110 " <method name=\"Dump\"/>\n" \
111 " <method name=\"CreateSnapshot\">\n" \
112 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
113 " <arg name=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \
114 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
116 " <method name=\"Reload\"/>\n" \
117 " <method name=\"Reexecute\"/>\n" \
118 " <method name=\"Exit\"/>\n" \
119 " <method name=\"Reboot\"/>\n" \
120 " <method name=\"PowerOff\"/>\n" \
121 " <method name=\"Halt\"/>\n" \
122 " <method name=\"KExec\"/>\n" \
123 " <method name=\"SetEnvironment\">\n" \
124 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
126 " <method name=\"UnsetEnvironment\">\n" \
127 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
130 #define BUS_MANAGER_INTERFACE_SIGNALS \
131 " <signal name=\"UnitNew\">\n" \
132 " <arg name=\"id\" type=\"s\"/>\n" \
133 " <arg name=\"unit\" type=\"o\"/>\n" \
135 " <signal name=\"UnitRemoved\">\n" \
136 " <arg name=\"id\" type=\"s\"/>\n" \
137 " <arg name=\"unit\" type=\"o\"/>\n" \
139 " <signal name=\"JobNew\">\n" \
140 " <arg name=\"id\" type=\"u\"/>\n" \
141 " <arg name=\"job\" type=\"o\"/>\n" \
143 " <signal name=\"JobRemoved\">\n" \
144 " <arg name=\"id\" type=\"u\"/>\n" \
145 " <arg name=\"job\" type=\"o\"/>\n" \
146 " <arg name=\"success\" type=\"b\"/>\n" \
150 #define BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
151 " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
152 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
153 " <property name=\"InitRDTimestamp\" type=\"t\" access=\"read\"/>\n" \
154 " <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
155 " <property name=\"FinishTimestamp\" type=\"t\" access=\"read\"/>\n" \
156 " <property name=\"LogLevel\" type=\"s\" access=\"read\"/>\n" \
157 " <property name=\"LogTarget\" type=\"s\" access=\"read\"/>\n" \
158 " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
159 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
160 " <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \
161 " <property name=\"NFailedJobs\" type=\"u\" access=\"read\"/>\n" \
162 " <property name=\"Progress\" type=\"d\" access=\"read\"/>\n" \
163 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
164 " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
165 " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
166 " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
167 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
168 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
169 " <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
170 " <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n" \
171 " <property name=\"DefaultControllers\" type=\"as\" access=\"read\"/>\n" \
172 " <property name=\"DefaultStandardOutput\" type=\"s\" access=\"read\"/>\n" \
173 " <property name=\"DefaultStandardError\" type=\"s\" access=\"read\"/>\n"
175 #ifdef HAVE_SYSV_COMPAT
176 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
177 " <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
178 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
179 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n"
181 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV
184 #define BUS_MANAGER_INTERFACE_END \
187 #define BUS_MANAGER_INTERFACE \
188 BUS_MANAGER_INTERFACE_BEGIN \
189 BUS_MANAGER_INTERFACE_METHODS \
190 BUS_MANAGER_INTERFACE_SIGNALS \
191 BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
192 BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
193 BUS_MANAGER_INTERFACE_END
195 #define INTROSPECTION_BEGIN \
196 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
198 BUS_MANAGER_INTERFACE \
199 BUS_PROPERTIES_INTERFACE \
201 BUS_INTROSPECTABLE_INTERFACE
203 #define INTROSPECTION_END \
206 const char bus_manager_interface
[] _introspect_("Manager") = BUS_MANAGER_INTERFACE
;
208 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as
, manager_running_as
, ManagerRunningAs
);
209 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_exec_output
, exec_output
, ExecOutput
);
211 static int bus_manager_append_log_target(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
218 t
= log_target_to_string(log_get_target());
220 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
))
226 static int bus_manager_append_log_level(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
233 t
= log_level_to_string(log_get_max_level());
235 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
))
241 static int bus_manager_append_n_names(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
248 u
= hashmap_size(m
->units
);
250 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT32
, &u
))
256 static int bus_manager_append_n_jobs(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
263 u
= hashmap_size(m
->jobs
);
265 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT32
, &u
))
271 static int bus_manager_append_progress(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
278 if (dual_timestamp_is_set(&m
->finish_timestamp
))
281 d
= 1.0 - ((double) hashmap_size(m
->jobs
) / (double) m
->n_installed_jobs
);
283 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_DOUBLE
, &d
))
289 static const char *message_get_sender_with_fallback(DBusMessage
*m
) {
294 if ((s
= dbus_message_get_sender(m
)))
297 /* When the message came in from a direct connection the
298 * message will have no sender. We fix that here. */
303 static DBusHandlerResult
bus_manager_message_handler(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
306 const BusProperty properties
[] = {
307 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string
, "s", PACKAGE_STRING
},
308 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as
, "s", &m
->running_as
},
309 { "org.freedesktop.systemd1.Manager", "InitRDTimestamp", bus_property_append_uint64
, "t", &m
->initrd_timestamp
.realtime
},
310 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64
, "t", &m
->startup_timestamp
.realtime
},
311 { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64
, "t", &m
->finish_timestamp
.realtime
},
312 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level
, "s", NULL
},
313 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target
, "s", NULL
},
314 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names
, "u", NULL
},
315 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs
, "u", NULL
},
316 { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32
, "u", &m
->n_installed_jobs
},
317 { "org.freedesktop.systemd1.Manager", "NFailedJobs", bus_property_append_uint32
, "u", &m
->n_failed_jobs
},
318 { "org.freedesktop.systemd1.Manager", "Progress", bus_manager_append_progress
, "d", NULL
},
319 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv
, "as", m
->environment
},
320 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool
, "b", &m
->confirm_spawn
},
321 { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool
, "b", &m
->show_status
},
322 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv
, "as", m
->lookup_paths
.unit_path
},
323 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string
, "s", m
->notify_socket
},
324 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string
, "s", m
->cgroup_hierarchy
},
325 { "org.freedesktop.systemd1.Manager", "MountAuto", bus_property_append_bool
, "b", &m
->mount_auto
},
326 { "org.freedesktop.systemd1.Manager", "SwapAuto", bus_property_append_bool
, "b", &m
->swap_auto
},
327 { "org.freedesktop.systemd1.Manager", "DefaultControllers", bus_property_append_strv
, "as", m
->default_controllers
},
328 { "org.freedesktop.systemd1.Manager", "DefaultStandardOutput", bus_manager_append_exec_output
, "s", &m
->default_std_output
},
329 { "org.freedesktop.systemd1.Manager", "DefaultStandardError", bus_manager_append_exec_output
, "s", &m
->default_std_error
},
330 #ifdef HAVE_SYSV_COMPAT
331 { "org.freedesktop.systemd1.Manager", "SysVConsole", bus_property_append_bool
, "b", &m
->sysv_console
},
332 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv
, "as", m
->lookup_paths
.sysvinit_path
},
333 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv
, "as", m
->lookup_paths
.sysvrcnd_path
},
335 { NULL
, NULL
, NULL
, NULL
, NULL
}
340 DBusMessage
*reply
= NULL
;
342 JobType job_type
= _JOB_TYPE_INVALID
;
343 bool reload_if_possible
= false;
349 dbus_error_init(&error
);
351 if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "GetUnit")) {
355 if (!dbus_message_get_args(
358 DBUS_TYPE_STRING
, &name
,
360 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
362 if (!(u
= manager_get_unit(m
, name
))) {
363 dbus_set_error(&error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s is not loaded.", name
);
364 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOENT
);
367 if (!(reply
= dbus_message_new_method_return(message
)))
370 if (!(path
= unit_dbus_path(u
)))
373 if (!dbus_message_append_args(
375 DBUS_TYPE_OBJECT_PATH
, &path
,
378 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
382 if (!dbus_message_get_args(
385 DBUS_TYPE_UINT32
, &pid
,
387 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
389 if (!(u
= cgroup_unit_by_pid(m
, (pid_t
) pid
))) {
390 dbus_set_error(&error
, BUS_ERROR_NO_SUCH_UNIT
, "No unit for PID %lu is loaded.", (unsigned long) pid
);
391 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOENT
);
394 if (!(reply
= dbus_message_new_method_return(message
)))
397 if (!(path
= unit_dbus_path(u
)))
400 if (!dbus_message_append_args(
402 DBUS_TYPE_OBJECT_PATH
, &path
,
405 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
409 if (!dbus_message_get_args(
412 DBUS_TYPE_STRING
, &name
,
414 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
416 if ((r
= manager_load_unit(m
, name
, NULL
, &error
, &u
)) < 0)
417 return bus_send_error_reply(m
, connection
, message
, &error
, r
);
419 if (!(reply
= dbus_message_new_method_return(message
)))
422 if (!(path
= unit_dbus_path(u
)))
425 if (!dbus_message_append_args(
427 DBUS_TYPE_OBJECT_PATH
, &path
,
431 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "StartUnit"))
432 job_type
= JOB_START
;
433 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
434 job_type
= JOB_START
;
435 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "StopUnit"))
437 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
438 job_type
= JOB_RELOAD
;
439 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "RestartUnit"))
440 job_type
= JOB_RESTART
;
441 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
442 job_type
= JOB_TRY_RESTART
;
443 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
444 reload_if_possible
= true;
445 job_type
= JOB_RESTART
;
446 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
447 reload_if_possible
= true;
448 job_type
= JOB_TRY_RESTART
;
449 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "KillUnit")) {
450 const char *name
, *swho
, *smode
;
456 if (!dbus_message_get_args(
459 DBUS_TYPE_STRING
, &name
,
460 DBUS_TYPE_STRING
, &swho
,
461 DBUS_TYPE_STRING
, &smode
,
462 DBUS_TYPE_INT32
, &signo
,
464 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
466 if ((mode
= kill_mode_from_string(smode
)) < 0 ||
467 (who
= kill_who_from_string(swho
)) < 0 ||
470 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
472 if (!(u
= manager_get_unit(m
, name
))) {
473 dbus_set_error(&error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s is not loaded.", name
);
474 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOENT
);
477 if ((r
= unit_kill(u
, who
, mode
, signo
, &error
)) < 0)
478 return bus_send_error_reply(m
, connection
, message
, &error
, r
);
480 if (!(reply
= dbus_message_new_method_return(message
)))
483 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "GetJob")) {
487 if (!dbus_message_get_args(
490 DBUS_TYPE_UINT32
, &id
,
492 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
494 if (!(j
= manager_get_job(m
, id
))) {
495 dbus_set_error(&error
, BUS_ERROR_NO_SUCH_JOB
, "Job %u does not exist.", (unsigned) id
);
496 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOENT
);
499 if (!(reply
= dbus_message_new_method_return(message
)))
502 if (!(path
= job_dbus_path(j
)))
505 if (!dbus_message_append_args(
507 DBUS_TYPE_OBJECT_PATH
, &path
,
511 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
513 manager_clear_jobs(m
);
515 if (!(reply
= dbus_message_new_method_return(message
)))
518 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
520 manager_reset_failed(m
);
522 if (!(reply
= dbus_message_new_method_return(message
)))
525 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
529 if (!dbus_message_get_args(
532 DBUS_TYPE_STRING
, &name
,
534 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
536 if (!(u
= manager_get_unit(m
, name
))) {
537 dbus_set_error(&error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s is not loaded.", name
);
538 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOENT
);
541 unit_reset_failed(u
);
543 if (!(reply
= dbus_message_new_method_return(message
)))
546 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ListUnits")) {
547 DBusMessageIter iter
, sub
;
552 if (!(reply
= dbus_message_new_method_return(message
)))
555 dbus_message_iter_init_append(reply
, &iter
);
557 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(ssssssouso)", &sub
))
560 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
561 char *u_path
, *j_path
;
562 const char *description
, *load_state
, *active_state
, *sub_state
, *sjob_type
, *following
;
563 DBusMessageIter sub2
;
570 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_STRUCT
, NULL
, &sub2
))
573 description
= unit_description(u
);
574 load_state
= unit_load_state_to_string(u
->meta
.load_state
);
575 active_state
= unit_active_state_to_string(unit_active_state(u
));
576 sub_state
= unit_sub_state_to_string(u
);
578 f
= unit_following(u
);
579 following
= f
? f
->meta
.id
: "";
581 if (!(u_path
= unit_dbus_path(u
)))
585 job_id
= (uint32_t) u
->meta
.job
->id
;
587 if (!(j_path
= job_dbus_path(u
->meta
.job
))) {
592 sjob_type
= job_type_to_string(u
->meta
.job
->type
);
599 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &u
->meta
.id
) ||
600 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &description
) ||
601 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &load_state
) ||
602 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &active_state
) ||
603 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &sub_state
) ||
604 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &following
) ||
605 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u_path
) ||
606 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &job_id
) ||
607 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &sjob_type
) ||
608 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &j_path
)) {
619 if (!dbus_message_iter_close_container(&sub
, &sub2
))
623 if (!dbus_message_iter_close_container(&iter
, &sub
))
626 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ListJobs")) {
627 DBusMessageIter iter
, sub
;
631 if (!(reply
= dbus_message_new_method_return(message
)))
634 dbus_message_iter_init_append(reply
, &iter
);
636 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(usssoo)", &sub
))
639 HASHMAP_FOREACH(j
, m
->jobs
, i
) {
640 char *u_path
, *j_path
;
641 const char *state
, *type
;
643 DBusMessageIter sub2
;
645 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_STRUCT
, NULL
, &sub2
))
648 id
= (uint32_t) j
->id
;
649 state
= job_state_to_string(j
->state
);
650 type
= job_type_to_string(j
->type
);
652 if (!(j_path
= job_dbus_path(j
)))
655 if (!(u_path
= unit_dbus_path(j
->unit
))) {
660 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &id
) ||
661 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &j
->unit
->meta
.id
) ||
662 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &type
) ||
663 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &state
) ||
664 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &j_path
) ||
665 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u_path
)) {
674 if (!dbus_message_iter_close_container(&sub
, &sub2
))
678 if (!dbus_message_iter_close_container(&iter
, &sub
))
681 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Subscribe")) {
685 if (!(s
= BUS_CONNECTION_SUBSCRIBED(m
, connection
))) {
686 if (!(s
= set_new(string_hash_func
, string_compare_func
)))
689 if (!(dbus_connection_set_data(connection
, m
->subscribed_data_slot
, s
, NULL
))) {
695 if (!(client
= strdup(message_get_sender_with_fallback(message
))))
698 if ((r
= set_put(s
, client
)) < 0) {
700 return bus_send_error_reply(m
, connection
, message
, NULL
, r
);
703 if (!(reply
= dbus_message_new_method_return(message
)))
706 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
709 if (!(client
= set_remove(BUS_CONNECTION_SUBSCRIBED(m
, connection
), (char*) message_get_sender_with_fallback(message
)))) {
710 dbus_set_error(&error
, BUS_ERROR_NOT_SUBSCRIBED
, "Client is not subscribed.");
711 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOENT
);
716 if (!(reply
= dbus_message_new_method_return(message
)))
719 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Dump")) {
724 if (!(reply
= dbus_message_new_method_return(message
)))
727 if (!(f
= open_memstream(&dump
, &size
)))
730 manager_dump_units(m
, f
, NULL
);
731 manager_dump_jobs(m
, f
, NULL
);
741 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &dump
, DBUS_TYPE_INVALID
)) {
747 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
752 if (!dbus_message_get_args(
755 DBUS_TYPE_STRING
, &name
,
756 DBUS_TYPE_BOOLEAN
, &cleanup
,
758 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
760 if (name
&& name
[0] == 0)
763 if ((r
= snapshot_create(m
, name
, cleanup
, &error
, &s
)) < 0)
764 return bus_send_error_reply(m
, connection
, message
, &error
, r
);
766 if (!(reply
= dbus_message_new_method_return(message
)))
769 if (!(path
= unit_dbus_path(UNIT(s
))))
772 if (!dbus_message_append_args(
774 DBUS_TYPE_OBJECT_PATH
, &path
,
778 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", "Introspect")) {
779 char *introspection
= NULL
;
787 if (!(reply
= dbus_message_new_method_return(message
)))
790 /* We roll our own introspection code here, instead of
791 * relying on bus_default_message_handler() because we
792 * need to generate our introspection string
795 if (!(f
= open_memstream(&introspection
, &size
)))
798 fputs(INTROSPECTION_BEGIN
, f
);
800 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
806 if (!(p
= bus_path_escape(k
))) {
812 fprintf(f
, "<node name=\"unit/%s\"/>", p
);
816 HASHMAP_FOREACH(j
, m
->jobs
, i
)
817 fprintf(f
, "<node name=\"job/%lu\"/>", (unsigned long) j
->id
);
819 fputs(INTROSPECTION_END
, f
);
832 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &introspection
, DBUS_TYPE_INVALID
)) {
839 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Reload")) {
841 assert(!m
->queued_message
);
843 /* Instead of sending the reply back right away, we
844 * just remember that we need to and then send it
845 * after the reload is finished. That way the caller
846 * knows when the reload finished. */
848 if (!(m
->queued_message
= dbus_message_new_method_return(message
)))
851 m
->queued_message_connection
= connection
;
852 m
->exit_code
= MANAGER_RELOAD
;
854 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Reexecute")) {
856 if (!(reply
= dbus_message_new_method_return(message
)))
859 m
->exit_code
= MANAGER_REEXECUTE
;
861 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Exit")) {
863 if (m
->running_as
== MANAGER_SYSTEM
) {
864 dbus_set_error(&error
, BUS_ERROR_NOT_SUPPORTED
, "Exit is only supported for user service managers.");
865 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOTSUP
);
868 if (!(reply
= dbus_message_new_method_return(message
)))
871 m
->exit_code
= MANAGER_EXIT
;
873 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Reboot")) {
875 if (m
->running_as
!= MANAGER_SYSTEM
) {
876 dbus_set_error(&error
, BUS_ERROR_NOT_SUPPORTED
, "Reboot is only supported for system managers.");
877 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOTSUP
);
880 if (!(reply
= dbus_message_new_method_return(message
)))
883 m
->exit_code
= MANAGER_REBOOT
;
885 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "PowerOff")) {
887 if (m
->running_as
!= MANAGER_SYSTEM
) {
888 dbus_set_error(&error
, BUS_ERROR_NOT_SUPPORTED
, "Powering off is only supported for system managers.");
889 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOTSUP
);
892 if (!(reply
= dbus_message_new_method_return(message
)))
895 m
->exit_code
= MANAGER_POWEROFF
;
897 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Halt")) {
899 if (m
->running_as
!= MANAGER_SYSTEM
) {
900 dbus_set_error(&error
, BUS_ERROR_NOT_SUPPORTED
, "Halting is only supported for system managers.");
901 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOTSUP
);
904 if (!(reply
= dbus_message_new_method_return(message
)))
907 m
->exit_code
= MANAGER_HALT
;
909 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "KExec")) {
911 if (m
->running_as
!= MANAGER_SYSTEM
) {
912 dbus_set_error(&error
, BUS_ERROR_NOT_SUPPORTED
, "kexec is only supported for system managers.");
913 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOTSUP
);
916 if (!(reply
= dbus_message_new_method_return(message
)))
919 m
->exit_code
= MANAGER_KEXEC
;
921 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
922 char **l
= NULL
, **e
= NULL
;
924 if ((r
= bus_parse_strv(message
, &l
)) < 0) {
928 return bus_send_error_reply(m
, connection
, message
, NULL
, r
);
931 e
= strv_env_merge(2, m
->environment
, l
);
937 if (!(reply
= dbus_message_new_method_return(message
))) {
942 strv_free(m
->environment
);
945 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
946 char **l
= NULL
, **e
= NULL
;
948 if ((r
= bus_parse_strv(message
, &l
)) < 0) {
952 return bus_send_error_reply(m
, connection
, message
, NULL
, r
);
955 e
= strv_env_delete(m
->environment
, 1, l
);
961 if (!(reply
= dbus_message_new_method_return(message
)))
964 strv_free(m
->environment
);
968 return bus_default_message_handler(m
, connection
, message
, NULL
, properties
);
970 if (job_type
!= _JOB_TYPE_INVALID
) {
971 const char *name
, *smode
, *old_name
= NULL
;
977 if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
978 b
= dbus_message_get_args(
981 DBUS_TYPE_STRING
, &old_name
,
982 DBUS_TYPE_STRING
, &name
,
983 DBUS_TYPE_STRING
, &smode
,
986 b
= dbus_message_get_args(
989 DBUS_TYPE_STRING
, &name
,
990 DBUS_TYPE_STRING
, &smode
,
994 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
997 if (!(u
= manager_get_unit(m
, old_name
)) ||
999 u
->meta
.job
->type
!= JOB_START
) {
1000 dbus_set_error(&error
, BUS_ERROR_NO_SUCH_JOB
, "No job queued for unit %s", old_name
);
1001 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOENT
);
1005 if ((mode
= job_mode_from_string(smode
)) == _JOB_MODE_INVALID
) {
1006 dbus_set_error(&error
, BUS_ERROR_INVALID_JOB_MODE
, "Job mode %s is invalid.", smode
);
1007 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
1010 if ((r
= manager_load_unit(m
, name
, NULL
, &error
, &u
)) < 0)
1011 return bus_send_error_reply(m
, connection
, message
, &error
, r
);
1013 if (reload_if_possible
&& unit_can_reload(u
)) {
1014 if (job_type
== JOB_RESTART
)
1015 job_type
= JOB_RELOAD_OR_START
;
1016 else if (job_type
== JOB_TRY_RESTART
)
1017 job_type
= JOB_RELOAD
;
1020 if ((job_type
== JOB_START
&& u
->meta
.refuse_manual_start
) ||
1021 (job_type
== JOB_STOP
&& u
->meta
.refuse_manual_stop
) ||
1022 ((job_type
== JOB_RESTART
|| job_type
== JOB_TRY_RESTART
) &&
1023 (u
->meta
.refuse_manual_start
|| u
->meta
.refuse_manual_stop
))) {
1024 dbus_set_error(&error
, BUS_ERROR_ONLY_BY_DEPENDENCY
, "Operation refused, may be requested by dependency only.");
1025 return bus_send_error_reply(m
, connection
, message
, &error
, -EPERM
);
1028 if ((r
= manager_add_job(m
, job_type
, u
, mode
, true, &error
, &j
)) < 0)
1029 return bus_send_error_reply(m
, connection
, message
, &error
, r
);
1031 if (!(j
->bus_client
= strdup(message_get_sender_with_fallback(message
))))
1034 j
->bus
= connection
;
1036 if (!(reply
= dbus_message_new_method_return(message
)))
1039 if (!(path
= job_dbus_path(j
)))
1042 if (!dbus_message_append_args(
1044 DBUS_TYPE_OBJECT_PATH
, &path
,
1052 if (!dbus_connection_send(connection
, reply
, NULL
))
1055 dbus_message_unref(reply
);
1058 return DBUS_HANDLER_RESULT_HANDLED
;
1064 dbus_message_unref(reply
);
1066 dbus_error_free(&error
);
1068 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
1071 const DBusObjectPathVTable bus_manager_vtable
= {
1072 .message_function
= bus_manager_message_handler