summaryrefslogtreecommitdiff
path: root/Kernel/FileSystem/SysFS/Subsystems/Kernel/Constants/ConstantInformation.cpp
blob: d9e6ea3e3f83d1e7b30a6dfaa840f4968c072695 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
 * Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <Kernel/FileSystem/SysFS/Subsystems/Kernel/Constants/ConstantInformation.h>
#include <Kernel/Process.h>
#include <Kernel/Sections.h>

namespace Kernel {

StringView SysFSSystemConstantInformation::name() const
{
    switch (m_node_name) {
    case NodeName::LoadBase:
        return "load_base"sv;
    case NodeName::CommandLine:
        return "cmdline"sv;
    case NodeName::SystemMode:
        return "system_mode"sv;
    default:
        VERIFY_NOT_REACHED();
    }
}

NonnullRefPtr<SysFSSystemConstantInformation> SysFSSystemConstantInformation::must_create(SysFSDirectory const& parent_directory, NonnullOwnPtr<KBuffer> constant_data_buffer, mode_t mode, ReadableByJailedProcesses readable_by_jailed_processes, NodeName name)
{
    auto node = adopt_ref_if_nonnull(new (nothrow) SysFSSystemConstantInformation(parent_directory, move(constant_data_buffer), mode, readable_by_jailed_processes, name)).release_nonnull();
    return node;
}

ErrorOr<size_t> SysFSSystemConstantInformation::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const
{
    if (offset < 0)
        return EINVAL;
    if (static_cast<size_t>(offset) >= m_constant_data_buffer->size())
        return 0;
    TRY(Process::current().jail().with([&](auto const& my_jail) -> ErrorOr<void> {
        if (my_jail && m_readable_by_jailed_processes == ReadableByJailedProcesses::No)
            return Error::from_errno(EPERM);
        return {};
    }));
    ssize_t nread = min(static_cast<off_t>(m_constant_data_buffer->size() - offset), static_cast<off_t>(count));
    TRY(buffer.write(m_constant_data_buffer->data() + offset, nread));
    return nread;
}

}