]>
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" | |
07630cea | 7 | #include "dbus.h" |
718db961 | 8 | #include "job.h" |
07630cea LP |
9 | #include "log.h" |
10 | #include "selinux-access.h" | |
11 | #include "string-util.h" | |
718db961 LP |
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, | |
ebcf1f97 LP |
22 | void *userdata, |
23 | sd_bus_error *error) { | |
ea430986 | 24 | |
68eda4bd | 25 | _cleanup_free_ char *p = NULL; |
718db961 | 26 | Job *j = userdata; |
86fbf370 | 27 | |
718db961 LP |
28 | assert(bus); |
29 | assert(reply); | |
86fbf370 LP |
30 | assert(j); |
31 | ||
cad45ba1 LP |
32 | p = unit_dbus_path(j->unit); |
33 | if (!p) | |
86fbf370 LP |
34 | return -ENOMEM; |
35 | ||
718db961 | 36 | return sd_bus_message_append(reply, "(so)", j->unit->id, p); |
86fbf370 LP |
37 | } |
38 | ||
19070062 | 39 | int bus_job_method_cancel(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
718db961 | 40 | Job *j = userdata; |
ebcf1f97 | 41 | int r; |
ea430986 | 42 | |
ea430986 | 43 | assert(message); |
718db961 | 44 | assert(j); |
86fbf370 | 45 | |
8a188de9 | 46 | r = mac_selinux_unit_access_check(j->unit, message, "stop", error); |
ebcf1f97 LP |
47 | if (r < 0) |
48 | return r; | |
49 | ||
1d22e906 | 50 | /* Access is granted to the job owner */ |
1a465207 | 51 | if (!sd_bus_track_contains(j->bus_track, sd_bus_message_get_sender(message))) { |
1d22e906 | 52 | |
d35f51ea | 53 | /* And for everybody else consult polkit */ |
1d22e906 LP |
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 | ||
833f92ad | 61 | job_finish_and_invalidate(j, JOB_CANCELED, true, false); |
2cccbca4 | 62 | |
df2d202e | 63 | return sd_bus_reply_method_return(message, NULL); |
ea430986 LP |
64 | } |
65 | ||
15ea79f8 LP |
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 | ||
718db961 LP |
116 | const sd_bus_vtable bus_job_vtable[] = { |
117 | SD_BUS_VTABLE_START(0), | |
283868e1 | 118 | SD_BUS_METHOD("Cancel", NULL, NULL, bus_job_method_cancel, SD_BUS_VTABLE_UNPRIVILEGED), |
15ea79f8 LP |
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), | |
556089dc LP |
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), | |
718db961 LP |
124 | SD_BUS_PROPERTY("State", "s", property_get_state, offsetof(Job, state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
125 | SD_BUS_VTABLE_END | |
ea430986 | 126 | }; |
c1e1601e | 127 | |
8f8f05a9 | 128 | static int send_new_signal(sd_bus *bus, void *userdata) { |
4afd3348 | 129 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
1508e858 | 130 | _cleanup_free_ char *p = NULL; |
8f8f05a9 | 131 | Job *j = userdata; |
718db961 LP |
132 | int r; |
133 | ||
134 | assert(bus); | |
135 | assert(j); | |
c1e1601e | 136 | |
97e6a119 MS |
137 | p = job_dbus_path(j); |
138 | if (!p) | |
718db961 | 139 | return -ENOMEM; |
c1e1601e | 140 | |
718db961 LP |
141 | r = sd_bus_message_new_signal( |
142 | bus, | |
151b9b96 | 143 | &m, |
718db961 LP |
144 | "/org/freedesktop/systemd1", |
145 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 146 | "JobNew"); |
718db961 LP |
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 | ||
8f8f05a9 | 154 | return sd_bus_send(bus, m, NULL); |
c1e1601e LP |
155 | } |
156 | ||
8f8f05a9 | 157 | static int send_changed_signal(sd_bus *bus, void *userdata) { |
1508e858 | 158 | _cleanup_free_ char *p = NULL; |
8f8f05a9 | 159 | Job *j = userdata; |
718db961 LP |
160 | |
161 | assert(bus); | |
162 | assert(j); | |
c1e1601e | 163 | |
97e6a119 MS |
164 | p = job_dbus_path(j); |
165 | if (!p) | |
718db961 | 166 | return -ENOMEM; |
c1e1601e | 167 | |
718db961 | 168 | return sd_bus_emit_properties_changed(bus, p, "org.freedesktop.systemd1.Job", "State", NULL); |
97e6a119 MS |
169 | } |
170 | ||
171 | void bus_job_send_change_signal(Job *j) { | |
718db961 LP |
172 | int r; |
173 | ||
97e6a119 MS |
174 | assert(j); |
175 | ||
176 | if (j->in_dbus_queue) { | |
71fda00f | 177 | LIST_REMOVE(dbus_queue, j->manager->dbus_job_queue, j); |
97e6a119 MS |
178 | j->in_dbus_queue = false; |
179 | } | |
180 | ||
1a465207 | 181 | r = bus_foreach_bus(j->manager, j->bus_track, j->sent_dbus_new_signal ? send_changed_signal : send_new_signal, j); |
718db961 | 182 | if (r < 0) |
da927ba9 | 183 | log_debug_errno(r, "Failed to send job change signal for %u: %m", j->id); |
97e6a119 MS |
184 | |
185 | j->sent_dbus_new_signal = true; | |
718db961 LP |
186 | } |
187 | ||
8f8f05a9 | 188 | static int send_removed_signal(sd_bus *bus, void *userdata) { |
4afd3348 | 189 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
718db961 | 190 | _cleanup_free_ char *p = NULL; |
8f8f05a9 | 191 | Job *j = userdata; |
718db961 LP |
192 | int r; |
193 | ||
194 | assert(bus); | |
195 | assert(j); | |
196 | ||
197 | p = job_dbus_path(j); | |
198 | if (!p) | |
199 | return -ENOMEM; | |
c1e1601e | 200 | |
718db961 LP |
201 | r = sd_bus_message_new_signal( |
202 | bus, | |
151b9b96 | 203 | &m, |
718db961 LP |
204 | "/org/freedesktop/systemd1", |
205 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 206 | "JobRemoved"); |
718db961 LP |
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 | ||
8f8f05a9 | 214 | return sd_bus_send(bus, m, NULL); |
97e6a119 | 215 | } |
c1e1601e | 216 | |
97e6a119 | 217 | void bus_job_send_removed_signal(Job *j) { |
718db961 | 218 | int r; |
c1e1601e | 219 | |
718db961 | 220 | assert(j); |
97e6a119 MS |
221 | |
222 | if (!j->sent_dbus_new_signal) | |
223 | bus_job_send_change_signal(j); | |
224 | ||
1a465207 | 225 | r = bus_foreach_bus(j->manager, j->bus_track, send_removed_signal, j); |
718db961 | 226 | if (r < 0) |
da927ba9 | 227 | log_debug_errno(r, "Failed to send job remove signal for %u: %m", j->id); |
c1e1601e | 228 | } |
c5a97ed1 LP |
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) { | |
c5a97ed1 LP |
244 | |
245 | assert(j); | |
246 | ||
247 | if (j->bus_track) | |
248 | return 0; | |
249 | ||
76d8ca22 | 250 | return sd_bus_track_new(j->unit->manager->api_bus, &j->bus_track, bus_job_track_handler, j); |
c5a97ed1 LP |
251 | } |
252 | ||
253 | int bus_job_coldplug_bus_track(Job *j) { | |
254 | int r = 0; | |
76d8ca22 | 255 | _cleanup_strv_free_ char **deserialized_clients = NULL; |
c5a97ed1 LP |
256 | |
257 | assert(j); | |
258 | ||
ae2a15bc | 259 | deserialized_clients = TAKE_PTR(j->deserialized_clients); |
76d8ca22 ZJS |
260 | |
261 | if (strv_isempty(deserialized_clients)) | |
262 | return 0; | |
c5a97ed1 LP |
263 | |
264 | if (!j->manager->api_bus) | |
76d8ca22 | 265 | return 0; |
c5a97ed1 LP |
266 | |
267 | r = bus_job_allocate_bus_track(j); | |
268 | if (r < 0) | |
76d8ca22 | 269 | return r; |
c5a97ed1 | 270 | |
76d8ca22 | 271 | return bus_track_add_name_many(j->bus_track, deserialized_clients); |
c5a97ed1 LP |
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 | } |