[#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 7966c42636
commit 9d695e03db

View File

@ -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;
@ -377,11 +378,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;
}