1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "sd-messages.h"
8 #include "alloc-util.h"
17 #include "parse-util.h"
18 #include "serialize.h"
20 #include "sort-util.h"
22 #include "stdio-util.h"
23 #include "string-table.h"
24 #include "string-util.h"
26 #include "terminal-util.h"
30 Job
* job_new_raw(Unit
*unit
) {
33 /* used for deserialization */
42 .manager
= unit
->manager
,
44 .type
= _JOB_TYPE_INVALID
,
50 Job
* job_new(Unit
*unit
, JobType type
) {
53 assert(type
< _JOB_TYPE_MAX
);
55 j
= job_new_raw(unit
);
59 j
->id
= j
->manager
->current_job_id
++;
62 /* We don't link it here, that's what job_dependency() is for */
67 void job_unlink(Job
*j
) {
69 assert(!j
->installed
);
70 assert(!j
->transaction_prev
);
71 assert(!j
->transaction_next
);
72 assert(!j
->subject_list
);
73 assert(!j
->object_list
);
75 if (j
->in_run_queue
) {
76 LIST_REMOVE(run_queue
, j
->manager
->run_queue
, j
);
77 j
->in_run_queue
= false;
80 if (j
->in_dbus_queue
) {
81 LIST_REMOVE(dbus_queue
, j
->manager
->dbus_job_queue
, j
);
82 j
->in_dbus_queue
= false;
86 LIST_REMOVE(gc_queue
, j
->manager
->gc_job_queue
, j
);
87 j
->in_gc_queue
= false;
90 j
->timer_event_source
= sd_event_source_unref(j
->timer_event_source
);
93 Job
* job_free(Job
*j
) {
95 assert(!j
->installed
);
96 assert(!j
->transaction_prev
);
97 assert(!j
->transaction_next
);
98 assert(!j
->subject_list
);
99 assert(!j
->object_list
);
103 sd_bus_track_unref(j
->bus_track
);
104 strv_free(j
->deserialized_clients
);
109 static void job_set_state(Job
*j
, JobState state
) {
112 assert(state
< _JOB_STATE_MAX
);
114 if (j
->state
== state
)
122 if (j
->state
== JOB_RUNNING
)
123 j
->unit
->manager
->n_running_jobs
++;
125 assert(j
->state
== JOB_WAITING
);
126 assert(j
->unit
->manager
->n_running_jobs
> 0);
128 j
->unit
->manager
->n_running_jobs
--;
130 if (j
->unit
->manager
->n_running_jobs
<= 0)
131 j
->unit
->manager
->jobs_in_progress_event_source
= sd_event_source_unref(j
->unit
->manager
->jobs_in_progress_event_source
);
135 void job_uninstall(Job
*j
) {
138 assert(j
->installed
);
140 job_set_state(j
, JOB_WAITING
);
142 pj
= (j
->type
== JOB_NOP
) ? &j
->unit
->nop_job
: &j
->unit
->job
;
145 /* Detach from next 'bigger' objects */
147 /* daemon-reload should be transparent to job observers */
148 if (!MANAGER_IS_RELOADING(j
->manager
))
149 bus_job_send_removed_signal(j
);
153 unit_add_to_gc_queue(j
->unit
);
155 unit_add_to_dbus_queue(j
->unit
); /* The Job property of the unit has changed now */
157 hashmap_remove_value(j
->manager
->jobs
, UINT32_TO_PTR(j
->id
), j
);
158 j
->installed
= false;
161 static bool job_type_allows_late_merge(JobType t
) {
162 /* Tells whether it is OK to merge a job of type 't' with an already
164 * Reloads cannot be merged this way. Think of the sequence:
165 * 1. Reload of a daemon is in progress; the daemon has already loaded
166 * its config file, but hasn't completed the reload operation yet.
167 * 2. Edit foo's config file.
168 * 3. Trigger another reload to have the daemon use the new config.
169 * Should the second reload job be merged into the first one, the daemon
170 * would not know about the new config.
171 * JOB_RESTART jobs on the other hand can be merged, because they get
172 * patched into JOB_START after stopping the unit. So if we see a
173 * JOB_RESTART running, it means the unit hasn't stopped yet and at
174 * this time the merge is still allowed. */
175 return t
!= JOB_RELOAD
;
178 static void job_merge_into_installed(Job
*j
, Job
*other
) {
179 assert(j
->installed
);
180 assert(j
->unit
== other
->unit
);
182 if (j
->type
!= JOB_NOP
)
183 assert_se(job_type_merge_and_collapse(&j
->type
, other
->type
, j
->unit
) == 0);
185 assert(other
->type
== JOB_NOP
);
187 j
->irreversible
= j
->irreversible
|| other
->irreversible
;
188 j
->ignore_order
= j
->ignore_order
|| other
->ignore_order
;
191 Job
* job_install(Job
*j
) {
195 assert(!j
->installed
);
196 assert(j
->type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
197 assert(j
->state
== JOB_WAITING
);
199 pj
= (j
->type
== JOB_NOP
) ? &j
->unit
->nop_job
: &j
->unit
->job
;
203 if (job_type_is_conflicting(uj
->type
, j
->type
))
204 job_finish_and_invalidate(uj
, JOB_CANCELED
, false, false);
206 /* not conflicting, i.e. mergeable */
208 if (uj
->state
== JOB_WAITING
||
209 (job_type_allows_late_merge(j
->type
) && job_type_is_superset(uj
->type
, j
->type
))) {
210 job_merge_into_installed(uj
, j
);
211 log_unit_debug(uj
->unit
,
212 "Merged %s/%s into installed job %s/%s as %"PRIu32
,
213 j
->unit
->id
, job_type_to_string(j
->type
), uj
->unit
->id
,
214 job_type_to_string(uj
->type
), uj
->id
);
217 /* already running and not safe to merge into */
218 /* Patch uj to become a merged job and re-run it. */
219 /* XXX It should be safer to queue j to run after uj finishes, but it is
220 * not currently possible to have more than one installed job per unit. */
221 job_merge_into_installed(uj
, j
);
222 log_unit_debug(uj
->unit
,
223 "Merged into running job, re-running: %s/%s as %"PRIu32
,
224 uj
->unit
->id
, job_type_to_string(uj
->type
), uj
->id
);
226 job_set_state(uj
, JOB_WAITING
);
232 /* Install the job */
236 j
->manager
->n_installed_jobs
++;
237 log_unit_debug(j
->unit
,
238 "Installed new job %s/%s as %u",
239 j
->unit
->id
, job_type_to_string(j
->type
), (unsigned) j
->id
);
241 job_add_to_gc_queue(j
);
243 job_add_to_dbus_queue(j
); /* announce this job to clients */
244 unit_add_to_dbus_queue(j
->unit
); /* The Job property of the unit has changed now */
249 int job_install_deserialized(Job
*j
) {
253 assert(!j
->installed
);
255 if (j
->type
< 0 || j
->type
>= _JOB_TYPE_MAX_IN_TRANSACTION
)
256 return log_unit_debug_errno(j
->unit
, SYNTHETIC_ERRNO(EINVAL
),
257 "Invalid job type %s in deserialization.",
258 strna(job_type_to_string(j
->type
)));
260 pj
= (j
->type
== JOB_NOP
) ? &j
->unit
->nop_job
: &j
->unit
->job
;
262 return log_unit_debug_errno(j
->unit
, SYNTHETIC_ERRNO(EEXIST
),
263 "Unit already has a job installed. Not installing deserialized job.");
265 r
= hashmap_put(j
->manager
->jobs
, UINT32_TO_PTR(j
->id
), j
);
267 return log_unit_debug_errno(j
->unit
, r
, "Job ID %" PRIu32
" already used, cannot deserialize job.", j
->id
);
269 return log_unit_debug_errno(j
->unit
, r
, "Failed to insert job into jobs hash table: %m");
274 if (j
->state
== JOB_RUNNING
)
275 j
->unit
->manager
->n_running_jobs
++;
277 log_unit_debug(j
->unit
,
278 "Reinstalled deserialized job %s/%s as %u",
279 j
->unit
->id
, job_type_to_string(j
->type
), (unsigned) j
->id
);
283 JobDependency
* job_dependency_new(Job
*subject
, Job
*object
, bool matters
, bool conflicts
) {
288 /* Adds a new job link, which encodes that the 'subject' job
289 * needs the 'object' job in some way. If 'subject' is NULL
290 * this means the 'anchor' job (i.e. the one the user
291 * explicitly asked for) is the requester. */
293 l
= new0(JobDependency
, 1);
297 l
->subject
= subject
;
299 l
->matters
= matters
;
300 l
->conflicts
= conflicts
;
303 LIST_PREPEND(subject
, subject
->subject_list
, l
);
305 LIST_PREPEND(object
, object
->object_list
, l
);
310 void job_dependency_free(JobDependency
*l
) {
314 LIST_REMOVE(subject
, l
->subject
->subject_list
, l
);
316 LIST_REMOVE(object
, l
->object
->object_list
, l
);
321 void job_dump(Job
*j
, FILE *f
, const char *prefix
) {
325 prefix
= strempty(prefix
);
329 "%s\tAction: %s -> %s\n"
331 "%s\tIrreversible: %s\n"
334 prefix
, j
->unit
->id
, job_type_to_string(j
->type
),
335 prefix
, job_state_to_string(j
->state
),
336 prefix
, yes_no(j
->irreversible
),
337 prefix
, yes_no(job_may_gc(j
)));
341 * Merging is commutative, so imagine the matrix as symmetric. We store only
342 * its lower triangle to avoid duplication. We don't store the main diagonal,
343 * because A merged with A is simply A.
345 * If the resulting type is collapsed immediately afterwards (to get rid of
346 * the JOB_RELOAD_OR_START, which lies outside the lookup function's domain),
347 * the following properties hold:
349 * Merging is associative! A merged with B, and then merged with C is the same
350 * as A merged with the result of B merged with C.
352 * Mergeability is transitive! If A can be merged with B and B with C then
355 * Also, if A merged with B cannot be merged with C, then either A or B cannot
356 * be merged with C either.
358 static const JobType job_merging_table
[] = {
359 /* What \ With * JOB_START JOB_VERIFY_ACTIVE JOB_STOP JOB_RELOAD */
360 /*********************************************************************************/
362 /*JOB_VERIFY_ACTIVE */ JOB_START
,
363 /*JOB_STOP */ -1, -1,
364 /*JOB_RELOAD */ JOB_RELOAD_OR_START
, JOB_RELOAD
, -1,
365 /*JOB_RESTART */ JOB_RESTART
, JOB_RESTART
, -1, JOB_RESTART
,
368 JobType
job_type_lookup_merge(JobType a
, JobType b
) {
369 assert_cc(ELEMENTSOF(job_merging_table
) == _JOB_TYPE_MAX_MERGING
* (_JOB_TYPE_MAX_MERGING
- 1) / 2);
370 assert(a
>= 0 && a
< _JOB_TYPE_MAX_MERGING
);
371 assert(b
>= 0 && b
< _JOB_TYPE_MAX_MERGING
);
382 return job_merging_table
[(a
- 1) * a
/ 2 + b
];
385 bool job_type_is_redundant(JobType a
, UnitActiveState b
) {
389 return IN_SET(b
, UNIT_ACTIVE
, UNIT_RELOADING
);
392 return IN_SET(b
, UNIT_INACTIVE
, UNIT_FAILED
);
394 case JOB_VERIFY_ACTIVE
:
395 return IN_SET(b
, UNIT_ACTIVE
, UNIT_RELOADING
);
403 b
== UNIT_ACTIVATING
;
409 assert_not_reached("Invalid job type");
413 JobType
job_type_collapse(JobType t
, Unit
*u
) {
418 case JOB_TRY_RESTART
:
419 s
= unit_active_state(u
);
420 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s
))
426 s
= unit_active_state(u
);
427 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s
))
432 case JOB_RELOAD_OR_START
:
433 s
= unit_active_state(u
);
434 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s
))
444 int job_type_merge_and_collapse(JobType
*a
, JobType b
, Unit
*u
) {
447 t
= job_type_lookup_merge(*a
, b
);
451 *a
= job_type_collapse(t
, u
);
455 static bool job_is_runnable(Job
*j
) {
461 assert(j
->installed
);
463 /* Checks whether there is any job running for the units this
464 * job needs to be running after (in the case of a 'positive'
465 * job type) or before (in the case of a 'negative' job
468 /* Note that unit types have a say in what is runnable,
469 * too. For example, if they return -EAGAIN from
470 * unit_start() they can indicate they are not
473 /* First check if there is an override */
477 if (j
->type
== JOB_NOP
)
480 if (IN_SET(j
->type
, JOB_START
, JOB_VERIFY_ACTIVE
, JOB_RELOAD
)) {
481 /* Immediate result is that the job is or might be
482 * started. In this case let's wait for the
483 * dependencies, regardless whether they are
484 * starting or stopping something. */
486 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_AFTER
], i
)
491 /* Also, if something else is being stopped and we should
492 * change state after it, then let's wait. */
494 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_BEFORE
], i
)
496 IN_SET(other
->job
->type
, JOB_STOP
, JOB_RESTART
))
499 /* This means that for a service a and a service b where b
500 * shall be started after a:
502 * start a + start b → 1st step start a, 2nd step start b
503 * start a + stop b → 1st step stop b, 2nd step start a
504 * stop a + start b → 1st step stop a, 2nd step start b
505 * stop a + stop b → 1st step stop b, 2nd step stop a
507 * This has the side effect that restarts are properly
508 * synchronized too. */
513 static void job_change_type(Job
*j
, JobType newtype
) {
516 log_unit_debug(j
->unit
,
517 "Converting job %s/%s -> %s/%s",
518 j
->unit
->id
, job_type_to_string(j
->type
),
519 j
->unit
->id
, job_type_to_string(newtype
));
524 _pure_
static const char* job_get_begin_status_message_format(Unit
*u
, JobType t
) {
530 return "Reloading %s.";
532 assert(IN_SET(t
, JOB_START
, JOB_STOP
));
534 format
= UNIT_VTABLE(u
)->status_message_formats
.starting_stopping
[t
== JOB_STOP
];
538 /* Return generic strings */
540 return "Starting %s.";
542 assert(t
== JOB_STOP
);
543 return "Stopping %s.";
547 static void job_print_begin_status_message(Unit
*u
, JobType t
) {
552 /* Reload status messages have traditionally not been printed to console. */
553 if (!IN_SET(t
, JOB_START
, JOB_STOP
))
556 format
= job_get_begin_status_message_format(u
, t
);
558 DISABLE_WARNING_FORMAT_NONLITERAL
;
559 unit_status_printf(u
, "", format
);
563 static void job_log_begin_status_message(Unit
*u
, uint32_t job_id
, JobType t
) {
564 const char *format
, *mid
;
569 assert(t
< _JOB_TYPE_MAX
);
571 if (!IN_SET(t
, JOB_START
, JOB_STOP
, JOB_RELOAD
))
574 if (log_on_console()) /* Skip this if it would only go on the console anyway */
577 /* We log status messages for all units and all operations. */
579 format
= job_get_begin_status_message_format(u
, t
);
581 DISABLE_WARNING_FORMAT_NONLITERAL
;
582 (void) snprintf(buf
, sizeof buf
, format
, unit_description(u
));
585 mid
= t
== JOB_START
? "MESSAGE_ID=" SD_MESSAGE_UNIT_STARTING_STR
:
586 t
== JOB_STOP
? "MESSAGE_ID=" SD_MESSAGE_UNIT_STOPPING_STR
:
587 "MESSAGE_ID=" SD_MESSAGE_UNIT_RELOADING_STR
;
589 /* Note that we deliberately use LOG_MESSAGE() instead of
590 * LOG_UNIT_MESSAGE() here, since this is supposed to mimic
591 * closely what is written to screen using the status output,
592 * which is supposed the highest level, friendliest output
593 * possible, which means we should avoid the low-level unit
596 LOG_MESSAGE("%s", buf
),
597 "JOB_ID=%" PRIu32
, job_id
,
598 "JOB_TYPE=%s", job_type_to_string(t
),
600 LOG_UNIT_INVOCATION_ID(u
),
604 static void job_emit_begin_status_message(Unit
*u
, uint32_t job_id
, JobType t
) {
607 assert(t
< _JOB_TYPE_MAX
);
609 job_log_begin_status_message(u
, job_id
, t
);
610 job_print_begin_status_message(u
, t
);
613 static int job_perform_on_unit(Job
**j
) {
620 /* While we execute this operation the job might go away (for
621 * example: because it finishes immediately or is replaced by
622 * a new, conflicting job.) To make sure we don't access a
623 * freed job later on we store the id here, so that we can
624 * verify the job is still valid. */
651 assert_not_reached("Invalid job type");
654 /* Log if the job still exists and the start/stop/reload function actually did something. Note that this means
655 * for units for which there's no 'activating' phase (i.e. because we transition directly from 'inactive' to
656 * 'active') we'll possibly skip the "Starting..." message. */
657 *j
= manager_get_job(m
, id
);
659 job_emit_begin_status_message(u
, id
, t
);
664 int job_run_and_invalidate(Job
*j
) {
668 assert(j
->installed
);
669 assert(j
->type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
670 assert(j
->in_run_queue
);
672 LIST_REMOVE(run_queue
, j
->manager
->run_queue
, j
);
673 j
->in_run_queue
= false;
675 if (j
->state
!= JOB_WAITING
)
678 if (!job_is_runnable(j
))
681 job_start_timer(j
, true);
682 job_set_state(j
, JOB_RUNNING
);
683 job_add_to_dbus_queue(j
);
687 case JOB_VERIFY_ACTIVE
: {
690 t
= unit_active_state(j
->unit
);
691 if (UNIT_IS_ACTIVE_OR_RELOADING(t
))
693 else if (t
== UNIT_ACTIVATING
)
703 r
= job_perform_on_unit(&j
);
705 /* If the unit type does not support starting/stopping, then simply wait. */
711 r
= job_perform_on_unit(&j
);
719 assert_not_reached("Unknown job type");
724 job_set_state(j
, JOB_WAITING
); /* Hmm, not ready after all, let's return to JOB_WAITING state */
725 else if (r
== -EALREADY
) /* already being executed */
726 r
= job_finish_and_invalidate(j
, JOB_DONE
, true, true);
727 else if (r
== -ECOMM
) /* condition failed, but all is good */
728 r
= job_finish_and_invalidate(j
, JOB_DONE
, true, false);
729 else if (r
== -EBADR
)
730 r
= job_finish_and_invalidate(j
, JOB_SKIPPED
, true, false);
731 else if (r
== -ENOEXEC
)
732 r
= job_finish_and_invalidate(j
, JOB_INVALID
, true, false);
733 else if (r
== -EPROTO
)
734 r
= job_finish_and_invalidate(j
, JOB_ASSERT
, true, false);
735 else if (r
== -EOPNOTSUPP
)
736 r
= job_finish_and_invalidate(j
, JOB_UNSUPPORTED
, true, false);
737 else if (r
== -ENOLINK
)
738 r
= job_finish_and_invalidate(j
, JOB_DEPENDENCY
, true, false);
739 else if (r
== -ESTALE
)
740 r
= job_finish_and_invalidate(j
, JOB_ONCE
, true, false);
742 r
= job_finish_and_invalidate(j
, JOB_FAILED
, true, false);
748 _pure_
static const char *job_get_done_status_message_format(Unit
*u
, JobType t
, JobResult result
) {
750 static const char *const generic_finished_start_job
[_JOB_RESULT_MAX
] = {
751 [JOB_DONE
] = "Started %s.",
752 [JOB_TIMEOUT
] = "Timed out starting %s.",
753 [JOB_FAILED
] = "Failed to start %s.",
754 [JOB_DEPENDENCY
] = "Dependency failed for %s.",
755 [JOB_ASSERT
] = "Assertion failed for %s.",
756 [JOB_UNSUPPORTED
] = "Starting of %s not supported.",
757 [JOB_COLLECTED
] = "Unnecessary job for %s was removed.",
758 [JOB_ONCE
] = "Unit %s has been started before and cannot be started again."
760 static const char *const generic_finished_stop_job
[_JOB_RESULT_MAX
] = {
761 [JOB_DONE
] = "Stopped %s.",
762 [JOB_FAILED
] = "Stopped (with error) %s.",
763 [JOB_TIMEOUT
] = "Timed out stopping %s.",
765 static const char *const generic_finished_reload_job
[_JOB_RESULT_MAX
] = {
766 [JOB_DONE
] = "Reloaded %s.",
767 [JOB_FAILED
] = "Reload failed for %s.",
768 [JOB_TIMEOUT
] = "Timed out reloading %s.",
770 /* When verify-active detects the unit is inactive, report it.
771 * Most likely a DEPEND warning from a requisiting unit will
772 * occur next and it's nice to see what was requisited. */
773 static const char *const generic_finished_verify_active_job
[_JOB_RESULT_MAX
] = {
774 [JOB_SKIPPED
] = "%s is not active.",
781 assert(t
< _JOB_TYPE_MAX
);
783 if (IN_SET(t
, JOB_START
, JOB_STOP
, JOB_RESTART
)) {
784 format
= t
== JOB_START
?
785 UNIT_VTABLE(u
)->status_message_formats
.finished_start_job
[result
] :
786 UNIT_VTABLE(u
)->status_message_formats
.finished_stop_job
[result
];
791 /* Return generic strings */
793 return generic_finished_start_job
[result
];
794 else if (IN_SET(t
, JOB_STOP
, JOB_RESTART
))
795 return generic_finished_stop_job
[result
];
796 else if (t
== JOB_RELOAD
)
797 return generic_finished_reload_job
[result
];
798 else if (t
== JOB_VERIFY_ACTIVE
)
799 return generic_finished_verify_active_job
[result
];
804 static const struct {
805 const char *color
, *word
;
806 } job_print_done_status_messages
[_JOB_RESULT_MAX
] = {
807 [JOB_DONE
] = { ANSI_OK_COLOR
, " OK " },
808 [JOB_TIMEOUT
] = { ANSI_HIGHLIGHT_RED
, " TIME " },
809 [JOB_FAILED
] = { ANSI_HIGHLIGHT_RED
, "FAILED" },
810 [JOB_DEPENDENCY
] = { ANSI_HIGHLIGHT_YELLOW
, "DEPEND" },
811 [JOB_SKIPPED
] = { ANSI_HIGHLIGHT
, " INFO " },
812 [JOB_ASSERT
] = { ANSI_HIGHLIGHT_YELLOW
, "ASSERT" },
813 [JOB_UNSUPPORTED
] = { ANSI_HIGHLIGHT_YELLOW
, "UNSUPP" },
815 [JOB_ONCE
] = { ANSI_HIGHLIGHT_RED
, " ONCE " },
818 static void job_print_done_status_message(Unit
*u
, JobType t
, JobResult result
) {
824 assert(t
< _JOB_TYPE_MAX
);
826 /* Reload status messages have traditionally not been printed to console. */
830 /* No message if the job did not actually do anything due to failed condition. */
831 if (t
== JOB_START
&& result
== JOB_DONE
&& !u
->condition_result
)
834 if (!job_print_done_status_messages
[result
].word
)
837 format
= job_get_done_status_message_format(u
, t
, result
);
841 if (log_get_show_color())
842 status
= strjoina(job_print_done_status_messages
[result
].color
,
843 job_print_done_status_messages
[result
].word
,
846 status
= job_print_done_status_messages
[result
].word
;
848 if (result
!= JOB_DONE
)
849 manager_flip_auto_status(u
->manager
, true);
851 DISABLE_WARNING_FORMAT_NONLITERAL
;
852 unit_status_printf(u
, status
, format
);
855 if (t
== JOB_START
&& result
== JOB_FAILED
) {
856 _cleanup_free_
char *quoted
;
858 quoted
= shell_maybe_quote(u
->id
, ESCAPE_BACKSLASH
);
859 manager_status_printf(u
->manager
, STATUS_TYPE_NORMAL
, NULL
, "See 'systemctl status %s' for details.", strna(quoted
));
863 static void job_log_done_status_message(Unit
*u
, uint32_t job_id
, JobType t
, JobResult result
) {
864 const char *format
, *mid
;
866 static const int job_result_log_level
[_JOB_RESULT_MAX
] = {
867 [JOB_DONE
] = LOG_INFO
,
868 [JOB_CANCELED
] = LOG_INFO
,
869 [JOB_TIMEOUT
] = LOG_ERR
,
870 [JOB_FAILED
] = LOG_ERR
,
871 [JOB_DEPENDENCY
] = LOG_WARNING
,
872 [JOB_SKIPPED
] = LOG_NOTICE
,
873 [JOB_INVALID
] = LOG_INFO
,
874 [JOB_ASSERT
] = LOG_WARNING
,
875 [JOB_UNSUPPORTED
] = LOG_WARNING
,
876 [JOB_COLLECTED
] = LOG_INFO
,
877 [JOB_ONCE
] = LOG_ERR
,
882 assert(t
< _JOB_TYPE_MAX
);
884 /* Skip printing if output goes to the console, and job_print_status_message()
885 will actually print something to the console. */
886 if (log_on_console() && job_print_done_status_messages
[result
].word
)
889 /* Show condition check message if the job did not actually do anything due to failed condition. */
890 if (t
== JOB_START
&& result
== JOB_DONE
&& !u
->condition_result
) {
892 "MESSAGE=Condition check resulted in %s being skipped.", unit_description(u
),
893 "JOB_ID=%" PRIu32
, job_id
,
894 "JOB_TYPE=%s", job_type_to_string(t
),
895 "JOB_RESULT=%s", job_result_to_string(result
),
897 LOG_UNIT_INVOCATION_ID(u
),
898 "MESSAGE_ID=" SD_MESSAGE_UNIT_STARTED_STR
);
903 format
= job_get_done_status_message_format(u
, t
, result
);
907 /* The description might be longer than the buffer, but that's OK,
908 * we'll just truncate it here. Note that we use snprintf() rather than
909 * xsprintf() on purpose here: we are fine with truncation and don't
910 * consider that an error. */
911 DISABLE_WARNING_FORMAT_NONLITERAL
;
912 (void) snprintf(buf
, sizeof(buf
), format
, unit_description(u
));
918 if (result
== JOB_DONE
)
919 mid
= "MESSAGE_ID=" SD_MESSAGE_UNIT_STARTED_STR
;
921 mid
= "MESSAGE_ID=" SD_MESSAGE_UNIT_FAILED_STR
;
925 mid
= "MESSAGE_ID=" SD_MESSAGE_UNIT_RELOADED_STR
;
930 mid
= "MESSAGE_ID=" SD_MESSAGE_UNIT_STOPPED_STR
;
934 log_struct(job_result_log_level
[result
],
935 LOG_MESSAGE("%s", buf
),
936 "JOB_ID=%" PRIu32
, job_id
,
937 "JOB_TYPE=%s", job_type_to_string(t
),
938 "JOB_RESULT=%s", job_result_to_string(result
),
940 LOG_UNIT_INVOCATION_ID(u
));
944 log_struct(job_result_log_level
[result
],
945 LOG_MESSAGE("%s", buf
),
946 "JOB_ID=%" PRIu32
, job_id
,
947 "JOB_TYPE=%s", job_type_to_string(t
),
948 "JOB_RESULT=%s", job_result_to_string(result
),
950 LOG_UNIT_INVOCATION_ID(u
),
954 static void job_emit_done_status_message(Unit
*u
, uint32_t job_id
, JobType t
, JobResult result
) {
957 job_log_done_status_message(u
, job_id
, t
, result
);
958 job_print_done_status_message(u
, t
, result
);
961 static void job_fail_dependencies(Unit
*u
, UnitDependency d
) {
968 HASHMAP_FOREACH_KEY(v
, other
, u
->dependencies
[d
], i
) {
973 if (!IN_SET(j
->type
, JOB_START
, JOB_VERIFY_ACTIVE
))
976 job_finish_and_invalidate(j
, JOB_DEPENDENCY
, true, false);
980 int job_finish_and_invalidate(Job
*j
, JobResult result
, bool recursive
, bool already
) {
988 assert(j
->installed
);
989 assert(j
->type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
996 log_unit_debug(u
, "Job %" PRIu32
" %s/%s finished, result=%s", j
->id
, u
->id
, job_type_to_string(t
), job_result_to_string(result
));
998 /* If this job did nothing to respective unit we don't log the status message */
1000 job_emit_done_status_message(u
, j
->id
, t
, result
);
1002 /* Patch restart jobs so that they become normal start jobs */
1003 if (result
== JOB_DONE
&& t
== JOB_RESTART
) {
1005 job_change_type(j
, JOB_START
);
1006 job_set_state(j
, JOB_WAITING
);
1008 job_add_to_dbus_queue(j
);
1009 job_add_to_run_queue(j
);
1010 job_add_to_gc_queue(j
);
1015 if (IN_SET(result
, JOB_FAILED
, JOB_INVALID
))
1016 j
->manager
->n_failed_jobs
++;
1021 /* Fail depending jobs on failure */
1022 if (result
!= JOB_DONE
&& recursive
) {
1023 if (IN_SET(t
, JOB_START
, JOB_VERIFY_ACTIVE
)) {
1024 job_fail_dependencies(u
, UNIT_REQUIRED_BY
);
1025 job_fail_dependencies(u
, UNIT_REQUISITE_OF
);
1026 job_fail_dependencies(u
, UNIT_BOUND_BY
);
1027 } else if (t
== JOB_STOP
)
1028 job_fail_dependencies(u
, UNIT_CONFLICTED_BY
);
1031 /* A special check to make sure we take down anything RequisiteOf if we
1032 * aren't active. This is when the verify-active job merges with a
1033 * satisfying job type, and then loses it's invalidation effect, as the
1034 * result there is JOB_DONE for the start job we merged into, while we
1035 * should be failing the depending job if the said unit isn't infact
1036 * active. Oneshots are an example of this, where going directly from
1037 * activating to inactive is success.
1039 * This happens when you use ConditionXYZ= in a unit too, since in that
1040 * case the job completes with the JOB_DONE result, but the unit never
1041 * really becomes active. Note that such a case still involves merging:
1043 * A start job waits for something else, and a verify-active comes in
1044 * and merges in the installed job. Then, later, when it becomes
1045 * runnable, it finishes with JOB_DONE result as execution on conditions
1046 * not being met is skipped, breaking our dependency semantics.
1048 * Also, depending on if start job waits or not, the merging may or may
1049 * not happen (the verify-active job may trigger after it finishes), so
1050 * you get undeterministic results without this check.
1052 if (result
== JOB_DONE
&& recursive
&& !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u
))) {
1053 if (IN_SET(t
, JOB_START
, JOB_RELOAD
))
1054 job_fail_dependencies(u
, UNIT_REQUISITE_OF
);
1056 /* Trigger OnFailure dependencies that are not generated by
1057 * the unit itself. We don't treat JOB_CANCELED as failure in
1058 * this context. And JOB_FAILURE is already handled by the
1060 if (IN_SET(result
, JOB_TIMEOUT
, JOB_DEPENDENCY
)) {
1061 log_struct(LOG_NOTICE
,
1062 "JOB_TYPE=%s", job_type_to_string(t
),
1063 "JOB_RESULT=%s", job_result_to_string(result
),
1065 LOG_UNIT_MESSAGE(u
, "Job %s/%s failed with result '%s'.",
1067 job_type_to_string(t
),
1068 job_result_to_string(result
)));
1070 unit_start_on_failure(u
);
1073 unit_trigger_notify(u
);
1076 /* Try to start the next jobs that can be started */
1077 HASHMAP_FOREACH_KEY(v
, other
, u
->dependencies
[UNIT_AFTER
], i
)
1079 job_add_to_run_queue(other
->job
);
1080 job_add_to_gc_queue(other
->job
);
1082 HASHMAP_FOREACH_KEY(v
, other
, u
->dependencies
[UNIT_BEFORE
], i
)
1084 job_add_to_run_queue(other
->job
);
1085 job_add_to_gc_queue(other
->job
);
1088 manager_check_finished(u
->manager
);
1093 static int job_dispatch_timer(sd_event_source
*s
, uint64_t monotonic
, void *userdata
) {
1098 assert(s
== j
->timer_event_source
);
1100 log_unit_warning(j
->unit
, "Job %s/%s timed out.", j
->unit
->id
, job_type_to_string(j
->type
));
1103 job_finish_and_invalidate(j
, JOB_TIMEOUT
, true, false);
1105 emergency_action(u
->manager
, u
->job_timeout_action
,
1106 EMERGENCY_ACTION_IS_WATCHDOG
|EMERGENCY_ACTION_WARN
,
1107 u
->job_timeout_reboot_arg
, -1, "job timed out");
1112 int job_start_timer(Job
*j
, bool job_running
) {
1114 usec_t timeout_time
, old_timeout_time
;
1117 j
->begin_running_usec
= now(CLOCK_MONOTONIC
);
1119 if (j
->unit
->job_running_timeout
== USEC_INFINITY
)
1122 timeout_time
= usec_add(j
->begin_running_usec
, j
->unit
->job_running_timeout
);
1124 if (j
->timer_event_source
) {
1125 /* Update only if JobRunningTimeoutSec= results in earlier timeout */
1126 r
= sd_event_source_get_time(j
->timer_event_source
, &old_timeout_time
);
1130 if (old_timeout_time
<= timeout_time
)
1133 return sd_event_source_set_time(j
->timer_event_source
, timeout_time
);
1136 if (j
->timer_event_source
)
1139 j
->begin_usec
= now(CLOCK_MONOTONIC
);
1141 if (j
->unit
->job_timeout
== USEC_INFINITY
)
1144 timeout_time
= usec_add(j
->begin_usec
, j
->unit
->job_timeout
);
1147 r
= sd_event_add_time(
1149 &j
->timer_event_source
,
1152 job_dispatch_timer
, j
);
1156 (void) sd_event_source_set_description(j
->timer_event_source
, "job-start");
1161 void job_add_to_run_queue(Job
*j
) {
1165 assert(j
->installed
);
1167 if (j
->in_run_queue
)
1170 if (!j
->manager
->run_queue
) {
1171 r
= sd_event_source_set_enabled(j
->manager
->run_queue_event_source
, SD_EVENT_ONESHOT
);
1173 log_warning_errno(r
, "Failed to enable job run queue event source, ignoring: %m");
1176 LIST_PREPEND(run_queue
, j
->manager
->run_queue
, j
);
1177 j
->in_run_queue
= true;
1180 void job_add_to_dbus_queue(Job
*j
) {
1182 assert(j
->installed
);
1184 if (j
->in_dbus_queue
)
1187 /* We don't check if anybody is subscribed here, since this
1188 * job might just have been created and not yet assigned to a
1189 * connection/client. */
1191 LIST_PREPEND(dbus_queue
, j
->manager
->dbus_job_queue
, j
);
1192 j
->in_dbus_queue
= true;
1195 char *job_dbus_path(Job
*j
) {
1200 if (asprintf(&p
, "/org/freedesktop/systemd1/job/%"PRIu32
, j
->id
) < 0)
1206 int job_serialize(Job
*j
, FILE *f
) {
1210 (void) serialize_item_format(f
, "job-id", "%u", j
->id
);
1211 (void) serialize_item(f
, "job-type", job_type_to_string(j
->type
));
1212 (void) serialize_item(f
, "job-state", job_state_to_string(j
->state
));
1213 (void) serialize_bool(f
, "job-irreversible", j
->irreversible
);
1214 (void) serialize_bool(f
, "job-sent-dbus-new-signal", j
->sent_dbus_new_signal
);
1215 (void) serialize_bool(f
, "job-ignore-order", j
->ignore_order
);
1217 if (j
->begin_usec
> 0)
1218 (void) serialize_usec(f
, "job-begin", j
->begin_usec
);
1219 if (j
->begin_running_usec
> 0)
1220 (void) serialize_usec(f
, "job-begin-running", j
->begin_running_usec
);
1222 bus_track_serialize(j
->bus_track
, f
, "subscribed");
1229 int job_deserialize(Job
*j
, FILE *f
) {
1236 _cleanup_free_
char *line
= NULL
;
1240 r
= read_line(f
, LONG_LINE_MAX
, &line
);
1242 return log_error_errno(r
, "Failed to read serialization line: %m");
1252 k
= strcspn(l
, "=");
1260 if (streq(l
, "job-id")) {
1262 if (safe_atou32(v
, &j
->id
) < 0)
1263 log_debug("Failed to parse job id value: %s", v
);
1265 } else if (streq(l
, "job-type")) {
1268 t
= job_type_from_string(v
);
1270 log_debug("Failed to parse job type: %s", v
);
1271 else if (t
>= _JOB_TYPE_MAX_IN_TRANSACTION
)
1272 log_debug("Cannot deserialize job of type: %s", v
);
1276 } else if (streq(l
, "job-state")) {
1279 s
= job_state_from_string(v
);
1281 log_debug("Failed to parse job state: %s", v
);
1283 job_set_state(j
, s
);
1285 } else if (streq(l
, "job-irreversible")) {
1288 b
= parse_boolean(v
);
1290 log_debug("Failed to parse job irreversible flag: %s", v
);
1292 j
->irreversible
= j
->irreversible
|| b
;
1294 } else if (streq(l
, "job-sent-dbus-new-signal")) {
1297 b
= parse_boolean(v
);
1299 log_debug("Failed to parse job sent_dbus_new_signal flag: %s", v
);
1301 j
->sent_dbus_new_signal
= j
->sent_dbus_new_signal
|| b
;
1303 } else if (streq(l
, "job-ignore-order")) {
1306 b
= parse_boolean(v
);
1308 log_debug("Failed to parse job ignore_order flag: %s", v
);
1310 j
->ignore_order
= j
->ignore_order
|| b
;
1312 } else if (streq(l
, "job-begin"))
1313 (void) deserialize_usec(v
, &j
->begin_usec
);
1315 else if (streq(l
, "job-begin-running"))
1316 (void) deserialize_usec(v
, &j
->begin_running_usec
);
1318 else if (streq(l
, "subscribed")) {
1319 if (strv_extend(&j
->deserialized_clients
, v
) < 0)
1322 log_debug("Unknown job serialization key: %s", l
);
1326 int job_coldplug(Job
*j
) {
1328 usec_t timeout_time
= USEC_INFINITY
;
1332 /* After deserialization is complete and the bus connection
1333 * set up again, let's start watching our subscribers again */
1334 (void) bus_job_coldplug_bus_track(j
);
1336 if (j
->state
== JOB_WAITING
)
1337 job_add_to_run_queue(j
);
1339 /* Maybe due to new dependencies we don't actually need this job anymore? */
1340 job_add_to_gc_queue(j
);
1342 /* Create timer only when job began or began running and the respective timeout is finite.
1343 * Follow logic of job_start_timer() if both timeouts are finite */
1344 if (j
->begin_usec
== 0)
1347 if (j
->unit
->job_timeout
!= USEC_INFINITY
)
1348 timeout_time
= usec_add(j
->begin_usec
, j
->unit
->job_timeout
);
1350 if (timestamp_is_set(j
->begin_running_usec
))
1351 timeout_time
= MIN(timeout_time
, usec_add(j
->begin_running_usec
, j
->unit
->job_running_timeout
));
1353 if (timeout_time
== USEC_INFINITY
)
1356 j
->timer_event_source
= sd_event_source_unref(j
->timer_event_source
);
1358 r
= sd_event_add_time(
1360 &j
->timer_event_source
,
1363 job_dispatch_timer
, j
);
1365 log_debug_errno(r
, "Failed to restart timeout for job: %m");
1367 (void) sd_event_source_set_description(j
->timer_event_source
, "job-timeout");
1372 void job_shutdown_magic(Job
*j
) {
1375 /* The shutdown target gets some special treatment here: we
1376 * tell the kernel to begin with flushing its disk caches, to
1377 * optimize shutdown time a bit. Ideally we wouldn't hardcode
1378 * this magic into PID 1. However all other processes aren't
1379 * options either since they'd exit much sooner than PID 1 and
1380 * asynchronous sync() would cause their exit to be
1383 if (j
->type
!= JOB_START
)
1386 if (!MANAGER_IS_SYSTEM(j
->unit
->manager
))
1389 if (!unit_has_name(j
->unit
, SPECIAL_SHUTDOWN_TARGET
))
1392 /* In case messages on console has been disabled on boot */
1393 j
->unit
->manager
->no_console_output
= false;
1395 if (detect_container() > 0)
1398 (void) asynchronous_sync(NULL
);
1401 int job_get_timeout(Job
*j
, usec_t
*timeout
) {
1402 usec_t x
= USEC_INFINITY
, y
= USEC_INFINITY
;
1408 if (j
->timer_event_source
) {
1409 r
= sd_event_source_get_time(j
->timer_event_source
, &x
);
1414 if (UNIT_VTABLE(u
)->get_timeout
) {
1415 r
= UNIT_VTABLE(u
)->get_timeout(u
, &y
);
1420 if (x
== USEC_INFINITY
&& y
== USEC_INFINITY
)
1423 *timeout
= MIN(x
, y
);
1427 bool job_may_gc(Job
*j
) {
1434 /* Checks whether this job should be GC'ed away. We only do this for jobs of units that have no effect on their
1435 * own and just track external state. For now the only unit type that qualifies for this are .device units.
1436 * Returns true if the job can be collected. */
1438 if (!UNIT_VTABLE(j
->unit
)->gc_jobs
)
1441 if (sd_bus_track_count(j
->bus_track
) > 0)
1444 /* FIXME: So this is a bit ugly: for now we don't properly track references made via private bus connections
1445 * (because it's nasty, as sd_bus_track doesn't apply to it). We simply remember that the job was once
1446 * referenced by one, and reset this whenever we notice that no private bus connections are around. This means
1447 * the GC is a bit too conservative when it comes to jobs created by private bus connections. */
1448 if (j
->ref_by_private_bus
) {
1449 if (set_isempty(j
->unit
->manager
->private_buses
))
1450 j
->ref_by_private_bus
= false;
1455 if (j
->type
== JOB_NOP
)
1458 /* If a job is ordered after ours, and is to be started, then it needs to wait for us, regardless if we stop or
1459 * start, hence let's not GC in that case. */
1460 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_BEFORE
], i
) {
1464 if (other
->job
->ignore_order
)
1467 if (IN_SET(other
->job
->type
, JOB_START
, JOB_VERIFY_ACTIVE
, JOB_RELOAD
))
1471 /* If we are going down, but something else is ordered After= us, then it needs to wait for us */
1472 if (IN_SET(j
->type
, JOB_STOP
, JOB_RESTART
))
1473 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_AFTER
], i
) {
1477 if (other
->job
->ignore_order
)
1483 /* The logic above is kinda the inverse of the job_is_runnable() logic. Specifically, if the job "we" is
1484 * ordered before the job "other":
1486 * we start + other start → stay
1487 * we start + other stop → gc
1488 * we stop + other start → stay
1489 * we stop + other stop → gc
1491 * "we" are ordered after "other":
1493 * we start + other start → gc
1494 * we start + other stop → gc
1495 * we stop + other start → stay
1496 * we stop + other stop → stay
1502 void job_add_to_gc_queue(Job
*j
) {
1511 LIST_PREPEND(gc_queue
, j
->unit
->manager
->gc_job_queue
, j
);
1512 j
->in_gc_queue
= true;
1515 static int job_compare(Job
* const *a
, Job
* const *b
) {
1516 return CMP((*a
)->id
, (*b
)->id
);
1519 static size_t sort_job_list(Job
**list
, size_t n
) {
1520 Job
*previous
= NULL
;
1523 /* Order by numeric IDs */
1524 typesafe_qsort(list
, n
, job_compare
);
1526 /* Filter out duplicates */
1527 for (a
= 0, b
= 0; a
< n
; a
++) {
1529 if (previous
== list
[a
])
1532 previous
= list
[b
++] = list
[a
];
1538 int job_get_before(Job
*j
, Job
*** ret
) {
1539 _cleanup_free_ Job
** list
= NULL
;
1540 size_t n
= 0, n_allocated
= 0;
1545 /* Returns a list of all pending jobs that need to finish before this job may be started. */
1550 if (j
->ignore_order
) {
1555 if (IN_SET(j
->type
, JOB_START
, JOB_VERIFY_ACTIVE
, JOB_RELOAD
)) {
1557 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_AFTER
], i
) {
1561 if (!GREEDY_REALLOC(list
, n_allocated
, n
+1))
1563 list
[n
++] = other
->job
;
1567 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_BEFORE
], i
) {
1571 if (!IN_SET(other
->job
->type
, JOB_STOP
, JOB_RESTART
))
1574 if (!GREEDY_REALLOC(list
, n_allocated
, n
+1))
1576 list
[n
++] = other
->job
;
1579 n
= sort_job_list(list
, n
);
1581 *ret
= TAKE_PTR(list
);
1586 int job_get_after(Job
*j
, Job
*** ret
) {
1587 _cleanup_free_ Job
** list
= NULL
;
1588 size_t n
= 0, n_allocated
= 0;
1596 /* Returns a list of all pending jobs that are waiting for this job to finish. */
1598 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_BEFORE
], i
) {
1602 if (other
->job
->ignore_order
)
1605 if (!IN_SET(other
->job
->type
, JOB_START
, JOB_VERIFY_ACTIVE
, JOB_RELOAD
))
1608 if (!GREEDY_REALLOC(list
, n_allocated
, n
+1))
1610 list
[n
++] = other
->job
;
1613 if (IN_SET(j
->type
, JOB_STOP
, JOB_RESTART
)) {
1615 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_AFTER
], i
) {
1619 if (other
->job
->ignore_order
)
1622 if (!GREEDY_REALLOC(list
, n_allocated
, n
+1))
1624 list
[n
++] = other
->job
;
1628 n
= sort_job_list(list
, n
);
1630 *ret
= TAKE_PTR(list
);
1635 static const char* const job_state_table
[_JOB_STATE_MAX
] = {
1636 [JOB_WAITING
] = "waiting",
1637 [JOB_RUNNING
] = "running",
1640 DEFINE_STRING_TABLE_LOOKUP(job_state
, JobState
);
1642 static const char* const job_type_table
[_JOB_TYPE_MAX
] = {
1643 [JOB_START
] = "start",
1644 [JOB_VERIFY_ACTIVE
] = "verify-active",
1645 [JOB_STOP
] = "stop",
1646 [JOB_RELOAD
] = "reload",
1647 [JOB_RELOAD_OR_START
] = "reload-or-start",
1648 [JOB_RESTART
] = "restart",
1649 [JOB_TRY_RESTART
] = "try-restart",
1650 [JOB_TRY_RELOAD
] = "try-reload",
1654 DEFINE_STRING_TABLE_LOOKUP(job_type
, JobType
);
1656 static const char* const job_mode_table
[_JOB_MODE_MAX
] = {
1657 [JOB_FAIL
] = "fail",
1658 [JOB_REPLACE
] = "replace",
1659 [JOB_REPLACE_IRREVERSIBLY
] = "replace-irreversibly",
1660 [JOB_ISOLATE
] = "isolate",
1661 [JOB_FLUSH
] = "flush",
1662 [JOB_IGNORE_DEPENDENCIES
] = "ignore-dependencies",
1663 [JOB_IGNORE_REQUIREMENTS
] = "ignore-requirements",
1666 DEFINE_STRING_TABLE_LOOKUP(job_mode
, JobMode
);
1668 static const char* const job_result_table
[_JOB_RESULT_MAX
] = {
1669 [JOB_DONE
] = "done",
1670 [JOB_CANCELED
] = "canceled",
1671 [JOB_TIMEOUT
] = "timeout",
1672 [JOB_FAILED
] = "failed",
1673 [JOB_DEPENDENCY
] = "dependency",
1674 [JOB_SKIPPED
] = "skipped",
1675 [JOB_INVALID
] = "invalid",
1676 [JOB_ASSERT
] = "assert",
1677 [JOB_UNSUPPORTED
] = "unsupported",
1678 [JOB_COLLECTED
] = "collected",
1679 [JOB_ONCE
] = "once",
1682 DEFINE_STRING_TABLE_LOOKUP(job_result
, JobResult
);
1684 const char* job_type_to_access_method(JobType t
) {
1686 assert(t
< _JOB_TYPE_MAX
);
1688 if (IN_SET(t
, JOB_START
, JOB_RESTART
, JOB_TRY_RESTART
))
1690 else if (t
== JOB_STOP
)