Kernel: Move TTY subsystem to use KString instead of AK::String

This is minor progress on removing the `AK::String` API from the Kernel
in the interest of improving OOM safety.
This commit is contained in:
Brian Gianforcaro 2021-10-31 22:47:19 -07:00 committed by Andreas Kling
parent 71f05c70b4
commit 9f6eabd73a
Notes: sideshowbarker 2024-07-18 01:37:19 +09:00
10 changed files with 36 additions and 27 deletions

View File

@ -497,7 +497,7 @@ private:
process_object.add("nfds", process.fds().open_count());
process_object.add("name", process.name());
process_object.add("executable", process.executable() ? process.executable()->absolute_path() : "");
process_object.add("tty", process.tty() ? process.tty()->tty_name() : "notty");
process_object.add("tty", process.tty() ? process.tty()->tty_name().view() : "notty"sv);
process_object.add("amount_virtual", process.address_space().amount_virtual());
process_object.add("amount_resident", process.address_space().amount_resident());
process_object.add("amount_dirty_private", process.address_space().amount_dirty_private());

View File

@ -18,7 +18,7 @@ KResultOr<FlatPtr> Process::sys$ttyname(int fd, Userspace<char*> buffer, size_t
auto description = TRY(fds().open_file_description(fd));
if (!description->is_tty())
return ENOTTY;
auto tty_name = description->tty()->tty_name();
auto& tty_name = description->tty()->tty_name();
if (size < tty_name.length() + 1)
return ERANGE;
return copy_to_user(buffer, tty_name.characters(), tty_name.length() + 1);
@ -32,7 +32,7 @@ KResultOr<FlatPtr> Process::sys$ptsname(int fd, Userspace<char*> buffer, size_t
auto* master_pty = description->master_pty();
if (!master_pty)
return ENOTTY;
auto pts_name = master_pty->pts_name();
auto& pts_name = master_pty->pts_name();
if (size < pts_name.length() + 1)
return ERANGE;
return copy_to_user(buffer, pts_name.characters(), pts_name.length() + 1);

View File

@ -18,21 +18,25 @@ namespace Kernel {
KResultOr<NonnullRefPtr<MasterPTY>> MasterPTY::try_create(unsigned int index)
{
// FIXME: Don't make a temporary String here
auto pts_name = TRY(KString::try_create(String::formatted("/dev/pts/{}", index)));
auto tty_name = TRY(pts_name->try_clone());
auto buffer = TRY(DoubleBuffer::try_create());
auto master_pty = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) MasterPTY(index, move(buffer))));
auto slave_pty = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) SlavePTY(*master_pty, index)));
auto master_pty = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) MasterPTY(index, move(buffer), move(pts_name))));
auto slave_pty = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) SlavePTY(*master_pty, index, move(tty_name))));
master_pty->m_slave = slave_pty;
master_pty->after_inserting();
slave_pty->after_inserting();
return master_pty;
}
MasterPTY::MasterPTY(unsigned index, NonnullOwnPtr<DoubleBuffer> buffer)
MasterPTY::MasterPTY(unsigned index, NonnullOwnPtr<DoubleBuffer> buffer, NonnullOwnPtr<KString> pts_name)
: CharacterDevice(200, index)
, m_index(index)
, m_buffer(move(buffer))
, m_pts_name(move(pts_name))
{
m_pts_name = String::formatted("/dev/pts/{}", m_index);
auto& process = Process::current();
set_uid(process.uid());
set_gid(process.gid());
@ -49,9 +53,9 @@ MasterPTY::~MasterPTY()
PTYMultiplexer::the().notify_master_destroyed({}, m_index);
}
String MasterPTY::pts_name() const
KString const& MasterPTY::pts_name() const
{
return m_pts_name;
return *m_pts_name;
}
KResultOr<size_t> MasterPTY::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size)

View File

@ -20,7 +20,7 @@ public:
virtual ~MasterPTY() override;
unsigned index() const { return m_index; }
String pts_name() const;
KString const& pts_name() const;
KResultOr<size_t> on_slave_write(const UserOrKernelBuffer&, size_t);
bool can_write_from_slave() const;
void notify_slave_closed(Badge<SlavePTY>);
@ -29,7 +29,7 @@ public:
virtual KResultOr<NonnullOwnPtr<KString>> pseudo_path(const OpenFileDescription&) const override;
private:
explicit MasterPTY(unsigned index, NonnullOwnPtr<DoubleBuffer> buffer);
explicit MasterPTY(unsigned index, NonnullOwnPtr<DoubleBuffer> buffer, NonnullOwnPtr<KString> pts_name);
// ^CharacterDevice
virtual KResultOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override;
virtual KResultOr<size_t> write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override;
@ -44,7 +44,7 @@ private:
unsigned m_index;
bool m_closed { false };
NonnullOwnPtr<DoubleBuffer> m_buffer;
String m_pts_name;
NonnullOwnPtr<KString> m_pts_name;
};
}

View File

@ -35,12 +35,12 @@ bool SlavePTY::unref() const
return did_hit_zero;
}
SlavePTY::SlavePTY(MasterPTY& master, unsigned index)
SlavePTY::SlavePTY(MasterPTY& master, unsigned index, NonnullOwnPtr<KString> tty_name)
: TTY(201, index)
, m_master(master)
, m_index(index)
, m_tty_name(move(tty_name))
{
m_tty_name = String::formatted("/dev/pts/{}", m_index);
auto& process = Process::current();
set_uid(process.uid());
set_gid(process.gid());
@ -54,9 +54,9 @@ SlavePTY::~SlavePTY()
dbgln_if(SLAVEPTY_DEBUG, "~SlavePTY({})", m_index);
}
String const& SlavePTY::tty_name() const
KString const& SlavePTY::tty_name() const
{
return m_tty_name;
return *m_tty_name;
}
void SlavePTY::echo(u8 ch)

View File

@ -27,7 +27,7 @@ public:
private:
// ^TTY
virtual String const& tty_name() const override;
virtual KString const& tty_name() const override;
virtual KResultOr<size_t> on_tty_write(const UserOrKernelBuffer&, size_t) override;
virtual void echo(u8) override;
@ -39,12 +39,12 @@ private:
virtual KResult close() override;
friend class MasterPTY;
SlavePTY(MasterPTY&, unsigned index);
SlavePTY(MasterPTY&, unsigned index, NonnullOwnPtr<KString> pts_name);
RefPtr<MasterPTY> m_master;
time_t m_time_of_last_write { 0 };
unsigned m_index { 0 };
String m_tty_name;
NonnullOwnPtr<KString> m_tty_name;
mutable IntrusiveListNode<SlavePTY> m_list_node;

View File

@ -578,7 +578,7 @@ KResult TTY::ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg)
KResultOr<NonnullOwnPtr<KString>> TTY::pseudo_path(const OpenFileDescription&) const
{
return KString::try_create(tty_name());
return tty_name().try_clone();
}
void TTY::set_size(unsigned short columns, unsigned short rows)

View File

@ -28,7 +28,7 @@ public:
virtual KResult ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg) override final;
virtual KResultOr<NonnullOwnPtr<KString>> pseudo_path(const OpenFileDescription&) const override;
virtual String const& tty_name() const = 0;
virtual KString const& tty_name() const = 0;
unsigned short rows() const { return m_rows; }
unsigned short columns() const { return m_columns; }

View File

@ -103,7 +103,12 @@ void VirtualConsole::set_graphical(bool graphical)
UNMAP_AFTER_INIT NonnullRefPtr<VirtualConsole> VirtualConsole::create(size_t index)
{
auto virtual_console_or_error = DeviceManagement::try_create_device<VirtualConsole>(index);
// FIXME: Don't make a temporary String here
auto pts_name_or_error = KString::try_create(String::formatted("/dev/tty/{}", index));
VERIFY(!pts_name_or_error.is_error());
auto pts_name = pts_name_or_error.release_value();
auto virtual_console_or_error = DeviceManagement::try_create_device<VirtualConsole>(index, move(pts_name));
// FIXME: Find a way to propagate errors
VERIFY(!virtual_console_or_error.is_error());
return virtual_console_or_error.release_value();
@ -123,7 +128,6 @@ UNMAP_AFTER_INIT NonnullRefPtr<VirtualConsole> VirtualConsole::create_with_prese
UNMAP_AFTER_INIT void VirtualConsole::initialize()
{
m_tty_name = String::formatted("/dev/tty{}", m_index);
VERIFY(GraphicsManagement::the().console());
set_size(GraphicsManagement::the().console()->max_column(), GraphicsManagement::the().console()->max_row());
m_console_impl.set_size(GraphicsManagement::the().console()->max_column(), GraphicsManagement::the().console()->max_row());
@ -174,9 +178,10 @@ void VirtualConsole::refresh_after_resolution_change()
flush_dirty_lines();
}
UNMAP_AFTER_INIT VirtualConsole::VirtualConsole(const unsigned index)
UNMAP_AFTER_INIT VirtualConsole::VirtualConsole(const unsigned index, NonnullOwnPtr<KString> tty_name)
: TTY(4, index)
, m_index(index)
, m_tty_name(move(tty_name))
, m_console_impl(*this)
{
initialize();

View File

@ -86,13 +86,13 @@ public:
void emit_char(char);
private:
explicit VirtualConsole(const unsigned index);
explicit VirtualConsole(const unsigned index, NonnullOwnPtr<KString> tty_name);
// ^KeyboardClient
virtual void on_key_pressed(KeyEvent) override;
// ^TTY
virtual KResultOr<size_t> on_tty_write(const UserOrKernelBuffer&, size_t) override;
virtual String const& tty_name() const override { return m_tty_name; }
virtual KString const& tty_name() const override { return *m_tty_name; }
virtual void echo(u8) override;
// ^TerminalClient
@ -114,7 +114,7 @@ private:
bool m_active { false };
bool m_graphical { false };
String m_tty_name;
NonnullOwnPtr<KString> m_tty_name;
private:
void initialize();