1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
8 static const char introspection[] =
9 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
11 " <interface name=\"org.freedesktop.systemd1.Unit\">"
12 " <method name=\"Start\">"
13 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>"
14 " <arg name=\"job\" type=\"o\" direction=\"out\"/>"
16 " <method name=\"Stop\">"
17 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>"
18 " <arg name=\"job\" type=\"o\" direction=\"out\"/>"
20 " <method name=\"Restart\">"
21 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>"
22 " <arg name=\"job\" type=\"o\" direction=\"out\"/>"
24 " <method name=\"Reload\">"
25 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>"
26 " <arg name=\"job\" type=\"o\" direction=\"out\"/>"
28 " <property name=\"Id\" type=\"s\" access=\"read\"/>"
29 " <property name=\"Description\" type=\"s\" access=\"read\"/>"
30 " <property name=\"LoadState\" type=\"s\" access=\"read\"/>"
31 " <property name=\"ActiveState\" type=\"s\" access=\"read\"/>"
32 " <property name=\"LoadPath\" type=\"s\" access=\"read\"/>"
33 " <property name=\"ActiveEnterTimestamp\" type=\"t\" access=\"read\"/>"
34 " <property name=\"ActiveExitTimestamp\" type=\"t\" access=\"read\"/>"
35 " <property name=\"CanReload\" type=\"b\" access=\"read\"/>"
36 " <property name=\"CanStart\" type=\"b\" access=\"read\"/>"
37 " <property name=\"Job\" type=\"(uo)\" access=\"read\"/>"
39 BUS_PROPERTIES_INTERFACE
40 BUS_INTROSPECTABLE_INTERFACE
43 static int bus_unit_append_id(Manager *m, DBusMessageIter *i, const char *property, void *data) {
54 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &id))
60 static int bus_unit_append_description(Manager *m, DBusMessageIter *i, const char *property, void *data) {
69 d = unit_description(u);
71 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
77 static int bus_unit_append_load_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
86 state = unit_load_state_to_string(u->meta.load_state);
88 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
94 static int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
103 state = unit_active_state_to_string(unit_active_state(u));
105 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
111 static int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
120 b = unit_can_reload(u);
122 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
128 static int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *property, void *data) {
137 b = unit_can_start(u);
139 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
145 static int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) {
155 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
160 if (!(p = job_dbus_path(u->meta.job)))
163 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) ||
164 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
171 /* No job, so let's fill in some placeholder
172 * data. Since we need to fill in a valid path we
173 * simple point to ourselves. */
175 if (!(p = unit_dbus_path(u)))
178 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) ||
179 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
187 if (!dbus_message_iter_close_container(i, &sub))
193 static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusMessage *message) {
195 const BusProperty properties[] = {
196 { "org.freedesktop.systemd1.Unit", "Id", bus_unit_append_id, "s", u },
197 { "org.freedesktop.systemd1.Unit", "Description", bus_unit_append_description, "s", u },
198 { "org.freedesktop.systemd1.Unit", "LoadState", bus_unit_append_load_state, "s", u },
199 { "org.freedesktop.systemd1.Unit", "ActiveState", bus_unit_append_active_state, "s", u },
200 { "org.freedesktop.systemd1.Unit", "LoadPath", bus_property_append_string, "s", u->meta.load_path },
201 { "org.freedesktop.systemd1.Unit", "ActiveEnterTimestamp", bus_property_append_uint64, "t", &u->meta.active_enter_timestamp },
202 { "org.freedesktop.systemd1.Unit", "ActiveExitTimestamp", bus_property_append_uint64, "t", &u->meta.active_exit_timestamp },
203 { "org.freedesktop.systemd1.Unit", "CanReload", bus_unit_append_can_reload, "b", u },
204 { "org.freedesktop.systemd1.Unit", "CanStart", bus_unit_append_can_start, "b", u },
205 { "org.freedesktop.systemd1.Unit", "Job", bus_unit_append_job, "(uo)", u },
206 { NULL, NULL, NULL, NULL, NULL }
209 DBusMessage *reply = NULL;
210 Manager *m = u->meta.manager;
212 JobType job_type = _JOB_TYPE_INVALID;
214 dbus_error_init(&error);
216 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start"))
217 job_type = JOB_START;
218 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop"))
220 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload"))
221 job_type = JOB_RELOAD;
222 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart"))
223 job_type = JOB_RESTART;
225 return bus_default_message_handler(u->meta.manager, message, introspection, properties);
227 if (job_type != _JOB_TYPE_INVALID) {
234 if (!dbus_message_get_args(
237 DBUS_TYPE_STRING, &smode,
239 return bus_send_error_reply(m, message, &error, -EINVAL);
241 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID)
242 return bus_send_error_reply(m, message, NULL, -EINVAL);
244 if ((r = manager_add_job(m, job_type, u, mode, true, &j)) < 0)
245 return bus_send_error_reply(m, message, NULL, r);
247 if (!(reply = dbus_message_new_method_return(message)))
250 if (!(path = job_dbus_path(j)))
253 if (!dbus_message_append_args(
255 DBUS_TYPE_OBJECT_PATH, &path,
261 if (!dbus_connection_send(m->bus, reply, NULL))
264 dbus_message_unref(reply);
267 return DBUS_HANDLER_RESULT_HANDLED;
271 dbus_message_unref(reply);
273 dbus_error_free(&error);
275 return DBUS_HANDLER_RESULT_NEED_MEMORY;
278 static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
287 log_debug("Got D-Bus request: %s.%s() on %s",
288 dbus_message_get_interface(message),
289 dbus_message_get_member(message),
290 dbus_message_get_path(message));
292 if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
295 return DBUS_HANDLER_RESULT_NEED_MEMORY;
298 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
300 return bus_send_error_reply(m, message, NULL, r);
303 return bus_unit_message_dispatch(u, message);
306 const DBusObjectPathVTable bus_unit_vtable = {
307 .message_function = bus_unit_message_handler