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