summaryrefslogtreecommitdiff
path: root/Kernel/FileSystem/DevPtsFS.cpp
blob: 73d2113e516f84aa4712eb5742cd18437165a7f8 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <AK/StringBuilder.h>
#include <Kernel/FileSystem/DevPtsFS.h>
#include <Kernel/FileSystem/VirtualFileSystem.h>
#include <Kernel/TTY/SlavePTY.h>

NonnullRefPtr<DevPtsFS> DevPtsFS::create()
{
    return adopt(*new DevPtsFS);
}

DevPtsFS::DevPtsFS()
{
}

DevPtsFS::~DevPtsFS()
{
}

static HashTable<unsigned>* ptys;

bool DevPtsFS::initialize()
{
    if (ptys == nullptr) {
        ptys = new HashTable<unsigned>();
    }

    m_root_inode = adopt(*new DevPtsFSInode(*this, 1));
    m_root_inode->m_metadata.inode = { fsid(), 1 };
    m_root_inode->m_metadata.mode = 0040555;
    m_root_inode->m_metadata.uid = 0;
    m_root_inode->m_metadata.gid = 0;
    m_root_inode->m_metadata.size = 0;
    m_root_inode->m_metadata.mtime = mepoch;

    return true;
}

static unsigned inode_index_to_pty_index(unsigned inode_index)
{
    ASSERT(inode_index > 1);
    return inode_index - 2;
}

static unsigned pty_index_to_inode_index(unsigned pty_index)
{
    return pty_index + 2;
}

InodeIdentifier DevPtsFS::root_inode() const
{
    return { fsid(), 1 };
}

RefPtr<Inode> DevPtsFS::create_inode(InodeIdentifier, const String&, mode_t, off_t, dev_t, int& error)
{
    error = -EROFS;
    return nullptr;
}

RefPtr<Inode> DevPtsFS::create_directory(InodeIdentifier, const String&, mode_t, int& error)
{
    error = -EROFS;
    return nullptr;
}

RefPtr<Inode> DevPtsFS::get_inode(InodeIdentifier inode_id) const
{
    if (inode_id.index() == 1)
        return m_root_inode;

    unsigned pty_index = inode_index_to_pty_index(inode_id.index());
    auto* device = Device::get_device(11, pty_index);
    ASSERT(device);

    auto inode = adopt(*new DevPtsFSInode(const_cast<DevPtsFS&>(*this), inode_id.index()));
    inode->m_metadata.inode = inode_id;
    inode->m_metadata.size = 0;
    inode->m_metadata.uid = device->uid();
    inode->m_metadata.gid = device->gid();
    inode->m_metadata.mode = 0020644;
    inode->m_metadata.major_device = device->major();
    inode->m_metadata.minor_device = device->minor();
    inode->m_metadata.mtime = mepoch;

    return inode;
}

void DevPtsFS::register_slave_pty(SlavePTY& slave_pty)
{
    ptys->set(slave_pty.index());
}

void DevPtsFS::unregister_slave_pty(SlavePTY& slave_pty)
{
    ptys->remove(slave_pty.index());
}

DevPtsFSInode::DevPtsFSInode(DevPtsFS& fs, unsigned index)
    : Inode(fs, index)
{
}

DevPtsFSInode::~DevPtsFSInode()
{
}

ssize_t DevPtsFSInode::read_bytes(off_t, ssize_t, u8*, FileDescription*) const
{
    ASSERT_NOT_REACHED();
}

ssize_t DevPtsFSInode::write_bytes(off_t, ssize_t, const u8*, FileDescription*)
{
    ASSERT_NOT_REACHED();
}

InodeMetadata DevPtsFSInode::metadata() const
{
    return m_metadata;
}

bool DevPtsFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntry&)> callback) const
{
    if (identifier().index() > 1)
        return false;

    callback({ ".", 1, identifier(), 0 });
    callback({ "..", 2, identifier(), 0 });

    for (unsigned pty_index : *ptys) {
        String name = String::number(pty_index);
        InodeIdentifier identifier = { fsid(), pty_index_to_inode_index(pty_index) };
        callback({ name.characters(), name.length(), identifier, 0 });
    }

    return true;
}

size_t DevPtsFSInode::directory_entry_count() const
{
    ASSERT(identifier().index() == 1);

    return 2 + ptys->size();
}

InodeIdentifier DevPtsFSInode::lookup(StringView name)
{
    ASSERT(identifier().index() == 1);

    if (name == "." || name == "..")
        return identifier();

    bool ok;
    unsigned pty_index = name.to_uint(ok);
    if (ok && ptys->contains(pty_index)) {
        return { fsid(), pty_index_to_inode_index(pty_index) };
    }

    return {};
}

void DevPtsFSInode::flush_metadata()
{
}

KResult DevPtsFSInode::add_child(InodeIdentifier, const StringView&, mode_t)
{
    return KResult(-EROFS);
}

KResult DevPtsFSInode::remove_child(const StringView&)
{
    return KResult(-EROFS);
}

KResult DevPtsFSInode::chmod(mode_t)
{
    return KResult(-EPERM);
}

KResult DevPtsFSInode::chown(uid_t, gid_t)
{
    return KResult(-EPERM);
}