]>
git.ipfire.org Git - people/ms/systemd.git/blob - job.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
9 Job
* job_new(Manager
*m
, JobType type
, Name
*name
) {
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
->name
->meta
.job
== j
)
35 j
->name
->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 l
->subject_next
= subject
->subject_list
;
66 subject
->subject_list
= l
;
68 l
->subject_next
= object
->manager
->transaction_anchor
;
69 object
->manager
->transaction_anchor
= l
;
73 l
->subject_next
->subject_prev
= l
;
74 l
->subject_prev
= NULL
;
76 if ((l
->object_next
= object
->object_list
))
77 l
->object_next
->object_prev
= l
;
78 l
->object_prev
= NULL
;
79 object
->object_list
= l
;
84 void job_dependency_free(JobDependency
*l
) {
88 l
->subject_prev
->subject_next
= l
->subject_next
;
90 l
->subject
->subject_list
= l
->subject_next
;
92 l
->object
->manager
->transaction_anchor
= l
->subject_next
;
95 l
->subject_next
->subject_prev
= l
->subject_prev
;
98 l
->object_prev
->object_next
= l
->object_next
;
100 l
->object
->object_list
= l
->object_next
;
103 l
->object_next
->object_prev
= l
->object_prev
;
108 void job_dependency_delete(Job
*subject
, Job
*object
, bool *matters
) {
113 for (l
= object
->object_list
; l
; l
= l
->object_next
) {
114 assert(l
->object
== object
);
116 if (l
->subject
== subject
)
127 *matters
= l
->matters
;
129 job_dependency_free(l
);
132 const char* job_type_to_string(JobType t
) {
134 static const char* const job_type_table
[_JOB_TYPE_MAX
] = {
135 [JOB_START
] = "start",
136 [JOB_VERIFY_ACTIVE
] = "verify-active",
138 [JOB_RELOAD
] = "reload",
139 [JOB_RELOAD_OR_START
] = "reload-or-start",
140 [JOB_RESTART
] = "restart",
141 [JOB_TRY_RESTART
] = "try-restart",
144 if (t
< 0 || t
>= _JOB_TYPE_MAX
)
147 return job_type_table
[t
];
150 void job_dump(Job
*j
, FILE*f
, const char *prefix
) {
152 static const char* const job_state_table
[_JOB_STATE_MAX
] = {
153 [JOB_WAITING
] = "waiting",
154 [JOB_RUNNING
] = "running"
162 "%s\tAction: %s → %s\n"
166 prefix
, name_id(j
->name
), job_type_to_string(j
->type
),
167 prefix
, job_state_table
[j
->state
],
168 prefix
, yes_no(j
->forced
));
171 bool job_is_anchor(Job
*j
) {
176 for (l
= j
->object_list
; l
; l
= l
->object_next
)
183 static bool types_match(JobType a
, JobType b
, JobType c
, JobType d
) {
185 (a
== c
&& b
== d
) ||
189 int job_type_merge(JobType
*a
, JobType b
) {
193 /* Merging is associative! a merged with b merged with c is
194 * the same as a merged with c merged with b. */
196 /* Mergeability is transitive! if a can be merged with b and b
197 * with c then a also with c */
199 /* Also, if a merged with b cannot be merged with c, then
200 * either a or b cannot be merged with c either */
202 if (types_match(*a
, b
, JOB_START
, JOB_VERIFY_ACTIVE
))
204 else if (types_match(*a
, b
, JOB_START
, JOB_RELOAD
) ||
205 types_match(*a
, b
, JOB_START
, JOB_RELOAD_OR_START
) ||
206 types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_RELOAD_OR_START
) ||
207 types_match(*a
, b
, JOB_RELOAD
, JOB_RELOAD_OR_START
))
208 *a
= JOB_RELOAD_OR_START
;
209 else if (types_match(*a
, b
, JOB_START
, JOB_RESTART
) ||
210 types_match(*a
, b
, JOB_START
, JOB_TRY_RESTART
) ||
211 types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_RESTART
) ||
212 types_match(*a
, b
, JOB_RELOAD
, JOB_RESTART
) ||
213 types_match(*a
, b
, JOB_RELOAD_OR_START
, JOB_RESTART
) ||
214 types_match(*a
, b
, JOB_RELOAD_OR_START
, JOB_TRY_RESTART
) ||
215 types_match(*a
, b
, JOB_RESTART
, JOB_TRY_RESTART
))
217 else if (types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_RELOAD
))
219 else if (types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_TRY_RESTART
) ||
220 types_match(*a
, b
, JOB_RELOAD
, JOB_TRY_RESTART
))
221 *a
= JOB_TRY_RESTART
;
228 bool job_type_is_mergeable(JobType a
, JobType b
) {
229 return job_type_merge(&a
, b
) >= 0;
232 bool job_type_is_superset(JobType a
, JobType b
) {
234 /* Checks whether operation a is a "superset" of b in its
242 return b
== JOB_VERIFY_ACTIVE
;
246 b
== JOB_VERIFY_ACTIVE
;
248 case JOB_RELOAD_OR_START
:
252 b
== JOB_VERIFY_ACTIVE
;
257 b
== JOB_VERIFY_ACTIVE
||
259 b
== JOB_RELOAD_OR_START
||
260 b
== JOB_TRY_RESTART
;
262 case JOB_TRY_RESTART
:
264 b
== JOB_VERIFY_ACTIVE
||
272 bool job_type_is_conflicting(JobType a
, JobType b
) {
273 assert(a
>= 0 && a
< _JOB_TYPE_MAX
);
274 assert(b
>= 0 && b
< _JOB_TYPE_MAX
);
276 return (a
== JOB_STOP
) != (b
== JOB_STOP
);
279 bool job_is_runnable(Job
*j
) {
286 /* Checks whether there is any job running for the names this
287 * job needs to be running after (in the case of a 'positive'
288 * job type) or before (in the case of a 'negative' job type
291 if (j
->type
== JOB_START
||
292 j
->type
== JOB_VERIFY_ACTIVE
||
293 j
->type
== JOB_RELOAD
||
294 j
->type
== JOB_RELOAD_OR_START
) {
296 /* Immediate result is that the job is or might be
297 * started. In this case lets wait for the
298 * dependencies, regardless whether they are
299 * starting or stopping something. */
301 SET_FOREACH(other
, j
->name
->meta
.dependencies
[NAME_AFTER
], i
)
306 /* Also, if something else is being stopped and we should
307 * change state after it, then lets wait. */
309 SET_FOREACH(other
, j
->name
->meta
.dependencies
[NAME_BEFORE
], i
)
310 if (other
->meta
.job
&&
311 (other
->meta
.job
->type
== JOB_STOP
||
312 other
->meta
.job
->type
== JOB_RESTART
||
313 other
->meta
.job
->type
== JOB_TRY_RESTART
))
316 /* This means that for a service a and a service b where b
317 * shall be started after a:
319 * start a + start b → 1st step start a, 2nd step start b
320 * start a + stop b → 1st step stop b, 2nd step start a
321 * stop a + start b → 1st step stop a, 2nd step start b
322 * stop a + stop b → 1st step stop b, 2nd step stop a
324 * This has the side effect that restarts are properly
325 * synchronized too. */
330 int job_run_and_invalidate(Job
*j
) {
334 if (j
->in_run_queue
) {
335 LIST_REMOVE(Job
, run_queue
, j
->manager
->run_queue
, j
);
336 j
->in_run_queue
= false;
339 if (j
->state
!= JOB_WAITING
)
342 if (!job_is_runnable(j
))
345 j
->state
= JOB_RUNNING
;
350 r
= name_start(j
->name
);
355 case JOB_VERIFY_ACTIVE
: {
356 NameActiveState t
= name_active_state(j
->name
);
357 if (NAME_IS_ACTIVE_OR_RELOADING(t
))
359 else if (t
== NAME_ACTIVATING
)
367 r
= name_stop(j
->name
);
371 r
= name_reload(j
->name
);
374 case JOB_RELOAD_OR_START
:
375 if (name_active_state(j
->name
) == NAME_ACTIVE
)
376 r
= name_reload(j
->name
);
378 r
= name_start(j
->name
);
382 NameActiveState t
= name_active_state(j
->name
);
383 if (t
== NAME_INACTIVE
|| t
== NAME_ACTIVATING
) {
385 r
= name_start(j
->name
);
387 r
= name_stop(j
->name
);
391 case JOB_TRY_RESTART
: {
392 NameActiveState t
= name_active_state(j
->name
);
393 if (t
== NAME_INACTIVE
|| t
== NAME_DEACTIVATING
)
395 else if (t
== NAME_ACTIVATING
) {
397 r
= name_start(j
->name
);
399 r
= name_stop(j
->name
);
408 r
= job_finish_and_invalidate(j
, true);
409 else if (r
== -EAGAIN
) {
410 j
->state
= JOB_WAITING
;
413 r
= job_finish_and_invalidate(j
, false);
418 int job_finish_and_invalidate(Job
*j
, bool success
) {
426 /* Patch restart jobs so that they become normal start jobs */
427 if (success
&& (j
->type
== JOB_RESTART
|| j
->type
== JOB_TRY_RESTART
)) {
428 j
->state
= JOB_RUNNING
;
438 /* Fail depending jobs on failure */
441 if (t
== JOB_START
||
442 t
== JOB_VERIFY_ACTIVE
||
443 t
== JOB_RELOAD_OR_START
) {
445 SET_FOREACH(other
, n
->meta
.dependencies
[NAME_REQUIRED_BY
], i
)
446 if (other
->meta
.job
&&
447 (other
->meta
.type
== JOB_START
||
448 other
->meta
.type
== JOB_VERIFY_ACTIVE
||
449 other
->meta
.type
== JOB_RELOAD_OR_START
))
450 job_finish_and_invalidate(other
->meta
.job
, false);
452 SET_FOREACH(other
, n
->meta
.dependencies
[NAME_SOFT_REQUIRED_BY
], i
)
453 if (other
->meta
.job
&&
454 !other
->meta
.job
->forced
&&
455 (other
->meta
.type
== JOB_START
||
456 other
->meta
.type
== JOB_VERIFY_ACTIVE
||
457 other
->meta
.type
== JOB_RELOAD_OR_START
))
458 job_finish_and_invalidate(other
->meta
.job
, false);
460 } else if (t
== JOB_STOP
) {
462 SET_FOREACH(other
, n
->meta
.dependencies
[NAME_CONFLICTS
], i
)
463 if (other
->meta
.job
&&
465 t
== JOB_VERIFY_ACTIVE
||
466 t
== JOB_RELOAD_OR_START
))
467 job_finish_and_invalidate(other
->meta
.job
, false);
471 /* Try to start the next jobs that can be started */
472 SET_FOREACH(other
, n
->meta
.dependencies
[NAME_AFTER
], i
)
474 job_schedule_run(other
->meta
.job
);
475 SET_FOREACH(other
, n
->meta
.dependencies
[NAME_BEFORE
], i
)
477 job_schedule_run(other
->meta
.job
);
482 void job_schedule_run(Job
*j
) {
489 LIST_PREPEND(Job
, run_queue
, j
->manager
->run_queue
, j
);
490 j
->in_run_queue
= true;