Browse Source

Kernel: Add a way to print the pseudo name of attached TTY of a process

Contrary to the past, we don't attempt to assume the real name of a TTY
device, but instead, we generate a pseudo name only when needed to do so
which is still OK because we don't break abstraction layer rules and we
still can provide userspace with the required information.
Liav A 3 years ago
parent
commit
7053d7ece3

+ 6 - 0
Kernel/GlobalProcessExposed.cpp

@@ -495,6 +495,12 @@ private:
             TRY(process_object.add("uid", process.uid().value()));
             TRY(process_object.add("gid", process.gid().value()));
             TRY(process_object.add("ppid", process.ppid().value()));
+            if (process.tty()) {
+                auto tty_pseudo_name = TRY(process.tty()->pseudo_name());
+                TRY(process_object.add("tty", tty_pseudo_name->view()));
+            } else {
+                TRY(process_object.add("tty", ""));
+            }
             TRY(process_object.add("nfds", process.fds().with_shared([](auto& fds) { return fds.open_count(); })));
             TRY(process_object.add("name", process.name()));
             TRY(process_object.add("executable", process.executable() ? TRY(process.executable()->try_serialize_absolute_path())->view() : ""sv));

+ 5 - 0
Kernel/TTY/SlavePTY.cpp

@@ -54,6 +54,11 @@ SlavePTY::~SlavePTY()
     dbgln_if(SLAVEPTY_DEBUG, "~SlavePTY({})", m_index);
 }
 
+ErrorOr<NonnullOwnPtr<KString>> SlavePTY::pseudo_name() const
+{
+    return KString::formatted("pts:{}", m_index);
+}
+
 void SlavePTY::echo(u8 ch)
 {
     if (should_echo_input()) {

+ 1 - 0
Kernel/TTY/SlavePTY.h

@@ -27,6 +27,7 @@ public:
 
 private:
     // ^TTY
+    virtual ErrorOr<NonnullOwnPtr<KString>> pseudo_name() const override;
     virtual ErrorOr<size_t> on_tty_write(const UserOrKernelBuffer&, size_t) override;
     virtual void echo(u8) override;
 

+ 2 - 0
Kernel/TTY/TTY.h

@@ -46,6 +46,8 @@ public:
     void set_default_termios();
     void hang_up();
 
+    virtual ErrorOr<NonnullOwnPtr<KString>> pseudo_name() const = 0;
+
 protected:
     virtual ErrorOr<size_t> on_tty_write(const UserOrKernelBuffer&, size_t) = 0;
     void set_size(unsigned short columns, unsigned short rows);

+ 5 - 0
Kernel/TTY/VirtualConsole.cpp

@@ -102,6 +102,11 @@ void VirtualConsole::set_graphical(bool graphical)
     m_graphical = graphical;
 }
 
+ErrorOr<NonnullOwnPtr<KString>> VirtualConsole::pseudo_name() const
+{
+    return KString::formatted("tty:{}", m_index);
+}
+
 UNMAP_AFTER_INIT NonnullRefPtr<VirtualConsole> VirtualConsole::create(size_t index)
 {
     auto virtual_console_or_error = DeviceManagement::try_create_device<VirtualConsole>(index);

+ 1 - 0
Kernel/TTY/VirtualConsole.h

@@ -89,6 +89,7 @@ private:
     virtual void on_key_pressed(KeyEvent) override;
 
     // ^TTY
+    virtual ErrorOr<NonnullOwnPtr<KString>> pseudo_name() const override;
     virtual ErrorOr<size_t> on_tty_write(const UserOrKernelBuffer&, size_t) override;
     virtual void echo(u8) override;