]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/job.c
udev: gracefully handle ENODEV or friends in opening device node
[thirdparty/systemd.git] / src / core / job.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
a7334b09 2
836e4e7e 3#include "sd-bus.h"
718db961 4#include "sd-messages.h"
4f5dd394 5
b5efdb8a 6#include "alloc-util.h"
b7120388 7#include "ansi-color.h"
f485606b 8#include "async.h"
da8e1782 9#include "cgroup.h"
836e4e7e 10#include "condition.h"
8f8f05a9 11#include "dbus.h"
1cf40697 12#include "dbus-job.h"
4f5dd394 13#include "escape.h"
6bedfcbb 14#include "job.h"
4f5dd394 15#include "log.h"
4ea4abb6 16#include "manager.h"
6bedfcbb 17#include "parse-util.h"
836e4e7e 18#include "prioq.h"
d68c645b 19#include "serialize.h"
4f5dd394 20#include "set.h"
760877e9 21#include "sort-util.h"
4f5dd394 22#include "special.h"
8b43440b 23#include "string-table.h"
07630cea 24#include "string-util.h"
4f5dd394 25#include "strv.h"
4f5dd394
LP
26#include "unit.h"
27#include "virt.h"
97e6a119 28
39a18c60 29Job* job_new_raw(Unit *unit) {
60918275
LP
30 Job *j;
31
39a18c60
MS
32 /* used for deserialization */
33
87f0e418 34 assert(unit);
60918275 35
08ac00f2 36 j = new(Job, 1);
39a18c60 37 if (!j)
60918275
LP
38 return NULL;
39
08ac00f2
LP
40 *j = (Job) {
41 .manager = unit->manager,
42 .unit = unit,
43 .type = _JOB_TYPE_INVALID,
44 };
faf919f1 45
39a18c60
MS
46 return j;
47}
48
3e1aec30
YW
49static uint32_t manager_get_new_job_id(Manager *m) {
50 bool overflow = false;
51
52 assert(m);
53
54 for (;;) {
55 uint32_t id = m->current_job_id;
56
57 if (_unlikely_(id == UINT32_MAX)) {
58 assert_se(!overflow);
59 m->current_job_id = 1;
60 overflow = true;
61 } else
62 m->current_job_id++;
63
64 if (hashmap_contains(m->jobs, UINT32_TO_PTR(id)))
65 continue;
66
67 return id;
68 }
69}
70
39a18c60
MS
71Job* job_new(Unit *unit, JobType type) {
72 Job *j;
73
74 assert(type < _JOB_TYPE_MAX);
75
76 j = job_new_raw(unit);
77 if (!j)
78 return NULL;
79
3e1aec30 80 j->id = manager_get_new_job_id(j->manager);
39a18c60
MS
81 j->type = type;
82
e5b5ae50 83 /* We don't link it here, that's what job_dependency() is for */
60918275
LP
84
85 return j;
86}
87
a7a7163d 88void job_unlink(Job *j) {
97e7d748
MS
89 assert(j);
90 assert(!j->installed);
02a3bcc6
MS
91 assert(!j->transaction_prev);
92 assert(!j->transaction_next);
93 assert(!j->subject_list);
94 assert(!j->object_list);
60918275 95
a7a7163d 96 if (j->in_run_queue) {
da8e1782 97 prioq_remove(j->manager->run_queue, j, &j->run_queue_idx);
a7a7163d
DT
98 j->in_run_queue = false;
99 }
c1e1601e 100
a7a7163d 101 if (j->in_dbus_queue) {
71fda00f 102 LIST_REMOVE(dbus_queue, j->manager->dbus_job_queue, j);
a7a7163d
DT
103 j->in_dbus_queue = false;
104 }
c1e1601e 105
a7a7163d 106 if (j->in_gc_queue) {
c5a97ed1 107 LIST_REMOVE(gc_queue, j->manager->gc_job_queue, j);
a7a7163d
DT
108 j->in_gc_queue = false;
109 }
c5a97ed1 110
5dcadb4c 111 j->timer_event_source = sd_event_source_disable_unref(j->timer_event_source);
a7a7163d
DT
112}
113
728ba51e 114Job* job_free(Job *j) {
a7a7163d
DT
115 assert(j);
116 assert(!j->installed);
117 assert(!j->transaction_prev);
118 assert(!j->transaction_next);
119 assert(!j->subject_list);
120 assert(!j->object_list);
121
122 job_unlink(j);
faf919f1 123
1a465207 124 sd_bus_track_unref(j->bus_track);
b39a2770 125 strv_free(j->deserialized_clients);
faf919f1 126
48b92b37
LB
127 activation_details_unref(j->activation_details);
128
728ba51e 129 return mfree(j);
60918275 130}
a66d02c3 131
9c3349e2
LP
132static void job_set_state(Job *j, JobState state) {
133 assert(j);
6985e396 134 assert(j->manager);
9c3349e2
LP
135 assert(state >= 0);
136 assert(state < _JOB_STATE_MAX);
137
138 if (j->state == state)
139 return;
140
141 j->state = state;
142
143 if (!j->installed)
144 return;
145
146 if (j->state == JOB_RUNNING)
6985e396 147 j->manager->n_running_jobs++;
9c3349e2
LP
148 else {
149 assert(j->state == JOB_WAITING);
6985e396 150 assert(j->manager->n_running_jobs > 0);
9c3349e2 151
6985e396 152 j->manager->n_running_jobs--;
9c3349e2 153
6985e396
MY
154 if (j->manager->n_running_jobs <= 0)
155 j->manager->jobs_in_progress_event_source = sd_event_source_disable_unref(j->manager->jobs_in_progress_event_source);
9c3349e2
LP
156 }
157}
158
05d576f1 159void job_uninstall(Job *j) {
e0209d83
MS
160 Job **pj;
161
215ffd6c 162 assert(j);
05d576f1 163 assert(j->installed);
e0209d83 164
9c3349e2
LP
165 job_set_state(j, JOB_WAITING);
166
0b6a4795 167 pj = j->type == JOB_NOP ? &j->unit->nop_job : &j->unit->job;
e0209d83
MS
168 assert(*pj == j);
169
05d576f1
MS
170 /* Detach from next 'bigger' objects */
171
39a18c60 172 /* daemon-reload should be transparent to job observers */
2c289ea8 173 if (!MANAGER_IS_RELOADING(j->manager))
39a18c60 174 bus_job_send_removed_signal(j);
05d576f1 175
e0209d83
MS
176 *pj = NULL;
177
d6a093d0 178 unit_add_to_gc_queue(j->unit);
05d576f1 179
52c6c9ea
AM
180 unit_add_to_dbus_queue(j->unit); /* The Job property of the unit has changed now */
181
48235ad6 182 hashmap_remove_value(j->manager->jobs, UINT32_TO_PTR(j->id), j);
05d576f1
MS
183 j->installed = false;
184}
185
569269d0
MY
186static bool jobs_may_late_merge(const Job *j, const Job *uj) {
187 assert(j);
188 assert(!j->installed);
189 assert(uj);
190 assert(uj->installed);
191 assert(uj->state == JOB_RUNNING);
192
193 /* Tells whether it is OK to merge a job with an already running job. */
194
195 if (j->refuse_late_merge) /* refused when constructing transaction? */
196 return false;
197
198 /* Reloads cannot be merged this way. Think of the sequence:
199 * 1. Reload of a daemon is in progress; the daemon has already loaded its config file, but hasn't
200 * completed the reload operation yet.
656bbffc
MS
201 * 2. Edit foo's config file.
202 * 3. Trigger another reload to have the daemon use the new config.
569269d0
MY
203 * Should the second reload job be merged into the first one, the daemon would not know about the new config.
204 * JOB_RESTART jobs on the other hand can be merged, because they get patched into JOB_START
205 * after stopping the unit. So if we see a JOB_RESTART running, it means the unit hasn't stopped yet
206 * and at this time the merge is still allowed. */
207 if (j->type == JOB_RELOAD)
208 return false;
209
210 return job_type_is_superset(uj->type, j->type);
656bbffc
MS
211}
212
213static void job_merge_into_installed(Job *j, Job *other) {
569269d0 214 assert(j);
656bbffc 215 assert(j->installed);
569269d0 216 assert(other);
656bbffc
MS
217 assert(j->unit == other->unit);
218
48b92b37 219 if (j->type != JOB_NOP) {
53a2383b 220 assert_se(job_type_merge_and_collapse(&j->type, other->type, j->unit) == 0);
48b92b37
LB
221
222 /* Keep the oldest ActivationDetails, if any */
223 if (!j->activation_details)
224 j->activation_details = TAKE_PTR(other->activation_details);
225 } else
e0209d83 226 assert(other->type == JOB_NOP);
656bbffc 227
23ade460 228 j->irreversible = j->irreversible || other->irreversible;
e45460d6 229 j->ignore_order = j->ignore_order || other->ignore_order;
656bbffc
MS
230}
231
569269d0 232Job* job_install(Job *j) {
e0209d83
MS
233 Job **pj;
234 Job *uj;
05d576f1 235
39939e7d 236 assert(j);
656bbffc 237 assert(!j->installed);
569269d0 238 assert(j->type >= 0 && j->type < _JOB_TYPE_MAX_IN_TRANSACTION);
9c3349e2 239 assert(j->state == JOB_WAITING);
e0209d83 240
0b6a4795 241 pj = j->type == JOB_NOP ? &j->unit->nop_job : &j->unit->job;
e0209d83 242 uj = *pj;
656bbffc 243
05d576f1 244 if (uj) {
61da906a 245 if (job_type_is_conflicting(uj->type, j->type))
833f92ad 246 job_finish_and_invalidate(uj, JOB_CANCELED, false, false);
656bbffc
MS
247 else {
248 /* not conflicting, i.e. mergeable */
249
569269d0 250 if (uj->state == JOB_WAITING || jobs_may_late_merge(j, uj)) {
656bbffc 251 job_merge_into_installed(uj, j);
f2341e0a 252 log_unit_debug(uj->unit,
28d78d07 253 "Merged %s/%s into installed job %s/%s as %"PRIu32,
92e29d82 254 j->unit->id, job_type_to_string(j->type), uj->unit->id,
28d78d07 255 job_type_to_string(uj->type), uj->id);
656bbffc
MS
256 return uj;
257 } else {
258 /* already running and not safe to merge into */
259 /* Patch uj to become a merged job and re-run it. */
260 /* XXX It should be safer to queue j to run after uj finishes, but it is
261 * not currently possible to have more than one installed job per unit. */
262 job_merge_into_installed(uj, j);
f2341e0a 263 log_unit_debug(uj->unit,
28d78d07
JK
264 "Merged into running job, re-running: %s/%s as %"PRIu32,
265 uj->unit->id, job_type_to_string(uj->type), uj->id);
9c3349e2
LP
266
267 job_set_state(uj, JOB_WAITING);
656bbffc
MS
268 return uj;
269 }
270 }
05d576f1
MS
271 }
272
656bbffc 273 /* Install the job */
6ff13400 274 assert(!*pj);
e0209d83 275 *pj = j;
05d576f1 276 j->installed = true;
9c3349e2 277
313cefa1 278 j->manager->n_installed_jobs++;
f2341e0a 279 log_unit_debug(j->unit,
66870f90
ZJS
280 "Installed new job %s/%s as %u",
281 j->unit->id, job_type_to_string(j->type), (unsigned) j->id);
c5a97ed1
LP
282
283 job_add_to_gc_queue(j);
284
e6d05912
LP
285 job_add_to_dbus_queue(j); /* announce this job to clients */
286 unit_add_to_dbus_queue(j->unit); /* The Job property of the unit has changed now */
287
656bbffc 288 return j;
05d576f1
MS
289}
290
e0209d83
MS
291int job_install_deserialized(Job *j) {
292 Job **pj;
b17c9620 293 int r;
e0209d83 294
6985e396
MY
295 assert(j);
296 assert(j->manager);
39a18c60
MS
297 assert(!j->installed);
298
baaa35ad 299 if (j->type < 0 || j->type >= _JOB_TYPE_MAX_IN_TRANSACTION)
b17c9620 300 return log_unit_debug_errno(j->unit, SYNTHETIC_ERRNO(EINVAL),
ace15701
YW
301 "Invalid job type %s in deserialization.",
302 strna(job_type_to_string(j->type)));
e0209d83 303
0b6a4795 304 pj = j->type == JOB_NOP ? &j->unit->nop_job : &j->unit->job;
b17c9620
LP
305 if (*pj)
306 return log_unit_debug_errno(j->unit, SYNTHETIC_ERRNO(EEXIST),
307 "Unit already has a job installed. Not installing deserialized job.");
308
aafb96d5
YW
309 /* When the job does not have ID, or we failed to deserialize the job ID, then use a new ID. */
310 if (j->id <= 0)
311 j->id = manager_get_new_job_id(j->manager);
312
53dba3ef 313 r = hashmap_ensure_put(&j->manager->jobs, NULL, UINT32_TO_PTR(j->id), j);
b17c9620
LP
314 if (r == -EEXIST)
315 return log_unit_debug_errno(j->unit, r, "Job ID %" PRIu32 " already used, cannot deserialize job.", j->id);
316 if (r < 0)
317 return log_unit_debug_errno(j->unit, r, "Failed to insert job into jobs hash table: %m");
9c3349e2 318
e0209d83 319 *pj = j;
39a18c60 320 j->installed = true;
9c3349e2
LP
321
322 if (j->state == JOB_RUNNING)
6985e396 323 j->manager->n_running_jobs++;
9c3349e2 324
f2341e0a 325 log_unit_debug(j->unit,
66870f90
ZJS
326 "Reinstalled deserialized job %s/%s as %u",
327 j->unit->id, job_type_to_string(j->type), (unsigned) j->id);
e0209d83 328 return 0;
39a18c60
MS
329}
330
1da4264f 331JobDependency* job_dependency_new(Job *subject, Job *object, bool matters, bool conflicts) {
e5b5ae50
LP
332 JobDependency *l;
333
334 assert(object);
335
336 /* Adds a new job link, which encodes that the 'subject' job
337 * needs the 'object' job in some way. If 'subject' is NULL
338 * this means the 'anchor' job (i.e. the one the user
35b8ca3a 339 * explicitly asked for) is the requester. */
e5b5ae50 340
215ffd6c 341 l = new(JobDependency, 1);
0a23a627 342 if (!l)
e5b5ae50
LP
343 return NULL;
344
215ffd6c
MY
345 *l = (JobDependency) {
346 .subject = subject,
347 .object = object,
348 .matters = matters,
349 .conflicts = conflicts,
350 };
e5b5ae50 351
44d8db9e 352 if (subject)
71fda00f 353 LIST_PREPEND(subject, subject->subject_list, l);
e5b5ae50 354
71fda00f 355 LIST_PREPEND(object, object->object_list, l);
e5b5ae50
LP
356
357 return l;
358}
359
1da4264f 360void job_dependency_free(JobDependency *l) {
e5b5ae50
LP
361 assert(l);
362
44d8db9e 363 if (l->subject)
71fda00f 364 LIST_REMOVE(subject, l->subject->subject_list, l);
e5b5ae50 365
71fda00f 366 LIST_REMOVE(object, l->object->object_list, l);
e5b5ae50
LP
367
368 free(l);
369}
370
f2a3de01 371void job_dump(Job *j, FILE *f, const char *prefix) {
a66d02c3
LP
372 assert(j);
373 assert(f);
374
ad5d4b17 375 prefix = strempty(prefix);
9eb63b3c 376
ceed3570 377 fprintf(f,
40d50879
LP
378 "%s-> Job %u:\n"
379 "%s\tAction: %s -> %s\n"
5cb5a6ff 380 "%s\tState: %s\n"
f698d99c
ZJS
381 "%s\tIrreversible: %s\n"
382 "%s\tMay GC: %s\n",
ceed3570 383 prefix, j->id,
ac155bb8 384 prefix, j->unit->id, job_type_to_string(j->type),
94f04347 385 prefix, job_state_to_string(j->state),
f698d99c
ZJS
386 prefix, yes_no(j->irreversible),
387 prefix, yes_no(job_may_gc(j)));
a66d02c3 388}
e5b5ae50 389
348e27fe
MS
390/*
391 * Merging is commutative, so imagine the matrix as symmetric. We store only
392 * its lower triangle to avoid duplication. We don't store the main diagonal,
393 * because A merged with A is simply A.
394 *
e0209d83
MS
395 * If the resulting type is collapsed immediately afterwards (to get rid of
396 * the JOB_RELOAD_OR_START, which lies outside the lookup function's domain),
397 * the following properties hold:
398 *
48b4eab4 399 * Merging is associative! A merged with B, and then merged with C is the same
103635db 400 * as A merged with the result of B merged with C.
348e27fe
MS
401 *
402 * Mergeability is transitive! If A can be merged with B and B with C then
403 * A also with C.
404 *
405 * Also, if A merged with B cannot be merged with C, then either A or B cannot
406 * be merged with C either.
407 */
408static const JobType job_merging_table[] = {
e0209d83
MS
409/* What \ With * JOB_START JOB_VERIFY_ACTIVE JOB_STOP JOB_RELOAD */
410/*********************************************************************************/
348e27fe
MS
411/*JOB_START */
412/*JOB_VERIFY_ACTIVE */ JOB_START,
413/*JOB_STOP */ -1, -1,
414/*JOB_RELOAD */ JOB_RELOAD_OR_START, JOB_RELOAD, -1,
e0209d83 415/*JOB_RESTART */ JOB_RESTART, JOB_RESTART, -1, JOB_RESTART,
348e27fe
MS
416};
417
418JobType job_type_lookup_merge(JobType a, JobType b) {
e0209d83
MS
419 assert_cc(ELEMENTSOF(job_merging_table) == _JOB_TYPE_MAX_MERGING * (_JOB_TYPE_MAX_MERGING - 1) / 2);
420 assert(a >= 0 && a < _JOB_TYPE_MAX_MERGING);
421 assert(b >= 0 && b < _JOB_TYPE_MAX_MERGING);
1ffba6fe
LP
422
423 if (a == b)
348e27fe 424 return a;
1ffba6fe 425
348e27fe
MS
426 if (a < b) {
427 JobType tmp = a;
428 a = b;
429 b = tmp;
1ffba6fe 430 }
e094e853 431
348e27fe 432 return job_merging_table[(a - 1) * a / 2 + b];
e094e853 433}
cd2dbd7d 434
cc479760
DR
435bool job_type_is_redundant(JobType a, UnitActiveState b) {
436 switch (a) {
593fbdd2
LP
437
438 case JOB_START:
5162829e 439 return IN_SET(b, UNIT_ACTIVE, UNIT_RELOADING, UNIT_REFRESHING);
593fbdd2
LP
440
441 case JOB_STOP:
cc479760 442 return IN_SET(b, UNIT_INACTIVE, UNIT_FAILED);
593fbdd2
LP
443
444 case JOB_VERIFY_ACTIVE:
5162829e 445 return IN_SET(b, UNIT_ACTIVE, UNIT_RELOADING, UNIT_REFRESHING);
593fbdd2
LP
446
447 case JOB_RELOAD:
f3ba767d 448 /* Reload jobs are never considered redundant/duplicate. Refer to jobs_may_late_merge() for
7b940d8d 449 * a detailed justification. */
593fbdd2 450 case JOB_RESTART:
dc06321f
AS
451 /* Restart jobs must always be kept.
452 *
453 * For ACTIVE/RELOADING units, this is obvious.
454 *
455 * For ACTIVATING units, it's more subtle:
456 *
457 * Generally, if a service Requires= another unit, restarts of
458 * the unit must be propagated to the service. If the service is
459 * ACTIVATING, it must still be restarted since it might have
460 * stale information regarding the other unit.
461 *
462 * For example, consider a service that Requires= a socket: if
463 * the socket is restarted, but the service is still ACTIVATING,
464 * it's necessary to restart the service so that it gets the new
465 * socket. */
466 return false;
593fbdd2 467
7e803f5e
MS
468 case JOB_NOP:
469 return true;
470
e0209d83 471 default:
04499a70 472 assert_not_reached();
e0209d83
MS
473 }
474}
475
c6497ccb 476JobType job_type_collapse(JobType t, Unit *u) {
e0209d83
MS
477 UnitActiveState s;
478
c6497ccb 479 switch (t) {
e0209d83 480
593fbdd2 481 case JOB_TRY_RESTART:
dc06321f
AS
482 /* Be sure to keep the restart job even if the unit is
483 * ACTIVATING.
484 *
485 * See the job_type_is_redundant(JOB_RESTART) for more info */
e0209d83 486 s = unit_active_state(u);
dc06321f 487 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(s))
c6497ccb
LP
488 return JOB_NOP;
489
490 return JOB_RESTART;
e0209d83 491
3282591d
LP
492 case JOB_TRY_RELOAD:
493 s = unit_active_state(u);
d1559793 494 if (!UNIT_IS_ACTIVE_OR_RELOADING(s))
3282591d
LP
495 return JOB_NOP;
496
497 return JOB_RELOAD;
498
e0209d83
MS
499 case JOB_RELOAD_OR_START:
500 s = unit_active_state(u);
d1559793 501 if (!UNIT_IS_ACTIVE_OR_RELOADING(s))
c6497ccb
LP
502 return JOB_START;
503
504 return JOB_RELOAD;
593fbdd2
LP
505
506 default:
215ffd6c 507 assert(t >= 0 && t < _JOB_TYPE_MAX_IN_TRANSACTION);
c6497ccb 508 return t;
593fbdd2
LP
509 }
510}
511
e0209d83 512int job_type_merge_and_collapse(JobType *a, JobType b, Unit *u) {
c6497ccb
LP
513 JobType t;
514
515 t = job_type_lookup_merge(*a, b);
e0209d83
MS
516 if (t < 0)
517 return -EEXIST;
c6497ccb
LP
518
519 *a = job_type_collapse(t, u);
e0209d83
MS
520 return 0;
521}
522
9588bc32 523static bool job_is_runnable(Job *j) {
87f0e418 524 Unit *other;
5cb5a6ff
LP
525
526 assert(j);
ac1135be 527 assert(j->installed);
5cb5a6ff 528
87f0e418 529 /* Checks whether there is any job running for the units this
5cb5a6ff 530 * job needs to be running after (in the case of a 'positive'
e67c3609
LP
531 * job type) or before (in the case of a 'negative' job
532 * type. */
533
66ca4ec4
LP
534 /* Note that unit types have a say in what is runnable,
535 * too. For example, if they return -EAGAIN from
536 * unit_start() they can indicate they are not
537 * runnable yet. */
538
e67c3609 539 /* First check if there is an override */
cebe0d41 540 if (j->ignore_order)
e67c3609 541 return true;
5cb5a6ff 542
e0209d83
MS
543 if (j->type == JOB_NOP)
544 return true;
545
15ed3c3a
LP
546 UNIT_FOREACH_DEPENDENCY(other, j->unit, UNIT_ATOM_AFTER)
547 if (other->job && job_compare(j, other->job, UNIT_ATOM_AFTER) > 0) {
c03fbd37
LB
548 log_unit_debug(j->unit,
549 "starting held back, waiting for: %s",
550 other->id);
e602f152 551 return false;
c03fbd37 552 }
5cb5a6ff 553
15ed3c3a
LP
554 UNIT_FOREACH_DEPENDENCY(other, j->unit, UNIT_ATOM_BEFORE)
555 if (other->job && job_compare(j, other->job, UNIT_ATOM_BEFORE) > 0) {
c03fbd37
LB
556 log_unit_debug(j->unit,
557 "stopping held back, waiting for: %s",
558 other->id);
5cb5a6ff 559 return false;
c03fbd37 560 }
5cb5a6ff 561
5cb5a6ff
LP
562 return true;
563}
564
bbd1a837 565static void job_change_type(Job *j, JobType newtype) {
f2341e0a
LP
566 assert(j);
567
568 log_unit_debug(j->unit,
66870f90
ZJS
569 "Converting job %s/%s -> %s/%s",
570 j->unit->id, job_type_to_string(j->type),
571 j->unit->id, job_type_to_string(newtype));
bbd1a837
MS
572
573 j->type = newtype;
574}
575
04d232d8 576static const char* job_start_message_format(Unit *u, JobType t) {
33a3fdd9 577 assert(u);
04d232d8 578 assert(IN_SET(t, JOB_START, JOB_STOP, JOB_RELOAD));
a69b3872
LP
579
580 if (t == JOB_RELOAD)
04d232d8
ZJS
581 return "Reloading %s...";
582 else if (t == JOB_START)
583 return UNIT_VTABLE(u)->status_message_formats.starting_stopping[0] ?: "Starting %s...";
584 else
585 return UNIT_VTABLE(u)->status_message_formats.starting_stopping[1] ?: "Stopping %s...";
586}
a69b3872 587
04d232d8
ZJS
588static void job_emit_start_message(Unit *u, uint32_t job_id, JobType t) {
589 _cleanup_free_ char *free_ident = NULL;
590 const char *ident, *format;
a69b3872 591
04d232d8
ZJS
592 assert(u);
593 assert(t >= 0);
594 assert(t < _JOB_TYPE_MAX);
595 assert(u->id); /* We better don't try to run a unit that doesn't even have an id. */
33a3fdd9 596
04d232d8
ZJS
597 if (!IN_SET(t, JOB_START, JOB_STOP, JOB_RELOAD))
598 return;
599
600 if (!unit_log_level_test(u, LOG_INFO))
601 return;
602
603 format = job_start_message_format(u, t);
604 ident = unit_status_string(u, &free_ident);
605
606 bool do_console = t != JOB_RELOAD;
607 bool console_only = do_console && log_on_console(); /* Reload status messages have traditionally
608 * not been printed to the console. */
609
610 /* Print to the log first. */
611 if (!console_only) { /* Skip this if it would only go on the console anyway */
612
613 const char *mid =
614 t == JOB_START ? "MESSAGE_ID=" SD_MESSAGE_UNIT_STARTING_STR :
615 t == JOB_STOP ? "MESSAGE_ID=" SD_MESSAGE_UNIT_STOPPING_STR :
616 "MESSAGE_ID=" SD_MESSAGE_UNIT_RELOADING_STR;
617 const char *msg_fmt = strjoina("MESSAGE=", format);
618
619 /* Note that we deliberately use LOG_MESSAGE() instead of LOG_UNIT_MESSAGE() here, since this
620 * is supposed to mimic closely what is written to screen using the status output, which is
621 * supposed to be high level friendly output. */
622
623 DISABLE_WARNING_FORMAT_NONLITERAL;
624 log_unit_struct(u, LOG_INFO,
625 msg_fmt, ident,
3cf6a3a3
YW
626 LOG_ITEM("JOB_ID=%" PRIu32, job_id),
627 LOG_ITEM("JOB_TYPE=%s", job_type_to_string(t)),
04d232d8
ZJS
628 LOG_UNIT_INVOCATION_ID(u),
629 mid);
630 REENABLE_WARNING;
631 }
632
633 /* Log to the console second. */
634 if (do_console) {
635 DISABLE_WARNING_FORMAT_NONLITERAL;
636 unit_status_printf(u, STATUS_TYPE_NORMAL, "", format, ident);
637 REENABLE_WARNING;
a69b3872 638 }
33a3fdd9
LP
639}
640
b8643ee2 641static const char* job_done_message_format(Unit *u, JobType t, JobResult result) {
04d232d8
ZJS
642 static const char* const generic_finished_start_job[_JOB_RESULT_MAX] = {
643 [JOB_DONE] = "Started %s.",
644 [JOB_TIMEOUT] = "Timed out starting %s.",
645 [JOB_FAILED] = "Failed to start %s.",
646 [JOB_DEPENDENCY] = "Dependency failed for %s.",
647 [JOB_ASSERT] = "Assertion failed for %s.",
648 [JOB_UNSUPPORTED] = "Starting of %s unsupported.",
649 [JOB_COLLECTED] = "Unnecessary job was removed for %s.",
650 [JOB_ONCE] = "Unit %s has been started before and cannot be started again.",
4cb2e6af 651 [JOB_FROZEN] = "Cannot start frozen unit %s.",
9ca16f6f 652 [JOB_CONCURRENCY] = "Hard concurrency limit hit for slice of unit %s.",
04d232d8
ZJS
653 };
654 static const char* const generic_finished_stop_job[_JOB_RESULT_MAX] = {
655 [JOB_DONE] = "Stopped %s.",
656 [JOB_FAILED] = "Stopped %s with error.",
657 [JOB_TIMEOUT] = "Timed out stopping %s.",
4cb2e6af 658 [JOB_FROZEN] = "Cannot stop frozen unit %s.",
04d232d8
ZJS
659 };
660 static const char* const generic_finished_reload_job[_JOB_RESULT_MAX] = {
661 [JOB_DONE] = "Reloaded %s.",
662 [JOB_FAILED] = "Reload failed for %s.",
663 [JOB_TIMEOUT] = "Timed out reloading %s.",
4cb2e6af 664 [JOB_FROZEN] = "Cannot reload frozen unit %s.",
04d232d8
ZJS
665 };
666 /* When verify-active detects the unit is inactive, report it.
667 * Most likely a DEPEND warning from a requisiting unit will
668 * occur next and it's nice to see what was requisited. */
669 static const char* const generic_finished_verify_active_job[_JOB_RESULT_MAX] = {
670 [JOB_SKIPPED] = "%s is inactive.",
671 };
33a3fdd9
LP
672 const char *format;
673
674 assert(u);
04d232d8
ZJS
675 assert(t >= 0);
676 assert(t < _JOB_TYPE_MAX);
33a3fdd9 677
413e8650 678 /* Show condition check message if the job did not actually do anything due to unmet condition. */
b8643ee2
LP
679 if (t == JOB_START && result == JOB_DONE && !u->condition_result)
680 return "Condition check resulted in %s being skipped.";
04d232d8
ZJS
681
682 if (IN_SET(t, JOB_START, JOB_STOP, JOB_RESTART)) {
683 const UnitStatusMessageFormats *formats = &UNIT_VTABLE(u)->status_message_formats;
684 if (formats->finished_job) {
685 format = formats->finished_job(u, t, result);
686 if (format)
b8643ee2 687 return format;
04d232d8 688 }
33a3fdd9 689
04d232d8
ZJS
690 format = (t == JOB_START ? formats->finished_start_job : formats->finished_stop_job)[result];
691 if (format)
b8643ee2 692 return format;
04d232d8 693 }
33a3fdd9 694
04d232d8
ZJS
695 /* Return generic strings */
696 switch (t) {
697 case JOB_START:
b8643ee2 698 return generic_finished_start_job[result];
04d232d8
ZJS
699 case JOB_STOP:
700 case JOB_RESTART:
b8643ee2 701 return generic_finished_stop_job[result];
04d232d8 702 case JOB_RELOAD:
b8643ee2 703 return generic_finished_reload_job[result];
04d232d8 704 case JOB_VERIFY_ACTIVE:
b8643ee2 705 return generic_finished_verify_active_job[result];
04d232d8
ZJS
706 default:
707 return NULL;
708 }
709}
710
711static const struct {
712 int log_level;
713 const char *color, *word;
714} job_done_messages[_JOB_RESULT_MAX] = {
715 [JOB_DONE] = { LOG_INFO, ANSI_OK_COLOR, " OK " },
716 [JOB_CANCELED] = { LOG_INFO, },
717 [JOB_TIMEOUT] = { LOG_ERR, ANSI_HIGHLIGHT_RED, " TIME " },
718 [JOB_FAILED] = { LOG_ERR, ANSI_HIGHLIGHT_RED, "FAILED" },
719 [JOB_DEPENDENCY] = { LOG_WARNING, ANSI_HIGHLIGHT_YELLOW, "DEPEND" },
720 [JOB_SKIPPED] = { LOG_NOTICE, ANSI_HIGHLIGHT, " INFO " },
721 [JOB_INVALID] = { LOG_INFO, },
722 [JOB_ASSERT] = { LOG_WARNING, ANSI_HIGHLIGHT_YELLOW, "ASSERT" },
723 [JOB_UNSUPPORTED] = { LOG_WARNING, ANSI_HIGHLIGHT_YELLOW, "UNSUPP" },
724 [JOB_COLLECTED] = { LOG_INFO, },
725 [JOB_ONCE] = { LOG_ERR, ANSI_HIGHLIGHT_RED, " ONCE " },
4cb2e6af 726 [JOB_FROZEN] = { LOG_ERR, ANSI_HIGHLIGHT_RED, "FROZEN" },
9ca16f6f 727 [JOB_CONCURRENCY] = { LOG_ERR, ANSI_HIGHLIGHT_RED, "CONCUR" },
04d232d8
ZJS
728};
729
730static const char* job_done_mid(JobType type, JobResult result) {
731 switch (type) {
732 case JOB_START:
733 if (result == JOB_DONE)
734 return "MESSAGE_ID=" SD_MESSAGE_UNIT_STARTED_STR;
735 else
736 return "MESSAGE_ID=" SD_MESSAGE_UNIT_FAILED_STR;
737
738 case JOB_RELOAD:
739 return "MESSAGE_ID=" SD_MESSAGE_UNIT_RELOADED_STR;
740
741 case JOB_STOP:
742 case JOB_RESTART:
743 return "MESSAGE_ID=" SD_MESSAGE_UNIT_STOPPED_STR;
744
745 default:
746 return NULL;
747 }
33a3fdd9
LP
748}
749
04d232d8 750static void job_emit_done_message(Unit *u, uint32_t job_id, JobType t, JobResult result) {
b8643ee2
LP
751 _cleanup_free_ char *free_ident = NULL;
752 const char *ident, *format;
33a3fdd9
LP
753
754 assert(u);
b344b363
LP
755 assert(t >= 0);
756 assert(t < _JOB_TYPE_MAX);
33a3fdd9 757
04d232d8 758 if (!unit_log_level_test(u, job_done_messages[result].log_level))
33a3fdd9
LP
759 return;
760
04d232d8
ZJS
761 format = job_done_message_format(u, t, result);
762 if (!format)
c2503e35
RH
763 return;
764
04d232d8 765 ident = unit_status_string(u, &free_ident);
33a3fdd9 766
04d232d8
ZJS
767 const char *status = job_done_messages[result].word;
768 bool do_console = t != JOB_RELOAD && status;
769 bool console_only = do_console && log_on_console();
33a3fdd9 770
04d232d8 771 if (t == JOB_START && result == JOB_DONE && !u->condition_result) {
413e8650 772 /* No message on the console if the job did not actually do anything due to unmet condition. */
04d232d8
ZJS
773 if (console_only)
774 return;
775 else
776 do_console = false;
777 }
778
779 if (!console_only) { /* Skip printing if output goes to the console, and job_print_status_message()
780 * will actually print something to the console. */
6e548561 781 Condition *c;
04d232d8 782 const char *mid = job_done_mid(t, result); /* mid may be NULL. log_unit_struct() will ignore it. */
04d232d8 783
6e548561
DDM
784 c = t == JOB_START && result == JOB_DONE ? unit_find_failed_condition(u) : NULL;
785 if (c) {
413e8650 786 /* Special case units that were skipped because of a unmet condition check so that
6e548561
DDM
787 * we can add more information to the message. */
788 if (c->trigger)
789 log_unit_struct(
790 u,
791 job_done_messages[result].log_level,
413e8650 792 LOG_MESSAGE("%s was skipped because no trigger condition checks were met.",
92663a5e 793 ident),
3cf6a3a3
YW
794 LOG_ITEM("JOB_ID=%" PRIu32, job_id),
795 LOG_ITEM("JOB_TYPE=%s", job_type_to_string(t)),
796 LOG_ITEM("JOB_RESULT=%s", job_result_to_string(result)),
6e548561
DDM
797 LOG_UNIT_INVOCATION_ID(u),
798 mid);
799 else
800 log_unit_struct(
801 u,
802 job_done_messages[result].log_level,
413e8650 803 LOG_MESSAGE("%s was skipped because of an unmet condition check (%s=%s%s).",
92663a5e
ZJS
804 ident,
805 condition_type_to_string(c->type),
806 c->negate ? "!" : "",
807 c->parameter),
3cf6a3a3
YW
808 LOG_ITEM("JOB_ID=%" PRIu32, job_id),
809 LOG_ITEM("JOB_TYPE=%s", job_type_to_string(t)),
810 LOG_ITEM("JOB_RESULT=%s", job_result_to_string(result)),
6e548561
DDM
811 LOG_UNIT_INVOCATION_ID(u),
812 mid);
813 } else {
814 const char *msg_fmt = strjoina("MESSAGE=", format);
815
816 DISABLE_WARNING_FORMAT_NONLITERAL;
817 log_unit_struct(u, job_done_messages[result].log_level,
818 msg_fmt, ident,
3cf6a3a3
YW
819 LOG_ITEM("JOB_ID=%" PRIu32, job_id),
820 LOG_ITEM("JOB_TYPE=%s", job_type_to_string(t)),
821 LOG_ITEM("JOB_RESULT=%s", job_result_to_string(result)),
6e548561
DDM
822 LOG_UNIT_INVOCATION_ID(u),
823 mid);
824 REENABLE_WARNING;
825 }
04d232d8
ZJS
826 }
827
828 if (do_console) {
829 if (log_get_show_color())
830 status = strjoina(job_done_messages[result].color,
831 status,
832 ANSI_NORMAL);
833
834 DISABLE_WARNING_FORMAT_NONLITERAL;
835 unit_status_printf(u,
836 result == JOB_DONE ? STATUS_TYPE_NORMAL : STATUS_TYPE_NOTICE,
837 status, format, ident);
838 REENABLE_WARNING;
839
840 if (t == JOB_START && result == JOB_FAILED) {
841 _cleanup_free_ char *quoted = NULL;
842
843 quoted = shell_maybe_quote(u->id, 0);
844 if (quoted)
845 manager_status_printf(u->manager, STATUS_TYPE_NORMAL, NULL,
846 "See 'systemctl status %s' for details.", quoted);
847 }
848 }
33a3fdd9
LP
849}
850
d1a34ae9 851static int job_perform_on_unit(Job **j) {
48b92b37 852 ActivationDetails *a;
df446f96
LP
853 uint32_t id;
854 Manager *m;
855 JobType t;
856 Unit *u;
d8deb187 857 bool wait_only;
d1a34ae9
MS
858 int r;
859
d8deb187
MY
860 /* While we execute this operation the job might go away (for example: because it finishes immediately
861 * or is replaced by a new, conflicting job). To make sure we don't access a freed job later on we
862 * store the id here, so that we can verify the job is still valid. */
df446f96
LP
863
864 assert(j);
865 assert(*j);
866
867 m = (*j)->manager;
868 u = (*j)->unit;
869 t = (*j)->type;
870 id = (*j)->id;
48b92b37 871 a = (*j)->activation_details;
df446f96 872
d1a34ae9
MS
873 switch (t) {
874 case JOB_START:
48b92b37 875 r = unit_start(u, a);
d8deb187 876 wait_only = r == -EBADR; /* If the unit type does not support starting, then simply wait. */
d1a34ae9
MS
877 break;
878
879 case JOB_RESTART:
880 t = JOB_STOP;
4831981d 881 _fallthrough_;
d1a34ae9
MS
882 case JOB_STOP:
883 r = unit_stop(u);
d8deb187 884 wait_only = r == -EBADR; /* If the unit type does not support stopping, then simply wait. */
d1a34ae9
MS
885 break;
886
887 case JOB_RELOAD:
888 r = unit_reload(u);
d8deb187 889 wait_only = false; /* A clear error is generated if reload is not supported. */
d1a34ae9
MS
890 break;
891
892 default:
04499a70 893 assert_not_reached();
d1a34ae9
MS
894 }
895
d8deb187
MY
896 /* Log if the job still exists and the start/stop/reload function actually did something or we're
897 * only waiting for unit status change (common for device units). The latter ensures that job start
898 * messages for device units are correctly shown. Note that if the job disappears too quickly, e.g.
899 * for units for which there's no 'activating' phase (i.e. because we transition directly from
900 * 'inactive' to 'active'), we'll possibly skip the "Starting..." message. */
d1a34ae9 901 *j = manager_get_job(m, id);
d8deb187 902 if (*j && (r > 0 || wait_only))
04d232d8 903 job_emit_start_message(u, id, t);
d1a34ae9 904
d8deb187 905 return wait_only ? 0 : r;
d1a34ae9
MS
906}
907
5cb5a6ff
LP
908int job_run_and_invalidate(Job *j) {
909 int r;
ac1135be 910
5cb5a6ff 911 assert(j);
ac1135be 912 assert(j->installed);
e0209d83 913 assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION);
66aa6f7f 914 assert(j->in_run_queue);
5cb5a6ff 915
da8e1782 916 prioq_remove(j->manager->run_queue, j, &j->run_queue_idx);
66aa6f7f 917 j->in_run_queue = false;
5cb5a6ff
LP
918
919 if (j->state != JOB_WAITING)
920 return 0;
921
034c6ed7
LP
922 if (!job_is_runnable(j))
923 return -EAGAIN;
924
a2df3ea4 925 job_start_timer(j, true);
9c3349e2 926 job_set_state(j, JOB_RUNNING);
c1e1601e 927 job_add_to_dbus_queue(j);
83c60c9f 928
5cb5a6ff
LP
929 switch (j->type) {
930
5cb5a6ff 931 case JOB_VERIFY_ACTIVE: {
ea2c0e45
LP
932 UnitActiveState t;
933
934 t = unit_active_state(j->unit);
87f0e418 935 if (UNIT_IS_ACTIVE_OR_RELOADING(t))
5cb5a6ff 936 r = -EALREADY;
87f0e418 937 else if (t == UNIT_ACTIVATING)
5cb5a6ff
LP
938 r = -EAGAIN;
939 else
6a371e23 940 r = -EBADR;
5cb5a6ff
LP
941 break;
942 }
943
d1a34ae9 944 case JOB_START:
5cb5a6ff 945 case JOB_STOP:
dd17d388 946 case JOB_RESTART:
5cb5a6ff 947 case JOB_RELOAD:
d1a34ae9 948 r = job_perform_on_unit(&j);
5cb5a6ff
LP
949 break;
950
e0209d83
MS
951 case JOB_NOP:
952 r = -EALREADY;
953 break;
954
5cb5a6ff 955 default:
04499a70 956 assert_not_reached();
5cb5a6ff
LP
957 }
958
e0209d83 959 if (j) {
8ebd9175
LP
960 if (r == -EAGAIN)
961 job_set_state(j, JOB_WAITING); /* Hmm, not ready after all, let's return to JOB_WAITING state */
6e64994d 962 else if (r == -EALREADY) /* already being executed */
833f92ad 963 r = job_finish_and_invalidate(j, JOB_DONE, true, true);
2e3bb01d
LB
964 else if (r == -ECOMM)
965 r = job_finish_and_invalidate(j, JOB_DONE, true, false);
6a371e23 966 else if (r == -EBADR)
833f92ad 967 r = job_finish_and_invalidate(j, JOB_SKIPPED, true, false);
6a371e23 968 else if (r == -ENOEXEC)
833f92ad 969 r = job_finish_and_invalidate(j, JOB_INVALID, true, false);
59fccdc5 970 else if (r == -EPROTO)
833f92ad 971 r = job_finish_and_invalidate(j, JOB_ASSERT, true, false);
15411c0c 972 else if (r == -EOPNOTSUPP)
833f92ad 973 r = job_finish_and_invalidate(j, JOB_UNSUPPORTED, true, false);
631b676b
LP
974 else if (r == -ENOLINK)
975 r = job_finish_and_invalidate(j, JOB_DEPENDENCY, true, false);
d4fd1cf2
LP
976 else if (r == -ESTALE)
977 r = job_finish_and_invalidate(j, JOB_ONCE, true, false);
4cb2e6af
AV
978 else if (r == -EDEADLK)
979 r = job_finish_and_invalidate(j, JOB_FROZEN, true, false);
9ca16f6f
LP
980 else if (r == -ETOOMANYREFS)
981 r = job_finish_and_invalidate(j, JOB_CONCURRENCY, /* recursive= */ true, /* already= */ false);
9c3349e2 982 else if (r < 0)
833f92ad 983 r = job_finish_and_invalidate(j, JOB_FAILED, true, false);
2cf19a7a 984 }
5cb5a6ff
LP
985
986 return r;
987}
988
15ed3c3a 989static void job_fail_dependencies(Unit *u, UnitDependencyAtom match_atom) {
be7d9ff7 990 Unit *other;
be7d9ff7
LP
991
992 assert(u);
993
15ed3c3a 994 UNIT_FOREACH_DEPENDENCY(other, u, match_atom) {
be7d9ff7
LP
995 Job *j = other->job;
996
997 if (!j)
998 continue;
999 if (!IN_SET(j->type, JOB_START, JOB_VERIFY_ACTIVE))
1000 continue;
1001
833f92ad 1002 job_finish_and_invalidate(j, JOB_DEPENDENCY, true, false);
be7d9ff7
LP
1003 }
1004}
1005
833f92ad 1006int job_finish_and_invalidate(Job *j, JobResult result, bool recursive, bool already) {
15ed3c3a 1007 Unit *u, *other;
b866264a 1008 JobType t;
5cb5a6ff
LP
1009
1010 assert(j);
ac1135be 1011 assert(j->installed);
e0209d83 1012 assert(j->type < _JOB_TYPE_MAX_IN_TRANSACTION);
5cb5a6ff 1013
c6918296
MS
1014 u = j->unit;
1015 t = j->type;
1016
1017 j->result = result;
1018
771b5242
ZJS
1019 log_unit_debug(u, "Job %" PRIu32 " %s/%s finished, result=%s",
1020 j->id, u->id, job_type_to_string(t), job_result_to_string(result));
c6918296 1021
771b5242 1022 /* If this job did nothing to the respective unit we don't log the status message */
833f92ad 1023 if (!already)
04d232d8 1024 job_emit_done_message(u, j->id, t, result);
c6918296 1025
034c6ed7 1026 /* Patch restart jobs so that they become normal start jobs */
c6918296 1027 if (result == JOB_DONE && t == JOB_RESTART) {
f50e0a01 1028
bbd1a837 1029 job_change_type(j, JOB_START);
9c3349e2 1030 job_set_state(j, JOB_WAITING);
cc42e081 1031
fec7615c 1032 job_add_to_dbus_queue(j);
cc42e081 1033 job_add_to_run_queue(j);
c5a97ed1 1034 job_add_to_gc_queue(j);
57981b98 1035
57981b98 1036 goto finish;
5cb5a6ff
LP
1037 }
1038
9ca16f6f 1039 if (IN_SET(result, JOB_FAILED, JOB_INVALID, JOB_FROZEN, JOB_CONCURRENCY))
313cefa1 1040 j->manager->n_failed_jobs++;
76bf48b7 1041
97e7d748 1042 job_uninstall(j);
4a53080b 1043 job_free(j);
5cb5a6ff
LP
1044
1045 /* Fail depending jobs on failure */
5273510e 1046 if (result != JOB_DONE && recursive) {
15ed3c3a
LP
1047 if (IN_SET(t, JOB_START, JOB_VERIFY_ACTIVE))
1048 job_fail_dependencies(u, UNIT_ATOM_PROPAGATE_START_FAILURE);
1049 else if (t == JOB_STOP)
1050 job_fail_dependencies(u, UNIT_ATOM_PROPAGATE_STOP_FAILURE);
5cb5a6ff
LP
1051 }
1052
defe63b0 1053 /* A special check to make sure we take down anything RequisiteOf= if we aren't active. This is when
18fe76eb 1054 * the verify-active job merges with a satisfying job type, and then loses its invalidation effect,
defe63b0
LP
1055 * as the result there is JOB_DONE for the start job we merged into, while we should be failing the
1056 * depending job if the said unit isn't in fact active. Oneshots are an example of this, where going
1057 * directly from activating to inactive is success.
791cd159 1058 *
defe63b0
LP
1059 * This happens when you use ConditionXYZ= in a unit too, since in that case the job completes with
1060 * the JOB_DONE result, but the unit never really becomes active. Note that such a case still
1061 * involves merging:
791cd159 1062 *
defe63b0
LP
1063 * A start job waits for something else, and a verify-active comes in and merges in the installed
1064 * job. Then, later, when it becomes runnable, it finishes with JOB_DONE result as execution on
1065 * conditions not being met is skipped, breaking our dependency semantics.
791cd159 1066 *
defe63b0
LP
1067 * Also, depending on if start job waits or not, the merging may or may not happen (the verify-active
1068 * job may trigger after it finishes), so you get undeterministic results without this check.
791cd159 1069 */
15ed3c3a
LP
1070 if (result == JOB_DONE && recursive &&
1071 IN_SET(t, JOB_START, JOB_RELOAD) &&
1072 !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u)))
1073 job_fail_dependencies(u, UNIT_ATOM_PROPAGATE_INACTIVE_START_AS_FAILURE);
1074
9faeb2d0
MY
1075 /* Trigger OnFailure= dependencies manually here. We need to do that because a failed job might not
1076 * cause a unit state change. Note that we don't treat JOB_CANCELED as failure in this context.
1077 * And JOB_FAILURE is already handled by the unit itself (unit_notify). */
646cc98d 1078 if (IN_SET(result, JOB_TIMEOUT, JOB_DEPENDENCY)) {
c2503e35 1079 log_unit_struct(u, LOG_NOTICE,
3cf6a3a3
YW
1080 LOG_ITEM("JOB_TYPE=%s", job_type_to_string(t)),
1081 LOG_ITEM("JOB_RESULT=%s", job_result_to_string(result)),
c2503e35
RH
1082 LOG_UNIT_MESSAGE(u, "Job %s/%s failed with result '%s'.",
1083 u->id,
1084 job_type_to_string(t),
1085 job_result_to_string(result)));
222ae6a8 1086
9faeb2d0 1087 unit_start_on_termination_deps(u, UNIT_ATOM_ON_FAILURE);
222ae6a8 1088 }
c0daa706 1089
3ecaa09b
LP
1090 unit_trigger_notify(u);
1091
57981b98 1092finish:
5cb5a6ff 1093 /* Try to start the next jobs that can be started */
15ed3c3a 1094 UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_AFTER)
c5a97ed1 1095 if (other->job) {
ac155bb8 1096 job_add_to_run_queue(other->job);
c5a97ed1
LP
1097 job_add_to_gc_queue(other->job);
1098 }
15ed3c3a 1099 UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_BEFORE)
c5a97ed1 1100 if (other->job) {
ac155bb8 1101 job_add_to_run_queue(other->job);
c5a97ed1
LP
1102 job_add_to_gc_queue(other->job);
1103 }
5cb5a6ff 1104
4c7a0fc8
LB
1105 /* Ensure that when an upheld/unneeded/bound unit activation job fails we requeue it, if it still
1106 * necessary. If there are no state changes in the triggerer, it would not be retried otherwise. */
1107 unit_submit_to_start_when_upheld_queue(u);
1108 unit_submit_to_stop_when_bound_queue(u);
1109 unit_submit_to_stop_when_unneeded_queue(u);
1110
1b4ab5a2
MY
1111 /* All jobs might have finished, let's see */
1112 if (u->manager->may_dispatch_stop_notify_queue == 0)
1113 u->manager->may_dispatch_stop_notify_queue = -1;
1114
ac155bb8 1115 manager_check_finished(u->manager);
b0c918b9 1116
5273510e 1117 return 0;
5cb5a6ff 1118}
034c6ed7 1119
718db961 1120static int job_dispatch_timer(sd_event_source *s, uint64_t monotonic, void *userdata) {
99534007 1121 Job *j = ASSERT_PTR(userdata);
f189ab18 1122 Unit *u;
faf919f1 1123
718db961 1124 assert(s == j->timer_event_source);
faf919f1 1125
f2341e0a 1126 log_unit_warning(j->unit, "Job %s/%s timed out.", j->unit->id, job_type_to_string(j->type));
faf919f1 1127
f189ab18 1128 u = j->unit;
833f92ad 1129 job_finish_and_invalidate(j, JOB_TIMEOUT, true, false);
f189ab18 1130
96a0cfbf
LP
1131 emergency_action(
1132 u->manager,
1133 u->job_timeout_action,
1134 EMERGENCY_ACTION_IS_WATCHDOG|EMERGENCY_ACTION_WARN|EMERGENCY_ACTION_SLEEP_5S,
1135 u->job_timeout_reboot_arg,
1136 /* exit_status= */ -1,
1137 "job timed out");
f189ab18 1138
718db961
LP
1139 return 0;
1140}
faf919f1 1141
a2df3ea4 1142int job_start_timer(Job *j, bool job_running) {
718db961 1143 int r;
171f12ce 1144 usec_t timeout_time, old_timeout_time;
faf919f1 1145
a2df3ea4 1146 if (job_running) {
171f12ce
MK
1147 j->begin_running_usec = now(CLOCK_MONOTONIC);
1148
a2df3ea4
MK
1149 if (j->unit->job_running_timeout == USEC_INFINITY)
1150 return 0;
faf919f1 1151
171f12ce 1152 timeout_time = usec_add(j->begin_running_usec, j->unit->job_running_timeout);
faf919f1 1153
a2df3ea4
MK
1154 if (j->timer_event_source) {
1155 /* Update only if JobRunningTimeoutSec= results in earlier timeout */
1156 r = sd_event_source_get_time(j->timer_event_source, &old_timeout_time);
1157 if (r < 0)
1158 return r;
1159
1160 if (old_timeout_time <= timeout_time)
1161 return 0;
1162
1163 return sd_event_source_set_time(j->timer_event_source, timeout_time);
1164 }
1165 } else {
1166 if (j->timer_event_source)
1167 return 0;
1168
1169 j->begin_usec = now(CLOCK_MONOTONIC);
1170
1171 if (j->unit->job_timeout == USEC_INFINITY)
1172 return 0;
1173
1174 timeout_time = usec_add(j->begin_usec, j->unit->job_timeout);
1175 }
8bb310c3 1176
6a0f1f6d
LP
1177 r = sd_event_add_time(
1178 j->manager->event,
1179 &j->timer_event_source,
1180 CLOCK_MONOTONIC,
a2df3ea4 1181 timeout_time, 0,
6a0f1f6d 1182 job_dispatch_timer, j);
718db961
LP
1183 if (r < 0)
1184 return r;
faf919f1 1185
7dfbe2e3
TG
1186 (void) sd_event_source_set_description(j->timer_event_source, "job-start");
1187
718db961 1188 return 0;
faf919f1
LP
1189}
1190
c1e1601e 1191void job_add_to_run_queue(Job *j) {
f8c34706
LP
1192 int r;
1193
034c6ed7 1194 assert(j);
ac1135be 1195 assert(j->installed);
034c6ed7
LP
1196
1197 if (j->in_run_queue)
1198 return;
1199
735a8b6d
LP
1200 r = prioq_put(j->manager->run_queue, j, &j->run_queue_idx);
1201 if (r < 0)
1202 log_warning_errno(r, "Failed put job in run queue, ignoring: %m");
1203 else
1204 j->in_run_queue = true;
b0c4b282
LP
1205
1206 manager_trigger_run_queue(j->manager);
034c6ed7 1207}
94f04347 1208
c1e1601e
LP
1209void job_add_to_dbus_queue(Job *j) {
1210 assert(j);
1211 assert(j->installed);
1212
1213 if (j->in_dbus_queue)
1214 return;
1215
a567261a
LP
1216 /* We don't check if anybody is subscribed here, since this
1217 * job might just have been created and not yet assigned to a
1218 * connection/client. */
94b6dfa2 1219
71fda00f 1220 LIST_PREPEND(dbus_queue, j->manager->dbus_job_queue, j);
c1e1601e
LP
1221 j->in_dbus_queue = true;
1222}
1223
ff3f2953 1224char* job_dbus_path(Job *j) {
ea430986
LP
1225 char *p;
1226
1227 assert(j);
1228
ccd06097 1229 if (asprintf(&p, "/org/freedesktop/systemd1/job/%"PRIu32, j->id) < 0)
ea430986
LP
1230 return NULL;
1231
1232 return p;
1233}
1234
05a98afd
LP
1235int job_serialize(Job *j, FILE *f) {
1236 assert(j);
1237 assert(f);
1238
d68c645b
LP
1239 (void) serialize_item_format(f, "job-id", "%u", j->id);
1240 (void) serialize_item(f, "job-type", job_type_to_string(j->type));
1241 (void) serialize_item(f, "job-state", job_state_to_string(j->state));
1242 (void) serialize_bool(f, "job-irreversible", j->irreversible);
1243 (void) serialize_bool(f, "job-sent-dbus-new-signal", j->sent_dbus_new_signal);
1244 (void) serialize_bool(f, "job-ignore-order", j->ignore_order);
718db961
LP
1245
1246 if (j->begin_usec > 0)
d68c645b 1247 (void) serialize_usec(f, "job-begin", j->begin_usec);
171f12ce 1248 if (j->begin_running_usec > 0)
d68c645b 1249 (void) serialize_usec(f, "job-begin-running", j->begin_running_usec);
718db961 1250
1a465207 1251 bus_track_serialize(j->bus_track, f, "subscribed");
39a18c60 1252
48b92b37
LB
1253 activation_details_serialize(j->activation_details, f);
1254
39a18c60
MS
1255 /* End marker */
1256 fputc('\n', f);
1257 return 0;
1258}
1259
05a98afd 1260int job_deserialize(Job *j, FILE *f) {
8948b341
LP
1261 int r;
1262
718db961 1263 assert(j);
05a98afd 1264 assert(f);
718db961 1265
39a18c60 1266 for (;;) {
0df7d525 1267 _cleanup_free_ char *l = NULL;
39a18c60 1268 size_t k;
215ffd6c 1269 const char *v;
39a18c60 1270
0df7d525 1271 r = deserialize_read_line(f, &l);
8948b341 1272 if (r < 0)
0df7d525
LP
1273 return r;
1274 if (r == 0) /* eof or end marker */
1275 break;
39a18c60
MS
1276
1277 k = strcspn(l, "=");
1278
1279 if (l[k] == '=') {
1280 l[k] = 0;
1281 v = l+k+1;
1282 } else
1283 v = l+k;
1284
1285 if (streq(l, "job-id")) {
718db961 1286
39a18c60 1287 if (safe_atou32(v, &j->id) < 0)
15ec1021 1288 log_debug("Failed to parse job id value: %s", v);
718db961 1289
39a18c60 1290 } else if (streq(l, "job-type")) {
718db961
LP
1291 JobType t;
1292
1293 t = job_type_from_string(v);
39a18c60 1294 if (t < 0)
15ec1021 1295 log_debug("Failed to parse job type: %s", v);
e0209d83 1296 else if (t >= _JOB_TYPE_MAX_IN_TRANSACTION)
15ec1021 1297 log_debug("Cannot deserialize job of type: %s", v);
39a18c60
MS
1298 else
1299 j->type = t;
718db961 1300
39a18c60 1301 } else if (streq(l, "job-state")) {
718db961
LP
1302 JobState s;
1303
1304 s = job_state_from_string(v);
39a18c60 1305 if (s < 0)
15ec1021 1306 log_debug("Failed to parse job state: %s", v);
39a18c60 1307 else
9c3349e2 1308 job_set_state(j, s);
718db961 1309
23ade460 1310 } else if (streq(l, "job-irreversible")) {
718db961
LP
1311 int b;
1312
1313 b = parse_boolean(v);
23ade460 1314 if (b < 0)
15ec1021 1315 log_debug("Failed to parse job irreversible flag: %s", v);
23ade460
MS
1316 else
1317 j->irreversible = j->irreversible || b;
718db961 1318
39a18c60 1319 } else if (streq(l, "job-sent-dbus-new-signal")) {
718db961
LP
1320 int b;
1321
1322 b = parse_boolean(v);
39a18c60 1323 if (b < 0)
15ec1021 1324 log_debug("Failed to parse job sent_dbus_new_signal flag: %s", v);
39a18c60
MS
1325 else
1326 j->sent_dbus_new_signal = j->sent_dbus_new_signal || b;
718db961 1327
39a18c60 1328 } else if (streq(l, "job-ignore-order")) {
718db961
LP
1329 int b;
1330
1331 b = parse_boolean(v);
39a18c60 1332 if (b < 0)
15ec1021 1333 log_debug("Failed to parse job ignore_order flag: %s", v);
39a18c60
MS
1334 else
1335 j->ignore_order = j->ignore_order || b;
718db961 1336
d68c645b
LP
1337 } else if (streq(l, "job-begin"))
1338 (void) deserialize_usec(v, &j->begin_usec);
718db961 1339
d68c645b
LP
1340 else if (streq(l, "job-begin-running"))
1341 (void) deserialize_usec(v, &j->begin_running_usec);
718db961 1342
d68c645b 1343 else if (streq(l, "subscribed")) {
b39a2770 1344 if (strv_extend(&j->deserialized_clients, v) < 0)
d68c645b 1345 return log_oom();
48b92b37
LB
1346
1347 } else if (startswith(l, "activation-details")) {
1348 if (activation_details_deserialize(l, v, &j->activation_details) < 0)
1349 log_debug("Failed to parse job ActivationDetails element: %s", v);
1350
d68c645b
LP
1351 } else
1352 log_debug("Unknown job serialization key: %s", l);
39a18c60 1353 }
0df7d525
LP
1354
1355 return 0;
39a18c60
MS
1356}
1357
1358int job_coldplug(Job *j) {
718db961 1359 int r;
171f12ce 1360 usec_t timeout_time = USEC_INFINITY;
718db961
LP
1361
1362 assert(j);
39a18c60 1363
8f8f05a9
LP
1364 /* After deserialization is complete and the bus connection
1365 * set up again, let's start watching our subscribers again */
c5a97ed1 1366 (void) bus_job_coldplug_bus_track(j);
8f8f05a9 1367
1727a595
MM
1368 if (j->state == JOB_WAITING)
1369 job_add_to_run_queue(j);
1370
c5a97ed1
LP
1371 /* Maybe due to new dependencies we don't actually need this job anymore? */
1372 job_add_to_gc_queue(j);
1373
171f12ce
MK
1374 /* Create timer only when job began or began running and the respective timeout is finite.
1375 * Follow logic of job_start_timer() if both timeouts are finite */
1376 if (j->begin_usec == 0)
1377 return 0;
1378
1379 if (j->unit->job_timeout != USEC_INFINITY)
1380 timeout_time = usec_add(j->begin_usec, j->unit->job_timeout);
1381
1f65fd49 1382 if (timestamp_is_set(j->begin_running_usec))
171f12ce
MK
1383 timeout_time = MIN(timeout_time, usec_add(j->begin_running_usec, j->unit->job_running_timeout));
1384
1385 if (timeout_time == USEC_INFINITY)
39a18c60
MS
1386 return 0;
1387
5dcadb4c 1388 j->timer_event_source = sd_event_source_disable_unref(j->timer_event_source);
39a18c60 1389
6a0f1f6d
LP
1390 r = sd_event_add_time(
1391 j->manager->event,
1392 &j->timer_event_source,
1393 CLOCK_MONOTONIC,
171f12ce 1394 timeout_time, 0,
6a0f1f6d 1395 job_dispatch_timer, j);
718db961 1396 if (r < 0)
da927ba9 1397 log_debug_errno(r, "Failed to restart timeout for job: %m");
718db961 1398
7dfbe2e3
TG
1399 (void) sd_event_source_set_description(j->timer_event_source, "job-timeout");
1400
718db961 1401 return r;
39a18c60
MS
1402}
1403
c65eb836
LP
1404void job_shutdown_magic(Job *j) {
1405 assert(j);
6985e396 1406 assert(j->manager);
c65eb836
LP
1407
1408 /* The shutdown target gets some special treatment here: we
1409 * tell the kernel to begin with flushing its disk caches, to
1410 * optimize shutdown time a bit. Ideally we wouldn't hardcode
1411 * this magic into PID 1. However all other processes aren't
1412 * options either since they'd exit much sooner than PID 1 and
1413 * asynchronous sync() would cause their exit to be
1414 * delayed. */
1415
c2756a68 1416 if (j->type != JOB_START)
c65eb836
LP
1417 return;
1418
b3f54861 1419 if (!unit_has_name(j->unit, SPECIAL_SHUTDOWN_TARGET))
c2756a68
LP
1420 return;
1421
b3f54861 1422 /* This is the very beginning of the shutdown phase, so take the timestamp here */
6985e396 1423 dual_timestamp_now(j->manager->timestamps + MANAGER_TIMESTAMP_SHUTDOWN_START);
b3f54861
LB
1424
1425 if (!MANAGER_IS_SYSTEM(j->manager))
c65eb836
LP
1426 return;
1427
5b1869ea 1428 /* In case messages on console has been disabled on boot */
6985e396 1429 j->manager->no_console_output = false;
5b1869ea 1430
6985e396 1431 manager_invalidate_startup_units(j->manager);
9dfb6a3a 1432
75f86906 1433 if (detect_container() > 0)
c65eb836
LP
1434 return;
1435
d00c2631 1436 (void) asynchronous_sync(NULL);
c65eb836
LP
1437}
1438
fcd7e0b7 1439int job_get_timeout(Job *j, usec_t *ret) {
7a7821c8 1440 usec_t x = USEC_INFINITY, y = USEC_INFINITY;
fbd747a4 1441 Unit *u = ASSERT_PTR(ASSERT_PTR(j)->unit);
7a7821c8 1442 int r;
68db7a3b 1443
fcd7e0b7
YW
1444 assert(ret);
1445
68db7a3b
ZJS
1446 if (j->timer_event_source) {
1447 r = sd_event_source_get_time(j->timer_event_source, &x);
1448 if (r < 0)
1449 return r;
68db7a3b
ZJS
1450 }
1451
1452 if (UNIT_VTABLE(u)->get_timeout) {
7a7821c8
LP
1453 r = UNIT_VTABLE(u)->get_timeout(u, &y);
1454 if (r < 0)
1455 return r;
68db7a3b
ZJS
1456 }
1457
fcd7e0b7
YW
1458 if (x == USEC_INFINITY && y == USEC_INFINITY) {
1459 *ret = 0;
68db7a3b 1460 return 0;
fcd7e0b7 1461 }
68db7a3b 1462
fcd7e0b7 1463 *ret = MIN(x, y);
68db7a3b
ZJS
1464 return 1;
1465}
1466
2ab3050f 1467bool job_may_gc(Job *j) {
c5a97ed1 1468 Unit *other;
c5a97ed1
LP
1469
1470 assert(j);
6985e396 1471 assert(j->manager);
c5a97ed1
LP
1472
1473 /* Checks whether this job should be GC'ed away. We only do this for jobs of units that have no effect on their
2ab3050f
ZJS
1474 * own and just track external state. For now the only unit type that qualifies for this are .device units.
1475 * Returns true if the job can be collected. */
c5a97ed1
LP
1476
1477 if (!UNIT_VTABLE(j->unit)->gc_jobs)
2ab3050f 1478 return false;
c5a97ed1 1479
af05bb97
LP
1480 /* Make sure to send out pending D-Bus events before we unload the unit */
1481 if (j->in_dbus_queue)
1482 return false;
1483
c5a97ed1 1484 if (sd_bus_track_count(j->bus_track) > 0)
2ab3050f 1485 return false;
c5a97ed1
LP
1486
1487 /* FIXME: So this is a bit ugly: for now we don't properly track references made via private bus connections
1488 * (because it's nasty, as sd_bus_track doesn't apply to it). We simply remember that the job was once
1489 * referenced by one, and reset this whenever we notice that no private bus connections are around. This means
1490 * the GC is a bit too conservative when it comes to jobs created by private bus connections. */
1491 if (j->ref_by_private_bus) {
6985e396 1492 if (set_isempty(j->manager->private_buses))
c5a97ed1
LP
1493 j->ref_by_private_bus = false;
1494 else
2ab3050f 1495 return false;
c5a97ed1
LP
1496 }
1497
1498 if (j->type == JOB_NOP)
2ab3050f 1499 return false;
c5a97ed1 1500
e602f152 1501 /* The logic is inverse to job_is_runnable, we cannot GC as long as we block any job. */
15ed3c3a
LP
1502 UNIT_FOREACH_DEPENDENCY(other, j->unit, UNIT_ATOM_BEFORE)
1503 if (other->job && job_compare(j, other->job, UNIT_ATOM_BEFORE) < 0)
2ab3050f 1504 return false;
c5a97ed1 1505
15ed3c3a
LP
1506 UNIT_FOREACH_DEPENDENCY(other, j->unit, UNIT_ATOM_AFTER)
1507 if (other->job && job_compare(j, other->job, UNIT_ATOM_AFTER) < 0)
2ab3050f 1508 return false;
c5a97ed1 1509
2ab3050f 1510 return true;
c5a97ed1
LP
1511}
1512
1513void job_add_to_gc_queue(Job *j) {
1514 assert(j);
6985e396 1515 assert(j->manager);
c5a97ed1
LP
1516
1517 if (j->in_gc_queue)
1518 return;
1519
2ab3050f 1520 if (!job_may_gc(j))
c5a97ed1
LP
1521 return;
1522
6985e396 1523 LIST_PREPEND(gc_queue, j->manager->gc_job_queue, j);
c5a97ed1
LP
1524 j->in_gc_queue = true;
1525}
1526
e602f152 1527static int job_compare_id(Job * const *a, Job * const *b) {
93bab288 1528 return CMP((*a)->id, (*b)->id);
15ea79f8
LP
1529}
1530
1531static size_t sort_job_list(Job **list, size_t n) {
1532 Job *previous = NULL;
1533 size_t a, b;
1534
1535 /* Order by numeric IDs */
e602f152 1536 typesafe_qsort(list, n, job_compare_id);
15ea79f8
LP
1537
1538 /* Filter out duplicates */
1539 for (a = 0, b = 0; a < n; a++) {
1540
1541 if (previous == list[a])
1542 continue;
1543
1544 previous = list[b++] = list[a];
1545 }
1546
1547 return b;
1548}
1549
1550int job_get_before(Job *j, Job*** ret) {
1551 _cleanup_free_ Job** list = NULL;
15ea79f8 1552 Unit *other = NULL;
319a4f4b 1553 size_t n = 0;
15ea79f8
LP
1554
1555 /* Returns a list of all pending jobs that need to finish before this job may be started. */
1556
1557 assert(j);
1558 assert(ret);
1559
1560 if (j->ignore_order) {
1561 *ret = NULL;
1562 return 0;
1563 }
1564
15ed3c3a 1565 UNIT_FOREACH_DEPENDENCY(other, j->unit, UNIT_ATOM_AFTER) {
e602f152
MK
1566 if (!other->job)
1567 continue;
15ed3c3a 1568 if (job_compare(j, other->job, UNIT_ATOM_AFTER) <= 0)
e602f152 1569 continue;
15ea79f8 1570
319a4f4b 1571 if (!GREEDY_REALLOC(list, n+1))
e602f152
MK
1572 return -ENOMEM;
1573 list[n++] = other->job;
15ea79f8
LP
1574 }
1575
15ed3c3a 1576 UNIT_FOREACH_DEPENDENCY(other, j->unit, UNIT_ATOM_BEFORE) {
15ea79f8
LP
1577 if (!other->job)
1578 continue;
15ed3c3a 1579 if (job_compare(j, other->job, UNIT_ATOM_BEFORE) <= 0)
15ea79f8
LP
1580 continue;
1581
319a4f4b 1582 if (!GREEDY_REALLOC(list, n+1))
15ea79f8
LP
1583 return -ENOMEM;
1584 list[n++] = other->job;
1585 }
1586
1587 n = sort_job_list(list, n);
1588
1cc6c93a 1589 *ret = TAKE_PTR(list);
15ea79f8
LP
1590
1591 return (int) n;
1592}
1593
1594int job_get_after(Job *j, Job*** ret) {
1595 _cleanup_free_ Job** list = NULL;
15ea79f8 1596 Unit *other = NULL;
319a4f4b 1597 size_t n = 0;
15ea79f8
LP
1598
1599 assert(j);
1600 assert(ret);
1601
1602 /* Returns a list of all pending jobs that are waiting for this job to finish. */
1603
15ed3c3a 1604 UNIT_FOREACH_DEPENDENCY(other, j->unit, UNIT_ATOM_BEFORE) {
15ea79f8
LP
1605 if (!other->job)
1606 continue;
1607
1608 if (other->job->ignore_order)
1609 continue;
1610
15ed3c3a 1611 if (job_compare(j, other->job, UNIT_ATOM_BEFORE) >= 0)
15ea79f8
LP
1612 continue;
1613
319a4f4b 1614 if (!GREEDY_REALLOC(list, n+1))
15ea79f8
LP
1615 return -ENOMEM;
1616 list[n++] = other->job;
1617 }
1618
15ed3c3a 1619 UNIT_FOREACH_DEPENDENCY(other, j->unit, UNIT_ATOM_AFTER) {
e602f152
MK
1620 if (!other->job)
1621 continue;
1622
1623 if (other->job->ignore_order)
1624 continue;
15ea79f8 1625
15ed3c3a 1626 if (job_compare(j, other->job, UNIT_ATOM_AFTER) >= 0)
e602f152 1627 continue;
15ea79f8 1628
319a4f4b 1629 if (!GREEDY_REALLOC(list, n+1))
e602f152
MK
1630 return -ENOMEM;
1631 list[n++] = other->job;
15ea79f8
LP
1632 }
1633
1634 n = sort_job_list(list, n);
1635
1cc6c93a 1636 *ret = TAKE_PTR(list);
15ea79f8
LP
1637
1638 return (int) n;
1639}
1640
94f04347
LP
1641static const char* const job_state_table[_JOB_STATE_MAX] = {
1642 [JOB_WAITING] = "waiting",
0a23a627 1643 [JOB_RUNNING] = "running",
94f04347
LP
1644};
1645
1646DEFINE_STRING_TABLE_LOOKUP(job_state, JobState);
1647
1648static const char* const job_type_table[_JOB_TYPE_MAX] = {
48d83e33
ZJS
1649 [JOB_START] = "start",
1650 [JOB_VERIFY_ACTIVE] = "verify-active",
1651 [JOB_STOP] = "stop",
1652 [JOB_RELOAD] = "reload",
94f04347 1653 [JOB_RELOAD_OR_START] = "reload-or-start",
48d83e33
ZJS
1654 [JOB_RESTART] = "restart",
1655 [JOB_TRY_RESTART] = "try-restart",
1656 [JOB_TRY_RELOAD] = "try-reload",
1657 [JOB_NOP] = "nop",
94f04347
LP
1658};
1659
1660DEFINE_STRING_TABLE_LOOKUP(job_type, JobType);
b548631a 1661
5d44db4a 1662static const char* const job_result_table[_JOB_RESULT_MAX] = {
48d83e33
ZJS
1663 [JOB_DONE] = "done",
1664 [JOB_CANCELED] = "canceled",
1665 [JOB_TIMEOUT] = "timeout",
1666 [JOB_FAILED] = "failed",
1667 [JOB_DEPENDENCY] = "dependency",
1668 [JOB_SKIPPED] = "skipped",
1669 [JOB_INVALID] = "invalid",
1670 [JOB_ASSERT] = "assert",
0faacd47 1671 [JOB_UNSUPPORTED] = "unsupported",
48d83e33
ZJS
1672 [JOB_COLLECTED] = "collected",
1673 [JOB_ONCE] = "once",
4cb2e6af 1674 [JOB_FROZEN] = "frozen",
9ca16f6f 1675 [JOB_CONCURRENCY] = "concurrency",
5d44db4a
LP
1676};
1677
1678DEFINE_STRING_TABLE_LOOKUP(job_result, JobResult);
94bd7323
EV
1679
1680const char* job_type_to_access_method(JobType t) {
1681 assert(t >= 0);
1682 assert(t < _JOB_TYPE_MAX);
1683
1684 if (IN_SET(t, JOB_START, JOB_RESTART, JOB_TRY_RESTART))
1685 return "start";
1686 else if (t == JOB_STOP)
1687 return "stop";
1688 else
1689 return "reload";
1690}
e602f152
MK
1691
1692/*
1693 * assume_dep assumed dependency between units (a is before/after b)
1694 *
1695 * Returns
1696 * 0 jobs are independent,
1697 * >0 a should run after b,
1698 * <0 a should run before b,
1699 *
1700 * The logic means that for a service a and a service b where b.After=a:
1701 *
1702 * start a + start b → 1st step start a, 2nd step start b
1703 * start a + stop b → 1st step stop b, 2nd step start a
1704 * stop a + start b → 1st step stop a, 2nd step start b
1705 * stop a + stop b → 1st step stop b, 2nd step stop a
1706 *
defe63b0 1707 * This has the side effect that restarts are properly synchronized too.
e602f152 1708 */
15ed3c3a
LP
1709int job_compare(Job *a, Job *b, UnitDependencyAtom assume_dep) {
1710 assert(a);
1711 assert(b);
e602f152
MK
1712 assert(a->type < _JOB_TYPE_MAX_IN_TRANSACTION);
1713 assert(b->type < _JOB_TYPE_MAX_IN_TRANSACTION);
15ed3c3a 1714 assert(IN_SET(assume_dep, UNIT_ATOM_AFTER, UNIT_ATOM_BEFORE));
e602f152
MK
1715
1716 /* Trivial cases first */
1717 if (a->type == JOB_NOP || b->type == JOB_NOP)
1718 return 0;
1719
1720 if (a->ignore_order || b->ignore_order)
1721 return 0;
1722
15ed3c3a
LP
1723 if (assume_dep == UNIT_ATOM_AFTER)
1724 return -job_compare(b, a, UNIT_ATOM_BEFORE);
e602f152 1725
defe63b0
LP
1726 /* Let's make it simple, JOB_STOP goes always first (in case both ua and ub stop, then ub's stop goes
1727 * first anyway). JOB_RESTART is JOB_STOP in disguise (before it is patched to JOB_START). */
e602f152
MK
1728 if (IN_SET(b->type, JOB_STOP, JOB_RESTART))
1729 return 1;
1730 else
1731 return -1;
1732}
48b92b37
LB
1733
1734void job_set_activation_details(Job *j, ActivationDetails *info) {
1735 /* Existing (older) ActivationDetails win, newer ones are discarded. */
1736 if (!j || j->activation_details || !info)
1737 return; /* Nothing to do. */
1738
1739 j->activation_details = activation_details_ref(info);
1740}