+
+/* Errors:
+ * -EBADR: This name type does not support starting.
+ * -EALREADY: Name is already started.
+ * -EAGAIN: An operation is already in progress. Retry later.
+ */
+int name_start(Name *n) {
+ NameActiveState state;
+
+ assert(n);
+
+ if (!NAME_VTABLE(n)->start)
+ return -EBADR;
+
+ state = name_active_state(n);
+ if (NAME_IS_ACTIVE_OR_RELOADING(state))
+ return -EALREADY;
+
+ /* We don't suppress calls to ->start() here when we are
+ * already starting, to allow this request to be used as a
+ * "hurry up" call, for example when the name is in some "auto
+ * restart" state where it waits for a holdoff timer to elapse
+ * before it will start again. */
+
+ return NAME_VTABLE(n)->start(n);
+}
+
+bool name_type_can_start(NameType t) {
+ assert(t >= 0 && t < _NAME_TYPE_MAX);
+
+ return !!name_vtable[t]->start;
+}
+
+/* Errors:
+ * -EBADR: This name type does not support stopping.
+ * -EALREADY: Name is already stopped.
+ * -EAGAIN: An operation is already in progress. Retry later.
+ */
+int name_stop(Name *n) {
+ NameActiveState state;
+
+ assert(n);
+
+ if (!NAME_VTABLE(n)->stop)
+ return -EBADR;
+
+ state = name_active_state(n);
+ if (state == NAME_INACTIVE)
+ return -EALREADY;
+
+ if (state == NAME_DEACTIVATING)
+ return 0;
+
+ return NAME_VTABLE(n)->stop(n);
+}
+
+/* Errors:
+ * -EBADR: This name type does not support reloading.
+ * -ENOEXEC: Name is not started.
+ * -EAGAIN: An operation is already in progress. Retry later.
+ */
+int name_reload(Name *n) {
+ NameActiveState state;
+
+ assert(n);
+
+ if (!name_can_reload(n))
+ return -EBADR;
+
+ state = name_active_state(n);
+ if (name_active_state(n) == NAME_ACTIVE_RELOADING)
+ return -EALREADY;
+
+ if (name_active_state(n) != NAME_ACTIVE)
+ return -ENOEXEC;
+
+ return NAME_VTABLE(n)->reload(n);
+}
+
+bool name_type_can_reload(NameType t) {
+ assert(t >= 0 && t < _NAME_TYPE_MAX);
+
+ return !!name_vtable[t]->reload;
+}
+
+bool name_can_reload(Name *n) {
+ assert(n);
+
+ if (!name_type_can_reload(n->meta.type))
+ return false;
+
+ if (!NAME_VTABLE(n)->can_reload)
+ return true;
+
+ return NAME_VTABLE(n)->can_reload(n);
+}
+
+static void retroactively_start_dependencies(Name *n) {
+ Iterator i;
+ Name *other;
+
+ assert(n);
+ assert(NAME_IS_ACTIVE_OR_ACTIVATING(name_active_state(n)));
+
+ SET_FOREACH(other, n->meta.dependencies[NAME_REQUIRES], i)
+ if (!NAME_IS_ACTIVE_OR_ACTIVATING(name_active_state(other)))
+ manager_add_job(n->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
+
+ SET_FOREACH(other, n->meta.dependencies[NAME_SOFT_REQUIRES], i)
+ if (!NAME_IS_ACTIVE_OR_ACTIVATING(name_active_state(other)))
+ manager_add_job(n->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
+
+ SET_FOREACH(other, n->meta.dependencies[NAME_REQUISITE], i)
+ if (!NAME_IS_ACTIVE_OR_ACTIVATING(name_active_state(other)))
+ manager_add_job(n->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
+
+ SET_FOREACH(other, n->meta.dependencies[NAME_WANTS], i)
+ if (!NAME_IS_ACTIVE_OR_ACTIVATING(name_active_state(other)))
+ manager_add_job(n->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
+
+ SET_FOREACH(other, n->meta.dependencies[NAME_CONFLICTS], i)
+ if (!NAME_IS_ACTIVE_OR_ACTIVATING(name_active_state(other)))
+ manager_add_job(n->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
+}
+
+static void retroactively_stop_dependencies(Name *n) {
+ Iterator i;
+ Name *other;
+
+ assert(n);
+ assert(NAME_IS_INACTIVE_OR_DEACTIVATING(name_active_state(n)));
+
+ SET_FOREACH(other, n->meta.dependencies[NAME_REQUIRED_BY], i)
+ if (!NAME_IS_INACTIVE_OR_DEACTIVATING(name_active_state(other)))
+ manager_add_job(n->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
+}
+
+void name_notify(Name *n, NameActiveState os, NameActiveState ns) {
+ assert(n);
+ assert(os < _NAME_ACTIVE_STATE_MAX);
+ assert(ns < _NAME_ACTIVE_STATE_MAX);
+ assert(!(os == NAME_ACTIVE && ns == NAME_ACTIVATING));
+ assert(!(os == NAME_INACTIVE && ns == NAME_DEACTIVATING));
+
+ if (os == ns)
+ return;
+
+ if (!NAME_IS_ACTIVE_OR_RELOADING(os) && NAME_IS_ACTIVE_OR_RELOADING(ns))
+ n->meta.active_enter_timestamp = now(CLOCK_REALTIME);
+ else if (NAME_IS_ACTIVE_OR_RELOADING(os) && !NAME_IS_ACTIVE_OR_RELOADING(ns))
+ n->meta.active_exit_timestamp = now(CLOCK_REALTIME);
+
+ if (n->meta.job) {
+
+ if (n->meta.job->state == JOB_WAITING)
+
+ /* So we reached a different state for this
+ * job. Let's see if we can run it now if it
+ * failed previously due to EAGAIN. */
+ job_schedule_run(n->meta.job);
+
+ else {
+ assert(n->meta.job->state == JOB_RUNNING);
+
+ /* Let's check of this state change
+ * constitutes a finished job, or maybe
+ * cotradicts a running job and hence needs to
+ * invalidate jobs. */
+
+ switch (n->meta.job->type) {
+
+ case JOB_START:
+ case JOB_VERIFY_ACTIVE:
+
+ if (NAME_IS_ACTIVE_OR_RELOADING(ns)) {
+ job_finish_and_invalidate(n->meta.job, true);
+ return;
+ } else if (ns == NAME_ACTIVATING)
+ return;
+ else
+ job_finish_and_invalidate(n->meta.job, false);
+
+ break;
+
+ case JOB_RELOAD:
+ case JOB_RELOAD_OR_START:
+
+ if (ns == NAME_ACTIVE) {
+ job_finish_and_invalidate(n->meta.job, true);
+ return;
+ } else if (ns == NAME_ACTIVATING || ns == NAME_ACTIVE_RELOADING)
+ return;
+ else
+ job_finish_and_invalidate(n->meta.job, false);
+
+ break;
+
+ case JOB_STOP:
+ case JOB_RESTART:
+ case JOB_TRY_RESTART:
+
+ if (ns == NAME_INACTIVE) {
+ job_finish_and_invalidate(n->meta.job, true);
+ return;
+ } else if (ns == NAME_DEACTIVATING)
+ return;
+ else
+ job_finish_and_invalidate(n->meta.job, false);
+
+ break;
+
+ default:
+ assert_not_reached("Job type unknown");
+ }
+ }
+ }
+
+ /* If this state change happened without being requested by a
+ * job, then let's retroactively start or stop dependencies */
+
+ if (NAME_IS_INACTIVE_OR_DEACTIVATING(os) && NAME_IS_ACTIVE_OR_ACTIVATING(ns))
+ retroactively_start_dependencies(n);
+ else if (NAME_IS_ACTIVE_OR_ACTIVATING(os) && NAME_IS_INACTIVE_OR_DEACTIVATING(ns))
+ retroactively_stop_dependencies(n);
+}
+
+int name_watch_fd(Name *n, int fd, uint32_t events) {
+ struct epoll_event ev;
+
+ assert(n);
+ assert(fd >= 0);
+
+ zero(ev);
+ ev.data.fd = fd;
+ ev.data.ptr = n;
+ ev.data.u32 = MANAGER_FD;
+ ev.events = events;
+
+ if (epoll_ctl(n->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) >= 0)
+ return 0;
+
+ if (errno == EEXIST)
+ if (epoll_ctl(n->meta.manager->epoll_fd, EPOLL_CTL_MOD, fd, &ev) >= 0)
+ return 0;
+
+ return -errno;
+}
+
+void name_unwatch_fd(Name *n, int fd) {
+ assert(n);
+ assert(fd >= 0);
+
+ assert_se(epoll_ctl(n->meta.manager->epoll_fd, EPOLL_CTL_DEL, fd, NULL) >= 0 || errno == ENOENT);
+}
+
+int name_watch_pid(Name *n, pid_t pid) {
+ assert(n);
+ assert(pid >= 1);
+
+ return hashmap_put(n->meta.manager->watch_pids, UINT32_TO_PTR(pid), n);
+}
+
+void name_unwatch_pid(Name *n, pid_t pid) {
+ assert(n);
+ assert(pid >= 1);
+
+ hashmap_remove(n->meta.manager->watch_pids, UINT32_TO_PTR(pid));
+}
+
+int name_watch_timer(Name *n, usec_t delay, int *id) {
+ struct epoll_event ev;
+ int fd;
+ struct itimerspec its;
+ int flags;
+ bool ours;
+
+ assert(n);
+ assert(id);
+
+ /* This will try to reuse the old timer if there is one */
+
+ if (*id >= 0) {
+ ours = false;
+ fd = *id;
+
+ } else {
+ ours = true;
+
+ if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
+ return -errno;
+ }
+
+ zero(its);
+
+ if (delay <= 0) {
+ /* Set absolute time in the past, but not 0, since we
+ * don't want to disarm the timer */
+ its.it_value.tv_sec = 0;
+ its.it_value.tv_nsec = 1;
+
+ flags = TFD_TIMER_ABSTIME;
+ } else {
+ timespec_store(&its.it_value, delay);
+ flags = 0;
+ }
+
+ /* This will also flush the elapse counter */
+ if (timerfd_settime(fd, flags, &its, NULL) < 0)
+ goto fail;
+
+ zero(ev);
+ ev.data.fd = fd;
+ ev.data.ptr = n;
+ ev.data.u32 = MANAGER_TIMER;
+ ev.events = POLLIN;
+
+ if (epoll_ctl(n->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
+ goto fail;
+
+ *id = fd;
+ return 0;
+
+fail:
+ if (ours)
+ assert_se(close_nointr(fd) == 0);
+
+ return -errno;
+}
+
+void name_unwatch_timer(Name *n, int *id) {
+ assert(n);
+ assert(id);
+
+ if (*id >= 0) {
+ assert_se(epoll_ctl(n->meta.manager->epoll_fd, EPOLL_CTL_DEL, *id, NULL) >= 0);
+ assert_se(close_nointr(*id) == 0);
+
+ *id = -1;
+ }
+}
+
+char *name_change_suffix(const char *t, const char *suffix) {
+ char *e, *n;
+ size_t a, b;
+
+ assert(t);
+ assert(name_is_valid(t));
+ assert(suffix);
+
+ assert_se(e = strrchr(t, '.'));
+ a = e - t;
+ b = strlen(suffix);
+
+ if (!(n = new(char, a + b + 1)))
+ return NULL;
+
+ memcpy(n, t, a);
+ memcpy(n+a, t, b+1);
+
+ return n;
+}
+
+bool name_job_is_applicable(Name *n, JobType j) {
+ assert(n);
+ assert(j >= 0 && j < _JOB_TYPE_MAX);
+
+ switch (j) {
+ case JOB_VERIFY_ACTIVE:
+ case JOB_START:
+ return true;
+
+ case JOB_STOP:
+ case JOB_RESTART:
+ case JOB_TRY_RESTART:
+ return name_can_start(n);
+
+ case JOB_RELOAD:
+ return name_can_reload(n);
+
+ case JOB_RELOAD_OR_START:
+ return name_can_reload(n) && name_can_start(n);
+
+ default:
+ assert_not_reached("Invalid job type");
+ }
+}