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