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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser 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 JobBusClient* job_bus_client_new(DBusConnection *connection, const char *name) {
40 name_len = strlen(name);
41 cl = malloc0(sizeof(JobBusClient) + name_len + 1);
46 memcpy(cl->name, name, name_len + 1);
50 Job* job_new_raw(Unit *unit) {
53 /* used for deserialization */
61 j->manager = unit->manager;
63 j->timer_watch.type = WATCH_INVALID;
68 Job* job_new(Unit *unit, JobType type) {
71 assert(type < _JOB_TYPE_MAX);
73 j = job_new_raw(unit);
77 j->id = j->manager->current_job_id++;
80 /* We don't link it here, that's what job_dependency() is for */
85 void job_free(Job *j) {
89 assert(!j->installed);
90 assert(!j->transaction_prev);
91 assert(!j->transaction_next);
92 assert(!j->subject_list);
93 assert(!j->object_list);
96 LIST_REMOVE(Job, run_queue, j->manager->run_queue, j);
99 LIST_REMOVE(Job, dbus_queue, j->manager->dbus_job_queue, j);
101 if (j->timer_watch.type != WATCH_INVALID) {
102 assert(j->timer_watch.type == WATCH_JOB_TIMER);
103 assert(j->timer_watch.data.job == j);
104 assert(j->timer_watch.fd >= 0);
106 assert_se(epoll_ctl(j->manager->epoll_fd, EPOLL_CTL_DEL, j->timer_watch.fd, NULL) >= 0);
107 close_nointr_nofail(j->timer_watch.fd);
110 while ((cl = j->bus_client_list)) {
111 LIST_REMOVE(JobBusClient, client, j->bus_client_list, cl);
117 void job_uninstall(Job *j) {
118 assert(j->installed);
119 assert(j->unit->job == j);
120 /* Detach from next 'bigger' objects */
122 /* daemon-reload should be transparent to job observers */
123 if (j->manager->n_reloading <= 0)
124 bus_job_send_removed_signal(j);
127 unit_add_to_gc_queue(j->unit);
129 hashmap_remove(j->manager->jobs, UINT32_TO_PTR(j->id));
130 j->installed = false;
133 static bool job_type_allows_late_merge(JobType t) {
134 /* Tells whether it is OK to merge a job of type 't' with an already
136 * Reloads cannot be merged this way. Think of the sequence:
137 * 1. Reload of a daemon is in progress; the daemon has already loaded
138 * its config file, but hasn't completed the reload operation yet.
139 * 2. Edit foo's config file.
140 * 3. Trigger another reload to have the daemon use the new config.
141 * Should the second reload job be merged into the first one, the daemon
142 * would not know about the new config.
143 * JOB_RESTART jobs on the other hand can be merged, because they get
144 * patched into JOB_START after stopping the unit. So if we see a
145 * JOB_RESTART running, it means the unit hasn't stopped yet and at
146 * this time the merge is still allowed. */
147 return !(t == JOB_RELOAD || t == JOB_RELOAD_OR_START);
150 static void job_merge_into_installed(Job *j, Job *other) {
151 assert(j->installed);
152 assert(j->unit == other->unit);
154 j->type = job_type_lookup_merge(j->type, other->type);
155 assert(j->type >= 0);
157 j->override = j->override || other->override;
160 Job* job_install(Job *j) {
161 Job *uj = j->unit->job;
163 assert(!j->installed);
166 if (job_type_is_conflicting(uj->type, j->type))
167 job_finish_and_invalidate(uj, JOB_CANCELED, true);
169 /* not conflicting, i.e. mergeable */
171 if (uj->state == JOB_WAITING ||
172 (job_type_allows_late_merge(j->type) && job_type_is_superset(uj->type, j->type))) {
173 job_merge_into_installed(uj, j);
174 log_debug("Merged into installed job %s/%s as %u",
175 uj->unit->id, job_type_to_string(uj->type), (unsigned) uj->id);
178 /* already running and not safe to merge into */
179 /* Patch uj to become a merged job and re-run it. */
180 /* XXX It should be safer to queue j to run after uj finishes, but it is
181 * not currently possible to have more than one installed job per unit. */
182 job_merge_into_installed(uj, j);
183 log_debug("Merged into running job, re-running: %s/%s as %u",
184 uj->unit->id, job_type_to_string(uj->type), (unsigned) uj->id);
185 uj->state = JOB_WAITING;
191 /* Install the job */
194 j->manager->n_installed_jobs ++;
195 log_debug("Installed new job %s/%s as %u", j->unit->id, job_type_to_string(j->type), (unsigned) j->id);
199 void job_install_deserialized(Job *j) {
200 assert(!j->installed);
203 log_debug("Unit %s already has a job installed. Not installing deserialized job.", j->unit->id);
208 log_debug("Reinstalled deserialized job %s/%s as %u", j->unit->id, job_type_to_string(j->type), (unsigned) j->id);
211 JobDependency* job_dependency_new(Job *subject, Job *object, bool matters, bool conflicts) {
216 /* Adds a new job link, which encodes that the 'subject' job
217 * needs the 'object' job in some way. If 'subject' is NULL
218 * this means the 'anchor' job (i.e. the one the user
219 * explicitly asked for) is the requester. */
221 if (!(l = new0(JobDependency, 1)))
224 l->subject = subject;
226 l->matters = matters;
227 l->conflicts = conflicts;
230 LIST_PREPEND(JobDependency, subject, subject->subject_list, l);
232 LIST_PREPEND(JobDependency, object, object->object_list, l);
237 void job_dependency_free(JobDependency *l) {
241 LIST_REMOVE(JobDependency, subject, l->subject->subject_list, l);
243 LIST_REMOVE(JobDependency, object, l->object->object_list, l);
248 void job_dump(Job *j, FILE*f, const char *prefix) {
257 "%s\tAction: %s -> %s\n"
261 prefix, j->unit->id, job_type_to_string(j->type),
262 prefix, job_state_to_string(j->state),
263 prefix, yes_no(j->override));
267 * Merging is commutative, so imagine the matrix as symmetric. We store only
268 * its lower triangle to avoid duplication. We don't store the main diagonal,
269 * because A merged with A is simply A.
271 * Merging is associative! A merged with B merged with C is the same as
272 * A merged with C merged with B.
274 * Mergeability is transitive! If A can be merged with B and B with C then
277 * Also, if A merged with B cannot be merged with C, then either A or B cannot
278 * be merged with C either.
280 static const JobType job_merging_table[] = {
281 /* What \ With * JOB_START JOB_VERIFY_ACTIVE JOB_STOP JOB_RELOAD JOB_RELOAD_OR_START JOB_RESTART JOB_TRY_RESTART */
282 /************************************************************************************************************************************/
284 /*JOB_VERIFY_ACTIVE */ JOB_START,
285 /*JOB_STOP */ -1, -1,
286 /*JOB_RELOAD */ JOB_RELOAD_OR_START, JOB_RELOAD, -1,
287 /*JOB_RELOAD_OR_START*/ JOB_RELOAD_OR_START, JOB_RELOAD_OR_START, -1, JOB_RELOAD_OR_START,
288 /*JOB_RESTART */ JOB_RESTART, JOB_RESTART, -1, JOB_RESTART, JOB_RESTART,
289 /*JOB_TRY_RESTART */ JOB_RESTART, JOB_TRY_RESTART, -1, JOB_TRY_RESTART, JOB_RESTART, JOB_RESTART,
292 JobType job_type_lookup_merge(JobType a, JobType b) {
293 assert_cc(ELEMENTSOF(job_merging_table) == _JOB_TYPE_MAX * (_JOB_TYPE_MAX - 1) / 2);
294 assert(a >= 0 && a < _JOB_TYPE_MAX);
295 assert(b >= 0 && b < _JOB_TYPE_MAX);
306 return job_merging_table[(a - 1) * a / 2 + b];
309 bool job_type_is_redundant(JobType a, UnitActiveState b) {
319 b == UNIT_INACTIVE ||
322 case JOB_VERIFY_ACTIVE:
331 case JOB_RELOAD_OR_START:
333 b == UNIT_ACTIVATING ||
338 b == UNIT_ACTIVATING;
340 case JOB_TRY_RESTART:
342 b == UNIT_ACTIVATING;
345 assert_not_reached("Invalid job type");
349 bool job_is_runnable(Job *j) {
354 assert(j->installed);
356 /* Checks whether there is any job running for the units this
357 * job needs to be running after (in the case of a 'positive'
358 * job type) or before (in the case of a 'negative' job
361 /* First check if there is an override */
365 if (j->type == JOB_START ||
366 j->type == JOB_VERIFY_ACTIVE ||
367 j->type == JOB_RELOAD ||
368 j->type == JOB_RELOAD_OR_START) {
370 /* Immediate result is that the job is or might be
371 * started. In this case lets wait for the
372 * dependencies, regardless whether they are
373 * starting or stopping something. */
375 SET_FOREACH(other, j->unit->dependencies[UNIT_AFTER], i)
380 /* Also, if something else is being stopped and we should
381 * change state after it, then lets wait. */
383 SET_FOREACH(other, j->unit->dependencies[UNIT_BEFORE], i)
385 (other->job->type == JOB_STOP ||
386 other->job->type == JOB_RESTART ||
387 other->job->type == JOB_TRY_RESTART))
390 /* This means that for a service a and a service b where b
391 * shall be started after a:
393 * start a + start b → 1st step start a, 2nd step start b
394 * start a + stop b → 1st step stop b, 2nd step start a
395 * stop a + start b → 1st step stop a, 2nd step start b
396 * stop a + stop b → 1st step stop b, 2nd step stop a
398 * This has the side effect that restarts are properly
399 * synchronized too. */
404 static void job_change_type(Job *j, JobType newtype) {
405 log_debug("Converting job %s/%s -> %s/%s",
406 j->unit->id, job_type_to_string(j->type),
407 j->unit->id, job_type_to_string(newtype));
412 int job_run_and_invalidate(Job *j) {
418 assert(j->installed);
420 if (j->in_run_queue) {
421 LIST_REMOVE(Job, run_queue, j->manager->run_queue, j);
422 j->in_run_queue = false;
425 if (j->state != JOB_WAITING)
428 if (!job_is_runnable(j))
431 j->state = JOB_RUNNING;
432 job_add_to_dbus_queue(j);
434 /* While we execute this operation the job might go away (for
435 * example: because it is replaced by a new, conflicting
436 * job.) To make sure we don't access a freed job later on we
437 * store the id here, so that we can verify the job is still
444 case JOB_RELOAD_OR_START:
445 if (unit_active_state(j->unit) == UNIT_ACTIVE) {
446 job_change_type(j, JOB_RELOAD);
447 r = unit_reload(j->unit);
450 job_change_type(j, JOB_START);
454 r = unit_start(j->unit);
456 /* If this unit cannot be started, then simply wait */
461 case JOB_VERIFY_ACTIVE: {
462 UnitActiveState t = unit_active_state(j->unit);
463 if (UNIT_IS_ACTIVE_OR_RELOADING(t))
465 else if (t == UNIT_ACTIVATING)
472 case JOB_TRY_RESTART:
473 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(j->unit))) {
477 job_change_type(j, JOB_RESTART);
482 r = unit_stop(j->unit);
484 /* If this unit cannot stopped, then simply wait. */
490 r = unit_reload(j->unit);
494 assert_not_reached("Unknown job type");
497 if ((j = manager_get_job(m, id))) {
499 r = job_finish_and_invalidate(j, JOB_DONE, true);
500 else if (r == -ENOEXEC)
501 r = job_finish_and_invalidate(j, JOB_SKIPPED, true);
502 else if (r == -EAGAIN)
503 j->state = JOB_WAITING;
505 r = job_finish_and_invalidate(j, JOB_FAILED, true);
511 static void job_print_status_message(Unit *u, JobType t, JobResult result) {
514 if (t == JOB_START) {
519 if (u->condition_result)
520 unit_status_printf(u, ANSI_HIGHLIGHT_GREEN_ON " OK " ANSI_HIGHLIGHT_OFF, "Started %s.", unit_description(u));
524 unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON "FAILED" ANSI_HIGHLIGHT_OFF, "Failed to start %s.", unit_description(u));
525 unit_status_printf(u, NULL, "See 'systemctl status %s' for details.", u->id);
529 unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON " ABORT" ANSI_HIGHLIGHT_OFF, "Dependency failed. Aborted start of %s.", unit_description(u));
533 unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON " TIME " ANSI_HIGHLIGHT_OFF, "Timed out starting %s.", unit_description(u));
540 } else if (t == JOB_STOP) {
545 unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON " TIME " ANSI_HIGHLIGHT_OFF, "Timed out stopping %s.", unit_description(u));
550 unit_status_printf(u, ANSI_HIGHLIGHT_GREEN_ON " OK " ANSI_HIGHLIGHT_OFF, "Stopped %s.", unit_description(u));
559 int job_finish_and_invalidate(Job *j, JobResult result, bool recursive) {
566 assert(j->installed);
568 job_add_to_dbus_queue(j);
570 /* Patch restart jobs so that they become normal start jobs */
571 if (result == JOB_DONE && j->type == JOB_RESTART) {
573 job_change_type(j, JOB_START);
574 j->state = JOB_WAITING;
576 job_add_to_run_queue(j);
584 log_debug("Job %s/%s finished, result=%s", j->unit->id, job_type_to_string(j->type), job_result_to_string(result));
586 if (result == JOB_FAILED)
587 j->manager->n_failed_jobs ++;
594 job_print_status_message(u, t, result);
596 /* Fail depending jobs on failure */
597 if (result != JOB_DONE && recursive) {
599 if (t == JOB_START ||
600 t == JOB_VERIFY_ACTIVE ||
601 t == JOB_RELOAD_OR_START) {
603 SET_FOREACH(other, u->dependencies[UNIT_REQUIRED_BY], i)
605 (other->job->type == JOB_START ||
606 other->job->type == JOB_VERIFY_ACTIVE ||
607 other->job->type == JOB_RELOAD_OR_START))
608 job_finish_and_invalidate(other->job, JOB_DEPENDENCY, true);
610 SET_FOREACH(other, u->dependencies[UNIT_BOUND_BY], i)
612 (other->job->type == JOB_START ||
613 other->job->type == JOB_VERIFY_ACTIVE ||
614 other->job->type == JOB_RELOAD_OR_START))
615 job_finish_and_invalidate(other->job, JOB_DEPENDENCY, true);
617 SET_FOREACH(other, u->dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
619 !other->job->override &&
620 (other->job->type == JOB_START ||
621 other->job->type == JOB_VERIFY_ACTIVE ||
622 other->job->type == JOB_RELOAD_OR_START))
623 job_finish_and_invalidate(other->job, JOB_DEPENDENCY, true);
625 } else if (t == JOB_STOP) {
627 SET_FOREACH(other, u->dependencies[UNIT_CONFLICTED_BY], i)
629 (other->job->type == JOB_START ||
630 other->job->type == JOB_VERIFY_ACTIVE ||
631 other->job->type == JOB_RELOAD_OR_START))
632 job_finish_and_invalidate(other->job, JOB_DEPENDENCY, true);
636 /* Trigger OnFailure dependencies that are not generated by
637 * the unit itself. We don't tread JOB_CANCELED as failure in
638 * this context. And JOB_FAILURE is already handled by the
640 if (result == JOB_TIMEOUT || result == JOB_DEPENDENCY) {
641 log_notice("Job %s/%s failed with result '%s'.",
643 job_type_to_string(t),
644 job_result_to_string(result));
646 unit_trigger_on_failure(u);
650 /* Try to start the next jobs that can be started */
651 SET_FOREACH(other, u->dependencies[UNIT_AFTER], i)
653 job_add_to_run_queue(other->job);
654 SET_FOREACH(other, u->dependencies[UNIT_BEFORE], i)
656 job_add_to_run_queue(other->job);
658 manager_check_finished(u->manager);
663 int job_start_timer(Job *j) {
664 struct itimerspec its;
665 struct epoll_event ev;
669 if (j->unit->job_timeout <= 0 ||
670 j->timer_watch.type == WATCH_JOB_TIMER)
673 assert(j->timer_watch.type == WATCH_INVALID);
675 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0) {
681 timespec_store(&its.it_value, j->unit->job_timeout);
683 if (timerfd_settime(fd, 0, &its, NULL) < 0) {
689 ev.data.ptr = &j->timer_watch;
692 if (epoll_ctl(j->manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
697 j->timer_watch.type = WATCH_JOB_TIMER;
698 j->timer_watch.fd = fd;
699 j->timer_watch.data.job = j;
705 close_nointr_nofail(fd);
710 void job_add_to_run_queue(Job *j) {
712 assert(j->installed);
717 LIST_PREPEND(Job, run_queue, j->manager->run_queue, j);
718 j->in_run_queue = true;
721 void job_add_to_dbus_queue(Job *j) {
723 assert(j->installed);
725 if (j->in_dbus_queue)
728 /* We don't check if anybody is subscribed here, since this
729 * job might just have been created and not yet assigned to a
730 * connection/client. */
732 LIST_PREPEND(Job, dbus_queue, j->manager->dbus_job_queue, j);
733 j->in_dbus_queue = true;
736 char *job_dbus_path(Job *j) {
741 if (asprintf(&p, "/org/freedesktop/systemd1/job/%lu", (unsigned long) j->id) < 0)
747 void job_timer_event(Job *j, uint64_t n_elapsed, Watch *w) {
749 assert(w == &j->timer_watch);
751 log_warning("Job %s/%s timed out.", j->unit->id, job_type_to_string(j->type));
752 job_finish_and_invalidate(j, JOB_TIMEOUT, true);
755 int job_serialize(Job *j, FILE *f, FDSet *fds) {
756 fprintf(f, "job-id=%u\n", j->id);
757 fprintf(f, "job-type=%s\n", job_type_to_string(j->type));
758 fprintf(f, "job-state=%s\n", job_state_to_string(j->state));
759 fprintf(f, "job-override=%s\n", yes_no(j->override));
760 fprintf(f, "job-sent-dbus-new-signal=%s\n", yes_no(j->sent_dbus_new_signal));
761 fprintf(f, "job-ignore-order=%s\n", yes_no(j->ignore_order));
762 /* Cannot save bus clients. Just note the fact that we're losing
763 * them. job_send_message() will fallback to broadcasting. */
764 fprintf(f, "job-forgot-bus-clients=%s\n",
765 yes_no(j->forgot_bus_clients || j->bus_client_list));
766 if (j->timer_watch.type == WATCH_JOB_TIMER) {
767 int copy = fdset_put_dup(fds, j->timer_watch.fd);
770 fprintf(f, "job-timer-watch-fd=%d\n", copy);
778 int job_deserialize(Job *j, FILE *f, FDSet *fds) {
780 char line[LINE_MAX], *l, *v;
783 if (!fgets(line, sizeof(line), f)) {
804 if (streq(l, "job-id")) {
805 if (safe_atou32(v, &j->id) < 0)
806 log_debug("Failed to parse job id value %s", v);
807 } else if (streq(l, "job-type")) {
808 JobType t = job_type_from_string(v);
810 log_debug("Failed to parse job type %s", v);
813 } else if (streq(l, "job-state")) {
814 JobState s = job_state_from_string(v);
816 log_debug("Failed to parse job state %s", v);
819 } else if (streq(l, "job-override")) {
820 int b = parse_boolean(v);
822 log_debug("Failed to parse job override flag %s", v);
824 j->override = j->override || b;
825 } else if (streq(l, "job-sent-dbus-new-signal")) {
826 int b = parse_boolean(v);
828 log_debug("Failed to parse job sent_dbus_new_signal flag %s", v);
830 j->sent_dbus_new_signal = j->sent_dbus_new_signal || b;
831 } else if (streq(l, "job-ignore-order")) {
832 int b = parse_boolean(v);
834 log_debug("Failed to parse job ignore_order flag %s", v);
836 j->ignore_order = j->ignore_order || b;
837 } else if (streq(l, "job-forgot-bus-clients")) {
838 int b = parse_boolean(v);
840 log_debug("Failed to parse job forgot_bus_clients flag %s", v);
842 j->forgot_bus_clients = j->forgot_bus_clients || b;
843 } else if (streq(l, "job-timer-watch-fd")) {
845 if (safe_atoi(v, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
846 log_debug("Failed to parse job-timer-watch-fd value %s", v);
848 if (j->timer_watch.type == WATCH_JOB_TIMER)
849 close_nointr_nofail(j->timer_watch.fd);
851 j->timer_watch.type = WATCH_JOB_TIMER;
852 j->timer_watch.fd = fdset_remove(fds, fd);
853 j->timer_watch.data.job = j;
859 int job_coldplug(Job *j) {
860 struct epoll_event ev;
862 if (j->timer_watch.type != WATCH_JOB_TIMER)
866 ev.data.ptr = &j->timer_watch;
869 if (epoll_ctl(j->manager->epoll_fd, EPOLL_CTL_ADD, j->timer_watch.fd, &ev) < 0)
875 static const char* const job_state_table[_JOB_STATE_MAX] = {
876 [JOB_WAITING] = "waiting",
877 [JOB_RUNNING] = "running"
880 DEFINE_STRING_TABLE_LOOKUP(job_state, JobState);
882 static const char* const job_type_table[_JOB_TYPE_MAX] = {
883 [JOB_START] = "start",
884 [JOB_VERIFY_ACTIVE] = "verify-active",
886 [JOB_RELOAD] = "reload",
887 [JOB_RELOAD_OR_START] = "reload-or-start",
888 [JOB_RESTART] = "restart",
889 [JOB_TRY_RESTART] = "try-restart",
892 DEFINE_STRING_TABLE_LOOKUP(job_type, JobType);
894 static const char* const job_mode_table[_JOB_MODE_MAX] = {
896 [JOB_REPLACE] = "replace",
897 [JOB_ISOLATE] = "isolate",
898 [JOB_IGNORE_DEPENDENCIES] = "ignore-dependencies",
899 [JOB_IGNORE_REQUIREMENTS] = "ignore-requirements"
902 DEFINE_STRING_TABLE_LOOKUP(job_mode, JobMode);
904 static const char* const job_result_table[_JOB_RESULT_MAX] = {
906 [JOB_CANCELED] = "canceled",
907 [JOB_TIMEOUT] = "timeout",
908 [JOB_FAILED] = "failed",
909 [JOB_DEPENDENCY] = "dependency",
910 [JOB_SKIPPED] = "skipped"
913 DEFINE_STRING_TABLE_LOOKUP(job_result, JobResult);