2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "sd-messages.h"
25 #include "alloc-util.h"
33 #include "parse-util.h"
36 #include "stdio-util.h"
37 #include "string-table.h"
38 #include "string-util.h"
40 #include "terminal-util.h"
44 Job
* job_new_raw(Unit
*unit
) {
47 /* used for deserialization */
55 j
->manager
= unit
->manager
;
57 j
->type
= _JOB_TYPE_INVALID
;
62 Job
* job_new(Unit
*unit
, JobType type
) {
65 assert(type
< _JOB_TYPE_MAX
);
67 j
= job_new_raw(unit
);
71 j
->id
= j
->manager
->current_job_id
++;
74 /* We don't link it here, that's what job_dependency() is for */
79 void job_free(Job
*j
) {
81 assert(!j
->installed
);
82 assert(!j
->transaction_prev
);
83 assert(!j
->transaction_next
);
84 assert(!j
->subject_list
);
85 assert(!j
->object_list
);
88 LIST_REMOVE(run_queue
, j
->manager
->run_queue
, j
);
91 LIST_REMOVE(dbus_queue
, j
->manager
->dbus_job_queue
, j
);
93 sd_event_source_unref(j
->timer_event_source
);
95 sd_bus_track_unref(j
->clients
);
96 strv_free(j
->deserialized_clients
);
101 static void job_set_state(Job
*j
, JobState state
) {
104 assert(state
< _JOB_STATE_MAX
);
106 if (j
->state
== state
)
114 if (j
->state
== JOB_RUNNING
)
115 j
->unit
->manager
->n_running_jobs
++;
117 assert(j
->state
== JOB_WAITING
);
118 assert(j
->unit
->manager
->n_running_jobs
> 0);
120 j
->unit
->manager
->n_running_jobs
--;
122 if (j
->unit
->manager
->n_running_jobs
<= 0)
123 j
->unit
->manager
->jobs_in_progress_event_source
= sd_event_source_unref(j
->unit
->manager
->jobs_in_progress_event_source
);
127 void job_uninstall(Job
*j
) {
130 assert(j
->installed
);
132 job_set_state(j
, JOB_WAITING
);
134 pj
= (j
->type
== JOB_NOP
) ? &j
->unit
->nop_job
: &j
->unit
->job
;
137 /* Detach from next 'bigger' objects */
139 /* daemon-reload should be transparent to job observers */
140 if (j
->manager
->n_reloading
<= 0)
141 bus_job_send_removed_signal(j
);
145 unit_add_to_gc_queue(j
->unit
);
147 hashmap_remove(j
->manager
->jobs
, UINT32_TO_PTR(j
->id
));
148 j
->installed
= false;
151 static bool job_type_allows_late_merge(JobType t
) {
152 /* Tells whether it is OK to merge a job of type 't' with an already
154 * Reloads cannot be merged this way. Think of the sequence:
155 * 1. Reload of a daemon is in progress; the daemon has already loaded
156 * its config file, but hasn't completed the reload operation yet.
157 * 2. Edit foo's config file.
158 * 3. Trigger another reload to have the daemon use the new config.
159 * Should the second reload job be merged into the first one, the daemon
160 * would not know about the new config.
161 * JOB_RESTART jobs on the other hand can be merged, because they get
162 * patched into JOB_START after stopping the unit. So if we see a
163 * JOB_RESTART running, it means the unit hasn't stopped yet and at
164 * this time the merge is still allowed. */
165 return t
!= JOB_RELOAD
;
168 static void job_merge_into_installed(Job
*j
, Job
*other
) {
169 assert(j
->installed
);
170 assert(j
->unit
== other
->unit
);
172 if (j
->type
!= JOB_NOP
)
173 job_type_merge_and_collapse(&j
->type
, other
->type
, j
->unit
);
175 assert(other
->type
== JOB_NOP
);
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"
308 "%s\tIrreversible: %s\n",
310 prefix
, j
->unit
->id
, job_type_to_string(j
->type
),
311 prefix
, job_state_to_string(j
->state
),
312 prefix
, yes_no(j
->irreversible
));
316 * Merging is commutative, so imagine the matrix as symmetric. We store only
317 * its lower triangle to avoid duplication. We don't store the main diagonal,
318 * because A merged with A is simply A.
320 * If the resulting type is collapsed immediately afterwards (to get rid of
321 * the JOB_RELOAD_OR_START, which lies outside the lookup function's domain),
322 * the following properties hold:
324 * Merging is associative! A merged with B, and then merged with C is the same
325 * as A merged with the result of B merged with C.
327 * Mergeability is transitive! If A can be merged with B and B with C then
330 * Also, if A merged with B cannot be merged with C, then either A or B cannot
331 * be merged with C either.
333 static const JobType job_merging_table
[] = {
334 /* What \ With * JOB_START JOB_VERIFY_ACTIVE JOB_STOP JOB_RELOAD */
335 /*********************************************************************************/
337 /*JOB_VERIFY_ACTIVE */ JOB_START
,
338 /*JOB_STOP */ -1, -1,
339 /*JOB_RELOAD */ JOB_RELOAD_OR_START
, JOB_RELOAD
, -1,
340 /*JOB_RESTART */ JOB_RESTART
, JOB_RESTART
, -1, JOB_RESTART
,
343 JobType
job_type_lookup_merge(JobType a
, JobType b
) {
344 assert_cc(ELEMENTSOF(job_merging_table
) == _JOB_TYPE_MAX_MERGING
* (_JOB_TYPE_MAX_MERGING
- 1) / 2);
345 assert(a
>= 0 && a
< _JOB_TYPE_MAX_MERGING
);
346 assert(b
>= 0 && b
< _JOB_TYPE_MAX_MERGING
);
357 return job_merging_table
[(a
- 1) * a
/ 2 + b
];
360 bool job_type_is_redundant(JobType a
, UnitActiveState b
) {
370 b
== UNIT_INACTIVE
||
373 case JOB_VERIFY_ACTIVE
:
384 b
== UNIT_ACTIVATING
;
390 assert_not_reached("Invalid job type");
394 JobType
job_type_collapse(JobType t
, Unit
*u
) {
399 case JOB_TRY_RESTART
:
400 s
= unit_active_state(u
);
401 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s
))
407 s
= unit_active_state(u
);
408 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s
))
413 case JOB_RELOAD_OR_START
:
414 s
= unit_active_state(u
);
415 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s
))
425 int job_type_merge_and_collapse(JobType
*a
, JobType b
, Unit
*u
) {
428 t
= job_type_lookup_merge(*a
, b
);
432 *a
= job_type_collapse(t
, u
);
436 static bool job_is_runnable(Job
*j
) {
441 assert(j
->installed
);
443 /* Checks whether there is any job running for the units this
444 * job needs to be running after (in the case of a 'positive'
445 * job type) or before (in the case of a 'negative' job
448 /* Note that unit types have a say in what is runnable,
449 * too. For example, if they return -EAGAIN from
450 * unit_start() they can indicate they are not
453 /* First check if there is an override */
457 if (j
->type
== JOB_NOP
)
460 if (j
->type
== JOB_START
||
461 j
->type
== JOB_VERIFY_ACTIVE
||
462 j
->type
== JOB_RELOAD
) {
464 /* Immediate result is that the job is or might be
465 * started. In this case let's wait for the
466 * dependencies, regardless whether they are
467 * starting or stopping something. */
469 SET_FOREACH(other
, j
->unit
->dependencies
[UNIT_AFTER
], i
)
474 /* Also, if something else is being stopped and we should
475 * change state after it, then let's wait. */
477 SET_FOREACH(other
, j
->unit
->dependencies
[UNIT_BEFORE
], i
)
479 (other
->job
->type
== JOB_STOP
||
480 other
->job
->type
== JOB_RESTART
))
483 /* This means that for a service a and a service b where b
484 * shall be started after a:
486 * start a + start b → 1st step start a, 2nd step start b
487 * start a + stop b → 1st step stop b, 2nd step start a
488 * stop a + start b → 1st step stop a, 2nd step start b
489 * stop a + stop b → 1st step stop b, 2nd step stop a
491 * This has the side effect that restarts are properly
492 * synchronized too. */
497 static void job_change_type(Job
*j
, JobType newtype
) {
500 log_unit_debug(j
->unit
,
501 "Converting job %s/%s -> %s/%s",
502 j
->unit
->id
, job_type_to_string(j
->type
),
503 j
->unit
->id
, job_type_to_string(newtype
));
508 static int job_perform_on_unit(Job
**j
) {
515 /* While we execute this operation the job might go away (for
516 * example: because it finishes immediately or is replaced by
517 * a new, conflicting job.) To make sure we don't access a
518 * freed job later on we store the id here, so that we can
519 * verify the job is still valid. */
546 assert_not_reached("Invalid job type");
549 /* Log if the job still exists and the start/stop/reload function
550 * actually did something. */
551 *j
= manager_get_job(m
, id
);
553 unit_status_emit_starting_stopping_reloading(u
, t
);
558 int job_run_and_invalidate(Job
*j
) {
562 assert(j
->installed
);
563 assert(j
->type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
564 assert(j
->in_run_queue
);
566 LIST_REMOVE(run_queue
, j
->manager
->run_queue
, j
);
567 j
->in_run_queue
= false;
569 if (j
->state
!= JOB_WAITING
)
572 if (!job_is_runnable(j
))
575 job_set_state(j
, JOB_RUNNING
);
576 job_add_to_dbus_queue(j
);
581 case JOB_VERIFY_ACTIVE
: {
582 UnitActiveState t
= unit_active_state(j
->unit
);
583 if (UNIT_IS_ACTIVE_OR_RELOADING(t
))
585 else if (t
== UNIT_ACTIVATING
)
595 r
= job_perform_on_unit(&j
);
597 /* If the unit type does not support starting/stopping,
598 * then simply wait. */
604 r
= job_perform_on_unit(&j
);
612 assert_not_reached("Unknown job type");
617 r
= job_finish_and_invalidate(j
, JOB_DONE
, true);
618 else if (r
== -EBADR
)
619 r
= job_finish_and_invalidate(j
, JOB_SKIPPED
, true);
620 else if (r
== -ENOEXEC
)
621 r
= job_finish_and_invalidate(j
, JOB_INVALID
, true);
622 else if (r
== -EPROTO
)
623 r
= job_finish_and_invalidate(j
, JOB_ASSERT
, true);
624 else if (r
== -EOPNOTSUPP
)
625 r
= job_finish_and_invalidate(j
, JOB_UNSUPPORTED
, true);
626 else if (r
== -EAGAIN
)
627 job_set_state(j
, JOB_WAITING
);
629 r
= job_finish_and_invalidate(j
, JOB_FAILED
, true);
635 _pure_
static const char *job_get_status_message_format(Unit
*u
, JobType t
, JobResult result
) {
637 static const char *const generic_finished_start_job
[_JOB_RESULT_MAX
] = {
638 [JOB_DONE
] = "Started %s.",
639 [JOB_TIMEOUT
] = "Timed out starting %s.",
640 [JOB_FAILED
] = "Failed to start %s.",
641 [JOB_DEPENDENCY
] = "Dependency failed for %s.",
642 [JOB_ASSERT
] = "Assertion failed for %s.",
643 [JOB_UNSUPPORTED
] = "Starting of %s not supported.",
645 static const char *const generic_finished_stop_job
[_JOB_RESULT_MAX
] = {
646 [JOB_DONE
] = "Stopped %s.",
647 [JOB_FAILED
] = "Stopped (with error) %s.",
648 [JOB_TIMEOUT
] = "Timed out stoppping %s.",
650 static const char *const generic_finished_reload_job
[_JOB_RESULT_MAX
] = {
651 [JOB_DONE
] = "Reloaded %s.",
652 [JOB_FAILED
] = "Reload failed for %s.",
653 [JOB_TIMEOUT
] = "Timed out reloading %s.",
655 /* When verify-active detects the unit is inactive, report it.
656 * Most likely a DEPEND warning from a requisiting unit will
657 * occur next and it's nice to see what was requisited. */
658 static const char *const generic_finished_verify_active_job
[_JOB_RESULT_MAX
] = {
659 [JOB_SKIPPED
] = "%s is not active.",
662 const UnitStatusMessageFormats
*format_table
;
667 assert(t
< _JOB_TYPE_MAX
);
669 if (IN_SET(t
, JOB_START
, JOB_STOP
, JOB_RESTART
)) {
670 format_table
= &UNIT_VTABLE(u
)->status_message_formats
;
672 format
= t
== JOB_START
? format_table
->finished_start_job
[result
] :
673 format_table
->finished_stop_job
[result
];
679 /* Return generic strings */
681 return generic_finished_start_job
[result
];
682 else if (t
== JOB_STOP
|| t
== JOB_RESTART
)
683 return generic_finished_stop_job
[result
];
684 else if (t
== JOB_RELOAD
)
685 return generic_finished_reload_job
[result
];
686 else if (t
== JOB_VERIFY_ACTIVE
)
687 return generic_finished_verify_active_job
[result
];
692 static void job_print_status_message(Unit
*u
, JobType t
, JobResult result
) {
693 static const char* const job_result_status_table
[_JOB_RESULT_MAX
] = {
694 [JOB_DONE
] = ANSI_GREEN
" OK " ANSI_NORMAL
,
695 [JOB_TIMEOUT
] = ANSI_HIGHLIGHT_RED
" TIME " ANSI_NORMAL
,
696 [JOB_FAILED
] = ANSI_HIGHLIGHT_RED
"FAILED" ANSI_NORMAL
,
697 [JOB_DEPENDENCY
] = ANSI_HIGHLIGHT_YELLOW
"DEPEND" ANSI_NORMAL
,
698 [JOB_SKIPPED
] = ANSI_HIGHLIGHT
" INFO " ANSI_NORMAL
,
699 [JOB_ASSERT
] = ANSI_HIGHLIGHT_YELLOW
"ASSERT" ANSI_NORMAL
,
700 [JOB_UNSUPPORTED
] = ANSI_HIGHLIGHT_YELLOW
"UNSUPP" ANSI_NORMAL
,
707 assert(t
< _JOB_TYPE_MAX
);
709 /* Reload status messages have traditionally not been printed to console. */
713 format
= job_get_status_message_format(u
, t
, result
);
717 if (result
!= JOB_DONE
)
718 manager_flip_auto_status(u
->manager
, true);
720 DISABLE_WARNING_FORMAT_NONLITERAL
;
721 unit_status_printf(u
, job_result_status_table
[result
], format
);
724 if (t
== JOB_START
&& result
== JOB_FAILED
) {
725 _cleanup_free_
char *quoted
;
727 quoted
= shell_maybe_quote(u
->id
);
728 manager_status_printf(u
->manager
, STATUS_TYPE_NORMAL
, NULL
, "See 'systemctl status %s' for details.", strna(quoted
));
732 static void job_log_status_message(Unit
*u
, JobType t
, JobResult result
) {
736 static const int job_result_log_level
[_JOB_RESULT_MAX
] = {
737 [JOB_DONE
] = LOG_INFO
,
738 [JOB_CANCELED
] = LOG_INFO
,
739 [JOB_TIMEOUT
] = LOG_ERR
,
740 [JOB_FAILED
] = LOG_ERR
,
741 [JOB_DEPENDENCY
] = LOG_WARNING
,
742 [JOB_SKIPPED
] = LOG_NOTICE
,
743 [JOB_INVALID
] = LOG_INFO
,
744 [JOB_ASSERT
] = LOG_WARNING
,
745 [JOB_UNSUPPORTED
] = LOG_WARNING
,
750 assert(t
< _JOB_TYPE_MAX
);
752 /* Skip this if it goes to the console. since we already print
753 * to the console anyway... */
755 if (log_on_console())
758 format
= job_get_status_message_format(u
, t
, result
);
762 DISABLE_WARNING_FORMAT_NONLITERAL
;
763 xsprintf(buf
, format
, unit_description(u
));
769 mid
= result
== JOB_DONE
? SD_MESSAGE_UNIT_STARTED
: SD_MESSAGE_UNIT_FAILED
;
773 mid
= SD_MESSAGE_UNIT_RELOADED
;
778 mid
= SD_MESSAGE_UNIT_STOPPED
;
782 log_struct(job_result_log_level
[result
],
784 LOG_MESSAGE("%s", buf
),
785 "RESULT=%s", job_result_to_string(result
),
790 log_struct(job_result_log_level
[result
],
793 LOG_MESSAGE("%s", buf
),
794 "RESULT=%s", job_result_to_string(result
),
798 static void job_emit_status_message(Unit
*u
, JobType t
, JobResult result
) {
800 /* No message if the job did not actually do anything due to failed condition. */
801 if (t
== JOB_START
&& result
== JOB_DONE
&& !u
->condition_result
)
804 job_log_status_message(u
, t
, result
);
805 job_print_status_message(u
, t
, result
);
808 static void job_fail_dependencies(Unit
*u
, UnitDependency d
) {
814 SET_FOREACH(other
, u
->dependencies
[d
], i
) {
819 if (!IN_SET(j
->type
, JOB_START
, JOB_VERIFY_ACTIVE
))
822 job_finish_and_invalidate(j
, JOB_DEPENDENCY
, true);
826 int job_finish_and_invalidate(Job
*j
, JobResult result
, bool recursive
) {
833 assert(j
->installed
);
834 assert(j
->type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
841 log_unit_debug(u
, "Job %s/%s finished, result=%s", u
->id
, job_type_to_string(t
), job_result_to_string(result
));
843 job_emit_status_message(u
, t
, result
);
845 job_add_to_dbus_queue(j
);
847 /* Patch restart jobs so that they become normal start jobs */
848 if (result
== JOB_DONE
&& t
== JOB_RESTART
) {
850 job_change_type(j
, JOB_START
);
851 job_set_state(j
, JOB_WAITING
);
853 job_add_to_run_queue(j
);
858 if (result
== JOB_FAILED
|| result
== JOB_INVALID
)
859 j
->manager
->n_failed_jobs
++;
864 /* Fail depending jobs on failure */
865 if (result
!= JOB_DONE
&& recursive
) {
866 if (IN_SET(t
, JOB_START
, JOB_VERIFY_ACTIVE
)) {
867 job_fail_dependencies(u
, UNIT_REQUIRED_BY
);
868 job_fail_dependencies(u
, UNIT_REQUISITE_OF
);
869 job_fail_dependencies(u
, UNIT_BOUND_BY
);
870 } else if (t
== JOB_STOP
)
871 job_fail_dependencies(u
, UNIT_CONFLICTED_BY
);
874 /* Trigger OnFailure dependencies that are not generated by
875 * the unit itself. We don't treat JOB_CANCELED as failure in
876 * this context. And JOB_FAILURE is already handled by the
878 if (result
== JOB_TIMEOUT
|| result
== JOB_DEPENDENCY
) {
879 log_struct(LOG_NOTICE
,
880 "JOB_TYPE=%s", job_type_to_string(t
),
881 "JOB_RESULT=%s", job_result_to_string(result
),
883 LOG_UNIT_MESSAGE(u
, "Job %s/%s failed with result '%s'.",
885 job_type_to_string(t
),
886 job_result_to_string(result
)),
889 unit_start_on_failure(u
);
892 unit_trigger_notify(u
);
895 /* Try to start the next jobs that can be started */
896 SET_FOREACH(other
, u
->dependencies
[UNIT_AFTER
], i
)
898 job_add_to_run_queue(other
->job
);
899 SET_FOREACH(other
, u
->dependencies
[UNIT_BEFORE
], i
)
901 job_add_to_run_queue(other
->job
);
903 manager_check_finished(u
->manager
);
908 static int job_dispatch_timer(sd_event_source
*s
, uint64_t monotonic
, void *userdata
) {
913 assert(s
== j
->timer_event_source
);
915 log_unit_warning(j
->unit
, "Job %s/%s timed out.", j
->unit
->id
, job_type_to_string(j
->type
));
918 job_finish_and_invalidate(j
, JOB_TIMEOUT
, true);
920 failure_action(u
->manager
, u
->job_timeout_action
, u
->job_timeout_reboot_arg
);
925 int job_start_timer(Job
*j
) {
928 if (j
->timer_event_source
)
931 j
->begin_usec
= now(CLOCK_MONOTONIC
);
933 if (j
->unit
->job_timeout
== USEC_INFINITY
)
936 r
= sd_event_add_time(
938 &j
->timer_event_source
,
940 usec_add(j
->begin_usec
, j
->unit
->job_timeout
), 0,
941 job_dispatch_timer
, j
);
945 (void) sd_event_source_set_description(j
->timer_event_source
, "job-start");
950 void job_add_to_run_queue(Job
*j
) {
952 assert(j
->installed
);
957 if (!j
->manager
->run_queue
)
958 sd_event_source_set_enabled(j
->manager
->run_queue_event_source
, SD_EVENT_ONESHOT
);
960 LIST_PREPEND(run_queue
, j
->manager
->run_queue
, j
);
961 j
->in_run_queue
= true;
964 void job_add_to_dbus_queue(Job
*j
) {
966 assert(j
->installed
);
968 if (j
->in_dbus_queue
)
971 /* We don't check if anybody is subscribed here, since this
972 * job might just have been created and not yet assigned to a
973 * connection/client. */
975 LIST_PREPEND(dbus_queue
, j
->manager
->dbus_job_queue
, j
);
976 j
->in_dbus_queue
= true;
979 char *job_dbus_path(Job
*j
) {
984 if (asprintf(&p
, "/org/freedesktop/systemd1/job/%"PRIu32
, j
->id
) < 0)
990 int job_serialize(Job
*j
, FILE *f
, FDSet
*fds
) {
991 fprintf(f
, "job-id=%u\n", j
->id
);
992 fprintf(f
, "job-type=%s\n", job_type_to_string(j
->type
));
993 fprintf(f
, "job-state=%s\n", job_state_to_string(j
->state
));
994 fprintf(f
, "job-irreversible=%s\n", yes_no(j
->irreversible
));
995 fprintf(f
, "job-sent-dbus-new-signal=%s\n", yes_no(j
->sent_dbus_new_signal
));
996 fprintf(f
, "job-ignore-order=%s\n", yes_no(j
->ignore_order
));
998 if (j
->begin_usec
> 0)
999 fprintf(f
, "job-begin="USEC_FMT
"\n", j
->begin_usec
);
1001 bus_track_serialize(j
->clients
, f
);
1008 int job_deserialize(Job
*j
, FILE *f
, FDSet
*fds
) {
1012 char line
[LINE_MAX
], *l
, *v
;
1015 if (!fgets(line
, sizeof(line
), f
)) {
1028 k
= strcspn(l
, "=");
1036 if (streq(l
, "job-id")) {
1038 if (safe_atou32(v
, &j
->id
) < 0)
1039 log_debug("Failed to parse job id value %s", v
);
1041 } else if (streq(l
, "job-type")) {
1044 t
= job_type_from_string(v
);
1046 log_debug("Failed to parse job type %s", v
);
1047 else if (t
>= _JOB_TYPE_MAX_IN_TRANSACTION
)
1048 log_debug("Cannot deserialize job of type %s", v
);
1052 } else if (streq(l
, "job-state")) {
1055 s
= job_state_from_string(v
);
1057 log_debug("Failed to parse job state %s", v
);
1059 job_set_state(j
, s
);
1061 } else if (streq(l
, "job-irreversible")) {
1064 b
= parse_boolean(v
);
1066 log_debug("Failed to parse job irreversible flag %s", v
);
1068 j
->irreversible
= j
->irreversible
|| b
;
1070 } else if (streq(l
, "job-sent-dbus-new-signal")) {
1073 b
= parse_boolean(v
);
1075 log_debug("Failed to parse job sent_dbus_new_signal flag %s", v
);
1077 j
->sent_dbus_new_signal
= j
->sent_dbus_new_signal
|| b
;
1079 } else if (streq(l
, "job-ignore-order")) {
1082 b
= parse_boolean(v
);
1084 log_debug("Failed to parse job ignore_order flag %s", v
);
1086 j
->ignore_order
= j
->ignore_order
|| b
;
1088 } else if (streq(l
, "job-begin")) {
1089 unsigned long long ull
;
1091 if (sscanf(v
, "%llu", &ull
) != 1)
1092 log_debug("Failed to parse job-begin value %s", v
);
1094 j
->begin_usec
= ull
;
1096 } else if (streq(l
, "subscribed")) {
1098 if (strv_extend(&j
->deserialized_clients
, v
) < 0)
1104 int job_coldplug(Job
*j
) {
1109 /* After deserialization is complete and the bus connection
1110 * set up again, let's start watching our subscribers again */
1111 r
= bus_track_coldplug(j
->manager
, &j
->clients
, &j
->deserialized_clients
);
1115 if (j
->state
== JOB_WAITING
)
1116 job_add_to_run_queue(j
);
1118 if (j
->begin_usec
== 0 || j
->unit
->job_timeout
== USEC_INFINITY
)
1121 j
->timer_event_source
= sd_event_source_unref(j
->timer_event_source
);
1123 r
= sd_event_add_time(
1125 &j
->timer_event_source
,
1127 usec_add(j
->begin_usec
, j
->unit
->job_timeout
), 0,
1128 job_dispatch_timer
, j
);
1130 log_debug_errno(r
, "Failed to restart timeout for job: %m");
1132 (void) sd_event_source_set_description(j
->timer_event_source
, "job-timeout");
1137 void job_shutdown_magic(Job
*j
) {
1140 /* The shutdown target gets some special treatment here: we
1141 * tell the kernel to begin with flushing its disk caches, to
1142 * optimize shutdown time a bit. Ideally we wouldn't hardcode
1143 * this magic into PID 1. However all other processes aren't
1144 * options either since they'd exit much sooner than PID 1 and
1145 * asynchronous sync() would cause their exit to be
1148 if (j
->type
!= JOB_START
)
1151 if (j
->unit
->manager
->running_as
!= MANAGER_SYSTEM
)
1154 if (!unit_has_name(j
->unit
, SPECIAL_SHUTDOWN_TARGET
))
1157 /* In case messages on console has been disabled on boot */
1158 j
->unit
->manager
->no_console_output
= false;
1160 if (detect_container() > 0)
1163 asynchronous_sync();
1166 int job_get_timeout(Job
*j
, usec_t
*timeout
) {
1167 usec_t x
= USEC_INFINITY
, y
= USEC_INFINITY
;
1173 if (j
->timer_event_source
) {
1174 r
= sd_event_source_get_time(j
->timer_event_source
, &x
);
1179 if (UNIT_VTABLE(u
)->get_timeout
) {
1180 r
= UNIT_VTABLE(u
)->get_timeout(u
, &y
);
1185 if (x
== USEC_INFINITY
&& y
== USEC_INFINITY
)
1188 *timeout
= MIN(x
, y
);
1192 static const char* const job_state_table
[_JOB_STATE_MAX
] = {
1193 [JOB_WAITING
] = "waiting",
1194 [JOB_RUNNING
] = "running"
1197 DEFINE_STRING_TABLE_LOOKUP(job_state
, JobState
);
1199 static const char* const job_type_table
[_JOB_TYPE_MAX
] = {
1200 [JOB_START
] = "start",
1201 [JOB_VERIFY_ACTIVE
] = "verify-active",
1202 [JOB_STOP
] = "stop",
1203 [JOB_RELOAD
] = "reload",
1204 [JOB_RELOAD_OR_START
] = "reload-or-start",
1205 [JOB_RESTART
] = "restart",
1206 [JOB_TRY_RESTART
] = "try-restart",
1207 [JOB_TRY_RELOAD
] = "try-reload",
1211 DEFINE_STRING_TABLE_LOOKUP(job_type
, JobType
);
1213 static const char* const job_mode_table
[_JOB_MODE_MAX
] = {
1214 [JOB_FAIL
] = "fail",
1215 [JOB_REPLACE
] = "replace",
1216 [JOB_REPLACE_IRREVERSIBLY
] = "replace-irreversibly",
1217 [JOB_ISOLATE
] = "isolate",
1218 [JOB_FLUSH
] = "flush",
1219 [JOB_IGNORE_DEPENDENCIES
] = "ignore-dependencies",
1220 [JOB_IGNORE_REQUIREMENTS
] = "ignore-requirements",
1223 DEFINE_STRING_TABLE_LOOKUP(job_mode
, JobMode
);
1225 static const char* const job_result_table
[_JOB_RESULT_MAX
] = {
1226 [JOB_DONE
] = "done",
1227 [JOB_CANCELED
] = "canceled",
1228 [JOB_TIMEOUT
] = "timeout",
1229 [JOB_FAILED
] = "failed",
1230 [JOB_DEPENDENCY
] = "dependency",
1231 [JOB_SKIPPED
] = "skipped",
1232 [JOB_INVALID
] = "invalid",
1233 [JOB_ASSERT
] = "assert",
1234 [JOB_UNSUPPORTED
] = "unsupported",
1237 DEFINE_STRING_TABLE_LOOKUP(job_result
, JobResult
);
1239 const char* job_type_to_access_method(JobType t
) {
1241 assert(t
< _JOB_TYPE_MAX
);
1243 if (IN_SET(t
, JOB_START
, JOB_RESTART
, JOB_TRY_RESTART
))
1245 else if (t
== JOB_STOP
)