summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSyrus Akbary <me@syrusakbary.com>2024-04-22 18:53:02 +0200
committerGitHub <noreply@github.com>2024-04-22 18:53:02 +0200
commitba6930c65135774b2365d34901b3755e0449d54f (patch)
treee9f0f603e99768a13e008e8a966d4405b79a0eb0
parentb7faeae4fca71ff730b9e856e7ab1235558e0b96 (diff)
parent82a2fa97e9b2b8b287a5963fca2f5c9497c80a37 (diff)
Merge pull request #32 from rodjjo/epoll-please
Expose the epoll support.
-rw-r--r--Makefile3
-rw-r--r--expected/wasm32-wasi/defined-symbols.txt5
-rw-r--r--expected/wasm32-wasi/include-all.c1
-rw-r--r--expected/wasm32-wasi/predefined-macros.txt23
-rw-r--r--expected/wasm64-wasi/defined-symbols.txt5
-rw-r--r--expected/wasm64-wasi/include-all.c1
-rw-r--r--expected/wasm64-wasi/predefined-macros.txt23
-rw-r--r--libc-top-half/musl/src/linux/epoll.c104
8 files changed, 148 insertions, 17 deletions
diff --git a/Makefile b/Makefile
index a424ef7..899d49a 100644
--- a/Makefile
+++ b/Makefile
@@ -193,6 +193,7 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
unistd/ttyname_r.c \
linux/wait3.c \
linux/wait4.c \
+ linux/epoll.c \
linux/eventfd.c \
linux/setgroups.c \
stat/futimesat.c \
@@ -469,7 +470,7 @@ MUSL_OMIT_HEADERS += \
"lastlog.h" \
"sys/acct.h" \
"sys/cachectl.h" \
- "sys/epoll.h" "sys/reboot.h" "sys/swap.h" \
+ "sys/reboot.h" "sys/swap.h" \
"sys/inotify.h" \
"sys/quota.h" \
"sys/klog.h" \
diff --git a/expected/wasm32-wasi/defined-symbols.txt b/expected/wasm32-wasi/defined-symbols.txt
index 498a360..63650b7 100644
--- a/expected/wasm32-wasi/defined-symbols.txt
+++ b/expected/wasm32-wasi/defined-symbols.txt
@@ -757,6 +757,11 @@ endprotoent
endpwent
endspent
environ
+epoll_create
+epoll_create1
+epoll_ctl
+epoll_pwait
+epoll_wait
erand48
erf
erfc
diff --git a/expected/wasm32-wasi/include-all.c b/expected/wasm32-wasi/include-all.c
index 7087f83..db2ef0f 100644
--- a/expected/wasm32-wasi/include-all.c
+++ b/expected/wasm32-wasi/include-all.c
@@ -152,6 +152,7 @@
#include <strings.h>
#include <stropts.h>
#include <sys/dir.h>
+#include <sys/epoll.h>
#include <sys/errno.h>
#include <sys/eventfd.h>
#include <sys/fcntl.h>
diff --git a/expected/wasm32-wasi/predefined-macros.txt b/expected/wasm32-wasi/predefined-macros.txt
index 789184b..7979b02 100644
--- a/expected/wasm32-wasi/predefined-macros.txt
+++ b/expected/wasm32-wasi/predefined-macros.txt
@@ -999,7 +999,27 @@
#define EOWNERDEAD __WASI_ERRNO_OWNERDEAD
#define EPERM __WASI_ERRNO_PERM
#define EPIPE __WASI_ERRNO_PIPE
-#define EPOLLPRI EPOLLIN
+#define EPOLLERR 0x008
+#define EPOLLET (1U<<31)
+#define EPOLLEXCLUSIVE (1U<<28)
+#define EPOLLHUP 0x010
+#define EPOLLIN 0x001
+#define EPOLLMSG 0x400
+#define EPOLLNVAL 0x020
+#define EPOLLONESHOT (1U<<30)
+#define EPOLLOUT 0x004
+#define EPOLLPRI 0x002
+#define EPOLLRDBAND 0x080
+#define EPOLLRDHUP 0x2000
+#define EPOLLRDNORM 0x040
+#define EPOLLWAKEUP (1U<<29)
+#define EPOLLWRBAND 0x200
+#define EPOLLWRNORM 0x100
+#define EPOLL_CLOEXEC O_CLOEXEC
+#define EPOLL_CTL_ADD 1
+#define EPOLL_CTL_DEL 2
+#define EPOLL_CTL_MOD 3
+#define EPOLL_NONBLOCK O_NONBLOCK
#define EPROTO __WASI_ERRNO_PROTO
#define EPROTONOSUPPORT __WASI_ERRNO_PROTONOSUPPORT
#define EPROTOTYPE __WASI_ERRNO_PROTOTYPE
@@ -5288,6 +5308,7 @@
#define _STROPTS_H
#define _SYSEXITS_H
#define _SYSLOG_H
+#define _SYS_EPOLL_H
#define _SYS_EVENTFD_H
#define _SYS_FILE_H
#define _SYS_IOCTL_H
diff --git a/expected/wasm64-wasi/defined-symbols.txt b/expected/wasm64-wasi/defined-symbols.txt
index 498a360..63650b7 100644
--- a/expected/wasm64-wasi/defined-symbols.txt
+++ b/expected/wasm64-wasi/defined-symbols.txt
@@ -757,6 +757,11 @@ endprotoent
endpwent
endspent
environ
+epoll_create
+epoll_create1
+epoll_ctl
+epoll_pwait
+epoll_wait
erand48
erf
erfc
diff --git a/expected/wasm64-wasi/include-all.c b/expected/wasm64-wasi/include-all.c
index 7087f83..db2ef0f 100644
--- a/expected/wasm64-wasi/include-all.c
+++ b/expected/wasm64-wasi/include-all.c
@@ -152,6 +152,7 @@
#include <strings.h>
#include <stropts.h>
#include <sys/dir.h>
+#include <sys/epoll.h>
#include <sys/errno.h>
#include <sys/eventfd.h>
#include <sys/fcntl.h>
diff --git a/expected/wasm64-wasi/predefined-macros.txt b/expected/wasm64-wasi/predefined-macros.txt
index 2ef2ccc..9b8831e 100644
--- a/expected/wasm64-wasi/predefined-macros.txt
+++ b/expected/wasm64-wasi/predefined-macros.txt
@@ -999,7 +999,27 @@
#define EOWNERDEAD __WASI_ERRNO_OWNERDEAD
#define EPERM __WASI_ERRNO_PERM
#define EPIPE __WASI_ERRNO_PIPE
-#define EPOLLPRI EPOLLIN
+#define EPOLLERR 0x008
+#define EPOLLET (1U<<31)
+#define EPOLLEXCLUSIVE (1U<<28)
+#define EPOLLHUP 0x010
+#define EPOLLIN 0x001
+#define EPOLLMSG 0x400
+#define EPOLLNVAL 0x020
+#define EPOLLONESHOT (1U<<30)
+#define EPOLLOUT 0x004
+#define EPOLLPRI 0x002
+#define EPOLLRDBAND 0x080
+#define EPOLLRDHUP 0x2000
+#define EPOLLRDNORM 0x040
+#define EPOLLWAKEUP (1U<<29)
+#define EPOLLWRBAND 0x200
+#define EPOLLWRNORM 0x100
+#define EPOLL_CLOEXEC O_CLOEXEC
+#define EPOLL_CTL_ADD 1
+#define EPOLL_CTL_DEL 2
+#define EPOLL_CTL_MOD 3
+#define EPOLL_NONBLOCK O_NONBLOCK
#define EPROTO __WASI_ERRNO_PROTO
#define EPROTONOSUPPORT __WASI_ERRNO_PROTONOSUPPORT
#define EPROTOTYPE __WASI_ERRNO_PROTOTYPE
@@ -5286,6 +5306,7 @@
#define _STROPTS_H
#define _SYSEXITS_H
#define _SYSLOG_H
+#define _SYS_EPOLL_H
#define _SYS_EVENTFD_H
#define _SYS_FILE_H
#define _SYS_IOCTL_H
diff --git a/libc-top-half/musl/src/linux/epoll.c b/libc-top-half/musl/src/linux/epoll.c
index deff5b1..474dec7 100644
--- a/libc-top-half/musl/src/linux/epoll.c
+++ b/libc-top-half/musl/src/linux/epoll.c
@@ -1,37 +1,113 @@
+#include <wasi/api.h>
#include <sys/epoll.h>
#include <signal.h>
#include <errno.h>
-#include "syscall.h"
int epoll_create(int size)
{
- return epoll_create1(0);
+ int ret_val = 0;
+ int error = __wasi_epoll_create(&ret_val);
+ if (error == 0)
+ {
+ return ret_val;
+ }
+ errno = error;
+ return -1;
}
int epoll_create1(int flags)
{
- int r = __syscall(SYS_epoll_create1, flags);
-#ifdef SYS_epoll_create
- if (r==-ENOSYS && !flags) r = __syscall(SYS_epoll_create, 1);
-#endif
- return __syscall_ret(r);
+ return epoll_create(0);
}
int epoll_ctl(int fd, int op, int fd2, struct epoll_event *ev)
{
- return syscall(SYS_epoll_ctl, fd, op, fd2, ev);
+ if (ev)
+ {
+ struct __wasi_epoll_event_t ev2;
+
+ ev2.events = 0;
+ if ((ev->events & EPOLLIN) != 0)
+ ev2.events |= __WASI_EPOLL_TYPE_EPOLLIN;
+ if ((ev->events & EPOLLOUT) != 0)
+ ev2.events |= __WASI_EPOLL_TYPE_EPOLLOUT;
+ if ((ev->events & EPOLLRDHUP) != 0)
+ ev2.events |= __WASI_EPOLL_TYPE_EPOLLRDHUP;
+ if ((ev->events & EPOLLPRI) != 0)
+ ev2.events |= __WASI_EPOLL_TYPE_EPOLLPRI;
+ if ((ev->events & EPOLLERR) != 0)
+ ev2.events |= __WASI_EPOLL_TYPE_EPOLLERR;
+ if ((ev->events & EPOLLHUP) != 0)
+ ev2.events |= __WASI_EPOLL_TYPE_EPOLLHUP;
+ if ((ev->events & EPOLLET) != 0)
+ ev2.events |= __WASI_EPOLL_TYPE_EPOLLET;
+ if ((ev->events & EPOLLONESHOT) != 0)
+ ev2.events |= __WASI_EPOLL_TYPE_EPOLLONESHOT;
+ ev2.data.ptr = (__wasi_pointersize_t)ev->data.ptr;
+ ev2.data.fd = fd2;
+ ev2.data.data1 = ev->data.u32;
+ ev2.data.data2 = ev->data.u64;
+ return __wasi_epoll_ctl(fd, op, fd2, &ev2);
+ }
+ return __wasi_epoll_ctl(fd, op, fd2, NULL);
}
int epoll_pwait(int fd, struct epoll_event *ev, int cnt, int to, const sigset_t *sigs)
{
- int r = __syscall(SYS_epoll_pwait, fd, ev, cnt, to, sigs, _NSIG/8);
-#ifdef SYS_epoll_wait
- if (r==-ENOSYS && !sigs) r = __syscall(SYS_epoll_wait, fd, ev, cnt, to);
-#endif
- return __syscall_ret(r);
+ (void)sigs;
+ __wasi_size_t ret_val;
+ struct __wasi_epoll_event_t ev2[64];
+ if (cnt > 64)
+ {
+ cnt = 64;
+ }
+ int error = __wasi_epoll_wait(fd, &ev2[0], cnt, to, &ret_val);
+ if (error == 0)
+ {
+ cnt = ret_val;
+ for (int c = 0; c < cnt; c++)
+ {
+ ev[c].events = 0;
+ if ((ev2[c].events & __WASI_EPOLL_TYPE_EPOLLIN) != 0)
+ ev[c].events |= EPOLLIN;
+ if ((ev2[c].events & __WASI_EPOLL_TYPE_EPOLLOUT) != 0)
+ ev[c].events |= EPOLLOUT;
+ if ((ev2[c].events & __WASI_EPOLL_TYPE_EPOLLRDHUP) != 0)
+ ev[c].events |= EPOLLRDHUP;
+ if ((ev2[c].events & __WASI_EPOLL_TYPE_EPOLLPRI) != 0)
+ ev[c].events |= EPOLLPRI;
+ if ((ev2[c].events & __WASI_EPOLL_TYPE_EPOLLERR) != 0)
+ ev[c].events |= EPOLLERR;
+ if ((ev2[c].events & __WASI_EPOLL_TYPE_EPOLLHUP) != 0)
+ ev[c].events |= EPOLLHUP;
+ if ((ev2[c].events & __WASI_EPOLL_TYPE_EPOLLET) != 0)
+ ev[c].events |= EPOLLET;
+ if ((ev2[c].events & __WASI_EPOLL_TYPE_EPOLLONESHOT) != 0)
+ ev[c].events |= EPOLLONESHOT;
+ if (ev2[c].data.ptr)
+ {
+ ev[c].data.ptr = (void *)ev2[c].data.ptr;
+ }
+ else if (ev2[c].data.fd)
+ {
+ ev[c].data.fd = ev2[c].data.fd;
+ }
+ else if (ev2[c].data.data1)
+ {
+ ev[c].data.u32 = ev2[c].data.data1;
+ }
+ else
+ {
+ ev[c].data.u64 = ev2[c].data.data2;
+ }
+ }
+ return (int)ret_val;
+ }
+ errno = error;
+ return -1;
}
int epoll_wait(int fd, struct epoll_event *ev, int cnt, int to)
{
- return epoll_pwait(fd, ev, cnt, to, 0);
+ return epoll_pwait(fd, ev, cnt, to, 0);
}