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