1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "sd-messages.h"
8 #include "alloc-util.h"
17 #include "parse-util.h"
20 #include "stdio-util.h"
21 #include "string-table.h"
22 #include "string-util.h"
24 #include "terminal-util.h"
28 Job
* job_new_raw(Unit
*unit
) {
31 /* used for deserialization */
40 .manager
= unit
->manager
,
42 .type
= _JOB_TYPE_INVALID
,
48 Job
* job_new(Unit
*unit
, JobType type
) {
51 assert(type
< _JOB_TYPE_MAX
);
53 j
= job_new_raw(unit
);
57 j
->id
= j
->manager
->current_job_id
++;
60 /* We don't link it here, that's what job_dependency() is for */
65 void job_unlink(Job
*j
) {
67 assert(!j
->installed
);
68 assert(!j
->transaction_prev
);
69 assert(!j
->transaction_next
);
70 assert(!j
->subject_list
);
71 assert(!j
->object_list
);
73 if (j
->in_run_queue
) {
74 LIST_REMOVE(run_queue
, j
->manager
->run_queue
, j
);
75 j
->in_run_queue
= false;
78 if (j
->in_dbus_queue
) {
79 LIST_REMOVE(dbus_queue
, j
->manager
->dbus_job_queue
, j
);
80 j
->in_dbus_queue
= false;
84 LIST_REMOVE(gc_queue
, j
->manager
->gc_job_queue
, j
);
85 j
->in_gc_queue
= false;
88 j
->timer_event_source
= sd_event_source_unref(j
->timer_event_source
);
91 void job_free(Job
*j
) {
93 assert(!j
->installed
);
94 assert(!j
->transaction_prev
);
95 assert(!j
->transaction_next
);
96 assert(!j
->subject_list
);
97 assert(!j
->object_list
);
101 sd_bus_track_unref(j
->bus_track
);
102 strv_free(j
->deserialized_clients
);
107 static void job_set_state(Job
*j
, JobState state
) {
110 assert(state
< _JOB_STATE_MAX
);
112 if (j
->state
== state
)
120 if (j
->state
== JOB_RUNNING
)
121 j
->unit
->manager
->n_running_jobs
++;
123 assert(j
->state
== JOB_WAITING
);
124 assert(j
->unit
->manager
->n_running_jobs
> 0);
126 j
->unit
->manager
->n_running_jobs
--;
128 if (j
->unit
->manager
->n_running_jobs
<= 0)
129 j
->unit
->manager
->jobs_in_progress_event_source
= sd_event_source_unref(j
->unit
->manager
->jobs_in_progress_event_source
);
133 void job_uninstall(Job
*j
) {
136 assert(j
->installed
);
138 job_set_state(j
, JOB_WAITING
);
140 pj
= (j
->type
== JOB_NOP
) ? &j
->unit
->nop_job
: &j
->unit
->job
;
143 /* Detach from next 'bigger' objects */
145 /* daemon-reload should be transparent to job observers */
146 if (!MANAGER_IS_RELOADING(j
->manager
))
147 bus_job_send_removed_signal(j
);
151 unit_add_to_gc_queue(j
->unit
);
153 hashmap_remove(j
->manager
->jobs
, UINT32_TO_PTR(j
->id
));
154 j
->installed
= false;
157 static bool job_type_allows_late_merge(JobType t
) {
158 /* Tells whether it is OK to merge a job of type 't' with an already
160 * Reloads cannot be merged this way. Think of the sequence:
161 * 1. Reload of a daemon is in progress; the daemon has already loaded
162 * its config file, but hasn't completed the reload operation yet.
163 * 2. Edit foo's config file.
164 * 3. Trigger another reload to have the daemon use the new config.
165 * Should the second reload job be merged into the first one, the daemon
166 * would not know about the new config.
167 * JOB_RESTART jobs on the other hand can be merged, because they get
168 * patched into JOB_START after stopping the unit. So if we see a
169 * JOB_RESTART running, it means the unit hasn't stopped yet and at
170 * this time the merge is still allowed. */
171 return t
!= JOB_RELOAD
;
174 static void job_merge_into_installed(Job
*j
, Job
*other
) {
175 assert(j
->installed
);
176 assert(j
->unit
== other
->unit
);
178 if (j
->type
!= JOB_NOP
)
179 assert_se(job_type_merge_and_collapse(&j
->type
, other
->type
, j
->unit
) == 0);
181 assert(other
->type
== JOB_NOP
);
183 j
->irreversible
= j
->irreversible
|| other
->irreversible
;
184 j
->ignore_order
= j
->ignore_order
|| other
->ignore_order
;
187 Job
* job_install(Job
*j
) {
191 assert(!j
->installed
);
192 assert(j
->type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
193 assert(j
->state
== JOB_WAITING
);
195 pj
= (j
->type
== JOB_NOP
) ? &j
->unit
->nop_job
: &j
->unit
->job
;
199 if (job_type_is_conflicting(uj
->type
, j
->type
))
200 job_finish_and_invalidate(uj
, JOB_CANCELED
, false, false);
202 /* not conflicting, i.e. mergeable */
204 if (uj
->state
== JOB_WAITING
||
205 (job_type_allows_late_merge(j
->type
) && job_type_is_superset(uj
->type
, j
->type
))) {
206 job_merge_into_installed(uj
, j
);
207 log_unit_debug(uj
->unit
,
208 "Merged into installed job %s/%s as %u",
209 uj
->unit
->id
, job_type_to_string(uj
->type
), (unsigned) uj
->id
);
212 /* already running and not safe to merge into */
213 /* Patch uj to become a merged job and re-run it. */
214 /* XXX It should be safer to queue j to run after uj finishes, but it is
215 * not currently possible to have more than one installed job per unit. */
216 job_merge_into_installed(uj
, j
);
217 log_unit_debug(uj
->unit
,
218 "Merged into running job, re-running: %s/%s as %u",
219 uj
->unit
->id
, job_type_to_string(uj
->type
), (unsigned) uj
->id
);
221 job_set_state(uj
, JOB_WAITING
);
227 /* Install the job */
231 j
->manager
->n_installed_jobs
++;
232 log_unit_debug(j
->unit
,
233 "Installed new job %s/%s as %u",
234 j
->unit
->id
, job_type_to_string(j
->type
), (unsigned) j
->id
);
236 job_add_to_gc_queue(j
);
241 int job_install_deserialized(Job
*j
) {
244 assert(!j
->installed
);
246 if (j
->type
< 0 || j
->type
>= _JOB_TYPE_MAX_IN_TRANSACTION
) {
247 log_debug("Invalid job type %s in deserialization.", strna(job_type_to_string(j
->type
)));
251 pj
= (j
->type
== JOB_NOP
) ? &j
->unit
->nop_job
: &j
->unit
->job
;
253 log_unit_debug(j
->unit
, "Unit already has a job installed. Not installing deserialized job.");
261 if (j
->state
== JOB_RUNNING
)
262 j
->unit
->manager
->n_running_jobs
++;
264 log_unit_debug(j
->unit
,
265 "Reinstalled deserialized job %s/%s as %u",
266 j
->unit
->id
, job_type_to_string(j
->type
), (unsigned) j
->id
);
270 JobDependency
* job_dependency_new(Job
*subject
, Job
*object
, bool matters
, bool conflicts
) {
275 /* Adds a new job link, which encodes that the 'subject' job
276 * needs the 'object' job in some way. If 'subject' is NULL
277 * this means the 'anchor' job (i.e. the one the user
278 * explicitly asked for) is the requester. */
280 l
= new0(JobDependency
, 1);
284 l
->subject
= subject
;
286 l
->matters
= matters
;
287 l
->conflicts
= conflicts
;
290 LIST_PREPEND(subject
, subject
->subject_list
, l
);
292 LIST_PREPEND(object
, object
->object_list
, l
);
297 void job_dependency_free(JobDependency
*l
) {
301 LIST_REMOVE(subject
, l
->subject
->subject_list
, l
);
303 LIST_REMOVE(object
, l
->object
->object_list
, l
);
308 void job_dump(Job
*j
, FILE*f
, const char *prefix
) {
312 prefix
= strempty(prefix
);
316 "%s\tAction: %s -> %s\n"
318 "%s\tIrreversible: %s\n"
321 prefix
, j
->unit
->id
, job_type_to_string(j
->type
),
322 prefix
, job_state_to_string(j
->state
),
323 prefix
, yes_no(j
->irreversible
),
324 prefix
, yes_no(job_may_gc(j
)));
328 * Merging is commutative, so imagine the matrix as symmetric. We store only
329 * its lower triangle to avoid duplication. We don't store the main diagonal,
330 * because A merged with A is simply A.
332 * If the resulting type is collapsed immediately afterwards (to get rid of
333 * the JOB_RELOAD_OR_START, which lies outside the lookup function's domain),
334 * the following properties hold:
336 * Merging is associative! A merged with B, and then merged with C is the same
337 * as A merged with the result of B merged with C.
339 * Mergeability is transitive! If A can be merged with B and B with C then
342 * Also, if A merged with B cannot be merged with C, then either A or B cannot
343 * be merged with C either.
345 static const JobType job_merging_table
[] = {
346 /* What \ With * JOB_START JOB_VERIFY_ACTIVE JOB_STOP JOB_RELOAD */
347 /*********************************************************************************/
349 /*JOB_VERIFY_ACTIVE */ JOB_START
,
350 /*JOB_STOP */ -1, -1,
351 /*JOB_RELOAD */ JOB_RELOAD_OR_START
, JOB_RELOAD
, -1,
352 /*JOB_RESTART */ JOB_RESTART
, JOB_RESTART
, -1, JOB_RESTART
,
355 JobType
job_type_lookup_merge(JobType a
, JobType b
) {
356 assert_cc(ELEMENTSOF(job_merging_table
) == _JOB_TYPE_MAX_MERGING
* (_JOB_TYPE_MAX_MERGING
- 1) / 2);
357 assert(a
>= 0 && a
< _JOB_TYPE_MAX_MERGING
);
358 assert(b
>= 0 && b
< _JOB_TYPE_MAX_MERGING
);
369 return job_merging_table
[(a
- 1) * a
/ 2 + b
];
372 bool job_type_is_redundant(JobType a
, UnitActiveState b
) {
376 return IN_SET(b
, UNIT_ACTIVE
, UNIT_RELOADING
);
379 return IN_SET(b
, UNIT_INACTIVE
, UNIT_FAILED
);
381 case JOB_VERIFY_ACTIVE
:
382 return IN_SET(b
, UNIT_ACTIVE
, UNIT_RELOADING
);
390 b
== UNIT_ACTIVATING
;
396 assert_not_reached("Invalid job type");
400 JobType
job_type_collapse(JobType t
, Unit
*u
) {
405 case JOB_TRY_RESTART
:
406 s
= unit_active_state(u
);
407 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s
))
413 s
= unit_active_state(u
);
414 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s
))
419 case JOB_RELOAD_OR_START
:
420 s
= unit_active_state(u
);
421 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s
))
431 int job_type_merge_and_collapse(JobType
*a
, JobType b
, Unit
*u
) {
434 t
= job_type_lookup_merge(*a
, b
);
438 *a
= job_type_collapse(t
, u
);
442 static bool job_is_runnable(Job
*j
) {
448 assert(j
->installed
);
450 /* Checks whether there is any job running for the units this
451 * job needs to be running after (in the case of a 'positive'
452 * job type) or before (in the case of a 'negative' job
455 /* Note that unit types have a say in what is runnable,
456 * too. For example, if they return -EAGAIN from
457 * unit_start() they can indicate they are not
460 /* First check if there is an override */
464 if (j
->type
== JOB_NOP
)
467 if (IN_SET(j
->type
, JOB_START
, JOB_VERIFY_ACTIVE
, JOB_RELOAD
)) {
468 /* Immediate result is that the job is or might be
469 * started. In this case let's wait for the
470 * dependencies, regardless whether they are
471 * starting or stopping something. */
473 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_AFTER
], i
)
478 /* Also, if something else is being stopped and we should
479 * change state after it, then let's wait. */
481 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_BEFORE
], i
)
483 IN_SET(other
->job
->type
, JOB_STOP
, JOB_RESTART
))
486 /* This means that for a service a and a service b where b
487 * shall be started after a:
489 * start a + start b → 1st step start a, 2nd step start b
490 * start a + stop b → 1st step stop b, 2nd step start a
491 * stop a + start b → 1st step stop a, 2nd step start b
492 * stop a + stop b → 1st step stop b, 2nd step stop a
494 * This has the side effect that restarts are properly
495 * synchronized too. */
500 static void job_change_type(Job
*j
, JobType newtype
) {
503 log_unit_debug(j
->unit
,
504 "Converting job %s/%s -> %s/%s",
505 j
->unit
->id
, job_type_to_string(j
->type
),
506 j
->unit
->id
, job_type_to_string(newtype
));
511 static int job_perform_on_unit(Job
**j
) {
518 /* While we execute this operation the job might go away (for
519 * example: because it finishes immediately or is replaced by
520 * a new, conflicting job.) To make sure we don't access a
521 * freed job later on we store the id here, so that we can
522 * verify the job is still valid. */
549 assert_not_reached("Invalid job type");
552 /* Log if the job still exists and the start/stop/reload function
553 * actually did something. */
554 *j
= manager_get_job(m
, id
);
556 unit_status_emit_starting_stopping_reloading(u
, t
);
561 int job_run_and_invalidate(Job
*j
) {
565 assert(j
->installed
);
566 assert(j
->type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
567 assert(j
->in_run_queue
);
569 LIST_REMOVE(run_queue
, j
->manager
->run_queue
, j
);
570 j
->in_run_queue
= false;
572 if (j
->state
!= JOB_WAITING
)
575 if (!job_is_runnable(j
))
578 job_start_timer(j
, true);
579 job_set_state(j
, JOB_RUNNING
);
580 job_add_to_dbus_queue(j
);
584 case JOB_VERIFY_ACTIVE
: {
585 UnitActiveState t
= unit_active_state(j
->unit
);
586 if (UNIT_IS_ACTIVE_OR_RELOADING(t
))
588 else if (t
== UNIT_ACTIVATING
)
598 r
= job_perform_on_unit(&j
);
600 /* If the unit type does not support starting/stopping,
601 * then simply wait. */
607 r
= job_perform_on_unit(&j
);
615 assert_not_reached("Unknown job type");
620 r
= job_finish_and_invalidate(j
, JOB_DONE
, true, true);
621 else if (r
== -EBADR
)
622 r
= job_finish_and_invalidate(j
, JOB_SKIPPED
, true, false);
623 else if (r
== -ENOEXEC
)
624 r
= job_finish_and_invalidate(j
, JOB_INVALID
, true, false);
625 else if (r
== -EPROTO
)
626 r
= job_finish_and_invalidate(j
, JOB_ASSERT
, true, false);
627 else if (r
== -EOPNOTSUPP
)
628 r
= job_finish_and_invalidate(j
, JOB_UNSUPPORTED
, true, false);
629 else if (r
== -ENOLINK
)
630 r
= job_finish_and_invalidate(j
, JOB_DEPENDENCY
, true, false);
631 else if (r
== -ESTALE
)
632 r
= job_finish_and_invalidate(j
, JOB_ONCE
, true, false);
633 else if (r
== -EAGAIN
)
634 job_set_state(j
, JOB_WAITING
);
636 r
= job_finish_and_invalidate(j
, JOB_FAILED
, true, false);
642 _pure_
static const char *job_get_status_message_format(Unit
*u
, JobType t
, JobResult result
) {
644 static const char *const generic_finished_start_job
[_JOB_RESULT_MAX
] = {
645 [JOB_DONE
] = "Started %s.",
646 [JOB_TIMEOUT
] = "Timed out starting %s.",
647 [JOB_FAILED
] = "Failed to start %s.",
648 [JOB_DEPENDENCY
] = "Dependency failed for %s.",
649 [JOB_ASSERT
] = "Assertion failed for %s.",
650 [JOB_UNSUPPORTED
] = "Starting of %s not supported.",
651 [JOB_COLLECTED
] = "Unnecessary job for %s was removed.",
652 [JOB_ONCE
] = "Unit %s has been started before and cannot be started again."
654 static const char *const generic_finished_stop_job
[_JOB_RESULT_MAX
] = {
655 [JOB_DONE
] = "Stopped %s.",
656 [JOB_FAILED
] = "Stopped (with error) %s.",
657 [JOB_TIMEOUT
] = "Timed out stopping %s.",
659 static const char *const generic_finished_reload_job
[_JOB_RESULT_MAX
] = {
660 [JOB_DONE
] = "Reloaded %s.",
661 [JOB_FAILED
] = "Reload failed for %s.",
662 [JOB_TIMEOUT
] = "Timed out reloading %s.",
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 not active.",
671 const UnitStatusMessageFormats
*format_table
;
676 assert(t
< _JOB_TYPE_MAX
);
678 if (IN_SET(t
, JOB_START
, JOB_STOP
, JOB_RESTART
)) {
679 format_table
= &UNIT_VTABLE(u
)->status_message_formats
;
681 format
= t
== JOB_START
? format_table
->finished_start_job
[result
] :
682 format_table
->finished_stop_job
[result
];
688 /* Return generic strings */
690 return generic_finished_start_job
[result
];
691 else if (IN_SET(t
, JOB_STOP
, JOB_RESTART
))
692 return generic_finished_stop_job
[result
];
693 else if (t
== JOB_RELOAD
)
694 return generic_finished_reload_job
[result
];
695 else if (t
== JOB_VERIFY_ACTIVE
)
696 return generic_finished_verify_active_job
[result
];
701 static const struct {
702 const char *color
, *word
;
703 } job_print_status_messages
[_JOB_RESULT_MAX
] = {
704 [JOB_DONE
] = { ANSI_OK_COLOR
, " OK " },
705 [JOB_TIMEOUT
] = { ANSI_HIGHLIGHT_RED
, " TIME " },
706 [JOB_FAILED
] = { ANSI_HIGHLIGHT_RED
, "FAILED" },
707 [JOB_DEPENDENCY
] = { ANSI_HIGHLIGHT_YELLOW
, "DEPEND" },
708 [JOB_SKIPPED
] = { ANSI_HIGHLIGHT
, " INFO " },
709 [JOB_ASSERT
] = { ANSI_HIGHLIGHT_YELLOW
, "ASSERT" },
710 [JOB_UNSUPPORTED
] = { ANSI_HIGHLIGHT_YELLOW
, "UNSUPP" },
712 [JOB_ONCE
] = { ANSI_HIGHLIGHT_RED
, " ONCE " },
715 static void job_print_status_message(Unit
*u
, JobType t
, JobResult result
) {
721 assert(t
< _JOB_TYPE_MAX
);
723 /* Reload status messages have traditionally not been printed to console. */
727 if (!job_print_status_messages
[result
].word
)
730 format
= job_get_status_message_format(u
, t
, result
);
734 if (log_get_show_color())
735 status
= strjoina(job_print_status_messages
[result
].color
,
736 job_print_status_messages
[result
].word
,
739 status
= job_print_status_messages
[result
].word
;
741 if (result
!= JOB_DONE
)
742 manager_flip_auto_status(u
->manager
, true);
744 DISABLE_WARNING_FORMAT_NONLITERAL
;
745 unit_status_printf(u
, status
, format
);
748 if (t
== JOB_START
&& result
== JOB_FAILED
) {
749 _cleanup_free_
char *quoted
;
751 quoted
= shell_maybe_quote(u
->id
, ESCAPE_BACKSLASH
);
752 manager_status_printf(u
->manager
, STATUS_TYPE_NORMAL
, NULL
, "See 'systemctl status %s' for details.", strna(quoted
));
756 static void job_log_status_message(Unit
*u
, JobType t
, JobResult result
) {
757 const char *format
, *mid
;
759 static const int job_result_log_level
[_JOB_RESULT_MAX
] = {
760 [JOB_DONE
] = LOG_INFO
,
761 [JOB_CANCELED
] = LOG_INFO
,
762 [JOB_TIMEOUT
] = LOG_ERR
,
763 [JOB_FAILED
] = LOG_ERR
,
764 [JOB_DEPENDENCY
] = LOG_WARNING
,
765 [JOB_SKIPPED
] = LOG_NOTICE
,
766 [JOB_INVALID
] = LOG_INFO
,
767 [JOB_ASSERT
] = LOG_WARNING
,
768 [JOB_UNSUPPORTED
] = LOG_WARNING
,
769 [JOB_COLLECTED
] = LOG_INFO
,
770 [JOB_ONCE
] = LOG_ERR
,
775 assert(t
< _JOB_TYPE_MAX
);
777 /* Skip printing if output goes to the console, and job_print_status_message()
778 will actually print something to the console. */
779 if (log_on_console() && job_print_status_messages
[result
].word
)
782 format
= job_get_status_message_format(u
, t
, result
);
786 /* The description might be longer than the buffer, but that's OK,
787 * we'll just truncate it here. Note that we use snprintf() rather than
788 * xsprintf() on purpose here: we are fine with truncation and don't
789 * consider that an error. */
790 DISABLE_WARNING_FORMAT_NONLITERAL
;
791 (void) snprintf(buf
, sizeof(buf
), format
, unit_description(u
));
797 if (result
== JOB_DONE
)
798 mid
= "MESSAGE_ID=" SD_MESSAGE_UNIT_STARTED_STR
;
800 mid
= "MESSAGE_ID=" SD_MESSAGE_UNIT_FAILED_STR
;
804 mid
= "MESSAGE_ID=" SD_MESSAGE_UNIT_RELOADED_STR
;
809 mid
= "MESSAGE_ID=" SD_MESSAGE_UNIT_STOPPED_STR
;
813 log_struct(job_result_log_level
[result
],
814 LOG_MESSAGE("%s", buf
),
815 "JOB_TYPE=%s", job_type_to_string(t
),
816 "JOB_RESULT=%s", job_result_to_string(result
),
818 LOG_UNIT_INVOCATION_ID(u
));
822 log_struct(job_result_log_level
[result
],
823 LOG_MESSAGE("%s", buf
),
824 "JOB_TYPE=%s", job_type_to_string(t
),
825 "JOB_RESULT=%s", job_result_to_string(result
),
827 LOG_UNIT_INVOCATION_ID(u
),
831 static void job_emit_status_message(Unit
*u
, JobType t
, JobResult result
) {
834 /* No message if the job did not actually do anything due to failed condition. */
835 if (t
== JOB_START
&& result
== JOB_DONE
&& !u
->condition_result
)
838 job_log_status_message(u
, t
, result
);
839 job_print_status_message(u
, t
, result
);
842 static void job_fail_dependencies(Unit
*u
, UnitDependency d
) {
849 HASHMAP_FOREACH_KEY(v
, other
, u
->dependencies
[d
], i
) {
854 if (!IN_SET(j
->type
, JOB_START
, JOB_VERIFY_ACTIVE
))
857 job_finish_and_invalidate(j
, JOB_DEPENDENCY
, true, false);
861 static int job_save_pending_finished_job(Job
*j
) {
866 r
= set_ensure_allocated(&j
->manager
->pending_finished_jobs
, NULL
);
871 return set_put(j
->manager
->pending_finished_jobs
, j
);
874 int job_finish_and_invalidate(Job
*j
, JobResult result
, bool recursive
, bool already
) {
882 assert(j
->installed
);
883 assert(j
->type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
890 log_unit_debug(u
, "Job %s/%s finished, result=%s", u
->id
, job_type_to_string(t
), job_result_to_string(result
));
892 /* If this job did nothing to respective unit we don't log the status message */
894 job_emit_status_message(u
, t
, result
);
896 /* Patch restart jobs so that they become normal start jobs */
897 if (result
== JOB_DONE
&& t
== JOB_RESTART
) {
899 job_change_type(j
, JOB_START
);
900 job_set_state(j
, JOB_WAITING
);
902 job_add_to_dbus_queue(j
);
903 job_add_to_run_queue(j
);
904 job_add_to_gc_queue(j
);
909 if (IN_SET(result
, JOB_FAILED
, JOB_INVALID
))
910 j
->manager
->n_failed_jobs
++;
913 /* Keep jobs started before the reload to send singal later, free all others */
914 if (!MANAGER_IS_RELOADING(j
->manager
) ||
916 job_save_pending_finished_job(j
) < 0)
919 /* Fail depending jobs on failure */
920 if (result
!= JOB_DONE
&& recursive
) {
921 if (IN_SET(t
, JOB_START
, JOB_VERIFY_ACTIVE
)) {
922 job_fail_dependencies(u
, UNIT_REQUIRED_BY
);
923 job_fail_dependencies(u
, UNIT_REQUISITE_OF
);
924 job_fail_dependencies(u
, UNIT_BOUND_BY
);
925 } else if (t
== JOB_STOP
)
926 job_fail_dependencies(u
, UNIT_CONFLICTED_BY
);
929 /* Trigger OnFailure dependencies that are not generated by
930 * the unit itself. We don't treat JOB_CANCELED as failure in
931 * this context. And JOB_FAILURE is already handled by the
933 if (IN_SET(result
, JOB_TIMEOUT
, JOB_DEPENDENCY
)) {
934 log_struct(LOG_NOTICE
,
935 "JOB_TYPE=%s", job_type_to_string(t
),
936 "JOB_RESULT=%s", job_result_to_string(result
),
938 LOG_UNIT_MESSAGE(u
, "Job %s/%s failed with result '%s'.",
940 job_type_to_string(t
),
941 job_result_to_string(result
)));
943 unit_start_on_failure(u
);
946 unit_trigger_notify(u
);
949 /* Try to start the next jobs that can be started */
950 HASHMAP_FOREACH_KEY(v
, other
, u
->dependencies
[UNIT_AFTER
], i
)
952 job_add_to_run_queue(other
->job
);
953 job_add_to_gc_queue(other
->job
);
955 HASHMAP_FOREACH_KEY(v
, other
, u
->dependencies
[UNIT_BEFORE
], i
)
957 job_add_to_run_queue(other
->job
);
958 job_add_to_gc_queue(other
->job
);
961 manager_check_finished(u
->manager
);
966 static int job_dispatch_timer(sd_event_source
*s
, uint64_t monotonic
, void *userdata
) {
971 assert(s
== j
->timer_event_source
);
973 log_unit_warning(j
->unit
, "Job %s/%s timed out.", j
->unit
->id
, job_type_to_string(j
->type
));
976 job_finish_and_invalidate(j
, JOB_TIMEOUT
, true, false);
978 emergency_action(u
->manager
, u
->job_timeout_action
,
979 EMERGENCY_ACTION_IS_WATCHDOG
|EMERGENCY_ACTION_WARN
,
980 u
->job_timeout_reboot_arg
, "job timed out");
985 int job_start_timer(Job
*j
, bool job_running
) {
987 usec_t timeout_time
, old_timeout_time
;
990 j
->begin_running_usec
= now(CLOCK_MONOTONIC
);
992 if (j
->unit
->job_running_timeout
== USEC_INFINITY
)
995 timeout_time
= usec_add(j
->begin_running_usec
, j
->unit
->job_running_timeout
);
997 if (j
->timer_event_source
) {
998 /* Update only if JobRunningTimeoutSec= results in earlier timeout */
999 r
= sd_event_source_get_time(j
->timer_event_source
, &old_timeout_time
);
1003 if (old_timeout_time
<= timeout_time
)
1006 return sd_event_source_set_time(j
->timer_event_source
, timeout_time
);
1009 if (j
->timer_event_source
)
1012 j
->begin_usec
= now(CLOCK_MONOTONIC
);
1014 if (j
->unit
->job_timeout
== USEC_INFINITY
)
1017 timeout_time
= usec_add(j
->begin_usec
, j
->unit
->job_timeout
);
1020 r
= sd_event_add_time(
1022 &j
->timer_event_source
,
1025 job_dispatch_timer
, j
);
1029 (void) sd_event_source_set_description(j
->timer_event_source
, "job-start");
1034 void job_add_to_run_queue(Job
*j
) {
1036 assert(j
->installed
);
1038 if (j
->in_run_queue
)
1041 if (!j
->manager
->run_queue
)
1042 sd_event_source_set_enabled(j
->manager
->run_queue_event_source
, SD_EVENT_ONESHOT
);
1044 LIST_PREPEND(run_queue
, j
->manager
->run_queue
, j
);
1045 j
->in_run_queue
= true;
1048 void job_add_to_dbus_queue(Job
*j
) {
1050 assert(j
->installed
);
1052 if (j
->in_dbus_queue
)
1055 /* We don't check if anybody is subscribed here, since this
1056 * job might just have been created and not yet assigned to a
1057 * connection/client. */
1059 LIST_PREPEND(dbus_queue
, j
->manager
->dbus_job_queue
, j
);
1060 j
->in_dbus_queue
= true;
1063 char *job_dbus_path(Job
*j
) {
1068 if (asprintf(&p
, "/org/freedesktop/systemd1/job/%"PRIu32
, j
->id
) < 0)
1074 int job_serialize(Job
*j
, FILE *f
) {
1078 fprintf(f
, "job-id=%u\n", j
->id
);
1079 fprintf(f
, "job-type=%s\n", job_type_to_string(j
->type
));
1080 fprintf(f
, "job-state=%s\n", job_state_to_string(j
->state
));
1081 fprintf(f
, "job-irreversible=%s\n", yes_no(j
->irreversible
));
1082 fprintf(f
, "job-sent-dbus-new-signal=%s\n", yes_no(j
->sent_dbus_new_signal
));
1083 fprintf(f
, "job-ignore-order=%s\n", yes_no(j
->ignore_order
));
1085 if (j
->begin_usec
> 0)
1086 fprintf(f
, "job-begin="USEC_FMT
"\n", j
->begin_usec
);
1087 if (j
->begin_running_usec
> 0)
1088 fprintf(f
, "job-begin-running="USEC_FMT
"\n", j
->begin_running_usec
);
1090 bus_track_serialize(j
->bus_track
, f
, "subscribed");
1097 int job_deserialize(Job
*j
, FILE *f
) {
1104 _cleanup_free_
char *line
= NULL
;
1108 r
= read_line(f
, LONG_LINE_MAX
, &line
);
1110 return log_error_errno(r
, "Failed to read serialization line: %m");
1120 k
= strcspn(l
, "=");
1128 if (streq(l
, "job-id")) {
1130 if (safe_atou32(v
, &j
->id
) < 0)
1131 log_debug("Failed to parse job id value: %s", v
);
1133 } else if (streq(l
, "job-type")) {
1136 t
= job_type_from_string(v
);
1138 log_debug("Failed to parse job type: %s", v
);
1139 else if (t
>= _JOB_TYPE_MAX_IN_TRANSACTION
)
1140 log_debug("Cannot deserialize job of type: %s", v
);
1144 } else if (streq(l
, "job-state")) {
1147 s
= job_state_from_string(v
);
1149 log_debug("Failed to parse job state: %s", v
);
1151 job_set_state(j
, s
);
1153 } else if (streq(l
, "job-irreversible")) {
1156 b
= parse_boolean(v
);
1158 log_debug("Failed to parse job irreversible flag: %s", v
);
1160 j
->irreversible
= j
->irreversible
|| b
;
1162 } else if (streq(l
, "job-sent-dbus-new-signal")) {
1165 b
= parse_boolean(v
);
1167 log_debug("Failed to parse job sent_dbus_new_signal flag: %s", v
);
1169 j
->sent_dbus_new_signal
= j
->sent_dbus_new_signal
|| b
;
1171 } else if (streq(l
, "job-ignore-order")) {
1174 b
= parse_boolean(v
);
1176 log_debug("Failed to parse job ignore_order flag: %s", v
);
1178 j
->ignore_order
= j
->ignore_order
|| b
;
1180 } else if (streq(l
, "job-begin")) {
1181 unsigned long long ull
;
1183 if (sscanf(v
, "%llu", &ull
) != 1)
1184 log_debug("Failed to parse job-begin value %s", v
);
1186 j
->begin_usec
= ull
;
1188 } else if (streq(l
, "job-begin-running")) {
1189 unsigned long long ull
;
1191 if (sscanf(v
, "%llu", &ull
) != 1)
1192 log_debug("Failed to parse job-begin-running value %s", v
);
1194 j
->begin_running_usec
= ull
;
1196 } else if (streq(l
, "subscribed")) {
1198 if (strv_extend(&j
->deserialized_clients
, v
) < 0)
1204 int job_coldplug(Job
*j
) {
1206 usec_t timeout_time
= USEC_INFINITY
;
1210 /* After deserialization is complete and the bus connection
1211 * set up again, let's start watching our subscribers again */
1212 (void) bus_job_coldplug_bus_track(j
);
1214 if (j
->state
== JOB_WAITING
)
1215 job_add_to_run_queue(j
);
1217 /* Maybe due to new dependencies we don't actually need this job anymore? */
1218 job_add_to_gc_queue(j
);
1220 /* Create timer only when job began or began running and the respective timeout is finite.
1221 * Follow logic of job_start_timer() if both timeouts are finite */
1222 if (j
->begin_usec
== 0)
1225 if (j
->unit
->job_timeout
!= USEC_INFINITY
)
1226 timeout_time
= usec_add(j
->begin_usec
, j
->unit
->job_timeout
);
1228 if (j
->begin_running_usec
> 0 && j
->unit
->job_running_timeout
!= USEC_INFINITY
)
1229 timeout_time
= MIN(timeout_time
, usec_add(j
->begin_running_usec
, j
->unit
->job_running_timeout
));
1231 if (timeout_time
== USEC_INFINITY
)
1234 j
->timer_event_source
= sd_event_source_unref(j
->timer_event_source
);
1236 r
= sd_event_add_time(
1238 &j
->timer_event_source
,
1241 job_dispatch_timer
, j
);
1243 log_debug_errno(r
, "Failed to restart timeout for job: %m");
1245 (void) sd_event_source_set_description(j
->timer_event_source
, "job-timeout");
1250 void job_shutdown_magic(Job
*j
) {
1253 /* The shutdown target gets some special treatment here: we
1254 * tell the kernel to begin with flushing its disk caches, to
1255 * optimize shutdown time a bit. Ideally we wouldn't hardcode
1256 * this magic into PID 1. However all other processes aren't
1257 * options either since they'd exit much sooner than PID 1 and
1258 * asynchronous sync() would cause their exit to be
1261 if (j
->type
!= JOB_START
)
1264 if (!MANAGER_IS_SYSTEM(j
->unit
->manager
))
1267 if (!unit_has_name(j
->unit
, SPECIAL_SHUTDOWN_TARGET
))
1270 /* In case messages on console has been disabled on boot */
1271 j
->unit
->manager
->no_console_output
= false;
1273 if (detect_container() > 0)
1276 (void) asynchronous_sync(NULL
);
1279 int job_get_timeout(Job
*j
, usec_t
*timeout
) {
1280 usec_t x
= USEC_INFINITY
, y
= USEC_INFINITY
;
1286 if (j
->timer_event_source
) {
1287 r
= sd_event_source_get_time(j
->timer_event_source
, &x
);
1292 if (UNIT_VTABLE(u
)->get_timeout
) {
1293 r
= UNIT_VTABLE(u
)->get_timeout(u
, &y
);
1298 if (x
== USEC_INFINITY
&& y
== USEC_INFINITY
)
1301 *timeout
= MIN(x
, y
);
1305 bool job_may_gc(Job
*j
) {
1312 /* Checks whether this job should be GC'ed away. We only do this for jobs of units that have no effect on their
1313 * own and just track external state. For now the only unit type that qualifies for this are .device units.
1314 * Returns true if the job can be collected. */
1316 if (!UNIT_VTABLE(j
->unit
)->gc_jobs
)
1319 if (sd_bus_track_count(j
->bus_track
) > 0)
1322 /* FIXME: So this is a bit ugly: for now we don't properly track references made via private bus connections
1323 * (because it's nasty, as sd_bus_track doesn't apply to it). We simply remember that the job was once
1324 * referenced by one, and reset this whenever we notice that no private bus connections are around. This means
1325 * the GC is a bit too conservative when it comes to jobs created by private bus connections. */
1326 if (j
->ref_by_private_bus
) {
1327 if (set_isempty(j
->unit
->manager
->private_buses
))
1328 j
->ref_by_private_bus
= false;
1333 if (j
->type
== JOB_NOP
)
1336 /* If a job is ordered after ours, and is to be started, then it needs to wait for us, regardless if we stop or
1337 * start, hence let's not GC in that case. */
1338 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_BEFORE
], i
) {
1342 if (other
->job
->ignore_order
)
1345 if (IN_SET(other
->job
->type
, JOB_START
, JOB_VERIFY_ACTIVE
, JOB_RELOAD
))
1349 /* If we are going down, but something else is ordered After= us, then it needs to wait for us */
1350 if (IN_SET(j
->type
, JOB_STOP
, JOB_RESTART
))
1351 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_AFTER
], i
) {
1355 if (other
->job
->ignore_order
)
1361 /* The logic above is kinda the inverse of the job_is_runnable() logic. Specifically, if the job "we" is
1362 * ordered before the job "other":
1364 * we start + other start → stay
1365 * we start + other stop → gc
1366 * we stop + other start → stay
1367 * we stop + other stop → gc
1369 * "we" are ordered after "other":
1371 * we start + other start → gc
1372 * we start + other stop → gc
1373 * we stop + other start → stay
1374 * we stop + other stop → stay
1380 void job_add_to_gc_queue(Job
*j
) {
1389 LIST_PREPEND(gc_queue
, j
->unit
->manager
->gc_job_queue
, j
);
1390 j
->in_gc_queue
= true;
1393 static int job_compare(Job
* const *a
, Job
* const *b
) {
1394 return CMP((*a
)->id
, (*b
)->id
);
1397 static size_t sort_job_list(Job
**list
, size_t n
) {
1398 Job
*previous
= NULL
;
1401 /* Order by numeric IDs */
1402 typesafe_qsort(list
, n
, job_compare
);
1404 /* Filter out duplicates */
1405 for (a
= 0, b
= 0; a
< n
; a
++) {
1407 if (previous
== list
[a
])
1410 previous
= list
[b
++] = list
[a
];
1416 int job_get_before(Job
*j
, Job
*** ret
) {
1417 _cleanup_free_ Job
** list
= NULL
;
1418 size_t n
= 0, n_allocated
= 0;
1423 /* Returns a list of all pending jobs that need to finish before this job may be started. */
1428 if (j
->ignore_order
) {
1433 if (IN_SET(j
->type
, JOB_START
, JOB_VERIFY_ACTIVE
, JOB_RELOAD
)) {
1435 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_AFTER
], i
) {
1439 if (!GREEDY_REALLOC(list
, n_allocated
, n
+1))
1441 list
[n
++] = other
->job
;
1445 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_BEFORE
], i
) {
1449 if (!IN_SET(other
->job
->type
, JOB_STOP
, JOB_RESTART
))
1452 if (!GREEDY_REALLOC(list
, n_allocated
, n
+1))
1454 list
[n
++] = other
->job
;
1457 n
= sort_job_list(list
, n
);
1459 *ret
= TAKE_PTR(list
);
1464 int job_get_after(Job
*j
, Job
*** ret
) {
1465 _cleanup_free_ Job
** list
= NULL
;
1466 size_t n
= 0, n_allocated
= 0;
1474 /* Returns a list of all pending jobs that are waiting for this job to finish. */
1476 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_BEFORE
], i
) {
1480 if (other
->job
->ignore_order
)
1483 if (!IN_SET(other
->job
->type
, JOB_START
, JOB_VERIFY_ACTIVE
, JOB_RELOAD
))
1486 if (!GREEDY_REALLOC(list
, n_allocated
, n
+1))
1488 list
[n
++] = other
->job
;
1491 if (IN_SET(j
->type
, JOB_STOP
, JOB_RESTART
)) {
1493 HASHMAP_FOREACH_KEY(v
, other
, j
->unit
->dependencies
[UNIT_AFTER
], i
) {
1497 if (other
->job
->ignore_order
)
1500 if (!GREEDY_REALLOC(list
, n_allocated
, n
+1))
1502 list
[n
++] = other
->job
;
1506 n
= sort_job_list(list
, n
);
1508 *ret
= TAKE_PTR(list
);
1513 static const char* const job_state_table
[_JOB_STATE_MAX
] = {
1514 [JOB_WAITING
] = "waiting",
1515 [JOB_RUNNING
] = "running",
1518 DEFINE_STRING_TABLE_LOOKUP(job_state
, JobState
);
1520 static const char* const job_type_table
[_JOB_TYPE_MAX
] = {
1521 [JOB_START
] = "start",
1522 [JOB_VERIFY_ACTIVE
] = "verify-active",
1523 [JOB_STOP
] = "stop",
1524 [JOB_RELOAD
] = "reload",
1525 [JOB_RELOAD_OR_START
] = "reload-or-start",
1526 [JOB_RESTART
] = "restart",
1527 [JOB_TRY_RESTART
] = "try-restart",
1528 [JOB_TRY_RELOAD
] = "try-reload",
1532 DEFINE_STRING_TABLE_LOOKUP(job_type
, JobType
);
1534 static const char* const job_mode_table
[_JOB_MODE_MAX
] = {
1535 [JOB_FAIL
] = "fail",
1536 [JOB_REPLACE
] = "replace",
1537 [JOB_REPLACE_IRREVERSIBLY
] = "replace-irreversibly",
1538 [JOB_ISOLATE
] = "isolate",
1539 [JOB_FLUSH
] = "flush",
1540 [JOB_IGNORE_DEPENDENCIES
] = "ignore-dependencies",
1541 [JOB_IGNORE_REQUIREMENTS
] = "ignore-requirements",
1544 DEFINE_STRING_TABLE_LOOKUP(job_mode
, JobMode
);
1546 static const char* const job_result_table
[_JOB_RESULT_MAX
] = {
1547 [JOB_DONE
] = "done",
1548 [JOB_CANCELED
] = "canceled",
1549 [JOB_TIMEOUT
] = "timeout",
1550 [JOB_FAILED
] = "failed",
1551 [JOB_DEPENDENCY
] = "dependency",
1552 [JOB_SKIPPED
] = "skipped",
1553 [JOB_INVALID
] = "invalid",
1554 [JOB_ASSERT
] = "assert",
1555 [JOB_UNSUPPORTED
] = "unsupported",
1556 [JOB_COLLECTED
] = "collected",
1557 [JOB_ONCE
] = "once",
1560 DEFINE_STRING_TABLE_LOOKUP(job_result
, JobResult
);
1562 const char* job_type_to_access_method(JobType t
) {
1564 assert(t
< _JOB_TYPE_MAX
);
1566 if (IN_SET(t
, JOB_START
, JOB_RESTART
, JOB_TRY_RESTART
))
1568 else if (t
== JOB_STOP
)