]>
Commit | Line | Data |
---|---|---|
ea430986 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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/>. | |
20 | ***/ | |
21 | ||
86fbf370 LP |
22 | #include <errno.h> |
23 | ||
ea430986 LP |
24 | #include "dbus.h" |
25 | #include "log.h" | |
26 | ||
27 | static const char introspection[] = | |
28 | DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE | |
29 | "<node>" | |
30 | " <interface name=\"org.freedesktop.systemd1.Job\">" | |
b548631a | 31 | " <method name=\"Cancel\"/>" |
c1e1601e | 32 | " <signal name=\"Changed\"/>" |
86fbf370 LP |
33 | " <property name=\"Id\" type=\"u\" access=\"read\"/>" |
34 | " <property name=\"Unit\" type=\"(so)\" access=\"read\"/>" | |
b548631a | 35 | " <property name=\"JobType\" type=\"s\" access=\"read\"/>" |
86fbf370 | 36 | " <property name=\"State\" type=\"s\" access=\"read\"/>" |
ea430986 LP |
37 | " </interface>" |
38 | BUS_PROPERTIES_INTERFACE | |
39 | BUS_INTROSPECTABLE_INTERFACE | |
40 | "</node>"; | |
41 | ||
6f4706b7 LP |
42 | DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_state, job_state, JobState); |
43 | DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_type, job_type, JobType); | |
86fbf370 LP |
44 | |
45 | static int bus_job_append_unit(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
46 | Job *j = data; | |
47 | DBusMessageIter sub; | |
48 | char *p; | |
49 | const char *id; | |
50 | ||
51 | assert(m); | |
52 | assert(i); | |
53 | assert(property); | |
54 | assert(j); | |
55 | ||
56 | if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub)) | |
57 | return -ENOMEM; | |
58 | ||
59 | if (!(p = unit_dbus_path(j->unit))) | |
60 | return -ENOMEM; | |
61 | ||
62 | id = unit_id(j->unit); | |
63 | ||
64 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) || | |
65 | !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) { | |
66 | free(p); | |
67 | return -ENOMEM; | |
68 | } | |
69 | ||
70 | free(p); | |
71 | ||
72 | if (!dbus_message_iter_close_container(i, &sub)) | |
73 | return -ENOMEM; | |
74 | ||
75 | return 0; | |
76 | } | |
77 | ||
78 | static DBusHandlerResult bus_job_message_dispatch(Job *j, DBusMessage *message) { | |
86fbf370 | 79 | const BusProperty properties[] = { |
6f4706b7 LP |
80 | { "org.freedesktop.systemd1.Job", "Id", bus_property_append_uint32, "u", &j->id }, |
81 | { "org.freedesktop.systemd1.Job", "State", bus_job_append_state, "s", &j->state }, | |
82 | { "org.freedesktop.systemd1.Job", "JobType", bus_job_append_type, "s", &j->type }, | |
83 | { "org.freedesktop.systemd1.Job", "Unit", bus_job_append_unit, "(so)", j }, | |
86fbf370 LP |
84 | { NULL, NULL, NULL, NULL, NULL } |
85 | }; | |
86 | ||
b548631a LP |
87 | DBusMessage *reply = NULL; |
88 | Manager *m = j->manager; | |
89 | ||
90 | if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Job", "Cancel")) { | |
91 | if (!(reply = dbus_message_new_method_return(message))) | |
92 | goto oom; | |
93 | ||
94 | job_free(j); | |
95 | ||
96 | } else | |
97 | return bus_default_message_handler(j->manager, message, introspection, properties); | |
98 | ||
99 | if (reply) { | |
f278026d | 100 | if (!dbus_connection_send(m->api_bus, reply, NULL)) |
b548631a LP |
101 | goto oom; |
102 | ||
103 | dbus_message_unref(reply); | |
104 | } | |
105 | ||
106 | return DBUS_HANDLER_RESULT_HANDLED; | |
107 | ||
108 | oom: | |
109 | if (reply) | |
110 | dbus_message_unref(reply); | |
111 | ||
112 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
86fbf370 LP |
113 | } |
114 | ||
47be870b | 115 | static DBusHandlerResult bus_job_message_handler(DBusConnection *connection, DBusMessage *message, void *data) { |
ea430986 | 116 | Manager *m = data; |
86fbf370 LP |
117 | Job *j; |
118 | int r; | |
ea430986 LP |
119 | |
120 | assert(connection); | |
121 | assert(message); | |
122 | assert(m); | |
123 | ||
124 | log_debug("Got D-Bus request: %s.%s() on %s", | |
125 | dbus_message_get_interface(message), | |
126 | dbus_message_get_member(message), | |
127 | dbus_message_get_path(message)); | |
128 | ||
86fbf370 LP |
129 | if ((r = manager_get_job_from_dbus_path(m, dbus_message_get_path(message), &j)) < 0) { |
130 | ||
131 | if (r == -ENOMEM) | |
132 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
133 | ||
134 | if (r == -ENOENT) | |
135 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | |
136 | ||
137 | return bus_send_error_reply(m, message, NULL, r); | |
138 | } | |
139 | ||
140 | return bus_job_message_dispatch(j, message); | |
ea430986 LP |
141 | } |
142 | ||
143 | const DBusObjectPathVTable bus_job_vtable = { | |
144 | .message_function = bus_job_message_handler | |
145 | }; | |
c1e1601e LP |
146 | |
147 | void bus_job_send_change_signal(Job *j) { | |
148 | char *p = NULL; | |
149 | DBusMessage *m = NULL; | |
150 | ||
151 | assert(j); | |
152 | assert(j->in_dbus_queue); | |
153 | ||
154 | LIST_REMOVE(Job, dbus_queue, j->manager->dbus_job_queue, j); | |
155 | j->in_dbus_queue = false; | |
156 | ||
157 | if (set_isempty(j->manager->subscribed)) | |
158 | return; | |
159 | ||
160 | if (!(p = job_dbus_path(j))) | |
161 | goto oom; | |
162 | ||
163 | if (j->sent_dbus_new_signal) { | |
164 | /* Send a change signal */ | |
165 | ||
166 | if (!(m = dbus_message_new_signal(p, "org.freedesktop.systemd1.Job", "Changed"))) | |
167 | goto oom; | |
168 | } else { | |
169 | /* Send a new signal */ | |
170 | ||
171 | if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "JobNew"))) | |
172 | goto oom; | |
173 | ||
174 | if (!dbus_message_append_args(m, | |
175 | DBUS_TYPE_UINT32, &j->id, | |
176 | DBUS_TYPE_OBJECT_PATH, &p, | |
177 | DBUS_TYPE_INVALID)) | |
178 | goto oom; | |
179 | } | |
180 | ||
f278026d | 181 | if (!dbus_connection_send(j->manager->api_bus, m, NULL)) |
c1e1601e LP |
182 | goto oom; |
183 | ||
184 | free(p); | |
185 | dbus_message_unref(m); | |
186 | ||
187 | j->sent_dbus_new_signal = true; | |
188 | ||
189 | return; | |
190 | ||
191 | oom: | |
192 | free(p); | |
193 | ||
194 | if (m) | |
195 | dbus_message_unref(m); | |
196 | ||
197 | log_error("Failed to allocate job change signal."); | |
198 | } | |
199 | ||
200 | void bus_job_send_removed_signal(Job *j) { | |
201 | char *p = NULL; | |
202 | DBusMessage *m = NULL; | |
203 | ||
204 | assert(j); | |
205 | ||
206 | if (set_isempty(j->manager->subscribed) || !j->sent_dbus_new_signal) | |
207 | return; | |
208 | ||
209 | if (!(p = job_dbus_path(j))) | |
210 | goto oom; | |
211 | ||
212 | if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "JobRemoved"))) | |
213 | goto oom; | |
214 | ||
215 | if (!dbus_message_append_args(m, | |
216 | DBUS_TYPE_UINT32, &j->id, | |
217 | DBUS_TYPE_OBJECT_PATH, &p, | |
218 | DBUS_TYPE_INVALID)) | |
219 | goto oom; | |
220 | ||
f278026d | 221 | if (!dbus_connection_send(j->manager->api_bus, m, NULL)) |
c1e1601e LP |
222 | goto oom; |
223 | ||
224 | free(p); | |
225 | dbus_message_unref(m); | |
226 | ||
227 | return; | |
228 | ||
229 | oom: | |
230 | free(p); | |
231 | ||
232 | if (m) | |
233 | dbus_message_unref(m); | |
234 | ||
235 | log_error("Failed to allocate job remove signal."); | |
236 | } |