linux: improve kernel feature detection

Do not check for minimum kernel and glibc versions, just check that the kernel
headers export the syscall number and invoke the syscall directly. Effectively
bypasses glibc.
This commit is contained in:
Ben Noordhuis 2011-11-17 18:00:51 +01:00
parent a4c8ffa3c2
commit 6b3075cd73
3 changed files with 59 additions and 20 deletions

View File

@ -735,8 +735,8 @@ int uv__accept(int sockfd, struct sockaddr* saddr, socklen_t slen) {
assert(sockfd >= 0); assert(sockfd >= 0);
while (1) { while (1) {
#if HAVE_ACCEPT4 #if HAVE_SYS_ACCEPT4
peerfd = accept4(sockfd, saddr, &slen, SOCK_NONBLOCK | SOCK_CLOEXEC); peerfd = sys_accept4(sockfd, saddr, &slen, SOCK_NONBLOCK | SOCK_CLOEXEC);
if (peerfd != -1) if (peerfd != -1)
break; break;

View File

@ -32,30 +32,69 @@
#endif #endif
#undef HAVE_FUTIMES #undef HAVE_FUTIMES
#undef HAVE_PIPE2
#undef HAVE_ACCEPT4
#undef HAVE_KQUEUE #undef HAVE_KQUEUE
#undef HAVE_PORTS_FS #undef HAVE_PORTS_FS
#if defined(__linux__) #if defined(__linux__)
# undef HAVE_SYS_UTIMESAT
# undef HAVE_SYS_PIPE2
# undef HAVE_SYS_ACCEPT4
# undef _GNU_SOURCE
# define _GNU_SOURCE
# include <linux/version.h> # include <linux/version.h>
# include <sys/syscall.h>
# include <features.h> # include <features.h>
# include <unistd.h>
/* futimes() requires linux >= 2.6.22 and glib >= 2.6 */ # if __NR_utimensat
#if LINUX_VERSION_CODE >= 0x20616 && __GLIBC_PREREQ(2, 6) # define HAVE_SYS_UTIMESAT 1
# endif
# if __NR_pipe2
# define HAVE_SYS_PIPE2 1
# endif
# if __NR_accept4
# define HAVE_SYS_ACCEPT4 1
# endif
# if HAVE_SYS_UTIMESAT
inline static int sys_utimesat(int dirfd,
const char* path,
const struct timespec times[2],
int flags)
{
return syscall(__NR_utimensat, dirfd, path, times, flags);
}
inline static int sys_futimes(int fd, const struct timeval times[2])
{
struct timespec ts[2];
ts[0].tv_sec = times[0].tv_sec, ts[0].tv_nsec = times[0].tv_usec * 1000;
ts[1].tv_sec = times[1].tv_sec, ts[1].tv_nsec = times[1].tv_usec * 1000;
return sys_utimesat(fd, NULL, ts, 0);
}
# undef HAVE_FUTIMES
# define HAVE_FUTIMES 1 # define HAVE_FUTIMES 1
#endif # define futimes(fd, times) sys_futimes(fd, times)
# endif /* HAVE_SYS_FUTIMESAT */
/* pipe2() requires linux >= 2.6.27 and glibc >= 2.9 */ # if HAVE_SYS_PIPE2
#if LINUX_VERSION_CODE >= 0x2061B && __GLIBC_PREREQ(2, 9) inline static int sys_pipe2(int pipefd[2], int flags)
#define HAVE_PIPE2 1 {
#endif return syscall(__NR_pipe2, pipefd, flags);
}
# endif /* HAVE_SYS_PIPE2 */
/* accept4() requires linux >= 2.6.28 and glib >= 2.10 */ # if HAVE_SYS_ACCEPT4
#if LINUX_VERSION_CODE >= 0x2061C && __GLIBC_PREREQ(2, 10) inline static int sys_accept4(int fd,
#define HAVE_ACCEPT4 1 struct sockaddr* addr,
#endif socklen_t* addrlen,
int flags)
{
return syscall(__NR_accept4, fd, addr, addrlen, flags);
}
# endif /* HAVE_SYS_ACCEPT4 */
#endif /* __linux__ */ #endif /* __linux__ */

View File

@ -104,7 +104,7 @@ static int uv__make_socketpair(int fds[2], int flags) {
static int uv__make_pipe(int fds[2], int flags) { static int uv__make_pipe(int fds[2], int flags) {
#if HAVE_PIPE2 #if HAVE_SYS_PIPE2
int fl; int fl;
fl = O_CLOEXEC; fl = O_CLOEXEC;
@ -112,7 +112,7 @@ static int uv__make_pipe(int fds[2], int flags) {
if (flags & UV__F_NONBLOCK) if (flags & UV__F_NONBLOCK)
fl |= O_NONBLOCK; fl |= O_NONBLOCK;
if (pipe2(fds, fl) == 0) if (sys_pipe2(fds, fl) == 0)
return 0; return 0;
if (errno != ENOSYS) if (errno != ENOSYS)