HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/core/automount.c

41,408 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: ec42d7fb0d76770fe9489427925efff220931645

Lines 1001–1229 of 1,229

                        r = set_ensure_put(&a->expire_tokens, NULL, UINT_TO_PTR(token));
                        if (r < 0)
                                log_unit_error_errno(u, r, "Failed to add expire token to set: %m");
                }
        } else if (streq(key, "pipe-fd")) {
                safe_close(a->pipe_fd);
                a->pipe_fd = deserialize_fd(fds, value);
        } else
                log_unit_debug(u, "Unknown serialization key: %s", key);
 
        return 0;
}
 
static UnitActiveState automount_active_state(Unit *u) {
        assert(u);
 
        return state_translation_table[AUTOMOUNT(u)->state];
}
 
static const char *automount_sub_state_to_string(Unit *u) {
        assert(u);
 
        return automount_state_to_string(AUTOMOUNT(u)->state);
}
 
static bool automount_may_gc(Unit *u) {
        Unit *t;
 
        assert(u);
 
        t = UNIT_TRIGGER(u);
        if (!t)
                return true;
 
        return UNIT_VTABLE(t)->may_gc(t);
}
 
static int automount_dispatch_io(sd_event_source *s, int fd, uint32_t events, void *userdata) {
        Automount *a = ASSERT_PTR(AUTOMOUNT(userdata));
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        union autofs_v5_packet_union packet;
        Unit *trigger;
        int r;
 
        assert(fd == a->pipe_fd);
 
        if (events & (EPOLLHUP|EPOLLERR)) {
                log_unit_error(UNIT(a), "Got hangup/error on autofs pipe from kernel. Likely our automount point has been unmounted by someone or something else?");
                automount_enter_dead(a, AUTOMOUNT_FAILURE_UNMOUNTED);
                return 0;
        }
 
        if (events != EPOLLIN) {
                log_unit_error(UNIT(a), "Got invalid poll event %"PRIu32" on pipe (fd=%d)", events, fd);
                goto fail;
        }
 
        r = loop_read_exact(a->pipe_fd, &packet, sizeof(packet), true);
        if (r < 0) {
                log_unit_error_errno(UNIT(a), r, "Invalid read from pipe: %m");
                goto fail;
        }
 
        switch (packet.hdr.type) {
 
        case autofs_ptype_missing_direct:
 
                if (packet.v5_packet.pid > 0) {
                        _cleanup_free_ char *p = NULL;
 
                        (void) pid_get_comm(packet.v5_packet.pid, &p);
                        log_unit_info(UNIT(a), "Got automount request for %s, triggered by %"PRIu32" (%s)", a->where, packet.v5_packet.pid, strna(p));
                } else
                        log_unit_debug(UNIT(a), "Got direct mount request on %s", a->where);
 
                r = set_ensure_put(&a->tokens, NULL, UINT_TO_PTR(packet.v5_packet.wait_queue_token));
                if (r < 0) {
                        log_unit_error_errno(UNIT(a), r, "Failed to remember token: %m");
                        goto fail;
                }
 
                automount_enter_running(a);
                break;
 
        case autofs_ptype_expire_direct:
                log_unit_debug(UNIT(a), "Got direct umount request on %s", a->where);
 
                automount_stop_expire(a);
 
                r = set_ensure_put(&a->expire_tokens, NULL, UINT_TO_PTR(packet.v5_packet.wait_queue_token));
                if (r < 0) {
                        log_unit_error_errno(UNIT(a), r, "Failed to remember token: %m");
                        goto fail;
                }
 
                trigger = UNIT_TRIGGER(UNIT(a));
                if (!trigger) {
                        log_unit_error(UNIT(a), "Unit to trigger vanished.");
                        goto fail;
                }
 
                r = manager_add_job(UNIT(a)->manager, JOB_STOP, trigger, JOB_REPLACE, &error, /* ret= */ NULL);
                if (r < 0) {
                        log_unit_warning(UNIT(a), "Failed to queue unmount job: %s", bus_error_message(&error, r));
                        goto fail;
                }
                break;
 
        default:
                log_unit_error(UNIT(a), "Received unknown automount request %i", packet.hdr.type);
        }
 
        return 0;
 
fail:
        automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
        return 0;
}
 
static void automount_shutdown(Manager *m) {
        assert(m);
 
        m->dev_autofs_fd = safe_close(m->dev_autofs_fd);
}
 
static void automount_reset_failed(Unit *u) {
        Automount *a = ASSERT_PTR(AUTOMOUNT(u));
 
        if (a->state == AUTOMOUNT_FAILED)
                automount_set_state(a, AUTOMOUNT_DEAD);
 
        a->result = AUTOMOUNT_SUCCESS;
}
 
static bool automount_supported(void) {
        static int supported = -1;
 
        if (!unit_type_supported(UNIT_MOUNT))
                return false;
 
        if (supported < 0)
                supported = access("/dev/autofs", F_OK) >= 0;
 
        return supported;
}
 
static int automount_test_startable(Unit *u) {
        Automount *a = ASSERT_PTR(AUTOMOUNT(u));
        int r;
 
        r = unit_test_trigger_loaded(u);
        if (r < 0)
                return r;
 
        r = unit_test_start_limit(u);
        if (r < 0) {
                automount_enter_dead(a, AUTOMOUNT_FAILURE_START_LIMIT_HIT);
                return r;
        }
 
        return true;
}
 
static const char* const automount_result_table[_AUTOMOUNT_RESULT_MAX] = {
        [AUTOMOUNT_SUCCESS]                       = "success",
        [AUTOMOUNT_FAILURE_RESOURCES]             = "resources",
        [AUTOMOUNT_FAILURE_START_LIMIT_HIT]       = "start-limit-hit",
        [AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT] = "mount-start-limit-hit",
        [AUTOMOUNT_FAILURE_UNMOUNTED]             = "unmounted",
};
 
DEFINE_STRING_TABLE_LOOKUP(automount_result, AutomountResult);
 
const UnitVTable automount_vtable = {
        .object_size = sizeof(Automount),
 
        .sections =
                "Unit\0"
                "Automount\0"
                "Install\0",
        .private_section = "Automount",
 
        .can_transient = true,
        .can_fail = true,
        .can_trigger = true,
        .exclude_from_switch_root_serialization = true,
 
        .init = automount_init,
        .load = automount_load,
        .done = automount_done,
 
        .coldplug = automount_coldplug,
 
        .dump = automount_dump,
 
        .start = automount_start,
        .stop = automount_stop,
 
        .serialize = automount_serialize,
        .deserialize_item = automount_deserialize_item,
 
        .active_state = automount_active_state,
        .sub_state_to_string = automount_sub_state_to_string,
 
        .may_gc = automount_may_gc,
 
        .trigger_notify = automount_trigger_notify,
 
        .reset_failed = automount_reset_failed,
 
        .bus_set_property = bus_automount_set_property,
 
        .shutdown = automount_shutdown,
        .supported = automount_supported,
 
        .status_message_formats = {
                .finished_start_job = {
                        [JOB_DONE]       = "Set up automount %s.",
                        [JOB_FAILED]     = "Failed to set up automount %s.",
                },
                .finished_stop_job = {
                        [JOB_DONE]       = "Unset automount %s.",
                        [JOB_FAILED]     = "Failed to unset automount %s.",
                },
        },
 
        .test_startable = automount_test_startable,
};