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/>.
24 #include <sys/timerfd.h>
25 #include <sys/epoll.h>
31 #include "load-fragment.h"
32 #include "load-dropin.h"
36 Job* job_new(Manager *m, JobType type, Unit *unit) {
40 assert(type < _JOB_TYPE_MAX);
43 if (!(j = new0(Job, 1)))
47 j->id = m->current_job_id++;
51 j->timer_watch.type = WATCH_INVALID;
53 /* We don't link it here, that's what job_dependency() is for */
58 void job_free(Job *j) {
61 /* Detach from next 'bigger' objects */
63 bus_job_send_removed_signal(j, !j->failed);
65 if (j->unit->meta.job == j) {
66 j->unit->meta.job = NULL;
67 unit_add_to_gc_queue(j->unit);
70 hashmap_remove(j->manager->jobs, UINT32_TO_PTR(j->id));
74 /* Detach from next 'smaller' objects */
75 manager_transaction_unlink_job(j->manager, j, true);
78 LIST_REMOVE(Job, run_queue, j->manager->run_queue, j);
81 LIST_REMOVE(Job, dbus_queue, j->manager->dbus_job_queue, j);
83 if (j->timer_watch.type != WATCH_INVALID) {
84 assert(j->timer_watch.type == WATCH_JOB_TIMER);
85 assert(j->timer_watch.data.job == j);
86 assert(j->timer_watch.fd >= 0);
88 assert_se(epoll_ctl(j->manager->epoll_fd, EPOLL_CTL_DEL, j->timer_watch.fd, NULL) >= 0);
89 close_nointr_nofail(j->timer_watch.fd);
96 JobDependency* job_dependency_new(Job *subject, Job *object, bool matters, bool conflicts) {
101 /* Adds a new job link, which encodes that the 'subject' job
102 * needs the 'object' job in some way. If 'subject' is NULL
103 * this means the 'anchor' job (i.e. the one the user
104 * explcitily asked for) is the requester. */
106 if (!(l = new0(JobDependency, 1)))
109 l->subject = subject;
111 l->matters = matters;
112 l->conflicts = conflicts;
115 LIST_PREPEND(JobDependency, subject, subject->subject_list, l);
117 LIST_PREPEND(JobDependency, subject, object->manager->transaction_anchor, l);
119 LIST_PREPEND(JobDependency, object, object->object_list, l);
124 void job_dependency_free(JobDependency *l) {
128 LIST_REMOVE(JobDependency, subject, l->subject->subject_list, l);
130 LIST_REMOVE(JobDependency, subject, l->object->manager->transaction_anchor, l);
132 LIST_REMOVE(JobDependency, object, l->object->object_list, l);
137 void job_dependency_delete(Job *subject, Job *object, bool *matters) {
142 LIST_FOREACH(object, l, object->object_list) {
143 assert(l->object == object);
145 if (l->subject == subject)
156 *matters = l->matters;
158 job_dependency_free(l);
161 void job_dump(Job *j, FILE*f, const char *prefix) {
170 "%s\tAction: %s -> %s\n"
174 prefix, j->unit->meta.id, job_type_to_string(j->type),
175 prefix, job_state_to_string(j->state),
176 prefix, yes_no(j->override));
179 bool job_is_anchor(Job *j) {
184 LIST_FOREACH(object, l, j->object_list)
191 static bool types_match(JobType a, JobType b, JobType c, JobType d) {
193 (a == c && b == d) ||
197 int job_type_merge(JobType *a, JobType b) {
201 /* Merging is associative! a merged with b merged with c is
202 * the same as a merged with c merged with b. */
204 /* Mergeability is transitive! if a can be merged with b and b
205 * with c then a also with c */
207 /* Also, if a merged with b cannot be merged with c, then
208 * either a or b cannot be merged with c either */
210 if (types_match(*a, b, JOB_START, JOB_VERIFY_ACTIVE))
212 else if (types_match(*a, b, JOB_START, JOB_RELOAD) ||
213 types_match(*a, b, JOB_START, JOB_RELOAD_OR_START) ||
214 types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD_OR_START) ||
215 types_match(*a, b, JOB_RELOAD, JOB_RELOAD_OR_START))
216 *a = JOB_RELOAD_OR_START;
217 else if (types_match(*a, b, JOB_START, JOB_RESTART) ||
218 types_match(*a, b, JOB_START, JOB_TRY_RESTART) ||
219 types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RESTART) ||
220 types_match(*a, b, JOB_RELOAD, JOB_RESTART) ||
221 types_match(*a, b, JOB_RELOAD_OR_START, JOB_RESTART) ||
222 types_match(*a, b, JOB_RELOAD_OR_START, JOB_TRY_RESTART) ||
223 types_match(*a, b, JOB_RESTART, JOB_TRY_RESTART))
225 else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD))
227 else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_TRY_RESTART) ||
228 types_match(*a, b, JOB_RELOAD, JOB_TRY_RESTART))
229 *a = JOB_TRY_RESTART;
236 bool job_type_is_mergeable(JobType a, JobType b) {
237 return job_type_merge(&a, b) >= 0;
240 bool job_type_is_superset(JobType a, JobType b) {
242 /* Checks whether operation a is a "superset" of b in its
250 return b == JOB_VERIFY_ACTIVE;
254 b == JOB_VERIFY_ACTIVE;
256 case JOB_RELOAD_OR_START:
260 b == JOB_VERIFY_ACTIVE;
265 b == JOB_VERIFY_ACTIVE ||
267 b == JOB_RELOAD_OR_START ||
268 b == JOB_TRY_RESTART;
270 case JOB_TRY_RESTART:
272 b == JOB_VERIFY_ACTIVE ||
280 bool job_type_is_conflicting(JobType a, JobType b) {
281 assert(a >= 0 && a < _JOB_TYPE_MAX);
282 assert(b >= 0 && b < _JOB_TYPE_MAX);
284 return (a == JOB_STOP) != (b == JOB_STOP);
287 bool job_type_is_redundant(JobType a, UnitActiveState b) {
297 b == UNIT_INACTIVE ||
298 b == UNIT_MAINTENANCE;
300 case JOB_VERIFY_ACTIVE:
309 case JOB_RELOAD_OR_START:
311 b == UNIT_ACTIVATING ||
316 b == UNIT_ACTIVATING;
318 case JOB_TRY_RESTART:
320 b == UNIT_ACTIVATING;
323 assert_not_reached("Invalid job type");
327 bool job_is_runnable(Job *j) {
332 assert(j->installed);
334 /* Checks whether there is any job running for the units this
335 * job needs to be running after (in the case of a 'positive'
336 * job type) or before (in the case of a 'negative' job type
339 if (j->type == JOB_START ||
340 j->type == JOB_VERIFY_ACTIVE ||
341 j->type == JOB_RELOAD ||
342 j->type == JOB_RELOAD_OR_START) {
344 /* Immediate result is that the job is or might be
345 * started. In this case lets wait for the
346 * dependencies, regardless whether they are
347 * starting or stopping something. */
349 SET_FOREACH(other, j->unit->meta.dependencies[UNIT_AFTER], i)
354 /* Also, if something else is being stopped and we should
355 * change state after it, then lets wait. */
357 SET_FOREACH(other, j->unit->meta.dependencies[UNIT_BEFORE], i)
358 if (other->meta.job &&
359 (other->meta.job->type == JOB_STOP ||
360 other->meta.job->type == JOB_RESTART ||
361 other->meta.job->type == JOB_TRY_RESTART))
364 /* This means that for a service a and a service b where b
365 * shall be started after a:
367 * start a + start b → 1st step start a, 2nd step start b
368 * start a + stop b → 1st step stop b, 2nd step start a
369 * stop a + start b → 1st step stop a, 2nd step start b
370 * stop a + stop b → 1st step stop b, 2nd step stop a
372 * This has the side effect that restarts are properly
373 * synchronized too. */
378 int job_run_and_invalidate(Job *j) {
382 assert(j->installed);
384 if (j->in_run_queue) {
385 LIST_REMOVE(Job, run_queue, j->manager->run_queue, j);
386 j->in_run_queue = false;
389 if (j->state != JOB_WAITING)
392 if (!job_is_runnable(j))
395 j->state = JOB_RUNNING;
396 job_add_to_dbus_queue(j);
401 r = unit_start(j->unit);
406 case JOB_VERIFY_ACTIVE: {
407 UnitActiveState t = unit_active_state(j->unit);
408 if (UNIT_IS_ACTIVE_OR_RELOADING(t))
410 else if (t == UNIT_ACTIVATING)
418 r = unit_stop(j->unit);
422 r = unit_reload(j->unit);
425 case JOB_RELOAD_OR_START:
426 if (unit_active_state(j->unit) == UNIT_ACTIVE)
427 r = unit_reload(j->unit);
429 r = unit_start(j->unit);
433 UnitActiveState t = unit_active_state(j->unit);
434 if (t == UNIT_INACTIVE || t == UNIT_MAINTENANCE || t == UNIT_ACTIVATING) {
436 r = unit_start(j->unit);
438 r = unit_stop(j->unit);
442 case JOB_TRY_RESTART: {
443 UnitActiveState t = unit_active_state(j->unit);
444 if (t == UNIT_INACTIVE || t == UNIT_MAINTENANCE || t == UNIT_DEACTIVATING)
446 else if (t == UNIT_ACTIVATING) {
448 r = unit_start(j->unit);
450 r = unit_stop(j->unit);
455 assert_not_reached("Unknown job type");
459 r = job_finish_and_invalidate(j, true);
460 else if (r == -EAGAIN) {
461 j->state = JOB_WAITING;
464 r = job_finish_and_invalidate(j, false);
469 int job_finish_and_invalidate(Job *j, bool success) {
476 assert(j->installed);
478 job_add_to_dbus_queue(j);
480 /* Patch restart jobs so that they become normal start jobs */
481 if (success && (j->type == JOB_RESTART || j->type == JOB_TRY_RESTART)) {
483 log_debug("Converting job %s/%s -> %s/%s",
484 j->unit->meta.id, job_type_to_string(j->type),
485 j->unit->meta.id, job_type_to_string(JOB_START));
487 j->state = JOB_WAITING;
490 job_add_to_run_queue(j);
494 log_debug("Job %s/%s finished, success=%s", j->unit->meta.id, job_type_to_string(j->type), yes_no(success));
496 j->failed = !success;
502 unit_status_printf(u, "Starting %s " ANSI_HIGHLIGHT_ON "failed" ANSI_HIGHLIGHT_OFF ".\n", unit_description(u));
504 /* Fail depending jobs on failure */
507 if (t == JOB_START ||
508 t == JOB_VERIFY_ACTIVE ||
509 t == JOB_RELOAD_OR_START) {
511 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
512 if (!other->meta.ignore_dependency_failure &&
514 (other->meta.job->type == JOB_START ||
515 other->meta.job->type == JOB_VERIFY_ACTIVE ||
516 other->meta.job->type == JOB_RELOAD_OR_START))
517 job_finish_and_invalidate(other->meta.job, false);
519 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
520 if (!other->meta.ignore_dependency_failure &&
522 !other->meta.job->override &&
523 (other->meta.job->type == JOB_START ||
524 other->meta.job->type == JOB_VERIFY_ACTIVE ||
525 other->meta.job->type == JOB_RELOAD_OR_START))
526 job_finish_and_invalidate(other->meta.job, false);
528 } else if (t == JOB_STOP) {
530 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
531 if (!other->meta.ignore_dependency_failure &&
533 (other->meta.job->type == JOB_START ||
534 other->meta.job->type == JOB_VERIFY_ACTIVE ||
535 other->meta.job->type == JOB_RELOAD_OR_START))
536 job_finish_and_invalidate(other->meta.job, false);
538 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTED_BY], i)
539 if (!other->meta.ignore_dependency_failure &&
541 (other->meta.job->type == JOB_START ||
542 other->meta.job->type == JOB_VERIFY_ACTIVE ||
543 other->meta.job->type == JOB_RELOAD_OR_START))
544 job_finish_and_invalidate(other->meta.job, false);
548 /* Try to start the next jobs that can be started */
549 SET_FOREACH(other, u->meta.dependencies[UNIT_AFTER], i)
551 job_add_to_run_queue(other->meta.job);
552 SET_FOREACH(other, u->meta.dependencies[UNIT_BEFORE], i)
554 job_add_to_run_queue(other->meta.job);
559 int job_start_timer(Job *j) {
560 struct itimerspec its;
561 struct epoll_event ev;
565 if (j->unit->meta.job_timeout <= 0 ||
566 j->timer_watch.type == WATCH_JOB_TIMER)
569 assert(j->timer_watch.type == WATCH_INVALID);
571 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0) {
577 timespec_store(&its.it_value, j->unit->meta.job_timeout);
579 if (timerfd_settime(fd, 0, &its, NULL) < 0) {
585 ev.data.ptr = &j->timer_watch;
588 if (epoll_ctl(j->manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
593 j->timer_watch.type = WATCH_JOB_TIMER;
594 j->timer_watch.fd = fd;
595 j->timer_watch.data.job = j;
601 close_nointr_nofail(fd);
606 void job_add_to_run_queue(Job *j) {
608 assert(j->installed);
613 LIST_PREPEND(Job, run_queue, j->manager->run_queue, j);
614 j->in_run_queue = true;
617 void job_add_to_dbus_queue(Job *j) {
619 assert(j->installed);
621 if (j->in_dbus_queue)
624 /* We don't check if anybody is subscribed here, since this
625 * job might just have been created and not yet assigned to a
626 * connection/client. */
628 LIST_PREPEND(Job, dbus_queue, j->manager->dbus_job_queue, j);
629 j->in_dbus_queue = true;
632 char *job_dbus_path(Job *j) {
637 if (asprintf(&p, "/org/freedesktop/systemd1/job/%lu", (unsigned long) j->id) < 0)
643 void job_timer_event(Job *j, uint64_t n_elapsed, Watch *w) {
645 assert(w == &j->timer_watch);
647 log_warning("Job %s/%s timed out.", j->unit->meta.id, job_type_to_string(j->type));
648 job_finish_and_invalidate(j, false);
651 static const char* const job_state_table[_JOB_STATE_MAX] = {
652 [JOB_WAITING] = "waiting",
653 [JOB_RUNNING] = "running"
656 DEFINE_STRING_TABLE_LOOKUP(job_state, JobState);
658 static const char* const job_type_table[_JOB_TYPE_MAX] = {
659 [JOB_START] = "start",
660 [JOB_VERIFY_ACTIVE] = "verify-active",
662 [JOB_RELOAD] = "reload",
663 [JOB_RELOAD_OR_START] = "reload-or-start",
664 [JOB_RESTART] = "restart",
665 [JOB_TRY_RESTART] = "try-restart",
668 DEFINE_STRING_TABLE_LOOKUP(job_type, JobType);
670 static const char* const job_mode_table[_JOB_MODE_MAX] = {
672 [JOB_REPLACE] = "replace",
673 [JOB_ISOLATE] = "isolate"
676 DEFINE_STRING_TABLE_LOOKUP(job_mode, JobMode);