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