diff options
author | Rich Felker <dalias@aerifal.cx> | 2014-02-21 22:25:26 -0500 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2014-02-21 22:25:26 -0500 |
commit | dc01e2cbfb290198c03514fe51ed32c1098b774f (patch) | |
tree | 79357e35a582f1fea0012d1c9ff1faf4403c2c0e /src/network/accept4.c | |
parent | fdb3efa5ddfa7120de98f8ae78b5f5dc9e8e2e71 (diff) | |
download | musl-dc01e2cbfb290198c03514fe51ed32c1098b774f.tar.gz musl-dc01e2cbfb290198c03514fe51ed32c1098b774f.tar.bz2 musl-dc01e2cbfb290198c03514fe51ed32c1098b774f.tar.xz musl-dc01e2cbfb290198c03514fe51ed32c1098b774f.zip |
add fallback emulation for accept4 on old kernels
the other atomic FD_CLOEXEC interfaces (dup3, pipe2, socket) already
had such emulation in place. the justification for doing the emulation
here is the same as for the other functions: it allows applications to
simply use accept4 rather than having to have their own fallback code
for ENOSYS/EINVAL (which one you get is arch-specific!) and there is
no reasonable way an application could benefit from knowing the
operation is emulated/non-atomic since there is no workaround at the
application level for non-atomicity (that is the whole reason these
interfaces were added).
Diffstat (limited to 'src/network/accept4.c')
-rw-r--r-- | src/network/accept4.c | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/src/network/accept4.c b/src/network/accept4.c index 6b5c16ce..285d8588 100644 --- a/src/network/accept4.c +++ b/src/network/accept4.c @@ -1,9 +1,20 @@ #define _GNU_SOURCE #include <sys/socket.h> +#include <errno.h> +#include <fcntl.h> #include "syscall.h" #include "libc.h" int accept4(int fd, struct sockaddr *restrict addr, socklen_t *restrict len, int flg) { - return socketcall_cp(accept4, fd, addr, len, flg, 0, 0); + 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; } |