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"
36 #include "terminal-util.h"
38 Job
* job_new_raw(Unit
*unit
) {
41 /* used for deserialization */
49 j
->manager
= unit
->manager
;
51 j
->type
= _JOB_TYPE_INVALID
;
56 Job
* job_new(Unit
*unit
, JobType type
) {
59 assert(type
< _JOB_TYPE_MAX
);
61 j
= job_new_raw(unit
);
65 j
->id
= j
->manager
->current_job_id
++;
68 /* We don't link it here, that's what job_dependency() is for */
73 void job_free(Job
*j
) {
75 assert(!j
->installed
);
76 assert(!j
->transaction_prev
);
77 assert(!j
->transaction_next
);
78 assert(!j
->subject_list
);
79 assert(!j
->object_list
);
82 LIST_REMOVE(run_queue
, j
->manager
->run_queue
, j
);
85 LIST_REMOVE(dbus_queue
, j
->manager
->dbus_job_queue
, j
);
87 sd_event_source_unref(j
->timer_event_source
);
89 sd_bus_track_unref(j
->clients
);
90 strv_free(j
->deserialized_clients
);
95 static void job_set_state(Job
*j
, JobState state
) {
98 assert(state
< _JOB_STATE_MAX
);
100 if (j
->state
== state
)
108 if (j
->state
== JOB_RUNNING
)
109 j
->unit
->manager
->n_running_jobs
++;
111 assert(j
->state
== JOB_WAITING
);
112 assert(j
->unit
->manager
->n_running_jobs
> 0);
114 j
->unit
->manager
->n_running_jobs
--;
116 if (j
->unit
->manager
->n_running_jobs
<= 0)
117 j
->unit
->manager
->jobs_in_progress_event_source
= sd_event_source_unref(j
->unit
->manager
->jobs_in_progress_event_source
);
121 void job_uninstall(Job
*j
) {
124 assert(j
->installed
);
126 job_set_state(j
, JOB_WAITING
);
128 pj
= (j
->type
== JOB_NOP
) ? &j
->unit
->nop_job
: &j
->unit
->job
;
131 /* Detach from next 'bigger' objects */
133 /* daemon-reload should be transparent to job observers */
134 if (j
->manager
->n_reloading
<= 0)
135 bus_job_send_removed_signal(j
);
139 unit_add_to_gc_queue(j
->unit
);
141 hashmap_remove(j
->manager
->jobs
, UINT32_TO_PTR(j
->id
));
142 j
->installed
= false;
145 static bool job_type_allows_late_merge(JobType t
) {
146 /* Tells whether it is OK to merge a job of type 't' with an already
148 * Reloads cannot be merged this way. Think of the sequence:
149 * 1. Reload of a daemon is in progress; the daemon has already loaded
150 * its config file, but hasn't completed the reload operation yet.
151 * 2. Edit foo's config file.
152 * 3. Trigger another reload to have the daemon use the new config.
153 * Should the second reload job be merged into the first one, the daemon
154 * would not know about the new config.
155 * JOB_RESTART jobs on the other hand can be merged, because they get
156 * patched into JOB_START after stopping the unit. So if we see a
157 * JOB_RESTART running, it means the unit hasn't stopped yet and at
158 * this time the merge is still allowed. */
159 return t
!= JOB_RELOAD
;
162 static void job_merge_into_installed(Job
*j
, Job
*other
) {
163 assert(j
->installed
);
164 assert(j
->unit
== other
->unit
);
166 if (j
->type
!= JOB_NOP
)
167 job_type_merge_and_collapse(&j
->type
, other
->type
, j
->unit
);
169 assert(other
->type
== JOB_NOP
);
171 j
->override
= j
->override
|| other
->override
;
172 j
->irreversible
= j
->irreversible
|| other
->irreversible
;
173 j
->ignore_order
= j
->ignore_order
|| other
->ignore_order
;
176 Job
* job_install(Job
*j
) {
180 assert(!j
->installed
);
181 assert(j
->type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
182 assert(j
->state
== JOB_WAITING
);
184 pj
= (j
->type
== JOB_NOP
) ? &j
->unit
->nop_job
: &j
->unit
->job
;
188 if (job_type_is_conflicting(uj
->type
, j
->type
))
189 job_finish_and_invalidate(uj
, JOB_CANCELED
, false);
191 /* not conflicting, i.e. mergeable */
193 if (uj
->state
== JOB_WAITING
||
194 (job_type_allows_late_merge(j
->type
) && job_type_is_superset(uj
->type
, j
->type
))) {
195 job_merge_into_installed(uj
, j
);
196 log_unit_debug(uj
->unit
,
197 "Merged into installed job %s/%s as %u",
198 uj
->unit
->id
, job_type_to_string(uj
->type
), (unsigned) uj
->id
);
201 /* already running and not safe to merge into */
202 /* Patch uj to become a merged job and re-run it. */
203 /* XXX It should be safer to queue j to run after uj finishes, but it is
204 * not currently possible to have more than one installed job per unit. */
205 job_merge_into_installed(uj
, j
);
206 log_unit_debug(uj
->unit
,
207 "Merged into running job, re-running: %s/%s as %u",
208 uj
->unit
->id
, job_type_to_string(uj
->type
), (unsigned) uj
->id
);
210 job_set_state(uj
, JOB_WAITING
);
216 /* Install the job */
220 j
->manager
->n_installed_jobs
++;
221 log_unit_debug(j
->unit
,
222 "Installed new job %s/%s as %u",
223 j
->unit
->id
, job_type_to_string(j
->type
), (unsigned) j
->id
);
227 int job_install_deserialized(Job
*j
) {
230 assert(!j
->installed
);
232 if (j
->type
< 0 || j
->type
>= _JOB_TYPE_MAX_IN_TRANSACTION
) {
233 log_debug("Invalid job type %s in deserialization.", strna(job_type_to_string(j
->type
)));
237 pj
= (j
->type
== JOB_NOP
) ? &j
->unit
->nop_job
: &j
->unit
->job
;
239 log_unit_debug(j
->unit
, "Unit already has a job installed. Not installing deserialized job.");
246 if (j
->state
== JOB_RUNNING
)
247 j
->unit
->manager
->n_running_jobs
++;
249 log_unit_debug(j
->unit
,
250 "Reinstalled deserialized job %s/%s as %u",
251 j
->unit
->id
, job_type_to_string(j
->type
), (unsigned) j
->id
);
255 JobDependency
* job_dependency_new(Job
*subject
, Job
*object
, bool matters
, bool conflicts
) {
260 /* Adds a new job link, which encodes that the 'subject' job
261 * needs the 'object' job in some way. If 'subject' is NULL
262 * this means the 'anchor' job (i.e. the one the user
263 * explicitly asked for) is the requester. */
265 if (!(l
= new0(JobDependency
, 1)))
268 l
->subject
= subject
;
270 l
->matters
= matters
;
271 l
->conflicts
= conflicts
;
274 LIST_PREPEND(subject
, subject
->subject_list
, l
);
276 LIST_PREPEND(object
, object
->object_list
, l
);
281 void job_dependency_free(JobDependency
*l
) {
285 LIST_REMOVE(subject
, l
->subject
->subject_list
, l
);
287 LIST_REMOVE(object
, l
->object
->object_list
, l
);
292 void job_dump(Job
*j
, FILE*f
, const char *prefix
) {
301 "%s\tAction: %s -> %s\n"
304 "%s\tIrreversible: %s\n",
306 prefix
, j
->unit
->id
, job_type_to_string(j
->type
),
307 prefix
, job_state_to_string(j
->state
),
308 prefix
, yes_no(j
->override
),
309 prefix
, yes_no(j
->irreversible
));
313 * Merging is commutative, so imagine the matrix as symmetric. We store only
314 * its lower triangle to avoid duplication. We don't store the main diagonal,
315 * because A merged with A is simply A.
317 * If the resulting type is collapsed immediately afterwards (to get rid of
318 * the JOB_RELOAD_OR_START, which lies outside the lookup function's domain),
319 * the following properties hold:
321 * Merging is associative! A merged with B, and then merged with C is the same
322 * as A merged with the result of B merged with C.
324 * Mergeability is transitive! If A can be merged with B and B with C then
327 * Also, if A merged with B cannot be merged with C, then either A or B cannot
328 * be merged with C either.
330 static const JobType job_merging_table
[] = {
331 /* What \ With * JOB_START JOB_VERIFY_ACTIVE JOB_STOP JOB_RELOAD */
332 /*********************************************************************************/
334 /*JOB_VERIFY_ACTIVE */ JOB_START
,
335 /*JOB_STOP */ -1, -1,
336 /*JOB_RELOAD */ JOB_RELOAD_OR_START
, JOB_RELOAD
, -1,
337 /*JOB_RESTART */ JOB_RESTART
, JOB_RESTART
, -1, JOB_RESTART
,
340 JobType
job_type_lookup_merge(JobType a
, JobType b
) {
341 assert_cc(ELEMENTSOF(job_merging_table
) == _JOB_TYPE_MAX_MERGING
* (_JOB_TYPE_MAX_MERGING
- 1) / 2);
342 assert(a
>= 0 && a
< _JOB_TYPE_MAX_MERGING
);
343 assert(b
>= 0 && b
< _JOB_TYPE_MAX_MERGING
);
354 return job_merging_table
[(a
- 1) * a
/ 2 + b
];
357 bool job_type_is_redundant(JobType a
, UnitActiveState b
) {
367 b
== UNIT_INACTIVE
||
370 case JOB_VERIFY_ACTIVE
:
381 b
== UNIT_ACTIVATING
;
387 assert_not_reached("Invalid job type");
391 void job_type_collapse(JobType
*t
, Unit
*u
) {
396 case JOB_TRY_RESTART
:
397 s
= unit_active_state(u
);
398 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s
))
404 case JOB_RELOAD_OR_START
:
405 s
= unit_active_state(u
);
406 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s
))
417 int job_type_merge_and_collapse(JobType
*a
, JobType b
, Unit
*u
) {
418 JobType t
= job_type_lookup_merge(*a
, b
);
422 job_type_collapse(a
, u
);
426 static bool job_is_runnable(Job
*j
) {
431 assert(j
->installed
);
433 /* Checks whether there is any job running for the units this
434 * job needs to be running after (in the case of a 'positive'
435 * job type) or before (in the case of a 'negative' job
438 /* Note that unit types have a say in what is runnable,
439 * too. For example, if they return -EAGAIN from
440 * unit_start() they can indicate they are not
443 /* First check if there is an override */
447 if (j
->type
== JOB_NOP
)
450 if (j
->type
== JOB_START
||
451 j
->type
== JOB_VERIFY_ACTIVE
||
452 j
->type
== JOB_RELOAD
) {
454 /* Immediate result is that the job is or might be
455 * started. In this case lets wait for the
456 * dependencies, regardless whether they are
457 * starting or stopping something. */
459 SET_FOREACH(other
, j
->unit
->dependencies
[UNIT_AFTER
], i
)
464 /* Also, if something else is being stopped and we should
465 * change state after it, then lets wait. */
467 SET_FOREACH(other
, j
->unit
->dependencies
[UNIT_BEFORE
], i
)
469 (other
->job
->type
== JOB_STOP
||
470 other
->job
->type
== JOB_RESTART
))
473 /* This means that for a service a and a service b where b
474 * shall be started after a:
476 * start a + start b → 1st step start a, 2nd step start b
477 * start a + stop b → 1st step stop b, 2nd step start a
478 * stop a + start b → 1st step stop a, 2nd step start b
479 * stop a + stop b → 1st step stop b, 2nd step stop a
481 * This has the side effect that restarts are properly
482 * synchronized too. */
487 static void job_change_type(Job
*j
, JobType newtype
) {
490 log_unit_debug(j
->unit
,
491 "Converting job %s/%s -> %s/%s",
492 j
->unit
->id
, job_type_to_string(j
->type
),
493 j
->unit
->id
, job_type_to_string(newtype
));
498 int job_run_and_invalidate(Job
*j
) {
501 Manager
*m
= j
->manager
;
504 assert(j
->installed
);
505 assert(j
->type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
506 assert(j
->in_run_queue
);
508 LIST_REMOVE(run_queue
, j
->manager
->run_queue
, j
);
509 j
->in_run_queue
= false;
511 if (j
->state
!= JOB_WAITING
)
514 if (!job_is_runnable(j
))
517 job_set_state(j
, JOB_RUNNING
);
518 job_add_to_dbus_queue(j
);
520 /* While we execute this operation the job might go away (for
521 * example: because it is replaced by a new, conflicting
522 * job.) To make sure we don't access a freed job later on we
523 * store the id here, so that we can verify the job is still
530 r
= unit_start(j
->unit
);
532 /* If this unit cannot be started, then simply wait */
537 case JOB_VERIFY_ACTIVE
: {
538 UnitActiveState t
= unit_active_state(j
->unit
);
539 if (UNIT_IS_ACTIVE_OR_RELOADING(t
))
541 else if (t
== UNIT_ACTIVATING
)
550 r
= unit_stop(j
->unit
);
552 /* If this unit cannot stopped, then simply wait. */
558 r
= unit_reload(j
->unit
);
566 assert_not_reached("Unknown job type");
569 j
= manager_get_job(m
, id
);
572 r
= job_finish_and_invalidate(j
, JOB_DONE
, true);
573 else if (r
== -EBADR
)
574 r
= job_finish_and_invalidate(j
, JOB_SKIPPED
, true);
575 else if (r
== -ENOEXEC
)
576 r
= job_finish_and_invalidate(j
, JOB_INVALID
, true);
577 else if (r
== -EPROTO
)
578 r
= job_finish_and_invalidate(j
, JOB_ASSERT
, true);
579 else if (r
== -EOPNOTSUPP
)
580 r
= job_finish_and_invalidate(j
, JOB_UNSUPPORTED
, true);
581 else if (r
== -EAGAIN
)
582 job_set_state(j
, JOB_WAITING
);
584 r
= job_finish_and_invalidate(j
, JOB_FAILED
, true);
590 _pure_
static const char *job_get_status_message_format(Unit
*u
, JobType t
, JobResult result
) {
591 const UnitStatusMessageFormats
*format_table
;
595 assert(t
< _JOB_TYPE_MAX
);
597 format_table
= &UNIT_VTABLE(u
)->status_message_formats
;
602 return format_table
->finished_start_job
[result
];
603 else if (t
== JOB_STOP
|| t
== JOB_RESTART
)
604 return format_table
->finished_stop_job
[result
];
609 _pure_
static const char *job_get_status_message_format_try_harder(Unit
*u
, JobType t
, JobResult result
) {
614 assert(t
< _JOB_TYPE_MAX
);
616 format
= job_get_status_message_format(u
, t
, result
);
620 /* Return generic strings */
621 if (t
== JOB_START
) {
622 if (result
== JOB_DONE
)
623 return "Started %s.";
624 else if (result
== JOB_TIMEOUT
)
625 return "Timed out starting %s.";
626 else if (result
== JOB_FAILED
)
627 return "Failed to start %s.";
628 else if (result
== JOB_DEPENDENCY
)
629 return "Dependency failed for %s.";
630 else if (result
== JOB_ASSERT
)
631 return "Assertion failed for %s.";
632 else if (result
== JOB_UNSUPPORTED
)
633 return "Starting of %s not supported.";
634 } else if (t
== JOB_STOP
|| t
== JOB_RESTART
) {
635 if (result
== JOB_DONE
)
636 return "Stopped %s.";
637 else if (result
== JOB_FAILED
)
638 return "Stopped (with error) %s.";
639 else if (result
== JOB_TIMEOUT
)
640 return "Timed out stoppping %s.";
641 } else if (t
== JOB_RELOAD
) {
642 if (result
== JOB_DONE
)
643 return "Reloaded %s.";
644 else if (result
== JOB_FAILED
)
645 return "Reload failed for %s.";
646 else if (result
== JOB_TIMEOUT
)
647 return "Timed out reloading %s.";
653 static void job_print_status_message(Unit
*u
, JobType t
, JobResult result
) {
658 assert(t
< _JOB_TYPE_MAX
);
660 DISABLE_WARNING_FORMAT_NONLITERAL
;
662 if (t
== JOB_START
) {
663 format
= job_get_status_message_format(u
, t
, result
);
670 if (u
->condition_result
)
671 unit_status_printf(u
, ANSI_GREEN_ON
" OK " ANSI_HIGHLIGHT_OFF
, format
);
675 manager_flip_auto_status(u
->manager
, true);
676 unit_status_printf(u
, ANSI_HIGHLIGHT_RED_ON
" TIME " ANSI_HIGHLIGHT_OFF
, format
);
680 _cleanup_free_
char *quoted
= NULL
;
682 quoted
= shell_maybe_quote(u
->id
);
684 manager_flip_auto_status(u
->manager
, true);
685 unit_status_printf(u
, ANSI_HIGHLIGHT_RED_ON
"FAILED" ANSI_HIGHLIGHT_OFF
, format
);
686 manager_status_printf(u
->manager
, STATUS_TYPE_NORMAL
, NULL
, "See 'systemctl status %s' for details.", strna(quoted
));
691 manager_flip_auto_status(u
->manager
, true);
692 unit_status_printf(u
, ANSI_HIGHLIGHT_YELLOW_ON
"DEPEND" ANSI_HIGHLIGHT_OFF
, format
);
696 manager_flip_auto_status(u
->manager
, true);
697 unit_status_printf(u
, ANSI_HIGHLIGHT_YELLOW_ON
"ASSERT" ANSI_HIGHLIGHT_OFF
, format
);
700 case JOB_UNSUPPORTED
:
701 manager_flip_auto_status(u
->manager
, true);
702 unit_status_printf(u
, ANSI_HIGHLIGHT_YELLOW_ON
"UNSUPP" ANSI_HIGHLIGHT_OFF
, format
);
709 } else if (t
== JOB_STOP
|| t
== JOB_RESTART
) {
711 format
= job_get_status_message_format(u
, t
, result
);
718 manager_flip_auto_status(u
->manager
, true);
719 unit_status_printf(u
, ANSI_HIGHLIGHT_RED_ON
" TIME " ANSI_HIGHLIGHT_OFF
, format
);
724 unit_status_printf(u
, ANSI_GREEN_ON
" OK " ANSI_HIGHLIGHT_OFF
, format
);
731 } else if (t
== JOB_VERIFY_ACTIVE
) {
733 /* When verify-active detects the unit is inactive, report it.
734 * Most likely a DEPEND warning from a requisiting unit will
735 * occur next and it's nice to see what was requisited. */
736 if (result
== JOB_SKIPPED
)
737 unit_status_printf(u
, ANSI_HIGHLIGHT_ON
" INFO " ANSI_HIGHLIGHT_OFF
, "%s is not active.");
743 static void job_log_status_message(Unit
*u
, JobType t
, JobResult result
) {
749 assert(t
< _JOB_TYPE_MAX
);
751 /* Skip this if it goes to the console. since we already print
752 * to the console anyway... */
754 if (log_on_console())
757 format
= job_get_status_message_format_try_harder(u
, t
, result
);
761 DISABLE_WARNING_FORMAT_NONLITERAL
;
762 snprintf(buf
, sizeof(buf
), format
, unit_description(u
));
765 if (t
== JOB_START
) {
768 mid
= result
== JOB_DONE
? SD_MESSAGE_UNIT_STARTED
: SD_MESSAGE_UNIT_FAILED
;
769 log_struct(result
== JOB_DONE
? LOG_INFO
: LOG_ERR
,
772 LOG_MESSAGE("%s", buf
),
773 "RESULT=%s", job_result_to_string(result
),
776 } else if (t
== JOB_STOP
)
777 log_struct(result
== JOB_DONE
? LOG_INFO
: LOG_ERR
,
778 LOG_MESSAGE_ID(SD_MESSAGE_UNIT_STOPPED
),
780 LOG_MESSAGE("%s", buf
),
781 "RESULT=%s", job_result_to_string(result
),
784 else if (t
== JOB_RELOAD
)
785 log_struct(result
== JOB_DONE
? LOG_INFO
: LOG_ERR
,
786 LOG_MESSAGE_ID(SD_MESSAGE_UNIT_RELOADED
),
788 LOG_MESSAGE("%s", buf
),
789 "RESULT=%s", job_result_to_string(result
),
793 int job_finish_and_invalidate(Job
*j
, JobResult result
, bool recursive
) {
800 assert(j
->installed
);
801 assert(j
->type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
808 log_unit_debug(u
, "Job %s/%s finished, result=%s", u
->id
, job_type_to_string(t
), job_result_to_string(result
));
810 job_print_status_message(u
, t
, result
);
811 job_log_status_message(u
, t
, result
);
813 job_add_to_dbus_queue(j
);
815 /* Patch restart jobs so that they become normal start jobs */
816 if (result
== JOB_DONE
&& t
== JOB_RESTART
) {
818 job_change_type(j
, JOB_START
);
819 job_set_state(j
, JOB_WAITING
);
821 job_add_to_run_queue(j
);
826 if (result
== JOB_FAILED
|| result
== JOB_INVALID
)
827 j
->manager
->n_failed_jobs
++;
832 /* Fail depending jobs on failure */
833 if (result
!= JOB_DONE
&& recursive
) {
835 if (t
== JOB_START
||
836 t
== JOB_VERIFY_ACTIVE
) {
838 SET_FOREACH(other
, u
->dependencies
[UNIT_REQUIRED_BY
], i
)
840 (other
->job
->type
== JOB_START
||
841 other
->job
->type
== JOB_VERIFY_ACTIVE
))
842 job_finish_and_invalidate(other
->job
, JOB_DEPENDENCY
, true);
844 SET_FOREACH(other
, u
->dependencies
[UNIT_BOUND_BY
], i
)
846 (other
->job
->type
== JOB_START
||
847 other
->job
->type
== JOB_VERIFY_ACTIVE
))
848 job_finish_and_invalidate(other
->job
, JOB_DEPENDENCY
, true);
850 SET_FOREACH(other
, u
->dependencies
[UNIT_REQUIRED_BY_OVERRIDABLE
], i
)
852 !other
->job
->override
&&
853 (other
->job
->type
== JOB_START
||
854 other
->job
->type
== JOB_VERIFY_ACTIVE
))
855 job_finish_and_invalidate(other
->job
, JOB_DEPENDENCY
, true);
857 } else if (t
== JOB_STOP
) {
859 SET_FOREACH(other
, u
->dependencies
[UNIT_CONFLICTED_BY
], i
)
861 (other
->job
->type
== JOB_START
||
862 other
->job
->type
== JOB_VERIFY_ACTIVE
))
863 job_finish_and_invalidate(other
->job
, JOB_DEPENDENCY
, true);
867 /* Trigger OnFailure dependencies that are not generated by
868 * the unit itself. We don't treat JOB_CANCELED as failure in
869 * this context. And JOB_FAILURE is already handled by the
871 if (result
== JOB_TIMEOUT
|| result
== JOB_DEPENDENCY
) {
872 log_struct(LOG_NOTICE
,
873 "JOB_TYPE=%s", job_type_to_string(t
),
874 "JOB_RESULT=%s", job_result_to_string(result
),
876 LOG_UNIT_MESSAGE(u
, "Job %s/%s failed with result '%s'.",
878 job_type_to_string(t
),
879 job_result_to_string(result
)),
882 unit_start_on_failure(u
);
885 unit_trigger_notify(u
);
888 /* Try to start the next jobs that can be started */
889 SET_FOREACH(other
, u
->dependencies
[UNIT_AFTER
], i
)
891 job_add_to_run_queue(other
->job
);
892 SET_FOREACH(other
, u
->dependencies
[UNIT_BEFORE
], i
)
894 job_add_to_run_queue(other
->job
);
896 manager_check_finished(u
->manager
);
901 static int job_dispatch_timer(sd_event_source
*s
, uint64_t monotonic
, void *userdata
) {
906 assert(s
== j
->timer_event_source
);
908 log_unit_warning(j
->unit
, "Job %s/%s timed out.", j
->unit
->id
, job_type_to_string(j
->type
));
911 job_finish_and_invalidate(j
, JOB_TIMEOUT
, true);
913 failure_action(u
->manager
, u
->job_timeout_action
, u
->job_timeout_reboot_arg
);
918 int job_start_timer(Job
*j
) {
921 if (j
->timer_event_source
)
924 j
->begin_usec
= now(CLOCK_MONOTONIC
);
926 if (j
->unit
->job_timeout
<= 0)
929 r
= sd_event_add_time(
931 &j
->timer_event_source
,
933 j
->begin_usec
+ j
->unit
->job_timeout
, 0,
934 job_dispatch_timer
, j
);
938 (void) sd_event_source_set_description(j
->timer_event_source
, "job-start");
943 void job_add_to_run_queue(Job
*j
) {
945 assert(j
->installed
);
950 if (!j
->manager
->run_queue
)
951 sd_event_source_set_enabled(j
->manager
->run_queue_event_source
, SD_EVENT_ONESHOT
);
953 LIST_PREPEND(run_queue
, j
->manager
->run_queue
, j
);
954 j
->in_run_queue
= true;
957 void job_add_to_dbus_queue(Job
*j
) {
959 assert(j
->installed
);
961 if (j
->in_dbus_queue
)
964 /* We don't check if anybody is subscribed here, since this
965 * job might just have been created and not yet assigned to a
966 * connection/client. */
968 LIST_PREPEND(dbus_queue
, j
->manager
->dbus_job_queue
, j
);
969 j
->in_dbus_queue
= true;
972 char *job_dbus_path(Job
*j
) {
977 if (asprintf(&p
, "/org/freedesktop/systemd1/job/%"PRIu32
, j
->id
) < 0)
983 int job_serialize(Job
*j
, FILE *f
, FDSet
*fds
) {
984 fprintf(f
, "job-id=%u\n", j
->id
);
985 fprintf(f
, "job-type=%s\n", job_type_to_string(j
->type
));
986 fprintf(f
, "job-state=%s\n", job_state_to_string(j
->state
));
987 fprintf(f
, "job-override=%s\n", yes_no(j
->override
));
988 fprintf(f
, "job-irreversible=%s\n", yes_no(j
->irreversible
));
989 fprintf(f
, "job-sent-dbus-new-signal=%s\n", yes_no(j
->sent_dbus_new_signal
));
990 fprintf(f
, "job-ignore-order=%s\n", yes_no(j
->ignore_order
));
992 if (j
->begin_usec
> 0)
993 fprintf(f
, "job-begin="USEC_FMT
"\n", j
->begin_usec
);
995 bus_track_serialize(j
->clients
, f
);
1002 int job_deserialize(Job
*j
, FILE *f
, FDSet
*fds
) {
1006 char line
[LINE_MAX
], *l
, *v
;
1009 if (!fgets(line
, sizeof(line
), f
)) {
1022 k
= strcspn(l
, "=");
1030 if (streq(l
, "job-id")) {
1032 if (safe_atou32(v
, &j
->id
) < 0)
1033 log_debug("Failed to parse job id value %s", v
);
1035 } else if (streq(l
, "job-type")) {
1038 t
= job_type_from_string(v
);
1040 log_debug("Failed to parse job type %s", v
);
1041 else if (t
>= _JOB_TYPE_MAX_IN_TRANSACTION
)
1042 log_debug("Cannot deserialize job of type %s", v
);
1046 } else if (streq(l
, "job-state")) {
1049 s
= job_state_from_string(v
);
1051 log_debug("Failed to parse job state %s", v
);
1053 job_set_state(j
, s
);
1055 } else if (streq(l
, "job-override")) {
1058 b
= parse_boolean(v
);
1060 log_debug("Failed to parse job override flag %s", v
);
1062 j
->override
= j
->override
|| b
;
1064 } else if (streq(l
, "job-irreversible")) {
1067 b
= parse_boolean(v
);
1069 log_debug("Failed to parse job irreversible flag %s", v
);
1071 j
->irreversible
= j
->irreversible
|| b
;
1073 } else if (streq(l
, "job-sent-dbus-new-signal")) {
1076 b
= parse_boolean(v
);
1078 log_debug("Failed to parse job sent_dbus_new_signal flag %s", v
);
1080 j
->sent_dbus_new_signal
= j
->sent_dbus_new_signal
|| b
;
1082 } else if (streq(l
, "job-ignore-order")) {
1085 b
= parse_boolean(v
);
1087 log_debug("Failed to parse job ignore_order flag %s", v
);
1089 j
->ignore_order
= j
->ignore_order
|| b
;
1091 } else if (streq(l
, "job-begin")) {
1092 unsigned long long ull
;
1094 if (sscanf(v
, "%llu", &ull
) != 1)
1095 log_debug("Failed to parse job-begin value %s", v
);
1097 j
->begin_usec
= ull
;
1099 } else if (streq(l
, "subscribed")) {
1101 if (strv_extend(&j
->deserialized_clients
, v
) < 0)
1107 int job_coldplug(Job
*j
) {
1112 /* After deserialization is complete and the bus connection
1113 * set up again, let's start watching our subscribers again */
1114 r
= bus_track_coldplug(j
->manager
, &j
->clients
, &j
->deserialized_clients
);
1118 if (j
->state
== JOB_WAITING
)
1119 job_add_to_run_queue(j
);
1121 if (j
->begin_usec
== 0 || j
->unit
->job_timeout
== 0)
1124 if (j
->timer_event_source
)
1125 j
->timer_event_source
= sd_event_source_unref(j
->timer_event_source
);
1127 r
= sd_event_add_time(
1129 &j
->timer_event_source
,
1131 j
->begin_usec
+ j
->unit
->job_timeout
, 0,
1132 job_dispatch_timer
, j
);
1134 log_debug_errno(r
, "Failed to restart timeout for job: %m");
1136 (void) sd_event_source_set_description(j
->timer_event_source
, "job-timeout");
1141 void job_shutdown_magic(Job
*j
) {
1144 /* The shutdown target gets some special treatment here: we
1145 * tell the kernel to begin with flushing its disk caches, to
1146 * optimize shutdown time a bit. Ideally we wouldn't hardcode
1147 * this magic into PID 1. However all other processes aren't
1148 * options either since they'd exit much sooner than PID 1 and
1149 * asynchronous sync() would cause their exit to be
1152 if (j
->type
!= JOB_START
)
1155 if (j
->unit
->manager
->running_as
!= SYSTEMD_SYSTEM
)
1158 if (!unit_has_name(j
->unit
, SPECIAL_SHUTDOWN_TARGET
))
1161 /* In case messages on console has been disabled on boot */
1162 j
->unit
->manager
->no_console_output
= false;
1164 if (detect_container(NULL
) > 0)
1167 asynchronous_sync();
1170 int job_get_timeout(Job
*j
, uint64_t *timeout
) {
1172 uint64_t x
= -1, y
= -1;
1177 if (j
->timer_event_source
) {
1178 r
= sd_event_source_get_time(j
->timer_event_source
, &x
);
1184 if (UNIT_VTABLE(u
)->get_timeout
) {
1185 q
= UNIT_VTABLE(u
)->get_timeout(u
, &y
);
1190 if (r
== 0 && q
== 0)
1193 *timeout
= MIN(x
, y
);
1198 static const char* const job_state_table
[_JOB_STATE_MAX
] = {
1199 [JOB_WAITING
] = "waiting",
1200 [JOB_RUNNING
] = "running"
1203 DEFINE_STRING_TABLE_LOOKUP(job_state
, JobState
);
1205 static const char* const job_type_table
[_JOB_TYPE_MAX
] = {
1206 [JOB_START
] = "start",
1207 [JOB_VERIFY_ACTIVE
] = "verify-active",
1208 [JOB_STOP
] = "stop",
1209 [JOB_RELOAD
] = "reload",
1210 [JOB_RELOAD_OR_START
] = "reload-or-start",
1211 [JOB_RESTART
] = "restart",
1212 [JOB_TRY_RESTART
] = "try-restart",
1216 DEFINE_STRING_TABLE_LOOKUP(job_type
, JobType
);
1218 static const char* const job_mode_table
[_JOB_MODE_MAX
] = {
1219 [JOB_FAIL
] = "fail",
1220 [JOB_REPLACE
] = "replace",
1221 [JOB_REPLACE_IRREVERSIBLY
] = "replace-irreversibly",
1222 [JOB_ISOLATE
] = "isolate",
1223 [JOB_FLUSH
] = "flush",
1224 [JOB_IGNORE_DEPENDENCIES
] = "ignore-dependencies",
1225 [JOB_IGNORE_REQUIREMENTS
] = "ignore-requirements",
1228 DEFINE_STRING_TABLE_LOOKUP(job_mode
, JobMode
);
1230 static const char* const job_result_table
[_JOB_RESULT_MAX
] = {
1231 [JOB_DONE
] = "done",
1232 [JOB_CANCELED
] = "canceled",
1233 [JOB_TIMEOUT
] = "timeout",
1234 [JOB_FAILED
] = "failed",
1235 [JOB_DEPENDENCY
] = "dependency",
1236 [JOB_SKIPPED
] = "skipped",
1237 [JOB_INVALID
] = "invalid",
1238 [JOB_ASSERT
] = "assert",
1239 [JOB_UNSUPPORTED
] = "unsupported",
1242 DEFINE_STRING_TABLE_LOOKUP(job_result
, JobResult
);