]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/job.c
grypt-util: drop two emacs modelines
[thirdparty/systemd.git] / src / core / job.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2010 Lennart Poettering
4 ***/
5
6 #include <errno.h>
7
8 #include "sd-id128.h"
9 #include "sd-messages.h"
10
11 #include "alloc-util.h"
12 #include "async.h"
13 #include "dbus-job.h"
14 #include "dbus.h"
15 #include "escape.h"
16 #include "job.h"
17 #include "log.h"
18 #include "macro.h"
19 #include "parse-util.h"
20 #include "set.h"
21 #include "special.h"
22 #include "stdio-util.h"
23 #include "string-table.h"
24 #include "string-util.h"
25 #include "strv.h"
26 #include "terminal-util.h"
27 #include "unit.h"
28 #include "virt.h"
29
30 Job* job_new_raw(Unit *unit) {
31 Job *j;
32
33 /* used for deserialization */
34
35 assert(unit);
36
37 j = new0(Job, 1);
38 if (!j)
39 return NULL;
40
41 j->manager = unit->manager;
42 j->unit = unit;
43 j->type = _JOB_TYPE_INVALID;
44 j->reloaded = false;
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 job_type_merge_and_collapse(&j->type, other->type, j->unit);
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, 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
1375 return true;
1376 }
1377
1378 void job_add_to_gc_queue(Job *j) {
1379 assert(j);
1380
1381 if (j->in_gc_queue)
1382 return;
1383
1384 if (!job_may_gc(j))
1385 return;
1386
1387 LIST_PREPEND(gc_queue, j->unit->manager->gc_job_queue, j);
1388 j->in_gc_queue = true;
1389 }
1390
1391 static int job_compare(const void *a, const void *b) {
1392 Job *x = *(Job**) a, *y = *(Job**) b;
1393
1394 if (x->id < y->id)
1395 return -1;
1396 if (x->id > y->id)
1397 return 1;
1398
1399 return 0;
1400 }
1401
1402 static size_t sort_job_list(Job **list, size_t n) {
1403 Job *previous = NULL;
1404 size_t a, b;
1405
1406 /* Order by numeric IDs */
1407 qsort_safe(list, n, sizeof(Job*), job_compare);
1408
1409 /* Filter out duplicates */
1410 for (a = 0, b = 0; a < n; a++) {
1411
1412 if (previous == list[a])
1413 continue;
1414
1415 previous = list[b++] = list[a];
1416 }
1417
1418 return b;
1419 }
1420
1421 int job_get_before(Job *j, Job*** ret) {
1422 _cleanup_free_ Job** list = NULL;
1423 size_t n = 0, n_allocated = 0;
1424 Unit *other = NULL;
1425 Iterator i;
1426 void *v;
1427
1428 /* Returns a list of all pending jobs that need to finish before this job may be started. */
1429
1430 assert(j);
1431 assert(ret);
1432
1433 if (j->ignore_order) {
1434 *ret = NULL;
1435 return 0;
1436 }
1437
1438 if (IN_SET(j->type, JOB_START, JOB_VERIFY_ACTIVE, JOB_RELOAD)) {
1439
1440 HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_AFTER], i) {
1441 if (!other->job)
1442 continue;
1443
1444 if (!GREEDY_REALLOC(list, n_allocated, n+1))
1445 return -ENOMEM;
1446 list[n++] = other->job;
1447 }
1448 }
1449
1450 HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_BEFORE], i) {
1451 if (!other->job)
1452 continue;
1453
1454 if (!IN_SET(other->job->type, JOB_STOP, JOB_RESTART))
1455 continue;
1456
1457 if (!GREEDY_REALLOC(list, n_allocated, n+1))
1458 return -ENOMEM;
1459 list[n++] = other->job;
1460 }
1461
1462 n = sort_job_list(list, n);
1463
1464 *ret = TAKE_PTR(list);
1465
1466 return (int) n;
1467 }
1468
1469 int job_get_after(Job *j, Job*** ret) {
1470 _cleanup_free_ Job** list = NULL;
1471 size_t n = 0, n_allocated = 0;
1472 Unit *other = NULL;
1473 void *v;
1474 Iterator i;
1475
1476 assert(j);
1477 assert(ret);
1478
1479 /* Returns a list of all pending jobs that are waiting for this job to finish. */
1480
1481 HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_BEFORE], i) {
1482 if (!other->job)
1483 continue;
1484
1485 if (other->job->ignore_order)
1486 continue;
1487
1488 if (!IN_SET(other->job->type, JOB_START, JOB_VERIFY_ACTIVE, JOB_RELOAD))
1489 continue;
1490
1491 if (!GREEDY_REALLOC(list, n_allocated, n+1))
1492 return -ENOMEM;
1493 list[n++] = other->job;
1494 }
1495
1496 if (IN_SET(j->type, JOB_STOP, JOB_RESTART)) {
1497
1498 HASHMAP_FOREACH_KEY(v, other, j->unit->dependencies[UNIT_AFTER], i) {
1499 if (!other->job)
1500 continue;
1501
1502 if (other->job->ignore_order)
1503 continue;
1504
1505 if (!GREEDY_REALLOC(list, n_allocated, n+1))
1506 return -ENOMEM;
1507 list[n++] = other->job;
1508 }
1509 }
1510
1511 n = sort_job_list(list, n);
1512
1513 *ret = TAKE_PTR(list);
1514
1515 return (int) n;
1516 }
1517
1518 static const char* const job_state_table[_JOB_STATE_MAX] = {
1519 [JOB_WAITING] = "waiting",
1520 [JOB_RUNNING] = "running",
1521 };
1522
1523 DEFINE_STRING_TABLE_LOOKUP(job_state, JobState);
1524
1525 static const char* const job_type_table[_JOB_TYPE_MAX] = {
1526 [JOB_START] = "start",
1527 [JOB_VERIFY_ACTIVE] = "verify-active",
1528 [JOB_STOP] = "stop",
1529 [JOB_RELOAD] = "reload",
1530 [JOB_RELOAD_OR_START] = "reload-or-start",
1531 [JOB_RESTART] = "restart",
1532 [JOB_TRY_RESTART] = "try-restart",
1533 [JOB_TRY_RELOAD] = "try-reload",
1534 [JOB_NOP] = "nop",
1535 };
1536
1537 DEFINE_STRING_TABLE_LOOKUP(job_type, JobType);
1538
1539 static const char* const job_mode_table[_JOB_MODE_MAX] = {
1540 [JOB_FAIL] = "fail",
1541 [JOB_REPLACE] = "replace",
1542 [JOB_REPLACE_IRREVERSIBLY] = "replace-irreversibly",
1543 [JOB_ISOLATE] = "isolate",
1544 [JOB_FLUSH] = "flush",
1545 [JOB_IGNORE_DEPENDENCIES] = "ignore-dependencies",
1546 [JOB_IGNORE_REQUIREMENTS] = "ignore-requirements",
1547 };
1548
1549 DEFINE_STRING_TABLE_LOOKUP(job_mode, JobMode);
1550
1551 static const char* const job_result_table[_JOB_RESULT_MAX] = {
1552 [JOB_DONE] = "done",
1553 [JOB_CANCELED] = "canceled",
1554 [JOB_TIMEOUT] = "timeout",
1555 [JOB_FAILED] = "failed",
1556 [JOB_DEPENDENCY] = "dependency",
1557 [JOB_SKIPPED] = "skipped",
1558 [JOB_INVALID] = "invalid",
1559 [JOB_ASSERT] = "assert",
1560 [JOB_UNSUPPORTED] = "unsupported",
1561 [JOB_COLLECTED] = "collected",
1562 [JOB_ONCE] = "once",
1563 };
1564
1565 DEFINE_STRING_TABLE_LOOKUP(job_result, JobResult);
1566
1567 const char* job_type_to_access_method(JobType t) {
1568 assert(t >= 0);
1569 assert(t < _JOB_TYPE_MAX);
1570
1571 if (IN_SET(t, JOB_START, JOB_RESTART, JOB_TRY_RESTART))
1572 return "start";
1573 else if (t == JOB_STOP)
1574 return "stop";
1575 else
1576 return "reload";
1577 }