[#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
This commit is contained in:
Norman Maurer 2014-04-04 07:45:23 +02:00
parent 3eec26b0a2
commit 2fa79b2d5d

View File

@ -32,6 +32,7 @@
// optional // optional
extern int accept4(int sockFd, struct sockaddr *addr, socklen_t *addrlen, int flags) __attribute__((weak)); 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 // Those are initialized in the init(...) method and cached for performance reasons
jmethodID updatePosId = NULL; 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) { 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) { if (efd < 0) {
int err = errno; int err = errno;
throwRuntimeException(env, exceptionMessage("Error during epoll_create(...): ", err)); 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; return efd;
} }