From 3019f6db5565b5711540d1086d307eae00b5f812 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 4 Apr 2014 07:45:23 +0200 Subject: [PATCH] [#2361] Native.epollCreate(...) fails on systems using a kernel < 2.6.27 / glibc < 2.9 Motivation: Native.epollCreate(...) fails on systems using a kernel < 2.6.27 / glibc < 2.9 because it uses epoll_create1(...) without checking if it is present Modifications: Check if epoll_create1(...) exists abd if not fall back to use epoll_create(...) Result: Works even on systems with kernel < 2.6.27 / glibc < 2.9 --- .../src/main/c/io_netty_channel_epoll_Native.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/transport-native-epoll/src/main/c/io_netty_channel_epoll_Native.c b/transport-native-epoll/src/main/c/io_netty_channel_epoll_Native.c index bcaa1f8975..459aa251c4 100644 --- a/transport-native-epoll/src/main/c/io_netty_channel_epoll_Native.c +++ b/transport-native-epoll/src/main/c/io_netty_channel_epoll_Native.c @@ -32,6 +32,7 @@ // optional extern int accept4(int sockFd, struct sockaddr *addr, socklen_t *addrlen, int flags) __attribute__((weak)); +extern int epoll_create1(int flags) __attribute__((weak)); // Those are initialized in the init(...) method and cached for performance reasons jmethodID updatePosId = NULL; @@ -401,11 +402,25 @@ JNIEXPORT void JNICALL Java_io_netty_channel_epoll_Native_eventFdRead(JNIEnv * e } JNIEXPORT jint JNICALL Java_io_netty_channel_epoll_Native_epollCreate(JNIEnv * env, jclass clazz) { - jint efd = epoll_create1(EPOLL_CLOEXEC); + jint efd; + if (epoll_create1) { + efd = epoll_create1(EPOLL_CLOEXEC); + } else { + // size will be ignored anyway but must be positive + efd = epoll_create(126); + } if (efd < 0) { int err = errno; throwRuntimeException(env, exceptionMessage("Error during epoll_create(...): ", err)); } + if (!epoll_create1) { + if (fcntl(efd, F_SETFD, FD_CLOEXEC) < 0) { + int err = errno; + close(efd); + throwRuntimeException(env, exceptionMessage("Error during fcntl(...): ", err)); + return err; + } + } return efd; }