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