Kernel/LibC: Add SYS_SOCKET

This commit is contained in:
Bananymous 2024-02-01 23:39:09 +02:00
parent 99eed9c37a
commit cf28ecd5a6
8 changed files with 57 additions and 0 deletions

View File

@ -23,6 +23,8 @@ namespace Kernel
BAN::ErrorOr<int> open(BAN::StringView absolute_path, int flags); BAN::ErrorOr<int> open(BAN::StringView absolute_path, int flags);
BAN::ErrorOr<int> socket(int domain, int type, int protocol);
BAN::ErrorOr<void> pipe(int fds[2]); BAN::ErrorOr<void> pipe(int fds[2]);
BAN::ErrorOr<int> dup(int); BAN::ErrorOr<int> dup(int);

View File

@ -111,6 +111,8 @@ namespace Kernel
BAN::ErrorOr<long> sys_chmod(const char*, mode_t); BAN::ErrorOr<long> sys_chmod(const char*, mode_t);
BAN::ErrorOr<long> sys_chown(const char*, uid_t, gid_t); BAN::ErrorOr<long> sys_chown(const char*, uid_t, gid_t);
BAN::ErrorOr<long> sys_socket(int domain, int type, int protocol);
BAN::ErrorOr<long> sys_pipe(int fildes[2]); BAN::ErrorOr<long> sys_pipe(int fildes[2]);
BAN::ErrorOr<long> sys_dup(int fildes); BAN::ErrorOr<long> sys_dup(int fildes);
BAN::ErrorOr<long> sys_dup2(int fildes, int fildes2); BAN::ErrorOr<long> sys_dup2(int fildes, int fildes2);

View File

@ -1,8 +1,10 @@
#include <kernel/FS/Pipe.h> #include <kernel/FS/Pipe.h>
#include <kernel/FS/VirtualFileSystem.h> #include <kernel/FS/VirtualFileSystem.h>
#include <kernel/Networking/NetworkManager.h>
#include <kernel/OpenFileDescriptorSet.h> #include <kernel/OpenFileDescriptorSet.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/socket.h>
namespace Kernel namespace Kernel
{ {
@ -76,6 +78,38 @@ namespace Kernel
return fd; return fd;
} }
BAN::ErrorOr<int> OpenFileDescriptorSet::socket(int domain, int type, int protocol)
{
using SocketType = NetworkManager::SocketType;
if (domain != AF_INET)
return BAN::Error::from_errno(EAFNOSUPPORT);
if (protocol != 0)
return BAN::Error::from_errno(EPROTONOSUPPORT);
SocketType sock_type;
switch (type)
{
case SOCK_STREAM:
sock_type = SocketType::STREAM;
break;
case SOCK_DGRAM:
sock_type = SocketType::DGRAM;
break;
case SOCK_SEQPACKET:
sock_type = SocketType::SEQPACKET;
break;
default:
return BAN::Error::from_errno(EPROTOTYPE);
}
auto socket = TRY(NetworkManager::get().create_socket(sock_type, 0777, m_credentials.euid(), m_credentials.egid()));
int fd = TRY(get_free_fd());
m_open_files[fd] = TRY(BAN::RefPtr<OpenFileDescription>::create(socket, "no-path"sv, 0, O_RDWR));
return fd;
}
BAN::ErrorOr<void> OpenFileDescriptorSet::pipe(int fds[2]) BAN::ErrorOr<void> OpenFileDescriptorSet::pipe(int fds[2])
{ {
TRY(get_free_fd_pair(fds)); TRY(get_free_fd_pair(fds));

View File

@ -895,6 +895,12 @@ namespace Kernel
return 0; return 0;
} }
BAN::ErrorOr<long> Process::sys_socket(int domain, int type, int protocol)
{
LockGuard _(m_lock);
return TRY(m_open_file_descriptors.socket(domain, type, protocol));
}
BAN::ErrorOr<long> Process::sys_pipe(int fildes[2]) BAN::ErrorOr<long> Process::sys_pipe(int fildes[2])
{ {
LockGuard _(m_lock); LockGuard _(m_lock);

View File

@ -213,6 +213,9 @@ namespace Kernel
case SYS_LOAD_KEYMAP: case SYS_LOAD_KEYMAP:
ret = Process::current().sys_load_keymap((const char*)arg1); ret = Process::current().sys_load_keymap((const char*)arg1);
break; break;
case SYS_SOCKET:
ret = Process::current().sys_socket((int)arg1, (int)arg2, (int)arg3);
break;
default: default:
dwarnln("Unknown syscall {}", syscall); dwarnln("Unknown syscall {}", syscall);
break; break;

View File

@ -19,6 +19,7 @@ set(LIBC_SOURCES
strings.cpp strings.cpp
sys/banan-os.cpp sys/banan-os.cpp
sys/mman.cpp sys/mman.cpp
sys/socket.cpp
sys/stat.cpp sys/stat.cpp
sys/wait.cpp sys/wait.cpp
termios.cpp termios.cpp

View File

@ -63,6 +63,7 @@ __BEGIN_DECLS
#define SYS_PREAD 62 #define SYS_PREAD 62
#define SYS_CHOWN 63 #define SYS_CHOWN 63
#define SYS_LOAD_KEYMAP 64 #define SYS_LOAD_KEYMAP 64
#define SYS_SOCKET 65
__END_DECLS __END_DECLS

8
libc/sys/socket.cpp Normal file
View File

@ -0,0 +1,8 @@
#include <sys/socket.h>
#include <sys/syscall.h>
#include <unistd.h>
int socket(int domain, int type, int protocol)
{
return syscall(SYS_SOCKET, domain, type, protocol);
}