1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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);
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 * explicitly 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_dump(Job *j, FILE*f, const char *prefix) {
146 "%s\tAction: %s -> %s\n"
150 prefix, j->unit->meta.id, job_type_to_string(j->type),
151 prefix, job_state_to_string(j->state),
152 prefix, yes_no(j->override));
155 bool job_is_anchor(Job *j) {
160 LIST_FOREACH(object, l, j->object_list)
167 static bool types_match(JobType a, JobType b, JobType c, JobType d) {
169 (a == c && b == d) ||
173 int job_type_merge(JobType *a, JobType b) {
177 /* Merging is associative! a merged with b merged with c is
178 * the same as a merged with c merged with b. */
180 /* Mergeability is transitive! if a can be merged with b and b
181 * with c then a also with c */
183 /* Also, if a merged with b cannot be merged with c, then
184 * either a or b cannot be merged with c either */
186 if (types_match(*a, b, JOB_START, JOB_VERIFY_ACTIVE))
188 else if (types_match(*a, b, JOB_START, JOB_RELOAD) ||
189 types_match(*a, b, JOB_START, JOB_RELOAD_OR_START) ||
190 types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD_OR_START) ||
191 types_match(*a, b, JOB_RELOAD, JOB_RELOAD_OR_START))
192 *a = JOB_RELOAD_OR_START;
193 else if (types_match(*a, b, JOB_START, JOB_RESTART) ||
194 types_match(*a, b, JOB_START, JOB_TRY_RESTART) ||
195 types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RESTART) ||
196 types_match(*a, b, JOB_RELOAD, JOB_RESTART) ||
197 types_match(*a, b, JOB_RELOAD_OR_START, JOB_RESTART) ||
198 types_match(*a, b, JOB_RELOAD_OR_START, JOB_TRY_RESTART) ||
199 types_match(*a, b, JOB_RESTART, JOB_TRY_RESTART))
201 else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_RELOAD))
203 else if (types_match(*a, b, JOB_VERIFY_ACTIVE, JOB_TRY_RESTART) ||
204 types_match(*a, b, JOB_RELOAD, JOB_TRY_RESTART))
205 *a = JOB_TRY_RESTART;
212 bool job_type_is_mergeable(JobType a, JobType b) {
213 return job_type_merge(&a, b) >= 0;
216 bool job_type_is_superset(JobType a, JobType b) {
218 /* Checks whether operation a is a "superset" of b in its
226 return b == JOB_VERIFY_ACTIVE;
230 b == JOB_VERIFY_ACTIVE;
232 case JOB_RELOAD_OR_START:
236 b == JOB_VERIFY_ACTIVE;
241 b == JOB_VERIFY_ACTIVE ||
243 b == JOB_RELOAD_OR_START ||
244 b == JOB_TRY_RESTART;
246 case JOB_TRY_RESTART:
248 b == JOB_VERIFY_ACTIVE ||
256 bool job_type_is_conflicting(JobType a, JobType b) {
257 assert(a >= 0 && a < _JOB_TYPE_MAX);
258 assert(b >= 0 && b < _JOB_TYPE_MAX);
260 return (a == JOB_STOP) != (b == JOB_STOP);
263 bool job_type_is_redundant(JobType a, UnitActiveState b) {
273 b == UNIT_INACTIVE ||
276 case JOB_VERIFY_ACTIVE:
285 case JOB_RELOAD_OR_START:
287 b == UNIT_ACTIVATING ||
292 b == UNIT_ACTIVATING;
294 case JOB_TRY_RESTART:
296 b == UNIT_ACTIVATING;
299 assert_not_reached("Invalid job type");
303 bool job_is_runnable(Job *j) {
308 assert(j->installed);
310 /* Checks whether there is any job running for the units this
311 * job needs to be running after (in the case of a 'positive'
312 * job type) or before (in the case of a 'negative' job
315 /* First check if there is an override */
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) {
364 assert(j->installed);
366 if (j->in_run_queue) {
367 LIST_REMOVE(Job, run_queue, j->manager->run_queue, j);
368 j->in_run_queue = false;
371 if (j->state != JOB_WAITING)
374 if (!job_is_runnable(j))
377 j->state = JOB_RUNNING;
378 job_add_to_dbus_queue(j);
380 /* While we execute this operation the job might go away (for
381 * example: because it is replaced by a new, conflicting
382 * job.) To make sure we don't access a freed job later on we
383 * store the id here, so that we can verify the job is still
391 r = unit_start(j->unit);
393 /* If this unit cannot be started, then simply
400 case JOB_VERIFY_ACTIVE: {
401 UnitActiveState t = unit_active_state(j->unit);
402 if (UNIT_IS_ACTIVE_OR_RELOADING(t))
404 else if (t == UNIT_ACTIVATING)
412 r = unit_stop(j->unit);
414 /* If this unit cannot stopped, then simply
421 r = unit_reload(j->unit);
424 case JOB_RELOAD_OR_START:
425 if (unit_active_state(j->unit) == UNIT_ACTIVE) {
426 j->type = JOB_RELOAD;
427 r = unit_reload(j->unit);
430 r = unit_start(j->unit);
438 UnitActiveState t = unit_active_state(j->unit);
439 if (t == UNIT_INACTIVE || t == UNIT_FAILED || t == UNIT_ACTIVATING) {
441 r = unit_start(j->unit);
443 r = unit_stop(j->unit);
447 case JOB_TRY_RESTART: {
448 UnitActiveState t = unit_active_state(j->unit);
449 if (t == UNIT_INACTIVE || t == UNIT_FAILED || t == UNIT_DEACTIVATING)
451 else if (t == UNIT_ACTIVATING) {
453 r = unit_start(j->unit);
455 j->type = JOB_RESTART;
456 r = unit_stop(j->unit);
462 assert_not_reached("Unknown job type");
465 if ((j = manager_get_job(m, id))) {
467 r = job_finish_and_invalidate(j, JOB_DONE);
468 else if (r == -ENOEXEC)
469 r = job_finish_and_invalidate(j, JOB_SKIPPED);
470 else if (r == -EAGAIN)
471 j->state = JOB_WAITING;
473 r = job_finish_and_invalidate(j, JOB_FAILED);
479 static void job_print_status_message(Unit *u, JobType t, JobResult result) {
482 if (t == JOB_START) {
487 unit_status_printf(u, ANSI_HIGHLIGHT_GREEN_ON " OK " ANSI_HIGHLIGHT_OFF, "Started %s", unit_description(u));
491 unit_status_printf(u, ANSI_HIGHLIGHT_ON "FAILED" ANSI_HIGHLIGHT_OFF, "Failed to start %s", unit_description(u));
492 unit_status_printf(u, NULL, "See 'systemctl status %s' for details.", u->meta.id);
496 unit_status_printf(u, ANSI_HIGHLIGHT_ON " ABORT" ANSI_HIGHLIGHT_OFF, "Dependency failed. Aborted start of %s", unit_description(u));
500 unit_status_printf(u, ANSI_HIGHLIGHT_ON " TIME " ANSI_HIGHLIGHT_OFF, "Timed out starting %s", unit_description(u));
507 } else if (t == JOB_STOP) {
512 unit_status_printf(u, ANSI_HIGHLIGHT_ON " TIME " ANSI_HIGHLIGHT_OFF, "Timed out stopping %s", unit_description(u));
517 unit_status_printf(u, ANSI_HIGHLIGHT_GREEN_ON " OK " ANSI_HIGHLIGHT_OFF, "Stopped %s", unit_description(u));
526 int job_finish_and_invalidate(Job *j, JobResult result) {
531 bool recursed = false;
534 assert(j->installed);
536 job_add_to_dbus_queue(j);
538 /* Patch restart jobs so that they become normal start jobs */
539 if (result == JOB_DONE && (j->type == JOB_RESTART || j->type == JOB_TRY_RESTART)) {
541 log_debug("Converting job %s/%s -> %s/%s",
542 j->unit->meta.id, job_type_to_string(j->type),
543 j->unit->meta.id, job_type_to_string(JOB_START));
545 j->state = JOB_WAITING;
548 job_add_to_run_queue(j);
556 log_debug("Job %s/%s finished, result=%s", j->unit->meta.id, job_type_to_string(j->type), job_result_to_string(result));
558 if (result == JOB_FAILED)
559 j->manager->n_failed_jobs ++;
565 job_print_status_message(u, t, result);
567 /* Fail depending jobs on failure */
568 if (result != JOB_DONE) {
570 if (t == JOB_START ||
571 t == JOB_VERIFY_ACTIVE ||
572 t == JOB_RELOAD_OR_START) {
574 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
575 if (other->meta.job &&
576 (other->meta.job->type == JOB_START ||
577 other->meta.job->type == JOB_VERIFY_ACTIVE ||
578 other->meta.job->type == JOB_RELOAD_OR_START)) {
579 job_finish_and_invalidate(other->meta.job, JOB_DEPENDENCY);
583 SET_FOREACH(other, u->meta.dependencies[UNIT_BOUND_BY], i)
584 if (other->meta.job &&
585 (other->meta.job->type == JOB_START ||
586 other->meta.job->type == JOB_VERIFY_ACTIVE ||
587 other->meta.job->type == JOB_RELOAD_OR_START)) {
588 job_finish_and_invalidate(other->meta.job, JOB_DEPENDENCY);
592 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
593 if (other->meta.job &&
594 !other->meta.job->override &&
595 (other->meta.job->type == JOB_START ||
596 other->meta.job->type == JOB_VERIFY_ACTIVE ||
597 other->meta.job->type == JOB_RELOAD_OR_START)) {
598 job_finish_and_invalidate(other->meta.job, JOB_DEPENDENCY);
602 } else if (t == JOB_STOP) {
604 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTED_BY], i)
605 if (other->meta.job &&
606 (other->meta.job->type == JOB_START ||
607 other->meta.job->type == JOB_VERIFY_ACTIVE ||
608 other->meta.job->type == JOB_RELOAD_OR_START)) {
609 job_finish_and_invalidate(other->meta.job, JOB_DEPENDENCY);
615 /* Trigger OnFailure dependencies that are not generated by
616 * the unit itself. We don't tread JOB_CANCELED as failure in
617 * this context. And JOB_FAILURE is already handled by the
619 if (result == JOB_TIMEOUT || result == JOB_DEPENDENCY) {
620 log_notice("Job %s/%s failed with result '%s'.",
622 job_type_to_string(t),
623 job_result_to_string(result));
625 unit_trigger_on_failure(u);
629 /* Try to start the next jobs that can be started */
630 SET_FOREACH(other, u->meta.dependencies[UNIT_AFTER], i)
632 job_add_to_run_queue(other->meta.job);
633 SET_FOREACH(other, u->meta.dependencies[UNIT_BEFORE], i)
635 job_add_to_run_queue(other->meta.job);
637 manager_check_finished(u->meta.manager);
642 int job_start_timer(Job *j) {
643 struct itimerspec its;
644 struct epoll_event ev;
648 if (j->unit->meta.job_timeout <= 0 ||
649 j->timer_watch.type == WATCH_JOB_TIMER)
652 assert(j->timer_watch.type == WATCH_INVALID);
654 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0) {
660 timespec_store(&its.it_value, j->unit->meta.job_timeout);
662 if (timerfd_settime(fd, 0, &its, NULL) < 0) {
668 ev.data.ptr = &j->timer_watch;
671 if (epoll_ctl(j->manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
676 j->timer_watch.type = WATCH_JOB_TIMER;
677 j->timer_watch.fd = fd;
678 j->timer_watch.data.job = j;
684 close_nointr_nofail(fd);
689 void job_add_to_run_queue(Job *j) {
691 assert(j->installed);
696 LIST_PREPEND(Job, run_queue, j->manager->run_queue, j);
697 j->in_run_queue = true;
700 void job_add_to_dbus_queue(Job *j) {
702 assert(j->installed);
704 if (j->in_dbus_queue)
707 /* We don't check if anybody is subscribed here, since this
708 * job might just have been created and not yet assigned to a
709 * connection/client. */
711 LIST_PREPEND(Job, dbus_queue, j->manager->dbus_job_queue, j);
712 j->in_dbus_queue = true;
715 char *job_dbus_path(Job *j) {
720 if (asprintf(&p, "/org/freedesktop/systemd1/job/%lu", (unsigned long) j->id) < 0)
726 void job_timer_event(Job *j, uint64_t n_elapsed, Watch *w) {
728 assert(w == &j->timer_watch);
730 log_warning("Job %s/%s timed out.", j->unit->meta.id, job_type_to_string(j->type));
731 job_finish_and_invalidate(j, JOB_TIMEOUT);
734 static const char* const job_state_table[_JOB_STATE_MAX] = {
735 [JOB_WAITING] = "waiting",
736 [JOB_RUNNING] = "running"
739 DEFINE_STRING_TABLE_LOOKUP(job_state, JobState);
741 static const char* const job_type_table[_JOB_TYPE_MAX] = {
742 [JOB_START] = "start",
743 [JOB_VERIFY_ACTIVE] = "verify-active",
745 [JOB_RELOAD] = "reload",
746 [JOB_RELOAD_OR_START] = "reload-or-start",
747 [JOB_RESTART] = "restart",
748 [JOB_TRY_RESTART] = "try-restart",
751 DEFINE_STRING_TABLE_LOOKUP(job_type, JobType);
753 static const char* const job_mode_table[_JOB_MODE_MAX] = {
755 [JOB_REPLACE] = "replace",
756 [JOB_ISOLATE] = "isolate",
757 [JOB_IGNORE_DEPENDENCIES] = "ignore-dependencies",
758 [JOB_IGNORE_REQUIREMENTS] = "ignore-requirements"
761 DEFINE_STRING_TABLE_LOOKUP(job_mode, JobMode);
763 static const char* const job_result_table[_JOB_RESULT_MAX] = {
765 [JOB_CANCELED] = "canceled",
766 [JOB_TIMEOUT] = "timeout",
767 [JOB_FAILED] = "failed",
768 [JOB_DEPENDENCY] = "dependency",
769 [JOB_SKIPPED] = "skipped"
772 DEFINE_STRING_TABLE_LOOKUP(job_result, JobResult);