Kernel: Begin implementing UNIX domain sockets.

This commit is contained in:
Andreas Kling 2019-02-14 14:17:38 +01:00
parent dc200923f2
commit 2f35e54f80
Notes: sideshowbarker 2024-07-19 15:43:17 +09:00
12 changed files with 177 additions and 1 deletions

View file

@ -7,6 +7,7 @@
#include "FIFO.h"
#include "TTY.h"
#include "MasterPTY.h"
#include <Kernel/Socket.h>
RetainPtr<FileDescriptor> FileDescriptor::create(RetainPtr<Inode>&& inode)
{
@ -18,6 +19,11 @@ RetainPtr<FileDescriptor> FileDescriptor::create(RetainPtr<CharacterDevice>&& de
return adopt(*new FileDescriptor(move(device)));
}
RetainPtr<FileDescriptor> FileDescriptor::create(RetainPtr<Socket>&& socket)
{
return adopt(*new FileDescriptor(move(socket)));
}
RetainPtr<FileDescriptor> FileDescriptor::create_pipe_writer(FIFO& fifo)
{
return adopt(*new FileDescriptor(fifo, FIFO::Writer));
@ -38,6 +44,11 @@ FileDescriptor::FileDescriptor(RetainPtr<CharacterDevice>&& device)
{
}
FileDescriptor::FileDescriptor(RetainPtr<Socket>&& socket)
: m_socket(move(socket))
{
}
FileDescriptor::~FileDescriptor()
{
if (m_device) {

View file

@ -11,9 +11,11 @@
class TTY;
class MasterPTY;
class Process;
class Socket;
class FileDescriptor : public Retainable<FileDescriptor> {
public:
static RetainPtr<FileDescriptor> create(RetainPtr<Socket>&&);
static RetainPtr<FileDescriptor> create(RetainPtr<Inode>&&);
static RetainPtr<FileDescriptor> create(RetainPtr<CharacterDevice>&&);
static RetainPtr<FileDescriptor> create_pipe_writer(FIFO&);
@ -64,6 +66,10 @@ 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_fifo() const { return m_fifo; }
FIFO::Direction fifo_direction() { return m_fifo_direction; }
@ -73,6 +79,7 @@ public:
private:
friend class VFS;
explicit FileDescriptor(RetainPtr<Socket>&&);
explicit FileDescriptor(RetainPtr<Inode>&&);
explicit FileDescriptor(RetainPtr<CharacterDevice>&&);
FileDescriptor(FIFO&, FIFO::Direction);
@ -87,6 +94,8 @@ private:
bool m_is_blocking { true };
dword m_file_flags { 0 };
RetainPtr<Socket> m_socket;
RetainPtr<FIFO> m_fifo;
FIFO::Direction m_fifo_direction { FIFO::Neither };

19
Kernel/LocalSocket.cpp Normal file
View file

@ -0,0 +1,19 @@
#include <Kernel/LocalSocket.h>
#include <Kernel/UnixTypes.h>
#include <Kernel/Process.h>
RetainPtr<LocalSocket> LocalSocket::create(int type)
{
return adopt(*new LocalSocket(type));
}
LocalSocket::LocalSocket(int type)
: Socket(AF_LOCAL, type, 0)
{
kprintf("%s(%u) LocalSocket{%p} created with type=%u\n", current->name().characters(), current->pid(), type);
}
LocalSocket::~LocalSocket()
{
}

17
Kernel/LocalSocket.h Normal file
View file

@ -0,0 +1,17 @@
#pragma once
#include <Kernel/Socket.h>
#include <Kernel/DoubleBuffer.h>
class LocalSocket final : public Socket {
public:
static RetainPtr<LocalSocket> create(int type);
virtual ~LocalSocket() override;
private:
explicit LocalSocket(int type);
DoubleBuffer m_for_client;
DoubleBuffer m_for_server;
};

View file

@ -32,7 +32,9 @@ KERNEL_OBJS = \
BochsVGADevice.o \
PCI.o \
PS2MouseDevice.o \
GUIEventDevice.o
GUIEventDevice.o \
Socket.o \
LocalSocket.o
VFS_OBJS = \
DiskDevice.o \

View file

@ -20,6 +20,7 @@
#include "KSyms.h"
#include <WindowServer/WSMessageLoop.h>
#include <Kernel/BochsVGADevice.h>
#include <Kernel/Socket.h>
#include "MasterPTY.h"
#include "elf.h"
@ -2242,3 +2243,41 @@ DisplayInfo Process::set_video_resolution(int width, int height)
BochsVGADevice::the().set_resolution(width, height);
return info;
}
int Process::sys$socket(int domain, int type, int protocol)
{
if (number_of_open_file_descriptors() >= m_max_open_file_descriptors)
return -EMFILE;
int fd = 0;
for (; fd < (int)m_max_open_file_descriptors; ++fd) {
if (!m_fds[fd])
break;
}
int error;
auto socket = Socket::create(domain, type, protocol, error);
if (!socket)
return error;
auto descriptor = FileDescriptor::create(move(socket));
m_fds[fd].set(move(descriptor));
return fd;
}
int Process::sys$bind(int sockfd, const sockaddr* addr, socklen_t)
{
return -ENOTIMPL;
}
int Process::sys$listen(int sockfd, int backlog)
{
return -ENOTIMPL;
}
int Process::sys$accept(int sockfd, sockaddr*, socklen_t)
{
return -ENOTIMPL;
}
int Process::sys$connect(int sockfd, const sockaddr*, socklen_t)
{
return -ENOTIMPL;
}

View file

@ -218,6 +218,11 @@ public:
int sys$rmdir(const char* pathname);
int sys$read_tsc(dword* lsw, dword* msw);
int sys$chmod(const char* pathname, mode_t);
int sys$socket(int domain, int type, int protocol);
int sys$bind(int sockfd, const sockaddr* addr, socklen_t);
int sys$listen(int sockfd, int backlog);
int sys$accept(int sockfd, sockaddr*, socklen_t);
int sys$connect(int sockfd, const sockaddr*, socklen_t);
DisplayInfo set_video_resolution(int width, int height);

29
Kernel/Socket.cpp Normal file
View file

@ -0,0 +1,29 @@
#include <Kernel/Socket.h>
#include <Kernel/LocalSocket.h>
#include <Kernel/UnixTypes.h>
#include <LibC/errno_numbers.h>
RetainPtr<Socket> Socket::create(int domain, int type, int protocol, int& error)
{
(void)protocol;
switch (domain) {
case AF_LOCAL:
return LocalSocket::create(type);
default:
error = EAFNOSUPPORT;
return nullptr;
}
}
Socket::Socket(int domain, int type, int protocol)
: m_domain(domain)
, m_type(type)
, m_protocol(protocol)
{
}
Socket::~Socket()
{
}

23
Kernel/Socket.h Normal file
View file

@ -0,0 +1,23 @@
#pragma once
#include <AK/Retainable.h>
#include <AK/RetainPtr.h>
class Socket : public Retainable<Socket> {
public:
static RetainPtr<Socket> create(int domain, int type, int protocol, int& error);
virtual ~Socket();
int domain() const { return m_domain; }
int type() const { return m_type; }
int protocol() const { return m_protocol; }
protected:
Socket(int domain, int type, int protocol);
private:
int m_domain { 0 };
int m_type { 0 };
int m_protocol { 0 };
};

View file

@ -74,6 +74,11 @@
__ENUMERATE_SYSCALL(rmdir) \
__ENUMERATE_SYSCALL(chmod) \
__ENUMERATE_SYSCALL(usleep) \
__ENUMERATE_SYSCALL(socket) \
__ENUMERATE_SYSCALL(bind) \
__ENUMERATE_SYSCALL(accept) \
__ENUMERATE_SYSCALL(listen) \
__ENUMERATE_SYSCALL(connect) \
#ifdef SERENITY

View file

@ -219,6 +219,7 @@ typedef dword nlink_t;
typedef dword uid_t;
typedef dword gid_t;
typedef dword clock_t;
typedef dword socklen_t;
struct tms {
clock_t tms_utime;
@ -304,3 +305,18 @@ struct pollfd {
short events;
short revents;
};
#define AF_UNSPEC 0
#define AF_LOCAL 1
struct sockaddr {
word sa_family;
char sa_data[14];
};
#define UNIX_PATH_MAX 108
struct sockaddr_un {
word sun_family;
char sun_path[UNIX_PATH_MAX];
};

View file

@ -12,6 +12,7 @@ __BEGIN_DECLS
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
#define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
mode_t umask(mode_t);
int chmod(const char* pathname, mode_t);