0N/A/*
2362N/A * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include "jni.h"
0N/A#include "jni_util.h"
0N/A#include "jvm.h"
0N/A#include "jlong.h"
0N/A#include "sun_nio_ch_DevPollArrayWrapper.h"
0N/A#include <sys/poll.h>
0N/A#include <unistd.h>
0N/A#include <sys/time.h>
0N/A
0N/A#ifdef __cplusplus
0N/Aextern "C" {
0N/A#endif
0N/A
0N/Atypedef uint32_t caddr32_t;
0N/A
0N/A/* /dev/poll ioctl */
0N/A#define DPIOC (0xD0 << 8)
0N/A#define DP_POLL (DPIOC | 1) /* poll on fds in cached in /dev/poll */
0N/A#define DP_ISPOLLED (DPIOC | 2) /* is this fd cached in /dev/poll */
0N/A#define DEVPOLLSIZE 1000 /* /dev/poll table size increment */
0N/A#define POLLREMOVE 0x0800 /* Removes fd from monitored set */
0N/A
0N/A/*
0N/A * /dev/poll DP_POLL ioctl format
0N/A */
0N/Atypedef struct dvpoll {
0N/A pollfd_t *dp_fds; /* pollfd array */
0N/A nfds_t dp_nfds; /* num of pollfd's in dp_fds[] */
0N/A int dp_timeout; /* time out in millisec */
0N/A} dvpoll_t;
0N/A
0N/Atypedef struct dvpoll32 {
0N/A caddr32_t dp_fds; /* pollfd array */
0N/A uint32_t dp_nfds; /* num of pollfd's in dp_fds[] */
0N/A int32_t dp_timeout; /* time out in millisec */
0N/A} dvpoll32_t;
0N/A
0N/A#ifdef __cplusplus
0N/A}
0N/A#endif
0N/A
0N/A#define RESTARTABLE(_cmd, _result) do { \
0N/A do { \
0N/A _result = _cmd; \
0N/A } while((_result == -1) && (errno == EINTR)); \
0N/A} while(0)
0N/A
0N/Astatic int
0N/Aidevpoll(jint wfd, int dpctl, struct dvpoll a)
0N/A{
0N/A jlong start, now;
0N/A int remaining = a.dp_timeout;
0N/A struct timeval t;
0N/A int diff;
0N/A
0N/A gettimeofday(&t, NULL);
0N/A start = t.tv_sec * 1000 + t.tv_usec / 1000;
0N/A
0N/A for (;;) {
0N/A /* poll(7d) ioctl does not return remaining count */
0N/A int res = ioctl(wfd, dpctl, &a);
0N/A if (res < 0 && errno == EINTR) {
0N/A if (remaining >= 0) {
0N/A gettimeofday(&t, NULL);
0N/A now = t.tv_sec * 1000 + t.tv_usec / 1000;
0N/A diff = now - start;
0N/A remaining -= diff;
0N/A if (diff < 0 || remaining <= 0) {
0N/A return 0;
0N/A }
0N/A start = now;
0N/A }
0N/A } else {
0N/A return res;
0N/A }
0N/A }
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_nio_ch_DevPollArrayWrapper_init(JNIEnv *env, jobject this)
0N/A{
0N/A int wfd = open("/dev/poll", O_RDWR);
0N/A if (wfd < 0) {
0N/A JNU_ThrowIOExceptionWithLastError(env, "Error opening driver");
0N/A return -1;
0N/A }
0N/A return wfd;
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_nio_ch_DevPollArrayWrapper_register(JNIEnv *env, jobject this,
0N/A jint wfd, jint fd, jint mask)
0N/A{
5062N/A struct pollfd a[1];
5062N/A int n;
0N/A
0N/A a[0].fd = fd;
5062N/A a[0].events = mask;
0N/A a[0].revents = 0;
0N/A
5062N/A n = write(wfd, &a[0], sizeof(a));
5062N/A if (n != sizeof(a)) {
5062N/A if (n < 0) {
0N/A JNU_ThrowIOExceptionWithLastError(env, "Error writing pollfds");
5062N/A } else {
5062N/A JNU_ThrowIOException(env, "Unexpected number of bytes written");
0N/A }
0N/A }
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_nio_ch_DevPollArrayWrapper_registerMultiple(JNIEnv *env, jobject this,
0N/A jint wfd, jlong address,
0N/A jint len)
0N/A{
0N/A unsigned char *pollBytes = (unsigned char *)jlong_to_ptr(address);
0N/A unsigned char *pollEnd = pollBytes + sizeof(struct pollfd) * len;
0N/A while (pollBytes < pollEnd) {
0N/A int bytesWritten = write(wfd, pollBytes, (int)(pollEnd - pollBytes));
0N/A if (bytesWritten < 0) {
0N/A JNU_ThrowIOExceptionWithLastError(env, "Error writing pollfds");
0N/A return;
0N/A }
0N/A pollBytes += bytesWritten;
0N/A }
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_nio_ch_DevPollArrayWrapper_poll0(JNIEnv *env, jobject this,
0N/A jlong address, jint numfds,
0N/A jlong timeout, jint wfd)
0N/A{
0N/A struct dvpoll a;
0N/A void *pfd = (void *) jlong_to_ptr(address);
0N/A int result = 0;
0N/A
0N/A a.dp_fds = pfd;
0N/A a.dp_nfds = numfds;
0N/A a.dp_timeout = (int)timeout;
0N/A
0N/A if (timeout <= 0) { /* Indefinite or no wait */
0N/A RESTARTABLE (ioctl(wfd, DP_POLL, &a), result);
0N/A } else { /* Bounded wait; bounded restarts */
0N/A result = idevpoll(wfd, DP_POLL, a);
0N/A }
0N/A
0N/A if (result < 0) {
0N/A JNU_ThrowIOExceptionWithLastError(env, "Error reading driver");
0N/A return -1;
0N/A }
0N/A return result;
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_nio_ch_DevPollArrayWrapper_interrupt(JNIEnv *env, jclass this, jint fd)
0N/A{
0N/A int fakebuf[1];
0N/A fakebuf[0] = 1;
0N/A if (write(fd, fakebuf, 1) < 0) {
0N/A JNU_ThrowIOExceptionWithLastError(env,
0N/A "Write to interrupt fd failed");
0N/A }
0N/A}