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