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