From 299afd25fd6dead39bf5c78572782db885579911 Mon Sep 17 00:00:00 2001 From: jilles Date: Wed, 1 May 2013 20:10:21 +0000 Subject: Add accept4() system call. The accept4() function, compared to accept(), allows setting the new file descriptor atomically close-on-exec and explicitly controlling the non-blocking status on the new socket. (Note that the latter point means that accept() is not equivalent to any form of accept4().) The linuxulator's accept4 implementation leaves a race window where the new file descriptor is not close-on-exec because it calls sys_accept(). This implementation leaves no such race window (by using falloc() flags). The linuxulator could be fixed and simplified by using the new code. Like accept(), accept4() is async-signal-safe, a cancellation point and permitted in capability mode. --- lib/libthr/pthread.map | 5 +++++ lib/libthr/thread/thr_syscalls.c | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) (limited to 'lib/libthr') diff --git a/lib/libthr/pthread.map b/lib/libthr/pthread.map index 355edea..bbbd930e 100644 --- a/lib/libthr/pthread.map +++ b/lib/libthr/pthread.map @@ -181,6 +181,7 @@ FBSDprivate_1.0 { ___wait; ___waitpid; __accept; + __accept4; __aio_suspend; __close; __connect; @@ -408,3 +409,7 @@ FBSD_1.2 { setcontext; swapcontext; }; + +FBSD_1.3 { + accept4; +}; diff --git a/lib/libthr/thread/thr_syscalls.c b/lib/libthr/thread/thr_syscalls.c index 2327d74..7a08302 100644 --- a/lib/libthr/thread/thr_syscalls.c +++ b/lib/libthr/thread/thr_syscalls.c @@ -101,6 +101,7 @@ extern pid_t __waitpid(pid_t, int *, int); extern int __sys_aio_suspend(const struct aiocb * const[], int, const struct timespec *); extern int __sys_accept(int, struct sockaddr *, socklen_t *); +extern int __sys_accept4(int, struct sockaddr *, socklen_t *, int); extern int __sys_connect(int, const struct sockaddr *, socklen_t); extern int __sys_fsync(int); extern int __sys_msync(void *, size_t, int); @@ -129,6 +130,7 @@ int ___usleep(useconds_t useconds); pid_t ___wait(int *); pid_t ___waitpid(pid_t, int *, int); int __accept(int, struct sockaddr *, socklen_t *); +int __accept4(int, struct sockaddr *, socklen_t *, int); int __aio_suspend(const struct aiocb * const iocbs[], int, const struct timespec *); int __close(int); @@ -176,6 +178,26 @@ __accept(int s, struct sockaddr *addr, socklen_t *addrlen) return (ret); } +__weak_reference(__accept4, accept4); + +/* + * Cancellation behavior: + * If thread is canceled, no socket is created. + */ +int +__accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags) +{ + struct pthread *curthread; + int ret; + + curthread = _get_curthread(); + _thr_cancel_enter(curthread); + ret = __sys_accept4(s, addr, addrlen, flags); + _thr_cancel_leave(curthread, ret == -1); + + return (ret); +} + __weak_reference(__aio_suspend, aio_suspend); int -- cgit v1.1