accept4 emulation via accept ignores unknown flags, so it can spuriously succeed instead of failing (or succeed without doing the action implied by an unknown flag if it's added in a future kernel). Worse, unknown flags trigger the fallback code even on modern kernels if the real accept4 syscall returns EINVAL, because this is indistinguishable from socketcall returning EINVAL due to lack of accept4 support. Fix this by always failing with EINVAL if unknown flags are present and the syscall is missing or failed with EINVAL.
24 lines
657 B
C
24 lines
657 B
C
#define _GNU_SOURCE
|
|
#include <sys/socket.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include "syscall.h"
|
|
|
|
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;
|
|
if (flg & ~(SOCK_CLOEXEC|SOCK_NONBLOCK)) {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
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;
|
|
}
|