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