HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/home/homed-home.c

129,052 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 12d83b8f6fbc3ea7861f40de99c2ab85b02f6207

Lines 3001–3443 of 3,443

 
        assert(!h->current_operation);
 
        if (r != 0) /* failure or completed */
                operation_result(o, r, &error);
        else /* ongoing */
                h->current_operation = operation_ref(o);
 
        return 1;
}
 
static int home_dispatch_lock_all(Home *h, Operation *o) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        int r;
 
        assert(h);
        assert(o);
        assert(o->type == OPERATION_LOCK_ALL);
 
        switch (home_get_state(h)) {
 
        case HOME_UNFIXATED:
        case HOME_ABSENT:
        case HOME_INACTIVE:
        case HOME_DIRTY:
                log_info("Home %s is not active, no locking necessary.", h->user_name);
                r = 1; /* done */
                break;
 
        case HOME_LOCKED:
                log_info("Home %s is already locked.", h->user_name);
                r = 1; /* done */
                break;
 
        case HOME_ACTIVE:
        case HOME_LINGERING:
                log_info("Locking home %s.", h->user_name);
                r = home_lock(h, &error);
                break;
 
        default:
                /* All other cases means we are currently executing an operation, which means the job remains
                 * pending. */
                return 0;
        }
 
        assert(!h->current_operation);
 
        if (r != 0) /* failure or completed */
                operation_result(o, r, &error);
        else /* ongoing */
                h->current_operation = operation_ref(o);
 
        return 1;
}
 
static int home_dispatch_deactivate_all(Home *h, Operation *o) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        int r;
 
        assert(h);
        assert(o);
        assert(o->type == OPERATION_DEACTIVATE_ALL);
 
        switch (home_get_state(h)) {
 
        case HOME_UNFIXATED:
        case HOME_ABSENT:
        case HOME_INACTIVE:
        case HOME_DIRTY:
                log_info("Home %s is already deactivated.", h->user_name);
                r = 1; /* done */
                break;
 
        case HOME_LOCKED:
                log_info("Home %s is currently locked, not deactivating.", h->user_name);
                r = 1; /* done */
                break;
 
        case HOME_ACTIVE:
        case HOME_LINGERING:
                log_info("Deactivating home %s.", h->user_name);
                r = home_deactivate_internal(h, false, &error);
                break;
 
        default:
                /* All other cases means we are currently executing an operation, which means the job remains
                 * pending. */
                return 0;
        }
 
        assert(!h->current_operation);
 
        if (r != 0) /* failure or completed */
                operation_result(o, r, &error);
        else /* ongoing */
                h->current_operation = operation_ref(o);
 
        return 1;
}
 
static int home_dispatch_pipe_eof(Home *h, Operation *o) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        int r;
 
        assert(h);
        assert(o);
        assert(o->type == OPERATION_PIPE_EOF);
 
        if (home_is_referenced(h))
                return 1; /* Hmm, there's a reference again, let's cancel this */
 
        switch (home_get_state(h)) {
 
        case HOME_UNFIXATED:
        case HOME_ABSENT:
        case HOME_INACTIVE:
        case HOME_DIRTY:
                log_info("Home %s already deactivated, no automatic deactivation needed.", h->user_name);
                break;
 
        case HOME_DEACTIVATING:
                log_info("Home %s is already being deactivated, automatic deactivated unnecessary.", h->user_name);
                break;
 
        case HOME_ACTIVE:
        case HOME_LINGERING:
                r = home_deactivate_internal(h, false, &error);
                if (r < 0)
                        log_warning_errno(r, "Failed to deactivate %s, ignoring: %s", h->user_name, bus_error_message(&error, r));
                break;
 
        case HOME_LOCKED:
        default:
                /* If the device is locked or any operation is being executed, let's leave this pending */
                return 0;
        }
 
        /* Note that we don't call operation_fail() or operation_success() here, because this kind of
         * operation has no message associated with it, and thus there's no need to propagate success. */
 
        assert(!o->message);
        return 1;
}
 
static int home_dispatch_deactivate_force(Home *h, Operation *o) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        int r;
 
        assert(h);
        assert(o);
        assert(o->type == OPERATION_DEACTIVATE_FORCE);
 
        switch (home_get_state(h)) {
 
        case HOME_UNFIXATED:
        case HOME_ABSENT:
        case HOME_INACTIVE:
        case HOME_DIRTY:
                log_debug("Home %s already deactivated, no forced deactivation due to unplug needed.", h->user_name);
                break;
 
        case HOME_DEACTIVATING:
                log_debug("Home %s is already being deactivated, forced deactivation due to unplug unnecessary.", h->user_name);
                break;
 
        case HOME_ACTIVE:
        case HOME_LOCKED:
        case HOME_LINGERING:
                r = home_deactivate_internal(h, true, &error);
                if (r < 0)
                        log_warning_errno(r, "Failed to forcibly deactivate %s, ignoring: %s", h->user_name, bus_error_message(&error, r));
                break;
 
        default:
                /* If any operation is being executed, let's leave this pending */
                return 0;
        }
 
        /* Note that we don't call operation_fail() or operation_success() here, because this kind of
         * operation has no message associated with it, and thus there's no need to propagate success. */
 
        assert(!o->message);
        return 1;
}
 
static int on_pending(sd_event_source *s, void *userdata) {
        Home *h = ASSERT_PTR(userdata);
        Operation *o;
        int r;
 
        assert(s);
 
        o = ordered_set_first(h->pending_operations);
        if (o) {
                static int (* const operation_table[_OPERATION_MAX])(Home *h, Operation *o) = {
                        [OPERATION_ACQUIRE]          = home_dispatch_acquire,
                        [OPERATION_RELEASE]          = home_dispatch_release,
                        [OPERATION_LOCK_ALL]         = home_dispatch_lock_all,
                        [OPERATION_DEACTIVATE_ALL]   = home_dispatch_deactivate_all,
                        [OPERATION_PIPE_EOF]         = home_dispatch_pipe_eof,
                        [OPERATION_DEACTIVATE_FORCE] = home_dispatch_deactivate_force,
                };
 
                assert(operation_table[o->type]);
                r = operation_table[o->type](h, o);
                if (r != 0) {
                        /* The operation completed, let's remove it from the pending list, and exit while
                         * leaving the event source enabled as it is. */
                        assert_se(ordered_set_remove(h->pending_operations, o) == o);
                        operation_unref(o);
                        return 0;
                }
        }
 
        /* Nothing to do anymore, let's turn off this event source */
        r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
        if (r < 0)
                return log_error_errno(r, "Failed to disable event source: %m");
 
        /* No operations pending anymore, maybe this is a good time to trigger a rebalancing */
        manager_reschedule_rebalance(h->manager);
        return 0;
}
 
int home_schedule_operation(Home *h, Operation *o, sd_bus_error *error) {
        int r;
 
        assert(h);
 
        if (o) {
                if (ordered_set_size(h->pending_operations) >= PENDING_OPERATIONS_MAX)
                        return sd_bus_error_set(error, BUS_ERROR_TOO_MANY_OPERATIONS, "Too many client operations requested");
 
                r = ordered_set_ensure_put(&h->pending_operations, &operation_hash_ops, o);
                if (r < 0)
                        return r;
 
                operation_ref(o);
        }
 
        if (!h->pending_event_source) {
                r = sd_event_add_defer(h->manager->event, &h->pending_event_source, on_pending, h);
                if (r < 0)
                        return log_error_errno(r, "Failed to allocate pending defer event source: %m");
 
                (void) sd_event_source_set_description(h->pending_event_source, "pending");
 
                r = sd_event_source_set_priority(h->pending_event_source, SD_EVENT_PRIORITY_IDLE);
                if (r < 0)
                        return r;
        }
 
        r = sd_event_source_set_enabled(h->pending_event_source, SD_EVENT_ON);
        if (r < 0)
                return log_error_errno(r, "Failed to trigger pending event source: %m");
 
        return 0;
}
 
static int home_get_image_path_seat(Home *h, char **ret) {
        _cleanup_(sd_device_unrefp) sd_device *d = NULL;
        const char *ip, *seat;
        struct stat st;
        int r;
 
        assert(h);
 
        if (user_record_storage(h->record) != USER_LUKS)
                return -ENXIO;
 
        ip = user_record_image_path(h->record);
        if (!ip)
                return -ENXIO;
 
        if (!path_startswith(ip, "/dev/"))
                return -ENXIO;
 
        if (stat(ip, &st) < 0)
                return -errno;
 
        r = stat_verify_block(&st);
        if (r < 0)
                return r;
 
        r = sd_device_new_from_stat_rdev(&d, &st);
        if (r < 0)
                return r;
 
        r = device_get_seat(d, &seat);
        if (r < 0)
                return r;
 
        return strdup_to(ret, seat);
}
 
int home_auto_login(Home *h, char ***ret_seats) {
        _cleanup_free_ char *seat = NULL, *seat2 = NULL;
 
        assert(h);
        assert(ret_seats);
 
        (void) home_get_image_path_seat(h, &seat);
 
        if (h->record->auto_login > 0 && !streq_ptr(seat, "seat0")) {
                /* For now, when the auto-login boolean is set for a user, let's make it mean
                 * "seat0". Eventually we can extend the concept and allow configuration of any kind of seat,
                 * but let's keep simple initially, most likely the feature is interesting on single-user
                 * systems anyway, only.
                 *
                 * We filter out users marked for auto-login in we know for sure their home directory is
                 * absent. */
 
                if (user_record_test_image_path(h->record) != USER_TEST_ABSENT) {
                        seat2 = strdup("seat0");
                        if (!seat2)
                                return -ENOMEM;
                }
        }
 
        if (seat || seat2) {
                _cleanup_strv_free_ char **list = NULL;
                size_t i = 0;
 
                list = new(char*, 3);
                if (!list)
                        return -ENOMEM;
 
                if (seat)
                        list[i++] = TAKE_PTR(seat);
                if (seat2)
                        list[i++] = TAKE_PTR(seat2);
 
                list[i] = NULL;
                *ret_seats = TAKE_PTR(list);
                return 1;
        }
 
        *ret_seats = NULL;
        return 0;
}
 
int home_set_current_message(Home *h, sd_bus_message *m) {
        assert(h);
 
        if (!m)
                return 0;
 
        if (h->current_operation)
                return -EBUSY;
 
        h->current_operation = operation_new(OPERATION_IMMEDIATE, m);
        if (!h->current_operation)
                return -ENOMEM;
 
        return 1;
}
 
int home_wait_for_worker(Home *h) {
        int r;
 
        assert(h);
 
        if (!pidref_is_set(&h->worker_pid))
                return 0;
 
        log_info("Worker process for home %s is still running while exiting. Waiting for it to finish.", h->user_name);
 
        siginfo_t si;
        r = pidref_wait_for_terminate_full(&h->worker_pid, 30 * USEC_PER_SEC, &si);
        if (r == -ETIMEDOUT)
                log_warning_errno(r, "Waiting for worker process for home %s timed out. Ignoring.", h->user_name);
        else if (r < 0)
                log_warning_errno(r, "Failed to wait for worker process for home %s, ignoring: %m", h->user_name);
        else if (si.si_code != CLD_EXITED || si.si_status != 0)
                log_warning("Worker process for home %s failed with non-zero exit status. Ignoring.", h->user_name);
 
        (void) hashmap_remove_value(h->manager->homes_by_worker_pid, &h->worker_pid, h);
        pidref_done(&h->worker_pid);
        return 1;
}
 
bool home_shall_rebalance(Home *h) {
        HomeState state;
 
        assert(h);
 
        /* Determines if the home directory is a candidate for rebalancing */
 
        if (!user_record_shall_rebalance(h->record))
                return false;
 
        state = home_get_state(h);
        if (!HOME_STATE_SHALL_REBALANCE(state))
                return false;
 
        return true;
}
 
bool home_is_busy(Home *h) {
        assert(h);
 
        if (h->current_operation)
                return true;
 
        if (!ordered_set_isempty(h->pending_operations))
                return true;
 
        return HOME_STATE_IS_EXECUTING_OPERATION(home_get_state(h));
}
 
static const char* const home_state_table[_HOME_STATE_MAX] = {
        [HOME_UNFIXATED]                   = "unfixated",
        [HOME_ABSENT]                      = "absent",
        [HOME_INACTIVE]                    = "inactive",
        [HOME_DIRTY]                       = "dirty",
        [HOME_FIXATING]                    = "fixating",
        [HOME_FIXATING_FOR_ACTIVATION]     = "fixating-for-activation",
        [HOME_FIXATING_FOR_ACQUIRE]        = "fixating-for-acquire",
        [HOME_ACTIVATING]                  = "activating",
        [HOME_ACTIVATING_FOR_ACQUIRE]      = "activating-for-acquire",
        [HOME_DEACTIVATING]                = "deactivating",
        [HOME_ACTIVE]                      = "active",
        [HOME_LINGERING]                   = "lingering",
        [HOME_LOCKING]                     = "locking",
        [HOME_LOCKED]                      = "locked",
        [HOME_UNLOCKING]                   = "unlocking",
        [HOME_UNLOCKING_FOR_ACQUIRE]       = "unlocking-for-acquire",
        [HOME_CREATING]                    = "creating",
        [HOME_REMOVING]                    = "removing",
        [HOME_UPDATING]                    = "updating",
        [HOME_UPDATING_WHILE_ACTIVE]       = "updating-while-active",
        [HOME_RESIZING]                    = "resizing",
        [HOME_RESIZING_WHILE_ACTIVE]       = "resizing-while-active",
        [HOME_PASSWD]                      = "passwd",
        [HOME_PASSWD_WHILE_ACTIVE]         = "passwd-while-active",
        [HOME_AUTHENTICATING]              = "authenticating",
        [HOME_AUTHENTICATING_WHILE_ACTIVE] = "authenticating-while-active",
        [HOME_AUTHENTICATING_FOR_ACQUIRE]  = "authenticating-for-acquire",
};
 
DEFINE_STRING_TABLE_LOOKUP(home_state, HomeState);