]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/job.c
job: Ensure JobRunningTimeoutSec= survives serialization (#6128)
[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
372 b == UNIT_ACTIVE ||
373 b == UNIT_RELOADING;
374
375 case JOB_STOP:
376 return
377 b == UNIT_INACTIVE ||
378 b == UNIT_FAILED;
379
380 case JOB_VERIFY_ACTIVE:
381 return
382 b == UNIT_ACTIVE ||
383 b == 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
447 assert(j);
448 assert(j->installed);
449
450 /* Checks whether there is any job running for the units this
451 * job needs to be running after (in the case of a 'positive'
452 * job type) or before (in the case of a 'negative' job
453 * type. */
454
455 /* Note that unit types have a say in what is runnable,
456 * too. For example, if they return -EAGAIN from
457 * unit_start() they can indicate they are not
458 * runnable yet. */
459
460 /* First check if there is an override */
461 if (j->ignore_order)
462 return true;
463
464 if (j->type == JOB_NOP)
465 return true;
466
467 if (IN_SET(j->type, JOB_START, JOB_VERIFY_ACTIVE, JOB_RELOAD)) {
468
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 SET_FOREACH(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 SET_FOREACH(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 /* fall through */
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
584 switch (j->type) {
585
586 case JOB_VERIFY_ACTIVE: {
587 UnitActiveState t = unit_active_state(j->unit);
588 if (UNIT_IS_ACTIVE_OR_RELOADING(t))
589 r = -EALREADY;
590 else if (t == UNIT_ACTIVATING)
591 r = -EAGAIN;
592 else
593 r = -EBADR;
594 break;
595 }
596
597 case JOB_START:
598 case JOB_STOP:
599 case JOB_RESTART:
600 r = job_perform_on_unit(&j);
601
602 /* If the unit type does not support starting/stopping,
603 * then simply wait. */
604 if (r == -EBADR)
605 r = 0;
606 break;
607
608 case JOB_RELOAD:
609 r = job_perform_on_unit(&j);
610 break;
611
612 case JOB_NOP:
613 r = -EALREADY;
614 break;
615
616 default:
617 assert_not_reached("Unknown job type");
618 }
619
620 if (j) {
621 if (r == -EALREADY)
622 r = job_finish_and_invalidate(j, JOB_DONE, true, true);
623 else if (r == -EBADR)
624 r = job_finish_and_invalidate(j, JOB_SKIPPED, true, false);
625 else if (r == -ENOEXEC)
626 r = job_finish_and_invalidate(j, JOB_INVALID, true, false);
627 else if (r == -EPROTO)
628 r = job_finish_and_invalidate(j, JOB_ASSERT, true, false);
629 else if (r == -EOPNOTSUPP)
630 r = job_finish_and_invalidate(j, JOB_UNSUPPORTED, true, false);
631 else if (r == -ENOLINK)
632 r = job_finish_and_invalidate(j, JOB_DEPENDENCY, true, false);
633 else if (r == -EAGAIN)
634 job_set_state(j, JOB_WAITING);
635 else if (r < 0)
636 r = job_finish_and_invalidate(j, JOB_FAILED, true, false);
637 }
638
639 return r;
640 }
641
642 _pure_ static const char *job_get_status_message_format(Unit *u, JobType t, JobResult result) {
643
644 static const char *const generic_finished_start_job[_JOB_RESULT_MAX] = {
645 [JOB_DONE] = "Started %s.",
646 [JOB_TIMEOUT] = "Timed out starting %s.",
647 [JOB_FAILED] = "Failed to start %s.",
648 [JOB_DEPENDENCY] = "Dependency failed for %s.",
649 [JOB_ASSERT] = "Assertion failed for %s.",
650 [JOB_UNSUPPORTED] = "Starting of %s not supported.",
651 [JOB_COLLECTED] = "Unnecessary job for %s was removed.",
652 };
653 static const char *const generic_finished_stop_job[_JOB_RESULT_MAX] = {
654 [JOB_DONE] = "Stopped %s.",
655 [JOB_FAILED] = "Stopped (with error) %s.",
656 [JOB_TIMEOUT] = "Timed out stopping %s.",
657 };
658 static const char *const generic_finished_reload_job[_JOB_RESULT_MAX] = {
659 [JOB_DONE] = "Reloaded %s.",
660 [JOB_FAILED] = "Reload failed for %s.",
661 [JOB_TIMEOUT] = "Timed out reloading %s.",
662 };
663 /* When verify-active detects the unit is inactive, report it.
664 * Most likely a DEPEND warning from a requisiting unit will
665 * occur next and it's nice to see what was requisited. */
666 static const char *const generic_finished_verify_active_job[_JOB_RESULT_MAX] = {
667 [JOB_SKIPPED] = "%s is not active.",
668 };
669
670 const UnitStatusMessageFormats *format_table;
671 const char *format;
672
673 assert(u);
674 assert(t >= 0);
675 assert(t < _JOB_TYPE_MAX);
676
677 if (IN_SET(t, JOB_START, JOB_STOP, JOB_RESTART)) {
678 format_table = &UNIT_VTABLE(u)->status_message_formats;
679 if (format_table) {
680 format = t == JOB_START ? format_table->finished_start_job[result] :
681 format_table->finished_stop_job[result];
682 if (format)
683 return format;
684 }
685 }
686
687 /* Return generic strings */
688 if (t == JOB_START)
689 return generic_finished_start_job[result];
690 else if (t == JOB_STOP || t == JOB_RESTART)
691 return generic_finished_stop_job[result];
692 else if (t == JOB_RELOAD)
693 return generic_finished_reload_job[result];
694 else if (t == JOB_VERIFY_ACTIVE)
695 return generic_finished_verify_active_job[result];
696
697 return NULL;
698 }
699
700 static void job_print_status_message(Unit *u, JobType t, JobResult result) {
701 static const struct {
702 const char *color, *word;
703 } statuses[_JOB_RESULT_MAX] = {
704 [JOB_DONE] = { ANSI_GREEN, " OK " },
705 [JOB_TIMEOUT] = { ANSI_HIGHLIGHT_RED, " TIME " },
706 [JOB_FAILED] = { ANSI_HIGHLIGHT_RED, "FAILED" },
707 [JOB_DEPENDENCY] = { ANSI_HIGHLIGHT_YELLOW, "DEPEND" },
708 [JOB_SKIPPED] = { ANSI_HIGHLIGHT, " INFO " },
709 [JOB_ASSERT] = { ANSI_HIGHLIGHT_YELLOW, "ASSERT" },
710 [JOB_UNSUPPORTED] = { ANSI_HIGHLIGHT_YELLOW, "UNSUPP" },
711 [JOB_COLLECTED] = { ANSI_HIGHLIGHT, " INFO " },
712 };
713
714 const char *format;
715 const char *status;
716
717 assert(u);
718 assert(t >= 0);
719 assert(t < _JOB_TYPE_MAX);
720
721 /* Reload status messages have traditionally not been printed to console. */
722 if (t == JOB_RELOAD)
723 return;
724
725 format = job_get_status_message_format(u, t, result);
726 if (!format)
727 return;
728
729 if (log_get_show_color())
730 status = strjoina(statuses[result].color, statuses[result].word, ANSI_NORMAL);
731 else
732 status = statuses[result].word;
733
734 if (result != JOB_DONE)
735 manager_flip_auto_status(u->manager, true);
736
737 DISABLE_WARNING_FORMAT_NONLITERAL;
738 unit_status_printf(u, status, format);
739 REENABLE_WARNING;
740
741 if (t == JOB_START && result == JOB_FAILED) {
742 _cleanup_free_ char *quoted;
743
744 quoted = shell_maybe_quote(u->id);
745 manager_status_printf(u->manager, STATUS_TYPE_NORMAL, NULL, "See 'systemctl status %s' for details.", strna(quoted));
746 }
747 }
748
749 static void job_log_status_message(Unit *u, JobType t, JobResult result) {
750 const char *format, *mid;
751 char buf[LINE_MAX];
752 static const int job_result_log_level[_JOB_RESULT_MAX] = {
753 [JOB_DONE] = LOG_INFO,
754 [JOB_CANCELED] = LOG_INFO,
755 [JOB_TIMEOUT] = LOG_ERR,
756 [JOB_FAILED] = LOG_ERR,
757 [JOB_DEPENDENCY] = LOG_WARNING,
758 [JOB_SKIPPED] = LOG_NOTICE,
759 [JOB_INVALID] = LOG_INFO,
760 [JOB_ASSERT] = LOG_WARNING,
761 [JOB_UNSUPPORTED] = LOG_WARNING,
762 [JOB_COLLECTED] = LOG_INFO,
763 };
764
765 assert(u);
766 assert(t >= 0);
767 assert(t < _JOB_TYPE_MAX);
768
769 /* Skip this if it goes to the console. since we already print
770 * to the console anyway... */
771
772 if (log_on_console())
773 return;
774
775 format = job_get_status_message_format(u, t, result);
776 if (!format)
777 return;
778
779 /* The description might be longer than the buffer, but that's OK, we'll just truncate it here */
780 DISABLE_WARNING_FORMAT_NONLITERAL;
781 snprintf(buf, sizeof(buf), format, unit_description(u));
782 REENABLE_WARNING;
783
784 switch (t) {
785
786 case JOB_START:
787 if (result == JOB_DONE)
788 mid = "MESSAGE_ID=" SD_MESSAGE_UNIT_STARTED_STR;
789 else
790 mid = "MESSAGE_ID=" SD_MESSAGE_UNIT_FAILED_STR;
791 break;
792
793 case JOB_RELOAD:
794 mid = "MESSAGE_ID=" SD_MESSAGE_UNIT_RELOADED_STR;
795 break;
796
797 case JOB_STOP:
798 case JOB_RESTART:
799 mid = "MESSAGE_ID=" SD_MESSAGE_UNIT_STOPPED_STR;
800 break;
801
802 default:
803 log_struct(job_result_log_level[result],
804 LOG_MESSAGE("%s", buf),
805 "RESULT=%s", job_result_to_string(result),
806 LOG_UNIT_ID(u),
807 NULL);
808 return;
809 }
810
811 log_struct(job_result_log_level[result],
812 LOG_MESSAGE("%s", buf),
813 "RESULT=%s", job_result_to_string(result),
814 LOG_UNIT_ID(u),
815 mid,
816 NULL);
817 }
818
819 static void job_emit_status_message(Unit *u, JobType t, JobResult result) {
820
821 /* No message if the job did not actually do anything due to failed condition. */
822 if (t == JOB_START && result == JOB_DONE && !u->condition_result)
823 return;
824
825 job_log_status_message(u, t, result);
826 job_print_status_message(u, t, result);
827 }
828
829 static void job_fail_dependencies(Unit *u, UnitDependency d) {
830 Unit *other;
831 Iterator i;
832
833 assert(u);
834
835 SET_FOREACH(other, u->dependencies[d], i) {
836 Job *j = other->job;
837
838 if (!j)
839 continue;
840 if (!IN_SET(j->type, JOB_START, JOB_VERIFY_ACTIVE))
841 continue;
842
843 job_finish_and_invalidate(j, JOB_DEPENDENCY, true, false);
844 }
845 }
846
847 int job_finish_and_invalidate(Job *j, JobResult result, bool recursive, bool already) {
848 Unit *u;
849 Unit *other;
850 JobType t;
851 Iterator i;
852
853 assert(j);
854 assert(j->installed);
855 assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION);
856
857 u = j->unit;
858 t = j->type;
859
860 j->result = result;
861
862 log_unit_debug(u, "Job %s/%s finished, result=%s", u->id, job_type_to_string(t), job_result_to_string(result));
863
864 /* If this job did nothing to respective unit we don't log the status message */
865 if (!already)
866 job_emit_status_message(u, t, result);
867
868 job_add_to_dbus_queue(j);
869
870 /* Patch restart jobs so that they become normal start jobs */
871 if (result == JOB_DONE && t == JOB_RESTART) {
872
873 job_change_type(j, JOB_START);
874 job_set_state(j, JOB_WAITING);
875
876 job_add_to_run_queue(j);
877 job_add_to_gc_queue(j);
878
879 goto finish;
880 }
881
882 if (result == JOB_FAILED || result == JOB_INVALID)
883 j->manager->n_failed_jobs++;
884
885 job_uninstall(j);
886 job_free(j);
887
888 /* Fail depending jobs on failure */
889 if (result != JOB_DONE && recursive) {
890 if (IN_SET(t, JOB_START, JOB_VERIFY_ACTIVE)) {
891 job_fail_dependencies(u, UNIT_REQUIRED_BY);
892 job_fail_dependencies(u, UNIT_REQUISITE_OF);
893 job_fail_dependencies(u, UNIT_BOUND_BY);
894 } else if (t == JOB_STOP)
895 job_fail_dependencies(u, UNIT_CONFLICTED_BY);
896 }
897
898 /* Trigger OnFailure dependencies that are not generated by
899 * the unit itself. We don't treat JOB_CANCELED as failure in
900 * this context. And JOB_FAILURE is already handled by the
901 * unit itself. */
902 if (result == JOB_TIMEOUT || result == JOB_DEPENDENCY) {
903 log_struct(LOG_NOTICE,
904 "JOB_TYPE=%s", job_type_to_string(t),
905 "JOB_RESULT=%s", job_result_to_string(result),
906 LOG_UNIT_ID(u),
907 LOG_UNIT_MESSAGE(u, "Job %s/%s failed with result '%s'.",
908 u->id,
909 job_type_to_string(t),
910 job_result_to_string(result)),
911 NULL);
912
913 unit_start_on_failure(u);
914 }
915
916 unit_trigger_notify(u);
917
918 finish:
919 /* Try to start the next jobs that can be started */
920 SET_FOREACH(other, u->dependencies[UNIT_AFTER], i)
921 if (other->job) {
922 job_add_to_run_queue(other->job);
923 job_add_to_gc_queue(other->job);
924 }
925 SET_FOREACH(other, u->dependencies[UNIT_BEFORE], i)
926 if (other->job) {
927 job_add_to_run_queue(other->job);
928 job_add_to_gc_queue(other->job);
929 }
930
931 manager_check_finished(u->manager);
932
933 return 0;
934 }
935
936 static int job_dispatch_timer(sd_event_source *s, uint64_t monotonic, void *userdata) {
937 Job *j = userdata;
938 Unit *u;
939
940 assert(j);
941 assert(s == j->timer_event_source);
942
943 log_unit_warning(j->unit, "Job %s/%s timed out.", j->unit->id, job_type_to_string(j->type));
944
945 u = j->unit;
946 job_finish_and_invalidate(j, JOB_TIMEOUT, true, false);
947
948 emergency_action(u->manager, u->job_timeout_action, u->job_timeout_reboot_arg, "job timed out");
949
950 return 0;
951 }
952
953 int job_start_timer(Job *j, bool job_running) {
954 int r;
955 usec_t timeout_time, old_timeout_time;
956
957 if (job_running) {
958 j->begin_running_usec = now(CLOCK_MONOTONIC);
959
960 if (j->unit->job_running_timeout == USEC_INFINITY)
961 return 0;
962
963 timeout_time = usec_add(j->begin_running_usec, j->unit->job_running_timeout);
964
965 if (j->timer_event_source) {
966 /* Update only if JobRunningTimeoutSec= results in earlier timeout */
967 r = sd_event_source_get_time(j->timer_event_source, &old_timeout_time);
968 if (r < 0)
969 return r;
970
971 if (old_timeout_time <= timeout_time)
972 return 0;
973
974 return sd_event_source_set_time(j->timer_event_source, timeout_time);
975 }
976 } else {
977 if (j->timer_event_source)
978 return 0;
979
980 j->begin_usec = now(CLOCK_MONOTONIC);
981
982 if (j->unit->job_timeout == USEC_INFINITY)
983 return 0;
984
985 timeout_time = usec_add(j->begin_usec, j->unit->job_timeout);
986 }
987
988 r = sd_event_add_time(
989 j->manager->event,
990 &j->timer_event_source,
991 CLOCK_MONOTONIC,
992 timeout_time, 0,
993 job_dispatch_timer, j);
994 if (r < 0)
995 return r;
996
997 (void) sd_event_source_set_description(j->timer_event_source, "job-start");
998
999 return 0;
1000 }
1001
1002 void job_add_to_run_queue(Job *j) {
1003 assert(j);
1004 assert(j->installed);
1005
1006 if (j->in_run_queue)
1007 return;
1008
1009 if (!j->manager->run_queue)
1010 sd_event_source_set_enabled(j->manager->run_queue_event_source, SD_EVENT_ONESHOT);
1011
1012 LIST_PREPEND(run_queue, j->manager->run_queue, j);
1013 j->in_run_queue = true;
1014 }
1015
1016 void job_add_to_dbus_queue(Job *j) {
1017 assert(j);
1018 assert(j->installed);
1019
1020 if (j->in_dbus_queue)
1021 return;
1022
1023 /* We don't check if anybody is subscribed here, since this
1024 * job might just have been created and not yet assigned to a
1025 * connection/client. */
1026
1027 LIST_PREPEND(dbus_queue, j->manager->dbus_job_queue, j);
1028 j->in_dbus_queue = true;
1029 }
1030
1031 char *job_dbus_path(Job *j) {
1032 char *p;
1033
1034 assert(j);
1035
1036 if (asprintf(&p, "/org/freedesktop/systemd1/job/%"PRIu32, j->id) < 0)
1037 return NULL;
1038
1039 return p;
1040 }
1041
1042 int job_serialize(Job *j, FILE *f) {
1043 assert(j);
1044 assert(f);
1045
1046 fprintf(f, "job-id=%u\n", j->id);
1047 fprintf(f, "job-type=%s\n", job_type_to_string(j->type));
1048 fprintf(f, "job-state=%s\n", job_state_to_string(j->state));
1049 fprintf(f, "job-irreversible=%s\n", yes_no(j->irreversible));
1050 fprintf(f, "job-sent-dbus-new-signal=%s\n", yes_no(j->sent_dbus_new_signal));
1051 fprintf(f, "job-ignore-order=%s\n", yes_no(j->ignore_order));
1052
1053 if (j->begin_usec > 0)
1054 fprintf(f, "job-begin="USEC_FMT"\n", j->begin_usec);
1055 if (j->begin_running_usec > 0)
1056 fprintf(f, "job-begin-running="USEC_FMT"\n", j->begin_running_usec);
1057
1058 bus_track_serialize(j->bus_track, f, "subscribed");
1059
1060 /* End marker */
1061 fputc('\n', f);
1062 return 0;
1063 }
1064
1065 int job_deserialize(Job *j, FILE *f) {
1066 assert(j);
1067 assert(f);
1068
1069 for (;;) {
1070 char line[LINE_MAX], *l, *v;
1071 size_t k;
1072
1073 if (!fgets(line, sizeof(line), f)) {
1074 if (feof(f))
1075 return 0;
1076 return -errno;
1077 }
1078
1079 char_array_0(line);
1080 l = strstrip(line);
1081
1082 /* End marker */
1083 if (l[0] == 0)
1084 return 0;
1085
1086 k = strcspn(l, "=");
1087
1088 if (l[k] == '=') {
1089 l[k] = 0;
1090 v = l+k+1;
1091 } else
1092 v = l+k;
1093
1094 if (streq(l, "job-id")) {
1095
1096 if (safe_atou32(v, &j->id) < 0)
1097 log_debug("Failed to parse job id value %s", v);
1098
1099 } else if (streq(l, "job-type")) {
1100 JobType t;
1101
1102 t = job_type_from_string(v);
1103 if (t < 0)
1104 log_debug("Failed to parse job type %s", v);
1105 else if (t >= _JOB_TYPE_MAX_IN_TRANSACTION)
1106 log_debug("Cannot deserialize job of type %s", v);
1107 else
1108 j->type = t;
1109
1110 } else if (streq(l, "job-state")) {
1111 JobState s;
1112
1113 s = job_state_from_string(v);
1114 if (s < 0)
1115 log_debug("Failed to parse job state %s", v);
1116 else
1117 job_set_state(j, s);
1118
1119 } else if (streq(l, "job-irreversible")) {
1120 int b;
1121
1122 b = parse_boolean(v);
1123 if (b < 0)
1124 log_debug("Failed to parse job irreversible flag %s", v);
1125 else
1126 j->irreversible = j->irreversible || b;
1127
1128 } else if (streq(l, "job-sent-dbus-new-signal")) {
1129 int b;
1130
1131 b = parse_boolean(v);
1132 if (b < 0)
1133 log_debug("Failed to parse job sent_dbus_new_signal flag %s", v);
1134 else
1135 j->sent_dbus_new_signal = j->sent_dbus_new_signal || b;
1136
1137 } else if (streq(l, "job-ignore-order")) {
1138 int b;
1139
1140 b = parse_boolean(v);
1141 if (b < 0)
1142 log_debug("Failed to parse job ignore_order flag %s", v);
1143 else
1144 j->ignore_order = j->ignore_order || b;
1145
1146 } else if (streq(l, "job-begin")) {
1147 unsigned long long ull;
1148
1149 if (sscanf(v, "%llu", &ull) != 1)
1150 log_debug("Failed to parse job-begin value %s", v);
1151 else
1152 j->begin_usec = ull;
1153
1154 } else if (streq(l, "job-begin-running")) {
1155 unsigned long long ull;
1156
1157 if (sscanf(v, "%llu", &ull) != 1)
1158 log_debug("Failed to parse job-begin-running value %s", v);
1159 else
1160 j->begin_running_usec = ull;
1161
1162 } else if (streq(l, "subscribed")) {
1163
1164 if (strv_extend(&j->deserialized_clients, v) < 0)
1165 log_oom();
1166 }
1167 }
1168 }
1169
1170 int job_coldplug(Job *j) {
1171 int r;
1172 usec_t timeout_time = USEC_INFINITY;
1173
1174 assert(j);
1175
1176 /* After deserialization is complete and the bus connection
1177 * set up again, let's start watching our subscribers again */
1178 (void) bus_job_coldplug_bus_track(j);
1179
1180 if (j->state == JOB_WAITING)
1181 job_add_to_run_queue(j);
1182
1183 /* Maybe due to new dependencies we don't actually need this job anymore? */
1184 job_add_to_gc_queue(j);
1185
1186 /* Create timer only when job began or began running and the respective timeout is finite.
1187 * Follow logic of job_start_timer() if both timeouts are finite */
1188 if (j->begin_usec == 0)
1189 return 0;
1190
1191 if (j->unit->job_timeout != USEC_INFINITY)
1192 timeout_time = usec_add(j->begin_usec, j->unit->job_timeout);
1193
1194 if (j->begin_running_usec > 0 && j->unit->job_running_timeout != USEC_INFINITY)
1195 timeout_time = MIN(timeout_time, usec_add(j->begin_running_usec, j->unit->job_running_timeout));
1196
1197 if (timeout_time == USEC_INFINITY)
1198 return 0;
1199
1200 j->timer_event_source = sd_event_source_unref(j->timer_event_source);
1201
1202 r = sd_event_add_time(
1203 j->manager->event,
1204 &j->timer_event_source,
1205 CLOCK_MONOTONIC,
1206 timeout_time, 0,
1207 job_dispatch_timer, j);
1208 if (r < 0)
1209 log_debug_errno(r, "Failed to restart timeout for job: %m");
1210
1211 (void) sd_event_source_set_description(j->timer_event_source, "job-timeout");
1212
1213 return r;
1214 }
1215
1216 void job_shutdown_magic(Job *j) {
1217 assert(j);
1218
1219 /* The shutdown target gets some special treatment here: we
1220 * tell the kernel to begin with flushing its disk caches, to
1221 * optimize shutdown time a bit. Ideally we wouldn't hardcode
1222 * this magic into PID 1. However all other processes aren't
1223 * options either since they'd exit much sooner than PID 1 and
1224 * asynchronous sync() would cause their exit to be
1225 * delayed. */
1226
1227 if (j->type != JOB_START)
1228 return;
1229
1230 if (!MANAGER_IS_SYSTEM(j->unit->manager))
1231 return;
1232
1233 if (!unit_has_name(j->unit, SPECIAL_SHUTDOWN_TARGET))
1234 return;
1235
1236 /* In case messages on console has been disabled on boot */
1237 j->unit->manager->no_console_output = false;
1238
1239 if (detect_container() > 0)
1240 return;
1241
1242 asynchronous_sync();
1243 }
1244
1245 int job_get_timeout(Job *j, usec_t *timeout) {
1246 usec_t x = USEC_INFINITY, y = USEC_INFINITY;
1247 Unit *u = j->unit;
1248 int r;
1249
1250 assert(u);
1251
1252 if (j->timer_event_source) {
1253 r = sd_event_source_get_time(j->timer_event_source, &x);
1254 if (r < 0)
1255 return r;
1256 }
1257
1258 if (UNIT_VTABLE(u)->get_timeout) {
1259 r = UNIT_VTABLE(u)->get_timeout(u, &y);
1260 if (r < 0)
1261 return r;
1262 }
1263
1264 if (x == USEC_INFINITY && y == USEC_INFINITY)
1265 return 0;
1266
1267 *timeout = MIN(x, y);
1268 return 1;
1269 }
1270
1271 bool job_check_gc(Job *j) {
1272 Unit *other;
1273 Iterator i;
1274
1275 assert(j);
1276
1277 /* Checks whether this job should be GC'ed away. We only do this for jobs of units that have no effect on their
1278 * own and just track external state. For now the only unit type that qualifies for this are .device units. */
1279
1280 if (!UNIT_VTABLE(j->unit)->gc_jobs)
1281 return true;
1282
1283 if (sd_bus_track_count(j->bus_track) > 0)
1284 return true;
1285
1286 /* FIXME: So this is a bit ugly: for now we don't properly track references made via private bus connections
1287 * (because it's nasty, as sd_bus_track doesn't apply to it). We simply remember that the job was once
1288 * referenced by one, and reset this whenever we notice that no private bus connections are around. This means
1289 * the GC is a bit too conservative when it comes to jobs created by private bus connections. */
1290 if (j->ref_by_private_bus) {
1291 if (set_isempty(j->unit->manager->private_buses))
1292 j->ref_by_private_bus = false;
1293 else
1294 return true;
1295 }
1296
1297 if (j->type == JOB_NOP)
1298 return true;
1299
1300 /* If a job is ordered after ours, and is to be started, then it needs to wait for us, regardless if we stop or
1301 * start, hence let's not GC in that case. */
1302 SET_FOREACH(other, j->unit->dependencies[UNIT_BEFORE], i) {
1303 if (!other->job)
1304 continue;
1305
1306 if (other->job->ignore_order)
1307 continue;
1308
1309 if (IN_SET(other->job->type, JOB_START, JOB_VERIFY_ACTIVE, JOB_RELOAD))
1310 return true;
1311 }
1312
1313 /* If we are going down, but something else is orederd After= us, then it needs to wait for us */
1314 if (IN_SET(j->type, JOB_STOP, JOB_RESTART)) {
1315
1316 SET_FOREACH(other, j->unit->dependencies[UNIT_AFTER], i) {
1317 if (!other->job)
1318 continue;
1319
1320 if (other->job->ignore_order)
1321 continue;
1322
1323 return true;
1324 }
1325 }
1326
1327 /* The logic above is kinda the inverse of the job_is_runnable() logic. Specifically, if the job "we" is
1328 * ordered before the job "other":
1329 *
1330 * we start + other start → stay
1331 * we start + other stop → gc
1332 * we stop + other start → stay
1333 * we stop + other stop → gc
1334 *
1335 * "we" are ordered after "other":
1336 *
1337 * we start + other start → gc
1338 * we start + other stop → gc
1339 * we stop + other start → stay
1340 * we stop + other stop → stay
1341 *
1342 */
1343
1344 return false;
1345 }
1346
1347 void job_add_to_gc_queue(Job *j) {
1348 assert(j);
1349
1350 if (j->in_gc_queue)
1351 return;
1352
1353 if (job_check_gc(j))
1354 return;
1355
1356 LIST_PREPEND(gc_queue, j->unit->manager->gc_job_queue, j);
1357 j->in_gc_queue = true;
1358 }
1359
1360 static int job_compare(const void *a, const void *b) {
1361 Job *x = *(Job**) a, *y = *(Job**) b;
1362
1363 if (x->id < y->id)
1364 return -1;
1365 if (x->id > y->id)
1366 return 1;
1367
1368 return 0;
1369 }
1370
1371 static size_t sort_job_list(Job **list, size_t n) {
1372 Job *previous = NULL;
1373 size_t a, b;
1374
1375 /* Order by numeric IDs */
1376 qsort_safe(list, n, sizeof(Job*), job_compare);
1377
1378 /* Filter out duplicates */
1379 for (a = 0, b = 0; a < n; a++) {
1380
1381 if (previous == list[a])
1382 continue;
1383
1384 previous = list[b++] = list[a];
1385 }
1386
1387 return b;
1388 }
1389
1390 int job_get_before(Job *j, Job*** ret) {
1391 _cleanup_free_ Job** list = NULL;
1392 size_t n = 0, n_allocated = 0;
1393 Unit *other = NULL;
1394 Iterator i;
1395
1396 /* Returns a list of all pending jobs that need to finish before this job may be started. */
1397
1398 assert(j);
1399 assert(ret);
1400
1401 if (j->ignore_order) {
1402 *ret = NULL;
1403 return 0;
1404 }
1405
1406 if (IN_SET(j->type, JOB_START, JOB_VERIFY_ACTIVE, JOB_RELOAD)) {
1407
1408 SET_FOREACH(other, j->unit->dependencies[UNIT_AFTER], i) {
1409 if (!other->job)
1410 continue;
1411
1412 if (!GREEDY_REALLOC(list, n_allocated, n+1))
1413 return -ENOMEM;
1414 list[n++] = other->job;
1415 }
1416 }
1417
1418 SET_FOREACH(other, j->unit->dependencies[UNIT_BEFORE], i) {
1419 if (!other->job)
1420 continue;
1421
1422 if (!IN_SET(other->job->type, JOB_STOP, JOB_RESTART))
1423 continue;
1424
1425 if (!GREEDY_REALLOC(list, n_allocated, n+1))
1426 return -ENOMEM;
1427 list[n++] = other->job;
1428 }
1429
1430 n = sort_job_list(list, n);
1431
1432 *ret = list;
1433 list = NULL;
1434
1435 return (int) n;
1436 }
1437
1438 int job_get_after(Job *j, Job*** ret) {
1439 _cleanup_free_ Job** list = NULL;
1440 size_t n = 0, n_allocated = 0;
1441 Unit *other = NULL;
1442 Iterator i;
1443
1444 assert(j);
1445 assert(ret);
1446
1447 /* Returns a list of all pending jobs that are waiting for this job to finish. */
1448
1449 SET_FOREACH(other, j->unit->dependencies[UNIT_BEFORE], i) {
1450 if (!other->job)
1451 continue;
1452
1453 if (other->job->ignore_order)
1454 continue;
1455
1456 if (!IN_SET(other->job->type, JOB_START, JOB_VERIFY_ACTIVE, JOB_RELOAD))
1457 continue;
1458
1459 if (!GREEDY_REALLOC(list, n_allocated, n+1))
1460 return -ENOMEM;
1461 list[n++] = other->job;
1462 }
1463
1464 if (IN_SET(j->type, JOB_STOP, JOB_RESTART)) {
1465
1466 SET_FOREACH(other, j->unit->dependencies[UNIT_AFTER], i) {
1467 if (!other->job)
1468 continue;
1469
1470 if (other->job->ignore_order)
1471 continue;
1472
1473 if (!GREEDY_REALLOC(list, n_allocated, n+1))
1474 return -ENOMEM;
1475 list[n++] = other->job;
1476 }
1477 }
1478
1479 n = sort_job_list(list, n);
1480
1481 *ret = list;
1482 list = NULL;
1483
1484 return (int) n;
1485 }
1486
1487 static const char* const job_state_table[_JOB_STATE_MAX] = {
1488 [JOB_WAITING] = "waiting",
1489 [JOB_RUNNING] = "running",
1490 };
1491
1492 DEFINE_STRING_TABLE_LOOKUP(job_state, JobState);
1493
1494 static const char* const job_type_table[_JOB_TYPE_MAX] = {
1495 [JOB_START] = "start",
1496 [JOB_VERIFY_ACTIVE] = "verify-active",
1497 [JOB_STOP] = "stop",
1498 [JOB_RELOAD] = "reload",
1499 [JOB_RELOAD_OR_START] = "reload-or-start",
1500 [JOB_RESTART] = "restart",
1501 [JOB_TRY_RESTART] = "try-restart",
1502 [JOB_TRY_RELOAD] = "try-reload",
1503 [JOB_NOP] = "nop",
1504 };
1505
1506 DEFINE_STRING_TABLE_LOOKUP(job_type, JobType);
1507
1508 static const char* const job_mode_table[_JOB_MODE_MAX] = {
1509 [JOB_FAIL] = "fail",
1510 [JOB_REPLACE] = "replace",
1511 [JOB_REPLACE_IRREVERSIBLY] = "replace-irreversibly",
1512 [JOB_ISOLATE] = "isolate",
1513 [JOB_FLUSH] = "flush",
1514 [JOB_IGNORE_DEPENDENCIES] = "ignore-dependencies",
1515 [JOB_IGNORE_REQUIREMENTS] = "ignore-requirements",
1516 };
1517
1518 DEFINE_STRING_TABLE_LOOKUP(job_mode, JobMode);
1519
1520 static const char* const job_result_table[_JOB_RESULT_MAX] = {
1521 [JOB_DONE] = "done",
1522 [JOB_CANCELED] = "canceled",
1523 [JOB_TIMEOUT] = "timeout",
1524 [JOB_FAILED] = "failed",
1525 [JOB_DEPENDENCY] = "dependency",
1526 [JOB_SKIPPED] = "skipped",
1527 [JOB_INVALID] = "invalid",
1528 [JOB_ASSERT] = "assert",
1529 [JOB_UNSUPPORTED] = "unsupported",
1530 [JOB_COLLECTED] = "collected",
1531 };
1532
1533 DEFINE_STRING_TABLE_LOOKUP(job_result, JobResult);
1534
1535 const char* job_type_to_access_method(JobType t) {
1536 assert(t >= 0);
1537 assert(t < _JOB_TYPE_MAX);
1538
1539 if (IN_SET(t, JOB_START, JOB_RESTART, JOB_TRY_RESTART))
1540 return "start";
1541 else if (t == JOB_STOP)
1542 return "stop";
1543 else
1544 return "reload";
1545 }