]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/job.c
systemctl: port systemctl over to the new LookupPaths configuration directory fields
[thirdparty/systemd.git] / src / core / job.c
CommitLineData
a7334b09
LP
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
5430f7f2
LP
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
a7334b09
LP
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
5430f7f2 14 Lesser General Public License for more details.
a7334b09 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
a7334b09
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
1ffba6fe 20#include <errno.h>
60918275 21
718db961
LP
22#include "sd-id128.h"
23#include "sd-messages.h"
4f5dd394 24
b5efdb8a 25#include "alloc-util.h"
f485606b 26#include "async.h"
4f5dd394 27#include "dbus-job.h"
8f8f05a9 28#include "dbus.h"
4f5dd394 29#include "escape.h"
6bedfcbb 30#include "job.h"
4f5dd394
LP
31#include "log.h"
32#include "macro.h"
6bedfcbb 33#include "parse-util.h"
4f5dd394
LP
34#include "set.h"
35#include "special.h"
d054f0a4 36#include "stdio-util.h"
8b43440b 37#include "string-table.h"
07630cea 38#include "string-util.h"
4f5dd394 39#include "strv.h"
288a74cc 40#include "terminal-util.h"
4f5dd394
LP
41#include "unit.h"
42#include "virt.h"
97e6a119 43
39a18c60 44Job* job_new_raw(Unit *unit) {
60918275
LP
45 Job *j;
46
39a18c60
MS
47 /* used for deserialization */
48
87f0e418 49 assert(unit);
60918275 50
39a18c60
MS
51 j = new0(Job, 1);
52 if (!j)
60918275
LP
53 return NULL;
54
668ad332 55 j->manager = unit->manager;
87f0e418 56 j->unit = unit;
e0209d83 57 j->type = _JOB_TYPE_INVALID;
faf919f1 58
39a18c60
MS
59 return j;
60}
61
62Job* 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
e5b5ae50 74 /* We don't link it here, that's what job_dependency() is for */
60918275
LP
75
76 return j;
77}
78
97e7d748
MS
79void job_free(Job *j) {
80 assert(j);
81 assert(!j->installed);
02a3bcc6
MS
82 assert(!j->transaction_prev);
83 assert(!j->transaction_next);
84 assert(!j->subject_list);
85 assert(!j->object_list);
60918275 86
c1e1601e 87 if (j->in_run_queue)
71fda00f 88 LIST_REMOVE(run_queue, j->manager->run_queue, j);
c1e1601e
LP
89
90 if (j->in_dbus_queue)
71fda00f 91 LIST_REMOVE(dbus_queue, j->manager->dbus_job_queue, j);
c1e1601e 92
718db961 93 sd_event_source_unref(j->timer_event_source);
faf919f1 94
b39a2770
SW
95 sd_bus_track_unref(j->clients);
96 strv_free(j->deserialized_clients);
faf919f1 97
60918275
LP
98 free(j);
99}
a66d02c3 100
9c3349e2
LP
101static void job_set_state(Job *j, JobState state) {
102 assert(j);
103 assert(state >= 0);
104 assert(state < _JOB_STATE_MAX);
105
106 if (j->state == state)
107 return;
108
109 j->state = state;
110
111 if (!j->installed)
112 return;
113
114 if (j->state == JOB_RUNNING)
115 j->unit->manager->n_running_jobs++;
116 else {
117 assert(j->state == JOB_WAITING);
118 assert(j->unit->manager->n_running_jobs > 0);
119
120 j->unit->manager->n_running_jobs--;
121
122 if (j->unit->manager->n_running_jobs <= 0)
123 j->unit->manager->jobs_in_progress_event_source = sd_event_source_unref(j->unit->manager->jobs_in_progress_event_source);
124 }
125}
126
05d576f1 127void job_uninstall(Job *j) {
e0209d83
MS
128 Job **pj;
129
05d576f1 130 assert(j->installed);
e0209d83 131
9c3349e2
LP
132 job_set_state(j, JOB_WAITING);
133
e0209d83
MS
134 pj = (j->type == JOB_NOP) ? &j->unit->nop_job : &j->unit->job;
135 assert(*pj == j);
136
05d576f1
MS
137 /* Detach from next 'bigger' objects */
138
39a18c60
MS
139 /* daemon-reload should be transparent to job observers */
140 if (j->manager->n_reloading <= 0)
141 bus_job_send_removed_signal(j);
05d576f1 142
e0209d83
MS
143 *pj = NULL;
144
d6a093d0 145 unit_add_to_gc_queue(j->unit);
05d576f1
MS
146
147 hashmap_remove(j->manager->jobs, UINT32_TO_PTR(j->id));
148 j->installed = false;
149}
150
656bbffc
MS
151static bool job_type_allows_late_merge(JobType t) {
152 /* Tells whether it is OK to merge a job of type 't' with an already
153 * running job.
154 * Reloads cannot be merged this way. Think of the sequence:
155 * 1. Reload of a daemon is in progress; the daemon has already loaded
156 * its config file, but hasn't completed the reload operation yet.
157 * 2. Edit foo's config file.
158 * 3. Trigger another reload to have the daemon use the new config.
159 * Should the second reload job be merged into the first one, the daemon
160 * would not know about the new config.
161 * JOB_RESTART jobs on the other hand can be merged, because they get
162 * patched into JOB_START after stopping the unit. So if we see a
163 * JOB_RESTART running, it means the unit hasn't stopped yet and at
164 * this time the merge is still allowed. */
e0209d83 165 return t != JOB_RELOAD;
656bbffc
MS
166}
167
168static void job_merge_into_installed(Job *j, Job *other) {
169 assert(j->installed);
170 assert(j->unit == other->unit);
171
e0209d83
MS
172 if (j->type != JOB_NOP)
173 job_type_merge_and_collapse(&j->type, other->type, j->unit);
174 else
175 assert(other->type == JOB_NOP);
656bbffc 176
23ade460 177 j->irreversible = j->irreversible || other->irreversible;
e45460d6 178 j->ignore_order = j->ignore_order || other->ignore_order;
656bbffc
MS
179}
180
181Job* job_install(Job *j) {
e0209d83
MS
182 Job **pj;
183 Job *uj;
05d576f1 184
656bbffc 185 assert(!j->installed);
e0209d83 186 assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION);
9c3349e2 187 assert(j->state == JOB_WAITING);
e0209d83
MS
188
189 pj = (j->type == JOB_NOP) ? &j->unit->nop_job : &j->unit->job;
190 uj = *pj;
656bbffc 191
05d576f1 192 if (uj) {
61da906a 193 if (job_type_is_conflicting(uj->type, j->type))
1abc85b8 194 job_finish_and_invalidate(uj, JOB_CANCELED, false);
656bbffc
MS
195 else {
196 /* not conflicting, i.e. mergeable */
197
61da906a 198 if (uj->state == JOB_WAITING ||
656bbffc
MS
199 (job_type_allows_late_merge(j->type) && job_type_is_superset(uj->type, j->type))) {
200 job_merge_into_installed(uj, j);
f2341e0a 201 log_unit_debug(uj->unit,
66870f90
ZJS
202 "Merged into installed job %s/%s as %u",
203 uj->unit->id, job_type_to_string(uj->type), (unsigned) uj->id);
656bbffc
MS
204 return uj;
205 } else {
206 /* already running and not safe to merge into */
207 /* Patch uj to become a merged job and re-run it. */
208 /* XXX It should be safer to queue j to run after uj finishes, but it is
209 * not currently possible to have more than one installed job per unit. */
210 job_merge_into_installed(uj, j);
f2341e0a 211 log_unit_debug(uj->unit,
66870f90
ZJS
212 "Merged into running job, re-running: %s/%s as %u",
213 uj->unit->id, job_type_to_string(uj->type), (unsigned) uj->id);
9c3349e2
LP
214
215 job_set_state(uj, JOB_WAITING);
656bbffc
MS
216 return uj;
217 }
218 }
05d576f1
MS
219 }
220
656bbffc 221 /* Install the job */
e0209d83 222 *pj = j;
05d576f1 223 j->installed = true;
9c3349e2 224
313cefa1 225 j->manager->n_installed_jobs++;
f2341e0a 226 log_unit_debug(j->unit,
66870f90
ZJS
227 "Installed new job %s/%s as %u",
228 j->unit->id, job_type_to_string(j->type), (unsigned) j->id);
656bbffc 229 return j;
05d576f1
MS
230}
231
e0209d83
MS
232int job_install_deserialized(Job *j) {
233 Job **pj;
234
39a18c60
MS
235 assert(!j->installed);
236
e0209d83
MS
237 if (j->type < 0 || j->type >= _JOB_TYPE_MAX_IN_TRANSACTION) {
238 log_debug("Invalid job type %s in deserialization.", strna(job_type_to_string(j->type)));
239 return -EINVAL;
240 }
241
242 pj = (j->type == JOB_NOP) ? &j->unit->nop_job : &j->unit->job;
e0209d83 243 if (*pj) {
f2341e0a 244 log_unit_debug(j->unit, "Unit already has a job installed. Not installing deserialized job.");
e0209d83 245 return -EEXIST;
39a18c60 246 }
9c3349e2 247
e0209d83 248 *pj = j;
39a18c60 249 j->installed = true;
9c3349e2
LP
250
251 if (j->state == JOB_RUNNING)
252 j->unit->manager->n_running_jobs++;
253
f2341e0a 254 log_unit_debug(j->unit,
66870f90
ZJS
255 "Reinstalled deserialized job %s/%s as %u",
256 j->unit->id, job_type_to_string(j->type), (unsigned) j->id);
e0209d83 257 return 0;
39a18c60
MS
258}
259
1da4264f 260JobDependency* job_dependency_new(Job *subject, Job *object, bool matters, bool conflicts) {
e5b5ae50
LP
261 JobDependency *l;
262
263 assert(object);
264
265 /* Adds a new job link, which encodes that the 'subject' job
266 * needs the 'object' job in some way. If 'subject' is NULL
267 * this means the 'anchor' job (i.e. the one the user
35b8ca3a 268 * explicitly asked for) is the requester. */
e5b5ae50 269
ceed3570 270 if (!(l = new0(JobDependency, 1)))
e5b5ae50
LP
271 return NULL;
272
273 l->subject = subject;
274 l->object = object;
275 l->matters = matters;
69dd2852 276 l->conflicts = conflicts;
e5b5ae50 277
44d8db9e 278 if (subject)
71fda00f 279 LIST_PREPEND(subject, subject->subject_list, l);
e5b5ae50 280
71fda00f 281 LIST_PREPEND(object, object->object_list, l);
e5b5ae50
LP
282
283 return l;
284}
285
1da4264f 286void job_dependency_free(JobDependency *l) {
e5b5ae50
LP
287 assert(l);
288
44d8db9e 289 if (l->subject)
71fda00f 290 LIST_REMOVE(subject, l->subject->subject_list, l);
e5b5ae50 291
71fda00f 292 LIST_REMOVE(object, l->object->object_list, l);
e5b5ae50
LP
293
294 free(l);
295}
296
1ffba6fe 297void job_dump(Job *j, FILE*f, const char *prefix) {
a66d02c3
LP
298 assert(j);
299 assert(f);
300
9eb63b3c
LP
301 if (!prefix)
302 prefix = "";
303
ceed3570 304 fprintf(f,
40d50879
LP
305 "%s-> Job %u:\n"
306 "%s\tAction: %s -> %s\n"
5cb5a6ff 307 "%s\tState: %s\n"
23ade460 308 "%s\tIrreversible: %s\n",
ceed3570 309 prefix, j->id,
ac155bb8 310 prefix, j->unit->id, job_type_to_string(j->type),
94f04347 311 prefix, job_state_to_string(j->state),
23ade460 312 prefix, yes_no(j->irreversible));
a66d02c3 313}
e5b5ae50 314
348e27fe
MS
315/*
316 * Merging is commutative, so imagine the matrix as symmetric. We store only
317 * its lower triangle to avoid duplication. We don't store the main diagonal,
318 * because A merged with A is simply A.
319 *
e0209d83
MS
320 * If the resulting type is collapsed immediately afterwards (to get rid of
321 * the JOB_RELOAD_OR_START, which lies outside the lookup function's domain),
322 * the following properties hold:
323 *
48b4eab4 324 * Merging is associative! A merged with B, and then merged with C is the same
103635db 325 * as A merged with the result of B merged with C.
348e27fe
MS
326 *
327 * Mergeability is transitive! If A can be merged with B and B with C then
328 * A also with C.
329 *
330 * Also, if A merged with B cannot be merged with C, then either A or B cannot
331 * be merged with C either.
332 */
333static const JobType job_merging_table[] = {
e0209d83
MS
334/* What \ With * JOB_START JOB_VERIFY_ACTIVE JOB_STOP JOB_RELOAD */
335/*********************************************************************************/
348e27fe
MS
336/*JOB_START */
337/*JOB_VERIFY_ACTIVE */ JOB_START,
338/*JOB_STOP */ -1, -1,
339/*JOB_RELOAD */ JOB_RELOAD_OR_START, JOB_RELOAD, -1,
e0209d83 340/*JOB_RESTART */ JOB_RESTART, JOB_RESTART, -1, JOB_RESTART,
348e27fe
MS
341};
342
343JobType job_type_lookup_merge(JobType a, JobType b) {
e0209d83
MS
344 assert_cc(ELEMENTSOF(job_merging_table) == _JOB_TYPE_MAX_MERGING * (_JOB_TYPE_MAX_MERGING - 1) / 2);
345 assert(a >= 0 && a < _JOB_TYPE_MAX_MERGING);
346 assert(b >= 0 && b < _JOB_TYPE_MAX_MERGING);
1ffba6fe
LP
347
348 if (a == b)
348e27fe 349 return a;
1ffba6fe 350
348e27fe
MS
351 if (a < b) {
352 JobType tmp = a;
353 a = b;
354 b = tmp;
1ffba6fe 355 }
e094e853 356
348e27fe 357 return job_merging_table[(a - 1) * a / 2 + b];
e094e853 358}
cd2dbd7d 359
593fbdd2
LP
360bool job_type_is_redundant(JobType a, UnitActiveState b) {
361 switch (a) {
362
363 case JOB_START:
364 return
365 b == UNIT_ACTIVE ||
032ff4af 366 b == UNIT_RELOADING;
593fbdd2
LP
367
368 case JOB_STOP:
369 return
6124958c 370 b == UNIT_INACTIVE ||
fdf20a31 371 b == UNIT_FAILED;
593fbdd2
LP
372
373 case JOB_VERIFY_ACTIVE:
374 return
375 b == UNIT_ACTIVE ||
032ff4af 376 b == UNIT_RELOADING;
593fbdd2
LP
377
378 case JOB_RELOAD:
379 return
032ff4af 380 b == UNIT_RELOADING;
593fbdd2 381
593fbdd2
LP
382 case JOB_RESTART:
383 return
384 b == UNIT_ACTIVATING;
385
7e803f5e
MS
386 case JOB_NOP:
387 return true;
388
e0209d83
MS
389 default:
390 assert_not_reached("Invalid job type");
391 }
392}
393
c6497ccb 394JobType job_type_collapse(JobType t, Unit *u) {
e0209d83
MS
395 UnitActiveState s;
396
c6497ccb 397 switch (t) {
e0209d83 398
593fbdd2 399 case JOB_TRY_RESTART:
e0209d83
MS
400 s = unit_active_state(u);
401 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s))
c6497ccb
LP
402 return JOB_NOP;
403
404 return JOB_RESTART;
e0209d83 405
3282591d
LP
406 case JOB_TRY_RELOAD:
407 s = unit_active_state(u);
408 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s))
409 return JOB_NOP;
410
411 return JOB_RELOAD;
412
e0209d83
MS
413 case JOB_RELOAD_OR_START:
414 s = unit_active_state(u);
415 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s))
c6497ccb
LP
416 return JOB_START;
417
418 return JOB_RELOAD;
593fbdd2
LP
419
420 default:
c6497ccb 421 return t;
593fbdd2
LP
422 }
423}
424
e0209d83 425int job_type_merge_and_collapse(JobType *a, JobType b, Unit *u) {
c6497ccb
LP
426 JobType t;
427
428 t = job_type_lookup_merge(*a, b);
e0209d83
MS
429 if (t < 0)
430 return -EEXIST;
c6497ccb
LP
431
432 *a = job_type_collapse(t, u);
e0209d83
MS
433 return 0;
434}
435
9588bc32 436static bool job_is_runnable(Job *j) {
034c6ed7 437 Iterator i;
87f0e418 438 Unit *other;
5cb5a6ff
LP
439
440 assert(j);
ac1135be 441 assert(j->installed);
5cb5a6ff 442
87f0e418 443 /* Checks whether there is any job running for the units this
5cb5a6ff 444 * job needs to be running after (in the case of a 'positive'
e67c3609
LP
445 * job type) or before (in the case of a 'negative' job
446 * type. */
447
66ca4ec4
LP
448 /* Note that unit types have a say in what is runnable,
449 * too. For example, if they return -EAGAIN from
450 * unit_start() they can indicate they are not
451 * runnable yet. */
452
e67c3609 453 /* First check if there is an override */
cebe0d41 454 if (j->ignore_order)
e67c3609 455 return true;
5cb5a6ff 456
e0209d83
MS
457 if (j->type == JOB_NOP)
458 return true;
459
5cb5a6ff
LP
460 if (j->type == JOB_START ||
461 j->type == JOB_VERIFY_ACTIVE ||
e0209d83 462 j->type == JOB_RELOAD) {
5cb5a6ff
LP
463
464 /* Immediate result is that the job is or might be
fc08079e 465 * started. In this case let's wait for the
5cb5a6ff
LP
466 * dependencies, regardless whether they are
467 * starting or stopping something. */
468
ac155bb8
MS
469 SET_FOREACH(other, j->unit->dependencies[UNIT_AFTER], i)
470 if (other->job)
5cb5a6ff
LP
471 return false;
472 }
473
474 /* Also, if something else is being stopped and we should
fc08079e 475 * change state after it, then let's wait. */
5cb5a6ff 476
ac155bb8
MS
477 SET_FOREACH(other, j->unit->dependencies[UNIT_BEFORE], i)
478 if (other->job &&
479 (other->job->type == JOB_STOP ||
e0209d83 480 other->job->type == JOB_RESTART))
5cb5a6ff
LP
481 return false;
482
483 /* This means that for a service a and a service b where b
484 * shall be started after a:
485 *
486 * start a + start b → 1st step start a, 2nd step start b
487 * start a + stop b → 1st step stop b, 2nd step start a
488 * stop a + start b → 1st step stop a, 2nd step start b
489 * stop a + stop b → 1st step stop b, 2nd step stop a
490 *
491 * This has the side effect that restarts are properly
492 * synchronized too. */
493
494 return true;
495}
496
bbd1a837 497static void job_change_type(Job *j, JobType newtype) {
f2341e0a
LP
498 assert(j);
499
500 log_unit_debug(j->unit,
66870f90
ZJS
501 "Converting job %s/%s -> %s/%s",
502 j->unit->id, job_type_to_string(j->type),
503 j->unit->id, job_type_to_string(newtype));
bbd1a837
MS
504
505 j->type = newtype;
506}
507
d1a34ae9 508static int job_perform_on_unit(Job **j) {
df446f96
LP
509 uint32_t id;
510 Manager *m;
511 JobType t;
512 Unit *u;
d1a34ae9
MS
513 int r;
514
df446f96
LP
515 /* While we execute this operation the job might go away (for
516 * example: because it finishes immediately or is replaced by
517 * a new, conflicting job.) To make sure we don't access a
518 * freed job later on we store the id here, so that we can
519 * verify the job is still valid. */
520
521 assert(j);
522 assert(*j);
523
524 m = (*j)->manager;
525 u = (*j)->unit;
526 t = (*j)->type;
527 id = (*j)->id;
528
d1a34ae9
MS
529 switch (t) {
530 case JOB_START:
531 r = unit_start(u);
532 break;
533
534 case JOB_RESTART:
535 t = JOB_STOP;
df446f96 536 /* fall through */
d1a34ae9
MS
537 case JOB_STOP:
538 r = unit_stop(u);
539 break;
540
541 case JOB_RELOAD:
542 r = unit_reload(u);
543 break;
544
545 default:
546 assert_not_reached("Invalid job type");
547 }
548
549 /* Log if the job still exists and the start/stop/reload function
550 * actually did something. */
551 *j = manager_get_job(m, id);
552 if (*j && r > 0)
553 unit_status_emit_starting_stopping_reloading(u, t);
554
555 return r;
556}
557
5cb5a6ff
LP
558int job_run_and_invalidate(Job *j) {
559 int r;
ac1135be 560
5cb5a6ff 561 assert(j);
ac1135be 562 assert(j->installed);
e0209d83 563 assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION);
66aa6f7f 564 assert(j->in_run_queue);
5cb5a6ff 565
71fda00f 566 LIST_REMOVE(run_queue, j->manager->run_queue, j);
66aa6f7f 567 j->in_run_queue = false;
5cb5a6ff
LP
568
569 if (j->state != JOB_WAITING)
570 return 0;
571
034c6ed7
LP
572 if (!job_is_runnable(j))
573 return -EAGAIN;
574
9c3349e2 575 job_set_state(j, JOB_RUNNING);
c1e1601e 576 job_add_to_dbus_queue(j);
83c60c9f 577
2cf19a7a 578
5cb5a6ff
LP
579 switch (j->type) {
580
5cb5a6ff 581 case JOB_VERIFY_ACTIVE: {
87f0e418
LP
582 UnitActiveState t = unit_active_state(j->unit);
583 if (UNIT_IS_ACTIVE_OR_RELOADING(t))
5cb5a6ff 584 r = -EALREADY;
87f0e418 585 else if (t == UNIT_ACTIVATING)
5cb5a6ff
LP
586 r = -EAGAIN;
587 else
6a371e23 588 r = -EBADR;
5cb5a6ff
LP
589 break;
590 }
591
d1a34ae9 592 case JOB_START:
5cb5a6ff 593 case JOB_STOP:
dd17d388 594 case JOB_RESTART:
d1a34ae9 595 r = job_perform_on_unit(&j);
57339f47 596
d1a34ae9
MS
597 /* If the unit type does not support starting/stopping,
598 * then simply wait. */
57339f47
LP
599 if (r == -EBADR)
600 r = 0;
5cb5a6ff
LP
601 break;
602
603 case JOB_RELOAD:
d1a34ae9 604 r = job_perform_on_unit(&j);
5cb5a6ff
LP
605 break;
606
e0209d83
MS
607 case JOB_NOP:
608 r = -EALREADY;
609 break;
610
5cb5a6ff 611 default:
44d8db9e 612 assert_not_reached("Unknown job type");
5cb5a6ff
LP
613 }
614
e0209d83 615 if (j) {
2cf19a7a 616 if (r == -EALREADY)
5273510e 617 r = job_finish_and_invalidate(j, JOB_DONE, true);
6a371e23 618 else if (r == -EBADR)
5273510e 619 r = job_finish_and_invalidate(j, JOB_SKIPPED, true);
6a371e23
ZJS
620 else if (r == -ENOEXEC)
621 r = job_finish_and_invalidate(j, JOB_INVALID, true);
59fccdc5
LP
622 else if (r == -EPROTO)
623 r = job_finish_and_invalidate(j, JOB_ASSERT, true);
15411c0c 624 else if (r == -EOPNOTSUPP)
0faacd47 625 r = job_finish_and_invalidate(j, JOB_UNSUPPORTED, true);
9c3349e2
LP
626 else if (r == -EAGAIN)
627 job_set_state(j, JOB_WAITING);
628 else if (r < 0)
5273510e 629 r = job_finish_and_invalidate(j, JOB_FAILED, true);
2cf19a7a 630 }
5cb5a6ff
LP
631
632 return r;
633}
634
44a6b1b6 635_pure_ static const char *job_get_status_message_format(Unit *u, JobType t, JobResult result) {
df446f96 636
aa49ab5f
MS
637 static const char *const generic_finished_start_job[_JOB_RESULT_MAX] = {
638 [JOB_DONE] = "Started %s.",
639 [JOB_TIMEOUT] = "Timed out starting %s.",
640 [JOB_FAILED] = "Failed to start %s.",
641 [JOB_DEPENDENCY] = "Dependency failed for %s.",
642 [JOB_ASSERT] = "Assertion failed for %s.",
643 [JOB_UNSUPPORTED] = "Starting of %s not supported.",
644 };
645 static const char *const generic_finished_stop_job[_JOB_RESULT_MAX] = {
646 [JOB_DONE] = "Stopped %s.",
647 [JOB_FAILED] = "Stopped (with error) %s.",
b59f0ecd 648 [JOB_TIMEOUT] = "Timed out stopping %s.",
aa49ab5f
MS
649 };
650 static const char *const generic_finished_reload_job[_JOB_RESULT_MAX] = {
651 [JOB_DONE] = "Reloaded %s.",
652 [JOB_FAILED] = "Reload failed for %s.",
653 [JOB_TIMEOUT] = "Timed out reloading %s.",
654 };
655 /* When verify-active detects the unit is inactive, report it.
656 * Most likely a DEPEND warning from a requisiting unit will
657 * occur next and it's nice to see what was requisited. */
658 static const char *const generic_finished_verify_active_job[_JOB_RESULT_MAX] = {
659 [JOB_SKIPPED] = "%s is not active.",
660 };
877d54e9 661
df446f96
LP
662 const UnitStatusMessageFormats *format_table;
663 const char *format;
664
877d54e9
LP
665 assert(u);
666 assert(t >= 0);
667 assert(t < _JOB_TYPE_MAX);
c6918296 668
df446f96 669 if (IN_SET(t, JOB_START, JOB_STOP, JOB_RESTART)) {
aa49ab5f
MS
670 format_table = &UNIT_VTABLE(u)->status_message_formats;
671 if (format_table) {
672 format = t == JOB_START ? format_table->finished_start_job[result] :
673 format_table->finished_stop_job[result];
674 if (format)
675 return format;
676 }
677 }
877d54e9 678
aa49ab5f 679 /* Return generic strings */
877d54e9 680 if (t == JOB_START)
aa49ab5f 681 return generic_finished_start_job[result];
877d54e9 682 else if (t == JOB_STOP || t == JOB_RESTART)
aa49ab5f
MS
683 return generic_finished_stop_job[result];
684 else if (t == JOB_RELOAD)
685 return generic_finished_reload_job[result];
686 else if (t == JOB_VERIFY_ACTIVE)
687 return generic_finished_verify_active_job[result];
877d54e9
LP
688
689 return NULL;
690}
691
692static void job_print_status_message(Unit *u, JobType t, JobResult result) {
dc9b5816
ZJS
693 static struct {
694 const char *color, *word;
695 } const statuses[_JOB_RESULT_MAX] = {
696 [JOB_DONE] = {ANSI_GREEN, " OK "},
697 [JOB_TIMEOUT] = {ANSI_HIGHLIGHT_RED, " TIME "},
698 [JOB_FAILED] = {ANSI_HIGHLIGHT_RED, "FAILED"},
699 [JOB_DEPENDENCY] = {ANSI_HIGHLIGHT_YELLOW, "DEPEND"},
700 [JOB_SKIPPED] = {ANSI_HIGHLIGHT, " INFO "},
701 [JOB_ASSERT] = {ANSI_HIGHLIGHT_YELLOW, "ASSERT"},
702 [JOB_UNSUPPORTED] = {ANSI_HIGHLIGHT_YELLOW, "UNSUPP"},
aa49ab5f 703 };
e02cd6f7 704
df446f96 705 const char *format;
dc9b5816 706 const char *status;
df446f96 707
877d54e9
LP
708 assert(u);
709 assert(t >= 0);
710 assert(t < _JOB_TYPE_MAX);
711
df446f96
LP
712 /* Reload status messages have traditionally not been printed to console. */
713 if (t == JOB_RELOAD)
714 return;
715
aa49ab5f
MS
716 format = job_get_status_message_format(u, t, result);
717 if (!format)
718 return;
e02cd6f7 719
dc9b5816
ZJS
720 if (log_get_show_color())
721 status = strjoina(statuses[result].color, statuses[result].word, ANSI_NORMAL);
722 else
723 status = statuses[result].word;
724
aa49ab5f
MS
725 if (result != JOB_DONE)
726 manager_flip_auto_status(u->manager, true);
e02cd6f7 727
aa49ab5f 728 DISABLE_WARNING_FORMAT_NONLITERAL;
dc9b5816 729 unit_status_printf(u, status, format);
aa49ab5f 730 REENABLE_WARNING;
7cf82e0b 731
aa49ab5f 732 if (t == JOB_START && result == JOB_FAILED) {
df446f96 733 _cleanup_free_ char *quoted;
7cf82e0b 734
df446f96
LP
735 quoted = shell_maybe_quote(u->id);
736 manager_status_printf(u->manager, STATUS_TYPE_NORMAL, NULL, "See 'systemctl status %s' for details.", strna(quoted));
e02cd6f7
LP
737 }
738}
739
877d54e9
LP
740static void job_log_status_message(Unit *u, JobType t, JobResult result) {
741 const char *format;
742 char buf[LINE_MAX];
b81bbe53 743 sd_id128_t mid;
64f575d2
MS
744 static const int job_result_log_level[_JOB_RESULT_MAX] = {
745 [JOB_DONE] = LOG_INFO,
746 [JOB_CANCELED] = LOG_INFO,
747 [JOB_TIMEOUT] = LOG_ERR,
748 [JOB_FAILED] = LOG_ERR,
749 [JOB_DEPENDENCY] = LOG_WARNING,
750 [JOB_SKIPPED] = LOG_NOTICE,
751 [JOB_INVALID] = LOG_INFO,
752 [JOB_ASSERT] = LOG_WARNING,
753 [JOB_UNSUPPORTED] = LOG_WARNING,
754 };
877d54e9
LP
755
756 assert(u);
757 assert(t >= 0);
758 assert(t < _JOB_TYPE_MAX);
759
81270860
LP
760 /* Skip this if it goes to the console. since we already print
761 * to the console anyway... */
762
763 if (log_on_console())
764 return;
765
aa49ab5f 766 format = job_get_status_message_format(u, t, result);
877d54e9
LP
767 if (!format)
768 return;
769
bcfce235 770 DISABLE_WARNING_FORMAT_NONLITERAL;
d054f0a4 771 xsprintf(buf, format, unit_description(u));
bcfce235 772 REENABLE_WARNING;
877d54e9 773
df446f96
LP
774 switch (t) {
775
776 case JOB_START:
877d54e9 777 mid = result == JOB_DONE ? SD_MESSAGE_UNIT_STARTED : SD_MESSAGE_UNIT_FAILED;
df446f96
LP
778 break;
779
780 case JOB_RELOAD:
b81bbe53 781 mid = SD_MESSAGE_UNIT_RELOADED;
df446f96
LP
782 break;
783
784 case JOB_STOP:
785 case JOB_RESTART:
786 mid = SD_MESSAGE_UNIT_STOPPED;
787 break;
788
789 default:
64f575d2 790 log_struct(job_result_log_level[result],
4f29c6fe
MS
791 LOG_UNIT_ID(u),
792 LOG_MESSAGE("%s", buf),
793 "RESULT=%s", job_result_to_string(result),
794 NULL);
b81bbe53
MS
795 return;
796 }
797
64f575d2 798 log_struct(job_result_log_level[result],
b81bbe53
MS
799 LOG_MESSAGE_ID(mid),
800 LOG_UNIT_ID(u),
801 LOG_MESSAGE("%s", buf),
802 "RESULT=%s", job_result_to_string(result),
803 NULL);
877d54e9 804}
877d54e9 805
30961fa3
MS
806static void job_emit_status_message(Unit *u, JobType t, JobResult result) {
807
808 /* No message if the job did not actually do anything due to failed condition. */
809 if (t == JOB_START && result == JOB_DONE && !u->condition_result)
810 return;
811
812 job_log_status_message(u, t, result);
df446f96 813 job_print_status_message(u, t, result);
30961fa3
MS
814}
815
be7d9ff7
LP
816static void job_fail_dependencies(Unit *u, UnitDependency d) {
817 Unit *other;
818 Iterator i;
819
820 assert(u);
821
822 SET_FOREACH(other, u->dependencies[d], i) {
823 Job *j = other->job;
824
825 if (!j)
826 continue;
827 if (!IN_SET(j->type, JOB_START, JOB_VERIFY_ACTIVE))
828 continue;
829
830 job_finish_and_invalidate(j, JOB_DEPENDENCY, true);
831 }
832}
833
5273510e 834int job_finish_and_invalidate(Job *j, JobResult result, bool recursive) {
87f0e418
LP
835 Unit *u;
836 Unit *other;
b866264a 837 JobType t;
034c6ed7 838 Iterator i;
5cb5a6ff
LP
839
840 assert(j);
ac1135be 841 assert(j->installed);
e0209d83 842 assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION);
5cb5a6ff 843
c6918296
MS
844 u = j->unit;
845 t = j->type;
846
847 j->result = result;
848
f2341e0a 849 log_unit_debug(u, "Job %s/%s finished, result=%s", u->id, job_type_to_string(t), job_result_to_string(result));
c6918296 850
30961fa3 851 job_emit_status_message(u, t, result);
c6918296 852
c1e1601e 853 job_add_to_dbus_queue(j);
f50e0a01 854
034c6ed7 855 /* Patch restart jobs so that they become normal start jobs */
c6918296 856 if (result == JOB_DONE && t == JOB_RESTART) {
f50e0a01 857
bbd1a837 858 job_change_type(j, JOB_START);
9c3349e2 859 job_set_state(j, JOB_WAITING);
cc42e081
LP
860
861 job_add_to_run_queue(j);
57981b98 862
57981b98 863 goto finish;
5cb5a6ff
LP
864 }
865
6a371e23 866 if (result == JOB_FAILED || result == JOB_INVALID)
313cefa1 867 j->manager->n_failed_jobs++;
76bf48b7 868
97e7d748 869 job_uninstall(j);
5cb5a6ff
LP
870 job_free(j);
871
872 /* Fail depending jobs on failure */
5273510e 873 if (result != JOB_DONE && recursive) {
be7d9ff7
LP
874 if (IN_SET(t, JOB_START, JOB_VERIFY_ACTIVE)) {
875 job_fail_dependencies(u, UNIT_REQUIRED_BY);
876 job_fail_dependencies(u, UNIT_REQUISITE_OF);
877 job_fail_dependencies(u, UNIT_BOUND_BY);
be7d9ff7
LP
878 } else if (t == JOB_STOP)
879 job_fail_dependencies(u, UNIT_CONFLICTED_BY);
5cb5a6ff
LP
880 }
881
c0daa706 882 /* Trigger OnFailure dependencies that are not generated by
66870f90 883 * the unit itself. We don't treat JOB_CANCELED as failure in
c0daa706
LP
884 * this context. And JOB_FAILURE is already handled by the
885 * unit itself. */
222ae6a8 886 if (result == JOB_TIMEOUT || result == JOB_DEPENDENCY) {
f2341e0a
LP
887 log_struct(LOG_NOTICE,
888 "JOB_TYPE=%s", job_type_to_string(t),
889 "JOB_RESULT=%s", job_result_to_string(result),
890 LOG_UNIT_ID(u),
891 LOG_UNIT_MESSAGE(u, "Job %s/%s failed with result '%s'.",
e2cc6eca
LP
892 u->id,
893 job_type_to_string(t),
894 job_result_to_string(result)),
f2341e0a 895 NULL);
222ae6a8 896
3ecaa09b 897 unit_start_on_failure(u);
222ae6a8 898 }
c0daa706 899
3ecaa09b
LP
900 unit_trigger_notify(u);
901
57981b98 902finish:
5cb5a6ff 903 /* Try to start the next jobs that can be started */
ac155bb8
MS
904 SET_FOREACH(other, u->dependencies[UNIT_AFTER], i)
905 if (other->job)
906 job_add_to_run_queue(other->job);
907 SET_FOREACH(other, u->dependencies[UNIT_BEFORE], i)
908 if (other->job)
909 job_add_to_run_queue(other->job);
5cb5a6ff 910
ac155bb8 911 manager_check_finished(u->manager);
b0c918b9 912
5273510e 913 return 0;
5cb5a6ff 914}
034c6ed7 915
718db961
LP
916static int job_dispatch_timer(sd_event_source *s, uint64_t monotonic, void *userdata) {
917 Job *j = userdata;
f189ab18 918 Unit *u;
faf919f1 919
718db961
LP
920 assert(j);
921 assert(s == j->timer_event_source);
faf919f1 922
f2341e0a 923 log_unit_warning(j->unit, "Job %s/%s timed out.", j->unit->id, job_type_to_string(j->type));
faf919f1 924
f189ab18 925 u = j->unit;
718db961 926 job_finish_and_invalidate(j, JOB_TIMEOUT, true);
f189ab18
LP
927
928 failure_action(u->manager, u->job_timeout_action, u->job_timeout_reboot_arg);
929
718db961
LP
930 return 0;
931}
faf919f1 932
718db961
LP
933int job_start_timer(Job *j) {
934 int r;
faf919f1 935
8bb310c3 936 if (j->timer_event_source)
718db961 937 return 0;
faf919f1 938
718db961 939 j->begin_usec = now(CLOCK_MONOTONIC);
faf919f1 940
36c16a7c 941 if (j->unit->job_timeout == USEC_INFINITY)
8bb310c3
ZJS
942 return 0;
943
6a0f1f6d
LP
944 r = sd_event_add_time(
945 j->manager->event,
946 &j->timer_event_source,
947 CLOCK_MONOTONIC,
36c16a7c 948 usec_add(j->begin_usec, j->unit->job_timeout), 0,
6a0f1f6d 949 job_dispatch_timer, j);
718db961
LP
950 if (r < 0)
951 return r;
faf919f1 952
7dfbe2e3
TG
953 (void) sd_event_source_set_description(j->timer_event_source, "job-start");
954
718db961 955 return 0;
faf919f1
LP
956}
957
c1e1601e 958void job_add_to_run_queue(Job *j) {
034c6ed7 959 assert(j);
ac1135be 960 assert(j->installed);
034c6ed7
LP
961
962 if (j->in_run_queue)
963 return;
964
752b5905
LP
965 if (!j->manager->run_queue)
966 sd_event_source_set_enabled(j->manager->run_queue_event_source, SD_EVENT_ONESHOT);
967
71fda00f 968 LIST_PREPEND(run_queue, j->manager->run_queue, j);
034c6ed7
LP
969 j->in_run_queue = true;
970}
94f04347 971
c1e1601e
LP
972void job_add_to_dbus_queue(Job *j) {
973 assert(j);
974 assert(j->installed);
975
976 if (j->in_dbus_queue)
977 return;
978
a567261a
LP
979 /* We don't check if anybody is subscribed here, since this
980 * job might just have been created and not yet assigned to a
981 * connection/client. */
94b6dfa2 982
71fda00f 983 LIST_PREPEND(dbus_queue, j->manager->dbus_job_queue, j);
c1e1601e
LP
984 j->in_dbus_queue = true;
985}
986
ea430986
LP
987char *job_dbus_path(Job *j) {
988 char *p;
989
990 assert(j);
991
ccd06097 992 if (asprintf(&p, "/org/freedesktop/systemd1/job/%"PRIu32, j->id) < 0)
ea430986
LP
993 return NULL;
994
995 return p;
996}
997
39a18c60
MS
998int job_serialize(Job *j, FILE *f, FDSet *fds) {
999 fprintf(f, "job-id=%u\n", j->id);
1000 fprintf(f, "job-type=%s\n", job_type_to_string(j->type));
1001 fprintf(f, "job-state=%s\n", job_state_to_string(j->state));
23ade460 1002 fprintf(f, "job-irreversible=%s\n", yes_no(j->irreversible));
39a18c60
MS
1003 fprintf(f, "job-sent-dbus-new-signal=%s\n", yes_no(j->sent_dbus_new_signal));
1004 fprintf(f, "job-ignore-order=%s\n", yes_no(j->ignore_order));
718db961
LP
1005
1006 if (j->begin_usec > 0)
ccd06097 1007 fprintf(f, "job-begin="USEC_FMT"\n", j->begin_usec);
718db961 1008
b39a2770 1009 bus_track_serialize(j->clients, f);
39a18c60
MS
1010
1011 /* End marker */
1012 fputc('\n', f);
1013 return 0;
1014}
1015
1016int job_deserialize(Job *j, FILE *f, FDSet *fds) {
718db961
LP
1017 assert(j);
1018
39a18c60
MS
1019 for (;;) {
1020 char line[LINE_MAX], *l, *v;
1021 size_t k;
1022
1023 if (!fgets(line, sizeof(line), f)) {
1024 if (feof(f))
1025 return 0;
1026 return -errno;
1027 }
1028
1029 char_array_0(line);
1030 l = strstrip(line);
1031
1032 /* End marker */
1033 if (l[0] == 0)
1034 return 0;
1035
1036 k = strcspn(l, "=");
1037
1038 if (l[k] == '=') {
1039 l[k] = 0;
1040 v = l+k+1;
1041 } else
1042 v = l+k;
1043
1044 if (streq(l, "job-id")) {
718db961 1045
39a18c60
MS
1046 if (safe_atou32(v, &j->id) < 0)
1047 log_debug("Failed to parse job id value %s", v);
718db961 1048
39a18c60 1049 } else if (streq(l, "job-type")) {
718db961
LP
1050 JobType t;
1051
1052 t = job_type_from_string(v);
39a18c60
MS
1053 if (t < 0)
1054 log_debug("Failed to parse job type %s", v);
e0209d83
MS
1055 else if (t >= _JOB_TYPE_MAX_IN_TRANSACTION)
1056 log_debug("Cannot deserialize job of type %s", v);
39a18c60
MS
1057 else
1058 j->type = t;
718db961 1059
39a18c60 1060 } else if (streq(l, "job-state")) {
718db961
LP
1061 JobState s;
1062
1063 s = job_state_from_string(v);
39a18c60
MS
1064 if (s < 0)
1065 log_debug("Failed to parse job state %s", v);
1066 else
9c3349e2 1067 job_set_state(j, s);
718db961 1068
23ade460 1069 } else if (streq(l, "job-irreversible")) {
718db961
LP
1070 int b;
1071
1072 b = parse_boolean(v);
23ade460
MS
1073 if (b < 0)
1074 log_debug("Failed to parse job irreversible flag %s", v);
1075 else
1076 j->irreversible = j->irreversible || b;
718db961 1077
39a18c60 1078 } else if (streq(l, "job-sent-dbus-new-signal")) {
718db961
LP
1079 int b;
1080
1081 b = parse_boolean(v);
39a18c60
MS
1082 if (b < 0)
1083 log_debug("Failed to parse job sent_dbus_new_signal flag %s", v);
1084 else
1085 j->sent_dbus_new_signal = j->sent_dbus_new_signal || b;
718db961 1086
39a18c60 1087 } else if (streq(l, "job-ignore-order")) {
718db961
LP
1088 int b;
1089
1090 b = parse_boolean(v);
39a18c60
MS
1091 if (b < 0)
1092 log_debug("Failed to parse job ignore_order flag %s", v);
1093 else
1094 j->ignore_order = j->ignore_order || b;
718db961
LP
1095
1096 } else if (streq(l, "job-begin")) {
1097 unsigned long long ull;
1098
1099 if (sscanf(v, "%llu", &ull) != 1)
1100 log_debug("Failed to parse job-begin value %s", v);
39a18c60 1101 else
718db961
LP
1102 j->begin_usec = ull;
1103
8f8f05a9 1104 } else if (streq(l, "subscribed")) {
718db961 1105
b39a2770 1106 if (strv_extend(&j->deserialized_clients, v) < 0)
8f8f05a9 1107 return log_oom();
39a18c60
MS
1108 }
1109 }
1110}
1111
1112int job_coldplug(Job *j) {
718db961
LP
1113 int r;
1114
1115 assert(j);
39a18c60 1116
8f8f05a9
LP
1117 /* After deserialization is complete and the bus connection
1118 * set up again, let's start watching our subscribers again */
b39a2770 1119 r = bus_track_coldplug(j->manager, &j->clients, &j->deserialized_clients);
8f8f05a9
LP
1120 if (r < 0)
1121 return r;
1122
1727a595
MM
1123 if (j->state == JOB_WAITING)
1124 job_add_to_run_queue(j);
1125
36c16a7c 1126 if (j->begin_usec == 0 || j->unit->job_timeout == USEC_INFINITY)
39a18c60
MS
1127 return 0;
1128
36c16a7c 1129 j->timer_event_source = sd_event_source_unref(j->timer_event_source);
39a18c60 1130
6a0f1f6d
LP
1131 r = sd_event_add_time(
1132 j->manager->event,
1133 &j->timer_event_source,
1134 CLOCK_MONOTONIC,
36c16a7c 1135 usec_add(j->begin_usec, j->unit->job_timeout), 0,
6a0f1f6d 1136 job_dispatch_timer, j);
718db961 1137 if (r < 0)
da927ba9 1138 log_debug_errno(r, "Failed to restart timeout for job: %m");
718db961 1139
7dfbe2e3
TG
1140 (void) sd_event_source_set_description(j->timer_event_source, "job-timeout");
1141
718db961 1142 return r;
39a18c60
MS
1143}
1144
c65eb836
LP
1145void job_shutdown_magic(Job *j) {
1146 assert(j);
1147
1148 /* The shutdown target gets some special treatment here: we
1149 * tell the kernel to begin with flushing its disk caches, to
1150 * optimize shutdown time a bit. Ideally we wouldn't hardcode
1151 * this magic into PID 1. However all other processes aren't
1152 * options either since they'd exit much sooner than PID 1 and
1153 * asynchronous sync() would cause their exit to be
1154 * delayed. */
1155
c2756a68 1156 if (j->type != JOB_START)
c65eb836
LP
1157 return;
1158
b2c23da8 1159 if (j->unit->manager->running_as != MANAGER_SYSTEM)
c2756a68
LP
1160 return;
1161
1162 if (!unit_has_name(j->unit, SPECIAL_SHUTDOWN_TARGET))
c65eb836
LP
1163 return;
1164
5b1869ea
OB
1165 /* In case messages on console has been disabled on boot */
1166 j->unit->manager->no_console_output = false;
1167
75f86906 1168 if (detect_container() > 0)
c65eb836
LP
1169 return;
1170
1171 asynchronous_sync();
1172}
1173
7a7821c8
LP
1174int job_get_timeout(Job *j, usec_t *timeout) {
1175 usec_t x = USEC_INFINITY, y = USEC_INFINITY;
68db7a3b 1176 Unit *u = j->unit;
7a7821c8 1177 int r;
68db7a3b
ZJS
1178
1179 assert(u);
1180
1181 if (j->timer_event_source) {
1182 r = sd_event_source_get_time(j->timer_event_source, &x);
1183 if (r < 0)
1184 return r;
68db7a3b
ZJS
1185 }
1186
1187 if (UNIT_VTABLE(u)->get_timeout) {
7a7821c8
LP
1188 r = UNIT_VTABLE(u)->get_timeout(u, &y);
1189 if (r < 0)
1190 return r;
68db7a3b
ZJS
1191 }
1192
7a7821c8 1193 if (x == USEC_INFINITY && y == USEC_INFINITY)
68db7a3b
ZJS
1194 return 0;
1195
1196 *timeout = MIN(x, y);
68db7a3b
ZJS
1197 return 1;
1198}
1199
94f04347
LP
1200static const char* const job_state_table[_JOB_STATE_MAX] = {
1201 [JOB_WAITING] = "waiting",
1202 [JOB_RUNNING] = "running"
1203};
1204
1205DEFINE_STRING_TABLE_LOOKUP(job_state, JobState);
1206
1207static const char* const job_type_table[_JOB_TYPE_MAX] = {
1208 [JOB_START] = "start",
1209 [JOB_VERIFY_ACTIVE] = "verify-active",
1210 [JOB_STOP] = "stop",
1211 [JOB_RELOAD] = "reload",
1212 [JOB_RELOAD_OR_START] = "reload-or-start",
1213 [JOB_RESTART] = "restart",
1214 [JOB_TRY_RESTART] = "try-restart",
3282591d 1215 [JOB_TRY_RELOAD] = "try-reload",
e0209d83 1216 [JOB_NOP] = "nop",
94f04347
LP
1217};
1218
1219DEFINE_STRING_TABLE_LOOKUP(job_type, JobType);
b548631a
LP
1220
1221static const char* const job_mode_table[_JOB_MODE_MAX] = {
1222 [JOB_FAIL] = "fail",
c497c7a9 1223 [JOB_REPLACE] = "replace",
23ade460 1224 [JOB_REPLACE_IRREVERSIBLY] = "replace-irreversibly",
e67c3609 1225 [JOB_ISOLATE] = "isolate",
2c5859af 1226 [JOB_FLUSH] = "flush",
cebe0d41 1227 [JOB_IGNORE_DEPENDENCIES] = "ignore-dependencies",
255baef6 1228 [JOB_IGNORE_REQUIREMENTS] = "ignore-requirements",
b548631a
LP
1229};
1230
1231DEFINE_STRING_TABLE_LOOKUP(job_mode, JobMode);
5d44db4a
LP
1232
1233static const char* const job_result_table[_JOB_RESULT_MAX] = {
1234 [JOB_DONE] = "done",
1235 [JOB_CANCELED] = "canceled",
1236 [JOB_TIMEOUT] = "timeout",
1237 [JOB_FAILED] = "failed",
d68201e9 1238 [JOB_DEPENDENCY] = "dependency",
6a371e23
ZJS
1239 [JOB_SKIPPED] = "skipped",
1240 [JOB_INVALID] = "invalid",
59fccdc5 1241 [JOB_ASSERT] = "assert",
0faacd47 1242 [JOB_UNSUPPORTED] = "unsupported",
5d44db4a
LP
1243};
1244
1245DEFINE_STRING_TABLE_LOOKUP(job_result, JobResult);
94bd7323
EV
1246
1247const char* job_type_to_access_method(JobType t) {
1248 assert(t >= 0);
1249 assert(t < _JOB_TYPE_MAX);
1250
1251 if (IN_SET(t, JOB_START, JOB_RESTART, JOB_TRY_RESTART))
1252 return "start";
1253 else if (t == JOB_STOP)
1254 return "stop";
1255 else
1256 return "reload";
1257}