summaryrefslogtreecommitdiff
path: root/Kernel/SharedMemory.cpp
blob: 698efbf8dbf35c46bef3334a19023f4605f3c121 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <Kernel/SharedMemory.h>
#include <Kernel/VM/VMObject.h>
#include <Kernel/Lock.h>
#include <Kernel/Process.h>
#include <AK/HashMap.h>

Lockable<HashMap<String, RetainPtr<SharedMemory>>>& shared_memories()
{
    static Lockable<HashMap<String, RetainPtr<SharedMemory>>>* map;
    if (!map)
        map = new Lockable<HashMap<String, RetainPtr<SharedMemory>>>;
    return *map;
}

KResultOr<Retained<SharedMemory>> SharedMemory::open(const String& name, int flags, mode_t mode)
{
    UNUSED_PARAM(flags);
    LOCKER(shared_memories().lock());
    auto it = shared_memories().resource().find(name);
    if (it != shared_memories().resource().end()) {
        auto shared_memory = it->value;
        // FIXME: Improved access checking.
        if (shared_memory->uid() != current->process().uid())
            return KResult(-EACCES);
        return *shared_memory;
    }
    auto shared_memory = adopt(*new SharedMemory(name, current->process().uid(), current->process().gid(), mode));
    shared_memories().resource().set(name, shared_memory.ptr());
    return shared_memory;
}

KResult SharedMemory::unlink(const String& name)
{
    LOCKER(shared_memories().lock());
    auto it = shared_memories().resource().find(name);
    if (it == shared_memories().resource().end())
        return KResult(-ENOENT);
    shared_memories().resource().remove(it);
    return KSuccess;
}

SharedMemory::SharedMemory(const String& name, uid_t uid, gid_t gid, mode_t mode)
    : m_name(name)
    , m_uid(uid)
    , m_gid(gid)
    , m_mode(mode)
{
}

SharedMemory::~SharedMemory()
{
}

KResult SharedMemory::truncate(int length)
{
    if (!length) {
        m_vmo = nullptr;
        return KSuccess;
    }

    if (!m_vmo) {
        m_vmo = VMObject::create_anonymous(length);
        return KSuccess;
    }

    // FIXME: Support truncation.
    ASSERT_NOT_REACHED();
    return KResult(-ENOTIMPL);
}

String SharedMemory::absolute_path() const
{
    return String::format("shm:%u", this);
}

int SharedMemory::read(Process&, byte* buffer, int buffer_size)
{
    UNUSED_PARAM(buffer);
    UNUSED_PARAM(buffer_size);
    // FIXME: Implement.
    ASSERT_NOT_REACHED();
}

int SharedMemory::write(Process&, const byte* data, int data_size)
{
    UNUSED_PARAM(data);
    UNUSED_PARAM(data_size);
    // FIXME: Implement.
    ASSERT_NOT_REACHED();
}