1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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-unit.h"
28 int bus_unit_append_names(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
34 if (!dbus_message_iter_open_container(i
, DBUS_TYPE_ARRAY
, "s", &sub
))
37 SET_FOREACH(t
, u
->meta
.names
, j
)
38 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &t
))
41 if (!dbus_message_iter_close_container(i
, &sub
))
47 int bus_unit_append_dependencies(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
53 if (!dbus_message_iter_open_container(i
, DBUS_TYPE_ARRAY
, "s", &sub
))
57 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &u
->meta
.id
))
60 if (!dbus_message_iter_close_container(i
, &sub
))
66 int bus_unit_append_description(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
75 d
= unit_description(u
);
77 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &d
))
83 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_load_state
, unit_load_state
, UnitLoadState
);
85 int bus_unit_append_active_state(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
94 state
= unit_active_state_to_string(unit_active_state(u
));
96 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &state
))
102 int bus_unit_append_sub_state(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
111 state
= unit_sub_state_to_string(u
);
113 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &state
))
119 int bus_unit_append_can_start(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
128 b
= unit_can_start(u
);
130 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_BOOLEAN
, &b
))
136 int bus_unit_append_can_reload(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
145 b
= unit_can_reload(u
);
147 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_BOOLEAN
, &b
))
153 int bus_unit_append_job(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
163 if (!dbus_message_iter_open_container(i
, DBUS_TYPE_STRUCT
, NULL
, &sub
))
168 if (!(p
= job_dbus_path(u
->meta
.job
)))
171 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_UINT32
, &u
->meta
.job
->id
) ||
172 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_OBJECT_PATH
, &p
)) {
179 /* No job, so let's fill in some placeholder
180 * data. Since we need to fill in a valid path we
181 * simple point to ourselves. */
183 if (!(p
= unit_dbus_path(u
)))
186 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_UINT32
, &id
) ||
187 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_OBJECT_PATH
, &p
)) {
195 if (!dbus_message_iter_close_container(i
, &sub
))
201 int bus_unit_append_default_cgroup(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
212 if ((cgb
= unit_get_default_cgroup(u
))) {
213 if (!(t
= cgroup_bonding_to_string(cgb
)))
218 success
= dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
);
223 return success
? 0 : -ENOMEM
;
226 int bus_unit_append_cgroups(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
231 if (!dbus_message_iter_open_container(i
, DBUS_TYPE_ARRAY
, "s", &sub
))
234 LIST_FOREACH(by_unit
, cgb
, u
->meta
.cgroup_bondings
) {
238 if (!(t
= cgroup_bonding_to_string(cgb
)))
241 success
= dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &t
);
248 if (!dbus_message_iter_close_container(i
, &sub
))
254 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_kill_mode
, kill_mode
, KillMode
);
256 static DBusHandlerResult
bus_unit_message_dispatch(Unit
*u
, DBusMessage
*message
) {
257 DBusMessage
*reply
= NULL
;
258 Manager
*m
= u
->meta
.manager
;
260 JobType job_type
= _JOB_TYPE_INVALID
;
262 dbus_error_init(&error
);
264 if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Unit", "Start"))
265 job_type
= JOB_START
;
266 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Unit", "Stop"))
268 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Unit", "Reload"))
269 job_type
= JOB_RELOAD
;
270 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Unit", "Restart"))
271 job_type
= JOB_RESTART
;
272 else if (UNIT_VTABLE(u
)->bus_message_handler
)
273 return UNIT_VTABLE(u
)->bus_message_handler(u
, message
);
275 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
277 if (job_type
!= _JOB_TYPE_INVALID
) {
284 if (job_type
== JOB_START
&& u
->meta
.only_by_dependency
)
285 return bus_send_error_reply(m
, message
, NULL
, -EPERM
);
287 if (!dbus_message_get_args(
290 DBUS_TYPE_STRING
, &smode
,
292 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
294 if ((mode
= job_mode_from_string(smode
)) == _JOB_MODE_INVALID
)
295 return bus_send_error_reply(m
, message
, NULL
, -EINVAL
);
297 if ((r
= manager_add_job(m
, job_type
, u
, mode
, true, &j
)) < 0)
298 return bus_send_error_reply(m
, message
, NULL
, r
);
300 if (!(reply
= dbus_message_new_method_return(message
)))
303 if (!(path
= job_dbus_path(j
)))
306 if (!dbus_message_append_args(
308 DBUS_TYPE_OBJECT_PATH
, &path
,
314 if (!dbus_connection_send(m
->api_bus
, reply
, NULL
))
317 dbus_message_unref(reply
);
320 return DBUS_HANDLER_RESULT_HANDLED
;
324 dbus_message_unref(reply
);
326 dbus_error_free(&error
);
328 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
331 static DBusHandlerResult
bus_unit_message_handler(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
340 log_debug("Got D-Bus request: %s.%s() on %s",
341 dbus_message_get_interface(message
),
342 dbus_message_get_member(message
),
343 dbus_message_get_path(message
));
345 if ((r
= manager_get_unit_from_dbus_path(m
, dbus_message_get_path(message
), &u
)) < 0) {
348 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
351 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
353 return bus_send_error_reply(m
, message
, NULL
, r
);
356 return bus_unit_message_dispatch(u
, message
);
359 const DBusObjectPathVTable bus_unit_vtable
= {
360 .message_function
= bus_unit_message_handler
363 void bus_unit_send_change_signal(Unit
*u
) {
365 DBusMessage
*m
= NULL
;
368 assert(u
->meta
.in_dbus_queue
);
370 LIST_REMOVE(Meta
, dbus_queue
, u
->meta
.manager
->dbus_unit_queue
, &u
->meta
);
371 u
->meta
.in_dbus_queue
= false;
373 if (set_isempty(u
->meta
.manager
->subscribed
)) {
374 u
->meta
.sent_dbus_new_signal
= true;
378 if (!(p
= unit_dbus_path(u
)))
381 if (u
->meta
.sent_dbus_new_signal
) {
382 /* Send a change signal */
384 if (!(m
= dbus_message_new_signal(p
, "org.freedesktop.systemd1.Unit", "Changed")))
387 /* Send a new signal */
389 if (!(m
= dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
392 if (!dbus_message_append_args(m
,
393 DBUS_TYPE_STRING
, &u
->meta
.id
,
394 DBUS_TYPE_OBJECT_PATH
, &p
,
399 if (!dbus_connection_send(u
->meta
.manager
->api_bus
, m
, NULL
))
403 dbus_message_unref(m
);
405 u
->meta
.sent_dbus_new_signal
= true;
413 dbus_message_unref(m
);
415 log_error("Failed to allocate unit change/new signal.");
418 void bus_unit_send_removed_signal(Unit
*u
) {
420 DBusMessage
*m
= NULL
;
424 if (set_isempty(u
->meta
.manager
->subscribed
) || !u
->meta
.sent_dbus_new_signal
)
427 if (!(p
= unit_dbus_path(u
)))
430 if (!(m
= dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
433 if (!dbus_message_append_args(m
,
434 DBUS_TYPE_STRING
, &u
->meta
.id
,
435 DBUS_TYPE_OBJECT_PATH
, &p
,
439 if (!dbus_connection_send(u
->meta
.manager
->api_bus
, m
, NULL
))
443 dbus_message_unref(m
);
451 dbus_message_unref(m
);
453 log_error("Failed to allocate unit remove signal.");