Beispiel #1
0
static void busname_trigger_notify(Unit *u, Unit *other) {
        BusName *n = BUSNAME(u);

        assert(n);
        assert(other);

        if (!IN_SET(n->state, BUSNAME_RUNNING, BUSNAME_LISTENING))
                return;

        if (other->start_limit_hit) {
                busname_enter_dead(n, BUSNAME_FAILURE_SERVICE_START_LIMIT_HIT);
                return;
        }

        if (other->load_state != UNIT_LOADED || other->type != UNIT_SERVICE)
                return;

        if (IN_SET(SERVICE(other)->state,
                   SERVICE_DEAD, SERVICE_FAILED,
                   SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
                   SERVICE_AUTO_RESTART))
                busname_enter_listening(n);

        if (SERVICE(other)->state == SERVICE_RUNNING)
                busname_set_state(n, BUSNAME_RUNNING);
}
static void busname_enter_dead(BusName *n, BusNameResult f) {
        assert(n);

        if (f != BUSNAME_SUCCESS)
                n->result = f;

        busname_set_state(n, n->result != BUSNAME_SUCCESS ? BUSNAME_FAILED : BUSNAME_DEAD);
}
static void busname_reset_failed(Unit *u) {
        BusName *n = BUSNAME(u);

        assert(n);

        if (n->state == BUSNAME_FAILED)
                busname_set_state(n, BUSNAME_DEAD);

        n->result = BUSNAME_SUCCESS;
}
static void busname_enter_listening(BusName *n) {
        int r;

        assert(n);

        if (n->activating) {
                r = busname_watch_fd(n);
                if (r < 0) {
                        log_unit_warning_errno(UNIT(n)->id, r, "%s failed to watch names: %m", UNIT(n)->id);
                        goto fail;
                }

                busname_set_state(n, BUSNAME_LISTENING);
        } else
                busname_set_state(n, BUSNAME_REGISTERED);

        return;

fail:
        busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_FAILURE_RESOURCES);
}
Beispiel #5
0
static void busname_enter_running(BusName *n) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        bool pending = false;
        Unit *other;
        Iterator i;
        int r;

        assert(n);

        if (!n->activating)
                return;

        /* We don't take connections anymore if we are supposed to
         * shut down anyway */

        if (unit_stop_pending(UNIT(n))) {
                log_unit_debug(UNIT(n), "Suppressing activation request since unit stop is scheduled.");

                /* Flush all queued activation reqeuest by closing and reopening the connection */
                bus_kernel_drop_one(n->starter_fd);

                busname_enter_listening(n);
                return;
        }

        /* If there's already a start pending don't bother to do
         * anything */
        SET_FOREACH(other, UNIT(n)->dependencies[UNIT_TRIGGERS], i)
                if (unit_active_or_pending(other)) {
                        pending = true;
                        break;
                }

        if (!pending) {
                if (!UNIT_ISSET(n->service)) {
                        log_unit_error(UNIT(n), "Service to activate vanished, refusing activation.");
                        r = -ENOENT;
                        goto fail;
                }

                r = manager_add_job(UNIT(n)->manager, JOB_START, UNIT_DEREF(n->service), JOB_REPLACE, &error, NULL);
                if (r < 0)
                        goto fail;
        }

        busname_set_state(n, BUSNAME_RUNNING);
        return;

fail:
        log_unit_warning(UNIT(n), "Failed to queue service startup job: %s", bus_error_message(&error, r));
        busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
}
static void busname_enter_making(BusName *n) {
        int r;

        assert(n);

        r = busname_open_fd(n);
        if (r < 0)
                goto fail;

        if (n->policy) {
                /* If there is a policy, we need to resolve user/group
                 * names, which we can't do from PID1, hence let's
                 * fork. */
                busname_unwatch_control_pid(n);

                r = busname_make_starter(n, &n->control_pid);
                if (r < 0) {
                        log_unit_warning_errno(UNIT(n)->id, r, "%s failed to fork 'making' task: %m", UNIT(n)->id);
                        goto fail;
                }

                busname_set_state(n, BUSNAME_MAKING);
        } else {
                /* If there is no policy, we can do everything
                 * directly from PID 1, hence do so. */

                r = bus_kernel_make_starter(n->starter_fd, n->name, n->activating, n->accept_fd, NULL, n->policy_world);
                if (r < 0) {
                        log_unit_warning_errno(UNIT(n)->id, r, "%s failed to make starter: %m", UNIT(n)->id);
                        goto fail;
                }

                busname_enter_listening(n);
        }

        return;

fail:
        busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
}
static void busname_enter_signal(BusName *n, BusNameState state, BusNameResult f) {
        KillContext kill_context = {};
        int r;

        assert(n);

        if (f != BUSNAME_SUCCESS)
                n->result = f;

        kill_context_init(&kill_context);

        r = unit_kill_context(UNIT(n),
                              &kill_context,
                              state != BUSNAME_SIGTERM ? KILL_KILL : KILL_TERMINATE,
                              -1,
                              n->control_pid,
                              false);
        if (r < 0) {
                log_unit_warning_errno(UNIT(n)->id, r, "%s failed to kill control process: %m", UNIT(n)->id);
                goto fail;
        }

        if (r > 0) {
                r = busname_arm_timer(n);
                if (r < 0) {
                        log_unit_warning_errno(UNIT(n)->id, r, "%s failed to arm timer: %m", UNIT(n)->id);
                        goto fail;
                }

                busname_set_state(n, state);
        } else if (state == BUSNAME_SIGTERM)
                busname_enter_signal(n, BUSNAME_SIGKILL, BUSNAME_SUCCESS);
        else
                busname_enter_dead(n, BUSNAME_SUCCESS);

        return;

fail:
        busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
}
static int busname_coldplug(Unit *u, Hashmap *deferred_work) {
        BusName *n = BUSNAME(u);
        int r;

        assert(n);
        assert(n->state == BUSNAME_DEAD);

        if (n->deserialized_state == n->state)
                return 0;

        if (IN_SET(n->deserialized_state, BUSNAME_MAKING, BUSNAME_SIGTERM, BUSNAME_SIGKILL)) {

                if (n->control_pid <= 0)
                        return -EBADMSG;

                r = unit_watch_pid(UNIT(n), n->control_pid);
                if (r < 0)
                        return r;

                r = busname_arm_timer(n);
                if (r < 0)
                        return r;
        }

        if (IN_SET(n->deserialized_state, BUSNAME_MAKING, BUSNAME_LISTENING, BUSNAME_REGISTERED, BUSNAME_RUNNING)) {
                r = busname_open_fd(n);
                if (r < 0)
                        return r;
        }

        if (n->deserialized_state == BUSNAME_LISTENING) {
                r = busname_watch_fd(n);
                if (r < 0)
                        return r;
        }

        busname_set_state(n, n->deserialized_state);
        return 0;
}
Beispiel #9
0
static int busname_coldplug(Unit *u) {
        BusName *n = BUSNAME(u);
        int r;

        assert(n);
        assert(n->state == BUSNAME_DEAD);

        if (n->deserialized_state == n->state)
                return 0;

        if (n->control_pid > 0 &&
            pid_is_unwaited(n->control_pid) &&
            IN_SET(n->deserialized_state, BUSNAME_MAKING, BUSNAME_SIGTERM, BUSNAME_SIGKILL)) {

                r = unit_watch_pid(UNIT(n), n->control_pid);
                if (r < 0)
                        return r;

                r = busname_arm_timer(n, usec_add(u->state_change_timestamp.monotonic, n->timeout_usec));
                if (r < 0)
                        return r;
        }

        if (IN_SET(n->deserialized_state, BUSNAME_MAKING, BUSNAME_LISTENING, BUSNAME_REGISTERED, BUSNAME_RUNNING)) {
                r = busname_open_fd(n);
                if (r < 0)
                        return r;
        }

        if (n->deserialized_state == BUSNAME_LISTENING) {
                r = busname_watch_fd(n);
                if (r < 0)
                        return r;
        }

        busname_set_state(n, n->deserialized_state);
        return 0;
}