jdk/src/solaris/native/sun/nio/ch/FileDispatcher.c
changeset 2057 3acf8e5e2ca0
parent 2056 115e09b7a004
child 2058 577525f89bd4
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
     1 /*
       
     2  * Copyright 2000-2002 Sun Microsystems, Inc.  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 it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 #include "jni.h"
       
    27 #include "jni_util.h"
       
    28 #include "jvm.h"
       
    29 #include "jlong.h"
       
    30 #include "sun_nio_ch_FileDispatcher.h"
       
    31 #include <sys/types.h>
       
    32 #include <sys/socket.h>
       
    33 #include <fcntl.h>
       
    34 #include <sys/uio.h>
       
    35 #include "nio_util.h"
       
    36 
       
    37 
       
    38 static int preCloseFD = -1;     /* File descriptor to which we dup other fd's
       
    39                                    before closing them for real */
       
    40 
       
    41 
       
    42 JNIEXPORT void JNICALL
       
    43 Java_sun_nio_ch_FileDispatcher_init(JNIEnv *env, jclass cl)
       
    44 {
       
    45     int sp[2];
       
    46     if (socketpair(PF_UNIX, SOCK_STREAM, 0, sp) < 0) {
       
    47         JNU_ThrowIOExceptionWithLastError(env, "socketpair failed");
       
    48         return;
       
    49     }
       
    50     preCloseFD = sp[0];
       
    51     close(sp[1]);
       
    52 }
       
    53 
       
    54 JNIEXPORT jint JNICALL
       
    55 Java_sun_nio_ch_FileDispatcher_read0(JNIEnv *env, jclass clazz,
       
    56                              jobject fdo, jlong address, jint len)
       
    57 {
       
    58     jint fd = fdval(env, fdo);
       
    59     void *buf = (void *)jlong_to_ptr(address);
       
    60 
       
    61     return convertReturnVal(env, read(fd, buf, len), JNI_TRUE);
       
    62 }
       
    63 
       
    64 JNIEXPORT jint JNICALL
       
    65 Java_sun_nio_ch_FileDispatcher_pread0(JNIEnv *env, jclass clazz, jobject fdo,
       
    66                             jlong address, jint len, jlong offset)
       
    67 {
       
    68     jint fd = fdval(env, fdo);
       
    69     void *buf = (void *)jlong_to_ptr(address);
       
    70 
       
    71     return convertReturnVal(env, pread64(fd, buf, len, offset), JNI_TRUE);
       
    72 }
       
    73 
       
    74 JNIEXPORT jlong JNICALL
       
    75 Java_sun_nio_ch_FileDispatcher_readv0(JNIEnv *env, jclass clazz,
       
    76                               jobject fdo, jlong address, jint len)
       
    77 {
       
    78     jint fd = fdval(env, fdo);
       
    79     struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
       
    80     if (len > 16) {
       
    81         len = 16;
       
    82     }
       
    83     return convertLongReturnVal(env, readv(fd, iov, len), JNI_TRUE);
       
    84 }
       
    85 
       
    86 JNIEXPORT jint JNICALL
       
    87 Java_sun_nio_ch_FileDispatcher_write0(JNIEnv *env, jclass clazz,
       
    88                               jobject fdo, jlong address, jint len)
       
    89 {
       
    90     jint fd = fdval(env, fdo);
       
    91     void *buf = (void *)jlong_to_ptr(address);
       
    92 
       
    93     return convertReturnVal(env, write(fd, buf, len), JNI_FALSE);
       
    94 }
       
    95 
       
    96 JNIEXPORT jint JNICALL
       
    97 Java_sun_nio_ch_FileDispatcher_pwrite0(JNIEnv *env, jclass clazz, jobject fdo,
       
    98                             jlong address, jint len, jlong offset)
       
    99 {
       
   100     jint fd = fdval(env, fdo);
       
   101     void *buf = (void *)jlong_to_ptr(address);
       
   102 
       
   103     return convertReturnVal(env, pwrite64(fd, buf, len, offset), JNI_FALSE);
       
   104 }
       
   105 
       
   106 JNIEXPORT jlong JNICALL
       
   107 Java_sun_nio_ch_FileDispatcher_writev0(JNIEnv *env, jclass clazz,
       
   108                                        jobject fdo, jlong address, jint len)
       
   109 {
       
   110     jint fd = fdval(env, fdo);
       
   111     struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
       
   112     if (len > 16) {
       
   113         len = 16;
       
   114     }
       
   115     return convertLongReturnVal(env, writev(fd, iov, len), JNI_FALSE);
       
   116 }
       
   117 
       
   118 static void closeFileDescriptor(JNIEnv *env, int fd) {
       
   119     if (fd != -1) {
       
   120         int result = close(fd);
       
   121         if (result < 0)
       
   122             JNU_ThrowIOExceptionWithLastError(env, "Close failed");
       
   123     }
       
   124 }
       
   125 
       
   126 JNIEXPORT void JNICALL
       
   127 Java_sun_nio_ch_FileDispatcher_close0(JNIEnv *env, jclass clazz, jobject fdo)
       
   128 {
       
   129     jint fd = fdval(env, fdo);
       
   130     closeFileDescriptor(env, fd);
       
   131 }
       
   132 
       
   133 JNIEXPORT void JNICALL
       
   134 Java_sun_nio_ch_FileDispatcher_preClose0(JNIEnv *env, jclass clazz, jobject fdo)
       
   135 {
       
   136     jint fd = fdval(env, fdo);
       
   137     if (preCloseFD >= 0) {
       
   138         if (dup2(preCloseFD, fd) < 0)
       
   139             JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
       
   140     }
       
   141 }
       
   142 
       
   143 JNIEXPORT void JNICALL
       
   144 Java_sun_nio_ch_FileDispatcher_closeIntFD(JNIEnv *env, jclass clazz, jint fd)
       
   145 {
       
   146     closeFileDescriptor(env, fd);
       
   147 }