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/>.
27 #include "dbus-manager.h"
29 #include "bus-errors.h"
32 #define BUS_MANAGER_INTERFACE_BEGIN \
33 " <interface name=\"org.freedesktop.systemd1.Manager\">\n"
35 #define BUS_MANAGER_INTERFACE_METHODS \
36 " <method name=\"GetUnit\">\n" \
37 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
38 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
40 " <method name=\"GetUnitByPID\">\n" \
41 " <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n" \
42 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
44 " <method name=\"LoadUnit\">\n" \
45 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
46 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
48 " <method name=\"StartUnit\">\n" \
49 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
50 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
51 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
53 " <method name=\"StartUnitReplace\">\n" \
54 " <arg name=\"old_unit\" type=\"s\" direction=\"in\"/>\n" \
55 " <arg name=\"new_unit\" 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=\"StopUnit\">\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=\"ReloadUnit\">\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=\"RestartUnit\">\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=\"TryRestartUnit\">\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=\"ReloadOrRestartUnit\">\n" \
80 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
81 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
82 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
84 " <method name=\"ReloadOrTryRestartUnit\">\n" \
85 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
86 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
87 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
89 " <method name=\"KillUnit\">\n" \
90 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
91 " <arg name=\"who\" type=\"s\" direction=\"in\"/>\n" \
92 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
93 " <arg name=\"signal\" type=\"i\" direction=\"in\"/>\n" \
95 " <method name=\"ResetFailedUnit\">\n" \
96 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
98 " <method name=\"GetJob\">\n" \
99 " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
100 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
102 " <method name=\"ClearJobs\"/>\n" \
103 " <method name=\"ResetFailed\"/>\n" \
104 " <method name=\"ListUnits\">\n" \
105 " <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
107 " <method name=\"ListJobs\">\n" \
108 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
110 " <method name=\"Subscribe\"/>\n" \
111 " <method name=\"Unsubscribe\"/>\n" \
112 " <method name=\"Dump\"/>\n" \
113 " <method name=\"CreateSnapshot\">\n" \
114 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
115 " <arg name=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \
116 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
118 " <method name=\"Reload\"/>\n" \
119 " <method name=\"Reexecute\"/>\n" \
120 " <method name=\"Exit\"/>\n" \
121 " <method name=\"Reboot\"/>\n" \
122 " <method name=\"PowerOff\"/>\n" \
123 " <method name=\"Halt\"/>\n" \
124 " <method name=\"KExec\"/>\n" \
125 " <method name=\"SetEnvironment\">\n" \
126 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
128 " <method name=\"UnsetEnvironment\">\n" \
129 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
132 #define BUS_MANAGER_INTERFACE_SIGNALS \
133 " <signal name=\"UnitNew\">\n" \
134 " <arg name=\"id\" type=\"s\"/>\n" \
135 " <arg name=\"unit\" type=\"o\"/>\n" \
137 " <signal name=\"UnitRemoved\">\n" \
138 " <arg name=\"id\" type=\"s\"/>\n" \
139 " <arg name=\"unit\" type=\"o\"/>\n" \
141 " <signal name=\"JobNew\">\n" \
142 " <arg name=\"id\" type=\"u\"/>\n" \
143 " <arg name=\"job\" type=\"o\"/>\n" \
145 " <signal name=\"JobRemoved\">\n" \
146 " <arg name=\"id\" type=\"u\"/>\n" \
147 " <arg name=\"job\" type=\"o\"/>\n" \
148 " <arg name=\"result\" type=\"s\"/>\n" \
151 #define BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
152 " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
153 " <property name=\"Distribution\" type=\"s\" access=\"read\"/>\n" \
154 " <property name=\"Features\" type=\"s\" access=\"read\"/>\n" \
155 " <property name=\"Tainted\" type=\"s\" access=\"read\"/>\n" \
156 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
157 " <property name=\"InitRDTimestamp\" type=\"t\" access=\"read\"/>\n" \
158 " <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
159 " <property name=\"FinishTimestamp\" type=\"t\" access=\"read\"/>\n" \
160 " <property name=\"LogLevel\" type=\"s\" access=\"readwrite\"/>\n" \
161 " <property name=\"LogTarget\" type=\"s\" access=\"readwrite\"/>\n" \
162 " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
163 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
164 " <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \
165 " <property name=\"NFailedJobs\" type=\"u\" access=\"read\"/>\n" \
166 " <property name=\"Progress\" type=\"d\" access=\"read\"/>\n" \
167 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
168 " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
169 " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
170 " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
171 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
172 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
173 " <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
174 " <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n" \
175 " <property name=\"DefaultControllers\" type=\"as\" access=\"read\"/>\n" \
176 " <property name=\"DefaultStandardOutput\" type=\"s\" access=\"read\"/>\n" \
177 " <property name=\"DefaultStandardError\" type=\"s\" access=\"read\"/>\n"
179 #ifdef HAVE_SYSV_COMPAT
180 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
181 " <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
182 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
183 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n"
185 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV
188 #define BUS_MANAGER_INTERFACE_END \
191 #define BUS_MANAGER_INTERFACE \
192 BUS_MANAGER_INTERFACE_BEGIN \
193 BUS_MANAGER_INTERFACE_METHODS \
194 BUS_MANAGER_INTERFACE_SIGNALS \
195 BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
196 BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
197 BUS_MANAGER_INTERFACE_END
199 #define INTROSPECTION_BEGIN \
200 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
202 BUS_MANAGER_INTERFACE \
203 BUS_PROPERTIES_INTERFACE \
205 BUS_INTROSPECTABLE_INTERFACE
207 #define INTROSPECTION_END \
210 #define INTERFACES_LIST \
211 BUS_GENERIC_INTERFACES_LIST \
212 "org.freedesktop.systemd1.Manager\0"
214 const char bus_manager_interface
[] _introspect_("Manager") = BUS_MANAGER_INTERFACE
;
216 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as
, manager_running_as
, ManagerRunningAs
);
217 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_exec_output
, exec_output
, ExecOutput
);
219 static int bus_manager_append_tainted(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
221 char buf
[64] = "", *e
= buf
, *p
= NULL
;
228 e
= stpcpy(e
, "usr-separate-fs ");
230 if (readlink_malloc("/etc/mtab", &p
) < 0)
231 e
= stpcpy(e
, "etc-mtab-not-symlink ");
235 if (access("/proc/cgroups", F_OK
) < 0)
236 e
= stpcpy(e
, "cgroups-missing ");
240 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
))
246 static int bus_manager_append_log_target(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
253 t
= log_target_to_string(log_get_target());
255 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
))
261 static int bus_manager_set_log_target(Manager
*m
, DBusMessageIter
*i
, const char *property
) {
268 dbus_message_iter_get_basic(i
, &t
);
270 return log_set_target_from_string(t
);
273 static int bus_manager_append_log_level(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
280 t
= log_level_to_string(log_get_max_level());
282 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
))
288 static int bus_manager_set_log_level(Manager
*m
, DBusMessageIter
*i
, const char *property
) {
295 dbus_message_iter_get_basic(i
, &t
);
297 return log_set_max_level_from_string(t
);
300 static int bus_manager_append_n_names(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
307 u
= hashmap_size(m
->units
);
309 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT32
, &u
))
315 static int bus_manager_append_n_jobs(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
322 u
= hashmap_size(m
->jobs
);
324 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT32
, &u
))
330 static int bus_manager_append_progress(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
337 if (dual_timestamp_is_set(&m
->finish_timestamp
))
340 d
= 1.0 - ((double) hashmap_size(m
->jobs
) / (double) m
->n_installed_jobs
);
342 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_DOUBLE
, &d
))
348 static const char *message_get_sender_with_fallback(DBusMessage
*m
) {
353 if ((s
= dbus_message_get_sender(m
)))
356 /* When the message came in from a direct connection the
357 * message will have no sender. We fix that here. */
362 static DBusHandlerResult
bus_manager_message_handler(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
365 const BusProperty properties
[] = {
366 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string
, "s", PACKAGE_STRING
},
367 { "org.freedesktop.systemd1.Manager", "Distribution", bus_property_append_string
, "s", DISTRIBUTION
},
368 { "org.freedesktop.systemd1.Manager", "Features", bus_property_append_string
, "s", SYSTEMD_FEATURES
},
369 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as
, "s", &m
->running_as
},
370 { "org.freedesktop.systemd1.Manager", "Tainted", bus_manager_append_tainted
, "s", m
},
371 { "org.freedesktop.systemd1.Manager", "InitRDTimestamp", bus_property_append_uint64
, "t", &m
->initrd_timestamp
.realtime
},
372 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64
, "t", &m
->startup_timestamp
.realtime
},
373 { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64
, "t", &m
->finish_timestamp
.realtime
},
374 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level
, "s", NULL
, bus_manager_set_log_level
},
375 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target
, "s", NULL
, bus_manager_set_log_target
},
376 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names
, "u", NULL
},
377 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs
, "u", NULL
},
378 { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32
, "u", &m
->n_installed_jobs
},
379 { "org.freedesktop.systemd1.Manager", "NFailedJobs", bus_property_append_uint32
, "u", &m
->n_failed_jobs
},
380 { "org.freedesktop.systemd1.Manager", "Progress", bus_manager_append_progress
, "d", NULL
},
381 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv
, "as", m
->environment
},
382 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool
, "b", &m
->confirm_spawn
},
383 { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool
, "b", &m
->show_status
},
384 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv
, "as", m
->lookup_paths
.unit_path
},
385 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string
, "s", m
->notify_socket
},
386 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string
, "s", m
->cgroup_hierarchy
},
387 { "org.freedesktop.systemd1.Manager", "MountAuto", bus_property_append_bool
, "b", &m
->mount_auto
},
388 { "org.freedesktop.systemd1.Manager", "SwapAuto", bus_property_append_bool
, "b", &m
->swap_auto
},
389 { "org.freedesktop.systemd1.Manager", "DefaultControllers", bus_property_append_strv
, "as", m
->default_controllers
},
390 { "org.freedesktop.systemd1.Manager", "DefaultStandardOutput", bus_manager_append_exec_output
, "s", &m
->default_std_output
},
391 { "org.freedesktop.systemd1.Manager", "DefaultStandardError", bus_manager_append_exec_output
, "s", &m
->default_std_error
},
392 #ifdef HAVE_SYSV_COMPAT
393 { "org.freedesktop.systemd1.Manager", "SysVConsole", bus_property_append_bool
, "b", &m
->sysv_console
},
394 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv
, "as", m
->lookup_paths
.sysvinit_path
},
395 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv
, "as", m
->lookup_paths
.sysvrcnd_path
},
397 { NULL
, NULL
, NULL
, NULL
, NULL
}
402 DBusMessage
*reply
= NULL
;
404 JobType job_type
= _JOB_TYPE_INVALID
;
405 bool reload_if_possible
= false;
411 dbus_error_init(&error
);
413 if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "GetUnit")) {
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 if (!(reply
= dbus_message_new_method_return(message
)))
432 if (!(path
= unit_dbus_path(u
)))
435 if (!dbus_message_append_args(
437 DBUS_TYPE_OBJECT_PATH
, &path
,
440 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
444 if (!dbus_message_get_args(
447 DBUS_TYPE_UINT32
, &pid
,
449 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
451 if (!(u
= cgroup_unit_by_pid(m
, (pid_t
) pid
))) {
452 dbus_set_error(&error
, BUS_ERROR_NO_SUCH_UNIT
, "No unit for PID %lu is loaded.", (unsigned long) pid
);
453 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOENT
);
456 if (!(reply
= dbus_message_new_method_return(message
)))
459 if (!(path
= unit_dbus_path(u
)))
462 if (!dbus_message_append_args(
464 DBUS_TYPE_OBJECT_PATH
, &path
,
467 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
471 if (!dbus_message_get_args(
474 DBUS_TYPE_STRING
, &name
,
476 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
478 if ((r
= manager_load_unit(m
, name
, NULL
, &error
, &u
)) < 0)
479 return bus_send_error_reply(m
, connection
, message
, &error
, r
);
481 if (!(reply
= dbus_message_new_method_return(message
)))
484 if (!(path
= unit_dbus_path(u
)))
487 if (!dbus_message_append_args(
489 DBUS_TYPE_OBJECT_PATH
, &path
,
493 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "StartUnit"))
494 job_type
= JOB_START
;
495 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
496 job_type
= JOB_START
;
497 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "StopUnit"))
499 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
500 job_type
= JOB_RELOAD
;
501 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "RestartUnit"))
502 job_type
= JOB_RESTART
;
503 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
504 job_type
= JOB_TRY_RESTART
;
505 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
506 reload_if_possible
= true;
507 job_type
= JOB_RESTART
;
508 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
509 reload_if_possible
= true;
510 job_type
= JOB_TRY_RESTART
;
511 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "KillUnit")) {
512 const char *name
, *swho
, *smode
;
518 if (!dbus_message_get_args(
521 DBUS_TYPE_STRING
, &name
,
522 DBUS_TYPE_STRING
, &swho
,
523 DBUS_TYPE_STRING
, &smode
,
524 DBUS_TYPE_INT32
, &signo
,
526 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
528 if ((mode
= kill_mode_from_string(smode
)) < 0 ||
529 (who
= kill_who_from_string(swho
)) < 0 ||
532 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
534 if (!(u
= manager_get_unit(m
, name
))) {
535 dbus_set_error(&error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s is not loaded.", name
);
536 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOENT
);
539 if ((r
= unit_kill(u
, who
, mode
, signo
, &error
)) < 0)
540 return bus_send_error_reply(m
, connection
, message
, &error
, r
);
542 if (!(reply
= dbus_message_new_method_return(message
)))
545 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "GetJob")) {
549 if (!dbus_message_get_args(
552 DBUS_TYPE_UINT32
, &id
,
554 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
556 if (!(j
= manager_get_job(m
, id
))) {
557 dbus_set_error(&error
, BUS_ERROR_NO_SUCH_JOB
, "Job %u does not exist.", (unsigned) id
);
558 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOENT
);
561 if (!(reply
= dbus_message_new_method_return(message
)))
564 if (!(path
= job_dbus_path(j
)))
567 if (!dbus_message_append_args(
569 DBUS_TYPE_OBJECT_PATH
, &path
,
573 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
575 manager_clear_jobs(m
);
577 if (!(reply
= dbus_message_new_method_return(message
)))
580 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
582 manager_reset_failed(m
);
584 if (!(reply
= dbus_message_new_method_return(message
)))
587 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
591 if (!dbus_message_get_args(
594 DBUS_TYPE_STRING
, &name
,
596 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
598 if (!(u
= manager_get_unit(m
, name
))) {
599 dbus_set_error(&error
, BUS_ERROR_NO_SUCH_UNIT
, "Unit %s is not loaded.", name
);
600 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOENT
);
603 unit_reset_failed(u
);
605 if (!(reply
= dbus_message_new_method_return(message
)))
608 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ListUnits")) {
609 DBusMessageIter iter
, sub
;
614 if (!(reply
= dbus_message_new_method_return(message
)))
617 dbus_message_iter_init_append(reply
, &iter
);
619 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(ssssssouso)", &sub
))
622 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
623 char *u_path
, *j_path
;
624 const char *description
, *load_state
, *active_state
, *sub_state
, *sjob_type
, *following
;
625 DBusMessageIter sub2
;
632 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_STRUCT
, NULL
, &sub2
))
635 description
= unit_description(u
);
636 load_state
= unit_load_state_to_string(u
->meta
.load_state
);
637 active_state
= unit_active_state_to_string(unit_active_state(u
));
638 sub_state
= unit_sub_state_to_string(u
);
640 f
= unit_following(u
);
641 following
= f
? f
->meta
.id
: "";
643 if (!(u_path
= unit_dbus_path(u
)))
647 job_id
= (uint32_t) u
->meta
.job
->id
;
649 if (!(j_path
= job_dbus_path(u
->meta
.job
))) {
654 sjob_type
= job_type_to_string(u
->meta
.job
->type
);
661 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &u
->meta
.id
) ||
662 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &description
) ||
663 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &load_state
) ||
664 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &active_state
) ||
665 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &sub_state
) ||
666 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &following
) ||
667 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u_path
) ||
668 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &job_id
) ||
669 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &sjob_type
) ||
670 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &j_path
)) {
681 if (!dbus_message_iter_close_container(&sub
, &sub2
))
685 if (!dbus_message_iter_close_container(&iter
, &sub
))
688 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ListJobs")) {
689 DBusMessageIter iter
, sub
;
693 if (!(reply
= dbus_message_new_method_return(message
)))
696 dbus_message_iter_init_append(reply
, &iter
);
698 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(usssoo)", &sub
))
701 HASHMAP_FOREACH(j
, m
->jobs
, i
) {
702 char *u_path
, *j_path
;
703 const char *state
, *type
;
705 DBusMessageIter sub2
;
707 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_STRUCT
, NULL
, &sub2
))
710 id
= (uint32_t) j
->id
;
711 state
= job_state_to_string(j
->state
);
712 type
= job_type_to_string(j
->type
);
714 if (!(j_path
= job_dbus_path(j
)))
717 if (!(u_path
= unit_dbus_path(j
->unit
))) {
722 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &id
) ||
723 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &j
->unit
->meta
.id
) ||
724 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &type
) ||
725 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &state
) ||
726 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &j_path
) ||
727 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u_path
)) {
736 if (!dbus_message_iter_close_container(&sub
, &sub2
))
740 if (!dbus_message_iter_close_container(&iter
, &sub
))
743 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Subscribe")) {
747 if (!(s
= BUS_CONNECTION_SUBSCRIBED(m
, connection
))) {
748 if (!(s
= set_new(string_hash_func
, string_compare_func
)))
751 if (!(dbus_connection_set_data(connection
, m
->subscribed_data_slot
, s
, NULL
))) {
757 if (!(client
= strdup(message_get_sender_with_fallback(message
))))
760 if ((r
= set_put(s
, client
)) < 0) {
762 return bus_send_error_reply(m
, connection
, message
, NULL
, r
);
765 if (!(reply
= dbus_message_new_method_return(message
)))
768 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
771 if (!(client
= set_remove(BUS_CONNECTION_SUBSCRIBED(m
, connection
), (char*) message_get_sender_with_fallback(message
)))) {
772 dbus_set_error(&error
, BUS_ERROR_NOT_SUBSCRIBED
, "Client is not subscribed.");
773 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOENT
);
778 if (!(reply
= dbus_message_new_method_return(message
)))
781 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Dump")) {
786 if (!(reply
= dbus_message_new_method_return(message
)))
789 if (!(f
= open_memstream(&dump
, &size
)))
792 manager_dump_units(m
, f
, NULL
);
793 manager_dump_jobs(m
, f
, NULL
);
803 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &dump
, DBUS_TYPE_INVALID
)) {
809 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
814 if (!dbus_message_get_args(
817 DBUS_TYPE_STRING
, &name
,
818 DBUS_TYPE_BOOLEAN
, &cleanup
,
820 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
822 if (name
&& name
[0] == 0)
825 if ((r
= snapshot_create(m
, name
, cleanup
, &error
, &s
)) < 0)
826 return bus_send_error_reply(m
, connection
, message
, &error
, r
);
828 if (!(reply
= dbus_message_new_method_return(message
)))
831 if (!(path
= unit_dbus_path(UNIT(s
))))
834 if (!dbus_message_append_args(
836 DBUS_TYPE_OBJECT_PATH
, &path
,
840 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", "Introspect")) {
841 char *introspection
= NULL
;
849 if (!(reply
= dbus_message_new_method_return(message
)))
852 /* We roll our own introspection code here, instead of
853 * relying on bus_default_message_handler() because we
854 * need to generate our introspection string
857 if (!(f
= open_memstream(&introspection
, &size
)))
860 fputs(INTROSPECTION_BEGIN
, f
);
862 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
868 if (!(p
= bus_path_escape(k
))) {
874 fprintf(f
, "<node name=\"unit/%s\"/>", p
);
878 HASHMAP_FOREACH(j
, m
->jobs
, i
)
879 fprintf(f
, "<node name=\"job/%lu\"/>", (unsigned long) j
->id
);
881 fputs(INTROSPECTION_END
, f
);
894 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &introspection
, DBUS_TYPE_INVALID
)) {
901 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Reload")) {
903 assert(!m
->queued_message
);
905 /* Instead of sending the reply back right away, we
906 * just remember that we need to and then send it
907 * after the reload is finished. That way the caller
908 * knows when the reload finished. */
910 if (!(m
->queued_message
= dbus_message_new_method_return(message
)))
913 m
->queued_message_connection
= connection
;
914 m
->exit_code
= MANAGER_RELOAD
;
916 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Reexecute")) {
918 if (!(reply
= dbus_message_new_method_return(message
)))
921 m
->exit_code
= MANAGER_REEXECUTE
;
923 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Exit")) {
925 if (m
->running_as
== MANAGER_SYSTEM
) {
926 dbus_set_error(&error
, BUS_ERROR_NOT_SUPPORTED
, "Exit is only supported for user service managers.");
927 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOTSUP
);
930 if (!(reply
= dbus_message_new_method_return(message
)))
933 m
->exit_code
= MANAGER_EXIT
;
935 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Reboot")) {
937 if (m
->running_as
!= MANAGER_SYSTEM
) {
938 dbus_set_error(&error
, BUS_ERROR_NOT_SUPPORTED
, "Reboot is only supported for system managers.");
939 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOTSUP
);
942 if (!(reply
= dbus_message_new_method_return(message
)))
945 m
->exit_code
= MANAGER_REBOOT
;
947 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "PowerOff")) {
949 if (m
->running_as
!= MANAGER_SYSTEM
) {
950 dbus_set_error(&error
, BUS_ERROR_NOT_SUPPORTED
, "Powering off is only supported for system managers.");
951 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOTSUP
);
954 if (!(reply
= dbus_message_new_method_return(message
)))
957 m
->exit_code
= MANAGER_POWEROFF
;
959 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Halt")) {
961 if (m
->running_as
!= MANAGER_SYSTEM
) {
962 dbus_set_error(&error
, BUS_ERROR_NOT_SUPPORTED
, "Halting is only supported for system managers.");
963 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOTSUP
);
966 if (!(reply
= dbus_message_new_method_return(message
)))
969 m
->exit_code
= MANAGER_HALT
;
971 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "KExec")) {
973 if (m
->running_as
!= MANAGER_SYSTEM
) {
974 dbus_set_error(&error
, BUS_ERROR_NOT_SUPPORTED
, "kexec is only supported for system managers.");
975 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOTSUP
);
978 if (!(reply
= dbus_message_new_method_return(message
)))
981 m
->exit_code
= MANAGER_KEXEC
;
983 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
984 char **l
= NULL
, **e
= NULL
;
986 if ((r
= bus_parse_strv(message
, &l
)) < 0) {
990 return bus_send_error_reply(m
, connection
, message
, NULL
, r
);
993 e
= strv_env_merge(2, m
->environment
, l
);
999 if (!(reply
= dbus_message_new_method_return(message
))) {
1004 strv_free(m
->environment
);
1007 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
1008 char **l
= NULL
, **e
= NULL
;
1010 if ((r
= bus_parse_strv(message
, &l
)) < 0) {
1014 return bus_send_error_reply(m
, connection
, message
, NULL
, r
);
1017 e
= strv_env_delete(m
->environment
, 1, l
);
1023 if (!(reply
= dbus_message_new_method_return(message
))) {
1028 strv_free(m
->environment
);
1032 return bus_default_message_handler(m
, connection
, message
, NULL
, INTERFACES_LIST
, properties
);
1034 if (job_type
!= _JOB_TYPE_INVALID
) {
1035 const char *name
, *smode
, *old_name
= NULL
;
1041 if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
1042 b
= dbus_message_get_args(
1045 DBUS_TYPE_STRING
, &old_name
,
1046 DBUS_TYPE_STRING
, &name
,
1047 DBUS_TYPE_STRING
, &smode
,
1050 b
= dbus_message_get_args(
1053 DBUS_TYPE_STRING
, &name
,
1054 DBUS_TYPE_STRING
, &smode
,
1058 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
1061 if (!(u
= manager_get_unit(m
, old_name
)) ||
1063 u
->meta
.job
->type
!= JOB_START
) {
1064 dbus_set_error(&error
, BUS_ERROR_NO_SUCH_JOB
, "No job queued for unit %s", old_name
);
1065 return bus_send_error_reply(m
, connection
, message
, &error
, -ENOENT
);
1069 if ((mode
= job_mode_from_string(smode
)) == _JOB_MODE_INVALID
) {
1070 dbus_set_error(&error
, BUS_ERROR_INVALID_JOB_MODE
, "Job mode %s is invalid.", smode
);
1071 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
1074 if ((r
= manager_load_unit(m
, name
, NULL
, &error
, &u
)) < 0)
1075 return bus_send_error_reply(m
, connection
, message
, &error
, r
);
1077 if (reload_if_possible
&& unit_can_reload(u
)) {
1078 if (job_type
== JOB_RESTART
)
1079 job_type
= JOB_RELOAD_OR_START
;
1080 else if (job_type
== JOB_TRY_RESTART
)
1081 job_type
= JOB_RELOAD
;
1084 if ((job_type
== JOB_START
&& u
->meta
.refuse_manual_start
) ||
1085 (job_type
== JOB_STOP
&& u
->meta
.refuse_manual_stop
) ||
1086 ((job_type
== JOB_RESTART
|| job_type
== JOB_TRY_RESTART
) &&
1087 (u
->meta
.refuse_manual_start
|| u
->meta
.refuse_manual_stop
))) {
1088 dbus_set_error(&error
, BUS_ERROR_ONLY_BY_DEPENDENCY
, "Operation refused, may be requested by dependency only.");
1089 return bus_send_error_reply(m
, connection
, message
, &error
, -EPERM
);
1092 if ((r
= manager_add_job(m
, job_type
, u
, mode
, true, &error
, &j
)) < 0)
1093 return bus_send_error_reply(m
, connection
, message
, &error
, r
);
1095 if (!(j
->bus_client
= strdup(message_get_sender_with_fallback(message
))))
1098 j
->bus
= connection
;
1100 if (!(reply
= dbus_message_new_method_return(message
)))
1103 if (!(path
= job_dbus_path(j
)))
1106 if (!dbus_message_append_args(
1108 DBUS_TYPE_OBJECT_PATH
, &path
,
1114 if (!dbus_connection_send(connection
, reply
, NULL
))
1117 dbus_message_unref(reply
);
1122 return DBUS_HANDLER_RESULT_HANDLED
;
1128 dbus_message_unref(reply
);
1130 dbus_error_free(&error
);
1132 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
1135 const DBusObjectPathVTable bus_manager_vtable
= {
1136 .message_function
= bus_manager_message_handler