]>
git.ipfire.org Git - thirdparty/systemd.git/blob - job.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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 General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "load-fragment.h"
30 #include "load-dropin.h"
33 Job
* job_new(Manager
*m
, JobType type
, Unit
*unit
) {
37 assert(type
< _JOB_TYPE_MAX
);
40 if (!(j
= new0(Job
, 1)))
44 j
->id
= m
->current_job_id
++;
48 /* We don't link it here, that's what job_dependency() is for */
53 void job_free(Job
*j
) {
56 /* Detach from next 'bigger' objects */
58 if (j
->unit
->meta
.job
== j
)
59 j
->unit
->meta
.job
= NULL
;
61 hashmap_remove(j
->manager
->jobs
, UINT32_TO_PTR(j
->id
));
65 /* Detach from next 'smaller' objects */
66 manager_transaction_unlink_job(j
->manager
, j
);
71 JobDependency
* job_dependency_new(Job
*subject
, Job
*object
, bool matters
) {
76 /* Adds a new job link, which encodes that the 'subject' job
77 * needs the 'object' job in some way. If 'subject' is NULL
78 * this means the 'anchor' job (i.e. the one the user
79 * explcitily asked for) is the requester. */
81 if (!(l
= new0(JobDependency
, 1)))
89 LIST_PREPEND(JobDependency
, subject
, subject
->subject_list
, l
);
91 LIST_PREPEND(JobDependency
, subject
, object
->manager
->transaction_anchor
, l
);
93 LIST_PREPEND(JobDependency
, object
, object
->object_list
, l
);
98 void job_dependency_free(JobDependency
*l
) {
102 LIST_REMOVE(JobDependency
, subject
, l
->subject
->subject_list
, l
);
104 LIST_REMOVE(JobDependency
, subject
, l
->object
->manager
->transaction_anchor
, l
);
106 LIST_REMOVE(JobDependency
, object
, l
->object
->object_list
, l
);
111 void job_dependency_delete(Job
*subject
, Job
*object
, bool *matters
) {
116 LIST_FOREACH(object
, l
, object
->object_list
) {
117 assert(l
->object
== object
);
119 if (l
->subject
== subject
)
130 *matters
= l
->matters
;
132 job_dependency_free(l
);
135 void job_dump(Job
*j
, FILE*f
, const char *prefix
) {
143 "%s\tAction: %s ā %s\n"
147 prefix
, unit_id(j
->unit
), job_type_to_string(j
->type
),
148 prefix
, job_state_to_string(j
->state
),
149 prefix
, yes_no(j
->forced
));
152 bool job_is_anchor(Job
*j
) {
157 LIST_FOREACH(object
, l
, j
->object_list
)
164 static bool types_match(JobType a
, JobType b
, JobType c
, JobType d
) {
166 (a
== c
&& b
== d
) ||
170 int job_type_merge(JobType
*a
, JobType b
) {
174 /* Merging is associative! a merged with b merged with c is
175 * the same as a merged with c merged with b. */
177 /* Mergeability is transitive! if a can be merged with b and b
178 * with c then a also with c */
180 /* Also, if a merged with b cannot be merged with c, then
181 * either a or b cannot be merged with c either */
183 if (types_match(*a
, b
, JOB_START
, JOB_VERIFY_ACTIVE
))
185 else if (types_match(*a
, b
, JOB_START
, JOB_RELOAD
) ||
186 types_match(*a
, b
, JOB_START
, JOB_RELOAD_OR_START
) ||
187 types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_RELOAD_OR_START
) ||
188 types_match(*a
, b
, JOB_RELOAD
, JOB_RELOAD_OR_START
))
189 *a
= JOB_RELOAD_OR_START
;
190 else if (types_match(*a
, b
, JOB_START
, JOB_RESTART
) ||
191 types_match(*a
, b
, JOB_START
, JOB_TRY_RESTART
) ||
192 types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_RESTART
) ||
193 types_match(*a
, b
, JOB_RELOAD
, JOB_RESTART
) ||
194 types_match(*a
, b
, JOB_RELOAD_OR_START
, JOB_RESTART
) ||
195 types_match(*a
, b
, JOB_RELOAD_OR_START
, JOB_TRY_RESTART
) ||
196 types_match(*a
, b
, JOB_RESTART
, JOB_TRY_RESTART
))
198 else if (types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_RELOAD
))
200 else if (types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_TRY_RESTART
) ||
201 types_match(*a
, b
, JOB_RELOAD
, JOB_TRY_RESTART
))
202 *a
= JOB_TRY_RESTART
;
209 bool job_type_is_mergeable(JobType a
, JobType b
) {
210 return job_type_merge(&a
, b
) >= 0;
213 bool job_type_is_superset(JobType a
, JobType b
) {
215 /* Checks whether operation a is a "superset" of b in its
223 return b
== JOB_VERIFY_ACTIVE
;
227 b
== JOB_VERIFY_ACTIVE
;
229 case JOB_RELOAD_OR_START
:
233 b
== JOB_VERIFY_ACTIVE
;
238 b
== JOB_VERIFY_ACTIVE
||
240 b
== JOB_RELOAD_OR_START
||
241 b
== JOB_TRY_RESTART
;
243 case JOB_TRY_RESTART
:
245 b
== JOB_VERIFY_ACTIVE
||
253 bool job_type_is_conflicting(JobType a
, JobType b
) {
254 assert(a
>= 0 && a
< _JOB_TYPE_MAX
);
255 assert(b
>= 0 && b
< _JOB_TYPE_MAX
);
257 return (a
== JOB_STOP
) != (b
== JOB_STOP
);
260 bool job_is_runnable(Job
*j
) {
265 assert(j
->installed
);
267 /* Checks whether there is any job running for the units this
268 * job needs to be running after (in the case of a 'positive'
269 * job type) or before (in the case of a 'negative' job type
272 if (j
->type
== JOB_START
||
273 j
->type
== JOB_VERIFY_ACTIVE
||
274 j
->type
== JOB_RELOAD
||
275 j
->type
== JOB_RELOAD_OR_START
) {
277 /* Immediate result is that the job is or might be
278 * started. In this case lets wait for the
279 * dependencies, regardless whether they are
280 * starting or stopping something. */
282 SET_FOREACH(other
, j
->unit
->meta
.dependencies
[UNIT_AFTER
], i
)
287 /* Also, if something else is being stopped and we should
288 * change state after it, then lets wait. */
290 SET_FOREACH(other
, j
->unit
->meta
.dependencies
[UNIT_BEFORE
], i
)
291 if (other
->meta
.job
&&
292 (other
->meta
.job
->type
== JOB_STOP
||
293 other
->meta
.job
->type
== JOB_RESTART
||
294 other
->meta
.job
->type
== JOB_TRY_RESTART
))
297 /* This means that for a service a and a service b where b
298 * shall be started after a:
300 * start a + start b ā 1st step start a, 2nd step start b
301 * start a + stop b ā 1st step stop b, 2nd step start a
302 * stop a + start b ā 1st step stop a, 2nd step start b
303 * stop a + stop b ā 1st step stop b, 2nd step stop a
305 * This has the side effect that restarts are properly
306 * synchronized too. */
311 int job_run_and_invalidate(Job
*j
) {
315 assert(j
->installed
);
317 if (j
->in_run_queue
) {
318 LIST_REMOVE(Job
, run_queue
, j
->manager
->run_queue
, j
);
319 j
->in_run_queue
= false;
322 if (j
->state
!= JOB_WAITING
)
325 if (!job_is_runnable(j
))
328 j
->state
= JOB_RUNNING
;
333 r
= unit_start(j
->unit
);
338 case JOB_VERIFY_ACTIVE
: {
339 UnitActiveState t
= unit_active_state(j
->unit
);
340 if (UNIT_IS_ACTIVE_OR_RELOADING(t
))
342 else if (t
== UNIT_ACTIVATING
)
350 r
= unit_stop(j
->unit
);
354 r
= unit_reload(j
->unit
);
357 case JOB_RELOAD_OR_START
:
358 if (unit_active_state(j
->unit
) == UNIT_ACTIVE
)
359 r
= unit_reload(j
->unit
);
361 r
= unit_start(j
->unit
);
365 UnitActiveState t
= unit_active_state(j
->unit
);
366 if (t
== UNIT_INACTIVE
|| t
== UNIT_ACTIVATING
) {
368 r
= unit_start(j
->unit
);
370 r
= unit_stop(j
->unit
);
374 case JOB_TRY_RESTART
: {
375 UnitActiveState t
= unit_active_state(j
->unit
);
376 if (t
== UNIT_INACTIVE
|| t
== UNIT_DEACTIVATING
)
378 else if (t
== UNIT_ACTIVATING
) {
380 r
= unit_start(j
->unit
);
382 r
= unit_stop(j
->unit
);
387 assert_not_reached("Unknown job type");
391 r
= job_finish_and_invalidate(j
, true);
392 else if (r
== -EAGAIN
) {
393 j
->state
= JOB_WAITING
;
396 r
= job_finish_and_invalidate(j
, false);
401 int job_finish_and_invalidate(Job
*j
, bool success
) {
408 assert(j
->installed
);
410 log_debug("Job %s/%s finished, success=%s", unit_id(j
->unit
), job_type_to_string(j
->type
), yes_no(success
));
412 /* Patch restart jobs so that they become normal start jobs */
413 if (success
&& (j
->type
== JOB_RESTART
|| j
->type
== JOB_TRY_RESTART
)) {
415 log_debug("Converting job %s/%s ā %s/%s",
416 unit_id(j
->unit
), job_type_to_string(j
->type
),
417 unit_id(j
->unit
), job_type_to_string(JOB_START
));
419 j
->state
= JOB_RUNNING
;
430 /* Fail depending jobs on failure */
433 if (t
== JOB_START
||
434 t
== JOB_VERIFY_ACTIVE
||
435 t
== JOB_RELOAD_OR_START
) {
437 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRED_BY
], i
)
438 if (other
->meta
.job
&&
439 (other
->meta
.type
== JOB_START
||
440 other
->meta
.type
== JOB_VERIFY_ACTIVE
||
441 other
->meta
.type
== JOB_RELOAD_OR_START
))
442 job_finish_and_invalidate(other
->meta
.job
, false);
444 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_SOFT_REQUIRED_BY
], i
)
445 if (other
->meta
.job
&&
446 !other
->meta
.job
->forced
&&
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 } else if (t
== JOB_STOP
) {
454 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_CONFLICTS
], i
)
455 if (other
->meta
.job
&&
457 t
== JOB_VERIFY_ACTIVE
||
458 t
== JOB_RELOAD_OR_START
))
459 job_finish_and_invalidate(other
->meta
.job
, false);
463 /* Try to start the next jobs that can be started */
464 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_AFTER
], i
)
466 job_schedule_run(other
->meta
.job
);
467 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_BEFORE
], i
)
469 job_schedule_run(other
->meta
.job
);
474 void job_schedule_run(Job
*j
) {
476 assert(j
->installed
);
481 LIST_PREPEND(Job
, run_queue
, j
->manager
->run_queue
, j
);
482 j
->in_run_queue
= true;
485 char *job_dbus_path(Job
*j
) {
490 if (asprintf(&p
, "/org/freedesktop/systemd1/job/%lu", (unsigned long) j
->id
) < 0)
496 static const char* const job_state_table
[_JOB_STATE_MAX
] = {
497 [JOB_WAITING
] = "waiting",
498 [JOB_RUNNING
] = "running"
501 DEFINE_STRING_TABLE_LOOKUP(job_state
, JobState
);
503 static const char* const job_type_table
[_JOB_TYPE_MAX
] = {
504 [JOB_START
] = "start",
505 [JOB_VERIFY_ACTIVE
] = "verify-active",
507 [JOB_RELOAD
] = "reload",
508 [JOB_RELOAD_OR_START
] = "reload-or-start",
509 [JOB_RESTART
] = "restart",
510 [JOB_TRY_RESTART
] = "try-restart",
513 DEFINE_STRING_TABLE_LOOKUP(job_type
, JobType
);
515 static const char* const job_mode_table
[_JOB_MODE_MAX
] = {
517 [JOB_REPLACE
] = "replace"
520 DEFINE_STRING_TABLE_LOOKUP(job_mode
, JobMode
);