1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * 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 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <unistd.h>
  27 #include <errno.h>
  28 
  29 #include "jni.h"
  30 #include "jni_util.h"
  31 #include "jlong.h"
  32 #include "jdk_internal_jimage_concurrent_ConcurrentPReader.h"
  33 
  34 #ifdef _ALLBSD_SOURCE
  35   #define pread64 pread
  36 #endif
  37 
  38 #define RESTARTABLE(_cmd, _result) do { \
  39   do { \
  40     _result = _cmd; \
  41   } while((_result == -1) && (errno == EINTR)); \
  42 } while(0)
  43 
  44 static jfieldID fd_fdID;
  45 
  46 JNIEXPORT void JNICALL
  47 Java_jdk_internal_jimage_concurrent_ConcurrentPReader_initIDs(JNIEnv *env, jclass clazz)
  48 {
  49     CHECK_NULL(clazz = (*env)->FindClass(env, "java/io/FileDescriptor"));
  50     CHECK_NULL(fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I"));
  51 }
  52 
  53 JNIEXPORT jint JNICALL
  54 Java_jdk_internal_jimage_concurrent_ConcurrentPReader_pread(JNIEnv *env, jclass clazz,
  55                                                             jobject fdo, jlong address,
  56                                                             jint len, jlong offset)
  57 {
  58     jint fd = (*env)->GetIntField(env, fdo, fd_fdID);
  59     void *buf = (void *)jlong_to_ptr(address);
  60     int res;
  61     RESTARTABLE(pread64(fd, buf, len, offset), res);
  62     if (res == -1) {
  63         JNU_ThrowIOExceptionWithLastError(env, "pread failed");
  64     }
  65     return res;
  66 }