]>
git.ipfire.org Git - people/ms/systemd.git/blob - job.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
10 #include "load-fragment.h"
11 #include "load-dropin.h"
14 Job
* job_new(Manager
*m
, JobType type
, Unit
*unit
) {
18 assert(type
< _JOB_TYPE_MAX
);
21 if (!(j
= new0(Job
, 1)))
25 j
->id
= m
->current_job_id
++;
29 /* We don't link it here, that's what job_dependency() is for */
34 void job_free(Job
*j
) {
37 /* Detach from next 'bigger' objects */
39 if (j
->unit
->meta
.job
== j
)
40 j
->unit
->meta
.job
= NULL
;
42 hashmap_remove(j
->manager
->jobs
, UINT32_TO_PTR(j
->id
));
46 /* Detach from next 'smaller' objects */
47 manager_transaction_unlink_job(j
->manager
, j
);
52 JobDependency
* job_dependency_new(Job
*subject
, Job
*object
, bool matters
) {
57 /* Adds a new job link, which encodes that the 'subject' job
58 * needs the 'object' job in some way. If 'subject' is NULL
59 * this means the 'anchor' job (i.e. the one the user
60 * explcitily asked for) is the requester. */
62 if (!(l
= new0(JobDependency
, 1)))
70 LIST_PREPEND(JobDependency
, subject
, subject
->subject_list
, l
);
72 LIST_PREPEND(JobDependency
, subject
, object
->manager
->transaction_anchor
, l
);
74 LIST_PREPEND(JobDependency
, object
, object
->object_list
, l
);
79 void job_dependency_free(JobDependency
*l
) {
83 LIST_REMOVE(JobDependency
, subject
, l
->subject
->subject_list
, l
);
85 LIST_REMOVE(JobDependency
, subject
, l
->object
->manager
->transaction_anchor
, l
);
87 LIST_REMOVE(JobDependency
, object
, l
->object
->object_list
, l
);
92 void job_dependency_delete(Job
*subject
, Job
*object
, bool *matters
) {
97 LIST_FOREACH(object
, l
, object
->object_list
) {
98 assert(l
->object
== object
);
100 if (l
->subject
== subject
)
111 *matters
= l
->matters
;
113 job_dependency_free(l
);
116 void job_dump(Job
*j
, FILE*f
, const char *prefix
) {
124 "%s\tAction: %s → %s\n"
128 prefix
, unit_id(j
->unit
), job_type_to_string(j
->type
),
129 prefix
, job_state_to_string(j
->state
),
130 prefix
, yes_no(j
->forced
));
133 bool job_is_anchor(Job
*j
) {
138 LIST_FOREACH(object
, l
, j
->object_list
)
145 static bool types_match(JobType a
, JobType b
, JobType c
, JobType d
) {
147 (a
== c
&& b
== d
) ||
151 int job_type_merge(JobType
*a
, JobType b
) {
155 /* Merging is associative! a merged with b merged with c is
156 * the same as a merged with c merged with b. */
158 /* Mergeability is transitive! if a can be merged with b and b
159 * with c then a also with c */
161 /* Also, if a merged with b cannot be merged with c, then
162 * either a or b cannot be merged with c either */
164 if (types_match(*a
, b
, JOB_START
, JOB_VERIFY_ACTIVE
))
166 else if (types_match(*a
, b
, JOB_START
, JOB_RELOAD
) ||
167 types_match(*a
, b
, JOB_START
, JOB_RELOAD_OR_START
) ||
168 types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_RELOAD_OR_START
) ||
169 types_match(*a
, b
, JOB_RELOAD
, JOB_RELOAD_OR_START
))
170 *a
= JOB_RELOAD_OR_START
;
171 else if (types_match(*a
, b
, JOB_START
, JOB_RESTART
) ||
172 types_match(*a
, b
, JOB_START
, JOB_TRY_RESTART
) ||
173 types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_RESTART
) ||
174 types_match(*a
, b
, JOB_RELOAD
, JOB_RESTART
) ||
175 types_match(*a
, b
, JOB_RELOAD_OR_START
, JOB_RESTART
) ||
176 types_match(*a
, b
, JOB_RELOAD_OR_START
, JOB_TRY_RESTART
) ||
177 types_match(*a
, b
, JOB_RESTART
, JOB_TRY_RESTART
))
179 else if (types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_RELOAD
))
181 else if (types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_TRY_RESTART
) ||
182 types_match(*a
, b
, JOB_RELOAD
, JOB_TRY_RESTART
))
183 *a
= JOB_TRY_RESTART
;
190 bool job_type_is_mergeable(JobType a
, JobType b
) {
191 return job_type_merge(&a
, b
) >= 0;
194 bool job_type_is_superset(JobType a
, JobType b
) {
196 /* Checks whether operation a is a "superset" of b in its
204 return b
== JOB_VERIFY_ACTIVE
;
208 b
== JOB_VERIFY_ACTIVE
;
210 case JOB_RELOAD_OR_START
:
214 b
== JOB_VERIFY_ACTIVE
;
219 b
== JOB_VERIFY_ACTIVE
||
221 b
== JOB_RELOAD_OR_START
||
222 b
== JOB_TRY_RESTART
;
224 case JOB_TRY_RESTART
:
226 b
== JOB_VERIFY_ACTIVE
||
234 bool job_type_is_conflicting(JobType a
, JobType b
) {
235 assert(a
>= 0 && a
< _JOB_TYPE_MAX
);
236 assert(b
>= 0 && b
< _JOB_TYPE_MAX
);
238 return (a
== JOB_STOP
) != (b
== JOB_STOP
);
241 bool job_is_runnable(Job
*j
) {
246 assert(j
->installed
);
248 /* Checks whether there is any job running for the units this
249 * job needs to be running after (in the case of a 'positive'
250 * job type) or before (in the case of a 'negative' job type
253 if (j
->type
== JOB_START
||
254 j
->type
== JOB_VERIFY_ACTIVE
||
255 j
->type
== JOB_RELOAD
||
256 j
->type
== JOB_RELOAD_OR_START
) {
258 /* Immediate result is that the job is or might be
259 * started. In this case lets wait for the
260 * dependencies, regardless whether they are
261 * starting or stopping something. */
263 SET_FOREACH(other
, j
->unit
->meta
.dependencies
[UNIT_AFTER
], i
)
268 /* Also, if something else is being stopped and we should
269 * change state after it, then lets wait. */
271 SET_FOREACH(other
, j
->unit
->meta
.dependencies
[UNIT_BEFORE
], i
)
272 if (other
->meta
.job
&&
273 (other
->meta
.job
->type
== JOB_STOP
||
274 other
->meta
.job
->type
== JOB_RESTART
||
275 other
->meta
.job
->type
== JOB_TRY_RESTART
))
278 /* This means that for a service a and a service b where b
279 * shall be started after a:
281 * start a + start b → 1st step start a, 2nd step start b
282 * start a + stop b → 1st step stop b, 2nd step start a
283 * stop a + start b → 1st step stop a, 2nd step start b
284 * stop a + stop b → 1st step stop b, 2nd step stop a
286 * This has the side effect that restarts are properly
287 * synchronized too. */
292 int job_run_and_invalidate(Job
*j
) {
296 assert(j
->installed
);
298 if (j
->in_run_queue
) {
299 LIST_REMOVE(Job
, run_queue
, j
->manager
->run_queue
, j
);
300 j
->in_run_queue
= false;
303 if (j
->state
!= JOB_WAITING
)
306 if (!job_is_runnable(j
))
309 j
->state
= JOB_RUNNING
;
314 r
= unit_start(j
->unit
);
319 case JOB_VERIFY_ACTIVE
: {
320 UnitActiveState t
= unit_active_state(j
->unit
);
321 if (UNIT_IS_ACTIVE_OR_RELOADING(t
))
323 else if (t
== UNIT_ACTIVATING
)
331 r
= unit_stop(j
->unit
);
335 r
= unit_reload(j
->unit
);
338 case JOB_RELOAD_OR_START
:
339 if (unit_active_state(j
->unit
) == UNIT_ACTIVE
)
340 r
= unit_reload(j
->unit
);
342 r
= unit_start(j
->unit
);
346 UnitActiveState t
= unit_active_state(j
->unit
);
347 if (t
== UNIT_INACTIVE
|| t
== UNIT_ACTIVATING
) {
349 r
= unit_start(j
->unit
);
351 r
= unit_stop(j
->unit
);
355 case JOB_TRY_RESTART
: {
356 UnitActiveState t
= unit_active_state(j
->unit
);
357 if (t
== UNIT_INACTIVE
|| t
== UNIT_DEACTIVATING
)
359 else if (t
== UNIT_ACTIVATING
) {
361 r
= unit_start(j
->unit
);
363 r
= unit_stop(j
->unit
);
368 assert_not_reached("Unknown job type");
372 r
= job_finish_and_invalidate(j
, true);
373 else if (r
== -EAGAIN
) {
374 j
->state
= JOB_WAITING
;
377 r
= job_finish_and_invalidate(j
, false);
382 int job_finish_and_invalidate(Job
*j
, bool success
) {
389 assert(j
->installed
);
391 log_debug("Job %s/%s finished, success=%s", unit_id(j
->unit
), job_type_to_string(j
->type
), yes_no(success
));
393 /* Patch restart jobs so that they become normal start jobs */
394 if (success
&& (j
->type
== JOB_RESTART
|| j
->type
== JOB_TRY_RESTART
)) {
396 log_debug("Converting job %s/%s → %s/%s",
397 unit_id(j
->unit
), job_type_to_string(j
->type
),
398 unit_id(j
->unit
), job_type_to_string(JOB_START
));
400 j
->state
= JOB_RUNNING
;
411 /* Fail depending jobs on failure */
414 if (t
== JOB_START
||
415 t
== JOB_VERIFY_ACTIVE
||
416 t
== JOB_RELOAD_OR_START
) {
418 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRED_BY
], i
)
419 if (other
->meta
.job
&&
420 (other
->meta
.type
== JOB_START
||
421 other
->meta
.type
== JOB_VERIFY_ACTIVE
||
422 other
->meta
.type
== JOB_RELOAD_OR_START
))
423 job_finish_and_invalidate(other
->meta
.job
, false);
425 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_SOFT_REQUIRED_BY
], i
)
426 if (other
->meta
.job
&&
427 !other
->meta
.job
->forced
&&
428 (other
->meta
.type
== JOB_START
||
429 other
->meta
.type
== JOB_VERIFY_ACTIVE
||
430 other
->meta
.type
== JOB_RELOAD_OR_START
))
431 job_finish_and_invalidate(other
->meta
.job
, false);
433 } else if (t
== JOB_STOP
) {
435 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_CONFLICTS
], i
)
436 if (other
->meta
.job
&&
438 t
== JOB_VERIFY_ACTIVE
||
439 t
== JOB_RELOAD_OR_START
))
440 job_finish_and_invalidate(other
->meta
.job
, false);
444 /* Try to start the next jobs that can be started */
445 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_AFTER
], i
)
447 job_schedule_run(other
->meta
.job
);
448 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_BEFORE
], i
)
450 job_schedule_run(other
->meta
.job
);
455 void job_schedule_run(Job
*j
) {
457 assert(j
->installed
);
462 LIST_PREPEND(Job
, run_queue
, j
->manager
->run_queue
, j
);
463 j
->in_run_queue
= true;
466 char *job_dbus_path(Job
*j
) {
471 if (asprintf(&p
, "/org/freedesktop/systemd1/job/%lu", (unsigned long) j
->id
) < 0)
477 static const char* const job_state_table
[_JOB_STATE_MAX
] = {
478 [JOB_WAITING
] = "waiting",
479 [JOB_RUNNING
] = "running"
482 DEFINE_STRING_TABLE_LOOKUP(job_state
, JobState
);
484 static const char* const job_type_table
[_JOB_TYPE_MAX
] = {
485 [JOB_START
] = "start",
486 [JOB_VERIFY_ACTIVE
] = "verify-active",
488 [JOB_RELOAD
] = "reload",
489 [JOB_RELOAD_OR_START
] = "reload-or-start",
490 [JOB_RESTART
] = "restart",
491 [JOB_TRY_RESTART
] = "try-restart",
494 DEFINE_STRING_TABLE_LOOKUP(job_type
, JobType
);
496 static const char* const job_mode_table
[_JOB_MODE_MAX
] = {
498 [JOB_REPLACE
] = "replace"
501 DEFINE_STRING_TABLE_LOOKUP(job_mode
, JobMode
);