1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "sd-messages.h"
34 #include "parse-util.h"
37 #include "string-util.h"
39 #include "terminal-util.h"
43 Job
* job_new_raw(Unit
*unit
) {
46 /* used for deserialization */
54 j
->manager
= unit
->manager
;
56 j
->type
= _JOB_TYPE_INVALID
;
61 Job
* job_new(Unit
*unit
, JobType type
) {
64 assert(type
< _JOB_TYPE_MAX
);
66 j
= job_new_raw(unit
);
70 j
->id
= j
->manager
->current_job_id
++;
73 /* We don't link it here, that's what job_dependency() is for */
78 void job_free(Job
*j
) {
80 assert(!j
->installed
);
81 assert(!j
->transaction_prev
);
82 assert(!j
->transaction_next
);
83 assert(!j
->subject_list
);
84 assert(!j
->object_list
);
87 LIST_REMOVE(run_queue
, j
->manager
->run_queue
, j
);
90 LIST_REMOVE(dbus_queue
, j
->manager
->dbus_job_queue
, j
);
92 sd_event_source_unref(j
->timer_event_source
);
94 sd_bus_track_unref(j
->clients
);
95 strv_free(j
->deserialized_clients
);
100 static void job_set_state(Job
*j
, JobState state
) {
103 assert(state
< _JOB_STATE_MAX
);
105 if (j
->state
== state
)
113 if (j
->state
== JOB_RUNNING
)
114 j
->unit
->manager
->n_running_jobs
++;
116 assert(j
->state
== JOB_WAITING
);
117 assert(j
->unit
->manager
->n_running_jobs
> 0);
119 j
->unit
->manager
->n_running_jobs
--;
121 if (j
->unit
->manager
->n_running_jobs
<= 0)
122 j
->unit
->manager
->jobs_in_progress_event_source
= sd_event_source_unref(j
->unit
->manager
->jobs_in_progress_event_source
);
126 void job_uninstall(Job
*j
) {
129 assert(j
->installed
);
131 job_set_state(j
, JOB_WAITING
);
133 pj
= (j
->type
== JOB_NOP
) ? &j
->unit
->nop_job
: &j
->unit
->job
;
136 /* Detach from next 'bigger' objects */
138 /* daemon-reload should be transparent to job observers */
139 if (j
->manager
->n_reloading
<= 0)
140 bus_job_send_removed_signal(j
);
144 unit_add_to_gc_queue(j
->unit
);
146 hashmap_remove(j
->manager
->jobs
, UINT32_TO_PTR(j
->id
));
147 j
->installed
= false;
150 static bool job_type_allows_late_merge(JobType t
) {
151 /* Tells whether it is OK to merge a job of type 't' with an already
153 * Reloads cannot be merged this way. Think of the sequence:
154 * 1. Reload of a daemon is in progress; the daemon has already loaded
155 * its config file, but hasn't completed the reload operation yet.
156 * 2. Edit foo's config file.
157 * 3. Trigger another reload to have the daemon use the new config.
158 * Should the second reload job be merged into the first one, the daemon
159 * would not know about the new config.
160 * JOB_RESTART jobs on the other hand can be merged, because they get
161 * patched into JOB_START after stopping the unit. So if we see a
162 * JOB_RESTART running, it means the unit hasn't stopped yet and at
163 * this time the merge is still allowed. */
164 return t
!= JOB_RELOAD
;
167 static void job_merge_into_installed(Job
*j
, Job
*other
) {
168 assert(j
->installed
);
169 assert(j
->unit
== other
->unit
);
171 if (j
->type
!= JOB_NOP
)
172 job_type_merge_and_collapse(&j
->type
, other
->type
, j
->unit
);
174 assert(other
->type
== JOB_NOP
);
176 j
->override
= j
->override
|| other
->override
;
177 j
->irreversible
= j
->irreversible
|| other
->irreversible
;
178 j
->ignore_order
= j
->ignore_order
|| other
->ignore_order
;
181 Job
* job_install(Job
*j
) {
185 assert(!j
->installed
);
186 assert(j
->type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
187 assert(j
->state
== JOB_WAITING
);
189 pj
= (j
->type
== JOB_NOP
) ? &j
->unit
->nop_job
: &j
->unit
->job
;
193 if (job_type_is_conflicting(uj
->type
, j
->type
))
194 job_finish_and_invalidate(uj
, JOB_CANCELED
, false);
196 /* not conflicting, i.e. mergeable */
198 if (uj
->state
== JOB_WAITING
||
199 (job_type_allows_late_merge(j
->type
) && job_type_is_superset(uj
->type
, j
->type
))) {
200 job_merge_into_installed(uj
, j
);
201 log_unit_debug(uj
->unit
,
202 "Merged into installed job %s/%s as %u",
203 uj
->unit
->id
, job_type_to_string(uj
->type
), (unsigned) uj
->id
);
206 /* already running and not safe to merge into */
207 /* Patch uj to become a merged job and re-run it. */
208 /* XXX It should be safer to queue j to run after uj finishes, but it is
209 * not currently possible to have more than one installed job per unit. */
210 job_merge_into_installed(uj
, j
);
211 log_unit_debug(uj
->unit
,
212 "Merged into running job, re-running: %s/%s as %u",
213 uj
->unit
->id
, job_type_to_string(uj
->type
), (unsigned) uj
->id
);
215 job_set_state(uj
, JOB_WAITING
);
221 /* Install the job */
225 j
->manager
->n_installed_jobs
++;
226 log_unit_debug(j
->unit
,
227 "Installed new job %s/%s as %u",
228 j
->unit
->id
, job_type_to_string(j
->type
), (unsigned) j
->id
);
232 int job_install_deserialized(Job
*j
) {
235 assert(!j
->installed
);
237 if (j
->type
< 0 || j
->type
>= _JOB_TYPE_MAX_IN_TRANSACTION
) {
238 log_debug("Invalid job type %s in deserialization.", strna(job_type_to_string(j
->type
)));
242 pj
= (j
->type
== JOB_NOP
) ? &j
->unit
->nop_job
: &j
->unit
->job
;
244 log_unit_debug(j
->unit
, "Unit already has a job installed. Not installing deserialized job.");
251 if (j
->state
== JOB_RUNNING
)
252 j
->unit
->manager
->n_running_jobs
++;
254 log_unit_debug(j
->unit
,
255 "Reinstalled deserialized job %s/%s as %u",
256 j
->unit
->id
, job_type_to_string(j
->type
), (unsigned) j
->id
);
260 JobDependency
* job_dependency_new(Job
*subject
, Job
*object
, bool matters
, bool conflicts
) {
265 /* Adds a new job link, which encodes that the 'subject' job
266 * needs the 'object' job in some way. If 'subject' is NULL
267 * this means the 'anchor' job (i.e. the one the user
268 * explicitly asked for) is the requester. */
270 if (!(l
= new0(JobDependency
, 1)))
273 l
->subject
= subject
;
275 l
->matters
= matters
;
276 l
->conflicts
= conflicts
;
279 LIST_PREPEND(subject
, subject
->subject_list
, l
);
281 LIST_PREPEND(object
, object
->object_list
, l
);
286 void job_dependency_free(JobDependency
*l
) {
290 LIST_REMOVE(subject
, l
->subject
->subject_list
, l
);
292 LIST_REMOVE(object
, l
->object
->object_list
, l
);
297 void job_dump(Job
*j
, FILE*f
, const char *prefix
) {
306 "%s\tAction: %s -> %s\n"
309 "%s\tIrreversible: %s\n",
311 prefix
, j
->unit
->id
, job_type_to_string(j
->type
),
312 prefix
, job_state_to_string(j
->state
),
313 prefix
, yes_no(j
->override
),
314 prefix
, yes_no(j
->irreversible
));
318 * Merging is commutative, so imagine the matrix as symmetric. We store only
319 * its lower triangle to avoid duplication. We don't store the main diagonal,
320 * because A merged with A is simply A.
322 * If the resulting type is collapsed immediately afterwards (to get rid of
323 * the JOB_RELOAD_OR_START, which lies outside the lookup function's domain),
324 * the following properties hold:
326 * Merging is associative! A merged with B, and then merged with C is the same
327 * as A merged with the result of B merged with C.
329 * Mergeability is transitive! If A can be merged with B and B with C then
332 * Also, if A merged with B cannot be merged with C, then either A or B cannot
333 * be merged with C either.
335 static const JobType job_merging_table
[] = {
336 /* What \ With * JOB_START JOB_VERIFY_ACTIVE JOB_STOP JOB_RELOAD */
337 /*********************************************************************************/
339 /*JOB_VERIFY_ACTIVE */ JOB_START
,
340 /*JOB_STOP */ -1, -1,
341 /*JOB_RELOAD */ JOB_RELOAD_OR_START
, JOB_RELOAD
, -1,
342 /*JOB_RESTART */ JOB_RESTART
, JOB_RESTART
, -1, JOB_RESTART
,
345 JobType
job_type_lookup_merge(JobType a
, JobType b
) {
346 assert_cc(ELEMENTSOF(job_merging_table
) == _JOB_TYPE_MAX_MERGING
* (_JOB_TYPE_MAX_MERGING
- 1) / 2);
347 assert(a
>= 0 && a
< _JOB_TYPE_MAX_MERGING
);
348 assert(b
>= 0 && b
< _JOB_TYPE_MAX_MERGING
);
359 return job_merging_table
[(a
- 1) * a
/ 2 + b
];
362 bool job_type_is_redundant(JobType a
, UnitActiveState b
) {
372 b
== UNIT_INACTIVE
||
375 case JOB_VERIFY_ACTIVE
:
386 b
== UNIT_ACTIVATING
;
392 assert_not_reached("Invalid job type");
396 JobType
job_type_collapse(JobType t
, Unit
*u
) {
401 case JOB_TRY_RESTART
:
402 s
= unit_active_state(u
);
403 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s
))
408 case JOB_RELOAD_OR_START
:
409 s
= unit_active_state(u
);
410 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s
))
420 int job_type_merge_and_collapse(JobType
*a
, JobType b
, Unit
*u
) {
423 t
= job_type_lookup_merge(*a
, b
);
427 *a
= job_type_collapse(t
, u
);
431 static bool job_is_runnable(Job
*j
) {
436 assert(j
->installed
);
438 /* Checks whether there is any job running for the units this
439 * job needs to be running after (in the case of a 'positive'
440 * job type) or before (in the case of a 'negative' job
443 /* Note that unit types have a say in what is runnable,
444 * too. For example, if they return -EAGAIN from
445 * unit_start() they can indicate they are not
448 /* First check if there is an override */
452 if (j
->type
== JOB_NOP
)
455 if (j
->type
== JOB_START
||
456 j
->type
== JOB_VERIFY_ACTIVE
||
457 j
->type
== JOB_RELOAD
) {
459 /* Immediate result is that the job is or might be
460 * started. In this case let's wait for the
461 * dependencies, regardless whether they are
462 * starting or stopping something. */
464 SET_FOREACH(other
, j
->unit
->dependencies
[UNIT_AFTER
], i
)
469 /* Also, if something else is being stopped and we should
470 * change state after it, then let's wait. */
472 SET_FOREACH(other
, j
->unit
->dependencies
[UNIT_BEFORE
], i
)
474 (other
->job
->type
== JOB_STOP
||
475 other
->job
->type
== JOB_RESTART
))
478 /* This means that for a service a and a service b where b
479 * shall be started after a:
481 * start a + start b → 1st step start a, 2nd step start b
482 * start a + stop b → 1st step stop b, 2nd step start a
483 * stop a + start b → 1st step stop a, 2nd step start b
484 * stop a + stop b → 1st step stop b, 2nd step stop a
486 * This has the side effect that restarts are properly
487 * synchronized too. */
492 static void job_change_type(Job
*j
, JobType newtype
) {
495 log_unit_debug(j
->unit
,
496 "Converting job %s/%s -> %s/%s",
497 j
->unit
->id
, job_type_to_string(j
->type
),
498 j
->unit
->id
, job_type_to_string(newtype
));
503 static int job_perform_on_unit(Job
**j
) {
504 /* While we execute this operation the job might go away (for
505 * example: because it finishes immediately or is replaced by a new,
506 * conflicting job.) To make sure we don't access a freed job later on
507 * we store the id here, so that we can verify the job is still
509 Manager
*m
= (*j
)->manager
;
510 Unit
*u
= (*j
)->unit
;
511 JobType t
= (*j
)->type
;
512 uint32_t id
= (*j
)->id
;
531 assert_not_reached("Invalid job type");
534 /* Log if the job still exists and the start/stop/reload function
535 * actually did something. */
536 *j
= manager_get_job(m
, id
);
538 unit_status_emit_starting_stopping_reloading(u
, t
);
543 int job_run_and_invalidate(Job
*j
) {
547 assert(j
->installed
);
548 assert(j
->type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
549 assert(j
->in_run_queue
);
551 LIST_REMOVE(run_queue
, j
->manager
->run_queue
, j
);
552 j
->in_run_queue
= false;
554 if (j
->state
!= JOB_WAITING
)
557 if (!job_is_runnable(j
))
560 job_set_state(j
, JOB_RUNNING
);
561 job_add_to_dbus_queue(j
);
566 case JOB_VERIFY_ACTIVE
: {
567 UnitActiveState t
= unit_active_state(j
->unit
);
568 if (UNIT_IS_ACTIVE_OR_RELOADING(t
))
570 else if (t
== UNIT_ACTIVATING
)
580 r
= job_perform_on_unit(&j
);
582 /* If the unit type does not support starting/stopping,
583 * then simply wait. */
589 r
= job_perform_on_unit(&j
);
597 assert_not_reached("Unknown job type");
602 r
= job_finish_and_invalidate(j
, JOB_DONE
, true);
603 else if (r
== -EBADR
)
604 r
= job_finish_and_invalidate(j
, JOB_SKIPPED
, true);
605 else if (r
== -ENOEXEC
)
606 r
= job_finish_and_invalidate(j
, JOB_INVALID
, true);
607 else if (r
== -EPROTO
)
608 r
= job_finish_and_invalidate(j
, JOB_ASSERT
, true);
609 else if (r
== -EOPNOTSUPP
)
610 r
= job_finish_and_invalidate(j
, JOB_UNSUPPORTED
, true);
611 else if (r
== -EAGAIN
)
612 job_set_state(j
, JOB_WAITING
);
614 r
= job_finish_and_invalidate(j
, JOB_FAILED
, true);
620 _pure_
static const char *job_get_status_message_format(Unit
*u
, JobType t
, JobResult result
) {
622 const UnitStatusMessageFormats
*format_table
;
623 static const char *const generic_finished_start_job
[_JOB_RESULT_MAX
] = {
624 [JOB_DONE
] = "Started %s.",
625 [JOB_TIMEOUT
] = "Timed out starting %s.",
626 [JOB_FAILED
] = "Failed to start %s.",
627 [JOB_DEPENDENCY
] = "Dependency failed for %s.",
628 [JOB_ASSERT
] = "Assertion failed for %s.",
629 [JOB_UNSUPPORTED
] = "Starting of %s not supported.",
631 static const char *const generic_finished_stop_job
[_JOB_RESULT_MAX
] = {
632 [JOB_DONE
] = "Stopped %s.",
633 [JOB_FAILED
] = "Stopped (with error) %s.",
634 [JOB_TIMEOUT
] = "Timed out stoppping %s.",
636 static const char *const generic_finished_reload_job
[_JOB_RESULT_MAX
] = {
637 [JOB_DONE
] = "Reloaded %s.",
638 [JOB_FAILED
] = "Reload failed for %s.",
639 [JOB_TIMEOUT
] = "Timed out reloading %s.",
641 /* When verify-active detects the unit is inactive, report it.
642 * Most likely a DEPEND warning from a requisiting unit will
643 * occur next and it's nice to see what was requisited. */
644 static const char *const generic_finished_verify_active_job
[_JOB_RESULT_MAX
] = {
645 [JOB_SKIPPED
] = "%s is not active.",
650 assert(t
< _JOB_TYPE_MAX
);
652 if (t
== JOB_START
|| t
== JOB_STOP
|| t
== JOB_RESTART
) {
653 format_table
= &UNIT_VTABLE(u
)->status_message_formats
;
655 format
= t
== JOB_START
? format_table
->finished_start_job
[result
] :
656 format_table
->finished_stop_job
[result
];
662 /* Return generic strings */
664 return generic_finished_start_job
[result
];
665 else if (t
== JOB_STOP
|| t
== JOB_RESTART
)
666 return generic_finished_stop_job
[result
];
667 else if (t
== JOB_RELOAD
)
668 return generic_finished_reload_job
[result
];
669 else if (t
== JOB_VERIFY_ACTIVE
)
670 return generic_finished_verify_active_job
[result
];
675 static void job_print_status_message(Unit
*u
, JobType t
, JobResult result
) {
677 static const char* const job_result_status_table
[_JOB_RESULT_MAX
] = {
678 [JOB_DONE
] = ANSI_GREEN
" OK " ANSI_NORMAL
,
679 [JOB_TIMEOUT
] = ANSI_HIGHLIGHT_RED
" TIME " ANSI_NORMAL
,
680 [JOB_FAILED
] = ANSI_HIGHLIGHT_RED
"FAILED" ANSI_NORMAL
,
681 [JOB_DEPENDENCY
] = ANSI_HIGHLIGHT_YELLOW
"DEPEND" ANSI_NORMAL
,
682 [JOB_SKIPPED
] = ANSI_HIGHLIGHT
" INFO " ANSI_NORMAL
,
683 [JOB_ASSERT
] = ANSI_HIGHLIGHT_YELLOW
"ASSERT" ANSI_NORMAL
,
684 [JOB_UNSUPPORTED
] = ANSI_HIGHLIGHT_YELLOW
"UNSUPP" ANSI_NORMAL
,
689 assert(t
< _JOB_TYPE_MAX
);
691 format
= job_get_status_message_format(u
, t
, result
);
695 if (result
!= JOB_DONE
)
696 manager_flip_auto_status(u
->manager
, true);
698 DISABLE_WARNING_FORMAT_NONLITERAL
;
699 unit_status_printf(u
, job_result_status_table
[result
], format
);
702 if (t
== JOB_START
&& result
== JOB_FAILED
) {
703 _cleanup_free_
char *quoted
= shell_maybe_quote(u
->id
);
705 manager_status_printf(u
->manager
, STATUS_TYPE_NORMAL
, NULL
,
706 "See 'systemctl status %s' for details.", strna(quoted
));
710 static void job_log_status_message(Unit
*u
, JobType t
, JobResult result
) {
714 static const int job_result_log_level
[_JOB_RESULT_MAX
] = {
715 [JOB_DONE
] = LOG_INFO
,
716 [JOB_CANCELED
] = LOG_INFO
,
717 [JOB_TIMEOUT
] = LOG_ERR
,
718 [JOB_FAILED
] = LOG_ERR
,
719 [JOB_DEPENDENCY
] = LOG_WARNING
,
720 [JOB_SKIPPED
] = LOG_NOTICE
,
721 [JOB_INVALID
] = LOG_INFO
,
722 [JOB_ASSERT
] = LOG_WARNING
,
723 [JOB_UNSUPPORTED
] = LOG_WARNING
,
728 assert(t
< _JOB_TYPE_MAX
);
730 /* Skip this if it goes to the console. since we already print
731 * to the console anyway... */
733 if (log_on_console())
736 format
= job_get_status_message_format(u
, t
, result
);
740 DISABLE_WARNING_FORMAT_NONLITERAL
;
741 snprintf(buf
, sizeof(buf
), format
, unit_description(u
));
745 mid
= result
== JOB_DONE
? SD_MESSAGE_UNIT_STARTED
: SD_MESSAGE_UNIT_FAILED
;
746 else if (t
== JOB_STOP
|| t
== JOB_RESTART
)
747 mid
= SD_MESSAGE_UNIT_STOPPED
;
748 else if (t
== JOB_RELOAD
)
749 mid
= SD_MESSAGE_UNIT_RELOADED
;
751 log_struct(job_result_log_level
[result
],
753 LOG_MESSAGE("%s", buf
),
754 "RESULT=%s", job_result_to_string(result
),
759 log_struct(job_result_log_level
[result
],
762 LOG_MESSAGE("%s", buf
),
763 "RESULT=%s", job_result_to_string(result
),
767 static void job_emit_status_message(Unit
*u
, JobType t
, JobResult result
) {
769 /* No message if the job did not actually do anything due to failed condition. */
770 if (t
== JOB_START
&& result
== JOB_DONE
&& !u
->condition_result
)
773 job_log_status_message(u
, t
, result
);
775 /* Reload status messages have traditionally not been printed to console. */
777 job_print_status_message(u
, t
, result
);
780 static void job_fail_dependencies(Unit
*u
, UnitDependency d
) {
786 SET_FOREACH(other
, u
->dependencies
[d
], i
) {
791 if (!IN_SET(j
->type
, JOB_START
, JOB_VERIFY_ACTIVE
))
794 job_finish_and_invalidate(j
, JOB_DEPENDENCY
, true);
798 int job_finish_and_invalidate(Job
*j
, JobResult result
, bool recursive
) {
805 assert(j
->installed
);
806 assert(j
->type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
813 log_unit_debug(u
, "Job %s/%s finished, result=%s", u
->id
, job_type_to_string(t
), job_result_to_string(result
));
815 job_emit_status_message(u
, t
, result
);
817 job_add_to_dbus_queue(j
);
819 /* Patch restart jobs so that they become normal start jobs */
820 if (result
== JOB_DONE
&& t
== JOB_RESTART
) {
822 job_change_type(j
, JOB_START
);
823 job_set_state(j
, JOB_WAITING
);
825 job_add_to_run_queue(j
);
830 if (result
== JOB_FAILED
|| result
== JOB_INVALID
)
831 j
->manager
->n_failed_jobs
++;
836 /* Fail depending jobs on failure */
837 if (result
!= JOB_DONE
&& recursive
) {
838 if (IN_SET(t
, JOB_START
, JOB_VERIFY_ACTIVE
)) {
839 job_fail_dependencies(u
, UNIT_REQUIRED_BY
);
840 job_fail_dependencies(u
, UNIT_REQUISITE_OF
);
841 job_fail_dependencies(u
, UNIT_BOUND_BY
);
842 job_fail_dependencies(u
, UNIT_REQUIRED_BY_OVERRIDABLE
);
843 job_fail_dependencies(u
, UNIT_REQUISITE_OF_OVERRIDABLE
);
844 } else if (t
== JOB_STOP
)
845 job_fail_dependencies(u
, UNIT_CONFLICTED_BY
);
848 /* Trigger OnFailure dependencies that are not generated by
849 * the unit itself. We don't treat JOB_CANCELED as failure in
850 * this context. And JOB_FAILURE is already handled by the
852 if (result
== JOB_TIMEOUT
|| result
== JOB_DEPENDENCY
) {
853 log_struct(LOG_NOTICE
,
854 "JOB_TYPE=%s", job_type_to_string(t
),
855 "JOB_RESULT=%s", job_result_to_string(result
),
857 LOG_UNIT_MESSAGE(u
, "Job %s/%s failed with result '%s'.",
859 job_type_to_string(t
),
860 job_result_to_string(result
)),
863 unit_start_on_failure(u
);
866 unit_trigger_notify(u
);
869 /* Try to start the next jobs that can be started */
870 SET_FOREACH(other
, u
->dependencies
[UNIT_AFTER
], i
)
872 job_add_to_run_queue(other
->job
);
873 SET_FOREACH(other
, u
->dependencies
[UNIT_BEFORE
], i
)
875 job_add_to_run_queue(other
->job
);
877 manager_check_finished(u
->manager
);
882 static int job_dispatch_timer(sd_event_source
*s
, uint64_t monotonic
, void *userdata
) {
887 assert(s
== j
->timer_event_source
);
889 log_unit_warning(j
->unit
, "Job %s/%s timed out.", j
->unit
->id
, job_type_to_string(j
->type
));
892 job_finish_and_invalidate(j
, JOB_TIMEOUT
, true);
894 failure_action(u
->manager
, u
->job_timeout_action
, u
->job_timeout_reboot_arg
);
899 int job_start_timer(Job
*j
) {
902 if (j
->timer_event_source
)
905 j
->begin_usec
= now(CLOCK_MONOTONIC
);
907 if (j
->unit
->job_timeout
<= 0)
910 r
= sd_event_add_time(
912 &j
->timer_event_source
,
914 j
->begin_usec
+ j
->unit
->job_timeout
, 0,
915 job_dispatch_timer
, j
);
919 (void) sd_event_source_set_description(j
->timer_event_source
, "job-start");
924 void job_add_to_run_queue(Job
*j
) {
926 assert(j
->installed
);
931 if (!j
->manager
->run_queue
)
932 sd_event_source_set_enabled(j
->manager
->run_queue_event_source
, SD_EVENT_ONESHOT
);
934 LIST_PREPEND(run_queue
, j
->manager
->run_queue
, j
);
935 j
->in_run_queue
= true;
938 void job_add_to_dbus_queue(Job
*j
) {
940 assert(j
->installed
);
942 if (j
->in_dbus_queue
)
945 /* We don't check if anybody is subscribed here, since this
946 * job might just have been created and not yet assigned to a
947 * connection/client. */
949 LIST_PREPEND(dbus_queue
, j
->manager
->dbus_job_queue
, j
);
950 j
->in_dbus_queue
= true;
953 char *job_dbus_path(Job
*j
) {
958 if (asprintf(&p
, "/org/freedesktop/systemd1/job/%"PRIu32
, j
->id
) < 0)
964 int job_serialize(Job
*j
, FILE *f
, FDSet
*fds
) {
965 fprintf(f
, "job-id=%u\n", j
->id
);
966 fprintf(f
, "job-type=%s\n", job_type_to_string(j
->type
));
967 fprintf(f
, "job-state=%s\n", job_state_to_string(j
->state
));
968 fprintf(f
, "job-override=%s\n", yes_no(j
->override
));
969 fprintf(f
, "job-irreversible=%s\n", yes_no(j
->irreversible
));
970 fprintf(f
, "job-sent-dbus-new-signal=%s\n", yes_no(j
->sent_dbus_new_signal
));
971 fprintf(f
, "job-ignore-order=%s\n", yes_no(j
->ignore_order
));
973 if (j
->begin_usec
> 0)
974 fprintf(f
, "job-begin="USEC_FMT
"\n", j
->begin_usec
);
976 bus_track_serialize(j
->clients
, f
);
983 int job_deserialize(Job
*j
, FILE *f
, FDSet
*fds
) {
987 char line
[LINE_MAX
], *l
, *v
;
990 if (!fgets(line
, sizeof(line
), f
)) {
1003 k
= strcspn(l
, "=");
1011 if (streq(l
, "job-id")) {
1013 if (safe_atou32(v
, &j
->id
) < 0)
1014 log_debug("Failed to parse job id value %s", v
);
1016 } else if (streq(l
, "job-type")) {
1019 t
= job_type_from_string(v
);
1021 log_debug("Failed to parse job type %s", v
);
1022 else if (t
>= _JOB_TYPE_MAX_IN_TRANSACTION
)
1023 log_debug("Cannot deserialize job of type %s", v
);
1027 } else if (streq(l
, "job-state")) {
1030 s
= job_state_from_string(v
);
1032 log_debug("Failed to parse job state %s", v
);
1034 job_set_state(j
, s
);
1036 } else if (streq(l
, "job-override")) {
1039 b
= parse_boolean(v
);
1041 log_debug("Failed to parse job override flag %s", v
);
1043 j
->override
= j
->override
|| b
;
1045 } else if (streq(l
, "job-irreversible")) {
1048 b
= parse_boolean(v
);
1050 log_debug("Failed to parse job irreversible flag %s", v
);
1052 j
->irreversible
= j
->irreversible
|| b
;
1054 } else if (streq(l
, "job-sent-dbus-new-signal")) {
1057 b
= parse_boolean(v
);
1059 log_debug("Failed to parse job sent_dbus_new_signal flag %s", v
);
1061 j
->sent_dbus_new_signal
= j
->sent_dbus_new_signal
|| b
;
1063 } else if (streq(l
, "job-ignore-order")) {
1066 b
= parse_boolean(v
);
1068 log_debug("Failed to parse job ignore_order flag %s", v
);
1070 j
->ignore_order
= j
->ignore_order
|| b
;
1072 } else if (streq(l
, "job-begin")) {
1073 unsigned long long ull
;
1075 if (sscanf(v
, "%llu", &ull
) != 1)
1076 log_debug("Failed to parse job-begin value %s", v
);
1078 j
->begin_usec
= ull
;
1080 } else if (streq(l
, "subscribed")) {
1082 if (strv_extend(&j
->deserialized_clients
, v
) < 0)
1088 int job_coldplug(Job
*j
) {
1093 /* After deserialization is complete and the bus connection
1094 * set up again, let's start watching our subscribers again */
1095 r
= bus_track_coldplug(j
->manager
, &j
->clients
, &j
->deserialized_clients
);
1099 if (j
->state
== JOB_WAITING
)
1100 job_add_to_run_queue(j
);
1102 if (j
->begin_usec
== 0 || j
->unit
->job_timeout
== 0)
1105 if (j
->timer_event_source
)
1106 j
->timer_event_source
= sd_event_source_unref(j
->timer_event_source
);
1108 r
= sd_event_add_time(
1110 &j
->timer_event_source
,
1112 j
->begin_usec
+ j
->unit
->job_timeout
, 0,
1113 job_dispatch_timer
, j
);
1115 log_debug_errno(r
, "Failed to restart timeout for job: %m");
1117 (void) sd_event_source_set_description(j
->timer_event_source
, "job-timeout");
1122 void job_shutdown_magic(Job
*j
) {
1125 /* The shutdown target gets some special treatment here: we
1126 * tell the kernel to begin with flushing its disk caches, to
1127 * optimize shutdown time a bit. Ideally we wouldn't hardcode
1128 * this magic into PID 1. However all other processes aren't
1129 * options either since they'd exit much sooner than PID 1 and
1130 * asynchronous sync() would cause their exit to be
1133 if (j
->type
!= JOB_START
)
1136 if (j
->unit
->manager
->running_as
!= MANAGER_SYSTEM
)
1139 if (!unit_has_name(j
->unit
, SPECIAL_SHUTDOWN_TARGET
))
1142 /* In case messages on console has been disabled on boot */
1143 j
->unit
->manager
->no_console_output
= false;
1145 if (detect_container() > 0)
1148 asynchronous_sync();
1151 int job_get_timeout(Job
*j
, uint64_t *timeout
) {
1153 uint64_t x
= -1, y
= -1;
1158 if (j
->timer_event_source
) {
1159 r
= sd_event_source_get_time(j
->timer_event_source
, &x
);
1165 if (UNIT_VTABLE(u
)->get_timeout
) {
1166 q
= UNIT_VTABLE(u
)->get_timeout(u
, &y
);
1171 if (r
== 0 && q
== 0)
1174 *timeout
= MIN(x
, y
);
1179 static const char* const job_state_table
[_JOB_STATE_MAX
] = {
1180 [JOB_WAITING
] = "waiting",
1181 [JOB_RUNNING
] = "running"
1184 DEFINE_STRING_TABLE_LOOKUP(job_state
, JobState
);
1186 static const char* const job_type_table
[_JOB_TYPE_MAX
] = {
1187 [JOB_START
] = "start",
1188 [JOB_VERIFY_ACTIVE
] = "verify-active",
1189 [JOB_STOP
] = "stop",
1190 [JOB_RELOAD
] = "reload",
1191 [JOB_RELOAD_OR_START
] = "reload-or-start",
1192 [JOB_RESTART
] = "restart",
1193 [JOB_TRY_RESTART
] = "try-restart",
1197 DEFINE_STRING_TABLE_LOOKUP(job_type
, JobType
);
1199 static const char* const job_mode_table
[_JOB_MODE_MAX
] = {
1200 [JOB_FAIL
] = "fail",
1201 [JOB_REPLACE
] = "replace",
1202 [JOB_REPLACE_IRREVERSIBLY
] = "replace-irreversibly",
1203 [JOB_ISOLATE
] = "isolate",
1204 [JOB_FLUSH
] = "flush",
1205 [JOB_IGNORE_DEPENDENCIES
] = "ignore-dependencies",
1206 [JOB_IGNORE_REQUIREMENTS
] = "ignore-requirements",
1209 DEFINE_STRING_TABLE_LOOKUP(job_mode
, JobMode
);
1211 static const char* const job_result_table
[_JOB_RESULT_MAX
] = {
1212 [JOB_DONE
] = "done",
1213 [JOB_CANCELED
] = "canceled",
1214 [JOB_TIMEOUT
] = "timeout",
1215 [JOB_FAILED
] = "failed",
1216 [JOB_DEPENDENCY
] = "dependency",
1217 [JOB_SKIPPED
] = "skipped",
1218 [JOB_INVALID
] = "invalid",
1219 [JOB_ASSERT
] = "assert",
1220 [JOB_UNSUPPORTED
] = "unsupported",
1223 DEFINE_STRING_TABLE_LOOKUP(job_result
, JobResult
);