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/>.
28 static const char introspection
[] =
29 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
31 " <interface name=\"org.freedesktop.systemd1.Job\">"
32 " <method name=\"Cancel\"/>"
33 " <signal name=\"Changed\"/>"
34 " <property name=\"Id\" type=\"u\" access=\"read\"/>"
35 " <property name=\"Unit\" type=\"(so)\" access=\"read\"/>"
36 " <property name=\"JobType\" type=\"s\" access=\"read\"/>"
37 " <property name=\"State\" type=\"s\" access=\"read\"/>"
39 BUS_PROPERTIES_INTERFACE
40 BUS_INTROSPECTABLE_INTERFACE
43 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_state
, job_state
, JobState
);
44 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_type
, job_type
, JobType
);
46 static int bus_job_append_unit(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
56 if (!dbus_message_iter_open_container(i
, DBUS_TYPE_STRUCT
, NULL
, &sub
))
59 if (!(p
= unit_dbus_path(j
->unit
)))
62 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &j
->unit
->meta
.id
) ||
63 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_OBJECT_PATH
, &p
)) {
70 if (!dbus_message_iter_close_container(i
, &sub
))
76 static DBusHandlerResult
bus_job_message_dispatch(Job
*j
, DBusMessage
*message
) {
77 const BusProperty properties
[] = {
78 { "org.freedesktop.systemd1.Job", "Id", bus_property_append_uint32
, "u", &j
->id
},
79 { "org.freedesktop.systemd1.Job", "State", bus_job_append_state
, "s", &j
->state
},
80 { "org.freedesktop.systemd1.Job", "JobType", bus_job_append_type
, "s", &j
->type
},
81 { "org.freedesktop.systemd1.Job", "Unit", bus_job_append_unit
, "(so)", j
},
82 { NULL
, NULL
, NULL
, NULL
, NULL
}
85 DBusMessage
*reply
= NULL
;
86 Manager
*m
= j
->manager
;
88 if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Job", "Cancel")) {
89 if (!(reply
= dbus_message_new_method_return(message
)))
95 return bus_default_message_handler(j
->manager
, message
, introspection
, properties
);
98 if (!dbus_connection_send(m
->api_bus
, reply
, NULL
))
101 dbus_message_unref(reply
);
104 return DBUS_HANDLER_RESULT_HANDLED
;
108 dbus_message_unref(reply
);
110 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
113 static DBusHandlerResult
bus_job_message_handler(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
122 log_debug("Got D-Bus request: %s.%s() on %s",
123 dbus_message_get_interface(message
),
124 dbus_message_get_member(message
),
125 dbus_message_get_path(message
));
127 if ((r
= manager_get_job_from_dbus_path(m
, dbus_message_get_path(message
), &j
)) < 0) {
130 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
133 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
135 return bus_send_error_reply(m
, message
, NULL
, r
);
138 return bus_job_message_dispatch(j
, message
);
141 const DBusObjectPathVTable bus_job_vtable
= {
142 .message_function
= bus_job_message_handler
145 void bus_job_send_change_signal(Job
*j
) {
147 DBusMessage
*m
= NULL
;
150 assert(j
->in_dbus_queue
);
152 LIST_REMOVE(Job
, dbus_queue
, j
->manager
->dbus_job_queue
, j
);
153 j
->in_dbus_queue
= false;
155 if (set_isempty(j
->manager
->subscribed
))
158 if (!(p
= job_dbus_path(j
)))
161 if (j
->sent_dbus_new_signal
) {
162 /* Send a change signal */
164 if (!(m
= dbus_message_new_signal(p
, "org.freedesktop.systemd1.Job", "Changed")))
167 /* Send a new signal */
169 if (!(m
= dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "JobNew")))
172 if (!dbus_message_append_args(m
,
173 DBUS_TYPE_UINT32
, &j
->id
,
174 DBUS_TYPE_OBJECT_PATH
, &p
,
179 if (!dbus_connection_send(j
->manager
->api_bus
, m
, NULL
))
183 dbus_message_unref(m
);
185 j
->sent_dbus_new_signal
= true;
193 dbus_message_unref(m
);
195 log_error("Failed to allocate job change signal.");
198 void bus_job_send_removed_signal(Job
*j
) {
200 DBusMessage
*m
= NULL
;
204 if (set_isempty(j
->manager
->subscribed
) || !j
->sent_dbus_new_signal
)
207 if (!(p
= job_dbus_path(j
)))
210 if (!(m
= dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "JobRemoved")))
213 if (!dbus_message_append_args(m
,
214 DBUS_TYPE_UINT32
, &j
->id
,
215 DBUS_TYPE_OBJECT_PATH
, &p
,
219 if (!dbus_connection_send(j
->manager
->api_bus
, m
, NULL
))
223 dbus_message_unref(m
);
231 dbus_message_unref(m
);
233 log_error("Failed to allocate job remove signal.");