2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2020-01-24 16:45:29 +03:00
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2020-08-24 19:35:19 -06:00
|
|
|
#include <AK/Singleton.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-03-23 13:45:10 +01:00
|
|
|
#include <AK/StringView.h>
|
2019-04-03 12:25:24 +02:00
|
|
|
#include <Kernel/FileSystem/DevPtsFS.h>
|
|
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <Kernel/TTY/SlavePTY.h>
|
2019-01-30 00:49:20 +01:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
NonnullRefPtr<DevPtsFS> DevPtsFS::create()
|
2019-01-30 00:49:20 +01:00
|
|
|
{
|
2021-04-23 16:46:57 +02:00
|
|
|
return adopt_ref(*new DevPtsFS);
|
2019-01-30 00:49:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DevPtsFS::DevPtsFS()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DevPtsFS::~DevPtsFS()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 19:35:19 -06:00
|
|
|
static AK::Singleton<HashTable<unsigned>> s_ptys;
|
2019-08-16 18:46:18 +03:00
|
|
|
|
2019-01-30 00:49:20 +01:00
|
|
|
bool DevPtsFS::initialize()
|
|
|
|
|
{
|
2021-04-23 16:46:57 +02:00
|
|
|
m_root_inode = adopt_ref(*new DevPtsFSInode(*this, 1, nullptr));
|
2019-08-16 18:46:18 +03:00
|
|
|
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;
|
|
|
|
|
|
2019-01-30 00:49:20 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-12 09:18:47 +01:00
|
|
|
static unsigned inode_index_to_pty_index(InodeIndex inode_index)
|
2019-08-16 18:46:18 +03:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(inode_index > 1);
|
2021-02-12 09:18:47 +01:00
|
|
|
return inode_index.value() - 2;
|
2019-08-16 18:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
2021-02-12 09:18:47 +01:00
|
|
|
static InodeIndex pty_index_to_inode_index(unsigned pty_index)
|
2019-08-16 18:46:18 +03:00
|
|
|
{
|
|
|
|
|
return pty_index + 2;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-24 23:35:56 +03:00
|
|
|
NonnullRefPtr<Inode> DevPtsFS::root_inode() const
|
2019-08-16 18:46:18 +03:00
|
|
|
{
|
2020-06-24 23:35:56 +03:00
|
|
|
return *m_root_inode;
|
2019-01-30 00:49:20 +01:00
|
|
|
}
|
|
|
|
|
|
2019-08-16 18:46:18 +03:00
|
|
|
RefPtr<Inode> DevPtsFS::get_inode(InodeIdentifier inode_id) const
|
2019-01-30 00:49:20 +01:00
|
|
|
{
|
2019-08-16 18:46:18 +03:00
|
|
|
if (inode_id.index() == 1)
|
|
|
|
|
return m_root_inode;
|
2019-01-30 00:49:20 +01:00
|
|
|
|
2019-08-16 18:46:18 +03:00
|
|
|
unsigned pty_index = inode_index_to_pty_index(inode_id.index());
|
2019-12-09 21:03:39 +01:00
|
|
|
auto* device = Device::get_device(201, pty_index);
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(device);
|
2019-01-31 05:55:30 +01:00
|
|
|
|
2021-04-23 16:46:57 +02:00
|
|
|
auto inode = adopt_ref(*new DevPtsFSInode(const_cast<DevPtsFS&>(*this), inode_id.index(), static_cast<SlavePTY*>(device)));
|
2019-08-16 18:46:18 +03:00
|
|
|
inode->m_metadata.inode = inode_id;
|
|
|
|
|
inode->m_metadata.size = 0;
|
|
|
|
|
inode->m_metadata.uid = device->uid();
|
|
|
|
|
inode->m_metadata.gid = device->gid();
|
2020-01-04 12:44:27 +01:00
|
|
|
inode->m_metadata.mode = 0020600;
|
2019-08-16 18:46:18 +03:00
|
|
|
inode->m_metadata.major_device = device->major();
|
|
|
|
|
inode->m_metadata.minor_device = device->minor();
|
|
|
|
|
inode->m_metadata.mtime = mepoch;
|
|
|
|
|
|
|
|
|
|
return inode;
|
2019-01-30 00:49:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DevPtsFS::register_slave_pty(SlavePTY& slave_pty)
|
|
|
|
|
{
|
2020-08-24 19:35:19 -06:00
|
|
|
s_ptys->set(slave_pty.index());
|
2019-01-30 00:49:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DevPtsFS::unregister_slave_pty(SlavePTY& slave_pty)
|
|
|
|
|
{
|
2020-08-24 19:35:19 -06:00
|
|
|
s_ptys->remove(slave_pty.index());
|
2019-08-16 18:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
2021-02-12 09:18:47 +01:00
|
|
|
DevPtsFSInode::DevPtsFSInode(DevPtsFS& fs, InodeIndex index, SlavePTY* pty)
|
2019-08-16 18:46:18 +03:00
|
|
|
: Inode(fs, index)
|
|
|
|
|
{
|
2020-09-06 18:48:24 +02:00
|
|
|
if (pty)
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-29 16:26:13 -06:00
|
|
|
m_pty = *pty;
|
2019-08-16 18:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DevPtsFSInode::~DevPtsFSInode()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-01 14:29:39 -07:00
|
|
|
KResultOr<ssize_t> DevPtsFSInode::read_bytes(off_t, ssize_t, UserOrKernelBuffer&, FileDescription*) const
|
2019-08-16 18:46:18 +03:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-08-16 18:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
2021-05-01 14:29:39 -07:00
|
|
|
KResultOr<ssize_t> DevPtsFSInode::write_bytes(off_t, ssize_t, const UserOrKernelBuffer&, FileDescription*)
|
2019-08-16 18:46:18 +03:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-08-16 18:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InodeMetadata DevPtsFSInode::metadata() const
|
|
|
|
|
{
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-29 16:26:13 -06:00
|
|
|
if (auto pty = m_pty.strong_ref()) {
|
2020-09-06 18:48:24 +02:00
|
|
|
auto metadata = m_metadata;
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-29 16:26:13 -06:00
|
|
|
metadata.mtime = pty->time_of_last_write();
|
2020-09-06 18:48:24 +02:00
|
|
|
return metadata;
|
|
|
|
|
}
|
2019-08-16 18:46:18 +03:00
|
|
|
return m_metadata;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 12:41:27 +02:00
|
|
|
KResult DevPtsFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)> callback) const
|
2019-08-16 18:46:18 +03:00
|
|
|
{
|
|
|
|
|
if (identifier().index() > 1)
|
2021-01-20 23:11:17 +01:00
|
|
|
return ENOTDIR;
|
2019-08-16 18:46:18 +03:00
|
|
|
|
2020-08-18 12:41:27 +02:00
|
|
|
callback({ ".", identifier(), 0 });
|
|
|
|
|
callback({ "..", identifier(), 0 });
|
2019-08-16 18:46:18 +03:00
|
|
|
|
2020-08-24 19:35:19 -06:00
|
|
|
for (unsigned pty_index : *s_ptys) {
|
2019-08-16 18:46:18 +03:00
|
|
|
String name = String::number(pty_index);
|
|
|
|
|
InodeIdentifier identifier = { fsid(), pty_index_to_inode_index(pty_index) };
|
2020-08-18 12:41:27 +02:00
|
|
|
callback({ name, identifier, 0 });
|
2019-08-16 18:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
2020-05-26 00:36:11 -07:00
|
|
|
return KSuccess;
|
2019-08-16 18:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
2020-08-05 01:00:18 -07:00
|
|
|
KResultOr<size_t> DevPtsFSInode::directory_entry_count() const
|
2019-08-16 18:46:18 +03:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(identifier().index() == 1);
|
2019-08-16 18:46:18 +03:00
|
|
|
|
2020-08-24 19:35:19 -06:00
|
|
|
return 2 + s_ptys->size();
|
2019-08-16 18:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
2020-02-01 09:23:46 +01:00
|
|
|
RefPtr<Inode> DevPtsFSInode::lookup(StringView name)
|
2019-08-16 18:46:18 +03:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(identifier().index() == 1);
|
2019-08-16 18:46:18 +03:00
|
|
|
|
|
|
|
|
if (name == "." || name == "..")
|
2020-06-24 23:35:56 +03:00
|
|
|
return this;
|
|
|
|
|
|
|
|
|
|
auto& fs = static_cast<DevPtsFS&>(this->fs());
|
2019-08-16 18:46:18 +03:00
|
|
|
|
2020-06-12 21:07:52 +02:00
|
|
|
auto pty_index = name.to_uint();
|
2020-08-24 19:35:19 -06:00
|
|
|
if (pty_index.has_value() && s_ptys->contains(pty_index.value())) {
|
2020-06-24 23:35:56 +03:00
|
|
|
return fs.get_inode({ fsid(), pty_index_to_inode_index(pty_index.value()) });
|
2019-08-16 18:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DevPtsFSInode::flush_metadata()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-24 23:35:56 +03:00
|
|
|
KResult DevPtsFSInode::add_child(Inode&, const StringView&, mode_t)
|
|
|
|
|
{
|
2021-01-20 23:11:17 +01:00
|
|
|
return EROFS;
|
2020-06-24 23:35:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
KResultOr<NonnullRefPtr<Inode>> DevPtsFSInode::create_child(const String&, mode_t, dev_t, uid_t, gid_t)
|
2019-08-16 18:46:18 +03:00
|
|
|
{
|
2021-01-20 23:11:17 +01:00
|
|
|
return EROFS;
|
2019-08-16 18:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
KResult DevPtsFSInode::remove_child(const StringView&)
|
|
|
|
|
{
|
2021-01-20 23:11:17 +01:00
|
|
|
return EROFS;
|
2019-08-16 18:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
KResult DevPtsFSInode::chmod(mode_t)
|
|
|
|
|
{
|
2021-04-30 03:39:39 +02:00
|
|
|
return EROFS;
|
2019-08-16 18:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
KResult DevPtsFSInode::chown(uid_t, gid_t)
|
|
|
|
|
{
|
2021-04-30 03:39:39 +02:00
|
|
|
return EROFS;
|
2019-01-30 00:49:20 +01:00
|
|
|
}
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
|
}
|