]>
git.ipfire.org Git - thirdparty/systemd.git/blob - job.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
9 Job
* job_new(Manager
*m
, JobType type
, Unit
*unit
) {
13 assert(type
< _JOB_TYPE_MAX
);
16 if (!(j
= new0(Job
, 1)))
20 j
->id
= m
->current_job_id
++;
24 /* We don't link it here, that's what job_dependency() is for */
29 void job_free(Job
*j
) {
32 /* Detach from next 'bigger' objects */
34 if (j
->unit
->meta
.job
== j
)
35 j
->unit
->meta
.job
= NULL
;
37 hashmap_remove(j
->manager
->jobs
, UINT32_TO_PTR(j
->id
));
41 /* Detach from next 'smaller' objects */
42 manager_transaction_unlink_job(j
->manager
, j
);
47 JobDependency
* job_dependency_new(Job
*subject
, Job
*object
, bool matters
) {
52 /* Adds a new job link, which encodes that the 'subject' job
53 * needs the 'object' job in some way. If 'subject' is NULL
54 * this means the 'anchor' job (i.e. the one the user
55 * explcitily asked for) is the requester. */
57 if (!(l
= new0(JobDependency
, 1)))
65 LIST_PREPEND(JobDependency
, subject
, subject
->subject_list
, l
);
67 LIST_PREPEND(JobDependency
, subject
, object
->manager
->transaction_anchor
, l
);
69 LIST_PREPEND(JobDependency
, object
, object
->object_list
, l
);
74 void job_dependency_free(JobDependency
*l
) {
78 LIST_REMOVE(JobDependency
, subject
, l
->subject
->subject_list
, l
);
80 LIST_REMOVE(JobDependency
, subject
, l
->object
->manager
->transaction_anchor
, l
);
82 LIST_REMOVE(JobDependency
, object
, l
->object
->object_list
, l
);
87 void job_dependency_delete(Job
*subject
, Job
*object
, bool *matters
) {
92 LIST_FOREACH(object
, l
, object
->object_list
) {
93 assert(l
->object
== object
);
95 if (l
->subject
== subject
)
106 *matters
= l
->matters
;
108 job_dependency_free(l
);
111 const char* job_type_to_string(JobType t
) {
113 static const char* const job_type_table
[_JOB_TYPE_MAX
] = {
114 [JOB_START
] = "start",
115 [JOB_VERIFY_ACTIVE
] = "verify-active",
117 [JOB_RELOAD
] = "reload",
118 [JOB_RELOAD_OR_START
] = "reload-or-start",
119 [JOB_RESTART
] = "restart",
120 [JOB_TRY_RESTART
] = "try-restart",
123 if (t
< 0 || t
>= _JOB_TYPE_MAX
)
126 return job_type_table
[t
];
129 void job_dump(Job
*j
, FILE*f
, const char *prefix
) {
131 static const char* const job_state_table
[_JOB_STATE_MAX
] = {
132 [JOB_WAITING
] = "waiting",
133 [JOB_RUNNING
] = "running"
141 "%s\tAction: %s → %s\n"
145 prefix
, unit_id(j
->unit
), job_type_to_string(j
->type
),
146 prefix
, job_state_table
[j
->state
],
147 prefix
, yes_no(j
->forced
));
150 bool job_is_anchor(Job
*j
) {
155 LIST_FOREACH(object
, l
, j
->object_list
)
162 static bool types_match(JobType a
, JobType b
, JobType c
, JobType d
) {
164 (a
== c
&& b
== d
) ||
168 int job_type_merge(JobType
*a
, JobType b
) {
172 /* Merging is associative! a merged with b merged with c is
173 * the same as a merged with c merged with b. */
175 /* Mergeability is transitive! if a can be merged with b and b
176 * with c then a also with c */
178 /* Also, if a merged with b cannot be merged with c, then
179 * either a or b cannot be merged with c either */
181 if (types_match(*a
, b
, JOB_START
, JOB_VERIFY_ACTIVE
))
183 else if (types_match(*a
, b
, JOB_START
, JOB_RELOAD
) ||
184 types_match(*a
, b
, JOB_START
, JOB_RELOAD_OR_START
) ||
185 types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_RELOAD_OR_START
) ||
186 types_match(*a
, b
, JOB_RELOAD
, JOB_RELOAD_OR_START
))
187 *a
= JOB_RELOAD_OR_START
;
188 else if (types_match(*a
, b
, JOB_START
, JOB_RESTART
) ||
189 types_match(*a
, b
, JOB_START
, JOB_TRY_RESTART
) ||
190 types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_RESTART
) ||
191 types_match(*a
, b
, JOB_RELOAD
, JOB_RESTART
) ||
192 types_match(*a
, b
, JOB_RELOAD_OR_START
, JOB_RESTART
) ||
193 types_match(*a
, b
, JOB_RELOAD_OR_START
, JOB_TRY_RESTART
) ||
194 types_match(*a
, b
, JOB_RESTART
, JOB_TRY_RESTART
))
196 else if (types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_RELOAD
))
198 else if (types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_TRY_RESTART
) ||
199 types_match(*a
, b
, JOB_RELOAD
, JOB_TRY_RESTART
))
200 *a
= JOB_TRY_RESTART
;
207 bool job_type_is_mergeable(JobType a
, JobType b
) {
208 return job_type_merge(&a
, b
) >= 0;
211 bool job_type_is_superset(JobType a
, JobType b
) {
213 /* Checks whether operation a is a "superset" of b in its
221 return b
== JOB_VERIFY_ACTIVE
;
225 b
== JOB_VERIFY_ACTIVE
;
227 case JOB_RELOAD_OR_START
:
231 b
== JOB_VERIFY_ACTIVE
;
236 b
== JOB_VERIFY_ACTIVE
||
238 b
== JOB_RELOAD_OR_START
||
239 b
== JOB_TRY_RESTART
;
241 case JOB_TRY_RESTART
:
243 b
== JOB_VERIFY_ACTIVE
||
251 bool job_type_is_conflicting(JobType a
, JobType b
) {
252 assert(a
>= 0 && a
< _JOB_TYPE_MAX
);
253 assert(b
>= 0 && b
< _JOB_TYPE_MAX
);
255 return (a
== JOB_STOP
) != (b
== JOB_STOP
);
258 bool job_is_runnable(Job
*j
) {
263 assert(j
->installed
);
265 /* Checks whether there is any job running for the units this
266 * job needs to be running after (in the case of a 'positive'
267 * job type) or before (in the case of a 'negative' job type
270 if (j
->type
== JOB_START
||
271 j
->type
== JOB_VERIFY_ACTIVE
||
272 j
->type
== JOB_RELOAD
||
273 j
->type
== JOB_RELOAD_OR_START
) {
275 /* Immediate result is that the job is or might be
276 * started. In this case lets wait for the
277 * dependencies, regardless whether they are
278 * starting or stopping something. */
280 SET_FOREACH(other
, j
->unit
->meta
.dependencies
[UNIT_AFTER
], i
)
285 /* Also, if something else is being stopped and we should
286 * change state after it, then lets wait. */
288 SET_FOREACH(other
, j
->unit
->meta
.dependencies
[UNIT_BEFORE
], i
)
289 if (other
->meta
.job
&&
290 (other
->meta
.job
->type
== JOB_STOP
||
291 other
->meta
.job
->type
== JOB_RESTART
||
292 other
->meta
.job
->type
== JOB_TRY_RESTART
))
295 /* This means that for a service a and a service b where b
296 * shall be started after a:
298 * start a + start b → 1st step start a, 2nd step start b
299 * start a + stop b → 1st step stop b, 2nd step start a
300 * stop a + start b → 1st step stop a, 2nd step start b
301 * stop a + stop b → 1st step stop b, 2nd step stop a
303 * This has the side effect that restarts are properly
304 * synchronized too. */
309 int job_run_and_invalidate(Job
*j
) {
313 assert(j
->installed
);
315 if (j
->in_run_queue
) {
316 LIST_REMOVE(Job
, run_queue
, j
->manager
->run_queue
, j
);
317 j
->in_run_queue
= false;
320 if (j
->state
!= JOB_WAITING
)
323 if (!job_is_runnable(j
))
326 j
->state
= JOB_RUNNING
;
331 r
= unit_start(j
->unit
);
336 case JOB_VERIFY_ACTIVE
: {
337 UnitActiveState t
= unit_active_state(j
->unit
);
338 if (UNIT_IS_ACTIVE_OR_RELOADING(t
))
340 else if (t
== UNIT_ACTIVATING
)
348 r
= unit_stop(j
->unit
);
352 r
= unit_reload(j
->unit
);
355 case JOB_RELOAD_OR_START
:
356 if (unit_active_state(j
->unit
) == UNIT_ACTIVE
)
357 r
= unit_reload(j
->unit
);
359 r
= unit_start(j
->unit
);
363 UnitActiveState t
= unit_active_state(j
->unit
);
364 if (t
== UNIT_INACTIVE
|| t
== UNIT_ACTIVATING
) {
366 r
= unit_start(j
->unit
);
368 r
= unit_stop(j
->unit
);
372 case JOB_TRY_RESTART
: {
373 UnitActiveState t
= unit_active_state(j
->unit
);
374 if (t
== UNIT_INACTIVE
|| t
== UNIT_DEACTIVATING
)
376 else if (t
== UNIT_ACTIVATING
) {
378 r
= unit_start(j
->unit
);
380 r
= unit_stop(j
->unit
);
385 assert_not_reached("Unknown job type");
389 r
= job_finish_and_invalidate(j
, true);
390 else if (r
== -EAGAIN
) {
391 j
->state
= JOB_WAITING
;
394 r
= job_finish_and_invalidate(j
, false);
399 int job_finish_and_invalidate(Job
*j
, bool success
) {
406 assert(j
->installed
);
408 /* Patch restart jobs so that they become normal start jobs */
409 if (success
&& (j
->type
== JOB_RESTART
|| j
->type
== JOB_TRY_RESTART
)) {
410 j
->state
= JOB_RUNNING
;
420 /* Fail depending jobs on failure */
423 if (t
== JOB_START
||
424 t
== JOB_VERIFY_ACTIVE
||
425 t
== JOB_RELOAD_OR_START
) {
427 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRED_BY
], i
)
428 if (other
->meta
.job
&&
429 (other
->meta
.type
== JOB_START
||
430 other
->meta
.type
== JOB_VERIFY_ACTIVE
||
431 other
->meta
.type
== JOB_RELOAD_OR_START
))
432 job_finish_and_invalidate(other
->meta
.job
, false);
434 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_SOFT_REQUIRED_BY
], i
)
435 if (other
->meta
.job
&&
436 !other
->meta
.job
->forced
&&
437 (other
->meta
.type
== JOB_START
||
438 other
->meta
.type
== JOB_VERIFY_ACTIVE
||
439 other
->meta
.type
== JOB_RELOAD_OR_START
))
440 job_finish_and_invalidate(other
->meta
.job
, false);
442 } else if (t
== JOB_STOP
) {
444 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_CONFLICTS
], i
)
445 if (other
->meta
.job
&&
447 t
== JOB_VERIFY_ACTIVE
||
448 t
== JOB_RELOAD_OR_START
))
449 job_finish_and_invalidate(other
->meta
.job
, false);
453 /* Try to start the next jobs that can be started */
454 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_AFTER
], i
)
456 job_schedule_run(other
->meta
.job
);
457 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_BEFORE
], i
)
459 job_schedule_run(other
->meta
.job
);
464 void job_schedule_run(Job
*j
) {
466 assert(j
->installed
);
471 LIST_PREPEND(Job
, run_queue
, j
->manager
->run_queue
, j
);
472 j
->in_run_queue
= true;