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