1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "sd-messages.h"
8 #include "alloc-util.h"
16 #include "parse-util.h"
19 #include "stdio-util.h"
20 #include "string-table.h"
21 #include "string-util.h"
23 #include "terminal-util.h"
27 Job
* job_new_raw(Unit
*unit
) {
30 /* used for deserialization */
38 j
->manager
= unit
->manager
;
40 j
->type
= _JOB_TYPE_INVALID
;
46 Job
* job_new(Unit
*unit
, JobType type
) {
49 assert(type
< _JOB_TYPE_MAX
);
51 j
= job_new_raw(unit
);
55 j
->id
= j
->manager
->current_job_id
++;
58 /* We don't link it here, that's what job_dependency() is for */
63 void job_unlink(Job
*j
) {
65 assert(!j
->installed
);
66 assert(!j
->transaction_prev
);
67 assert(!j
->transaction_next
);
68 assert(!j
->subject_list
);
69 assert(!j
->object_list
);
71 if (j
->in_run_queue
) {
72 LIST_REMOVE(run_queue
, j
->manager
->run_queue
, j
);
73 j
->in_run_queue
= false;
76 if (j
->in_dbus_queue
) {
77 LIST_REMOVE(dbus_queue
, j
->manager
->dbus_job_queue
, j
);
78 j
->in_dbus_queue
= false;
82 LIST_REMOVE(gc_queue
, j
->manager
->gc_job_queue
, j
);
83 j
->in_gc_queue
= false;
86 j
->timer_event_source
= sd_event_source_unref(j
->timer_event_source
);
89 void job_free(Job
*j
) {
91 assert(!j
->installed
);
92 assert(!j
->transaction_prev
);
93 assert(!j
->transaction_next
);
94 assert(!j
->subject_list
);
95 assert(!j
->object_list
);
99 sd_bus_track_unref(j
->bus_track
);
100 strv_free(j
->deserialized_clients
);
105 static void job_set_state(Job
*j
, JobState state
) {
108 assert(state
< _JOB_STATE_MAX
);
110 if (j
->state
== state
)
118 if (j
->state
== JOB_RUNNING
)
119 j
->unit
->manager
->n_running_jobs
++;
121 assert(j
->state
== JOB_WAITING
);
122 assert(j
->unit
->manager
->n_running_jobs
> 0);
124 j
->unit
->manager
->n_running_jobs
--;
126 if (j
->unit
->manager
->n_running_jobs
<= 0)
127 j
->unit
->manager
->jobs_in_progress_event_source
= sd_event_source_unref(j
->unit
->manager
->jobs_in_progress_event_source
);
131 void job_uninstall(Job
*j
) {
134 assert(j
->installed
);
136 job_set_state(j
, JOB_WAITING
);
138 pj
= (j
->type
== JOB_NOP
) ? &j
->unit
->nop_job
: &j
->unit
->job
;
141 /* Detach from next 'bigger' objects */
143 /* daemon-reload should be transparent to job observers */
144 if (!MANAGER_IS_RELOADING(j
->manager
))
145 bus_job_send_removed_signal(j
);
149 unit_add_to_gc_queue(j
->unit
);
151 hashmap_remove(j
->manager
->jobs
, UINT32_TO_PTR(j
->id
));
152 j
->installed
= false;
155 static bool job_type_allows_late_merge(JobType t
) {
156 /* Tells whether it is OK to merge a job of type 't' with an already
158 * Reloads cannot be merged this way. Think of the sequence:
159 * 1. Reload of a daemon is in progress; the daemon has already loaded
160 * its config file, but hasn't completed the reload operation yet.
161 * 2. Edit foo's config file.
162 * 3. Trigger another reload to have the daemon use the new config.
163 * Should the second reload job be merged into the first one, the daemon
164 * would not know about the new config.
165 * JOB_RESTART jobs on the other hand can be merged, because they get
166 * patched into JOB_START after stopping the unit. So if we see a
167 * JOB_RESTART running, it means the unit hasn't stopped yet and at
168 * this time the merge is still allowed. */
169 return t
!= JOB_RELOAD
;
172 static void job_merge_into_installed(Job
*j
, Job
*other
) {
173 assert(j
->installed
);
174 assert(j
->unit
== other
->unit
);
176 if (j
->type
!= JOB_NOP
)
177 assert_se(job_type_merge_and_collapse(&j
->type
, other
->type
, j
->unit
) == 0);
179 assert(other
->type
== JOB_NOP
);
181 j
->irreversible
= j
->irreversible
|| other
->irreversible
;
182 j
->ignore_order
= j
->ignore_order
|| other
->ignore_order
;
185 Job
* job_install(Job
*j
) {
189 assert(!j
->installed
);
190 assert(j
->type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
191 assert(j
->state
== JOB_WAITING
);
193 pj
= (j
->type
== JOB_NOP
) ? &j
->unit
->nop_job
: &j
->unit
->job
;
197 if (job_type_is_conflicting(uj
->type
, j
->type
))
198 job_finish_and_invalidate(uj
, JOB_CANCELED
, false, false);
200 /* not conflicting, i.e. mergeable */
202 if (uj
->state
== JOB_WAITING
||
203 (job_type_allows_late_merge(j
->type
) && job_type_is_superset(uj
->type
, j
->type
))) {
204 job_merge_into_installed(uj
, j
);
205 log_unit_debug(uj
->unit
,
206 "Merged into installed job %s/%s as %u",
207 uj
->unit
->id
, job_type_to_string(uj
->type
), (unsigned) uj
->id
);
210 /* already running and not safe to merge into */
211 /* Patch uj to become a merged job and re-run it. */
212 /* XXX It should be safer to queue j to run after uj finishes, but it is
213 * not currently possible to have more than one installed job per unit. */
214 job_merge_into_installed(uj
, j
);
215 log_unit_debug(uj
->unit
,
216 "Merged into running job, re-running: %s/%s as %u",
217 uj
->unit
->id
, job_type_to_string(uj
->type
), (unsigned) uj
->id
);
219 job_set_state(uj
, JOB_WAITING
);
225 /* Install the job */
229 j
->manager
->n_installed_jobs
++;
230 log_unit_debug(j
->unit
,
231 "Installed new job %s/%s as %u",
232 j
->unit
->id
, job_type_to_string(j
->type
), (unsigned) j
->id
);
234 job_add_to_gc_queue(j
);
239 int job_install_deserialized(Job
*j
) {
242 assert(!j
->installed
);
244 if (j
->type
< 0 || j
->type
>= _JOB_TYPE_MAX_IN_TRANSACTION
) {
245 log_debug("Invalid job type %s in deserialization.", strna(job_type_to_string(j
->type
)));
249 pj
= (j
->type
== JOB_NOP
) ? &j
->unit
->nop_job
: &j
->unit
->job
;
251 log_unit_debug(j
->unit
, "Unit already has a job installed. Not installing deserialized job.");
259 if (j
->state
== JOB_RUNNING
)
260 j
->unit
->manager
->n_running_jobs
++;
262 log_unit_debug(j
->unit
,
263 "Reinstalled deserialized job %s/%s as %u",
264 j
->unit
->id
, job_type_to_string(j
->type
), (unsigned) j
->id
);
268 JobDependency
* job_dependency_new(Job
*subject
, Job
*object
, bool matters
, bool conflicts
) {
273 /* Adds a new job link, which encodes that the 'subject' job
274 * needs the 'object' job in some way. If 'subject' is NULL
275 * this means the 'anchor' job (i.e. the one the user
276 * explicitly asked for) is the requester. */
278 l
= new0(JobDependency
, 1);
282 l
->subject
= subject
;
284 l
->matters
= matters
;
285 l
->conflicts
= conflicts
;
288 LIST_PREPEND(subject
, subject
->subject_list
, l
);
290 LIST_PREPEND(object
, object
->object_list
, l
);
295 void job_dependency_free(JobDependency
*l
) {
299 LIST_REMOVE(subject
, l
->subject
->subject_list
, l
);
301 LIST_REMOVE(object
, l
->object
->object_list
, l
);
306 void job_dump(Job
*j
, FILE*f
, const char *prefix
) {
310 prefix
= strempty(prefix
);
314 "%s\tAction: %s -> %s\n"
316 "%s\tIrreversible: %s\n"
319 prefix
, j
->unit
->id
, job_type_to_string(j
->type
),
320 prefix
, job_state_to_string(j
->state
),
321 prefix
, yes_no(j
->irreversible
),
322 prefix
, yes_no(job_may_gc(j
)));
326 * Merging is commutative, so imagine the matrix as symmetric. We store only
327 * its lower triangle to avoid duplication. We don't store the main diagonal,
328 * because A merged with A is simply A.
330 * If the resulting type is collapsed immediately afterwards (to get rid of
331 * the JOB_RELOAD_OR_START, which lies outside the lookup function's domain),
332 * the following properties hold:
334 * Merging is associative! A merged with B, and then merged with C is the same
335 * as A merged with the result of B merged with C.
337 * Mergeability is transitive! If A can be merged with B and B with C then
340 * Also, if A merged with B cannot be merged with C, then either A or B cannot
341 * be merged with C either.
343 static const JobType job_merging_table
[] = {
344 /* What \ With * JOB_START JOB_VERIFY_ACTIVE JOB_STOP JOB_RELOAD */
345 /*********************************************************************************/
347 /*JOB_VERIFY_ACTIVE */ JOB_START
,
348 /*JOB_STOP */ -1, -1,
349 /*JOB_RELOAD */ JOB_RELOAD_OR_START
, JOB_RELOAD
, -1,
350 /*JOB_RESTART */ JOB_RESTART
, JOB_RESTART
, -1, JOB_RESTART
,
353 JobType
job_type_lookup_merge(JobType a
, JobType b
) {
354 assert_cc(ELEMENTSOF(job_merging_table
) == _JOB_TYPE_MAX_MERGING
* (_JOB_TYPE_MAX_MERGING
- 1) / 2);
355 assert(a
>= 0 && a
< _JOB_TYPE_MAX_MERGING
);
356 assert(b
>= 0 && b
< _JOB_TYPE_MAX_MERGING
);
367 return job_merging_table
[(a
- 1) * a
/ 2 + b
];
370 bool job_type_is_redundant(JobType a
, UnitActiveState b
) {
374 return IN_SET(b
, UNIT_ACTIVE
, UNIT_RELOADING
);
377 return IN_SET(b
, UNIT_INACTIVE
, UNIT_FAILED
);
379 case JOB_VERIFY_ACTIVE
:
380 return IN_SET(b
, UNIT_ACTIVE
, UNIT_RELOADING
);
388 b
== UNIT_ACTIVATING
;
394 assert_not_reached("Invalid job type");
398 JobType
job_type_collapse(JobType t
, Unit
*u
) {
403 case JOB_TRY_RESTART
:
404 s
= unit_active_state(u
);
405 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s
))
411 s
= unit_active_state(u
);
412 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s
))
417 case JOB_RELOAD_OR_START
:
418 s
= unit_active_state(u
);
419 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s
))
429 int job_type_merge_and_collapse(JobType
*a
, JobType b
, Unit
*u
) {
432 t
= job_type_lookup_merge(*a
, b
);
436 *a
= job_type_collapse(t
, u
);
440 static bool job_is_runnable(Job
*j
) {
446 assert(j
->installed
);
448 /* Checks whether there is any job running for the units this
449 * job needs to be running after (in the case of a 'positive'
450 * job type) or before (in the case of a 'negative' job
453 /* Note that unit types have a say in what is runnable,
454 * too. For example, if they return -EAGAIN from
455 * unit_start() they can indicate they are not
458 /* First check if there is an override */
462 if (j
->type
== JOB_NOP
)
465 if (IN_SET(j
->type
, JOB_START
, JOB_VERIFY_ACTIVE
, JOB_RELOAD
)) {
466 /* Immediate result is that the job is or might be
467 * started. In this case let's wait for the
468 * dependencies, regardless whether they are
469 * starting or stopping something. */
471 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_AFTER
], i
)
476 /* Also, if something else is being stopped and we should
477 * change state after it, then let's wait. */
479 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_BEFORE
], i
)
481 IN_SET(other
->job
->type
, JOB_STOP
, JOB_RESTART
))
484 /* This means that for a service a and a service b where b
485 * shall be started after a:
487 * start a + start b → 1st step start a, 2nd step start b
488 * start a + stop b → 1st step stop b, 2nd step start a
489 * stop a + start b → 1st step stop a, 2nd step start b
490 * stop a + stop b → 1st step stop b, 2nd step stop a
492 * This has the side effect that restarts are properly
493 * synchronized too. */
498 static void job_change_type(Job
*j
, JobType newtype
) {
501 log_unit_debug(j
->unit
,
502 "Converting job %s/%s -> %s/%s",
503 j
->unit
->id
, job_type_to_string(j
->type
),
504 j
->unit
->id
, job_type_to_string(newtype
));
509 static int job_perform_on_unit(Job
**j
) {
516 /* While we execute this operation the job might go away (for
517 * example: because it finishes immediately or is replaced by
518 * a new, conflicting job.) To make sure we don't access a
519 * freed job later on we store the id here, so that we can
520 * verify the job is still valid. */
547 assert_not_reached("Invalid job type");
550 /* Log if the job still exists and the start/stop/reload function
551 * actually did something. */
552 *j
= manager_get_job(m
, id
);
554 unit_status_emit_starting_stopping_reloading(u
, t
);
559 int job_run_and_invalidate(Job
*j
) {
563 assert(j
->installed
);
564 assert(j
->type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
565 assert(j
->in_run_queue
);
567 LIST_REMOVE(run_queue
, j
->manager
->run_queue
, j
);
568 j
->in_run_queue
= false;
570 if (j
->state
!= JOB_WAITING
)
573 if (!job_is_runnable(j
))
576 job_start_timer(j
, true);
577 job_set_state(j
, JOB_RUNNING
);
578 job_add_to_dbus_queue(j
);
582 case JOB_VERIFY_ACTIVE
: {
583 UnitActiveState t
= unit_active_state(j
->unit
);
584 if (UNIT_IS_ACTIVE_OR_RELOADING(t
))
586 else if (t
== UNIT_ACTIVATING
)
596 r
= job_perform_on_unit(&j
);
598 /* If the unit type does not support starting/stopping,
599 * then simply wait. */
605 r
= job_perform_on_unit(&j
);
613 assert_not_reached("Unknown job type");
618 r
= job_finish_and_invalidate(j
, JOB_DONE
, true, true);
619 else if (r
== -EBADR
)
620 r
= job_finish_and_invalidate(j
, JOB_SKIPPED
, true, false);
621 else if (r
== -ENOEXEC
)
622 r
= job_finish_and_invalidate(j
, JOB_INVALID
, true, false);
623 else if (r
== -EPROTO
)
624 r
= job_finish_and_invalidate(j
, JOB_ASSERT
, true, false);
625 else if (r
== -EOPNOTSUPP
)
626 r
= job_finish_and_invalidate(j
, JOB_UNSUPPORTED
, true, false);
627 else if (r
== -ENOLINK
)
628 r
= job_finish_and_invalidate(j
, JOB_DEPENDENCY
, true, false);
629 else if (r
== -ESTALE
)
630 r
= job_finish_and_invalidate(j
, JOB_ONCE
, true, false);
631 else if (r
== -EAGAIN
)
632 job_set_state(j
, JOB_WAITING
);
634 r
= job_finish_and_invalidate(j
, JOB_FAILED
, true, false);
640 _pure_
static const char *job_get_status_message_format(Unit
*u
, JobType t
, JobResult result
) {
642 static const char *const generic_finished_start_job
[_JOB_RESULT_MAX
] = {
643 [JOB_DONE
] = "Started %s.",
644 [JOB_TIMEOUT
] = "Timed out starting %s.",
645 [JOB_FAILED
] = "Failed to start %s.",
646 [JOB_DEPENDENCY
] = "Dependency failed for %s.",
647 [JOB_ASSERT
] = "Assertion failed for %s.",
648 [JOB_UNSUPPORTED
] = "Starting of %s not supported.",
649 [JOB_COLLECTED
] = "Unnecessary job for %s was removed.",
650 [JOB_ONCE
] = "Unit %s has been started before and cannot be started again."
652 static const char *const generic_finished_stop_job
[_JOB_RESULT_MAX
] = {
653 [JOB_DONE
] = "Stopped %s.",
654 [JOB_FAILED
] = "Stopped (with error) %s.",
655 [JOB_TIMEOUT
] = "Timed out stopping %s.",
657 static const char *const generic_finished_reload_job
[_JOB_RESULT_MAX
] = {
658 [JOB_DONE
] = "Reloaded %s.",
659 [JOB_FAILED
] = "Reload failed for %s.",
660 [JOB_TIMEOUT
] = "Timed out reloading %s.",
662 /* When verify-active detects the unit is inactive, report it.
663 * Most likely a DEPEND warning from a requisiting unit will
664 * occur next and it's nice to see what was requisited. */
665 static const char *const generic_finished_verify_active_job
[_JOB_RESULT_MAX
] = {
666 [JOB_SKIPPED
] = "%s is not active.",
669 const UnitStatusMessageFormats
*format_table
;
674 assert(t
< _JOB_TYPE_MAX
);
676 if (IN_SET(t
, JOB_START
, JOB_STOP
, JOB_RESTART
)) {
677 format_table
= &UNIT_VTABLE(u
)->status_message_formats
;
679 format
= t
== JOB_START
? format_table
->finished_start_job
[result
] :
680 format_table
->finished_stop_job
[result
];
686 /* Return generic strings */
688 return generic_finished_start_job
[result
];
689 else if (IN_SET(t
, JOB_STOP
, JOB_RESTART
))
690 return generic_finished_stop_job
[result
];
691 else if (t
== JOB_RELOAD
)
692 return generic_finished_reload_job
[result
];
693 else if (t
== JOB_VERIFY_ACTIVE
)
694 return generic_finished_verify_active_job
[result
];
699 static const struct {
700 const char *color
, *word
;
701 } job_print_status_messages
[_JOB_RESULT_MAX
] = {
702 [JOB_DONE
] = { ANSI_OK_COLOR
, " OK " },
703 [JOB_TIMEOUT
] = { ANSI_HIGHLIGHT_RED
, " TIME " },
704 [JOB_FAILED
] = { ANSI_HIGHLIGHT_RED
, "FAILED" },
705 [JOB_DEPENDENCY
] = { ANSI_HIGHLIGHT_YELLOW
, "DEPEND" },
706 [JOB_SKIPPED
] = { ANSI_HIGHLIGHT
, " INFO " },
707 [JOB_ASSERT
] = { ANSI_HIGHLIGHT_YELLOW
, "ASSERT" },
708 [JOB_UNSUPPORTED
] = { ANSI_HIGHLIGHT_YELLOW
, "UNSUPP" },
710 [JOB_ONCE
] = { ANSI_HIGHLIGHT_RED
, " ONCE " },
713 static void job_print_status_message(Unit
*u
, JobType t
, JobResult result
) {
719 assert(t
< _JOB_TYPE_MAX
);
721 /* Reload status messages have traditionally not been printed to console. */
725 if (!job_print_status_messages
[result
].word
)
728 format
= job_get_status_message_format(u
, t
, result
);
732 if (log_get_show_color())
733 status
= strjoina(job_print_status_messages
[result
].color
,
734 job_print_status_messages
[result
].word
,
737 status
= job_print_status_messages
[result
].word
;
739 if (result
!= JOB_DONE
)
740 manager_flip_auto_status(u
->manager
, true);
742 DISABLE_WARNING_FORMAT_NONLITERAL
;
743 unit_status_printf(u
, status
, format
);
746 if (t
== JOB_START
&& result
== JOB_FAILED
) {
747 _cleanup_free_
char *quoted
;
749 quoted
= shell_maybe_quote(u
->id
, ESCAPE_BACKSLASH
);
750 manager_status_printf(u
->manager
, STATUS_TYPE_NORMAL
, NULL
, "See 'systemctl status %s' for details.", strna(quoted
));
754 static void job_log_status_message(Unit
*u
, JobType t
, JobResult result
) {
755 const char *format
, *mid
;
757 static const int job_result_log_level
[_JOB_RESULT_MAX
] = {
758 [JOB_DONE
] = LOG_INFO
,
759 [JOB_CANCELED
] = LOG_INFO
,
760 [JOB_TIMEOUT
] = LOG_ERR
,
761 [JOB_FAILED
] = LOG_ERR
,
762 [JOB_DEPENDENCY
] = LOG_WARNING
,
763 [JOB_SKIPPED
] = LOG_NOTICE
,
764 [JOB_INVALID
] = LOG_INFO
,
765 [JOB_ASSERT
] = LOG_WARNING
,
766 [JOB_UNSUPPORTED
] = LOG_WARNING
,
767 [JOB_COLLECTED
] = LOG_INFO
,
768 [JOB_ONCE
] = LOG_ERR
,
773 assert(t
< _JOB_TYPE_MAX
);
775 /* Skip printing if output goes to the console, and job_print_status_message()
776 will actually print something to the console. */
777 if (log_on_console() && job_print_status_messages
[result
].word
)
780 format
= job_get_status_message_format(u
, t
, result
);
784 /* The description might be longer than the buffer, but that's OK,
785 * we'll just truncate it here. Note that we use snprintf() rather than
786 * xsprintf() on purpose here: we are fine with truncation and don't
787 * consider that an error. */
788 DISABLE_WARNING_FORMAT_NONLITERAL
;
789 (void) snprintf(buf
, sizeof(buf
), format
, unit_description(u
));
795 if (result
== JOB_DONE
)
796 mid
= "MESSAGE_ID=" SD_MESSAGE_UNIT_STARTED_STR
;
798 mid
= "MESSAGE_ID=" SD_MESSAGE_UNIT_FAILED_STR
;
802 mid
= "MESSAGE_ID=" SD_MESSAGE_UNIT_RELOADED_STR
;
807 mid
= "MESSAGE_ID=" SD_MESSAGE_UNIT_STOPPED_STR
;
811 log_struct(job_result_log_level
[result
],
812 LOG_MESSAGE("%s", buf
),
813 "JOB_TYPE=%s", job_type_to_string(t
),
814 "JOB_RESULT=%s", job_result_to_string(result
),
816 LOG_UNIT_INVOCATION_ID(u
));
820 log_struct(job_result_log_level
[result
],
821 LOG_MESSAGE("%s", buf
),
822 "JOB_TYPE=%s", job_type_to_string(t
),
823 "JOB_RESULT=%s", job_result_to_string(result
),
825 LOG_UNIT_INVOCATION_ID(u
),
829 static void job_emit_status_message(Unit
*u
, JobType t
, JobResult result
) {
832 /* No message if the job did not actually do anything due to failed condition. */
833 if (t
== JOB_START
&& result
== JOB_DONE
&& !u
->condition_result
)
836 job_log_status_message(u
, t
, result
);
837 job_print_status_message(u
, t
, result
);
840 static void job_fail_dependencies(Unit
*u
, UnitDependency d
) {
847 HASHMAP_FOREACH_KEY(v
, other
, u
->dependencies
[d
], i
) {
852 if (!IN_SET(j
->type
, JOB_START
, JOB_VERIFY_ACTIVE
))
855 job_finish_and_invalidate(j
, JOB_DEPENDENCY
, true, false);
859 static int job_save_pending_finished_job(Job
*j
) {
864 r
= set_ensure_allocated(&j
->manager
->pending_finished_jobs
, NULL
);
869 return set_put(j
->manager
->pending_finished_jobs
, j
);
872 int job_finish_and_invalidate(Job
*j
, JobResult result
, bool recursive
, bool already
) {
880 assert(j
->installed
);
881 assert(j
->type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
888 log_unit_debug(u
, "Job %s/%s finished, result=%s", u
->id
, job_type_to_string(t
), job_result_to_string(result
));
890 /* If this job did nothing to respective unit we don't log the status message */
892 job_emit_status_message(u
, t
, result
);
894 /* Patch restart jobs so that they become normal start jobs */
895 if (result
== JOB_DONE
&& t
== JOB_RESTART
) {
897 job_change_type(j
, JOB_START
);
898 job_set_state(j
, JOB_WAITING
);
900 job_add_to_dbus_queue(j
);
901 job_add_to_run_queue(j
);
902 job_add_to_gc_queue(j
);
907 if (IN_SET(result
, JOB_FAILED
, JOB_INVALID
))
908 j
->manager
->n_failed_jobs
++;
911 /* Keep jobs started before the reload to send singal later, free all others */
912 if (!MANAGER_IS_RELOADING(j
->manager
) ||
914 job_save_pending_finished_job(j
) < 0)
917 /* Fail depending jobs on failure */
918 if (result
!= JOB_DONE
&& recursive
) {
919 if (IN_SET(t
, JOB_START
, JOB_VERIFY_ACTIVE
)) {
920 job_fail_dependencies(u
, UNIT_REQUIRED_BY
);
921 job_fail_dependencies(u
, UNIT_REQUISITE_OF
);
922 job_fail_dependencies(u
, UNIT_BOUND_BY
);
923 } else if (t
== JOB_STOP
)
924 job_fail_dependencies(u
, UNIT_CONFLICTED_BY
);
927 /* Trigger OnFailure dependencies that are not generated by
928 * the unit itself. We don't treat JOB_CANCELED as failure in
929 * this context. And JOB_FAILURE is already handled by the
931 if (IN_SET(result
, JOB_TIMEOUT
, JOB_DEPENDENCY
)) {
932 log_struct(LOG_NOTICE
,
933 "JOB_TYPE=%s", job_type_to_string(t
),
934 "JOB_RESULT=%s", job_result_to_string(result
),
936 LOG_UNIT_MESSAGE(u
, "Job %s/%s failed with result '%s'.",
938 job_type_to_string(t
),
939 job_result_to_string(result
)));
941 unit_start_on_failure(u
);
944 unit_trigger_notify(u
);
947 /* Try to start the next jobs that can be started */
948 HASHMAP_FOREACH_KEY(v
, other
, u
->dependencies
[UNIT_AFTER
], i
)
950 job_add_to_run_queue(other
->job
);
951 job_add_to_gc_queue(other
->job
);
953 HASHMAP_FOREACH_KEY(v
, other
, u
->dependencies
[UNIT_BEFORE
], i
)
955 job_add_to_run_queue(other
->job
);
956 job_add_to_gc_queue(other
->job
);
959 manager_check_finished(u
->manager
);
964 static int job_dispatch_timer(sd_event_source
*s
, uint64_t monotonic
, void *userdata
) {
969 assert(s
== j
->timer_event_source
);
971 log_unit_warning(j
->unit
, "Job %s/%s timed out.", j
->unit
->id
, job_type_to_string(j
->type
));
974 job_finish_and_invalidate(j
, JOB_TIMEOUT
, true, false);
976 emergency_action(u
->manager
, u
->job_timeout_action
, u
->job_timeout_reboot_arg
, "job timed out");
981 int job_start_timer(Job
*j
, bool job_running
) {
983 usec_t timeout_time
, old_timeout_time
;
986 j
->begin_running_usec
= now(CLOCK_MONOTONIC
);
988 if (j
->unit
->job_running_timeout
== USEC_INFINITY
)
991 timeout_time
= usec_add(j
->begin_running_usec
, j
->unit
->job_running_timeout
);
993 if (j
->timer_event_source
) {
994 /* Update only if JobRunningTimeoutSec= results in earlier timeout */
995 r
= sd_event_source_get_time(j
->timer_event_source
, &old_timeout_time
);
999 if (old_timeout_time
<= timeout_time
)
1002 return sd_event_source_set_time(j
->timer_event_source
, timeout_time
);
1005 if (j
->timer_event_source
)
1008 j
->begin_usec
= now(CLOCK_MONOTONIC
);
1010 if (j
->unit
->job_timeout
== USEC_INFINITY
)
1013 timeout_time
= usec_add(j
->begin_usec
, j
->unit
->job_timeout
);
1016 r
= sd_event_add_time(
1018 &j
->timer_event_source
,
1021 job_dispatch_timer
, j
);
1025 (void) sd_event_source_set_description(j
->timer_event_source
, "job-start");
1030 void job_add_to_run_queue(Job
*j
) {
1032 assert(j
->installed
);
1034 if (j
->in_run_queue
)
1037 if (!j
->manager
->run_queue
)
1038 sd_event_source_set_enabled(j
->manager
->run_queue_event_source
, SD_EVENT_ONESHOT
);
1040 LIST_PREPEND(run_queue
, j
->manager
->run_queue
, j
);
1041 j
->in_run_queue
= true;
1044 void job_add_to_dbus_queue(Job
*j
) {
1046 assert(j
->installed
);
1048 if (j
->in_dbus_queue
)
1051 /* We don't check if anybody is subscribed here, since this
1052 * job might just have been created and not yet assigned to a
1053 * connection/client. */
1055 LIST_PREPEND(dbus_queue
, j
->manager
->dbus_job_queue
, j
);
1056 j
->in_dbus_queue
= true;
1059 char *job_dbus_path(Job
*j
) {
1064 if (asprintf(&p
, "/org/freedesktop/systemd1/job/%"PRIu32
, j
->id
) < 0)
1070 int job_serialize(Job
*j
, FILE *f
) {
1074 fprintf(f
, "job-id=%u\n", j
->id
);
1075 fprintf(f
, "job-type=%s\n", job_type_to_string(j
->type
));
1076 fprintf(f
, "job-state=%s\n", job_state_to_string(j
->state
));
1077 fprintf(f
, "job-irreversible=%s\n", yes_no(j
->irreversible
));
1078 fprintf(f
, "job-sent-dbus-new-signal=%s\n", yes_no(j
->sent_dbus_new_signal
));
1079 fprintf(f
, "job-ignore-order=%s\n", yes_no(j
->ignore_order
));
1081 if (j
->begin_usec
> 0)
1082 fprintf(f
, "job-begin="USEC_FMT
"\n", j
->begin_usec
);
1083 if (j
->begin_running_usec
> 0)
1084 fprintf(f
, "job-begin-running="USEC_FMT
"\n", j
->begin_running_usec
);
1086 bus_track_serialize(j
->bus_track
, f
, "subscribed");
1093 int job_deserialize(Job
*j
, FILE *f
) {
1098 char line
[LINE_MAX
], *l
, *v
;
1101 if (!fgets(line
, sizeof(line
), f
)) {
1114 k
= strcspn(l
, "=");
1122 if (streq(l
, "job-id")) {
1124 if (safe_atou32(v
, &j
->id
) < 0)
1125 log_debug("Failed to parse job id value %s", v
);
1127 } else if (streq(l
, "job-type")) {
1130 t
= job_type_from_string(v
);
1132 log_debug("Failed to parse job type %s", v
);
1133 else if (t
>= _JOB_TYPE_MAX_IN_TRANSACTION
)
1134 log_debug("Cannot deserialize job of type %s", v
);
1138 } else if (streq(l
, "job-state")) {
1141 s
= job_state_from_string(v
);
1143 log_debug("Failed to parse job state %s", v
);
1145 job_set_state(j
, s
);
1147 } else if (streq(l
, "job-irreversible")) {
1150 b
= parse_boolean(v
);
1152 log_debug("Failed to parse job irreversible flag %s", v
);
1154 j
->irreversible
= j
->irreversible
|| b
;
1156 } else if (streq(l
, "job-sent-dbus-new-signal")) {
1159 b
= parse_boolean(v
);
1161 log_debug("Failed to parse job sent_dbus_new_signal flag %s", v
);
1163 j
->sent_dbus_new_signal
= j
->sent_dbus_new_signal
|| b
;
1165 } else if (streq(l
, "job-ignore-order")) {
1168 b
= parse_boolean(v
);
1170 log_debug("Failed to parse job ignore_order flag %s", v
);
1172 j
->ignore_order
= j
->ignore_order
|| b
;
1174 } else if (streq(l
, "job-begin")) {
1175 unsigned long long ull
;
1177 if (sscanf(v
, "%llu", &ull
) != 1)
1178 log_debug("Failed to parse job-begin value %s", v
);
1180 j
->begin_usec
= ull
;
1182 } else if (streq(l
, "job-begin-running")) {
1183 unsigned long long ull
;
1185 if (sscanf(v
, "%llu", &ull
) != 1)
1186 log_debug("Failed to parse job-begin-running value %s", v
);
1188 j
->begin_running_usec
= ull
;
1190 } else if (streq(l
, "subscribed")) {
1192 if (strv_extend(&j
->deserialized_clients
, v
) < 0)
1198 int job_coldplug(Job
*j
) {
1200 usec_t timeout_time
= USEC_INFINITY
;
1204 /* After deserialization is complete and the bus connection
1205 * set up again, let's start watching our subscribers again */
1206 (void) bus_job_coldplug_bus_track(j
);
1208 if (j
->state
== JOB_WAITING
)
1209 job_add_to_run_queue(j
);
1211 /* Maybe due to new dependencies we don't actually need this job anymore? */
1212 job_add_to_gc_queue(j
);
1214 /* Create timer only when job began or began running and the respective timeout is finite.
1215 * Follow logic of job_start_timer() if both timeouts are finite */
1216 if (j
->begin_usec
== 0)
1219 if (j
->unit
->job_timeout
!= USEC_INFINITY
)
1220 timeout_time
= usec_add(j
->begin_usec
, j
->unit
->job_timeout
);
1222 if (j
->begin_running_usec
> 0 && j
->unit
->job_running_timeout
!= USEC_INFINITY
)
1223 timeout_time
= MIN(timeout_time
, usec_add(j
->begin_running_usec
, j
->unit
->job_running_timeout
));
1225 if (timeout_time
== USEC_INFINITY
)
1228 j
->timer_event_source
= sd_event_source_unref(j
->timer_event_source
);
1230 r
= sd_event_add_time(
1232 &j
->timer_event_source
,
1235 job_dispatch_timer
, j
);
1237 log_debug_errno(r
, "Failed to restart timeout for job: %m");
1239 (void) sd_event_source_set_description(j
->timer_event_source
, "job-timeout");
1244 void job_shutdown_magic(Job
*j
) {
1247 /* The shutdown target gets some special treatment here: we
1248 * tell the kernel to begin with flushing its disk caches, to
1249 * optimize shutdown time a bit. Ideally we wouldn't hardcode
1250 * this magic into PID 1. However all other processes aren't
1251 * options either since they'd exit much sooner than PID 1 and
1252 * asynchronous sync() would cause their exit to be
1255 if (j
->type
!= JOB_START
)
1258 if (!MANAGER_IS_SYSTEM(j
->unit
->manager
))
1261 if (!unit_has_name(j
->unit
, SPECIAL_SHUTDOWN_TARGET
))
1264 /* In case messages on console has been disabled on boot */
1265 j
->unit
->manager
->no_console_output
= false;
1267 if (detect_container() > 0)
1270 (void) asynchronous_sync(NULL
);
1273 int job_get_timeout(Job
*j
, usec_t
*timeout
) {
1274 usec_t x
= USEC_INFINITY
, y
= USEC_INFINITY
;
1280 if (j
->timer_event_source
) {
1281 r
= sd_event_source_get_time(j
->timer_event_source
, &x
);
1286 if (UNIT_VTABLE(u
)->get_timeout
) {
1287 r
= UNIT_VTABLE(u
)->get_timeout(u
, &y
);
1292 if (x
== USEC_INFINITY
&& y
== USEC_INFINITY
)
1295 *timeout
= MIN(x
, y
);
1299 bool job_may_gc(Job
*j
) {
1306 /* Checks whether this job should be GC'ed away. We only do this for jobs of units that have no effect on their
1307 * own and just track external state. For now the only unit type that qualifies for this are .device units.
1308 * Returns true if the job can be collected. */
1310 if (!UNIT_VTABLE(j
->unit
)->gc_jobs
)
1313 if (sd_bus_track_count(j
->bus_track
) > 0)
1316 /* FIXME: So this is a bit ugly: for now we don't properly track references made via private bus connections
1317 * (because it's nasty, as sd_bus_track doesn't apply to it). We simply remember that the job was once
1318 * referenced by one, and reset this whenever we notice that no private bus connections are around. This means
1319 * the GC is a bit too conservative when it comes to jobs created by private bus connections. */
1320 if (j
->ref_by_private_bus
) {
1321 if (set_isempty(j
->unit
->manager
->private_buses
))
1322 j
->ref_by_private_bus
= false;
1327 if (j
->type
== JOB_NOP
)
1330 /* If a job is ordered after ours, and is to be started, then it needs to wait for us, regardless if we stop or
1331 * start, hence let's not GC in that case. */
1332 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_BEFORE
], i
) {
1336 if (other
->job
->ignore_order
)
1339 if (IN_SET(other
->job
->type
, JOB_START
, JOB_VERIFY_ACTIVE
, JOB_RELOAD
))
1343 /* If we are going down, but something else is ordered After= us, then it needs to wait for us */
1344 if (IN_SET(j
->type
, JOB_STOP
, JOB_RESTART
))
1345 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_AFTER
], i
) {
1349 if (other
->job
->ignore_order
)
1355 /* The logic above is kinda the inverse of the job_is_runnable() logic. Specifically, if the job "we" is
1356 * ordered before the job "other":
1358 * we start + other start → stay
1359 * we start + other stop → gc
1360 * we stop + other start → stay
1361 * we stop + other stop → gc
1363 * "we" are ordered after "other":
1365 * we start + other start → gc
1366 * we start + other stop → gc
1367 * we stop + other start → stay
1368 * we stop + other stop → stay
1375 void job_add_to_gc_queue(Job
*j
) {
1384 LIST_PREPEND(gc_queue
, j
->unit
->manager
->gc_job_queue
, j
);
1385 j
->in_gc_queue
= true;
1388 static int job_compare(const void *a
, const void *b
) {
1389 Job
*x
= *(Job
**) a
, *y
= *(Job
**) b
;
1399 static size_t sort_job_list(Job
**list
, size_t n
) {
1400 Job
*previous
= NULL
;
1403 /* Order by numeric IDs */
1404 qsort_safe(list
, n
, sizeof(Job
*), job_compare
);
1406 /* Filter out duplicates */
1407 for (a
= 0, b
= 0; a
< n
; a
++) {
1409 if (previous
== list
[a
])
1412 previous
= list
[b
++] = list
[a
];
1418 int job_get_before(Job
*j
, Job
*** ret
) {
1419 _cleanup_free_ Job
** list
= NULL
;
1420 size_t n
= 0, n_allocated
= 0;
1425 /* Returns a list of all pending jobs that need to finish before this job may be started. */
1430 if (j
->ignore_order
) {
1435 if (IN_SET(j
->type
, JOB_START
, JOB_VERIFY_ACTIVE
, JOB_RELOAD
)) {
1437 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_AFTER
], i
) {
1441 if (!GREEDY_REALLOC(list
, n_allocated
, n
+1))
1443 list
[n
++] = other
->job
;
1447 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_BEFORE
], i
) {
1451 if (!IN_SET(other
->job
->type
, JOB_STOP
, JOB_RESTART
))
1454 if (!GREEDY_REALLOC(list
, n_allocated
, n
+1))
1456 list
[n
++] = other
->job
;
1459 n
= sort_job_list(list
, n
);
1461 *ret
= TAKE_PTR(list
);
1466 int job_get_after(Job
*j
, Job
*** ret
) {
1467 _cleanup_free_ Job
** list
= NULL
;
1468 size_t n
= 0, n_allocated
= 0;
1476 /* Returns a list of all pending jobs that are waiting for this job to finish. */
1478 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_BEFORE
], i
) {
1482 if (other
->job
->ignore_order
)
1485 if (!IN_SET(other
->job
->type
, JOB_START
, JOB_VERIFY_ACTIVE
, JOB_RELOAD
))
1488 if (!GREEDY_REALLOC(list
, n_allocated
, n
+1))
1490 list
[n
++] = other
->job
;
1493 if (IN_SET(j
->type
, JOB_STOP
, JOB_RESTART
)) {
1495 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_AFTER
], i
) {
1499 if (other
->job
->ignore_order
)
1502 if (!GREEDY_REALLOC(list
, n_allocated
, n
+1))
1504 list
[n
++] = other
->job
;
1508 n
= sort_job_list(list
, n
);
1510 *ret
= TAKE_PTR(list
);
1515 static const char* const job_state_table
[_JOB_STATE_MAX
] = {
1516 [JOB_WAITING
] = "waiting",
1517 [JOB_RUNNING
] = "running",
1520 DEFINE_STRING_TABLE_LOOKUP(job_state
, JobState
);
1522 static const char* const job_type_table
[_JOB_TYPE_MAX
] = {
1523 [JOB_START
] = "start",
1524 [JOB_VERIFY_ACTIVE
] = "verify-active",
1525 [JOB_STOP
] = "stop",
1526 [JOB_RELOAD
] = "reload",
1527 [JOB_RELOAD_OR_START
] = "reload-or-start",
1528 [JOB_RESTART
] = "restart",
1529 [JOB_TRY_RESTART
] = "try-restart",
1530 [JOB_TRY_RELOAD
] = "try-reload",
1534 DEFINE_STRING_TABLE_LOOKUP(job_type
, JobType
);
1536 static const char* const job_mode_table
[_JOB_MODE_MAX
] = {
1537 [JOB_FAIL
] = "fail",
1538 [JOB_REPLACE
] = "replace",
1539 [JOB_REPLACE_IRREVERSIBLY
] = "replace-irreversibly",
1540 [JOB_ISOLATE
] = "isolate",
1541 [JOB_FLUSH
] = "flush",
1542 [JOB_IGNORE_DEPENDENCIES
] = "ignore-dependencies",
1543 [JOB_IGNORE_REQUIREMENTS
] = "ignore-requirements",
1546 DEFINE_STRING_TABLE_LOOKUP(job_mode
, JobMode
);
1548 static const char* const job_result_table
[_JOB_RESULT_MAX
] = {
1549 [JOB_DONE
] = "done",
1550 [JOB_CANCELED
] = "canceled",
1551 [JOB_TIMEOUT
] = "timeout",
1552 [JOB_FAILED
] = "failed",
1553 [JOB_DEPENDENCY
] = "dependency",
1554 [JOB_SKIPPED
] = "skipped",
1555 [JOB_INVALID
] = "invalid",
1556 [JOB_ASSERT
] = "assert",
1557 [JOB_UNSUPPORTED
] = "unsupported",
1558 [JOB_COLLECTED
] = "collected",
1559 [JOB_ONCE
] = "once",
1562 DEFINE_STRING_TABLE_LOOKUP(job_result
, JobResult
);
1564 const char* job_type_to_access_method(JobType t
) {
1566 assert(t
< _JOB_TYPE_MAX
);
1568 if (IN_SET(t
, JOB_START
, JOB_RESTART
, JOB_TRY_RESTART
))
1570 else if (t
== JOB_STOP
)