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 " <property name=\"Id\" type=\"s\" access=\"read\"/>"
48 " <property name=\"Description\" type=\"s\" access=\"read\"/>"
49 " <property name=\"LoadState\" type=\"s\" access=\"read\"/>"
50 " <property name=\"ActiveState\" type=\"s\" access=\"read\"/>"
51 " <property name=\"LoadPath\" type=\"s\" access=\"read\"/>"
52 " <property name=\"ActiveEnterTimestamp\" type=\"t\" access=\"read\"/>"
53 " <property name=\"ActiveExitTimestamp\" type=\"t\" access=\"read\"/>"
54 " <property name=\"CanReload\" type=\"b\" access=\"read\"/>"
55 " <property name=\"CanStart\" type=\"b\" access=\"read\"/>"
56 " <property name=\"Job\" type=\"(uo)\" access=\"read\"/>"
58 BUS_PROPERTIES_INTERFACE
59 BUS_INTROSPECTABLE_INTERFACE
62 static int bus_unit_append_id(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
73 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &id
))
79 static int bus_unit_append_description(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
88 d
= unit_description(u
);
90 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &d
))
96 static int bus_unit_append_load_state(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
105 state
= unit_load_state_to_string(u
->meta
.load_state
);
107 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &state
))
113 static int bus_unit_append_active_state(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
122 state
= unit_active_state_to_string(unit_active_state(u
));
124 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &state
))
130 static int bus_unit_append_can_reload(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
139 b
= unit_can_reload(u
);
141 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_BOOLEAN
, &b
))
147 static int bus_unit_append_can_start(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
156 b
= unit_can_start(u
);
158 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_BOOLEAN
, &b
))
164 static int bus_unit_append_job(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
174 if (!dbus_message_iter_open_container(i
, DBUS_TYPE_STRUCT
, NULL
, &sub
))
179 if (!(p
= job_dbus_path(u
->meta
.job
)))
182 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_UINT32
, &u
->meta
.job
->id
) ||
183 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_OBJECT_PATH
, &p
)) {
190 /* No job, so let's fill in some placeholder
191 * data. Since we need to fill in a valid path we
192 * simple point to ourselves. */
194 if (!(p
= unit_dbus_path(u
)))
197 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_UINT32
, &id
) ||
198 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_OBJECT_PATH
, &p
)) {
206 if (!dbus_message_iter_close_container(i
, &sub
))
212 static DBusHandlerResult
bus_unit_message_dispatch(Unit
*u
, DBusMessage
*message
) {
214 const BusProperty properties
[] = {
215 { "org.freedesktop.systemd1.Unit", "Id", bus_unit_append_id
, "s", u
},
216 { "org.freedesktop.systemd1.Unit", "Description", bus_unit_append_description
, "s", u
},
217 { "org.freedesktop.systemd1.Unit", "LoadState", bus_unit_append_load_state
, "s", u
},
218 { "org.freedesktop.systemd1.Unit", "ActiveState", bus_unit_append_active_state
, "s", u
},
219 { "org.freedesktop.systemd1.Unit", "LoadPath", bus_property_append_string
, "s", u
->meta
.load_path
},
220 { "org.freedesktop.systemd1.Unit", "ActiveEnterTimestamp", bus_property_append_uint64
, "t", &u
->meta
.active_enter_timestamp
},
221 { "org.freedesktop.systemd1.Unit", "ActiveExitTimestamp", bus_property_append_uint64
, "t", &u
->meta
.active_exit_timestamp
},
222 { "org.freedesktop.systemd1.Unit", "CanReload", bus_unit_append_can_reload
, "b", u
},
223 { "org.freedesktop.systemd1.Unit", "CanStart", bus_unit_append_can_start
, "b", u
},
224 { "org.freedesktop.systemd1.Unit", "Job", bus_unit_append_job
, "(uo)", u
},
225 { NULL
, NULL
, NULL
, NULL
, NULL
}
228 DBusMessage
*reply
= NULL
;
229 Manager
*m
= u
->meta
.manager
;
231 JobType job_type
= _JOB_TYPE_INVALID
;
233 dbus_error_init(&error
);
235 if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Unit", "Start"))
236 job_type
= JOB_START
;
237 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Unit", "Stop"))
239 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Unit", "Reload"))
240 job_type
= JOB_RELOAD
;
241 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Unit", "Restart"))
242 job_type
= JOB_RESTART
;
244 return bus_default_message_handler(u
->meta
.manager
, message
, introspection
, properties
);
246 if (job_type
!= _JOB_TYPE_INVALID
) {
253 if (!dbus_message_get_args(
256 DBUS_TYPE_STRING
, &smode
,
258 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
260 if ((mode
= job_mode_from_string(smode
)) == _JOB_MODE_INVALID
)
261 return bus_send_error_reply(m
, message
, NULL
, -EINVAL
);
263 if ((r
= manager_add_job(m
, job_type
, u
, mode
, true, &j
)) < 0)
264 return bus_send_error_reply(m
, message
, NULL
, r
);
266 if (!(reply
= dbus_message_new_method_return(message
)))
269 if (!(path
= job_dbus_path(j
)))
272 if (!dbus_message_append_args(
274 DBUS_TYPE_OBJECT_PATH
, &path
,
280 if (!dbus_connection_send(m
->bus
, reply
, NULL
))
283 dbus_message_unref(reply
);
286 return DBUS_HANDLER_RESULT_HANDLED
;
290 dbus_message_unref(reply
);
292 dbus_error_free(&error
);
294 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
297 static DBusHandlerResult
bus_unit_message_handler(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
306 log_debug("Got D-Bus request: %s.%s() on %s",
307 dbus_message_get_interface(message
),
308 dbus_message_get_member(message
),
309 dbus_message_get_path(message
));
311 if ((r
= manager_get_unit_from_dbus_path(m
, dbus_message_get_path(message
), &u
)) < 0) {
314 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
317 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
319 return bus_send_error_reply(m
, message
, NULL
, r
);
322 return bus_unit_message_dispatch(u
, message
);
325 const DBusObjectPathVTable bus_unit_vtable
= {
326 .message_function
= bus_unit_message_handler