]>
git.ipfire.org Git - people/ms/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 bus_job_send_removed_signal(j
);
60 if (j
->unit
->meta
.job
== j
)
61 j
->unit
->meta
.job
= NULL
;
63 hashmap_remove(j
->manager
->jobs
, UINT32_TO_PTR(j
->id
));
67 /* Detach from next 'smaller' objects */
68 manager_transaction_unlink_job(j
->manager
, j
, true);
71 LIST_REMOVE(Job
, run_queue
, j
->manager
->run_queue
, j
);
74 LIST_REMOVE(Job
, dbus_queue
, j
->manager
->dbus_job_queue
, j
);
79 JobDependency
* job_dependency_new(Job
*subject
, Job
*object
, bool matters
) {
84 /* Adds a new job link, which encodes that the 'subject' job
85 * needs the 'object' job in some way. If 'subject' is NULL
86 * this means the 'anchor' job (i.e. the one the user
87 * explcitily asked for) is the requester. */
89 if (!(l
= new0(JobDependency
, 1)))
97 LIST_PREPEND(JobDependency
, subject
, subject
->subject_list
, l
);
99 LIST_PREPEND(JobDependency
, subject
, object
->manager
->transaction_anchor
, l
);
101 LIST_PREPEND(JobDependency
, object
, object
->object_list
, l
);
106 void job_dependency_free(JobDependency
*l
) {
110 LIST_REMOVE(JobDependency
, subject
, l
->subject
->subject_list
, l
);
112 LIST_REMOVE(JobDependency
, subject
, l
->object
->manager
->transaction_anchor
, l
);
114 LIST_REMOVE(JobDependency
, object
, l
->object
->object_list
, l
);
119 void job_dependency_delete(Job
*subject
, Job
*object
, bool *matters
) {
124 LIST_FOREACH(object
, l
, object
->object_list
) {
125 assert(l
->object
== object
);
127 if (l
->subject
== subject
)
138 *matters
= l
->matters
;
140 job_dependency_free(l
);
143 void job_dump(Job
*j
, FILE*f
, const char *prefix
) {
151 "%s\tAction: %s ā %s\n"
155 prefix
, unit_id(j
->unit
), job_type_to_string(j
->type
),
156 prefix
, job_state_to_string(j
->state
),
157 prefix
, yes_no(j
->forced
));
160 bool job_is_anchor(Job
*j
) {
165 LIST_FOREACH(object
, l
, j
->object_list
)
172 static bool types_match(JobType a
, JobType b
, JobType c
, JobType d
) {
174 (a
== c
&& b
== d
) ||
178 int job_type_merge(JobType
*a
, JobType b
) {
182 /* Merging is associative! a merged with b merged with c is
183 * the same as a merged with c merged with b. */
185 /* Mergeability is transitive! if a can be merged with b and b
186 * with c then a also with c */
188 /* Also, if a merged with b cannot be merged with c, then
189 * either a or b cannot be merged with c either */
191 if (types_match(*a
, b
, JOB_START
, JOB_VERIFY_ACTIVE
))
193 else if (types_match(*a
, b
, JOB_START
, JOB_RELOAD
) ||
194 types_match(*a
, b
, JOB_START
, JOB_RELOAD_OR_START
) ||
195 types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_RELOAD_OR_START
) ||
196 types_match(*a
, b
, JOB_RELOAD
, JOB_RELOAD_OR_START
))
197 *a
= JOB_RELOAD_OR_START
;
198 else if (types_match(*a
, b
, JOB_START
, JOB_RESTART
) ||
199 types_match(*a
, b
, JOB_START
, JOB_TRY_RESTART
) ||
200 types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_RESTART
) ||
201 types_match(*a
, b
, JOB_RELOAD
, JOB_RESTART
) ||
202 types_match(*a
, b
, JOB_RELOAD_OR_START
, JOB_RESTART
) ||
203 types_match(*a
, b
, JOB_RELOAD_OR_START
, JOB_TRY_RESTART
) ||
204 types_match(*a
, b
, JOB_RESTART
, JOB_TRY_RESTART
))
206 else if (types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_RELOAD
))
208 else if (types_match(*a
, b
, JOB_VERIFY_ACTIVE
, JOB_TRY_RESTART
) ||
209 types_match(*a
, b
, JOB_RELOAD
, JOB_TRY_RESTART
))
210 *a
= JOB_TRY_RESTART
;
217 bool job_type_is_mergeable(JobType a
, JobType b
) {
218 return job_type_merge(&a
, b
) >= 0;
221 bool job_type_is_superset(JobType a
, JobType b
) {
223 /* Checks whether operation a is a "superset" of b in its
231 return b
== JOB_VERIFY_ACTIVE
;
235 b
== JOB_VERIFY_ACTIVE
;
237 case JOB_RELOAD_OR_START
:
241 b
== JOB_VERIFY_ACTIVE
;
246 b
== JOB_VERIFY_ACTIVE
||
248 b
== JOB_RELOAD_OR_START
||
249 b
== JOB_TRY_RESTART
;
251 case JOB_TRY_RESTART
:
253 b
== JOB_VERIFY_ACTIVE
||
261 bool job_type_is_conflicting(JobType a
, JobType b
) {
262 assert(a
>= 0 && a
< _JOB_TYPE_MAX
);
263 assert(b
>= 0 && b
< _JOB_TYPE_MAX
);
265 return (a
== JOB_STOP
) != (b
== JOB_STOP
);
268 bool job_type_is_redundant(JobType a
, UnitActiveState b
) {
274 b
== UNIT_ACTIVE_RELOADING
;
280 case JOB_VERIFY_ACTIVE
:
283 b
== UNIT_ACTIVE_RELOADING
;
287 b
== UNIT_ACTIVE_RELOADING
;
289 case JOB_RELOAD_OR_START
:
291 b
== UNIT_ACTIVATING
||
292 b
== UNIT_ACTIVE_RELOADING
;
296 b
== UNIT_ACTIVATING
;
298 case JOB_TRY_RESTART
:
300 b
== UNIT_ACTIVATING
;
303 assert_not_reached("Invalid job type");
307 bool job_is_runnable(Job
*j
) {
312 assert(j
->installed
);
314 /* Checks whether there is any job running for the units this
315 * job needs to be running after (in the case of a 'positive'
316 * job type) or before (in the case of a 'negative' job type
319 if (j
->type
== JOB_START
||
320 j
->type
== JOB_VERIFY_ACTIVE
||
321 j
->type
== JOB_RELOAD
||
322 j
->type
== JOB_RELOAD_OR_START
) {
324 /* Immediate result is that the job is or might be
325 * started. In this case lets wait for the
326 * dependencies, regardless whether they are
327 * starting or stopping something. */
329 SET_FOREACH(other
, j
->unit
->meta
.dependencies
[UNIT_AFTER
], i
)
334 /* Also, if something else is being stopped and we should
335 * change state after it, then lets wait. */
337 SET_FOREACH(other
, j
->unit
->meta
.dependencies
[UNIT_BEFORE
], i
)
338 if (other
->meta
.job
&&
339 (other
->meta
.job
->type
== JOB_STOP
||
340 other
->meta
.job
->type
== JOB_RESTART
||
341 other
->meta
.job
->type
== JOB_TRY_RESTART
))
344 /* This means that for a service a and a service b where b
345 * shall be started after a:
347 * start a + start b ā 1st step start a, 2nd step start b
348 * start a + stop b ā 1st step stop b, 2nd step start a
349 * stop a + start b ā 1st step stop a, 2nd step start b
350 * stop a + stop b ā 1st step stop b, 2nd step stop a
352 * This has the side effect that restarts are properly
353 * synchronized too. */
358 int job_run_and_invalidate(Job
*j
) {
362 assert(j
->installed
);
364 if (j
->in_run_queue
) {
365 LIST_REMOVE(Job
, run_queue
, j
->manager
->run_queue
, j
);
366 j
->in_run_queue
= false;
369 if (j
->state
!= JOB_WAITING
)
372 if (!job_is_runnable(j
))
375 j
->state
= JOB_RUNNING
;
376 job_add_to_dbus_queue(j
);
381 r
= unit_start(j
->unit
);
386 case JOB_VERIFY_ACTIVE
: {
387 UnitActiveState t
= unit_active_state(j
->unit
);
388 if (UNIT_IS_ACTIVE_OR_RELOADING(t
))
390 else if (t
== UNIT_ACTIVATING
)
398 r
= unit_stop(j
->unit
);
402 r
= unit_reload(j
->unit
);
405 case JOB_RELOAD_OR_START
:
406 if (unit_active_state(j
->unit
) == UNIT_ACTIVE
)
407 r
= unit_reload(j
->unit
);
409 r
= unit_start(j
->unit
);
413 UnitActiveState t
= unit_active_state(j
->unit
);
414 if (t
== UNIT_INACTIVE
|| t
== UNIT_ACTIVATING
) {
416 r
= unit_start(j
->unit
);
418 r
= unit_stop(j
->unit
);
422 case JOB_TRY_RESTART
: {
423 UnitActiveState t
= unit_active_state(j
->unit
);
424 if (t
== UNIT_INACTIVE
|| t
== UNIT_DEACTIVATING
)
426 else if (t
== UNIT_ACTIVATING
) {
428 r
= unit_start(j
->unit
);
430 r
= unit_stop(j
->unit
);
435 assert_not_reached("Unknown job type");
439 r
= job_finish_and_invalidate(j
, true);
440 else if (r
== -EAGAIN
) {
441 j
->state
= JOB_WAITING
;
444 r
= job_finish_and_invalidate(j
, false);
449 int job_finish_and_invalidate(Job
*j
, bool success
) {
456 assert(j
->installed
);
458 log_debug("Job %s/%s finished, success=%s", unit_id(j
->unit
), job_type_to_string(j
->type
), yes_no(success
));
459 job_add_to_dbus_queue(j
);
461 /* Patch restart jobs so that they become normal start jobs */
462 if (success
&& (j
->type
== JOB_RESTART
|| j
->type
== JOB_TRY_RESTART
)) {
464 log_debug("Converting job %s/%s ā %s/%s",
465 unit_id(j
->unit
), job_type_to_string(j
->type
),
466 unit_id(j
->unit
), job_type_to_string(JOB_START
));
468 j
->state
= JOB_RUNNING
;
471 job_add_to_run_queue(j
);
479 /* Fail depending jobs on failure */
482 if (t
== JOB_START
||
483 t
== JOB_VERIFY_ACTIVE
||
484 t
== JOB_RELOAD_OR_START
) {
486 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_REQUIRED_BY
], i
)
487 if (other
->meta
.job
&&
488 (other
->meta
.job
->type
== JOB_START
||
489 other
->meta
.job
->type
== JOB_VERIFY_ACTIVE
||
490 other
->meta
.job
->type
== JOB_RELOAD_OR_START
))
491 job_finish_and_invalidate(other
->meta
.job
, false);
493 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_SOFT_REQUIRED_BY
], i
)
494 if (other
->meta
.job
&&
495 !other
->meta
.job
->forced
&&
496 (other
->meta
.job
->type
== JOB_START
||
497 other
->meta
.job
->type
== JOB_VERIFY_ACTIVE
||
498 other
->meta
.job
->type
== JOB_RELOAD_OR_START
))
499 job_finish_and_invalidate(other
->meta
.job
, false);
501 } else if (t
== JOB_STOP
) {
503 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_CONFLICTS
], i
)
504 if (other
->meta
.job
&&
505 (other
->meta
.job
->type
== JOB_START
||
506 other
->meta
.job
->type
== JOB_VERIFY_ACTIVE
||
507 other
->meta
.job
->type
== JOB_RELOAD_OR_START
))
508 job_finish_and_invalidate(other
->meta
.job
, false);
512 /* Try to start the next jobs that can be started */
513 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_AFTER
], i
)
515 job_add_to_run_queue(other
->meta
.job
);
516 SET_FOREACH(other
, u
->meta
.dependencies
[UNIT_BEFORE
], i
)
518 job_add_to_run_queue(other
->meta
.job
);
523 void job_add_to_run_queue(Job
*j
) {
525 assert(j
->installed
);
530 LIST_PREPEND(Job
, run_queue
, j
->manager
->run_queue
, j
);
531 j
->in_run_queue
= true;
534 void job_add_to_dbus_queue(Job
*j
) {
536 assert(j
->installed
);
538 if (j
->in_dbus_queue
)
541 LIST_PREPEND(Job
, dbus_queue
, j
->manager
->dbus_job_queue
, j
);
542 j
->in_dbus_queue
= true;
545 char *job_dbus_path(Job
*j
) {
550 if (asprintf(&p
, "/org/freedesktop/systemd1/job/%lu", (unsigned long) j
->id
) < 0)
556 static const char* const job_state_table
[_JOB_STATE_MAX
] = {
557 [JOB_WAITING
] = "waiting",
558 [JOB_RUNNING
] = "running"
561 DEFINE_STRING_TABLE_LOOKUP(job_state
, JobState
);
563 static const char* const job_type_table
[_JOB_TYPE_MAX
] = {
564 [JOB_START
] = "start",
565 [JOB_VERIFY_ACTIVE
] = "verify-active",
567 [JOB_RELOAD
] = "reload",
568 [JOB_RELOAD_OR_START
] = "reload-or-start",
569 [JOB_RESTART
] = "restart",
570 [JOB_TRY_RESTART
] = "try-restart",
573 DEFINE_STRING_TABLE_LOOKUP(job_type
, JobType
);
575 static const char* const job_mode_table
[_JOB_MODE_MAX
] = {
577 [JOB_REPLACE
] = "replace"
580 DEFINE_STRING_TABLE_LOOKUP(job_mode
, JobMode
);