]>
Commit | Line | Data |
---|---|---|
52ad194e | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
663996b3 | 2 | |
663996b3 | 3 | #include <errno.h> |
663996b3 | 4 | |
60f067b4 JS |
5 | #include "sd-id128.h" |
6 | #include "sd-messages.h" | |
db2df898 MP |
7 | |
8 | #include "alloc-util.h" | |
14228c0d | 9 | #include "async.h" |
db2df898 | 10 | #include "dbus-job.h" |
60f067b4 | 11 | #include "dbus.h" |
db2df898 | 12 | #include "escape.h" |
6e866b33 | 13 | #include "fileio.h" |
db2df898 MP |
14 | #include "job.h" |
15 | #include "log.h" | |
16 | #include "macro.h" | |
17 | #include "parse-util.h" | |
6e866b33 | 18 | #include "serialize.h" |
db2df898 MP |
19 | #include "set.h" |
20 | #include "special.h" | |
4c89c718 | 21 | #include "stdio-util.h" |
db2df898 MP |
22 | #include "string-table.h" |
23 | #include "string-util.h" | |
24 | #include "strv.h" | |
e3bff60a | 25 | #include "terminal-util.h" |
db2df898 MP |
26 | #include "unit.h" |
27 | #include "virt.h" | |
663996b3 MS |
28 | |
29 | Job* job_new_raw(Unit *unit) { | |
30 | Job *j; | |
31 | ||
32 | /* used for deserialization */ | |
33 | ||
34 | assert(unit); | |
35 | ||
6e866b33 | 36 | j = new(Job, 1); |
663996b3 MS |
37 | if (!j) |
38 | return NULL; | |
39 | ||
6e866b33 MB |
40 | *j = (Job) { |
41 | .manager = unit->manager, | |
42 | .unit = unit, | |
43 | .type = _JOB_TYPE_INVALID, | |
44 | }; | |
663996b3 MS |
45 | |
46 | return j; | |
47 | } | |
48 | ||
49 | Job* job_new(Unit *unit, JobType type) { | |
50 | Job *j; | |
51 | ||
52 | assert(type < _JOB_TYPE_MAX); | |
53 | ||
54 | j = job_new_raw(unit); | |
55 | if (!j) | |
56 | return NULL; | |
57 | ||
58 | j->id = j->manager->current_job_id++; | |
59 | j->type = type; | |
60 | ||
61 | /* We don't link it here, that's what job_dependency() is for */ | |
62 | ||
63 | return j; | |
64 | } | |
65 | ||
b012e921 | 66 | void job_unlink(Job *j) { |
663996b3 MS |
67 | assert(j); |
68 | assert(!j->installed); | |
69 | assert(!j->transaction_prev); | |
70 | assert(!j->transaction_next); | |
71 | assert(!j->subject_list); | |
72 | assert(!j->object_list); | |
73 | ||
b012e921 | 74 | if (j->in_run_queue) { |
60f067b4 | 75 | LIST_REMOVE(run_queue, j->manager->run_queue, j); |
b012e921 MB |
76 | j->in_run_queue = false; |
77 | } | |
663996b3 | 78 | |
b012e921 | 79 | if (j->in_dbus_queue) { |
60f067b4 | 80 | LIST_REMOVE(dbus_queue, j->manager->dbus_job_queue, j); |
b012e921 MB |
81 | j->in_dbus_queue = false; |
82 | } | |
663996b3 | 83 | |
b012e921 | 84 | if (j->in_gc_queue) { |
2897b343 | 85 | LIST_REMOVE(gc_queue, j->manager->gc_job_queue, j); |
b012e921 MB |
86 | j->in_gc_queue = false; |
87 | } | |
88 | ||
89 | j->timer_event_source = sd_event_source_unref(j->timer_event_source); | |
90 | } | |
91 | ||
6e866b33 | 92 | Job* job_free(Job *j) { |
b012e921 MB |
93 | assert(j); |
94 | assert(!j->installed); | |
95 | assert(!j->transaction_prev); | |
96 | assert(!j->transaction_next); | |
97 | assert(!j->subject_list); | |
98 | assert(!j->object_list); | |
2897b343 | 99 | |
b012e921 | 100 | job_unlink(j); |
663996b3 | 101 | |
2897b343 | 102 | sd_bus_track_unref(j->bus_track); |
5eef597e | 103 | strv_free(j->deserialized_clients); |
663996b3 | 104 | |
6e866b33 | 105 | return mfree(j); |
663996b3 MS |
106 | } |
107 | ||
e735f4d4 MP |
108 | static void job_set_state(Job *j, JobState state) { |
109 | assert(j); | |
110 | assert(state >= 0); | |
111 | assert(state < _JOB_STATE_MAX); | |
112 | ||
113 | if (j->state == state) | |
114 | return; | |
115 | ||
116 | j->state = state; | |
117 | ||
118 | if (!j->installed) | |
119 | return; | |
120 | ||
121 | if (j->state == JOB_RUNNING) | |
122 | j->unit->manager->n_running_jobs++; | |
123 | else { | |
124 | assert(j->state == JOB_WAITING); | |
125 | assert(j->unit->manager->n_running_jobs > 0); | |
126 | ||
127 | j->unit->manager->n_running_jobs--; | |
128 | ||
129 | if (j->unit->manager->n_running_jobs <= 0) | |
130 | j->unit->manager->jobs_in_progress_event_source = sd_event_source_unref(j->unit->manager->jobs_in_progress_event_source); | |
131 | } | |
132 | } | |
133 | ||
663996b3 MS |
134 | void job_uninstall(Job *j) { |
135 | Job **pj; | |
136 | ||
137 | assert(j->installed); | |
138 | ||
e735f4d4 MP |
139 | job_set_state(j, JOB_WAITING); |
140 | ||
663996b3 MS |
141 | pj = (j->type == JOB_NOP) ? &j->unit->nop_job : &j->unit->job; |
142 | assert(*pj == j); | |
143 | ||
144 | /* Detach from next 'bigger' objects */ | |
145 | ||
146 | /* daemon-reload should be transparent to job observers */ | |
aa27b158 | 147 | if (!MANAGER_IS_RELOADING(j->manager)) |
663996b3 MS |
148 | bus_job_send_removed_signal(j); |
149 | ||
150 | *pj = NULL; | |
151 | ||
152 | unit_add_to_gc_queue(j->unit); | |
153 | ||
6e866b33 | 154 | hashmap_remove_value(j->manager->jobs, UINT32_TO_PTR(j->id), j); |
663996b3 MS |
155 | j->installed = false; |
156 | } | |
157 | ||
158 | static bool job_type_allows_late_merge(JobType t) { | |
159 | /* Tells whether it is OK to merge a job of type 't' with an already | |
160 | * running job. | |
161 | * Reloads cannot be merged this way. Think of the sequence: | |
162 | * 1. Reload of a daemon is in progress; the daemon has already loaded | |
163 | * its config file, but hasn't completed the reload operation yet. | |
164 | * 2. Edit foo's config file. | |
165 | * 3. Trigger another reload to have the daemon use the new config. | |
166 | * Should the second reload job be merged into the first one, the daemon | |
167 | * would not know about the new config. | |
168 | * JOB_RESTART jobs on the other hand can be merged, because they get | |
169 | * patched into JOB_START after stopping the unit. So if we see a | |
170 | * JOB_RESTART running, it means the unit hasn't stopped yet and at | |
171 | * this time the merge is still allowed. */ | |
172 | return t != JOB_RELOAD; | |
173 | } | |
174 | ||
175 | static void job_merge_into_installed(Job *j, Job *other) { | |
176 | assert(j->installed); | |
177 | assert(j->unit == other->unit); | |
178 | ||
179 | if (j->type != JOB_NOP) | |
6e866b33 | 180 | assert_se(job_type_merge_and_collapse(&j->type, other->type, j->unit) == 0); |
663996b3 MS |
181 | else |
182 | assert(other->type == JOB_NOP); | |
183 | ||
663996b3 MS |
184 | j->irreversible = j->irreversible || other->irreversible; |
185 | j->ignore_order = j->ignore_order || other->ignore_order; | |
186 | } | |
187 | ||
188 | Job* job_install(Job *j) { | |
189 | Job **pj; | |
190 | Job *uj; | |
191 | ||
192 | assert(!j->installed); | |
193 | assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION); | |
e735f4d4 | 194 | assert(j->state == JOB_WAITING); |
663996b3 MS |
195 | |
196 | pj = (j->type == JOB_NOP) ? &j->unit->nop_job : &j->unit->job; | |
197 | uj = *pj; | |
198 | ||
199 | if (uj) { | |
f47781d8 | 200 | if (job_type_is_conflicting(uj->type, j->type)) |
aa27b158 | 201 | job_finish_and_invalidate(uj, JOB_CANCELED, false, false); |
663996b3 MS |
202 | else { |
203 | /* not conflicting, i.e. mergeable */ | |
204 | ||
f47781d8 | 205 | if (uj->state == JOB_WAITING || |
663996b3 MS |
206 | (job_type_allows_late_merge(j->type) && job_type_is_superset(uj->type, j->type))) { |
207 | job_merge_into_installed(uj, j); | |
e3bff60a | 208 | log_unit_debug(uj->unit, |
663996b3 MS |
209 | "Merged into installed job %s/%s as %u", |
210 | uj->unit->id, job_type_to_string(uj->type), (unsigned) uj->id); | |
211 | return uj; | |
212 | } else { | |
213 | /* already running and not safe to merge into */ | |
214 | /* Patch uj to become a merged job and re-run it. */ | |
215 | /* XXX It should be safer to queue j to run after uj finishes, but it is | |
216 | * not currently possible to have more than one installed job per unit. */ | |
217 | job_merge_into_installed(uj, j); | |
e3bff60a | 218 | log_unit_debug(uj->unit, |
663996b3 MS |
219 | "Merged into running job, re-running: %s/%s as %u", |
220 | uj->unit->id, job_type_to_string(uj->type), (unsigned) uj->id); | |
e735f4d4 MP |
221 | |
222 | job_set_state(uj, JOB_WAITING); | |
663996b3 MS |
223 | return uj; |
224 | } | |
225 | } | |
226 | } | |
227 | ||
228 | /* Install the job */ | |
229 | *pj = j; | |
230 | j->installed = true; | |
e735f4d4 | 231 | |
aa27b158 | 232 | j->manager->n_installed_jobs++; |
e3bff60a | 233 | log_unit_debug(j->unit, |
663996b3 MS |
234 | "Installed new job %s/%s as %u", |
235 | j->unit->id, job_type_to_string(j->type), (unsigned) j->id); | |
2897b343 MP |
236 | |
237 | job_add_to_gc_queue(j); | |
238 | ||
6e866b33 MB |
239 | job_add_to_dbus_queue(j); /* announce this job to clients */ |
240 | unit_add_to_dbus_queue(j->unit); /* The Job property of the unit has changed now */ | |
241 | ||
663996b3 MS |
242 | return j; |
243 | } | |
244 | ||
245 | int job_install_deserialized(Job *j) { | |
246 | Job **pj; | |
6e866b33 | 247 | int r; |
663996b3 MS |
248 | |
249 | assert(!j->installed); | |
250 | ||
6e866b33 MB |
251 | if (j->type < 0 || j->type >= _JOB_TYPE_MAX_IN_TRANSACTION) |
252 | return log_unit_debug_errno(j->unit, SYNTHETIC_ERRNO(EINVAL), | |
253 | "Invalid job type %s in deserialization.", | |
254 | strna(job_type_to_string(j->type))); | |
663996b3 MS |
255 | |
256 | pj = (j->type == JOB_NOP) ? &j->unit->nop_job : &j->unit->job; | |
6e866b33 MB |
257 | if (*pj) |
258 | return log_unit_debug_errno(j->unit, SYNTHETIC_ERRNO(EEXIST), | |
259 | "Unit already has a job installed. Not installing deserialized job."); | |
260 | ||
261 | r = hashmap_put(j->manager->jobs, UINT32_TO_PTR(j->id), j); | |
262 | if (r == -EEXIST) | |
263 | return log_unit_debug_errno(j->unit, r, "Job ID %" PRIu32 " already used, cannot deserialize job.", j->id); | |
264 | if (r < 0) | |
265 | return log_unit_debug_errno(j->unit, r, "Failed to insert job into jobs hash table: %m"); | |
e735f4d4 | 266 | |
663996b3 MS |
267 | *pj = j; |
268 | j->installed = true; | |
e735f4d4 MP |
269 | |
270 | if (j->state == JOB_RUNNING) | |
271 | j->unit->manager->n_running_jobs++; | |
272 | ||
e3bff60a | 273 | log_unit_debug(j->unit, |
663996b3 MS |
274 | "Reinstalled deserialized job %s/%s as %u", |
275 | j->unit->id, job_type_to_string(j->type), (unsigned) j->id); | |
276 | return 0; | |
277 | } | |
278 | ||
279 | JobDependency* job_dependency_new(Job *subject, Job *object, bool matters, bool conflicts) { | |
280 | JobDependency *l; | |
281 | ||
282 | assert(object); | |
283 | ||
284 | /* Adds a new job link, which encodes that the 'subject' job | |
285 | * needs the 'object' job in some way. If 'subject' is NULL | |
286 | * this means the 'anchor' job (i.e. the one the user | |
287 | * explicitly asked for) is the requester. */ | |
288 | ||
2897b343 MP |
289 | l = new0(JobDependency, 1); |
290 | if (!l) | |
663996b3 MS |
291 | return NULL; |
292 | ||
293 | l->subject = subject; | |
294 | l->object = object; | |
295 | l->matters = matters; | |
296 | l->conflicts = conflicts; | |
297 | ||
298 | if (subject) | |
60f067b4 | 299 | LIST_PREPEND(subject, subject->subject_list, l); |
663996b3 | 300 | |
60f067b4 | 301 | LIST_PREPEND(object, object->object_list, l); |
663996b3 MS |
302 | |
303 | return l; | |
304 | } | |
305 | ||
306 | void job_dependency_free(JobDependency *l) { | |
307 | assert(l); | |
308 | ||
309 | if (l->subject) | |
60f067b4 | 310 | LIST_REMOVE(subject, l->subject->subject_list, l); |
663996b3 | 311 | |
60f067b4 | 312 | LIST_REMOVE(object, l->object->object_list, l); |
663996b3 MS |
313 | |
314 | free(l); | |
315 | } | |
316 | ||
6e866b33 | 317 | void job_dump(Job *j, FILE *f, const char *prefix) { |
663996b3 MS |
318 | assert(j); |
319 | assert(f); | |
320 | ||
1d42b86d | 321 | prefix = strempty(prefix); |
663996b3 MS |
322 | |
323 | fprintf(f, | |
324 | "%s-> Job %u:\n" | |
325 | "%s\tAction: %s -> %s\n" | |
326 | "%s\tState: %s\n" | |
98393f85 MB |
327 | "%s\tIrreversible: %s\n" |
328 | "%s\tMay GC: %s\n", | |
663996b3 MS |
329 | prefix, j->id, |
330 | prefix, j->unit->id, job_type_to_string(j->type), | |
331 | prefix, job_state_to_string(j->state), | |
98393f85 MB |
332 | prefix, yes_no(j->irreversible), |
333 | prefix, yes_no(job_may_gc(j))); | |
663996b3 MS |
334 | } |
335 | ||
336 | /* | |
337 | * Merging is commutative, so imagine the matrix as symmetric. We store only | |
338 | * its lower triangle to avoid duplication. We don't store the main diagonal, | |
339 | * because A merged with A is simply A. | |
340 | * | |
341 | * If the resulting type is collapsed immediately afterwards (to get rid of | |
342 | * the JOB_RELOAD_OR_START, which lies outside the lookup function's domain), | |
343 | * the following properties hold: | |
344 | * | |
e3bff60a MP |
345 | * Merging is associative! A merged with B, and then merged with C is the same |
346 | * as A merged with the result of B merged with C. | |
663996b3 MS |
347 | * |
348 | * Mergeability is transitive! If A can be merged with B and B with C then | |
349 | * A also with C. | |
350 | * | |
351 | * Also, if A merged with B cannot be merged with C, then either A or B cannot | |
352 | * be merged with C either. | |
353 | */ | |
354 | static const JobType job_merging_table[] = { | |
355 | /* What \ With * JOB_START JOB_VERIFY_ACTIVE JOB_STOP JOB_RELOAD */ | |
356 | /*********************************************************************************/ | |
357 | /*JOB_START */ | |
358 | /*JOB_VERIFY_ACTIVE */ JOB_START, | |
359 | /*JOB_STOP */ -1, -1, | |
360 | /*JOB_RELOAD */ JOB_RELOAD_OR_START, JOB_RELOAD, -1, | |
361 | /*JOB_RESTART */ JOB_RESTART, JOB_RESTART, -1, JOB_RESTART, | |
362 | }; | |
363 | ||
364 | JobType job_type_lookup_merge(JobType a, JobType b) { | |
365 | assert_cc(ELEMENTSOF(job_merging_table) == _JOB_TYPE_MAX_MERGING * (_JOB_TYPE_MAX_MERGING - 1) / 2); | |
366 | assert(a >= 0 && a < _JOB_TYPE_MAX_MERGING); | |
367 | assert(b >= 0 && b < _JOB_TYPE_MAX_MERGING); | |
368 | ||
369 | if (a == b) | |
370 | return a; | |
371 | ||
372 | if (a < b) { | |
373 | JobType tmp = a; | |
374 | a = b; | |
375 | b = tmp; | |
376 | } | |
377 | ||
378 | return job_merging_table[(a - 1) * a / 2 + b]; | |
379 | } | |
380 | ||
381 | bool job_type_is_redundant(JobType a, UnitActiveState b) { | |
382 | switch (a) { | |
383 | ||
384 | case JOB_START: | |
f5e65279 | 385 | return IN_SET(b, UNIT_ACTIVE, UNIT_RELOADING); |
663996b3 MS |
386 | |
387 | case JOB_STOP: | |
f5e65279 | 388 | return IN_SET(b, UNIT_INACTIVE, UNIT_FAILED); |
663996b3 MS |
389 | |
390 | case JOB_VERIFY_ACTIVE: | |
f5e65279 | 391 | return IN_SET(b, UNIT_ACTIVE, UNIT_RELOADING); |
663996b3 MS |
392 | |
393 | case JOB_RELOAD: | |
394 | return | |
395 | b == UNIT_RELOADING; | |
396 | ||
397 | case JOB_RESTART: | |
398 | return | |
399 | b == UNIT_ACTIVATING; | |
400 | ||
f47781d8 MP |
401 | case JOB_NOP: |
402 | return true; | |
403 | ||
663996b3 MS |
404 | default: |
405 | assert_not_reached("Invalid job type"); | |
406 | } | |
407 | } | |
408 | ||
e3bff60a | 409 | JobType job_type_collapse(JobType t, Unit *u) { |
663996b3 MS |
410 | UnitActiveState s; |
411 | ||
e3bff60a | 412 | switch (t) { |
663996b3 MS |
413 | |
414 | case JOB_TRY_RESTART: | |
415 | s = unit_active_state(u); | |
416 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s)) | |
e3bff60a MP |
417 | return JOB_NOP; |
418 | ||
419 | return JOB_RESTART; | |
663996b3 | 420 | |
4c89c718 MP |
421 | case JOB_TRY_RELOAD: |
422 | s = unit_active_state(u); | |
423 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s)) | |
424 | return JOB_NOP; | |
425 | ||
426 | return JOB_RELOAD; | |
427 | ||
663996b3 MS |
428 | case JOB_RELOAD_OR_START: |
429 | s = unit_active_state(u); | |
430 | if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s)) | |
e3bff60a MP |
431 | return JOB_START; |
432 | ||
433 | return JOB_RELOAD; | |
663996b3 MS |
434 | |
435 | default: | |
e3bff60a | 436 | return t; |
663996b3 MS |
437 | } |
438 | } | |
439 | ||
440 | int job_type_merge_and_collapse(JobType *a, JobType b, Unit *u) { | |
e3bff60a MP |
441 | JobType t; |
442 | ||
443 | t = job_type_lookup_merge(*a, b); | |
663996b3 MS |
444 | if (t < 0) |
445 | return -EEXIST; | |
e3bff60a MP |
446 | |
447 | *a = job_type_collapse(t, u); | |
663996b3 MS |
448 | return 0; |
449 | } | |
450 | ||
60f067b4 | 451 | static bool job_is_runnable(Job *j) { |
663996b3 MS |
452 | Iterator i; |
453 | Unit *other; | |
52ad194e | 454 | void *v; |
663996b3 MS |
455 | |
456 | assert(j); | |
457 | assert(j->installed); | |
458 | ||
459 | /* Checks whether there is any job running for the units this | |
460 | * job needs to be running after (in the case of a 'positive' | |
461 | * job type) or before (in the case of a 'negative' job | |
462 | * type. */ | |
463 | ||
60f067b4 JS |
464 | /* Note that unit types have a say in what is runnable, |
465 | * too. For example, if they return -EAGAIN from | |
466 | * unit_start() they can indicate they are not | |
467 | * runnable yet. */ | |
468 | ||
663996b3 MS |
469 | /* First check if there is an override */ |
470 | if (j->ignore_order) | |
471 | return true; | |
472 | ||
473 | if (j->type == JOB_NOP) | |
474 | return true; | |
475 | ||
2897b343 | 476 | if (IN_SET(j->type, JOB_START, JOB_VERIFY_ACTIVE, JOB_RELOAD)) { |
663996b3 | 477 | /* Immediate result is that the job is or might be |
fb183854 | 478 | * started. In this case let's wait for the |
663996b3 MS |
479 | * dependencies, regardless whether they are |
480 | * starting or stopping something. */ | |
481 | ||
52ad194e | 482 | HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_AFTER], i) |
663996b3 MS |
483 | if (other->job) |
484 | return false; | |
485 | } | |
486 | ||
487 | /* Also, if something else is being stopped and we should | |
fb183854 | 488 | * change state after it, then let's wait. */ |
663996b3 | 489 | |
52ad194e | 490 | HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_BEFORE], i) |
663996b3 | 491 | if (other->job && |
2897b343 | 492 | IN_SET(other->job->type, JOB_STOP, JOB_RESTART)) |
663996b3 MS |
493 | return false; |
494 | ||
495 | /* This means that for a service a and a service b where b | |
496 | * shall be started after a: | |
497 | * | |
498 | * start a + start b → 1st step start a, 2nd step start b | |
499 | * start a + stop b → 1st step stop b, 2nd step start a | |
500 | * stop a + start b → 1st step stop a, 2nd step start b | |
501 | * stop a + stop b → 1st step stop b, 2nd step stop a | |
502 | * | |
503 | * This has the side effect that restarts are properly | |
504 | * synchronized too. */ | |
505 | ||
506 | return true; | |
507 | } | |
508 | ||
509 | static void job_change_type(Job *j, JobType newtype) { | |
e3bff60a MP |
510 | assert(j); |
511 | ||
512 | log_unit_debug(j->unit, | |
663996b3 MS |
513 | "Converting job %s/%s -> %s/%s", |
514 | j->unit->id, job_type_to_string(j->type), | |
515 | j->unit->id, job_type_to_string(newtype)); | |
516 | ||
517 | j->type = newtype; | |
518 | } | |
519 | ||
6e866b33 MB |
520 | _pure_ static const char* job_get_begin_status_message_format(Unit *u, JobType t) { |
521 | const char *format; | |
522 | ||
523 | assert(u); | |
524 | ||
525 | if (t == JOB_RELOAD) | |
526 | return "Reloading %s."; | |
527 | ||
528 | assert(IN_SET(t, JOB_START, JOB_STOP)); | |
529 | ||
530 | format = UNIT_VTABLE(u)->status_message_formats.starting_stopping[t == JOB_STOP]; | |
531 | if (format) | |
532 | return format; | |
533 | ||
534 | /* Return generic strings */ | |
535 | if (t == JOB_START) | |
536 | return "Starting %s."; | |
537 | else { | |
538 | assert(t == JOB_STOP); | |
539 | return "Stopping %s."; | |
540 | } | |
541 | } | |
542 | ||
543 | static void job_print_begin_status_message(Unit *u, JobType t) { | |
544 | const char *format; | |
545 | ||
546 | assert(u); | |
547 | ||
548 | /* Reload status messages have traditionally not been printed to console. */ | |
549 | if (!IN_SET(t, JOB_START, JOB_STOP)) | |
550 | return; | |
551 | ||
552 | format = job_get_begin_status_message_format(u, t); | |
553 | ||
554 | DISABLE_WARNING_FORMAT_NONLITERAL; | |
555 | unit_status_printf(u, "", format); | |
556 | REENABLE_WARNING; | |
557 | } | |
558 | ||
559 | static void job_log_begin_status_message(Unit *u, uint32_t job_id, JobType t) { | |
560 | const char *format, *mid; | |
561 | char buf[LINE_MAX]; | |
562 | ||
563 | assert(u); | |
564 | assert(t >= 0); | |
565 | assert(t < _JOB_TYPE_MAX); | |
566 | ||
567 | if (!IN_SET(t, JOB_START, JOB_STOP, JOB_RELOAD)) | |
568 | return; | |
569 | ||
570 | if (log_on_console()) /* Skip this if it would only go on the console anyway */ | |
571 | return; | |
572 | ||
573 | /* We log status messages for all units and all operations. */ | |
574 | ||
575 | format = job_get_begin_status_message_format(u, t); | |
576 | ||
577 | DISABLE_WARNING_FORMAT_NONLITERAL; | |
578 | (void) snprintf(buf, sizeof buf, format, unit_description(u)); | |
579 | REENABLE_WARNING; | |
580 | ||
581 | mid = t == JOB_START ? "MESSAGE_ID=" SD_MESSAGE_UNIT_STARTING_STR : | |
582 | t == JOB_STOP ? "MESSAGE_ID=" SD_MESSAGE_UNIT_STOPPING_STR : | |
583 | "MESSAGE_ID=" SD_MESSAGE_UNIT_RELOADING_STR; | |
584 | ||
585 | /* Note that we deliberately use LOG_MESSAGE() instead of | |
586 | * LOG_UNIT_MESSAGE() here, since this is supposed to mimic | |
587 | * closely what is written to screen using the status output, | |
588 | * which is supposed the highest level, friendliest output | |
589 | * possible, which means we should avoid the low-level unit | |
590 | * name. */ | |
591 | log_struct(LOG_INFO, | |
592 | LOG_MESSAGE("%s", buf), | |
593 | "JOB_ID=%" PRIu32, job_id, | |
594 | "JOB_TYPE=%s", job_type_to_string(t), | |
595 | LOG_UNIT_ID(u), | |
596 | LOG_UNIT_INVOCATION_ID(u), | |
597 | mid); | |
598 | } | |
599 | ||
600 | static void job_emit_begin_status_message(Unit *u, uint32_t job_id, JobType t) { | |
601 | assert(u); | |
602 | assert(t >= 0); | |
603 | assert(t < _JOB_TYPE_MAX); | |
604 | ||
605 | job_log_begin_status_message(u, job_id, t); | |
606 | job_print_begin_status_message(u, t); | |
607 | } | |
608 | ||
7035cd9e | 609 | static int job_perform_on_unit(Job **j) { |
db2df898 MP |
610 | uint32_t id; |
611 | Manager *m; | |
612 | JobType t; | |
613 | Unit *u; | |
7035cd9e MP |
614 | int r; |
615 | ||
db2df898 MP |
616 | /* While we execute this operation the job might go away (for |
617 | * example: because it finishes immediately or is replaced by | |
618 | * a new, conflicting job.) To make sure we don't access a | |
619 | * freed job later on we store the id here, so that we can | |
620 | * verify the job is still valid. */ | |
621 | ||
622 | assert(j); | |
623 | assert(*j); | |
624 | ||
625 | m = (*j)->manager; | |
626 | u = (*j)->unit; | |
627 | t = (*j)->type; | |
628 | id = (*j)->id; | |
629 | ||
7035cd9e MP |
630 | switch (t) { |
631 | case JOB_START: | |
632 | r = unit_start(u); | |
633 | break; | |
634 | ||
635 | case JOB_RESTART: | |
636 | t = JOB_STOP; | |
52ad194e | 637 | _fallthrough_; |
7035cd9e MP |
638 | case JOB_STOP: |
639 | r = unit_stop(u); | |
640 | break; | |
641 | ||
642 | case JOB_RELOAD: | |
643 | r = unit_reload(u); | |
644 | break; | |
645 | ||
646 | default: | |
647 | assert_not_reached("Invalid job type"); | |
648 | } | |
649 | ||
6e866b33 MB |
650 | /* Log if the job still exists and the start/stop/reload function actually did something. Note that this means |
651 | * for units for which there's no 'activating' phase (i.e. because we transition directly from 'inactive' to | |
652 | * 'active') we'll possibly skip the "Starting..." message. */ | |
7035cd9e MP |
653 | *j = manager_get_job(m, id); |
654 | if (*j && r > 0) | |
6e866b33 | 655 | job_emit_begin_status_message(u, id, t); |
7035cd9e MP |
656 | |
657 | return r; | |
658 | } | |
659 | ||
663996b3 MS |
660 | int job_run_and_invalidate(Job *j) { |
661 | int r; | |
663996b3 MS |
662 | |
663 | assert(j); | |
664 | assert(j->installed); | |
665 | assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION); | |
666 | assert(j->in_run_queue); | |
667 | ||
60f067b4 | 668 | LIST_REMOVE(run_queue, j->manager->run_queue, j); |
663996b3 MS |
669 | j->in_run_queue = false; |
670 | ||
671 | if (j->state != JOB_WAITING) | |
672 | return 0; | |
673 | ||
674 | if (!job_is_runnable(j)) | |
675 | return -EAGAIN; | |
676 | ||
81c58355 | 677 | job_start_timer(j, true); |
e735f4d4 | 678 | job_set_state(j, JOB_RUNNING); |
663996b3 MS |
679 | job_add_to_dbus_queue(j); |
680 | ||
663996b3 MS |
681 | switch (j->type) { |
682 | ||
663996b3 | 683 | case JOB_VERIFY_ACTIVE: { |
6e866b33 MB |
684 | UnitActiveState t; |
685 | ||
686 | t = unit_active_state(j->unit); | |
663996b3 MS |
687 | if (UNIT_IS_ACTIVE_OR_RELOADING(t)) |
688 | r = -EALREADY; | |
689 | else if (t == UNIT_ACTIVATING) | |
690 | r = -EAGAIN; | |
691 | else | |
60f067b4 | 692 | r = -EBADR; |
663996b3 MS |
693 | break; |
694 | } | |
695 | ||
7035cd9e | 696 | case JOB_START: |
663996b3 MS |
697 | case JOB_STOP: |
698 | case JOB_RESTART: | |
7035cd9e | 699 | r = job_perform_on_unit(&j); |
663996b3 | 700 | |
6e866b33 | 701 | /* If the unit type does not support starting/stopping, then simply wait. */ |
663996b3 MS |
702 | if (r == -EBADR) |
703 | r = 0; | |
704 | break; | |
705 | ||
706 | case JOB_RELOAD: | |
7035cd9e | 707 | r = job_perform_on_unit(&j); |
663996b3 MS |
708 | break; |
709 | ||
710 | case JOB_NOP: | |
711 | r = -EALREADY; | |
712 | break; | |
713 | ||
714 | default: | |
715 | assert_not_reached("Unknown job type"); | |
716 | } | |
717 | ||
663996b3 | 718 | if (j) { |
6e866b33 MB |
719 | if (r == -EAGAIN) |
720 | job_set_state(j, JOB_WAITING); /* Hmm, not ready after all, let's return to JOB_WAITING state */ | |
721 | else if (r == -EALREADY) /* already being executed */ | |
aa27b158 | 722 | r = job_finish_and_invalidate(j, JOB_DONE, true, true); |
6e866b33 MB |
723 | else if (r == -ECOMM) /* condition failed, but all is good */ |
724 | r = job_finish_and_invalidate(j, JOB_DONE, true, false); | |
60f067b4 | 725 | else if (r == -EBADR) |
aa27b158 | 726 | r = job_finish_and_invalidate(j, JOB_SKIPPED, true, false); |
60f067b4 | 727 | else if (r == -ENOEXEC) |
aa27b158 | 728 | r = job_finish_and_invalidate(j, JOB_INVALID, true, false); |
f47781d8 | 729 | else if (r == -EPROTO) |
aa27b158 | 730 | r = job_finish_and_invalidate(j, JOB_ASSERT, true, false); |
e3bff60a | 731 | else if (r == -EOPNOTSUPP) |
aa27b158 | 732 | r = job_finish_and_invalidate(j, JOB_UNSUPPORTED, true, false); |
2897b343 MP |
733 | else if (r == -ENOLINK) |
734 | r = job_finish_and_invalidate(j, JOB_DEPENDENCY, true, false); | |
b012e921 MB |
735 | else if (r == -ESTALE) |
736 | r = job_finish_and_invalidate(j, JOB_ONCE, true, false); | |
e735f4d4 | 737 | else if (r < 0) |
aa27b158 | 738 | r = job_finish_and_invalidate(j, JOB_FAILED, true, false); |
663996b3 MS |
739 | } |
740 | ||
741 | return r; | |
742 | } | |
743 | ||
6e866b33 | 744 | _pure_ static const char *job_get_done_status_message_format(Unit *u, JobType t, JobResult result) { |
db2df898 | 745 | |
7035cd9e MP |
746 | static const char *const generic_finished_start_job[_JOB_RESULT_MAX] = { |
747 | [JOB_DONE] = "Started %s.", | |
748 | [JOB_TIMEOUT] = "Timed out starting %s.", | |
749 | [JOB_FAILED] = "Failed to start %s.", | |
750 | [JOB_DEPENDENCY] = "Dependency failed for %s.", | |
751 | [JOB_ASSERT] = "Assertion failed for %s.", | |
752 | [JOB_UNSUPPORTED] = "Starting of %s not supported.", | |
2897b343 | 753 | [JOB_COLLECTED] = "Unnecessary job for %s was removed.", |
b012e921 | 754 | [JOB_ONCE] = "Unit %s has been started before and cannot be started again." |
7035cd9e MP |
755 | }; |
756 | static const char *const generic_finished_stop_job[_JOB_RESULT_MAX] = { | |
757 | [JOB_DONE] = "Stopped %s.", | |
758 | [JOB_FAILED] = "Stopped (with error) %s.", | |
aa27b158 | 759 | [JOB_TIMEOUT] = "Timed out stopping %s.", |
7035cd9e MP |
760 | }; |
761 | static const char *const generic_finished_reload_job[_JOB_RESULT_MAX] = { | |
762 | [JOB_DONE] = "Reloaded %s.", | |
763 | [JOB_FAILED] = "Reload failed for %s.", | |
764 | [JOB_TIMEOUT] = "Timed out reloading %s.", | |
765 | }; | |
766 | /* When verify-active detects the unit is inactive, report it. | |
767 | * Most likely a DEPEND warning from a requisiting unit will | |
768 | * occur next and it's nice to see what was requisited. */ | |
769 | static const char *const generic_finished_verify_active_job[_JOB_RESULT_MAX] = { | |
770 | [JOB_SKIPPED] = "%s is not active.", | |
771 | }; | |
663996b3 | 772 | |
db2df898 MP |
773 | const char *format; |
774 | ||
663996b3 MS |
775 | assert(u); |
776 | assert(t >= 0); | |
777 | assert(t < _JOB_TYPE_MAX); | |
778 | ||
db2df898 | 779 | if (IN_SET(t, JOB_START, JOB_STOP, JOB_RESTART)) { |
6e866b33 MB |
780 | format = t == JOB_START ? |
781 | UNIT_VTABLE(u)->status_message_formats.finished_start_job[result] : | |
782 | UNIT_VTABLE(u)->status_message_formats.finished_stop_job[result]; | |
783 | if (format) | |
784 | return format; | |
7035cd9e | 785 | } |
663996b3 | 786 | |
7035cd9e | 787 | /* Return generic strings */ |
663996b3 | 788 | if (t == JOB_START) |
7035cd9e | 789 | return generic_finished_start_job[result]; |
f5e65279 | 790 | else if (IN_SET(t, JOB_STOP, JOB_RESTART)) |
7035cd9e MP |
791 | return generic_finished_stop_job[result]; |
792 | else if (t == JOB_RELOAD) | |
793 | return generic_finished_reload_job[result]; | |
794 | else if (t == JOB_VERIFY_ACTIVE) | |
795 | return generic_finished_verify_active_job[result]; | |
663996b3 MS |
796 | |
797 | return NULL; | |
798 | } | |
799 | ||
81c58355 MB |
800 | static const struct { |
801 | const char *color, *word; | |
6e866b33 | 802 | } job_print_done_status_messages[_JOB_RESULT_MAX] = { |
98393f85 | 803 | [JOB_DONE] = { ANSI_OK_COLOR, " OK " }, |
81c58355 MB |
804 | [JOB_TIMEOUT] = { ANSI_HIGHLIGHT_RED, " TIME " }, |
805 | [JOB_FAILED] = { ANSI_HIGHLIGHT_RED, "FAILED" }, | |
806 | [JOB_DEPENDENCY] = { ANSI_HIGHLIGHT_YELLOW, "DEPEND" }, | |
807 | [JOB_SKIPPED] = { ANSI_HIGHLIGHT, " INFO " }, | |
808 | [JOB_ASSERT] = { ANSI_HIGHLIGHT_YELLOW, "ASSERT" }, | |
809 | [JOB_UNSUPPORTED] = { ANSI_HIGHLIGHT_YELLOW, "UNSUPP" }, | |
810 | /* JOB_COLLECTED */ | |
b012e921 | 811 | [JOB_ONCE] = { ANSI_HIGHLIGHT_RED, " ONCE " }, |
81c58355 | 812 | }; |
663996b3 | 813 | |
6e866b33 | 814 | static void job_print_done_status_message(Unit *u, JobType t, JobResult result) { |
db2df898 | 815 | const char *format; |
aa27b158 | 816 | const char *status; |
db2df898 | 817 | |
663996b3 MS |
818 | assert(u); |
819 | assert(t >= 0); | |
820 | assert(t < _JOB_TYPE_MAX); | |
821 | ||
db2df898 MP |
822 | /* Reload status messages have traditionally not been printed to console. */ |
823 | if (t == JOB_RELOAD) | |
824 | return; | |
825 | ||
6e866b33 MB |
826 | /* No message if the job did not actually do anything due to failed condition. */ |
827 | if (t == JOB_START && result == JOB_DONE && !u->condition_result) | |
81c58355 MB |
828 | return; |
829 | ||
6e866b33 MB |
830 | if (!job_print_done_status_messages[result].word) |
831 | return; | |
832 | ||
833 | format = job_get_done_status_message_format(u, t, result); | |
7035cd9e MP |
834 | if (!format) |
835 | return; | |
663996b3 | 836 | |
aa27b158 | 837 | if (log_get_show_color()) |
6e866b33 MB |
838 | status = strjoina(job_print_done_status_messages[result].color, |
839 | job_print_done_status_messages[result].word, | |
81c58355 | 840 | ANSI_NORMAL); |
aa27b158 | 841 | else |
6e866b33 | 842 | status = job_print_done_status_messages[result].word; |
aa27b158 | 843 | |
7035cd9e MP |
844 | if (result != JOB_DONE) |
845 | manager_flip_auto_status(u->manager, true); | |
663996b3 | 846 | |
60f067b4 | 847 | DISABLE_WARNING_FORMAT_NONLITERAL; |
aa27b158 | 848 | unit_status_printf(u, status, format); |
7035cd9e | 849 | REENABLE_WARNING; |
60f067b4 | 850 | |
7035cd9e | 851 | if (t == JOB_START && result == JOB_FAILED) { |
db2df898 | 852 | _cleanup_free_ char *quoted; |
663996b3 | 853 | |
81c58355 | 854 | quoted = shell_maybe_quote(u->id, ESCAPE_BACKSLASH); |
db2df898 | 855 | manager_status_printf(u->manager, STATUS_TYPE_NORMAL, NULL, "See 'systemctl status %s' for details.", strna(quoted)); |
663996b3 MS |
856 | } |
857 | } | |
858 | ||
6e866b33 | 859 | static void job_log_done_status_message(Unit *u, uint32_t job_id, JobType t, JobResult result) { |
2897b343 | 860 | const char *format, *mid; |
663996b3 | 861 | char buf[LINE_MAX]; |
7035cd9e MP |
862 | static const int job_result_log_level[_JOB_RESULT_MAX] = { |
863 | [JOB_DONE] = LOG_INFO, | |
864 | [JOB_CANCELED] = LOG_INFO, | |
865 | [JOB_TIMEOUT] = LOG_ERR, | |
866 | [JOB_FAILED] = LOG_ERR, | |
867 | [JOB_DEPENDENCY] = LOG_WARNING, | |
868 | [JOB_SKIPPED] = LOG_NOTICE, | |
869 | [JOB_INVALID] = LOG_INFO, | |
870 | [JOB_ASSERT] = LOG_WARNING, | |
871 | [JOB_UNSUPPORTED] = LOG_WARNING, | |
2897b343 | 872 | [JOB_COLLECTED] = LOG_INFO, |
b012e921 | 873 | [JOB_ONCE] = LOG_ERR, |
7035cd9e | 874 | }; |
663996b3 MS |
875 | |
876 | assert(u); | |
877 | assert(t >= 0); | |
878 | assert(t < _JOB_TYPE_MAX); | |
879 | ||
81c58355 MB |
880 | /* Skip printing if output goes to the console, and job_print_status_message() |
881 | will actually print something to the console. */ | |
6e866b33 MB |
882 | if (log_on_console() && job_print_done_status_messages[result].word) |
883 | return; | |
884 | ||
885 | /* Show condition check message if the job did not actually do anything due to failed condition. */ | |
886 | if (t == JOB_START && result == JOB_DONE && !u->condition_result) { | |
887 | log_struct(LOG_INFO, | |
888 | "MESSAGE=Condition check resulted in %s being skipped.", unit_description(u), | |
889 | "JOB_ID=%" PRIu32, job_id, | |
890 | "JOB_TYPE=%s", job_type_to_string(t), | |
891 | "JOB_RESULT=%s", job_result_to_string(result), | |
892 | LOG_UNIT_ID(u), | |
893 | LOG_UNIT_INVOCATION_ID(u), | |
894 | "MESSAGE_ID=" SD_MESSAGE_UNIT_STARTED_STR); | |
895 | ||
663996b3 | 896 | return; |
6e866b33 | 897 | } |
663996b3 | 898 | |
6e866b33 | 899 | format = job_get_done_status_message_format(u, t, result); |
663996b3 MS |
900 | if (!format) |
901 | return; | |
902 | ||
98393f85 MB |
903 | /* The description might be longer than the buffer, but that's OK, |
904 | * we'll just truncate it here. Note that we use snprintf() rather than | |
905 | * xsprintf() on purpose here: we are fine with truncation and don't | |
906 | * consider that an error. */ | |
60f067b4 | 907 | DISABLE_WARNING_FORMAT_NONLITERAL; |
98393f85 | 908 | (void) snprintf(buf, sizeof(buf), format, unit_description(u)); |
60f067b4 | 909 | REENABLE_WARNING; |
663996b3 | 910 | |
db2df898 MP |
911 | switch (t) { |
912 | ||
913 | case JOB_START: | |
2897b343 MP |
914 | if (result == JOB_DONE) |
915 | mid = "MESSAGE_ID=" SD_MESSAGE_UNIT_STARTED_STR; | |
916 | else | |
917 | mid = "MESSAGE_ID=" SD_MESSAGE_UNIT_FAILED_STR; | |
db2df898 MP |
918 | break; |
919 | ||
920 | case JOB_RELOAD: | |
2897b343 | 921 | mid = "MESSAGE_ID=" SD_MESSAGE_UNIT_RELOADED_STR; |
db2df898 MP |
922 | break; |
923 | ||
924 | case JOB_STOP: | |
925 | case JOB_RESTART: | |
2897b343 | 926 | mid = "MESSAGE_ID=" SD_MESSAGE_UNIT_STOPPED_STR; |
db2df898 MP |
927 | break; |
928 | ||
929 | default: | |
7035cd9e | 930 | log_struct(job_result_log_level[result], |
e3bff60a | 931 | LOG_MESSAGE("%s", buf), |
6e866b33 | 932 | "JOB_ID=%" PRIu32, job_id, |
f5e65279 MB |
933 | "JOB_TYPE=%s", job_type_to_string(t), |
934 | "JOB_RESULT=%s", job_result_to_string(result), | |
81c58355 | 935 | LOG_UNIT_ID(u), |
b012e921 | 936 | LOG_UNIT_INVOCATION_ID(u)); |
7035cd9e MP |
937 | return; |
938 | } | |
663996b3 | 939 | |
7035cd9e | 940 | log_struct(job_result_log_level[result], |
7035cd9e | 941 | LOG_MESSAGE("%s", buf), |
6e866b33 | 942 | "JOB_ID=%" PRIu32, job_id, |
f5e65279 MB |
943 | "JOB_TYPE=%s", job_type_to_string(t), |
944 | "JOB_RESULT=%s", job_result_to_string(result), | |
81c58355 | 945 | LOG_UNIT_ID(u), |
f5e65279 | 946 | LOG_UNIT_INVOCATION_ID(u), |
b012e921 | 947 | mid); |
7035cd9e | 948 | } |
663996b3 | 949 | |
6e866b33 | 950 | static void job_emit_done_status_message(Unit *u, uint32_t job_id, JobType t, JobResult result) { |
f5e65279 | 951 | assert(u); |
7035cd9e | 952 | |
6e866b33 MB |
953 | job_log_done_status_message(u, job_id, t, result); |
954 | job_print_done_status_message(u, t, result); | |
e3bff60a MP |
955 | } |
956 | ||
957 | static void job_fail_dependencies(Unit *u, UnitDependency d) { | |
958 | Unit *other; | |
959 | Iterator i; | |
52ad194e | 960 | void *v; |
e3bff60a MP |
961 | |
962 | assert(u); | |
963 | ||
52ad194e | 964 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[d], i) { |
e3bff60a MP |
965 | Job *j = other->job; |
966 | ||
967 | if (!j) | |
968 | continue; | |
969 | if (!IN_SET(j->type, JOB_START, JOB_VERIFY_ACTIVE)) | |
970 | continue; | |
971 | ||
aa27b158 | 972 | job_finish_and_invalidate(j, JOB_DEPENDENCY, true, false); |
e3bff60a | 973 | } |
663996b3 | 974 | } |
663996b3 | 975 | |
aa27b158 | 976 | int job_finish_and_invalidate(Job *j, JobResult result, bool recursive, bool already) { |
663996b3 MS |
977 | Unit *u; |
978 | Unit *other; | |
979 | JobType t; | |
980 | Iterator i; | |
52ad194e | 981 | void *v; |
663996b3 MS |
982 | |
983 | assert(j); | |
984 | assert(j->installed); | |
985 | assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION); | |
986 | ||
987 | u = j->unit; | |
988 | t = j->type; | |
989 | ||
990 | j->result = result; | |
991 | ||
6e866b33 | 992 | log_unit_debug(u, "Job %" PRIu32 " %s/%s finished, result=%s", j->id, u->id, job_type_to_string(t), job_result_to_string(result)); |
663996b3 | 993 | |
aa27b158 MP |
994 | /* If this job did nothing to respective unit we don't log the status message */ |
995 | if (!already) | |
6e866b33 | 996 | job_emit_done_status_message(u, j->id, t, result); |
663996b3 | 997 | |
663996b3 MS |
998 | /* Patch restart jobs so that they become normal start jobs */ |
999 | if (result == JOB_DONE && t == JOB_RESTART) { | |
1000 | ||
1001 | job_change_type(j, JOB_START); | |
e735f4d4 | 1002 | job_set_state(j, JOB_WAITING); |
663996b3 | 1003 | |
52ad194e | 1004 | job_add_to_dbus_queue(j); |
663996b3 | 1005 | job_add_to_run_queue(j); |
2897b343 | 1006 | job_add_to_gc_queue(j); |
663996b3 MS |
1007 | |
1008 | goto finish; | |
1009 | } | |
1010 | ||
f5e65279 | 1011 | if (IN_SET(result, JOB_FAILED, JOB_INVALID)) |
aa27b158 | 1012 | j->manager->n_failed_jobs++; |
663996b3 MS |
1013 | |
1014 | job_uninstall(j); | |
6e866b33 | 1015 | job_free(j); |
663996b3 MS |
1016 | |
1017 | /* Fail depending jobs on failure */ | |
1018 | if (result != JOB_DONE && recursive) { | |
e3bff60a MP |
1019 | if (IN_SET(t, JOB_START, JOB_VERIFY_ACTIVE)) { |
1020 | job_fail_dependencies(u, UNIT_REQUIRED_BY); | |
1021 | job_fail_dependencies(u, UNIT_REQUISITE_OF); | |
1022 | job_fail_dependencies(u, UNIT_BOUND_BY); | |
e3bff60a MP |
1023 | } else if (t == JOB_STOP) |
1024 | job_fail_dependencies(u, UNIT_CONFLICTED_BY); | |
663996b3 MS |
1025 | } |
1026 | ||
1027 | /* Trigger OnFailure dependencies that are not generated by | |
1028 | * the unit itself. We don't treat JOB_CANCELED as failure in | |
1029 | * this context. And JOB_FAILURE is already handled by the | |
1030 | * unit itself. */ | |
f5e65279 | 1031 | if (IN_SET(result, JOB_TIMEOUT, JOB_DEPENDENCY)) { |
e3bff60a MP |
1032 | log_struct(LOG_NOTICE, |
1033 | "JOB_TYPE=%s", job_type_to_string(t), | |
1034 | "JOB_RESULT=%s", job_result_to_string(result), | |
1035 | LOG_UNIT_ID(u), | |
1036 | LOG_UNIT_MESSAGE(u, "Job %s/%s failed with result '%s'.", | |
f47781d8 MP |
1037 | u->id, |
1038 | job_type_to_string(t), | |
b012e921 | 1039 | job_result_to_string(result))); |
663996b3 MS |
1040 | |
1041 | unit_start_on_failure(u); | |
1042 | } | |
1043 | ||
1044 | unit_trigger_notify(u); | |
1045 | ||
1046 | finish: | |
1047 | /* Try to start the next jobs that can be started */ | |
52ad194e | 1048 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_AFTER], i) |
2897b343 | 1049 | if (other->job) { |
663996b3 | 1050 | job_add_to_run_queue(other->job); |
2897b343 MP |
1051 | job_add_to_gc_queue(other->job); |
1052 | } | |
52ad194e | 1053 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_BEFORE], i) |
2897b343 | 1054 | if (other->job) { |
663996b3 | 1055 | job_add_to_run_queue(other->job); |
2897b343 MP |
1056 | job_add_to_gc_queue(other->job); |
1057 | } | |
663996b3 MS |
1058 | |
1059 | manager_check_finished(u->manager); | |
1060 | ||
1061 | return 0; | |
1062 | } | |
1063 | ||
60f067b4 JS |
1064 | static int job_dispatch_timer(sd_event_source *s, uint64_t monotonic, void *userdata) { |
1065 | Job *j = userdata; | |
5eef597e | 1066 | Unit *u; |
663996b3 | 1067 | |
60f067b4 JS |
1068 | assert(j); |
1069 | assert(s == j->timer_event_source); | |
663996b3 | 1070 | |
e3bff60a | 1071 | log_unit_warning(j->unit, "Job %s/%s timed out.", j->unit->id, job_type_to_string(j->type)); |
663996b3 | 1072 | |
5eef597e | 1073 | u = j->unit; |
aa27b158 | 1074 | job_finish_and_invalidate(j, JOB_TIMEOUT, true, false); |
5eef597e | 1075 | |
6e866b33 MB |
1076 | emergency_action(u->manager, u->job_timeout_action, |
1077 | EMERGENCY_ACTION_IS_WATCHDOG|EMERGENCY_ACTION_WARN, | |
1078 | u->job_timeout_reboot_arg, -1, "job timed out"); | |
5eef597e | 1079 | |
60f067b4 JS |
1080 | return 0; |
1081 | } | |
663996b3 | 1082 | |
81c58355 | 1083 | int job_start_timer(Job *j, bool job_running) { |
60f067b4 | 1084 | int r; |
81c58355 | 1085 | usec_t timeout_time, old_timeout_time; |
663996b3 | 1086 | |
81c58355 MB |
1087 | if (job_running) { |
1088 | j->begin_running_usec = now(CLOCK_MONOTONIC); | |
663996b3 | 1089 | |
81c58355 MB |
1090 | if (j->unit->job_running_timeout == USEC_INFINITY) |
1091 | return 0; | |
663996b3 | 1092 | |
81c58355 MB |
1093 | timeout_time = usec_add(j->begin_running_usec, j->unit->job_running_timeout); |
1094 | ||
1095 | if (j->timer_event_source) { | |
1096 | /* Update only if JobRunningTimeoutSec= results in earlier timeout */ | |
1097 | r = sd_event_source_get_time(j->timer_event_source, &old_timeout_time); | |
1098 | if (r < 0) | |
1099 | return r; | |
1100 | ||
1101 | if (old_timeout_time <= timeout_time) | |
1102 | return 0; | |
1103 | ||
1104 | return sd_event_source_set_time(j->timer_event_source, timeout_time); | |
1105 | } | |
1106 | } else { | |
1107 | if (j->timer_event_source) | |
1108 | return 0; | |
1109 | ||
1110 | j->begin_usec = now(CLOCK_MONOTONIC); | |
1111 | ||
1112 | if (j->unit->job_timeout == USEC_INFINITY) | |
1113 | return 0; | |
1114 | ||
1115 | timeout_time = usec_add(j->begin_usec, j->unit->job_timeout); | |
1116 | } | |
663996b3 | 1117 | |
60f067b4 JS |
1118 | r = sd_event_add_time( |
1119 | j->manager->event, | |
1120 | &j->timer_event_source, | |
1121 | CLOCK_MONOTONIC, | |
81c58355 | 1122 | timeout_time, 0, |
60f067b4 JS |
1123 | job_dispatch_timer, j); |
1124 | if (r < 0) | |
1125 | return r; | |
663996b3 | 1126 | |
e3bff60a MP |
1127 | (void) sd_event_source_set_description(j->timer_event_source, "job-start"); |
1128 | ||
60f067b4 | 1129 | return 0; |
663996b3 MS |
1130 | } |
1131 | ||
1132 | void job_add_to_run_queue(Job *j) { | |
6e866b33 MB |
1133 | int r; |
1134 | ||
663996b3 MS |
1135 | assert(j); |
1136 | assert(j->installed); | |
1137 | ||
1138 | if (j->in_run_queue) | |
1139 | return; | |
1140 | ||
6e866b33 MB |
1141 | if (!j->manager->run_queue) { |
1142 | r = sd_event_source_set_enabled(j->manager->run_queue_event_source, SD_EVENT_ONESHOT); | |
1143 | if (r < 0) | |
1144 | log_warning_errno(r, "Failed to enable job run queue event source, ignoring: %m"); | |
1145 | } | |
60f067b4 JS |
1146 | |
1147 | LIST_PREPEND(run_queue, j->manager->run_queue, j); | |
663996b3 MS |
1148 | j->in_run_queue = true; |
1149 | } | |
1150 | ||
1151 | void job_add_to_dbus_queue(Job *j) { | |
1152 | assert(j); | |
1153 | assert(j->installed); | |
1154 | ||
1155 | if (j->in_dbus_queue) | |
1156 | return; | |
1157 | ||
1158 | /* We don't check if anybody is subscribed here, since this | |
1159 | * job might just have been created and not yet assigned to a | |
1160 | * connection/client. */ | |
1161 | ||
60f067b4 | 1162 | LIST_PREPEND(dbus_queue, j->manager->dbus_job_queue, j); |
663996b3 MS |
1163 | j->in_dbus_queue = true; |
1164 | } | |
1165 | ||
1166 | char *job_dbus_path(Job *j) { | |
1167 | char *p; | |
1168 | ||
1169 | assert(j); | |
1170 | ||
60f067b4 | 1171 | if (asprintf(&p, "/org/freedesktop/systemd1/job/%"PRIu32, j->id) < 0) |
663996b3 MS |
1172 | return NULL; |
1173 | ||
1174 | return p; | |
1175 | } | |
1176 | ||
8a584da2 MP |
1177 | int job_serialize(Job *j, FILE *f) { |
1178 | assert(j); | |
1179 | assert(f); | |
1180 | ||
6e866b33 MB |
1181 | (void) serialize_item_format(f, "job-id", "%u", j->id); |
1182 | (void) serialize_item(f, "job-type", job_type_to_string(j->type)); | |
1183 | (void) serialize_item(f, "job-state", job_state_to_string(j->state)); | |
1184 | (void) serialize_bool(f, "job-irreversible", j->irreversible); | |
1185 | (void) serialize_bool(f, "job-sent-dbus-new-signal", j->sent_dbus_new_signal); | |
1186 | (void) serialize_bool(f, "job-ignore-order", j->ignore_order); | |
60f067b4 JS |
1187 | |
1188 | if (j->begin_usec > 0) | |
6e866b33 | 1189 | (void) serialize_usec(f, "job-begin", j->begin_usec); |
81c58355 | 1190 | if (j->begin_running_usec > 0) |
6e866b33 | 1191 | (void) serialize_usec(f, "job-begin-running", j->begin_running_usec); |
60f067b4 | 1192 | |
2897b343 | 1193 | bus_track_serialize(j->bus_track, f, "subscribed"); |
663996b3 MS |
1194 | |
1195 | /* End marker */ | |
1196 | fputc('\n', f); | |
1197 | return 0; | |
1198 | } | |
1199 | ||
8a584da2 | 1200 | int job_deserialize(Job *j, FILE *f) { |
6e866b33 MB |
1201 | int r; |
1202 | ||
60f067b4 | 1203 | assert(j); |
8a584da2 | 1204 | assert(f); |
60f067b4 | 1205 | |
663996b3 | 1206 | for (;;) { |
6e866b33 MB |
1207 | _cleanup_free_ char *line = NULL; |
1208 | char *l, *v; | |
663996b3 MS |
1209 | size_t k; |
1210 | ||
6e866b33 MB |
1211 | r = read_line(f, LONG_LINE_MAX, &line); |
1212 | if (r < 0) | |
1213 | return log_error_errno(r, "Failed to read serialization line: %m"); | |
1214 | if (r == 0) | |
1215 | return 0; | |
663996b3 | 1216 | |
663996b3 MS |
1217 | l = strstrip(line); |
1218 | ||
1219 | /* End marker */ | |
6e866b33 | 1220 | if (isempty(l)) |
663996b3 MS |
1221 | return 0; |
1222 | ||
1223 | k = strcspn(l, "="); | |
1224 | ||
1225 | if (l[k] == '=') { | |
1226 | l[k] = 0; | |
1227 | v = l+k+1; | |
1228 | } else | |
1229 | v = l+k; | |
1230 | ||
1231 | if (streq(l, "job-id")) { | |
60f067b4 | 1232 | |
663996b3 | 1233 | if (safe_atou32(v, &j->id) < 0) |
6e866b33 | 1234 | log_debug("Failed to parse job id value: %s", v); |
60f067b4 | 1235 | |
663996b3 | 1236 | } else if (streq(l, "job-type")) { |
60f067b4 JS |
1237 | JobType t; |
1238 | ||
1239 | t = job_type_from_string(v); | |
663996b3 | 1240 | if (t < 0) |
6e866b33 | 1241 | log_debug("Failed to parse job type: %s", v); |
663996b3 | 1242 | else if (t >= _JOB_TYPE_MAX_IN_TRANSACTION) |
6e866b33 | 1243 | log_debug("Cannot deserialize job of type: %s", v); |
663996b3 MS |
1244 | else |
1245 | j->type = t; | |
60f067b4 | 1246 | |
663996b3 | 1247 | } else if (streq(l, "job-state")) { |
60f067b4 JS |
1248 | JobState s; |
1249 | ||
1250 | s = job_state_from_string(v); | |
663996b3 | 1251 | if (s < 0) |
6e866b33 | 1252 | log_debug("Failed to parse job state: %s", v); |
663996b3 | 1253 | else |
e735f4d4 | 1254 | job_set_state(j, s); |
60f067b4 | 1255 | |
663996b3 | 1256 | } else if (streq(l, "job-irreversible")) { |
60f067b4 JS |
1257 | int b; |
1258 | ||
1259 | b = parse_boolean(v); | |
663996b3 | 1260 | if (b < 0) |
6e866b33 | 1261 | log_debug("Failed to parse job irreversible flag: %s", v); |
663996b3 MS |
1262 | else |
1263 | j->irreversible = j->irreversible || b; | |
60f067b4 | 1264 | |
663996b3 | 1265 | } else if (streq(l, "job-sent-dbus-new-signal")) { |
60f067b4 JS |
1266 | int b; |
1267 | ||
1268 | b = parse_boolean(v); | |
663996b3 | 1269 | if (b < 0) |
6e866b33 | 1270 | log_debug("Failed to parse job sent_dbus_new_signal flag: %s", v); |
663996b3 MS |
1271 | else |
1272 | j->sent_dbus_new_signal = j->sent_dbus_new_signal || b; | |
60f067b4 | 1273 | |
663996b3 | 1274 | } else if (streq(l, "job-ignore-order")) { |
60f067b4 JS |
1275 | int b; |
1276 | ||
1277 | b = parse_boolean(v); | |
663996b3 | 1278 | if (b < 0) |
6e866b33 | 1279 | log_debug("Failed to parse job ignore_order flag: %s", v); |
663996b3 MS |
1280 | else |
1281 | j->ignore_order = j->ignore_order || b; | |
60f067b4 | 1282 | |
6e866b33 MB |
1283 | } else if (streq(l, "job-begin")) |
1284 | (void) deserialize_usec(v, &j->begin_usec); | |
81c58355 | 1285 | |
6e866b33 MB |
1286 | else if (streq(l, "job-begin-running")) |
1287 | (void) deserialize_usec(v, &j->begin_running_usec); | |
60f067b4 | 1288 | |
6e866b33 | 1289 | else if (streq(l, "subscribed")) { |
5eef597e | 1290 | if (strv_extend(&j->deserialized_clients, v) < 0) |
6e866b33 MB |
1291 | return log_oom(); |
1292 | } else | |
1293 | log_debug("Unknown job serialization key: %s", l); | |
663996b3 MS |
1294 | } |
1295 | } | |
1296 | ||
1297 | int job_coldplug(Job *j) { | |
60f067b4 | 1298 | int r; |
81c58355 | 1299 | usec_t timeout_time = USEC_INFINITY; |
663996b3 | 1300 | |
60f067b4 JS |
1301 | assert(j); |
1302 | ||
1303 | /* After deserialization is complete and the bus connection | |
1304 | * set up again, let's start watching our subscribers again */ | |
2897b343 | 1305 | (void) bus_job_coldplug_bus_track(j); |
60f067b4 JS |
1306 | |
1307 | if (j->state == JOB_WAITING) | |
1308 | job_add_to_run_queue(j); | |
1309 | ||
2897b343 MP |
1310 | /* Maybe due to new dependencies we don't actually need this job anymore? */ |
1311 | job_add_to_gc_queue(j); | |
1312 | ||
81c58355 MB |
1313 | /* Create timer only when job began or began running and the respective timeout is finite. |
1314 | * Follow logic of job_start_timer() if both timeouts are finite */ | |
1315 | if (j->begin_usec == 0) | |
1316 | return 0; | |
1317 | ||
1318 | if (j->unit->job_timeout != USEC_INFINITY) | |
1319 | timeout_time = usec_add(j->begin_usec, j->unit->job_timeout); | |
1320 | ||
1321 | if (j->begin_running_usec > 0 && j->unit->job_running_timeout != USEC_INFINITY) | |
1322 | timeout_time = MIN(timeout_time, usec_add(j->begin_running_usec, j->unit->job_running_timeout)); | |
1323 | ||
1324 | if (timeout_time == USEC_INFINITY) | |
663996b3 MS |
1325 | return 0; |
1326 | ||
4c89c718 | 1327 | j->timer_event_source = sd_event_source_unref(j->timer_event_source); |
663996b3 | 1328 | |
60f067b4 JS |
1329 | r = sd_event_add_time( |
1330 | j->manager->event, | |
1331 | &j->timer_event_source, | |
1332 | CLOCK_MONOTONIC, | |
81c58355 | 1333 | timeout_time, 0, |
60f067b4 JS |
1334 | job_dispatch_timer, j); |
1335 | if (r < 0) | |
f47781d8 | 1336 | log_debug_errno(r, "Failed to restart timeout for job: %m"); |
60f067b4 | 1337 | |
e3bff60a MP |
1338 | (void) sd_event_source_set_description(j->timer_event_source, "job-timeout"); |
1339 | ||
60f067b4 | 1340 | return r; |
663996b3 MS |
1341 | } |
1342 | ||
1343 | void job_shutdown_magic(Job *j) { | |
1344 | assert(j); | |
1345 | ||
1346 | /* The shutdown target gets some special treatment here: we | |
1347 | * tell the kernel to begin with flushing its disk caches, to | |
1348 | * optimize shutdown time a bit. Ideally we wouldn't hardcode | |
1349 | * this magic into PID 1. However all other processes aren't | |
1350 | * options either since they'd exit much sooner than PID 1 and | |
1351 | * asynchronous sync() would cause their exit to be | |
1352 | * delayed. */ | |
1353 | ||
14228c0d | 1354 | if (j->type != JOB_START) |
663996b3 MS |
1355 | return; |
1356 | ||
aa27b158 | 1357 | if (!MANAGER_IS_SYSTEM(j->unit->manager)) |
14228c0d MB |
1358 | return; |
1359 | ||
1360 | if (!unit_has_name(j->unit, SPECIAL_SHUTDOWN_TARGET)) | |
663996b3 MS |
1361 | return; |
1362 | ||
60f067b4 JS |
1363 | /* In case messages on console has been disabled on boot */ |
1364 | j->unit->manager->no_console_output = false; | |
1365 | ||
6300502b | 1366 | if (detect_container() > 0) |
663996b3 MS |
1367 | return; |
1368 | ||
1d42b86d | 1369 | (void) asynchronous_sync(NULL); |
663996b3 MS |
1370 | } |
1371 | ||
4c89c718 MP |
1372 | int job_get_timeout(Job *j, usec_t *timeout) { |
1373 | usec_t x = USEC_INFINITY, y = USEC_INFINITY; | |
60f067b4 | 1374 | Unit *u = j->unit; |
4c89c718 | 1375 | int r; |
60f067b4 JS |
1376 | |
1377 | assert(u); | |
1378 | ||
1379 | if (j->timer_event_source) { | |
1380 | r = sd_event_source_get_time(j->timer_event_source, &x); | |
1381 | if (r < 0) | |
1382 | return r; | |
60f067b4 JS |
1383 | } |
1384 | ||
1385 | if (UNIT_VTABLE(u)->get_timeout) { | |
4c89c718 MP |
1386 | r = UNIT_VTABLE(u)->get_timeout(u, &y); |
1387 | if (r < 0) | |
1388 | return r; | |
60f067b4 JS |
1389 | } |
1390 | ||
4c89c718 | 1391 | if (x == USEC_INFINITY && y == USEC_INFINITY) |
60f067b4 JS |
1392 | return 0; |
1393 | ||
1394 | *timeout = MIN(x, y); | |
60f067b4 JS |
1395 | return 1; |
1396 | } | |
1397 | ||
98393f85 | 1398 | bool job_may_gc(Job *j) { |
2897b343 MP |
1399 | Unit *other; |
1400 | Iterator i; | |
52ad194e | 1401 | void *v; |
2897b343 MP |
1402 | |
1403 | assert(j); | |
1404 | ||
1405 | /* Checks whether this job should be GC'ed away. We only do this for jobs of units that have no effect on their | |
98393f85 MB |
1406 | * own and just track external state. For now the only unit type that qualifies for this are .device units. |
1407 | * Returns true if the job can be collected. */ | |
2897b343 MP |
1408 | |
1409 | if (!UNIT_VTABLE(j->unit)->gc_jobs) | |
98393f85 | 1410 | return false; |
2897b343 MP |
1411 | |
1412 | if (sd_bus_track_count(j->bus_track) > 0) | |
98393f85 | 1413 | return false; |
2897b343 MP |
1414 | |
1415 | /* FIXME: So this is a bit ugly: for now we don't properly track references made via private bus connections | |
1416 | * (because it's nasty, as sd_bus_track doesn't apply to it). We simply remember that the job was once | |
1417 | * referenced by one, and reset this whenever we notice that no private bus connections are around. This means | |
1418 | * the GC is a bit too conservative when it comes to jobs created by private bus connections. */ | |
1419 | if (j->ref_by_private_bus) { | |
1420 | if (set_isempty(j->unit->manager->private_buses)) | |
1421 | j->ref_by_private_bus = false; | |
1422 | else | |
98393f85 | 1423 | return false; |
2897b343 MP |
1424 | } |
1425 | ||
1426 | if (j->type == JOB_NOP) | |
98393f85 | 1427 | return false; |
2897b343 MP |
1428 | |
1429 | /* If a job is ordered after ours, and is to be started, then it needs to wait for us, regardless if we stop or | |
1430 | * start, hence let's not GC in that case. */ | |
52ad194e | 1431 | HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_BEFORE], i) { |
2897b343 MP |
1432 | if (!other->job) |
1433 | continue; | |
1434 | ||
1435 | if (other->job->ignore_order) | |
1436 | continue; | |
1437 | ||
1438 | if (IN_SET(other->job->type, JOB_START, JOB_VERIFY_ACTIVE, JOB_RELOAD)) | |
98393f85 | 1439 | return false; |
2897b343 MP |
1440 | } |
1441 | ||
81c58355 MB |
1442 | /* If we are going down, but something else is ordered After= us, then it needs to wait for us */ |
1443 | if (IN_SET(j->type, JOB_STOP, JOB_RESTART)) | |
52ad194e | 1444 | HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_AFTER], i) { |
2897b343 MP |
1445 | if (!other->job) |
1446 | continue; | |
1447 | ||
1448 | if (other->job->ignore_order) | |
1449 | continue; | |
1450 | ||
98393f85 | 1451 | return false; |
2897b343 | 1452 | } |
2897b343 MP |
1453 | |
1454 | /* The logic above is kinda the inverse of the job_is_runnable() logic. Specifically, if the job "we" is | |
1455 | * ordered before the job "other": | |
1456 | * | |
1457 | * we start + other start → stay | |
1458 | * we start + other stop → gc | |
1459 | * we stop + other start → stay | |
1460 | * we stop + other stop → gc | |
1461 | * | |
1462 | * "we" are ordered after "other": | |
1463 | * | |
1464 | * we start + other start → gc | |
1465 | * we start + other stop → gc | |
1466 | * we stop + other start → stay | |
1467 | * we stop + other stop → stay | |
2897b343 MP |
1468 | */ |
1469 | ||
98393f85 | 1470 | return true; |
2897b343 MP |
1471 | } |
1472 | ||
1473 | void job_add_to_gc_queue(Job *j) { | |
1474 | assert(j); | |
1475 | ||
1476 | if (j->in_gc_queue) | |
1477 | return; | |
1478 | ||
98393f85 | 1479 | if (!job_may_gc(j)) |
2897b343 MP |
1480 | return; |
1481 | ||
1482 | LIST_PREPEND(gc_queue, j->unit->manager->gc_job_queue, j); | |
1483 | j->in_gc_queue = true; | |
1484 | } | |
1485 | ||
6e866b33 MB |
1486 | static int job_compare(Job * const *a, Job * const *b) { |
1487 | return CMP((*a)->id, (*b)->id); | |
2897b343 MP |
1488 | } |
1489 | ||
1490 | static size_t sort_job_list(Job **list, size_t n) { | |
1491 | Job *previous = NULL; | |
1492 | size_t a, b; | |
1493 | ||
1494 | /* Order by numeric IDs */ | |
6e866b33 | 1495 | typesafe_qsort(list, n, job_compare); |
2897b343 MP |
1496 | |
1497 | /* Filter out duplicates */ | |
1498 | for (a = 0, b = 0; a < n; a++) { | |
1499 | ||
1500 | if (previous == list[a]) | |
1501 | continue; | |
1502 | ||
1503 | previous = list[b++] = list[a]; | |
1504 | } | |
1505 | ||
1506 | return b; | |
1507 | } | |
1508 | ||
1509 | int job_get_before(Job *j, Job*** ret) { | |
1510 | _cleanup_free_ Job** list = NULL; | |
1511 | size_t n = 0, n_allocated = 0; | |
1512 | Unit *other = NULL; | |
1513 | Iterator i; | |
52ad194e | 1514 | void *v; |
2897b343 MP |
1515 | |
1516 | /* Returns a list of all pending jobs that need to finish before this job may be started. */ | |
1517 | ||
1518 | assert(j); | |
1519 | assert(ret); | |
1520 | ||
1521 | if (j->ignore_order) { | |
1522 | *ret = NULL; | |
1523 | return 0; | |
1524 | } | |
1525 | ||
1526 | if (IN_SET(j->type, JOB_START, JOB_VERIFY_ACTIVE, JOB_RELOAD)) { | |
1527 | ||
52ad194e | 1528 | HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_AFTER], i) { |
2897b343 MP |
1529 | if (!other->job) |
1530 | continue; | |
1531 | ||
1532 | if (!GREEDY_REALLOC(list, n_allocated, n+1)) | |
1533 | return -ENOMEM; | |
1534 | list[n++] = other->job; | |
1535 | } | |
1536 | } | |
1537 | ||
52ad194e | 1538 | HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_BEFORE], i) { |
2897b343 MP |
1539 | if (!other->job) |
1540 | continue; | |
1541 | ||
1542 | if (!IN_SET(other->job->type, JOB_STOP, JOB_RESTART)) | |
1543 | continue; | |
1544 | ||
1545 | if (!GREEDY_REALLOC(list, n_allocated, n+1)) | |
1546 | return -ENOMEM; | |
1547 | list[n++] = other->job; | |
1548 | } | |
1549 | ||
1550 | n = sort_job_list(list, n); | |
1551 | ||
b012e921 | 1552 | *ret = TAKE_PTR(list); |
2897b343 MP |
1553 | |
1554 | return (int) n; | |
1555 | } | |
1556 | ||
1557 | int job_get_after(Job *j, Job*** ret) { | |
1558 | _cleanup_free_ Job** list = NULL; | |
1559 | size_t n = 0, n_allocated = 0; | |
1560 | Unit *other = NULL; | |
52ad194e | 1561 | void *v; |
2897b343 MP |
1562 | Iterator i; |
1563 | ||
1564 | assert(j); | |
1565 | assert(ret); | |
1566 | ||
1567 | /* Returns a list of all pending jobs that are waiting for this job to finish. */ | |
1568 | ||
52ad194e | 1569 | HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_BEFORE], i) { |
2897b343 MP |
1570 | if (!other->job) |
1571 | continue; | |
1572 | ||
1573 | if (other->job->ignore_order) | |
1574 | continue; | |
1575 | ||
1576 | if (!IN_SET(other->job->type, JOB_START, JOB_VERIFY_ACTIVE, JOB_RELOAD)) | |
1577 | continue; | |
1578 | ||
1579 | if (!GREEDY_REALLOC(list, n_allocated, n+1)) | |
1580 | return -ENOMEM; | |
1581 | list[n++] = other->job; | |
1582 | } | |
1583 | ||
1584 | if (IN_SET(j->type, JOB_STOP, JOB_RESTART)) { | |
1585 | ||
52ad194e | 1586 | HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_AFTER], i) { |
2897b343 MP |
1587 | if (!other->job) |
1588 | continue; | |
1589 | ||
1590 | if (other->job->ignore_order) | |
1591 | continue; | |
1592 | ||
1593 | if (!GREEDY_REALLOC(list, n_allocated, n+1)) | |
1594 | return -ENOMEM; | |
1595 | list[n++] = other->job; | |
1596 | } | |
1597 | } | |
1598 | ||
1599 | n = sort_job_list(list, n); | |
1600 | ||
b012e921 | 1601 | *ret = TAKE_PTR(list); |
2897b343 MP |
1602 | |
1603 | return (int) n; | |
1604 | } | |
1605 | ||
663996b3 MS |
1606 | static const char* const job_state_table[_JOB_STATE_MAX] = { |
1607 | [JOB_WAITING] = "waiting", | |
2897b343 | 1608 | [JOB_RUNNING] = "running", |
663996b3 MS |
1609 | }; |
1610 | ||
1611 | DEFINE_STRING_TABLE_LOOKUP(job_state, JobState); | |
1612 | ||
1613 | static const char* const job_type_table[_JOB_TYPE_MAX] = { | |
1614 | [JOB_START] = "start", | |
1615 | [JOB_VERIFY_ACTIVE] = "verify-active", | |
1616 | [JOB_STOP] = "stop", | |
1617 | [JOB_RELOAD] = "reload", | |
1618 | [JOB_RELOAD_OR_START] = "reload-or-start", | |
1619 | [JOB_RESTART] = "restart", | |
1620 | [JOB_TRY_RESTART] = "try-restart", | |
4c89c718 | 1621 | [JOB_TRY_RELOAD] = "try-reload", |
663996b3 MS |
1622 | [JOB_NOP] = "nop", |
1623 | }; | |
1624 | ||
1625 | DEFINE_STRING_TABLE_LOOKUP(job_type, JobType); | |
1626 | ||
1627 | static const char* const job_mode_table[_JOB_MODE_MAX] = { | |
1628 | [JOB_FAIL] = "fail", | |
1629 | [JOB_REPLACE] = "replace", | |
1630 | [JOB_REPLACE_IRREVERSIBLY] = "replace-irreversibly", | |
1631 | [JOB_ISOLATE] = "isolate", | |
60f067b4 | 1632 | [JOB_FLUSH] = "flush", |
663996b3 | 1633 | [JOB_IGNORE_DEPENDENCIES] = "ignore-dependencies", |
60f067b4 | 1634 | [JOB_IGNORE_REQUIREMENTS] = "ignore-requirements", |
663996b3 MS |
1635 | }; |
1636 | ||
1637 | DEFINE_STRING_TABLE_LOOKUP(job_mode, JobMode); | |
1638 | ||
1639 | static const char* const job_result_table[_JOB_RESULT_MAX] = { | |
1640 | [JOB_DONE] = "done", | |
1641 | [JOB_CANCELED] = "canceled", | |
1642 | [JOB_TIMEOUT] = "timeout", | |
1643 | [JOB_FAILED] = "failed", | |
1644 | [JOB_DEPENDENCY] = "dependency", | |
60f067b4 JS |
1645 | [JOB_SKIPPED] = "skipped", |
1646 | [JOB_INVALID] = "invalid", | |
f47781d8 | 1647 | [JOB_ASSERT] = "assert", |
e735f4d4 | 1648 | [JOB_UNSUPPORTED] = "unsupported", |
2897b343 | 1649 | [JOB_COLLECTED] = "collected", |
b012e921 | 1650 | [JOB_ONCE] = "once", |
663996b3 MS |
1651 | }; |
1652 | ||
1653 | DEFINE_STRING_TABLE_LOOKUP(job_result, JobResult); | |
4c89c718 MP |
1654 | |
1655 | const char* job_type_to_access_method(JobType t) { | |
1656 | assert(t >= 0); | |
1657 | assert(t < _JOB_TYPE_MAX); | |
1658 | ||
1659 | if (IN_SET(t, JOB_START, JOB_RESTART, JOB_TRY_RESTART)) | |
1660 | return "start"; | |
1661 | else if (t == JOB_STOP) | |
1662 | return "stop"; | |
1663 | else | |
1664 | return "reload"; | |
1665 | } |