Path: blob/master/src/java.base/aix/native/libnio/ch/AixPollPort.c
41133 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2012 SAP SE. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation. Oracle designates this8* particular file as subject to the "Classpath" exception as provided9* by Oracle in the LICENSE file that accompanied this code.10*11* This code is distributed in the hope that it will be useful, but WITHOUT12* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or13* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License14* version 2 for more details (a copy is included in the LICENSE file that15* accompanied this code).16*17* You should have received a copy of the GNU General Public License version18* 2 along with this work; if not, write to the Free Software Foundation,19* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.20*21* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA22* or visit www.oracle.com if you need additional information or have any23* questions.24*/2526#include "jni.h"27#include "jni_util.h"28#include "jvm.h"29#include "jlong.h"3031#include "sun_nio_ch_AixPollPort.h"3233#include <unistd.h>34#include <sys/types.h>35#include <sys/socket.h>36#include <poll.h>37#include <sys/pollset.h>38#include <fcntl.h>39#include <stddef.h>40#include <dlfcn.h>41#include <errno.h>4243/* Initially copied from src/solaris/native/sun/nio/ch/nio_util.h */44#define RESTARTABLE(_cmd, _result) do { \45do { \46_result = _cmd; \47} while((_result == -1) && (errno == EINTR)); \48} while(0)4950typedef pollset_t pollset_create_func(int maxfd);51typedef int pollset_destroy_func(pollset_t ps);52typedef int pollset_ctl_func(pollset_t ps, struct poll_ctl *pollctl_array, int array_length);53typedef int pollset_poll_func(pollset_t ps, struct pollfd *polldata_array, int array_length, int timeout);54static pollset_create_func* _pollset_create = NULL;55static pollset_destroy_func* _pollset_destroy = NULL;56static pollset_ctl_func* _pollset_ctl = NULL;57static pollset_poll_func* _pollset_poll = NULL;5859JNIEXPORT void JNICALL60Java_sun_nio_ch_AixPollPort_init(JNIEnv* env, jclass this) {61_pollset_create = (pollset_create_func*) dlsym(RTLD_DEFAULT, "pollset_create");62_pollset_destroy = (pollset_destroy_func*) dlsym(RTLD_DEFAULT, "pollset_destroy");63_pollset_ctl = (pollset_ctl_func*) dlsym(RTLD_DEFAULT, "pollset_ctl");64_pollset_poll = (pollset_poll_func*) dlsym(RTLD_DEFAULT, "pollset_poll");65if (_pollset_create == NULL || _pollset_destroy == NULL ||66_pollset_ctl == NULL || _pollset_poll == NULL) {67JNU_ThrowInternalError(env, "unable to get address of pollset functions");68}69}7071JNIEXPORT jint JNICALL72Java_sun_nio_ch_AixPollPort_eventSize(JNIEnv* env, jclass this) {73return sizeof(struct pollfd);74}7576JNIEXPORT jint JNICALL77Java_sun_nio_ch_AixPollPort_eventsOffset(JNIEnv* env, jclass this) {78return offsetof(struct pollfd, events);79}8081JNIEXPORT jint JNICALL82Java_sun_nio_ch_AixPollPort_reventsOffset(JNIEnv* env, jclass this) {83return offsetof(struct pollfd, revents);84}8586JNIEXPORT jint JNICALL87Java_sun_nio_ch_AixPollPort_fdOffset(JNIEnv* env, jclass this) {88return offsetof(struct pollfd, fd);89}9091JNIEXPORT jint JNICALL92Java_sun_nio_ch_AixPollPort_pollsetCreate(JNIEnv *env, jclass c) {93/* pollset_create can take the maximum number of fds, but we94* cannot predict this number so we leave it at OPEN_MAX. */95pollset_t ps = _pollset_create(-1);96if (ps < 0) {97JNU_ThrowIOExceptionWithLastError(env, "pollset_create failed");98}99return (int)ps;100}101102JNIEXPORT jint JNICALL103Java_sun_nio_ch_AixPollPort_pollsetCtl(JNIEnv *env, jclass c, jint ps,104jint opcode, jint fd, jint events) {105struct poll_ctl event;106int res;107108event.cmd = opcode;109event.events = events;110event.fd = fd;111112RESTARTABLE(_pollset_ctl((pollset_t)ps, &event, 1 /* length */), res);113114return (res == 0) ? 0 : errno;115}116117JNIEXPORT jint JNICALL118Java_sun_nio_ch_AixPollPort_pollsetPoll(JNIEnv *env, jclass c,119jint ps, jlong address, jint numfds) {120struct pollfd *events = jlong_to_ptr(address);121int res;122123RESTARTABLE(_pollset_poll(ps, events, numfds, -1), res);124if (res < 0) {125JNU_ThrowIOExceptionWithLastError(env, "pollset_poll failed");126}127return res;128}129130JNIEXPORT void JNICALL131Java_sun_nio_ch_AixPollPort_pollsetDestroy(JNIEnv *env, jclass c, jint ps) {132int res;133RESTARTABLE(_pollset_destroy((pollset_t)ps), res);134}135136JNIEXPORT void JNICALL137Java_sun_nio_ch_AixPollPort_socketpair(JNIEnv* env, jclass clazz, jintArray sv) {138int sp[2];139if (socketpair(PF_UNIX, SOCK_STREAM, 0, sp) == -1) {140JNU_ThrowIOExceptionWithLastError(env, "socketpair failed");141} else {142jint res[2];143res[0] = (jint)sp[0];144res[1] = (jint)sp[1];145(*env)->SetIntArrayRegion(env, sv, 0, 2, &res[0]);146}147}148149JNIEXPORT void JNICALL150Java_sun_nio_ch_AixPollPort_interrupt(JNIEnv *env, jclass c, jint fd) {151int res;152int buf[1];153buf[0] = 1;154RESTARTABLE(write(fd, buf, 1), res);155if (res < 0) {156JNU_ThrowIOExceptionWithLastError(env, "write failed");157}158}159160JNIEXPORT void JNICALL161Java_sun_nio_ch_AixPollPort_drain1(JNIEnv *env, jclass cl, jint fd) {162int res;163char buf[1];164RESTARTABLE(read(fd, buf, 1), res);165if (res < 0) {166JNU_ThrowIOExceptionWithLastError(env, "drain1 failed");167}168}169170JNIEXPORT void JNICALL171Java_sun_nio_ch_AixPollPort_close0(JNIEnv *env, jclass c, jint fd) {172int res;173RESTARTABLE(close(fd), res);174}175176177