Debian Codegraph @

src/shared/bus-wait-for-jobs.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-bus.h"
4
5 #include "alloc-util.h"
6 #include "bus-internal.h"
7 #include "bus-util.h"
8 #include "bus-wait-for-jobs.h"
9 #include "errno-util.h"
10 #include "escape.h"
11 #include "log.h"
12 #include "set.h"
13 #include "string-util.h"
14 #include "strv.h"
15 #include "unit-def.h"
16
17 typedef struct BusWaitForJobs {
18 sd_bus *bus;
19
20 /* The set of jobs to wait for, as bus object paths */
21 Set *jobs;
22
23 /* The unit name and job result of the last Job message */
24 char *name;
25 char *result;
26
27 sd_bus_slot *slot_job_removed;
28 sd_bus_slot *slot_disconnected;
29 } BusWaitForJobs;
30
31 BusWaitForJobs* bus_wait_for_jobs_free(BusWaitForJobs *d) {
32 if (!d)
33 return NULL;
34
35 set_free(d->jobs);
36
37 sd_bus_slot_unref(d->slot_disconnected);
38 sd_bus_slot_unref(d->slot_job_removed);
39
40 sd_bus_unref(d->bus);
41
42 free(d->name);
43 free(d->result);
44
45 return mfree(d);
46 }
47
48 static int match_disconnected(sd_bus_message *m, void *userdata, sd_bus_error *reterr_error) {
49 assert(m);
50
51 log_warning("D-Bus connection terminated while waiting for jobs.");
52 sd_bus_close(sd_bus_message_get_bus(m));
53
54 return 0;
55 }
56
57 static int match_job_removed(sd_bus_message *m, void *userdata, sd_bus_error *reterr_error) {
58 BusWaitForJobs *d = ASSERT_PTR(userdata);
59 _cleanup_free_ char *job_found = NULL;
60 const char *path, *unit, *result;
61 int r;
62
63 assert(m);
64
65 r = sd_bus_message_read(m, "uoss", /* id= */ NULL, &path, &unit, &result);
66 if (r < 0) {
67 bus_log_parse_error(r);
68 return 0;
69 }
70
71 job_found = set_remove(d->jobs, (char*) path);
72 if (!job_found)
73 return 0;
74
75 (void) free_and_strdup(&d->name, empty_to_null(unit));
76 (void) free_and_strdup(&d->result, empty_to_null(result));
77
78 return 0;
79 }
80
81 int bus_wait_for_jobs_new(sd_bus *bus, BusWaitForJobs **ret) {
82 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *d = NULL;
83 int r;
84
85 assert(bus);
86 assert(ret);
87
88 d = new(BusWaitForJobs, 1);
89 if (!d)
90 return -ENOMEM;
91
92 *d = (BusWaitForJobs) {
93 .bus = sd_bus_ref(bus),
94 };
95
96 /* When we are a bus client we match by sender. Direct connections OTOH have no initialized sender
97 * field, and hence we ignore the sender then */
98 r = sd_bus_match_signal_async(
99 bus,
100 &d->slot_job_removed,
101 bus->bus_client ? "org.freedesktop.systemd1" : NULL,
102 "/org/freedesktop/systemd1",
103 "org.freedesktop.systemd1.Manager",
104 "JobRemoved",
105 match_job_removed, NULL, d);
106 if (r < 0)
107 return r;
108
109 r = sd_bus_match_signal_async(
110 bus,
111 &d->slot_disconnected,
112 "org.freedesktop.DBus.Local",
113 NULL,
114 "org.freedesktop.DBus.Local",
115 "Disconnected",
116 match_disconnected, NULL, d);
117 if (r < 0)
118 return r;
119
120 *ret = TAKE_PTR(d);
121
122 return 0;
123 }
124
125 static int bus_process_wait(sd_bus *bus) {
126 int r;
127
128 for (;;) {
129 r = sd_bus_process(bus, NULL);
130 if (r < 0)
131 return r;
132 if (r > 0)
133 return 0;
134
135 r = sd_bus_wait(bus, UINT64_MAX);
136 if (r < 0)
137 return r;
138 }
139 }
140
141 static int bus_job_get_service_result(BusWaitForJobs *d, char **ret) {
142 _cleanup_free_ char *dbus_path = NULL;
143
144 assert(d);
145 assert(d->name);
146 assert(ret);
147
148 if (!endswith(d->name, ".service"))
149 return -EINVAL;
150
151 dbus_path = unit_dbus_path_from_name(d->name);
152 if (!dbus_path)
153 return -ENOMEM;
154
155 return sd_bus_get_property_string(d->bus,
156 "org.freedesktop.systemd1",
157 dbus_path,
158 "org.freedesktop.systemd1.Service",
159 "Result",
160 NULL,
161 ret);
162 }
163
164 static int log_job_error_with_service_result(
165 const char* service,
166 const char *result,
167 bool quiet,
168 const char* const* extra_args) {
169
170 static const struct {
171 const char *result;
172 int error;
173 const char *explanation;
174 } explanations[] = {
175 { "resources", EMFILE, "of unavailable resources or another system error" },
176 { "protocol", EPROTONOSUPPORT, "the service did not take the steps required by its unit configuration" },
177 { "timeout", ETIMEDOUT, "a timeout was exceeded" },
178 { "exit-code", EBADE, "the control process exited with error code" },
179 { "signal", ENOTRECOVERABLE, "a fatal signal was delivered to the control process" },
180 { "core-dump", ECONNABORTED, "a fatal signal was delivered causing the control process to dump core" },
181 { "watchdog", ECONNRESET, "the service failed to send watchdog ping" },
182 { "start-limit-hit", EBUSY, "start of the service was attempted too often" },
183 { "oom-kill", ENOMEM, "of an out-of-memory (OOM) situation" },
184 };
185
186 _cleanup_free_ char *service_shell_quoted = NULL;
187 const char *systemctl = "systemctl", *journalctl = "journalctl";
188 int r;
189
190 assert(service);
191
192 service_shell_quoted = shell_maybe_quote(service, 0);
193
194 if (!strv_isempty((char* const*) extra_args)) {
195 _cleanup_free_ char *t = NULL;
196
197 t = strv_join((char* const*) extra_args, " ");
198 systemctl = strjoina("systemctl ", t ?: "<args>");
199 journalctl = strjoina("journalctl ", t ?: "<args>");
200 }
201
202 if (!isempty(result))
203 FOREACH_ELEMENT(i, explanations)
204 if (streq(result, i->result)) {
205 r = log_full_errno(quiet ? LOG_DEBUG : LOG_ERR,
206 SYNTHETIC_ERRNO(i->error),
207 "Job for %s failed because %s.\n"
208 "See \"%s status %s\" and \"%s -xeu %s\" for details.\n",
209 service, i->explanation,
210 systemctl, service_shell_quoted ?: "<service>",
211 journalctl, service_shell_quoted ?: "<service>");
212 goto extra;
213 }
214
215 r = log_full_errno(quiet ? LOG_DEBUG : LOG_ERR,
216 SYNTHETIC_ERRNO(ENOMEDIUM),
217 "Job for %s failed.\n"
218 "See \"%s status %s\" and \"%s -xeu %s\" for details.\n",
219 service,
220 systemctl, service_shell_quoted ?: "<service>",
221 journalctl, service_shell_quoted ?: "<service>");
222
223 extra:
224 /* For some results maybe additional explanation is required */
225 if (streq_ptr(result, "start-limit-hit"))
226 log_full(quiet ? LOG_DEBUG : LOG_INFO,
227 "To force a start use \"%1$s reset-failed %2$s\"\n"
228 "followed by \"%1$s start %2$s\" again.",
229 systemctl,
230 service_shell_quoted ?: "<service>");
231
232 return r;
233 }
234
235 static int check_wait_response(BusWaitForJobs *d, WaitJobsFlags flags, const char* const* extra_args) {
236 int r;
237
238 assert(d);
239 assert(d->name);
240 assert(d->result);
241
242 if (streq(d->result, "done")) {
243 log_full(FLAGS_SET(flags, BUS_WAIT_JOBS_LOG_SUCCESS) ? LOG_INFO : LOG_DEBUG,
244 "Job for %s finished.", d->name);
245
246 return 0;
247 } else if (streq(d->result, "skipped")) {
248 log_full(FLAGS_SET(flags, BUS_WAIT_JOBS_LOG_SUCCESS) ? LOG_INFO : LOG_DEBUG,
249 "Job for %s was skipped.", d->name);
250
251 return 0;
252 }
253
254 int priority = FLAGS_SET(flags, BUS_WAIT_JOBS_LOG_ERROR) ? LOG_ERR : LOG_DEBUG;
255
256 if (streq(d->result, "canceled"))
257 return log_full_errno(priority, SYNTHETIC_ERRNO(ECANCELED), "Job for %s canceled.", d->name);
258 if (streq(d->result, "timeout"))
259 return log_full_errno(priority, SYNTHETIC_ERRNO(ETIME), "Job for %s timed out.", d->name);
260 if (streq(d->result, "dependency"))
261 return log_full_errno(priority, SYNTHETIC_ERRNO(EIO), "A dependency job for %s failed. See 'journalctl -xe' for details.", d->name);
262 if (streq(d->result, "invalid"))
263 return log_full_errno(priority, SYNTHETIC_ERRNO(ENOEXEC), "%s is not active, cannot reload.", d->name);
264 if (streq(d->result, "assert"))
265 return log_full_errno(priority, SYNTHETIC_ERRNO(EPROTO), "Assertion failed on job for %s.", d->name);
266 if (streq(d->result, "unsupported"))
267 return log_full_errno(priority, SYNTHETIC_ERRNO(EOPNOTSUPP), "Operation on or unit type of %s not supported on this system.", d->name);
268 if (streq(d->result, "collected"))
269 return log_full_errno(priority, SYNTHETIC_ERRNO(ECANCELED), "Queued job for %s was garbage collected.", d->name);
270 if (streq(d->result, "once"))
271 return log_full_errno(priority, SYNTHETIC_ERRNO(ESTALE), "Unit %s was started already once and can't be started again.", d->name);
272 if (streq(d->result, "frozen"))
273 return log_full_errno(priority, SYNTHETIC_ERRNO(EDEADLK), "Cannot perform operation on frozen unit %s.", d->name);
274 if (streq(d->result, "concurrency"))
275 return log_full_errno(priority, SYNTHETIC_ERRNO(ETOOMANYREFS), "Concurrency limit of a slice unit %s is contained in has been reached.", d->name);
276
277 if (!streq(d->result, "failed"))
278 log_debug("Unexpected job result '%s' for unit '%s', assuming server side newer than us.",
279 d->result, d->name);
280
281 /* Job is failed, or result is unknown. For non-service units, just show a generic message. */
282 if (!endswith(d->name, ".service"))
283 return log_full_errno(priority, SYNTHETIC_ERRNO(ENOMEDIUM), "Job failed. See \"journalctl -xe\" for details.");
284
285 /* For services, let's also try Result property. */
286 _cleanup_free_ char *result = NULL;
287 r = bus_job_get_service_result(d, &result);
288 if (r < 0)
289 log_debug_errno(r, "Failed to get Result property of unit %s, ignoring: %m", d->name);
290
291 return log_job_error_with_service_result(
292 d->name, result,
293 /* quiet= */ !FLAGS_SET(flags, BUS_WAIT_JOBS_LOG_ERROR),
294 extra_args);
295 }
296
297 int bus_wait_for_jobs(BusWaitForJobs *d, WaitJobsFlags flags, const char* const* extra_args) {
298 int r = 0;
299
300 assert(d);
301
302 while (!set_isempty(d->jobs)) {
303 int q;
304
305 q = bus_process_wait(d->bus);
306 if (q < 0)
307 return log_error_errno(q, "Failed to wait for response: %m");
308
309 if (d->name && d->result) {
310 q = check_wait_response(d, flags, extra_args);
311 /* Return the first error as it is most likely to be meaningful. */
312 RET_GATHER(r, q);
313
314 log_full_errno_zerook(LOG_DEBUG, q,
315 "Got result %s/%m for job %s.", d->result, d->name);
316 }
317
318 d->name = mfree(d->name);
319 d->result = mfree(d->result);
320 }
321
322 return r;
323 }
324
325 int bus_wait_for_jobs_add(BusWaitForJobs *d, const char *path) {
326 assert(d);
327
328 return set_put_strdup(&d->jobs, path);
329 }
330
331 int bus_wait_for_jobs_one(BusWaitForJobs *d, const char *path, WaitJobsFlags flags, const char* const* extra_args) {
332 int r;
333
334 r = bus_wait_for_jobs_add(d, path);
335 if (r < 0)
336 return log_oom();
337
338 return bus_wait_for_jobs(d, flags, extra_args);
339 }