Coverage Report

Created: 2026-05-04 06:47

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