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