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