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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Spencer Dixon <spencercdixon@gmail.com>
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Singleton.h>
#include <Kernel/Debug.h>
#include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/FileBackedFileSystem.h>
#include <Kernel/FileSystem/FileDescription.h>
#include <Kernel/FileSystem/ProcFS.h>
#include <Kernel/FileSystem/VirtualFileSystem.h>
#include <Kernel/Heap/kmalloc.h>
#include <Kernel/Sections.h>
#include <LibC/errno_numbers.h>
namespace Kernel {
static AK::Singleton<ProcFSComponentsRegistrar> s_the;
ProcFSComponentsRegistrar& ProcFSComponentsRegistrar::the()
{
return *s_the;
}
UNMAP_AFTER_INIT void ProcFSComponentsRegistrar::initialize()
{
VERIFY(!s_the.is_initialized());
s_the.ensure_instance();
}
UNMAP_AFTER_INIT ProcFSComponentsRegistrar::ProcFSComponentsRegistrar()
: m_root_folder(ProcFSRootFolder::must_create())
{
}
const ProcFSBusDirectory& ProcFSComponentsRegistrar::buses_folder() const
{
return *m_root_folder->m_buses_folder;
}
void ProcFSComponentsRegistrar::register_new_bus_folder(ProcFSExposedFolder& new_bus_folder)
{
VERIFY(!m_root_folder->m_buses_folder.is_null());
m_root_folder->m_buses_folder->m_components.append(new_bus_folder);
}
void ProcFSComponentsRegistrar::register_new_process(Process& new_process)
{
Locker locker(m_lock);
m_root_folder->m_process_folders.append(ProcFSProcessFolder::create(new_process));
}
void ProcFSComponentsRegistrar::unregister_process(Process& deleted_process)
{
auto process_folder = m_root_folder->process_folder_for(deleted_process).release_nonnull();
process_folder->prepare_for_deletion();
process_folder->m_list_node.remove();
dbgln_if(PROCFS_DEBUG, "ProcFSExposedFolder ref_count now: {}", process_folder->ref_count());
// Note: Let's ensure we are the last holder of the ProcFSProcessFolder object before it can be deleted for good
VERIFY(process_folder->ref_count() == 1);
}
RefPtr<ProcFS> ProcFS::create()
{
return adopt_ref_if_nonnull(new (nothrow) ProcFS);
}
ProcFS::~ProcFS()
{
}
bool ProcFS::initialize()
{
return true;
}
NonnullRefPtr<Inode> ProcFS::root_inode() const
{
return *m_root_inode;
}
NonnullRefPtr<ProcFSInode> ProcFSInode::create(const ProcFS& fs, const ProcFSExposedComponent& component)
{
return adopt_ref(*new (nothrow) ProcFSInode(fs, component));
}
ProcFSInode::ProcFSInode(const ProcFS& fs, const ProcFSExposedComponent& component)
: Inode(const_cast<ProcFS&>(fs), component.component_index())
, m_associated_component(component)
{
}
KResult ProcFSInode::attach(FileDescription& description)
{
return m_associated_component->refresh_data(description);
}
void ProcFSInode::did_seek(FileDescription& description, off_t new_offset)
{
if (new_offset != 0)
return;
auto result = m_associated_component->refresh_data(description);
if (result.is_error()) {
// Subsequent calls to read will return EIO!
dbgln("ProcFS: Could not refresh contents: {}", result.error());
}
}
ProcFSInode::~ProcFSInode()
{
}
ProcFS::ProcFS()
: m_root_inode(ProcFSComponentsRegistrar::the().m_root_folder->to_inode(*this))
{
}
KResultOr<size_t> ProcFSInode::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, FileDescription* fd) const
{
return m_associated_component->read_bytes(offset, count, buffer, fd);
}
StringView ProcFSInode::name() const
{
return m_associated_component->name();
}
KResult ProcFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)>) const
{
VERIFY_NOT_REACHED();
}
RefPtr<Inode> ProcFSInode::lookup(StringView)
{
VERIFY_NOT_REACHED();
}
InodeMetadata ProcFSInode::metadata() const
{
Locker locker(m_lock);
InodeMetadata metadata;
metadata.inode = { fsid(), m_associated_component->component_index() };
metadata.mode = m_associated_component->required_mode();
metadata.uid = m_associated_component->owner_user();
metadata.gid = m_associated_component->owner_group();
metadata.size = m_associated_component->size();
metadata.mtime = m_associated_component->modified_time();
return metadata;
}
void ProcFSInode::flush_metadata()
{
}
KResultOr<size_t> ProcFSInode::write_bytes(off_t offset, size_t count, const UserOrKernelBuffer& buffer, FileDescription* fd)
{
return m_associated_component->write_bytes(offset, count, buffer, fd);
}
KResultOr<NonnullRefPtr<Inode>> ProcFSInode::create_child(const String&, mode_t, dev_t, uid_t, gid_t)
{
return EROFS;
}
KResult ProcFSInode::add_child(Inode&, const StringView&, mode_t)
{
return EROFS;
}
KResult ProcFSInode::remove_child(const StringView&)
{
return EROFS;
}
KResultOr<size_t> ProcFSInode::directory_entry_count() const
{
VERIFY_NOT_REACHED();
}
KResult ProcFSInode::chmod(mode_t)
{
return EPERM;
}
KResult ProcFSInode::chown(uid_t, gid_t)
{
return EPERM;
}
KResult ProcFSInode::truncate(u64)
{
return EPERM;
}
NonnullRefPtr<ProcFSDirectoryInode> ProcFSDirectoryInode::create(const ProcFS& procfs, const ProcFSExposedComponent& component)
{
return adopt_ref(*new (nothrow) ProcFSDirectoryInode(procfs, component));
}
ProcFSDirectoryInode::ProcFSDirectoryInode(const ProcFS& fs, const ProcFSExposedComponent& component)
: ProcFSInode(fs, component)
, m_parent_fs(const_cast<ProcFS&>(fs))
{
}
ProcFSDirectoryInode::~ProcFSDirectoryInode()
{
}
InodeMetadata ProcFSDirectoryInode::metadata() const
{
Locker locker(m_lock);
InodeMetadata metadata;
metadata.inode = { fsid(), m_associated_component->component_index() };
metadata.mode = S_IFDIR | m_associated_component->required_mode();
metadata.uid = m_associated_component->owner_user();
metadata.gid = m_associated_component->owner_group();
metadata.size = 0;
metadata.mtime = m_associated_component->modified_time();
return metadata;
}
KResult ProcFSDirectoryInode::traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)> callback) const
{
Locker locker(m_parent_fs.m_lock);
return m_associated_component->traverse_as_directory(m_parent_fs.fsid(), move(callback));
}
RefPtr<Inode> ProcFSDirectoryInode::lookup(StringView name)
{
Locker locker(m_parent_fs.m_lock);
auto component = m_associated_component->lookup(name);
if (!component)
return {};
return component->to_inode(m_parent_fs);
}
KResultOr<size_t> ProcFSDirectoryInode::directory_entry_count() const
{
Locker locker(m_lock);
return m_associated_component->entries_count();
}
NonnullRefPtr<ProcFSLinkInode> ProcFSLinkInode::create(const ProcFS& procfs, const ProcFSExposedComponent& component)
{
return adopt_ref(*new (nothrow) ProcFSLinkInode(procfs, component));
}
ProcFSLinkInode::ProcFSLinkInode(const ProcFS& fs, const ProcFSExposedComponent& component)
: ProcFSInode(fs, component)
{
}
InodeMetadata ProcFSLinkInode::metadata() const
{
Locker locker(m_lock);
InodeMetadata metadata;
metadata.inode = { fsid(), m_associated_component->component_index() };
metadata.mode = S_IFLNK | m_associated_component->required_mode();
metadata.uid = m_associated_component->owner_user();
metadata.gid = m_associated_component->owner_group();
metadata.size = 0;
metadata.mtime = m_associated_component->modified_time();
return metadata;
}
}
|