]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
a7334b09 | 2 | |
718db961 | 3 | #include "sd-bus.h" |
07630cea | 4 | |
b5efdb8a LP |
5 | #include "alloc-util.h" |
6 | #include "dbus-job.h" | |
17407bc2 | 7 | #include "dbus-unit.h" |
07630cea | 8 | #include "dbus.h" |
718db961 | 9 | #include "job.h" |
07630cea LP |
10 | #include "log.h" |
11 | #include "selinux-access.h" | |
12 | #include "string-util.h" | |
718db961 LP |
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, | |
ebcf1f97 LP |
23 | void *userdata, |
24 | sd_bus_error *error) { | |
ea430986 | 25 | |
68eda4bd | 26 | _cleanup_free_ char *p = NULL; |
718db961 | 27 | Job *j = userdata; |
86fbf370 | 28 | |
718db961 LP |
29 | assert(bus); |
30 | assert(reply); | |
86fbf370 LP |
31 | assert(j); |
32 | ||
cad45ba1 LP |
33 | p = unit_dbus_path(j->unit); |
34 | if (!p) | |
86fbf370 LP |
35 | return -ENOMEM; |
36 | ||
718db961 | 37 | return sd_bus_message_append(reply, "(so)", j->unit->id, p); |
86fbf370 LP |
38 | } |
39 | ||
19070062 | 40 | int bus_job_method_cancel(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
718db961 | 41 | Job *j = userdata; |
ebcf1f97 | 42 | int r; |
ea430986 | 43 | |
ea430986 | 44 | assert(message); |
718db961 | 45 | assert(j); |
86fbf370 | 46 | |
8a188de9 | 47 | r = mac_selinux_unit_access_check(j->unit, message, "stop", error); |
ebcf1f97 LP |
48 | if (r < 0) |
49 | return r; | |
50 | ||
1d22e906 | 51 | /* Access is granted to the job owner */ |
1a465207 | 52 | if (!sd_bus_track_contains(j->bus_track, sd_bus_message_get_sender(message))) { |
1d22e906 | 53 | |
d35f51ea | 54 | /* And for everybody else consult polkit */ |
1d22e906 LP |
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 | ||
833f92ad | 62 | job_finish_and_invalidate(j, JOB_CANCELED, true, false); |
2cccbca4 | 63 | |
df2d202e | 64 | return sd_bus_reply_method_return(message, NULL); |
ea430986 LP |
65 | } |
66 | ||
15ea79f8 LP |
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 | ||
718db961 LP |
117 | const sd_bus_vtable bus_job_vtable[] = { |
118 | SD_BUS_VTABLE_START(0), | |
283868e1 | 119 | SD_BUS_METHOD("Cancel", NULL, NULL, bus_job_method_cancel, SD_BUS_VTABLE_UNPRIVILEGED), |
15ea79f8 LP |
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), | |
556089dc LP |
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), | |
718db961 LP |
125 | SD_BUS_PROPERTY("State", "s", property_get_state, offsetof(Job, state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
126 | SD_BUS_VTABLE_END | |
ea430986 | 127 | }; |
c1e1601e | 128 | |
8f8f05a9 | 129 | static int send_new_signal(sd_bus *bus, void *userdata) { |
4afd3348 | 130 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
1508e858 | 131 | _cleanup_free_ char *p = NULL; |
8f8f05a9 | 132 | Job *j = userdata; |
718db961 LP |
133 | int r; |
134 | ||
135 | assert(bus); | |
136 | assert(j); | |
c1e1601e | 137 | |
97e6a119 MS |
138 | p = job_dbus_path(j); |
139 | if (!p) | |
718db961 | 140 | return -ENOMEM; |
c1e1601e | 141 | |
718db961 LP |
142 | r = sd_bus_message_new_signal( |
143 | bus, | |
151b9b96 | 144 | &m, |
718db961 LP |
145 | "/org/freedesktop/systemd1", |
146 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 147 | "JobNew"); |
718db961 LP |
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 | ||
8f8f05a9 | 155 | return sd_bus_send(bus, m, NULL); |
c1e1601e LP |
156 | } |
157 | ||
8f8f05a9 | 158 | static int send_changed_signal(sd_bus *bus, void *userdata) { |
1508e858 | 159 | _cleanup_free_ char *p = NULL; |
8f8f05a9 | 160 | Job *j = userdata; |
718db961 LP |
161 | |
162 | assert(bus); | |
163 | assert(j); | |
c1e1601e | 164 | |
97e6a119 MS |
165 | p = job_dbus_path(j); |
166 | if (!p) | |
718db961 | 167 | return -ENOMEM; |
c1e1601e | 168 | |
718db961 | 169 | return sd_bus_emit_properties_changed(bus, p, "org.freedesktop.systemd1.Job", "State", NULL); |
97e6a119 MS |
170 | } |
171 | ||
172 | void bus_job_send_change_signal(Job *j) { | |
718db961 LP |
173 | int r; |
174 | ||
97e6a119 MS |
175 | assert(j); |
176 | ||
17407bc2 LP |
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 | ||
97e6a119 | 180 | if (j->in_dbus_queue) { |
71fda00f | 181 | LIST_REMOVE(dbus_queue, j->manager->dbus_job_queue, j); |
97e6a119 MS |
182 | j->in_dbus_queue = false; |
183 | } | |
184 | ||
1a465207 | 185 | r = bus_foreach_bus(j->manager, j->bus_track, j->sent_dbus_new_signal ? send_changed_signal : send_new_signal, j); |
718db961 | 186 | if (r < 0) |
da927ba9 | 187 | log_debug_errno(r, "Failed to send job change signal for %u: %m", j->id); |
97e6a119 MS |
188 | |
189 | j->sent_dbus_new_signal = true; | |
718db961 LP |
190 | } |
191 | ||
13142276 LP |
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 | ||
8f8f05a9 | 207 | static int send_removed_signal(sd_bus *bus, void *userdata) { |
4afd3348 | 208 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
718db961 | 209 | _cleanup_free_ char *p = NULL; |
8f8f05a9 | 210 | Job *j = userdata; |
718db961 LP |
211 | int r; |
212 | ||
213 | assert(bus); | |
214 | assert(j); | |
215 | ||
216 | p = job_dbus_path(j); | |
217 | if (!p) | |
218 | return -ENOMEM; | |
c1e1601e | 219 | |
718db961 LP |
220 | r = sd_bus_message_new_signal( |
221 | bus, | |
151b9b96 | 222 | &m, |
718db961 LP |
223 | "/org/freedesktop/systemd1", |
224 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 225 | "JobRemoved"); |
718db961 LP |
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 | ||
8f8f05a9 | 233 | return sd_bus_send(bus, m, NULL); |
97e6a119 | 234 | } |
c1e1601e | 235 | |
97e6a119 | 236 | void bus_job_send_removed_signal(Job *j) { |
718db961 | 237 | int r; |
c1e1601e | 238 | |
718db961 | 239 | assert(j); |
97e6a119 MS |
240 | |
241 | if (!j->sent_dbus_new_signal) | |
242 | bus_job_send_change_signal(j); | |
243 | ||
17407bc2 LP |
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 | ||
1a465207 | 247 | r = bus_foreach_bus(j->manager, j->bus_track, send_removed_signal, j); |
718db961 | 248 | if (r < 0) |
da927ba9 | 249 | log_debug_errno(r, "Failed to send job remove signal for %u: %m", j->id); |
c1e1601e | 250 | } |
c5a97ed1 LP |
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) { | |
c5a97ed1 LP |
266 | |
267 | assert(j); | |
268 | ||
269 | if (j->bus_track) | |
270 | return 0; | |
271 | ||
76d8ca22 | 272 | return sd_bus_track_new(j->unit->manager->api_bus, &j->bus_track, bus_job_track_handler, j); |
c5a97ed1 LP |
273 | } |
274 | ||
275 | int bus_job_coldplug_bus_track(Job *j) { | |
276 | int r = 0; | |
76d8ca22 | 277 | _cleanup_strv_free_ char **deserialized_clients = NULL; |
c5a97ed1 LP |
278 | |
279 | assert(j); | |
280 | ||
ae2a15bc | 281 | deserialized_clients = TAKE_PTR(j->deserialized_clients); |
76d8ca22 ZJS |
282 | |
283 | if (strv_isempty(deserialized_clients)) | |
284 | return 0; | |
c5a97ed1 LP |
285 | |
286 | if (!j->manager->api_bus) | |
76d8ca22 | 287 | return 0; |
c5a97ed1 LP |
288 | |
289 | r = bus_job_allocate_bus_track(j); | |
290 | if (r < 0) | |
76d8ca22 | 291 | return r; |
c5a97ed1 | 292 | |
76d8ca22 | 293 | return bus_track_add_name_many(j->bus_track, deserialized_clients); |
c5a97ed1 LP |
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 | } |