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