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, !j->failed);
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);
83 JobDependency* job_dependency_new(Job *subject, Job *object, bool matters) {
88 /* Adds a new job link, which encodes that the 'subject' job
89 * needs the 'object' job in some way. If 'subject' is NULL
90 * this means the 'anchor' job (i.e. the one the user
91 * explcitily asked for) is the requester. */
93 if (!(l = new0(JobDependency, 1)))
101 LIST_PREPEND(JobDependency, subject, subject->subject_list, l);
103 LIST_PREPEND(JobDependency, subject, object->manager->transaction_anchor, l);
105 LIST_PREPEND(JobDependency, object, object->object_list, l);
110 void job_dependency_free(JobDependency *l) {
114 LIST_REMOVE(JobDependency, subject, l->subject->subject_list, l);
116 LIST_REMOVE(JobDependency, subject, l->object->manager->transaction_anchor, l);
118 LIST_REMOVE(JobDependency, object, l->object->object_list, l);
123 void job_dependency_delete(Job *subject, Job *object, bool *matters) {
128 LIST_FOREACH(object, l, object->object_list) {
129 assert(l->object == object);
131 if (l->subject == subject)
142 *matters = l->matters;
144 job_dependency_free(l);
147 void job_dump(Job *j, FILE*f, const char *prefix) {
156 "%s\tAction: %s -> %s\n"
160 prefix, j->unit->meta.id, job_type_to_string(j->type),
161 prefix, job_state_to_string(j->state),
162 prefix, yes_no(j->override));
165 bool job_is_anchor(Job *j) {
170 LIST_FOREACH(object, l, j->object_list)
177 static bool types_match(JobType a, JobType b, JobType c, JobType d) {
179 (a == c && b == d) ||
183 int job_type_merge(JobType *a, JobType b) {
187 /* Merging is associative! a merged with b merged with c is
188 * the same as a merged with c merged with b. */
190 /* Mergeability is transitive! if a can be merged with b and b
191 * with c then a also with c */
193 /* Also, if a merged with b cannot be merged with c, then
194 * either a or b cannot be merged with c either */
196 if (types_match(*a, b, JOB_START, JOB_VERIFY_ACTIVE))
198 else if (types_match(*a, b, JOB_START, JOB_RELOAD) ||
199 types_match(*a, b, JOB_START, JOB_RELOAD_OR_START) ||
200 types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD_OR_START) ||
201 types_match(*a, b, JOB_RELOAD, JOB_RELOAD_OR_START))
202 *a = JOB_RELOAD_OR_START;
203 else if (types_match(*a, b, JOB_START, JOB_RESTART) ||
204 types_match(*a, b, JOB_START, JOB_TRY_RESTART) ||
205 types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RESTART) ||
206 types_match(*a, b, JOB_RELOAD, JOB_RESTART) ||
207 types_match(*a, b, JOB_RELOAD_OR_START, JOB_RESTART) ||
208 types_match(*a, b, JOB_RELOAD_OR_START, JOB_TRY_RESTART) ||
209 types_match(*a, b, JOB_RESTART, JOB_TRY_RESTART))
211 else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD))
213 else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_TRY_RESTART) ||
214 types_match(*a, b, JOB_RELOAD, JOB_TRY_RESTART))
215 *a = JOB_TRY_RESTART;
222 bool job_type_is_mergeable(JobType a, JobType b) {
223 return job_type_merge(&a, b) >= 0;
226 bool job_type_is_superset(JobType a, JobType b) {
228 /* Checks whether operation a is a "superset" of b in its
236 return b == JOB_VERIFY_ACTIVE;
240 b == JOB_VERIFY_ACTIVE;
242 case JOB_RELOAD_OR_START:
246 b == JOB_VERIFY_ACTIVE;
251 b == JOB_VERIFY_ACTIVE ||
253 b == JOB_RELOAD_OR_START ||
254 b == JOB_TRY_RESTART;
256 case JOB_TRY_RESTART:
258 b == JOB_VERIFY_ACTIVE ||
266 bool job_type_is_conflicting(JobType a, JobType b) {
267 assert(a >= 0 && a < _JOB_TYPE_MAX);
268 assert(b >= 0 && b < _JOB_TYPE_MAX);
270 return (a == JOB_STOP) != (b == JOB_STOP);
273 bool job_type_is_redundant(JobType a, UnitActiveState b) {
283 b == UNIT_INACTIVE ||
284 b == UNIT_MAINTENANCE;
286 case JOB_VERIFY_ACTIVE:
295 case JOB_RELOAD_OR_START:
297 b == UNIT_ACTIVATING ||
302 b == UNIT_ACTIVATING;
304 case JOB_TRY_RESTART:
306 b == UNIT_ACTIVATING;
309 assert_not_reached("Invalid job type");
313 bool job_is_runnable(Job *j) {
318 assert(j->installed);
320 /* Checks whether there is any job running for the units this
321 * job needs to be running after (in the case of a 'positive'
322 * job type) or before (in the case of a 'negative' job type
325 if (j->type == JOB_START ||
326 j->type == JOB_VERIFY_ACTIVE ||
327 j->type == JOB_RELOAD ||
328 j->type == JOB_RELOAD_OR_START) {
330 /* Immediate result is that the job is or might be
331 * started. In this case lets wait for the
332 * dependencies, regardless whether they are
333 * starting or stopping something. */
335 SET_FOREACH(other, j->unit->meta.dependencies[UNIT_AFTER], i)
340 /* Also, if something else is being stopped and we should
341 * change state after it, then lets wait. */
343 SET_FOREACH(other, j->unit->meta.dependencies[UNIT_BEFORE], i)
344 if (other->meta.job &&
345 (other->meta.job->type == JOB_STOP ||
346 other->meta.job->type == JOB_RESTART ||
347 other->meta.job->type == JOB_TRY_RESTART))
350 /* This means that for a service a and a service b where b
351 * shall be started after a:
353 * start a + start b → 1st step start a, 2nd step start b
354 * start a + stop b → 1st step stop b, 2nd step start a
355 * stop a + start b → 1st step stop a, 2nd step start b
356 * stop a + stop b → 1st step stop b, 2nd step stop a
358 * This has the side effect that restarts are properly
359 * synchronized too. */
364 int job_run_and_invalidate(Job *j) {
368 assert(j->installed);
370 if (j->in_run_queue) {
371 LIST_REMOVE(Job, run_queue, j->manager->run_queue, j);
372 j->in_run_queue = false;
375 if (j->state != JOB_WAITING)
378 if (!job_is_runnable(j))
381 j->state = JOB_RUNNING;
382 job_add_to_dbus_queue(j);
387 r = unit_start(j->unit);
392 case JOB_VERIFY_ACTIVE: {
393 UnitActiveState t = unit_active_state(j->unit);
394 if (UNIT_IS_ACTIVE_OR_RELOADING(t))
396 else if (t == UNIT_ACTIVATING)
404 r = unit_stop(j->unit);
408 r = unit_reload(j->unit);
411 case JOB_RELOAD_OR_START:
412 if (unit_active_state(j->unit) == UNIT_ACTIVE)
413 r = unit_reload(j->unit);
415 r = unit_start(j->unit);
419 UnitActiveState t = unit_active_state(j->unit);
420 if (t == UNIT_INACTIVE || t == UNIT_MAINTENANCE || t == UNIT_ACTIVATING) {
422 r = unit_start(j->unit);
424 r = unit_stop(j->unit);
428 case JOB_TRY_RESTART: {
429 UnitActiveState t = unit_active_state(j->unit);
430 if (t == UNIT_INACTIVE || t == UNIT_MAINTENANCE || t == UNIT_DEACTIVATING)
432 else if (t == UNIT_ACTIVATING) {
434 r = unit_start(j->unit);
436 r = unit_stop(j->unit);
441 assert_not_reached("Unknown job type");
445 r = job_finish_and_invalidate(j, true);
446 else if (r == -EAGAIN) {
447 j->state = JOB_WAITING;
450 r = job_finish_and_invalidate(j, false);
455 int job_finish_and_invalidate(Job *j, bool success) {
462 assert(j->installed);
464 log_debug("Job %s/%s finished, success=%s", j->unit->meta.id, job_type_to_string(j->type), yes_no(success));
465 job_add_to_dbus_queue(j);
467 /* Patch restart jobs so that they become normal start jobs */
468 if (success && (j->type == JOB_RESTART || j->type == JOB_TRY_RESTART)) {
470 log_debug("Converting job %s/%s -> %s/%s",
471 j->unit->meta.id, job_type_to_string(j->type),
472 j->unit->meta.id, job_type_to_string(JOB_START));
474 j->state = JOB_RUNNING;
477 job_add_to_run_queue(j);
481 j->failed = !success;
486 /* Fail depending jobs on failure */
489 if (t == JOB_START ||
490 t == JOB_VERIFY_ACTIVE ||
491 t == JOB_RELOAD_OR_START) {
493 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
494 if (other->meta.job &&
495 (other->meta.job->type == JOB_START ||
496 other->meta.job->type == JOB_VERIFY_ACTIVE ||
497 other->meta.job->type == JOB_RELOAD_OR_START))
498 job_finish_and_invalidate(other->meta.job, false);
500 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
501 if (other->meta.job &&
502 !other->meta.job->override &&
503 (other->meta.job->type == JOB_START ||
504 other->meta.job->type == JOB_VERIFY_ACTIVE ||
505 other->meta.job->type == JOB_RELOAD_OR_START))
506 job_finish_and_invalidate(other->meta.job, false);
508 } else if (t == JOB_STOP) {
510 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
511 if (other->meta.job &&
512 (other->meta.job->type == JOB_START ||
513 other->meta.job->type == JOB_VERIFY_ACTIVE ||
514 other->meta.job->type == JOB_RELOAD_OR_START))
515 job_finish_and_invalidate(other->meta.job, false);
519 /* Try to start the next jobs that can be started */
520 SET_FOREACH(other, u->meta.dependencies[UNIT_AFTER], i)
522 job_add_to_run_queue(other->meta.job);
523 SET_FOREACH(other, u->meta.dependencies[UNIT_BEFORE], i)
525 job_add_to_run_queue(other->meta.job);
530 void job_add_to_run_queue(Job *j) {
532 assert(j->installed);
537 LIST_PREPEND(Job, run_queue, j->manager->run_queue, j);
538 j->in_run_queue = true;
541 void job_add_to_dbus_queue(Job *j) {
543 assert(j->installed);
545 if (j->in_dbus_queue)
548 /* We don't check if anybody is subscribed here, since this
549 * job might just have been created and not yet assigned to a
550 * connection/client. */
552 LIST_PREPEND(Job, dbus_queue, j->manager->dbus_job_queue, j);
553 j->in_dbus_queue = true;
556 char *job_dbus_path(Job *j) {
561 if (asprintf(&p, "/org/freedesktop/systemd1/job/%lu", (unsigned long) j->id) < 0)
567 static const char* const job_state_table[_JOB_STATE_MAX] = {
568 [JOB_WAITING] = "waiting",
569 [JOB_RUNNING] = "running"
572 DEFINE_STRING_TABLE_LOOKUP(job_state, JobState);
574 static const char* const job_type_table[_JOB_TYPE_MAX] = {
575 [JOB_START] = "start",
576 [JOB_VERIFY_ACTIVE] = "verify-active",
578 [JOB_RELOAD] = "reload",
579 [JOB_RELOAD_OR_START] = "reload-or-start",
580 [JOB_RESTART] = "restart",
581 [JOB_TRY_RESTART] = "try-restart",
584 DEFINE_STRING_TABLE_LOOKUP(job_type, JobType);
586 static const char* const job_mode_table[_JOB_MODE_MAX] = {
588 [JOB_REPLACE] = "replace",
589 [JOB_ISOLATE] = "isolate"
592 DEFINE_STRING_TABLE_LOOKUP(job_mode, JobMode);