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/>.
27 static const char introspection
[] =
28 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
30 " <interface name=\"org.freedesktop.systemd1.Unit\">"
31 " <method name=\"Start\">"
32 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>"
33 " <arg name=\"job\" type=\"o\" direction=\"out\"/>"
35 " <method name=\"Stop\">"
36 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>"
37 " <arg name=\"job\" type=\"o\" direction=\"out\"/>"
39 " <method name=\"Restart\">"
40 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>"
41 " <arg name=\"job\" type=\"o\" direction=\"out\"/>"
43 " <method name=\"Reload\">"
44 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>"
45 " <arg name=\"job\" type=\"o\" direction=\"out\"/>"
47 " <signal name=\"Changed\"/>"
48 " <property name=\"Id\" type=\"s\" access=\"read\"/>"
49 " <property name=\"Description\" type=\"s\" access=\"read\"/>"
50 " <property name=\"LoadState\" type=\"s\" access=\"read\"/>"
51 " <property name=\"ActiveState\" type=\"s\" access=\"read\"/>"
52 " <property name=\"FragmentPath\" type=\"s\" access=\"read\"/>"
53 " <property name=\"ActiveEnterTimestamp\" type=\"t\" access=\"read\"/>"
54 " <property name=\"ActiveExitTimestamp\" type=\"t\" access=\"read\"/>"
55 " <property name=\"CanReload\" type=\"b\" access=\"read\"/>"
56 " <property name=\"CanStart\" type=\"b\" access=\"read\"/>"
57 " <property name=\"Job\" type=\"(uo)\" access=\"read\"/>"
59 BUS_PROPERTIES_INTERFACE
60 BUS_INTROSPECTABLE_INTERFACE
63 static int bus_unit_append_id(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
74 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &id
))
80 static int bus_unit_append_description(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
89 d
= unit_description(u
);
91 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &d
))
97 static int bus_unit_append_load_state(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
106 state
= unit_load_state_to_string(u
->meta
.load_state
);
108 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &state
))
114 static int bus_unit_append_active_state(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
123 state
= unit_active_state_to_string(unit_active_state(u
));
125 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &state
))
131 static int bus_unit_append_can_reload(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
140 b
= unit_can_reload(u
);
142 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_BOOLEAN
, &b
))
148 static int bus_unit_append_can_start(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
157 b
= unit_can_start(u
);
159 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_BOOLEAN
, &b
))
165 static int bus_unit_append_job(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
175 if (!dbus_message_iter_open_container(i
, DBUS_TYPE_STRUCT
, NULL
, &sub
))
180 if (!(p
= job_dbus_path(u
->meta
.job
)))
183 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_UINT32
, &u
->meta
.job
->id
) ||
184 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_OBJECT_PATH
, &p
)) {
191 /* No job, so let's fill in some placeholder
192 * data. Since we need to fill in a valid path we
193 * simple point to ourselves. */
195 if (!(p
= unit_dbus_path(u
)))
198 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_UINT32
, &id
) ||
199 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_OBJECT_PATH
, &p
)) {
207 if (!dbus_message_iter_close_container(i
, &sub
))
213 static DBusHandlerResult
bus_unit_message_dispatch(Unit
*u
, DBusMessage
*message
) {
215 const BusProperty properties
[] = {
216 { "org.freedesktop.systemd1.Unit", "Id", bus_unit_append_id
, "s", u
},
217 { "org.freedesktop.systemd1.Unit", "Description", bus_unit_append_description
, "s", u
},
218 { "org.freedesktop.systemd1.Unit", "LoadState", bus_unit_append_load_state
, "s", u
},
219 { "org.freedesktop.systemd1.Unit", "ActiveState", bus_unit_append_active_state
, "s", u
},
220 { "org.freedesktop.systemd1.Unit", "FragmentPath", bus_property_append_string
, "s", u
->meta
.fragment_path
},
221 { "org.freedesktop.systemd1.Unit", "ActiveEnterTimestamp", bus_property_append_uint64
, "t", &u
->meta
.active_enter_timestamp
},
222 { "org.freedesktop.systemd1.Unit", "ActiveExitTimestamp", bus_property_append_uint64
, "t", &u
->meta
.active_exit_timestamp
},
223 { "org.freedesktop.systemd1.Unit", "CanReload", bus_unit_append_can_reload
, "b", u
},
224 { "org.freedesktop.systemd1.Unit", "CanStart", bus_unit_append_can_start
, "b", u
},
225 { "org.freedesktop.systemd1.Unit", "Job", bus_unit_append_job
, "(uo)", u
},
226 { NULL
, NULL
, NULL
, NULL
, NULL
}
229 DBusMessage
*reply
= NULL
;
230 Manager
*m
= u
->meta
.manager
;
232 JobType job_type
= _JOB_TYPE_INVALID
;
234 dbus_error_init(&error
);
236 if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Unit", "Start"))
237 job_type
= JOB_START
;
238 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Unit", "Stop"))
240 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Unit", "Reload"))
241 job_type
= JOB_RELOAD
;
242 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Unit", "Restart"))
243 job_type
= JOB_RESTART
;
245 return bus_default_message_handler(u
->meta
.manager
, message
, introspection
, properties
);
247 if (job_type
!= _JOB_TYPE_INVALID
) {
254 if (!dbus_message_get_args(
257 DBUS_TYPE_STRING
, &smode
,
259 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
261 if ((mode
= job_mode_from_string(smode
)) == _JOB_MODE_INVALID
)
262 return bus_send_error_reply(m
, message
, NULL
, -EINVAL
);
264 if ((r
= manager_add_job(m
, job_type
, u
, mode
, true, &j
)) < 0)
265 return bus_send_error_reply(m
, message
, NULL
, r
);
267 if (!(reply
= dbus_message_new_method_return(message
)))
270 if (!(path
= job_dbus_path(j
)))
273 if (!dbus_message_append_args(
275 DBUS_TYPE_OBJECT_PATH
, &path
,
281 if (!dbus_connection_send(m
->api_bus
, reply
, NULL
))
284 dbus_message_unref(reply
);
287 return DBUS_HANDLER_RESULT_HANDLED
;
291 dbus_message_unref(reply
);
293 dbus_error_free(&error
);
295 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
298 static DBusHandlerResult
bus_unit_message_handler(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
307 log_debug("Got D-Bus request: %s.%s() on %s",
308 dbus_message_get_interface(message
),
309 dbus_message_get_member(message
),
310 dbus_message_get_path(message
));
312 if ((r
= manager_get_unit_from_dbus_path(m
, dbus_message_get_path(message
), &u
)) < 0) {
315 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
318 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
320 return bus_send_error_reply(m
, message
, NULL
, r
);
323 return bus_unit_message_dispatch(u
, message
);
326 const DBusObjectPathVTable bus_unit_vtable
= {
327 .message_function
= bus_unit_message_handler
330 void bus_unit_send_change_signal(Unit
*u
) {
332 DBusMessage
*m
= NULL
;
335 assert(u
->meta
.in_dbus_queue
);
337 LIST_REMOVE(Meta
, dbus_queue
, u
->meta
.manager
->dbus_unit_queue
, &u
->meta
);
338 u
->meta
.in_dbus_queue
= false;
340 if (set_isempty(u
->meta
.manager
->subscribed
))
343 if (!(p
= unit_dbus_path(u
)))
346 if (u
->meta
.sent_dbus_new_signal
) {
347 /* Send a change signal */
349 if (!(m
= dbus_message_new_signal(p
, "org.freedesktop.systemd1.Unit", "Changed")))
353 /* Send a new signal */
355 if (!(m
= dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "UnitNew")))
359 if (!dbus_message_append_args(m
,
360 DBUS_TYPE_STRING
, &id
,
361 DBUS_TYPE_OBJECT_PATH
, &p
,
366 if (!dbus_connection_send(u
->meta
.manager
->api_bus
, m
, NULL
))
370 dbus_message_unref(m
);
372 u
->meta
.sent_dbus_new_signal
= true;
380 dbus_message_unref(m
);
382 log_error("Failed to allocate unit change/new signal.");
385 void bus_unit_send_removed_signal(Unit
*u
) {
387 DBusMessage
*m
= NULL
;
392 if (set_isempty(u
->meta
.manager
->subscribed
) || !u
->meta
.sent_dbus_new_signal
)
395 if (!(p
= unit_dbus_path(u
)))
398 if (!(m
= dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "UnitRemoved")))
402 if (!dbus_message_append_args(m
,
403 DBUS_TYPE_STRING
, &id
,
404 DBUS_TYPE_OBJECT_PATH
, &p
,
408 if (!dbus_connection_send(u
->meta
.manager
->api_bus
, m
, NULL
))
412 dbus_message_unref(m
);
420 dbus_message_unref(m
);
422 log_error("Failed to allocate unit remove signal.");