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"
34 Job
* job_new(Manager
*m
, JobType type
, Unit
*unit
) {
38 assert(type
< _JOB_TYPE_MAX
);
41 if (!(j
= new0(Job
, 1)))
45 j
->id
= m
->current_job_id
++;
49 /* We don't link it here, that's what job_dependency() is for */
54 void job_free(Job
*j
) {
57 /* Detach from next 'bigger' objects */
59 bus_job_send_removed_signal(j
);
61 if (j
->unit
->meta
.job
== j
)
62 j
->unit
->meta
.job
= NULL
;
64 hashmap_remove(j
->manager
->jobs
, UINT32_TO_PTR(j
->id
));
68 /* Detach from next 'smaller' objects */
69 manager_transaction_unlink_job(j
->manager
, j
, true);
72 LIST_REMOVE(Job
, run_queue
, j
->manager
->run_queue
, j
);
75 LIST_REMOVE(Job
, dbus_queue
, j
->manager
->dbus_job_queue
, j
);
80 JobDependency
* job_dependency_new(Job
*subject
, Job
*object
, bool matters
) {
85 /* Adds a new job link, which encodes that the 'subject' job
86 * needs the 'object' job in some way. If 'subject' is NULL
87 * this means the 'anchor' job (i.e. the one the user
88 * explcitily asked for) is the requester. */
90 if (!(l
= new0(JobDependency
, 1)))
98 LIST_PREPEND(JobDependency
, subject
, subject
->subject_list
, l
);
100 LIST_PREPEND(JobDependency
, subject
, object
->manager
->transaction_anchor
, l
);
102 LIST_PREPEND(JobDependency
, object
, object
->object_list
, l
);
107 void job_dependency_free(JobDependency
*l
) {
111 LIST_REMOVE(JobDependency
, subject
, l
->subject
->subject_list
, l
);
113 LIST_REMOVE(JobDependency
, subject
, l
->object
->manager
->transaction_anchor
, l
);
115 LIST_REMOVE(JobDependency
, object
, l
->object
->object_list
, l
);
120 void job_dependency_delete(Job
*subject
, Job
*object
, bool *matters
) {
125 LIST_FOREACH(object
, l
, object
->object_list
) {
126 assert(l
->object
== object
);
128 if (l
->subject
== subject
)
139 *matters
= l
->matters
;
141 job_dependency_free(l
);
144 void job_dump(Job
*j
, FILE*f
, const char *prefix
) {
152 "%s\tAction: %s ā %s\n"
156 prefix
, j
->unit
->meta
.id
, job_type_to_string(j
->type
),
157 prefix
, job_state_to_string(j
->state
),
158 prefix
, yes_no(j
->override
));
161 bool job_is_anchor(Job
*j
) {
166 LIST_FOREACH(object
, l
, j
->object_list
)
173 static bool types_match(JobType a
, JobType b
, JobType c
, JobType d
) {
175 (a
== c
&& b
== d
) ||
179 int job_type_merge(JobType
*a
, JobType b
) {
183 /* Merging is associative! a merged with b merged with c is
184 * the same as a merged with c merged with b. */
186 /* Mergeability is transitive! if a can be merged with b and b
187 * with c then a also with c */
189 /* Also, if a merged with b cannot be merged with c, then
190 * either a or b cannot be merged with c either */
192 if (types_match(*a
, b
, JOB_START
, JOB_VERIFY_ACTIVE
))
194 else if (types_match(*a
, b
, JOB_START
, JOB_RELOAD
) ||
195 types_match(*a
, b
, JOB_START
, JOB_RELOAD_OR_START
) ||
196 types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_RELOAD_OR_START
) ||
197 types_match(*a
, b
, JOB_RELOAD
, JOB_RELOAD_OR_START
))
198 *a
= JOB_RELOAD_OR_START
;
199 else if (types_match(*a
, b
, JOB_START
, JOB_RESTART
) ||
200 types_match(*a
, b
, JOB_START
, JOB_TRY_RESTART
) ||
201 types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_RESTART
) ||
202 types_match(*a
, b
, JOB_RELOAD
, JOB_RESTART
) ||
203 types_match(*a
, b
, JOB_RELOAD_OR_START
, JOB_RESTART
) ||
204 types_match(*a
, b
, JOB_RELOAD_OR_START
, JOB_TRY_RESTART
) ||
205 types_match(*a
, b
, JOB_RESTART
, JOB_TRY_RESTART
))
207 else if (types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_RELOAD
))
209 else if (types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_TRY_RESTART
) ||
210 types_match(*a
, b
, JOB_RELOAD
, JOB_TRY_RESTART
))
211 *a
= JOB_TRY_RESTART
;
218 bool job_type_is_mergeable(JobType a
, JobType b
) {
219 return job_type_merge(&a
, b
) >= 0;
222 bool job_type_is_superset(JobType a
, JobType b
) {
224 /* Checks whether operation a is a "superset" of b in its
232 return b
== JOB_VERIFY_ACTIVE
;
236 b
== JOB_VERIFY_ACTIVE
;
238 case JOB_RELOAD_OR_START
:
242 b
== JOB_VERIFY_ACTIVE
;
247 b
== JOB_VERIFY_ACTIVE
||
249 b
== JOB_RELOAD_OR_START
||
250 b
== JOB_TRY_RESTART
;
252 case JOB_TRY_RESTART
:
254 b
== JOB_VERIFY_ACTIVE
||
262 bool job_type_is_conflicting(JobType a
, JobType b
) {
263 assert(a
>= 0 && a
< _JOB_TYPE_MAX
);
264 assert(b
>= 0 && b
< _JOB_TYPE_MAX
);
266 return (a
== JOB_STOP
) != (b
== JOB_STOP
);
269 bool job_type_is_redundant(JobType a
, UnitActiveState b
) {
275 b
== UNIT_ACTIVE_RELOADING
;
281 case JOB_VERIFY_ACTIVE
:
284 b
== UNIT_ACTIVE_RELOADING
;
288 b
== UNIT_ACTIVE_RELOADING
;
290 case JOB_RELOAD_OR_START
:
292 b
== UNIT_ACTIVATING
||
293 b
== UNIT_ACTIVE_RELOADING
;
297 b
== UNIT_ACTIVATING
;
299 case JOB_TRY_RESTART
:
301 b
== UNIT_ACTIVATING
;
304 assert_not_reached("Invalid job type");
308 bool job_is_runnable(Job
*j
) {
313 assert(j
->installed
);
315 /* Checks whether there is any job running for the units this
316 * job needs to be running after (in the case of a 'positive'
317 * job type) or before (in the case of a 'negative' job type
320 if (j
->type
== JOB_START
||
321 j
->type
== JOB_VERIFY_ACTIVE
||
322 j
->type
== JOB_RELOAD
||
323 j
->type
== JOB_RELOAD_OR_START
) {
325 /* Immediate result is that the job is or might be
326 * started. In this case lets wait for the
327 * dependencies, regardless whether they are
328 * starting or stopping something. */
330 SET_FOREACH(other
, j
->unit
->meta
.dependencies
[UNIT_AFTER
], i
)
335 /* Also, if something else is being stopped and we should
336 * change state after it, then lets wait. */
338 SET_FOREACH(other
, j
->unit
->meta
.dependencies
[UNIT_BEFORE
], i
)
339 if (other
->meta
.job
&&
340 (other
->meta
.job
->type
== JOB_STOP
||
341 other
->meta
.job
->type
== JOB_RESTART
||
342 other
->meta
.job
->type
== JOB_TRY_RESTART
))
345 /* This means that for a service a and a service b where b
346 * shall be started after a:
348 * start a + start b ā 1st step start a, 2nd step start b
349 * start a + stop b ā 1st step stop b, 2nd step start a
350 * stop a + start b ā 1st step stop a, 2nd step start b
351 * stop a + stop b ā 1st step stop b, 2nd step stop a
353 * This has the side effect that restarts are properly
354 * synchronized too. */
359 int job_run_and_invalidate(Job
*j
) {
363 assert(j
->installed
);
365 if (j
->in_run_queue
) {
366 LIST_REMOVE(Job
, run_queue
, j
->manager
->run_queue
, j
);
367 j
->in_run_queue
= false;
370 if (j
->state
!= JOB_WAITING
)
373 if (!job_is_runnable(j
))
376 j
->state
= JOB_RUNNING
;
377 job_add_to_dbus_queue(j
);
382 r
= unit_start(j
->unit
);
387 case JOB_VERIFY_ACTIVE
: {
388 UnitActiveState t
= unit_active_state(j
->unit
);
389 if (UNIT_IS_ACTIVE_OR_RELOADING(t
))
391 else if (t
== UNIT_ACTIVATING
)
399 r
= unit_stop(j
->unit
);
403 r
= unit_reload(j
->unit
);
406 case JOB_RELOAD_OR_START
:
407 if (unit_active_state(j
->unit
) == UNIT_ACTIVE
)
408 r
= unit_reload(j
->unit
);
410 r
= unit_start(j
->unit
);
414 UnitActiveState t
= unit_active_state(j
->unit
);
415 if (t
== UNIT_INACTIVE
|| t
== UNIT_ACTIVATING
) {
417 r
= unit_start(j
->unit
);
419 r
= unit_stop(j
->unit
);
423 case JOB_TRY_RESTART
: {
424 UnitActiveState t
= unit_active_state(j
->unit
);
425 if (t
== UNIT_INACTIVE
|| t
== UNIT_DEACTIVATING
)
427 else if (t
== UNIT_ACTIVATING
) {
429 r
= unit_start(j
->unit
);
431 r
= unit_stop(j
->unit
);
436 assert_not_reached("Unknown job type");
440 r
= job_finish_and_invalidate(j
, true);
441 else if (r
== -EAGAIN
) {
442 j
->state
= JOB_WAITING
;
445 r
= job_finish_and_invalidate(j
, false);
450 int job_finish_and_invalidate(Job
*j
, bool success
) {
457 assert(j
->installed
);
459 log_debug("Job %s/%s finished, success=%s", j
->unit
->meta
.id
, job_type_to_string(j
->type
), yes_no(success
));
460 job_add_to_dbus_queue(j
);
462 /* Patch restart jobs so that they become normal start jobs */
463 if (success
&& (j
->type
== JOB_RESTART
|| j
->type
== JOB_TRY_RESTART
)) {
465 log_debug("Converting job %s/%s ā %s/%s",
466 j
->unit
->meta
.id
, job_type_to_string(j
->type
),
467 j
->unit
->meta
.id
, job_type_to_string(JOB_START
));
469 j
->state
= JOB_RUNNING
;
472 job_add_to_run_queue(j
);
480 /* Fail depending jobs on failure */
483 if (t
== JOB_START
||
484 t
== JOB_VERIFY_ACTIVE
||
485 t
== JOB_RELOAD_OR_START
) {
487 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRED_BY
], i
)
488 if (other
->meta
.job
&&
489 (other
->meta
.job
->type
== JOB_START
||
490 other
->meta
.job
->type
== JOB_VERIFY_ACTIVE
||
491 other
->meta
.job
->type
== JOB_RELOAD_OR_START
))
492 job_finish_and_invalidate(other
->meta
.job
, false);
494 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRED_BY_OVERRIDABLE
], i
)
495 if (other
->meta
.job
&&
496 !other
->meta
.job
->override
&&
497 (other
->meta
.job
->type
== JOB_START
||
498 other
->meta
.job
->type
== JOB_VERIFY_ACTIVE
||
499 other
->meta
.job
->type
== JOB_RELOAD_OR_START
))
500 job_finish_and_invalidate(other
->meta
.job
, false);
502 } else if (t
== JOB_STOP
) {
504 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_CONFLICTS
], i
)
505 if (other
->meta
.job
&&
506 (other
->meta
.job
->type
== JOB_START
||
507 other
->meta
.job
->type
== JOB_VERIFY_ACTIVE
||
508 other
->meta
.job
->type
== JOB_RELOAD_OR_START
))
509 job_finish_and_invalidate(other
->meta
.job
, false);
513 /* Try to start the next jobs that can be started */
514 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_AFTER
], i
)
516 job_add_to_run_queue(other
->meta
.job
);
517 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_BEFORE
], i
)
519 job_add_to_run_queue(other
->meta
.job
);
524 void job_add_to_run_queue(Job
*j
) {
526 assert(j
->installed
);
531 LIST_PREPEND(Job
, run_queue
, j
->manager
->run_queue
, j
);
532 j
->in_run_queue
= true;
535 void job_add_to_dbus_queue(Job
*j
) {
537 assert(j
->installed
);
539 if (j
->in_dbus_queue
)
542 LIST_PREPEND(Job
, dbus_queue
, j
->manager
->dbus_job_queue
, j
);
543 j
->in_dbus_queue
= true;
546 char *job_dbus_path(Job
*j
) {
551 if (asprintf(&p
, "/org/freedesktop/systemd1/job/%lu", (unsigned long) j
->id
) < 0)
557 static const char* const job_state_table
[_JOB_STATE_MAX
] = {
558 [JOB_WAITING
] = "waiting",
559 [JOB_RUNNING
] = "running"
562 DEFINE_STRING_TABLE_LOOKUP(job_state
, JobState
);
564 static const char* const job_type_table
[_JOB_TYPE_MAX
] = {
565 [JOB_START
] = "start",
566 [JOB_VERIFY_ACTIVE
] = "verify-active",
568 [JOB_RELOAD
] = "reload",
569 [JOB_RELOAD_OR_START
] = "reload-or-start",
570 [JOB_RESTART
] = "restart",
571 [JOB_TRY_RESTART
] = "try-restart",
574 DEFINE_STRING_TABLE_LOOKUP(job_type
, JobType
);
576 static const char* const job_mode_table
[_JOB_MODE_MAX
] = {
578 [JOB_REPLACE
] = "replace"
581 DEFINE_STRING_TABLE_LOOKUP(job_mode
, JobMode
);