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;
63 unit_add_to_gc_queue(j->unit);
66 hashmap_remove(j->manager->jobs, UINT32_TO_PTR(j->id));
70 /* Detach from next 'smaller' objects */
71 manager_transaction_unlink_job(j->manager, j, true);
74 LIST_REMOVE(Job, run_queue, j->manager->run_queue, j);
77 LIST_REMOVE(Job, dbus_queue, j->manager->dbus_job_queue, j);
82 JobDependency* job_dependency_new(Job *subject, Job *object, bool matters) {
87 /* Adds a new job link, which encodes that the 'subject' job
88 * needs the 'object' job in some way. If 'subject' is NULL
89 * this means the 'anchor' job (i.e. the one the user
90 * explcitily asked for) is the requester. */
92 if (!(l = new0(JobDependency, 1)))
100 LIST_PREPEND(JobDependency, subject, subject->subject_list, l);
102 LIST_PREPEND(JobDependency, subject, object->manager->transaction_anchor, l);
104 LIST_PREPEND(JobDependency, object, object->object_list, l);
109 void job_dependency_free(JobDependency *l) {
113 LIST_REMOVE(JobDependency, subject, l->subject->subject_list, l);
115 LIST_REMOVE(JobDependency, subject, l->object->manager->transaction_anchor, l);
117 LIST_REMOVE(JobDependency, object, l->object->object_list, l);
122 void job_dependency_delete(Job *subject, Job *object, bool *matters) {
127 LIST_FOREACH(object, l, object->object_list) {
128 assert(l->object == object);
130 if (l->subject == subject)
141 *matters = l->matters;
143 job_dependency_free(l);
146 void job_dump(Job *j, FILE*f, const char *prefix) {
154 "%s\tAction: %s -> %s\n"
158 prefix, j->unit->meta.id, job_type_to_string(j->type),
159 prefix, job_state_to_string(j->state),
160 prefix, yes_no(j->override));
163 bool job_is_anchor(Job *j) {
168 LIST_FOREACH(object, l, j->object_list)
175 static bool types_match(JobType a, JobType b, JobType c, JobType d) {
177 (a == c && b == d) ||
181 int job_type_merge(JobType *a, JobType b) {
185 /* Merging is associative! a merged with b merged with c is
186 * the same as a merged with c merged with b. */
188 /* Mergeability is transitive! if a can be merged with b and b
189 * with c then a also with c */
191 /* Also, if a merged with b cannot be merged with c, then
192 * either a or b cannot be merged with c either */
194 if (types_match(*a, b, JOB_START, JOB_VERIFY_ACTIVE))
196 else if (types_match(*a, b, JOB_START, JOB_RELOAD) ||
197 types_match(*a, b, JOB_START, JOB_RELOAD_OR_START) ||
198 types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD_OR_START) ||
199 types_match(*a, b, JOB_RELOAD, JOB_RELOAD_OR_START))
200 *a = JOB_RELOAD_OR_START;
201 else if (types_match(*a, b, JOB_START, JOB_RESTART) ||
202 types_match(*a, b, JOB_START, JOB_TRY_RESTART) ||
203 types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RESTART) ||
204 types_match(*a, b, JOB_RELOAD, JOB_RESTART) ||
205 types_match(*a, b, JOB_RELOAD_OR_START, JOB_RESTART) ||
206 types_match(*a, b, JOB_RELOAD_OR_START, JOB_TRY_RESTART) ||
207 types_match(*a, b, JOB_RESTART, JOB_TRY_RESTART))
209 else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD))
211 else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_TRY_RESTART) ||
212 types_match(*a, b, JOB_RELOAD, JOB_TRY_RESTART))
213 *a = JOB_TRY_RESTART;
220 bool job_type_is_mergeable(JobType a, JobType b) {
221 return job_type_merge(&a, b) >= 0;
224 bool job_type_is_superset(JobType a, JobType b) {
226 /* Checks whether operation a is a "superset" of b in its
234 return b == JOB_VERIFY_ACTIVE;
238 b == JOB_VERIFY_ACTIVE;
240 case JOB_RELOAD_OR_START:
244 b == JOB_VERIFY_ACTIVE;
249 b == JOB_VERIFY_ACTIVE ||
251 b == JOB_RELOAD_OR_START ||
252 b == JOB_TRY_RESTART;
254 case JOB_TRY_RESTART:
256 b == JOB_VERIFY_ACTIVE ||
264 bool job_type_is_conflicting(JobType a, JobType b) {
265 assert(a >= 0 && a < _JOB_TYPE_MAX);
266 assert(b >= 0 && b < _JOB_TYPE_MAX);
268 return (a == JOB_STOP) != (b == JOB_STOP);
271 bool job_type_is_redundant(JobType a, UnitActiveState b) {
277 b == UNIT_ACTIVE_RELOADING;
283 case JOB_VERIFY_ACTIVE:
286 b == UNIT_ACTIVE_RELOADING;
290 b == UNIT_ACTIVE_RELOADING;
292 case JOB_RELOAD_OR_START:
294 b == UNIT_ACTIVATING ||
295 b == UNIT_ACTIVE_RELOADING;
299 b == UNIT_ACTIVATING;
301 case JOB_TRY_RESTART:
303 b == UNIT_ACTIVATING;
306 assert_not_reached("Invalid job type");
310 bool job_is_runnable(Job *j) {
315 assert(j->installed);
317 /* Checks whether there is any job running for the units this
318 * job needs to be running after (in the case of a 'positive'
319 * job type) or before (in the case of a 'negative' job type
322 if (j->type == JOB_START ||
323 j->type == JOB_VERIFY_ACTIVE ||
324 j->type == JOB_RELOAD ||
325 j->type == JOB_RELOAD_OR_START) {
327 /* Immediate result is that the job is or might be
328 * started. In this case lets wait for the
329 * dependencies, regardless whether they are
330 * starting or stopping something. */
332 SET_FOREACH(other, j->unit->meta.dependencies[UNIT_AFTER], i)
337 /* Also, if something else is being stopped and we should
338 * change state after it, then lets wait. */
340 SET_FOREACH(other, j->unit->meta.dependencies[UNIT_BEFORE], i)
341 if (other->meta.job &&
342 (other->meta.job->type == JOB_STOP ||
343 other->meta.job->type == JOB_RESTART ||
344 other->meta.job->type == JOB_TRY_RESTART))
347 /* This means that for a service a and a service b where b
348 * shall be started after a:
350 * start a + start b → 1st step start a, 2nd step start b
351 * start a + stop b → 1st step stop b, 2nd step start a
352 * stop a + start b → 1st step stop a, 2nd step start b
353 * stop a + stop b → 1st step stop b, 2nd step stop a
355 * This has the side effect that restarts are properly
356 * synchronized too. */
361 int job_run_and_invalidate(Job *j) {
365 assert(j->installed);
367 if (j->in_run_queue) {
368 LIST_REMOVE(Job, run_queue, j->manager->run_queue, j);
369 j->in_run_queue = false;
372 if (j->state != JOB_WAITING)
375 if (!job_is_runnable(j))
378 j->state = JOB_RUNNING;
379 job_add_to_dbus_queue(j);
384 r = unit_start(j->unit);
389 case JOB_VERIFY_ACTIVE: {
390 UnitActiveState t = unit_active_state(j->unit);
391 if (UNIT_IS_ACTIVE_OR_RELOADING(t))
393 else if (t == UNIT_ACTIVATING)
401 r = unit_stop(j->unit);
405 r = unit_reload(j->unit);
408 case JOB_RELOAD_OR_START:
409 if (unit_active_state(j->unit) == UNIT_ACTIVE)
410 r = unit_reload(j->unit);
412 r = unit_start(j->unit);
416 UnitActiveState t = unit_active_state(j->unit);
417 if (t == UNIT_INACTIVE || t == UNIT_ACTIVATING) {
419 r = unit_start(j->unit);
421 r = unit_stop(j->unit);
425 case JOB_TRY_RESTART: {
426 UnitActiveState t = unit_active_state(j->unit);
427 if (t == UNIT_INACTIVE || t == UNIT_DEACTIVATING)
429 else if (t == UNIT_ACTIVATING) {
431 r = unit_start(j->unit);
433 r = unit_stop(j->unit);
438 assert_not_reached("Unknown job type");
442 r = job_finish_and_invalidate(j, true);
443 else if (r == -EAGAIN) {
444 j->state = JOB_WAITING;
447 r = job_finish_and_invalidate(j, false);
452 int job_finish_and_invalidate(Job *j, bool success) {
459 assert(j->installed);
461 log_debug("Job %s/%s finished, success=%s", j->unit->meta.id, job_type_to_string(j->type), yes_no(success));
462 job_add_to_dbus_queue(j);
464 /* Patch restart jobs so that they become normal start jobs */
465 if (success && (j->type == JOB_RESTART || j->type == JOB_TRY_RESTART)) {
467 log_debug("Converting job %s/%s -> %s/%s",
468 j->unit->meta.id, job_type_to_string(j->type),
469 j->unit->meta.id, job_type_to_string(JOB_START));
471 j->state = JOB_RUNNING;
474 job_add_to_run_queue(j);
482 /* Fail depending jobs on failure */
485 if (t == JOB_START ||
486 t == JOB_VERIFY_ACTIVE ||
487 t == JOB_RELOAD_OR_START) {
489 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
490 if (other->meta.job &&
491 (other->meta.job->type == JOB_START ||
492 other->meta.job->type == JOB_VERIFY_ACTIVE ||
493 other->meta.job->type == JOB_RELOAD_OR_START))
494 job_finish_and_invalidate(other->meta.job, false);
496 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
497 if (other->meta.job &&
498 !other->meta.job->override &&
499 (other->meta.job->type == JOB_START ||
500 other->meta.job->type == JOB_VERIFY_ACTIVE ||
501 other->meta.job->type == JOB_RELOAD_OR_START))
502 job_finish_and_invalidate(other->meta.job, false);
504 } else if (t == JOB_STOP) {
506 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
507 if (other->meta.job &&
508 (other->meta.job->type == JOB_START ||
509 other->meta.job->type == JOB_VERIFY_ACTIVE ||
510 other->meta.job->type == JOB_RELOAD_OR_START))
511 job_finish_and_invalidate(other->meta.job, false);
515 /* Try to start the next jobs that can be started */
516 SET_FOREACH(other, u->meta.dependencies[UNIT_AFTER], i)
518 job_add_to_run_queue(other->meta.job);
519 SET_FOREACH(other, u->meta.dependencies[UNIT_BEFORE], i)
521 job_add_to_run_queue(other->meta.job);
526 void job_add_to_run_queue(Job *j) {
528 assert(j->installed);
533 LIST_PREPEND(Job, run_queue, j->manager->run_queue, j);
534 j->in_run_queue = true;
537 void job_add_to_dbus_queue(Job *j) {
539 assert(j->installed);
541 if (j->in_dbus_queue)
544 if (set_isempty(j->manager->subscribed)) {
545 j->sent_dbus_new_signal = true;
549 LIST_PREPEND(Job, dbus_queue, j->manager->dbus_job_queue, j);
550 j->in_dbus_queue = true;
553 char *job_dbus_path(Job *j) {
558 if (asprintf(&p, "/org/freedesktop/systemd1/job/%lu", (unsigned long) j->id) < 0)
564 static const char* const job_state_table[_JOB_STATE_MAX] = {
565 [JOB_WAITING] = "waiting",
566 [JOB_RUNNING] = "running"
569 DEFINE_STRING_TABLE_LOOKUP(job_state, JobState);
571 static const char* const job_type_table[_JOB_TYPE_MAX] = {
572 [JOB_START] = "start",
573 [JOB_VERIFY_ACTIVE] = "verify-active",
575 [JOB_RELOAD] = "reload",
576 [JOB_RELOAD_OR_START] = "reload-or-start",
577 [JOB_RESTART] = "restart",
578 [JOB_TRY_RESTART] = "try-restart",
581 DEFINE_STRING_TABLE_LOOKUP(job_type, JobType);
583 static const char* const job_mode_table[_JOB_MODE_MAX] = {
585 [JOB_REPLACE] = "replace",
586 [JOB_ISOLATE] = "isolate"
589 DEFINE_STRING_TABLE_LOOKUP(job_mode, JobMode);