summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-05-03 20:42:43 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-05-03 20:42:43 +0200
commit2470fdcd9b75a0b2bf2ac025b7030a7f53c8ac89 (patch)
treedcc5a6952be81dffba3f6f152488f373d477bcd4
parent03da7046bda1c2ff67305a8d9eb178d69dac46f8 (diff)
downloadserenity-2470fdcd9b75a0b2bf2ac025b7030a7f53c8ac89.zip
Kernel: Make Socket inherit from File.
-rw-r--r--Kernel/Devices/Device.cpp2
-rw-r--r--Kernel/Devices/Device.h2
-rw-r--r--Kernel/File.h3
-rw-r--r--Kernel/FileSystem/FIFO.cpp2
-rw-r--r--Kernel/FileSystem/FIFO.h2
-rw-r--r--Kernel/FileSystem/FileDescriptor.cpp78
-rw-r--r--Kernel/FileSystem/FileDescriptor.h13
-rw-r--r--Kernel/Net/IPv4Socket.h1
-rw-r--r--Kernel/Net/LocalSocket.cpp4
-rw-r--r--Kernel/Net/Socket.cpp20
-rw-r--r--Kernel/Net/Socket.h15
-rw-r--r--Kernel/Net/TCPSocket.h1
-rw-r--r--Kernel/Net/UDPSocket.h1
-rw-r--r--Kernel/ProcessTracer.cpp2
-rw-r--r--Kernel/ProcessTracer.h2
-rw-r--r--Kernel/SharedMemory.cpp2
-rw-r--r--Kernel/SharedMemory.h2
-rw-r--r--Kernel/TTY/TTY.h2
18 files changed, 81 insertions, 73 deletions
diff --git a/Kernel/Devices/Device.cpp b/Kernel/Devices/Device.cpp
index 4fbf493ee5..ba7f28dadd 100644
--- a/Kernel/Devices/Device.cpp
+++ b/Kernel/Devices/Device.cpp
@@ -14,7 +14,7 @@ Device::~Device()
VFS::the().unregister_device(*this);
}
-String Device::absolute_path() const
+String Device::absolute_path(FileDescriptor&) const
{
return String::format("device:%u,%u (%s)", m_major, m_minor, class_name());
}
diff --git a/Kernel/Devices/Device.h b/Kernel/Devices/Device.h
index cc8d70066e..ce0b407aa7 100644
--- a/Kernel/Devices/Device.h
+++ b/Kernel/Devices/Device.h
@@ -43,7 +43,7 @@ public:
unsigned major() const { return m_major; }
unsigned minor() const { return m_minor; }
- virtual String absolute_path() const override;
+ virtual String absolute_path(FileDescriptor&) const override;
uid_t uid() const { return m_uid; }
uid_t gid() const { return m_gid; }
diff --git a/Kernel/File.h b/Kernel/File.h
index 60fbab1d41..7655448d4b 100644
--- a/Kernel/File.h
+++ b/Kernel/File.h
@@ -26,7 +26,7 @@ public:
virtual int ioctl(FileDescriptor&, unsigned request, unsigned arg);
virtual KResultOr<Region*> mmap(Process&, LinearAddress preferred_laddr, size_t offset, size_t size);
- virtual String absolute_path() const = 0;
+ virtual String absolute_path(FileDescriptor&) const = 0;
virtual const char* class_name() const = 0;
@@ -39,6 +39,7 @@ public:
virtual bool is_master_pty() const { return false; }
virtual bool is_block_device() const { return false; }
virtual bool is_character_device() const { return false; }
+ virtual bool is_socket() const { return false; }
protected:
File();
diff --git a/Kernel/FileSystem/FIFO.cpp b/Kernel/FileSystem/FIFO.cpp
index 2564fb9d4a..1140a911ab 100644
--- a/Kernel/FileSystem/FIFO.cpp
+++ b/Kernel/FileSystem/FIFO.cpp
@@ -115,7 +115,7 @@ ssize_t FIFO::write(FileDescriptor&, const byte* buffer, ssize_t size)
return m_buffer.write(buffer, size);
}
-String FIFO::absolute_path() const
+String FIFO::absolute_path(FileDescriptor&) const
{
return String::format("fifo:%u", this);
}
diff --git a/Kernel/FileSystem/FIFO.h b/Kernel/FileSystem/FIFO.h
index e10d6e6c22..ac1a0bd1c5 100644
--- a/Kernel/FileSystem/FIFO.h
+++ b/Kernel/FileSystem/FIFO.h
@@ -30,7 +30,7 @@ private:
virtual ssize_t read(FileDescriptor&, byte*, ssize_t) override;
virtual bool can_read(FileDescriptor&) const override;
virtual bool can_write(FileDescriptor&) const override;
- virtual String absolute_path() const override;
+ virtual String absolute_path(FileDescriptor&) const override;
virtual const char* class_name() const override { return "FIFO"; }
virtual bool is_fifo() const override { return true; }
diff --git a/Kernel/FileSystem/FileDescriptor.cpp b/Kernel/FileSystem/FileDescriptor.cpp
index 65e7ba2f6e..a51e149c5f 100644
--- a/Kernel/FileSystem/FileDescriptor.cpp
+++ b/Kernel/FileSystem/FileDescriptor.cpp
@@ -18,14 +18,9 @@ Retained<FileDescriptor> FileDescriptor::create(RetainPtr<Inode>&& inode)
return adopt(*new FileDescriptor(move(inode)));
}
-Retained<FileDescriptor> FileDescriptor::create(RetainPtr<File>&& file)
+Retained<FileDescriptor> FileDescriptor::create(RetainPtr<File>&& file, SocketRole role)
{
- return adopt(*new FileDescriptor(move(file)));
-}
-
-Retained<FileDescriptor> FileDescriptor::create(RetainPtr<Socket>&& socket, SocketRole role)
-{
- return adopt(*new FileDescriptor(move(socket), role));
+ return adopt(*new FileDescriptor(move(file), role));
}
FileDescriptor::FileDescriptor(RetainPtr<Inode>&& inode)
@@ -33,23 +28,16 @@ FileDescriptor::FileDescriptor(RetainPtr<Inode>&& inode)
{
}
-FileDescriptor::FileDescriptor(RetainPtr<File>&& file)
+FileDescriptor::FileDescriptor(RetainPtr<File>&& file, SocketRole role)
: m_file(move(file))
{
-}
-
-FileDescriptor::FileDescriptor(RetainPtr<Socket>&& socket, SocketRole role)
- : m_socket(move(socket))
-{
set_socket_role(role);
}
FileDescriptor::~FileDescriptor()
{
- if (m_socket) {
- m_socket->detach(*this);
- m_socket = nullptr;
- }
+ if (is_socket())
+ socket()->detach(*this);
if (is_fifo())
static_cast<FIFO*>(m_file.ptr())->detach(m_fifo_direction);
if (m_file) {
@@ -64,11 +52,11 @@ void FileDescriptor::set_socket_role(SocketRole role)
if (role == m_socket_role)
return;
- ASSERT(m_socket);
+ ASSERT(is_socket());
if (m_socket_role != SocketRole::None)
- m_socket->detach(*this);
+ socket()->detach(*this);
m_socket_role = role;
- m_socket->attach(*this);
+ socket()->attach(*this);
}
Retained<FileDescriptor> FileDescriptor::clone()
@@ -78,10 +66,7 @@ Retained<FileDescriptor> FileDescriptor::clone()
descriptor = fifo()->open_direction(m_fifo_direction);
} else {
if (m_file) {
- descriptor = FileDescriptor::create(m_file.copy_ref());
- descriptor->m_inode = m_inode.copy_ref();
- } else if (m_socket) {
- descriptor = FileDescriptor::create(m_socket.copy_ref(), m_socket_role);
+ descriptor = FileDescriptor::create(m_file.copy_ref(), m_socket_role);
descriptor->m_inode = m_inode.copy_ref();
} else {
descriptor = FileDescriptor::create(m_inode.copy_ref());
@@ -180,8 +165,6 @@ ssize_t FileDescriptor::read(byte* buffer, ssize_t count)
m_current_offset += nread;
return nread;
}
- if (m_socket)
- return m_socket->read(*this, buffer, count);
ASSERT(inode());
ssize_t nread = inode()->read_bytes(m_current_offset, count, buffer, this);
m_current_offset += nread;
@@ -196,8 +179,6 @@ ssize_t FileDescriptor::write(const byte* data, ssize_t size)
m_current_offset += nwritten;
return nwritten;
}
- if (m_socket)
- return m_socket->write(*this, data, size);
ASSERT(m_inode);
ssize_t nwritten = m_inode->write_bytes(m_current_offset, size, data, this);
m_current_offset += nwritten;
@@ -208,8 +189,6 @@ bool FileDescriptor::can_write()
{
if (m_file)
return m_file->can_write(*this);
- if (m_socket)
- return m_socket->can_write(*this);
return true;
}
@@ -217,8 +196,6 @@ bool FileDescriptor::can_read()
{
if (m_file)
return m_file->can_read(*this);
- if (m_socket)
- return m_socket->can_read(*this);
return true;
}
@@ -322,20 +299,6 @@ int FileDescriptor::close()
return 0;
}
-const char* to_string(SocketRole role)
-{
- switch (role) {
- case SocketRole::Listener:
- return "Listener";
- case SocketRole::Accepted:
- return "Accepted";
- case SocketRole::Connected:
- return "Connected";
- default:
- return "None";
- }
-}
-
bool FileDescriptor::is_fsfile() const
{
return !is_tty() && !is_fifo() && !is_device() && !is_socket() && !is_shared_memory();
@@ -344,9 +307,7 @@ bool FileDescriptor::is_fsfile() const
KResultOr<String> FileDescriptor::absolute_path()
{
if (m_file)
- return m_file->absolute_path();
- if (is_socket())
- return String::format("socket:%x (role: %s)", m_socket.ptr(), to_string(m_socket_role));
+ return m_file->absolute_path(*this);
ASSERT(m_inode);
return VFS::the().absolute_path(*m_inode);
}
@@ -428,3 +389,22 @@ FIFO* FileDescriptor::fifo()
return nullptr;
return static_cast<FIFO*>(m_file.ptr());
}
+
+bool FileDescriptor::is_socket() const
+{
+ return m_file && m_file->is_socket();
+}
+
+Socket* FileDescriptor::socket()
+{
+ if (!is_socket())
+ return nullptr;
+ return static_cast<Socket*>(m_file.ptr());
+}
+
+const Socket* FileDescriptor::socket() const
+{
+ if (!is_socket())
+ return nullptr;
+ return static_cast<const Socket*>(m_file.ptr());
+}
diff --git a/Kernel/FileSystem/FileDescriptor.h b/Kernel/FileSystem/FileDescriptor.h
index 7de49267f2..2572ac27e1 100644
--- a/Kernel/FileSystem/FileDescriptor.h
+++ b/Kernel/FileSystem/FileDescriptor.h
@@ -20,9 +20,8 @@ class SharedMemory;
class FileDescriptor : public Retainable<FileDescriptor> {
public:
- static Retained<FileDescriptor> create(RetainPtr<Socket>&&, SocketRole = SocketRole::None);
static Retained<FileDescriptor> create(RetainPtr<Inode>&&);
- static Retained<FileDescriptor> create(RetainPtr<File>&&);
+ static Retained<FileDescriptor> create(RetainPtr<File>&&, SocketRole = SocketRole::None);
~FileDescriptor();
Retained<FileDescriptor> clone();
@@ -74,9 +73,9 @@ public:
dword file_flags() const { return m_file_flags; }
void set_file_flags(dword flags) { m_file_flags = flags; }
- bool is_socket() const { return m_socket; }
- Socket* socket() { return m_socket.ptr(); }
- const Socket* socket() const { return m_socket.ptr(); }
+ bool is_socket() const;
+ Socket* socket();
+ const Socket* socket() const;
bool is_fifo() const;
FIFO* fifo();
@@ -99,9 +98,8 @@ public:
private:
friend class VFS;
- FileDescriptor(RetainPtr<Socket>&&, SocketRole);
+ FileDescriptor(RetainPtr<File>&&, SocketRole);
explicit FileDescriptor(RetainPtr<Inode>&&);
- explicit FileDescriptor(RetainPtr<File>&&);
FileDescriptor(FIFO&, FIFO::Direction);
RetainPtr<Inode> m_inode;
@@ -114,7 +112,6 @@ private:
bool m_is_blocking { true };
dword m_file_flags { 0 };
- RetainPtr<Socket> m_socket;
SocketRole m_socket_role { SocketRole::None };
FIFO::Direction m_fifo_direction { FIFO::Neither };
diff --git a/Kernel/Net/IPv4Socket.h b/Kernel/Net/IPv4Socket.h
index fdd119d9ad..e31064740d 100644
--- a/Kernel/Net/IPv4Socket.h
+++ b/Kernel/Net/IPv4Socket.h
@@ -44,6 +44,7 @@ public:
protected:
IPv4Socket(int type, int protocol);
+ virtual const char* class_name() const override { return "IPv4Socket"; }
int allocate_source_port_if_needed();
diff --git a/Kernel/Net/LocalSocket.cpp b/Kernel/Net/LocalSocket.cpp
index c37dc72382..28ab02439f 100644
--- a/Kernel/Net/LocalSocket.cpp
+++ b/Kernel/Net/LocalSocket.cpp
@@ -113,6 +113,8 @@ void LocalSocket::attach(FileDescriptor& descriptor)
case SocketRole::Connecting:
++m_connecting_fds_open;
break;
+ default:
+ break;
}
}
@@ -131,6 +133,8 @@ void LocalSocket::detach(FileDescriptor& descriptor)
ASSERT(m_connecting_fds_open);
--m_connecting_fds_open;
break;
+ default:
+ break;
}
}
diff --git a/Kernel/Net/Socket.cpp b/Kernel/Net/Socket.cpp
index 7fef7bde1d..c01295d7a6 100644
--- a/Kernel/Net/Socket.cpp
+++ b/Kernel/Net/Socket.cpp
@@ -1,3 +1,4 @@
+#include <Kernel/FileSystem/FileDescriptor.h>
#include <Kernel/Net/Socket.h>
#include <Kernel/Net/LocalSocket.h>
#include <Kernel/Net/IPv4Socket.h>
@@ -119,3 +120,22 @@ void Socket::load_send_deadline()
m_send_deadline.tv_sec += (m_send_timeout.tv_usec / 1000000) * 1;
m_send_deadline.tv_usec %= 1000000;
}
+
+static const char* to_string(SocketRole role)
+{
+ switch (role) {
+ case SocketRole::Listener:
+ return "Listener";
+ case SocketRole::Accepted:
+ return "Accepted";
+ case SocketRole::Connected:
+ return "Connected";
+ default:
+ return "None";
+ }
+}
+
+String Socket::absolute_path(FileDescriptor& descriptor) const
+{
+ return String::format("socket:%x (role: %s)", this, to_string(descriptor.socket_role()));
+}
diff --git a/Kernel/Net/Socket.h b/Kernel/Net/Socket.h
index eebf9b3b77..ca5d0f32e9 100644
--- a/Kernel/Net/Socket.h
+++ b/Kernel/Net/Socket.h
@@ -5,6 +5,7 @@
#include <AK/RetainPtr.h>
#include <AK/HashTable.h>
#include <AK/Vector.h>
+#include <Kernel/File.h>
#include <Kernel/UnixTypes.h>
#include <Kernel/KResult.h>
@@ -13,10 +14,10 @@ enum class ShouldBlock { No = 0, Yes = 1 };
class FileDescriptor;
-class Socket : public Retainable<Socket> {
+class Socket : public File {
public:
static KResultOr<Retained<Socket>> create(int domain, int type, int protocol);
- virtual ~Socket();
+ virtual ~Socket() override;
int domain() const { return m_domain; }
int type() const { return m_type; }
@@ -34,10 +35,6 @@ public:
virtual bool is_ipv4() const { return false; }
virtual void attach(FileDescriptor&) = 0;
virtual void detach(FileDescriptor&) = 0;
- virtual bool can_read(FileDescriptor&) const = 0;
- virtual ssize_t read(FileDescriptor&, byte*, ssize_t) = 0;
- virtual ssize_t write(FileDescriptor&, const byte*, ssize_t) = 0;
- virtual bool can_write(FileDescriptor&) const = 0;
virtual ssize_t sendto(FileDescriptor&, const void*, size_t, int flags, const sockaddr*, socklen_t) = 0;
virtual ssize_t recvfrom(FileDescriptor&, void*, size_t, int flags, sockaddr*, socklen_t*) = 0;
@@ -53,6 +50,8 @@ public:
Lock& lock() { return m_lock; }
+ virtual String absolute_path(FileDescriptor&) const override;
+
protected:
Socket(int domain, int type, int protocol);
@@ -61,7 +60,11 @@ protected:
void load_receive_deadline();
void load_send_deadline();
+ virtual const char* class_name() const override { return "Socket"; }
+
private:
+ virtual bool is_socket() const final { return true; }
+
Lock m_lock { "Socket" };
pid_t m_origin_pid { 0 };
int m_domain { 0 };
diff --git a/Kernel/Net/TCPSocket.h b/Kernel/Net/TCPSocket.h
index bf4f2a24f5..186c3be078 100644
--- a/Kernel/Net/TCPSocket.h
+++ b/Kernel/Net/TCPSocket.h
@@ -29,6 +29,7 @@ public:
private:
explicit TCPSocket(int protocol);
+ virtual const char* class_name() const override { return "TCPSocket"; }
NetworkOrdered<word> compute_tcp_checksum(const IPv4Address& source, const IPv4Address& destination, const TCPPacket&, word payload_size);
diff --git a/Kernel/Net/UDPSocket.h b/Kernel/Net/UDPSocket.h
index f22ff900f1..41506d1df0 100644
--- a/Kernel/Net/UDPSocket.h
+++ b/Kernel/Net/UDPSocket.h
@@ -14,6 +14,7 @@ public:
private:
explicit UDPSocket(int protocol);
+ virtual const char* class_name() const override { return "UDPSocket"; }
virtual int protocol_receive(const ByteBuffer&, void* buffer, size_t buffer_size, int flags, sockaddr* addr, socklen_t* addr_length) override;
virtual int protocol_send(const void*, int) override;
diff --git a/Kernel/ProcessTracer.cpp b/Kernel/ProcessTracer.cpp
index 5805a84f70..18493f54be 100644
--- a/Kernel/ProcessTracer.cpp
+++ b/Kernel/ProcessTracer.cpp
@@ -29,7 +29,7 @@ int ProcessTracer::read(FileDescriptor&, byte* buffer, int buffer_size)
return sizeof(data);
}
-String ProcessTracer::absolute_path() const
+String ProcessTracer::absolute_path(FileDescriptor&) const
{
return String::format("tracer:%d", m_pid);
}
diff --git a/Kernel/ProcessTracer.h b/Kernel/ProcessTracer.h
index 8cc043d8fe..9db8aa60a4 100644
--- a/Kernel/ProcessTracer.h
+++ b/Kernel/ProcessTracer.h
@@ -18,7 +18,7 @@ public:
virtual bool can_write(FileDescriptor&) const override { return true; }
virtual int write(FileDescriptor&, const byte*, int) override { return -EIO; }
- virtual String absolute_path() const override;
+ virtual String absolute_path(FileDescriptor&) const override;
void did_syscall(dword function, dword arg1, dword arg2, dword arg3, dword result);
pid_t pid() const { return m_pid; }
diff --git a/Kernel/SharedMemory.cpp b/Kernel/SharedMemory.cpp
index 72472f3073..d489891512 100644
--- a/Kernel/SharedMemory.cpp
+++ b/Kernel/SharedMemory.cpp
@@ -68,7 +68,7 @@ KResult SharedMemory::truncate(int length)
return KResult(-ENOTIMPL);
}
-String SharedMemory::absolute_path() const
+String SharedMemory::absolute_path(FileDescriptor&) const
{
return String::format("shm:%u", this);
}
diff --git a/Kernel/SharedMemory.h b/Kernel/SharedMemory.h
index 0641519503..402d2acccb 100644
--- a/Kernel/SharedMemory.h
+++ b/Kernel/SharedMemory.h
@@ -28,7 +28,7 @@ private:
virtual bool can_write(FileDescriptor&) const override { return true; }
virtual int read(FileDescriptor&, byte*, int) override;
virtual int write(FileDescriptor&, const byte*, int) override;
- virtual String absolute_path() const override;
+ virtual String absolute_path(FileDescriptor&) const override;
virtual const char* class_name() const override { return "SharedMemory"; }
virtual bool is_shared_memory() const override { return true; }
diff --git a/Kernel/TTY/TTY.h b/Kernel/TTY/TTY.h
index ffda96f967..56fbcb3c6f 100644
--- a/Kernel/TTY/TTY.h
+++ b/Kernel/TTY/TTY.h
@@ -15,7 +15,7 @@ public:
virtual bool can_read(FileDescriptor&) const override;
virtual bool can_write(FileDescriptor&) const override;
virtual int ioctl(FileDescriptor&, unsigned request, unsigned arg) override final;
- virtual String absolute_path() const override { return tty_name(); }
+ virtual String absolute_path(FileDescriptor&) const override { return tty_name(); }
virtual String tty_name() const = 0;