Kernel/LibC: Implement basic epoll

This implementation is on top of inodes instead of fds as linux does it.
If I start finding ports/software that relies on epoll allowing
duplicate inodes, I will do what linux does.

I'm probably missing multiple epoll_notify's which may cause hangs but
the system seems to work fine :dd:
This commit is contained in:
2025-05-13 10:10:35 +03:00
parent 143a00626b
commit 1bcd1edbf5
43 changed files with 627 additions and 119 deletions

View File

@@ -30,6 +30,7 @@ set(LIBC_SOURCES
string.cpp
strings.cpp
sys/banan-os.cpp
sys/epoll.cpp
sys/ioctl.cpp
sys/mman.cpp
sys/resource.cpp

View File

@@ -0,0 +1,49 @@
#ifndef _SYS_EPOLL_H
#define _SYS_EPOLL_H 1
#include <sys/cdefs.h>
__BEGIN_DECLS
#include <stdint.h>
#include <signal.h>
union epoll_data
{
void* ptr;
int fd;
uint32_t u32;
uint64_t u64;
};
typedef union epoll_data epoll_data_t;
struct epoll_event
{
uint32_t events;
epoll_data_t data;
};
#define EPOLL_CTL_ADD 0
#define EPOLL_CTL_MOD 1
#define EPOLL_CTL_DEL 2
#define EPOLLIN 0x01
#define EPOLLOUT 0x02
#define EPOLLPRI 0x04
#define EPOLLERR 0x08
#define EPOLLHUP 0x10
#define EPOLLET 0x20
#define EPOLLONESHOT 0x40
#define EPOLL_CLOEXEC 1
int epoll_create(int size);
int epoll_create1(int flags);
int epoll_ctl(int epfd, int op, int fd, struct epoll_event* event);
int epoll_wait(int epfd, struct epoll_event* events, int maxevents, int timeout);
int epoll_pwait(int epfd, struct epoll_event* events, int maxevents, int timeout, const sigset_t* sigmask);
int epoll_pwait2(int epfd, struct epoll_event* events, int maxevents, const struct timespec* timeout, const sigset_t* sigmask);
__END_DECLS
#endif

View File

@@ -98,6 +98,9 @@ __BEGIN_DECLS
O(SYS_PTHREAD_EXIT, pthread_exit) \
O(SYS_PTHREAD_JOIN, pthread_join) \
O(SYS_PTHREAD_SELF, pthread_self) \
O(SYS_EPOLL_CREATE1, epoll_create1) \
O(SYS_EPOLL_CTL, epoll_ctl) \
O(SYS_EPOLL_PWAIT2, epoll_pwait2) \
enum Syscall
{

View File

@@ -0,0 +1,49 @@
#include <errno.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/syscall.h>
int epoll_create(int size)
{
if (size <= 0)
{
errno = EINVAL;
return -1;
}
return epoll_create1(0);
}
int epoll_create1(int flags)
{
return syscall(SYS_EPOLL_CREATE1, flags);
}
int epoll_ctl(int epfd, int op, int fd, struct epoll_event* event)
{
return syscall(SYS_EPOLL_CTL, epfd, op, fd, event);
}
int epoll_wait(int epfd, struct epoll_event* events, int maxevents, int timeout)
{
return epoll_pwait(epfd, events, maxevents, timeout, nullptr);
}
int epoll_pwait(int epfd, struct epoll_event* events, int maxevents, int timeout, const sigset_t* sigmask)
{
timespec ts;
timespec* ts_ptr = nullptr;
if (timeout >= 0)
{
ts.tv_sec = static_cast<time_t>(timeout / 1000),
ts.tv_nsec = (timeout % 1000) * 1'000'000,
ts_ptr = &ts;
}
return epoll_pwait2(epfd, events, maxevents, ts_ptr, sigmask);
}
int epoll_pwait2(int epfd, struct epoll_event* events, int maxevents, const struct timespec* timeout, const sigset_t* sigmask)
{
return syscall(SYS_EPOLL_PWAIT2, epfd, events, maxevents, timeout, sigmask);
}