Coverage Report

Created: 2019-06-19 13:33

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