Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for accept{,4} #284

Closed
rvolosatovs opened this issue Apr 14, 2022 · 3 comments · Fixed by #287
Closed

Support for accept{,4} #284

rvolosatovs opened this issue Apr 14, 2022 · 3 comments · Fixed by #287

Comments

@rvolosatovs
Copy link
Contributor

With #282 in place, it should be now possible to implement accept and accept4.

@rvolosatovs
Copy link
Contributor Author

cc @Deepansharora27 perhaps you could be interested in contributing to this?

@Deepansharora27
Copy link

Deepansharora27 commented Apr 27, 2022

#include <sys/socket.h>
#include "syscall.h"
int accept(int fd, struct sockaddr *restrict addr, socklen_t *restrict len)
{
return socketcall_cp(accept, fd, addr, len, 0, 0, 0);
}

int accept4(int fd, struct sockaddr *restrict addr, socklen_t *restrict len, int flg)
{
if (!flg) return accept(fd, addr, len);
int ret = socketcall_cp(accept4, fd, addr, len, flg, 0, 0);
if (ret>=0 || (errno != ENOSYS && errno != EINVAL)) return ret;
ret = accept(fd, addr, len);
if (ret<0) return ret;
if (flg & SOCK_CLOEXEC)
__syscall(SYS_fcntl, ret, F_SETFD, FD_CLOEXEC);
if (flg & SOCK_NONBLOCK)
__syscall(SYS_fcntl, ret, F_SETFL, O_NONBLOCK);
return ret;
}

As I was iterating and trying to get an understanding of the repository, I have found prototype implementations of both the accept() and accept4() functions.
Since the issue description entails that the required task is to add support for accept() and accept4(),How can I take this ahead from here given that these two are the prototype implementations for the function.

@sunfishcode
Copy link
Member

Those accept.c and accept4.c files are in the libc-top-half/musl directory and are part of musl. musl upstream only supports Linux, so to make it work for wasi, we only use musl's "top half"—things like printf, qsort, etc.—and disable its "bottom half—things like read, open, but also accept and accept4 as seen here. Specifically, in the code in these musl files, socketcall_cp and __syscall perform Linux syscalls, so the code won't work as-is.

For wasi-libc, we'll need to add libc-bottom-half/sources/accept.c and libc-bottom-half/sources/accept4.c, which likely do something similar, but which call the WASI functions instead of doing Linux syscalls. Also, WASI doesn't have O_CLOEXEC (as it doesn't have an exec), so it doesn't need SOCK_CLOEXEC/FD_CLOEXEC code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants