]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/dbus-job.c
check for _POSIX_C_SOURCE instead of __USE_POSIX*
[thirdparty/systemd.git] / src / core / dbus-job.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "sd-bus.h"
21
22 #include "alloc-util.h"
23 #include "dbus-job.h"
24 #include "dbus.h"
25 #include "job.h"
26 #include "log.h"
27 #include "selinux-access.h"
28 #include "string-util.h"
29
30 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type, job_type, JobType);
31 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_state, job_state, JobState);
32
33 static int property_get_unit(
34 sd_bus *bus,
35 const char *path,
36 const char *interface,
37 const char *property,
38 sd_bus_message *reply,
39 void *userdata,
40 sd_bus_error *error) {
41
42 _cleanup_free_ char *p = NULL;
43 Job *j = userdata;
44
45 assert(bus);
46 assert(reply);
47 assert(j);
48
49 p = unit_dbus_path(j->unit);
50 if (!p)
51 return -ENOMEM;
52
53 return sd_bus_message_append(reply, "(so)", j->unit->id, p);
54 }
55
56 int bus_job_method_cancel(sd_bus_message *message, void *userdata, sd_bus_error *error) {
57 Job *j = userdata;
58 int r;
59
60 assert(message);
61 assert(j);
62
63 r = mac_selinux_unit_access_check(j->unit, message, "stop", error);
64 if (r < 0)
65 return r;
66
67 /* Access is granted to the job owner */
68 if (!sd_bus_track_contains(j->bus_track, sd_bus_message_get_sender(message))) {
69
70 /* And for everybody else consult PolicyKit */
71 r = bus_verify_manage_units_async(j->unit->manager, message, error);
72 if (r < 0)
73 return r;
74 if (r == 0)
75 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
76 }
77
78 job_finish_and_invalidate(j, JOB_CANCELED, true, false);
79
80 return sd_bus_reply_method_return(message, NULL);
81 }
82
83 int bus_job_method_get_waiting_jobs(sd_bus_message *message, void *userdata, sd_bus_error *error) {
84 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
85 _cleanup_free_ Job **list = NULL;
86 Job *j = userdata;
87 int r, i, n;
88
89 if (strstr(sd_bus_message_get_member(message), "After"))
90 n = job_get_after(j, &list);
91 else
92 n = job_get_before(j, &list);
93 if (n < 0)
94 return n;
95
96 r = sd_bus_message_new_method_return(message, &reply);
97 if (r < 0)
98 return r;
99
100 r = sd_bus_message_open_container(reply, 'a', "(usssoo)");
101 if (r < 0)
102 return r;
103
104 for (i = 0; i < n; i ++) {
105 _cleanup_free_ char *unit_path = NULL, *job_path = NULL;
106
107 job_path = job_dbus_path(list[i]);
108 if (!job_path)
109 return -ENOMEM;
110
111 unit_path = unit_dbus_path(list[i]->unit);
112 if (!unit_path)
113 return -ENOMEM;
114
115 r = sd_bus_message_append(reply, "(usssoo)",
116 list[i]->id,
117 list[i]->unit->id,
118 job_type_to_string(list[i]->type),
119 job_state_to_string(list[i]->state),
120 job_path,
121 unit_path);
122 if (r < 0)
123 return r;
124 }
125
126 r = sd_bus_message_close_container(reply);
127 if (r < 0)
128 return r;
129
130 return sd_bus_send(NULL, reply, NULL);
131 }
132
133 const sd_bus_vtable bus_job_vtable[] = {
134 SD_BUS_VTABLE_START(0),
135 SD_BUS_METHOD("Cancel", NULL, NULL, bus_job_method_cancel, SD_BUS_VTABLE_UNPRIVILEGED),
136 SD_BUS_METHOD("GetAfter", NULL, "a(usssoo)", bus_job_method_get_waiting_jobs, SD_BUS_VTABLE_UNPRIVILEGED),
137 SD_BUS_METHOD("GetBefore", NULL, "a(usssoo)", bus_job_method_get_waiting_jobs, SD_BUS_VTABLE_UNPRIVILEGED),
138 SD_BUS_PROPERTY("Id", "u", NULL, offsetof(Job, id), SD_BUS_VTABLE_PROPERTY_CONST),
139 SD_BUS_PROPERTY("Unit", "(so)", property_get_unit, 0, SD_BUS_VTABLE_PROPERTY_CONST),
140 SD_BUS_PROPERTY("JobType", "s", property_get_type, offsetof(Job, type), SD_BUS_VTABLE_PROPERTY_CONST),
141 SD_BUS_PROPERTY("State", "s", property_get_state, offsetof(Job, state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
142 SD_BUS_VTABLE_END
143 };
144
145 static int send_new_signal(sd_bus *bus, void *userdata) {
146 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
147 _cleanup_free_ char *p = NULL;
148 Job *j = userdata;
149 int r;
150
151 assert(bus);
152 assert(j);
153
154 p = job_dbus_path(j);
155 if (!p)
156 return -ENOMEM;
157
158 r = sd_bus_message_new_signal(
159 bus,
160 &m,
161 "/org/freedesktop/systemd1",
162 "org.freedesktop.systemd1.Manager",
163 "JobNew");
164 if (r < 0)
165 return r;
166
167 r = sd_bus_message_append(m, "uos", j->id, p, j->unit->id);
168 if (r < 0)
169 return r;
170
171 return sd_bus_send(bus, m, NULL);
172 }
173
174 static int send_changed_signal(sd_bus *bus, void *userdata) {
175 _cleanup_free_ char *p = NULL;
176 Job *j = userdata;
177
178 assert(bus);
179 assert(j);
180
181 p = job_dbus_path(j);
182 if (!p)
183 return -ENOMEM;
184
185 return sd_bus_emit_properties_changed(bus, p, "org.freedesktop.systemd1.Job", "State", NULL);
186 }
187
188 void bus_job_send_change_signal(Job *j) {
189 int r;
190
191 assert(j);
192
193 if (j->in_dbus_queue) {
194 LIST_REMOVE(dbus_queue, j->manager->dbus_job_queue, j);
195 j->in_dbus_queue = false;
196 }
197
198 r = bus_foreach_bus(j->manager, j->bus_track, j->sent_dbus_new_signal ? send_changed_signal : send_new_signal, j);
199 if (r < 0)
200 log_debug_errno(r, "Failed to send job change signal for %u: %m", j->id);
201
202 j->sent_dbus_new_signal = true;
203 }
204
205 static int send_removed_signal(sd_bus *bus, void *userdata) {
206 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
207 _cleanup_free_ char *p = NULL;
208 Job *j = userdata;
209 int r;
210
211 assert(bus);
212 assert(j);
213
214 p = job_dbus_path(j);
215 if (!p)
216 return -ENOMEM;
217
218 r = sd_bus_message_new_signal(
219 bus,
220 &m,
221 "/org/freedesktop/systemd1",
222 "org.freedesktop.systemd1.Manager",
223 "JobRemoved");
224 if (r < 0)
225 return r;
226
227 r = sd_bus_message_append(m, "uoss", j->id, p, j->unit->id, job_result_to_string(j->result));
228 if (r < 0)
229 return r;
230
231 return sd_bus_send(bus, m, NULL);
232 }
233
234 void bus_job_send_removed_signal(Job *j) {
235 int r;
236
237 assert(j);
238
239 if (!j->sent_dbus_new_signal)
240 bus_job_send_change_signal(j);
241
242 r = bus_foreach_bus(j->manager, j->bus_track, send_removed_signal, j);
243 if (r < 0)
244 log_debug_errno(r, "Failed to send job remove signal for %u: %m", j->id);
245 }
246
247 static int bus_job_track_handler(sd_bus_track *t, void *userdata) {
248 Job *j = userdata;
249
250 assert(t);
251 assert(j);
252
253 j->bus_track = sd_bus_track_unref(j->bus_track); /* make sure we aren't called again */
254
255 /* Last client dropped off the bus, maybe we should GC this now? */
256 job_add_to_gc_queue(j);
257 return 0;
258 }
259
260 static int bus_job_allocate_bus_track(Job *j) {
261
262 assert(j);
263
264 if (j->bus_track)
265 return 0;
266
267 return sd_bus_track_new(j->unit->manager->api_bus, &j->bus_track, bus_job_track_handler, j);
268 }
269
270 int bus_job_coldplug_bus_track(Job *j) {
271 int r = 0;
272 _cleanup_strv_free_ char **deserialized_clients = NULL;
273
274 assert(j);
275
276 deserialized_clients = j->deserialized_clients;
277 j->deserialized_clients = NULL;
278
279 if (strv_isempty(deserialized_clients))
280 return 0;
281
282 if (!j->manager->api_bus)
283 return 0;
284
285 r = bus_job_allocate_bus_track(j);
286 if (r < 0)
287 return r;
288
289 return bus_track_add_name_many(j->bus_track, deserialized_clients);
290 }
291
292 int bus_job_track_sender(Job *j, sd_bus_message *m) {
293 int r;
294
295 assert(j);
296 assert(m);
297
298 if (sd_bus_message_get_bus(m) != j->unit->manager->api_bus) {
299 j->ref_by_private_bus = true;
300 return 0;
301 }
302
303 r = bus_job_allocate_bus_track(j);
304 if (r < 0)
305 return r;
306
307 return sd_bus_track_add_sender(j->bus_track, m);
308 }