libcore_io_Linux.cpp revision 8dc754726bb5303c25e2c48decdf76d9323ee231
1ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes/* 2ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * Copyright (C) 2011 The Android Open Source Project 3ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * 4ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 5ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * you may not use this file except in compliance with the License. 6ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * You may obtain a copy of the License at 7ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * 8ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 9ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * 10ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * Unless required by applicable law or agreed to in writing, software 11ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 12ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * See the License for the specific language governing permissions and 14ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * limitations under the License. 15ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes */ 16ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 17ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#define LOG_TAG "Posix" 18ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 19ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "JNIHelp.h" 20ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "JniConstants.h" 21ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include "JniException.h" 220a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include "NetworkUtilities.h" 230568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes#include "ScopedBytes.h" 240f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes#include "ScopedPrimitiveArray.h" 25ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "ScopedUtfChars.h" 2659fa7163774d6930a174bc038414a4b780581957Elliott Hughes#include "StaticAssert.h" 27ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include "toStringArray.h" 28ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 29ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include <errno.h> 300ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <fcntl.h> 31b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes#include <net/if.h> 324f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes#include <netdb.h> 330a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include <netinet/in.h> 34ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include <stdlib.h> 35461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes#include <sys/ioctl.h> 367e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes#include <sys/mman.h> 378b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes#include <sys/sendfile.h> 3859e4744d27231f260271dbbca406e0cc39768116Elliott Hughes#include <sys/socket.h> 3947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes#include <sys/stat.h> 400a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include <sys/time.h> 410ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <sys/types.h> 42bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes#include <sys/uio.h> 437341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#include <sys/utsname.h> 4459fa7163774d6930a174bc038414a4b780581957Elliott Hughes#include <sys/vfs.h> // Bionic doesn't have <sys/statvfs.h> 450ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <unistd.h> 46ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 474f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwException(JNIEnv* env, jclass exceptionClass, jmethodID ctor3, jmethodID ctor2, 484f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes const char* functionName, int error) { 49ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes jthrowable cause = NULL; 50ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes if (env->ExceptionCheck()) { 51ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes cause = env->ExceptionOccurred(); 52ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes env->ExceptionClear(); 53ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } 54ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 554f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName)); 564f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes if (detailMessage.get() == NULL) { 57f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes // Not really much we can do here. We're probably dead in the water, 58f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes // but let's try to stumble on... 59f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes env->ExceptionClear(); 60f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes } 61f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes 62ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes jobject exception; 63ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes if (cause != NULL) { 644f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes exception = env->NewObject(exceptionClass, ctor3, detailMessage.get(), error, cause); 65ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } else { 664f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes exception = env->NewObject(exceptionClass, ctor2, detailMessage.get(), error); 67ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } 68ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes env->Throw(reinterpret_cast<jthrowable>(exception)); 69dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes} 70dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes 714f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwErrnoException(JNIEnv* env, const char* functionName) { 724f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes int error = errno; 734f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes static jmethodID ctor3 = env->GetMethodID(JniConstants::errnoExceptionClass, 744f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V"); 754f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes static jmethodID ctor2 = env->GetMethodID(JniConstants::errnoExceptionClass, 764f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes "<init>", "(Ljava/lang/String;I)V"); 774f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwException(env, JniConstants::errnoExceptionClass, ctor3, ctor2, functionName, error); 784f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 794f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 804f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwGaiException(JNIEnv* env, const char* functionName, int error) { 814f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes if (error == EAI_SYSTEM) { 824f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // EAI_SYSTEM means "look at errno instead", so we want our GaiException to have the 834f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // relevant ErrnoException as its cause. 844f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwErrnoException(env, functionName); 854f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // Deliberately fall through to throw another exception... 864f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes } 874f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes static jmethodID ctor3 = env->GetMethodID(JniConstants::gaiExceptionClass, 884f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V"); 894f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes static jmethodID ctor2 = env->GetMethodID(JniConstants::gaiExceptionClass, 904f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes "<init>", "(Ljava/lang/String;I)V"); 914f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwException(env, JniConstants::gaiExceptionClass, ctor3, ctor2, functionName, error); 924f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 934f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 94dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughestemplate <typename rc_t> 957e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic rc_t throwIfMinusOne(JNIEnv* env, const char* name, rc_t rc) { 96dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == rc_t(-1)) { 977e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, name); 98dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes } 99dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes return rc; 10047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 10147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 1028dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughestemplate <typename ScopedT> 103bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesclass IoVec { 104bbac92e691de7d570928ddfba639067978e55b06Elliott Hughespublic: 105bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes IoVec(JNIEnv* env, size_t bufferCount) : mEnv(env), mBufferCount(bufferCount) { 106bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 107bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 108bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes bool init(jobjectArray javaBuffers, jintArray javaOffsets, jintArray javaByteCounts) { 109bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes // We can't delete our local references until after the I/O, so make sure we have room. 110bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (mEnv->PushLocalFrame(mBufferCount + 16) < 0) { 111bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 112bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 113bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes ScopedIntArrayRO offsets(mEnv, javaOffsets); 114bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (offsets.get() == NULL) { 115bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 116bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 117bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes ScopedIntArrayRO byteCounts(mEnv, javaByteCounts); 118bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (byteCounts.get() == NULL) { 119bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 120bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 121bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes // TODO: Linux actually has a 1024 buffer limit. glibc works around this, and we should too. 122bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes for (size_t i = 0; i < mBufferCount; ++i) { 123bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes jobject buffer = mEnv->GetObjectArrayElement(javaBuffers, i); 1248dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes mScopedBuffers.push_back(new ScopedT(mEnv, buffer)); 1258dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes jbyte* ptr = const_cast<jbyte*>(mScopedBuffers.back()->get()); 126bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (ptr == NULL) { 127bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 128bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 129bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes struct iovec iov; 130bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes iov.iov_base = reinterpret_cast<void*>(ptr + offsets[i]); 131bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes iov.iov_len = byteCounts[i]; 132bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes mIoVec.push_back(iov); 133bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 134bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return true; 135bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 136bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 137bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes ~IoVec() { 1388dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes for (size_t i = 0; i < mScopedBuffers.size(); ++i) { 1398dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes delete mScopedBuffers[i]; 140bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 141bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes mEnv->PopLocalFrame(NULL); 142bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 143bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 144bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes iovec* get() { 145bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return &mIoVec[0]; 146bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 147bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 148bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes size_t size() { 149bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return mBufferCount; 150bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 151bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 152bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesprivate: 153bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes JNIEnv* mEnv; 154bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes size_t mBufferCount; 155bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes std::vector<iovec> mIoVec; 1568dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes std::vector<ScopedT*> mScopedBuffers; 157bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes}; 158bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 1590a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeInetSocketAddress(JNIEnv* env, const sockaddr_storage* ss, int port) { 1600a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes jobject inetAddress = socketAddressToInetAddress(env, ss); 1610a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (inetAddress == NULL) { 1620a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 1630a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 1640a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::inetSocketAddressClass, "<init>", 1650a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes "(Ljava/net/InetAddress;I)V"); 1660a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return env->NewObject(JniConstants::inetSocketAddressClass, ctor, inetAddress, port); 1670a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 1680a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 1690a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeSocketAddress(JNIEnv* env, const sockaddr_storage* ss) { 1700a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (ss->ss_family == AF_INET) { 1710a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int port = ntohs(reinterpret_cast<const sockaddr_in*>(ss)->sin_port); 1720a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return makeInetSocketAddress(env, ss, port); 1730a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } else if (ss->ss_family == AF_INET6) { 1740a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int port = ntohs(reinterpret_cast<const sockaddr_in6*>(ss)->sin6_port); 1750a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return makeInetSocketAddress(env, ss, port); 1760a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 1770a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes // TODO: support AF_UNIX and AF_UNSPEC, and have some other behavior for other families 1780a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 1790a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 1800a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 18147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject makeStructStat(JNIEnv* env, const struct stat& sb) { 18247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structStatClass, "<init>", 18347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes "(JJIJIIJJJJJJJ)V"); 18447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return env->NewObject(JniConstants::structStatClass, ctor, 18559fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_dev), static_cast<jlong>(sb.st_ino), 18659fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jint>(sb.st_mode), static_cast<jlong>(sb.st_nlink), 18759fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jint>(sb.st_uid), static_cast<jint>(sb.st_gid), 18859fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_rdev), static_cast<jlong>(sb.st_size), 18959fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_atime), static_cast<jlong>(sb.st_mtime), 19059fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_ctime), static_cast<jlong>(sb.st_blksize), 19159fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_blocks)); 19259fa7163774d6930a174bc038414a4b780581957Elliott Hughes} 19359fa7163774d6930a174bc038414a4b780581957Elliott Hughes 19459fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject makeStructStatFs(JNIEnv* env, const struct statfs& sb) { 19559fa7163774d6930a174bc038414a4b780581957Elliott Hughes STATIC_ASSERT(sizeof(sb.f_bavail) == sizeof(jlong), statfs_not_64_bit); 19659fa7163774d6930a174bc038414a4b780581957Elliott Hughes STATIC_ASSERT(sizeof(sb.f_bfree) == sizeof(jlong), statfs_not_64_bit); 19759fa7163774d6930a174bc038414a4b780581957Elliott Hughes STATIC_ASSERT(sizeof(sb.f_blocks) == sizeof(jlong), statfs_not_64_bit); 19859fa7163774d6930a174bc038414a4b780581957Elliott Hughes 19959fa7163774d6930a174bc038414a4b780581957Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structStatFsClass, "<init>", 20059fa7163774d6930a174bc038414a4b780581957Elliott Hughes "(JJJJJJJJ)V"); 20159fa7163774d6930a174bc038414a4b780581957Elliott Hughes return env->NewObject(JniConstants::structStatFsClass, ctor, static_cast<jlong>(sb.f_bsize), 20259fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.f_blocks), static_cast<jlong>(sb.f_bfree), 20359fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.f_bavail), static_cast<jlong>(sb.f_files), 20459fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.f_ffree), static_cast<jlong>(sb.f_namelen), 20559fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.f_frsize)); 20647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 20747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 2080a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructLinger(JNIEnv* env, const struct linger& l) { 2090a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structLingerClass, "<init>", "(II)V"); 2100a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return env->NewObject(JniConstants::structLingerClass, ctor, l.l_onoff, l.l_linger); 2110a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 2120a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 2130a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructTimeval(JNIEnv* env, const struct timeval& tv) { 2140a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structTimevalClass, "<init>", "(JJ)V"); 2150a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return env->NewObject(JniConstants::structTimevalClass, ctor, 2160a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static_cast<jlong>(tv.tv_sec), static_cast<jlong>(tv.tv_usec)); 2170a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 2180a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 2197341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject makeStructUtsname(JNIEnv* env, const struct utsname& buf) { 2207341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#define TO_JAVA_STRING(NAME) \ 2217341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes jstring NAME = env->NewStringUTF(buf. NAME); \ 2227341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes if (NAME == NULL) return NULL; 2237341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes 2247341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes TO_JAVA_STRING(sysname); 2257341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes TO_JAVA_STRING(nodename); 2267341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes TO_JAVA_STRING(release); 2277341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes TO_JAVA_STRING(version); 2287341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes TO_JAVA_STRING(machine); 2297341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#undef TO_JAVA_STRING 2307341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes 2317341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structUtsnameClass, "<init>", 2327341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); 2337341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes return env->NewObject(JniConstants::structUtsnameClass, ctor, 2347341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes sysname, nodename, release, version, machine); 2357341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes}; 2367341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes 237a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic bool fillIfreq(JNIEnv* env, jstring javaInterfaceName, struct ifreq& req) { 238a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes ScopedUtfChars interfaceName(env, javaInterfaceName); 239a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (interfaceName.c_str() == NULL) { 240a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return false; 241a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes } 242a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes memset(&req, 0, sizeof(req)); 243a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes strncpy(req.ifr_name, interfaceName.c_str(), sizeof(req.ifr_name)); 244a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes req.ifr_name[sizeof(req.ifr_name) - 1] = '\0'; 245a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return true; 246a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes} 247a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes 24847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject doStat(JNIEnv* env, jstring javaPath, bool isLstat) { 24947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes ScopedUtfChars path(env, javaPath); 25047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes if (path.c_str() == NULL) { 25147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return NULL; 25247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes } 25347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes struct stat sb; 25447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int rc = isLstat ? TEMP_FAILURE_RETRY(lstat(path.c_str(), &sb)) 25547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes : TEMP_FAILURE_RETRY(stat(path.c_str(), &sb)); 256dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == -1) { 2577e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, isLstat ? "lstat" : "stat"); 25847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return NULL; 25947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes } 26047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return makeStructStat(env, sb); 261ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 262ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 263ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jboolean Posix_access(JNIEnv* env, jobject, jstring javaPath, jint mode) { 264ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes ScopedUtfChars path(env, javaPath); 265ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes if (path.c_str() == NULL) { 266ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return JNI_FALSE; 267ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } 26847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int rc = TEMP_FAILURE_RETRY(access(path.c_str(), mode)); 269dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == -1) { 2707e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "access"); 271dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes } 272ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return (rc == 0); 273ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 274ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 275b7190190e0ef8de883c952efb319ce7748831faaElliott Hughesstatic void Posix_chmod(JNIEnv* env, jobject, jstring javaPath, jint mode) { 276b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes ScopedUtfChars path(env, javaPath); 277b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes if (path.c_str() == NULL) { 278b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes return; 279b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes } 280b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes throwIfMinusOne(env, "chmod", TEMP_FAILURE_RETRY(chmod(path.c_str(), mode))); 281b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes} 282b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes 283462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughesstatic void Posix_close(JNIEnv* env, jobject, jobject javaFd) { 284462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // Get the FileDescriptor's 'fd' field and clear it. 285462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // We need to do this before we can throw an IOException (http://b/3222087). 286462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 287462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes jniSetFileDescriptorOfFD(env, javaFd, -1); 288462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes 289462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // Even if close(2) fails with EINTR, the fd will have been closed. 290462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone else's fd. 291462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html 292462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes throwIfMinusOne(env, "close", close(fd)); 293462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes} 294462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes 295ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jobjectArray Posix_environ(JNIEnv* env, jobject) { 296ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes extern char** environ; // Standard, but not in any header file. 297ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return toStringArray(env, environ); 298ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 299ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 300fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlVoid(JNIEnv* env, jobject, jobject javaFd, jint cmd) { 301fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 302fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd))); 303fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes} 304fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 305fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlLong(JNIEnv* env, jobject, jobject javaFd, jint cmd, jlong arg) { 306fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 307fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, arg))); 308fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes} 309fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 310fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlFlock(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaFlock) { 311fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID typeFid = env->GetFieldID(JniConstants::structFlockClass, "l_type", "S"); 312fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID whenceFid = env->GetFieldID(JniConstants::structFlockClass, "l_whence", "S"); 313fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID startFid = env->GetFieldID(JniConstants::structFlockClass, "l_start", "J"); 314fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID lenFid = env->GetFieldID(JniConstants::structFlockClass, "l_len", "J"); 315fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID pidFid = env->GetFieldID(JniConstants::structFlockClass, "l_pid", "I"); 316fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 317fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes struct flock64 lock; 318fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes memset(&lock, 0, sizeof(lock)); 319fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_type = env->GetShortField(javaFlock, typeFid); 320fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_whence = env->GetShortField(javaFlock, whenceFid); 321fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_start = env->GetLongField(javaFlock, startFid); 322fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_len = env->GetLongField(javaFlock, lenFid); 323fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_pid = env->GetIntField(javaFlock, pidFid); 324fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 325fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 326fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes int rc = throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, &lock))); 327fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes if (rc != -1) { 328fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetShortField(javaFlock, typeFid, lock.l_type); 329fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetShortField(javaFlock, whenceFid, lock.l_whence); 330fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetLongField(javaFlock, startFid, lock.l_start); 331fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetLongField(javaFlock, lenFid, lock.l_len); 332fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetIntField(javaFlock, pidFid, lock.l_pid); 333fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes } 334fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes return rc; 335fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes} 336fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 33752724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fdatasync(JNIEnv* env, jobject, jobject javaFd) { 33852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 3397e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "fdatasync", TEMP_FAILURE_RETRY(fdatasync(fd))); 34052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes} 34152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes 34247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_fstat(JNIEnv* env, jobject, jobject javaFd) { 34347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 34447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes struct stat sb; 34547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int rc = TEMP_FAILURE_RETRY(fstat(fd, &sb)); 346dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == -1) { 3477e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "fstat"); 34847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return NULL; 34947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes } 35047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return makeStructStat(env, sb); 35147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 35247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 35359fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject Posix_fstatfs(JNIEnv* env, jobject, jobject javaFd) { 35459fa7163774d6930a174bc038414a4b780581957Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 35559fa7163774d6930a174bc038414a4b780581957Elliott Hughes struct statfs sb; 35659fa7163774d6930a174bc038414a4b780581957Elliott Hughes int rc = TEMP_FAILURE_RETRY(fstatfs(fd, &sb)); 35759fa7163774d6930a174bc038414a4b780581957Elliott Hughes if (rc == -1) { 35859fa7163774d6930a174bc038414a4b780581957Elliott Hughes throwErrnoException(env, "fstatfs"); 35959fa7163774d6930a174bc038414a4b780581957Elliott Hughes return NULL; 36059fa7163774d6930a174bc038414a4b780581957Elliott Hughes } 36159fa7163774d6930a174bc038414a4b780581957Elliott Hughes return makeStructStatFs(env, sb); 36259fa7163774d6930a174bc038414a4b780581957Elliott Hughes} 36359fa7163774d6930a174bc038414a4b780581957Elliott Hughes 36452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fsync(JNIEnv* env, jobject, jobject javaFd) { 36552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 3667e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "fsync", TEMP_FAILURE_RETRY(fsync(fd))); 367f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes} 368f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes 369f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughesstatic void Posix_ftruncate(JNIEnv* env, jobject, jobject javaFd, jlong length) { 370f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 3717e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "ftruncate", TEMP_FAILURE_RETRY(ftruncate64(fd, length))); 37252724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes} 37352724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes 3744f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic jstring Posix_gai_strerror(JNIEnv* env, jobject, jint error) { 3754f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return env->NewStringUTF(gai_strerror(error)); 3764f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 3774f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 378ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic jstring Posix_getenv(JNIEnv* env, jobject, jstring javaName) { 379ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes ScopedUtfChars name(env, javaName); 380ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes if (name.c_str() == NULL) { 381ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes return NULL; 382ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes } 383ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes return env->NewStringUTF(getenv(name.c_str())); 384ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes} 385ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 3864f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic jstring Posix_getnameinfo(JNIEnv* env, jobject, jobject javaAddress, jint flags) { 3874f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes sockaddr_storage ss; 3884f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes if (!inetAddressToSocketAddress(env, javaAddress, 0, &ss)) { 3894f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return NULL; 3904f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes } 3914f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // TODO: bionic's getnameinfo(3) seems to want its length parameter to be exactly 3924f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // sizeof(sockaddr_in) for an IPv4 address and sizeof (sockaddr_in6) for an 3934f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // IPv6 address. Fix getnameinfo so it accepts sizeof(sockaddr_storage), and 3944f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // then remove this hack. 3954f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes socklen_t size = (ss.ss_family == AF_INET) ? sizeof(sockaddr_in) : sizeof(sockaddr_in6); 3964f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes char buf[NI_MAXHOST]; // NI_MAXHOST is longer than INET6_ADDRSTRLEN. 3974f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes int rc = getnameinfo(reinterpret_cast<sockaddr*>(&ss), size, buf, sizeof(buf), NULL, 0, flags); 3984f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes if (rc != 0) { 3994f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwGaiException(env, "getnameinfo", rc); 4004f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return NULL; 4014f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes } 4024f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return env->NewStringUTF(buf); 4034f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 4044f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 4050a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockname(JNIEnv* env, jobject, jobject javaFd) { 4060a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 4070a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes sockaddr_storage ss; 4080a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes sockaddr* sa = reinterpret_cast<sockaddr*>(&ss); 4090a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t byteCount = sizeof(ss); 4100a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes memset(&ss, 0, byteCount); 4110a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int rc = TEMP_FAILURE_RETRY(getsockname(fd, sa, &byteCount)); 4120a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (rc == -1) { 4130a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwErrnoException(env, "getsockname"); 4140a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 4150a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 4160a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return makeSocketAddress(env, &ss); 4170a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 4180a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 4190a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 4200a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 4210a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes u_char result = 0; 4220a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(result); 4230a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size))); 4240a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return result; 4250a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 4260a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 4270a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptInAddr(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 4280a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 4290a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes sockaddr_storage ss; 4300a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes memset(&ss, 0, sizeof(ss)); 4310a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes ss.ss_family = AF_INET; // This is only for the IPv4-only IP_MULTICAST_IF. 4320a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes sockaddr_in* sa = reinterpret_cast<sockaddr_in*>(&ss); 4330a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(sa->sin_addr); 4340a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &sa->sin_addr, &size)); 4350a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (rc == -1) { 4360a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwErrnoException(env, "getsockopt"); 4370a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 4380a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 4390a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return socketAddressToInetAddress(env, &ss); 4400a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 4410a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 4420a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 4430a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 4440a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes jint result = 0; 4450a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(result); 4460a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size))); 4470a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return result; 4480a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 4490a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 4500a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 4510a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 4520a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes struct linger l; 4530a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(l); 4540a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes memset(&l, 0, size); 4550a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &l, &size)); 4560a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (rc == -1) { 4570a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwErrnoException(env, "getsockopt"); 4580a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 4590a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 4600a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return makeStructLinger(env, l); 4610a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 4620a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 4630a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 4640a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 4650a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes struct timeval tv; 4660a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(tv); 4670a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes memset(&tv, 0, size); 4680a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &tv, &size)); 4690a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (rc == -1) { 4700a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwErrnoException(env, "getsockopt"); 4710a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 4720a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 4730a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return makeStructTimeval(env, tv); 4740a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 4750a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 476a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic jstring Posix_if_indextoname(JNIEnv* env, jobject, jint index) { 477a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes char buf[IF_NAMESIZE]; 478a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes char* name = if_indextoname(index, buf); 479a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes // if_indextoname(3) returns NULL on failure, which will come out of NewStringUTF unscathed. 480a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes // There's no useful information in errno, so we don't bother throwing. Callers can null-check. 481a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return env->NewStringUTF(name); 482a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes} 483a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes 484a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic jobject Posix_ioctlInetAddress(JNIEnv* env, jobject, jobject javaFd, jint cmd, jstring javaInterfaceName) { 485a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes struct ifreq req; 486a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (!fillIfreq(env, javaInterfaceName, req)) { 487a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return NULL; 488a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes } 489a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 490a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &req))); 491a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (rc == -1) { 492a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return NULL; 493a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes } 494a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return socketAddressToInetAddress(env, reinterpret_cast<sockaddr_storage*>(&req.ifr_addr)); 495a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes} 496a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes 4978b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jint Posix_ioctlInt(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaArg) { 4988b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes // This is complicated because ioctls may return their result by updating their argument 4998b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes // or via their return value, so we need to support both. 500461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 5018b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes static jfieldID valueFid = env->GetFieldID(JniConstants::mutableIntClass, "value", "I"); 5028b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes jint arg = env->GetIntField(javaArg, valueFid); 5038b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &arg))); 5048b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes if (!env->ExceptionCheck()) { 5058b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes env->SetIntField(javaArg, valueFid, arg); 5068b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes } 5078b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes return rc; 508461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes} 509461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes 5109a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughesstatic jboolean Posix_isatty(JNIEnv* env, jobject, jobject javaFd) { 5119a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 5129a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes return TEMP_FAILURE_RETRY(isatty(fd)) == 0; 5139a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes} 5149a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes 515e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughesstatic void Posix_listen(JNIEnv* env, jobject, jobject javaFd, jint backlog) { 516e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 517e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes throwIfMinusOne(env, "listen", TEMP_FAILURE_RETRY(listen(fd, backlog))); 518e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes} 519e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes 520dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughesstatic jlong Posix_lseek(JNIEnv* env, jobject, jobject javaFd, jlong offset, jint whence) { 521dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 5227e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes return throwIfMinusOne(env, "lseek", TEMP_FAILURE_RETRY(lseek64(fd, offset, whence))); 523dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes} 524dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes 52547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_lstat(JNIEnv* env, jobject, jstring javaPath) { 52647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return doStat(env, javaPath, true); 52747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 52847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 5290f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughesstatic void Posix_mincore(JNIEnv* env, jobject, jlong address, jlong byteCount, jbyteArray javaVector) { 5300f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes ScopedByteArrayRW vector(env, javaVector); 5310f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes if (vector.get() == NULL) { 5320f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes return; 5330f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes } 5340f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 5350f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes unsigned char* vec = reinterpret_cast<unsigned char*>(vector.get()); 5360f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes throwIfMinusOne(env, "mincore", TEMP_FAILURE_RETRY(mincore(ptr, byteCount, vec))); 5370f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes} 5380f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes 539c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_mkdir(JNIEnv* env, jobject, jstring javaPath, jint mode) { 540c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes ScopedUtfChars path(env, javaPath); 541c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes if (path.c_str() == NULL) { 542c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes return; 543c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes } 544c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes throwIfMinusOne(env, "mkdir", TEMP_FAILURE_RETRY(mkdir(path.c_str(), mode))); 545c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes} 546c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes 5477e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_mlock(JNIEnv* env, jobject, jlong address, jlong byteCount) { 5487e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 5497e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes throwIfMinusOne(env, "mlock", TEMP_FAILURE_RETRY(mlock(ptr, byteCount))); 5507e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes} 5517e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes 5527e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic jlong Posix_mmap(JNIEnv* env, jobject, jlong address, jlong byteCount, jint prot, jint flags, jobject javaFd, jlong offset) { 5537e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 5547e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* suggestedPtr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 5557e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* ptr = mmap(suggestedPtr, byteCount, prot, flags, fd, offset); 5567e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes if (ptr == MAP_FAILED) { 5577e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "mmap"); 5587e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes } 5597e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes return static_cast<jlong>(reinterpret_cast<uintptr_t>(ptr)); 5607e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes} 5617e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes 5627e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_msync(JNIEnv* env, jobject, jlong address, jlong byteCount, jint flags) { 5637e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 5647e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "msync", TEMP_FAILURE_RETRY(msync(ptr, byteCount, flags))); 5657e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes} 5667e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes 5677e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_munlock(JNIEnv* env, jobject, jlong address, jlong byteCount) { 5687e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 5697e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes throwIfMinusOne(env, "munlock", TEMP_FAILURE_RETRY(munlock(ptr, byteCount))); 5707e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes} 5717e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes 5727e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_munmap(JNIEnv* env, jobject, jlong address, jlong byteCount) { 5737e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 5747e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "munmap", TEMP_FAILURE_RETRY(munmap(ptr, byteCount))); 5757e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes} 5767e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes 5770ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughesstatic jobject Posix_open(JNIEnv* env, jobject, jstring javaPath, jint flags, jint mode) { 5780ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes ScopedUtfChars path(env, javaPath); 5790ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes if (path.c_str() == NULL) { 5800ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes return NULL; 5810ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes } 5820ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes int fd = throwIfMinusOne(env, "open", TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode))); 5830ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL; 5840ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes} 5850ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes 58641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughesstatic jobjectArray Posix_pipe(JNIEnv* env, jobject) { 58741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes int fds[2]; 58841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes throwIfMinusOne(env, "pipe", TEMP_FAILURE_RETRY(pipe(&fds[0]))); 58941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes jobjectArray result = env->NewObjectArray(2, JniConstants::fileDescriptorClass, NULL); 59041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes if (result == NULL) { 59141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return NULL; 59241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 59341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes for (int i = 0; i < 2; ++i) { 59441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes ScopedLocalRef<jobject> fd(env, jniCreateFileDescriptor(env, fds[i])); 59541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes if (fd.get() == NULL) { 59641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return NULL; 59741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 59841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes env->SetObjectArrayElement(result, i, fd.get()); 59941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes if (env->ExceptionCheck()) { 60041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return NULL; 60141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 60241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 60341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return result; 60441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes} 60541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes 6060568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughesstatic jint Posix_readBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount) { 6070568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes ScopedBytesRW bytes(env, javaBytes); 60826c7025a7a919044771fb89031161bd26fe03032Elliott Hughes if (bytes.get() == NULL) { 60926c7025a7a919044771fb89031161bd26fe03032Elliott Hughes return -1; 61026c7025a7a919044771fb89031161bd26fe03032Elliott Hughes } 61126c7025a7a919044771fb89031161bd26fe03032Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 6120568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes return throwIfMinusOne(env, "read", TEMP_FAILURE_RETRY(read(fd, bytes.get() + byteOffset, byteCount))); 61326c7025a7a919044771fb89031161bd26fe03032Elliott Hughes} 61426c7025a7a919044771fb89031161bd26fe03032Elliott Hughes 615bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_readv(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) { 6168dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes IoVec<ScopedBytesRW> ioVec(env, env->GetArrayLength(buffers)); 617bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (!ioVec.init(buffers, offsets, byteCounts)) { 618bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return -1; 619bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 620bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 621bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return throwIfMinusOne(env, "readv", TEMP_FAILURE_RETRY(readv(fd, ioVec.get(), ioVec.size()))); 622bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes} 623bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 624c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_remove(JNIEnv* env, jobject, jstring javaPath) { 625c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes ScopedUtfChars path(env, javaPath); 626c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes if (path.c_str() == NULL) { 627c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes return; 628c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes } 629c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes throwIfMinusOne(env, "remove", TEMP_FAILURE_RETRY(remove(path.c_str()))); 630c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes} 631c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes 632a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_rename(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) { 633a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars oldPath(env, javaOldPath); 634a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (oldPath.c_str() == NULL) { 635a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 636a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 637a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars newPath(env, javaNewPath); 638a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (newPath.c_str() == NULL) { 639a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 640a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 641a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes throwIfMinusOne(env, "rename", TEMP_FAILURE_RETRY(rename(oldPath.c_str(), newPath.c_str()))); 642a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes} 643a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes 6448b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jlong Posix_sendfile(JNIEnv* env, jobject, jobject javaOutFd, jobject javaInFd, jobject javaOffset, jlong byteCount) { 6458b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes int outFd = jniGetFDFromFileDescriptor(env, javaOutFd); 6468b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes int inFd = jniGetFDFromFileDescriptor(env, javaInFd); 6478b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes static jfieldID valueFid = env->GetFieldID(JniConstants::mutableLongClass, "value", "J"); 6488b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes off_t offset = 0; 6498b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes off_t* offsetPtr = NULL; 6508b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes if (javaOffset != NULL) { 6518b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes // TODO: fix bionic so we can have a 64-bit off_t! 6528b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes offset = env->GetLongField(javaOffset, valueFid); 6538b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes offsetPtr = &offset; 6548b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes } 6558b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes jlong result = throwIfMinusOne(env, "sendfile", TEMP_FAILURE_RETRY(sendfile(outFd, inFd, offsetPtr, byteCount))); 6568b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes if (javaOffset != NULL) { 6578b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes env->SetLongField(javaOffset, valueFid, offset); 6588b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes } 6598b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes return result; 6608b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes} 6618b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes 662c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 663c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 664c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes u_char byte = value; 665c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &byte, sizeof(byte)))); 666c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes} 667c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes 668a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic void Posix_setsockoptIfreq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jstring javaInterfaceName) { 669a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes struct ifreq req; 670a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (!fillIfreq(env, javaInterfaceName, req)) { 671b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes return; 672b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes } 673b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 674b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req)))); 675b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes} 676b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes 677454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic void Posix_setsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 678454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 679454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 680454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes} 681454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes 682b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughesstatic void Posix_setsockoptIpMreqn(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 683b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes ip_mreqn req; 684b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes memset(&req, 0, sizeof(req)); 685b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes req.imr_ifindex = value; 686b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 687b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req)))); 688b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes} 689b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes 690438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughesstatic void Posix_setsockoptGroupReq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaGroupReq) { 691438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes struct group_req value; 692438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes 693438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes static jfieldID grInterfaceFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_interface", "I"); 694438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes value.gr_interface = env->GetIntField(javaGroupReq, grInterfaceFid); 695438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes // Get the IPv4 or IPv6 multicast address to join or leave. 696438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes static jfieldID grGroupFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_group", "Ljava/net/InetAddress;"); 697438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes jobject javaGroup = env->GetObjectField(javaGroupReq, grGroupFid); 698438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes if (!inetAddressToSocketAddress(env, javaGroup, 0, &value.gr_group)) { 699438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes return; 700438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes } 701438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes 702438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 703438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes int rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))); 704438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes if (rc == -1 && errno == EINVAL) { 705438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes // Maybe we're a 32-bit binary talking to a 64-bit kernel? 706438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes // glibc doesn't automatically handle this. 707438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes struct GCC_HIDDEN group_req64 { 708438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes uint32_t gr_interface; 709438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes uint32_t my_padding; 710438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes sockaddr_storage gr_group; 711438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes }; 712438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes group_req64 value64; 713438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes value64.gr_interface = value.gr_interface; 714438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes memcpy(&value64.gr_group, &value.gr_group, sizeof(value.gr_group)); 715438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value64, sizeof(value64))); 716438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes } 717438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes throwIfMinusOne(env, "setsockopt", rc); 718438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes} 719438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes 720c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaLinger) { 721c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID lOnoffFid = env->GetFieldID(JniConstants::structLingerClass, "l_onoff", "I"); 722c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID lLingerFid = env->GetFieldID(JniConstants::structLingerClass, "l_linger", "I"); 723c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 724c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes struct linger value; 725c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes value.l_onoff = env->GetIntField(javaLinger, lOnoffFid); 726c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes value.l_linger = env->GetIntField(javaLinger, lLingerFid); 727c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 728c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes} 729c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes 730c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaTimeval) { 731c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID tvSecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_sec", "J"); 732c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID tvUsecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_usec", "J"); 733c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 734c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes struct timeval value; 735c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes value.tv_sec = env->GetIntField(javaTimeval, tvSecFid); 736c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes value.tv_usec = env->GetIntField(javaTimeval, tvUsecFid); 737c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 738c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes} 739c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes 74059e4744d27231f260271dbbca406e0cc39768116Elliott Hughesstatic void Posix_shutdown(JNIEnv* env, jobject, jobject javaFd, jint how) { 74159e4744d27231f260271dbbca406e0cc39768116Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 74259e4744d27231f260271dbbca406e0cc39768116Elliott Hughes throwIfMinusOne(env, "shutdown", TEMP_FAILURE_RETRY(shutdown(fd, how))); 74359e4744d27231f260271dbbca406e0cc39768116Elliott Hughes} 74459e4744d27231f260271dbbca406e0cc39768116Elliott Hughes 745454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic jobject Posix_socket(JNIEnv* env, jobject, jint domain, jint type, jint protocol) { 746454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes int fd = throwIfMinusOne(env, "socket", TEMP_FAILURE_RETRY(socket(domain, type, protocol))); 747454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL; 748454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes} 749454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes 75047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_stat(JNIEnv* env, jobject, jstring javaPath) { 75147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return doStat(env, javaPath, false); 75247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 75347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 7540ade5524d972b9dfd69b0126aa4c35319c1b7fa4Elliott Hughesstatic jobject Posix_statfs(JNIEnv* env, jobject, jstring javaPath) { 75559fa7163774d6930a174bc038414a4b780581957Elliott Hughes ScopedUtfChars path(env, javaPath); 75659fa7163774d6930a174bc038414a4b780581957Elliott Hughes if (path.c_str() == NULL) { 75759fa7163774d6930a174bc038414a4b780581957Elliott Hughes return NULL; 75859fa7163774d6930a174bc038414a4b780581957Elliott Hughes } 75959fa7163774d6930a174bc038414a4b780581957Elliott Hughes struct statfs sb; 76059fa7163774d6930a174bc038414a4b780581957Elliott Hughes int rc = TEMP_FAILURE_RETRY(statfs(path.c_str(), &sb)); 76159fa7163774d6930a174bc038414a4b780581957Elliott Hughes if (rc == -1) { 76259fa7163774d6930a174bc038414a4b780581957Elliott Hughes throwErrnoException(env, "statfs"); 76359fa7163774d6930a174bc038414a4b780581957Elliott Hughes return NULL; 76459fa7163774d6930a174bc038414a4b780581957Elliott Hughes } 76559fa7163774d6930a174bc038414a4b780581957Elliott Hughes return makeStructStatFs(env, sb); 76659fa7163774d6930a174bc038414a4b780581957Elliott Hughes} 76759fa7163774d6930a174bc038414a4b780581957Elliott Hughes 768ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jstring Posix_strerror(JNIEnv* env, jobject, jint errnum) { 76952724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes char buffer[BUFSIZ]; 770ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes const char* message = jniStrError(errnum, buffer, sizeof(buffer)); 771ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return env->NewStringUTF(message); 772ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 773ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 774a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_symlink(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) { 775a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars oldPath(env, javaOldPath); 776a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (oldPath.c_str() == NULL) { 777a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 778a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 779a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars newPath(env, javaNewPath); 780a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (newPath.c_str() == NULL) { 781a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 782a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 783a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes throwIfMinusOne(env, "symlink", TEMP_FAILURE_RETRY(symlink(oldPath.c_str(), newPath.c_str()))); 784a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes} 785a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes 7866fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughesstatic jlong Posix_sysconf(JNIEnv* env, jobject, jint name) { 7876fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes // Since -1 is a valid result from sysconf(3), detecting failure is a little more awkward. 7886fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes errno = 0; 7896fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes long result = sysconf(name); 7906fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes if (result == -1L && errno == EINVAL) { 7917e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "sysconf"); 7926fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes } 7936fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes return result; 7946fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes} 7956fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes 7967341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject Posix_uname(JNIEnv* env, jobject) { 7977341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes struct utsname buf; 7987341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes if (throwIfMinusOne(env, "uname", TEMP_FAILURE_RETRY(uname(&buf))) == -1) { 7997341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes return NULL; 8007341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes } 8017341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes return makeStructUtsname(env, buf); 8027341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes} 8037341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes 8040568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughesstatic jint Posix_writeBytes(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount) { 8050568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes ScopedBytesRO bytes(env, javaBytes); 80678c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes if (bytes.get() == NULL) { 80778c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes return -1; 80878c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes } 80978c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 8100568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes return throwIfMinusOne(env, "write", TEMP_FAILURE_RETRY(write(fd, bytes.get() + byteOffset, byteCount))); 81178c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes} 81278c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes 813bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_writev(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) { 8148dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes IoVec<ScopedBytesRO> ioVec(env, env->GetArrayLength(buffers)); 815bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (!ioVec.init(buffers, offsets, byteCounts)) { 816bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return -1; 817bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 818bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 819bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return throwIfMinusOne(env, "writev", TEMP_FAILURE_RETRY(writev(fd, ioVec.get(), ioVec.size()))); 820bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes} 821bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 822ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic JNINativeMethod gMethods[] = { 823ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes NATIVE_METHOD(Posix, access, "(Ljava/lang/String;I)Z"), 824b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes NATIVE_METHOD(Posix, chmod, "(Ljava/lang/String;I)V"), 825462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes NATIVE_METHOD(Posix, close, "(Ljava/io/FileDescriptor;)V"), 826ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes NATIVE_METHOD(Posix, environ, "()[Ljava/lang/String;"), 827fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes NATIVE_METHOD(Posix, fcntlVoid, "(Ljava/io/FileDescriptor;I)I"), 828fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes NATIVE_METHOD(Posix, fcntlLong, "(Ljava/io/FileDescriptor;IJ)I"), 829fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes NATIVE_METHOD(Posix, fcntlFlock, "(Ljava/io/FileDescriptor;ILlibcore/io/StructFlock;)I"), 83052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes NATIVE_METHOD(Posix, fdatasync, "(Ljava/io/FileDescriptor;)V"), 83147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes NATIVE_METHOD(Posix, fstat, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStat;"), 83259fa7163774d6930a174bc038414a4b780581957Elliott Hughes NATIVE_METHOD(Posix, fstatfs, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStatFs;"), 83352724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes NATIVE_METHOD(Posix, fsync, "(Ljava/io/FileDescriptor;)V"), 834f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes NATIVE_METHOD(Posix, ftruncate, "(Ljava/io/FileDescriptor;J)V"), 8354f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes NATIVE_METHOD(Posix, gai_strerror, "(I)Ljava/lang/String;"), 836ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes NATIVE_METHOD(Posix, getenv, "(Ljava/lang/String;)Ljava/lang/String;"), 8374f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes NATIVE_METHOD(Posix, getnameinfo, "(Ljava/net/InetAddress;I)Ljava/lang/String;"), 8380a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockname, "(Ljava/io/FileDescriptor;)Ljava/net/SocketAddress;"), 8390a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptByte, "(Ljava/io/FileDescriptor;II)I"), 8400a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptInAddr, "(Ljava/io/FileDescriptor;II)Ljava/net/InetAddress;"), 8410a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptInt, "(Ljava/io/FileDescriptor;II)I"), 8420a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptLinger, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructLinger;"), 8430a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptTimeval, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructTimeval;"), 844a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes NATIVE_METHOD(Posix, if_indextoname, "(I)Ljava/lang/String;"), 845a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes NATIVE_METHOD(Posix, ioctlInetAddress, "(Ljava/io/FileDescriptor;ILjava/lang/String;)Ljava/net/InetAddress;"), 8468b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes NATIVE_METHOD(Posix, ioctlInt, "(Ljava/io/FileDescriptor;ILlibcore/util/MutableInt;)I"), 8479a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes NATIVE_METHOD(Posix, isatty, "(Ljava/io/FileDescriptor;)Z"), 848e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes NATIVE_METHOD(Posix, listen, "(Ljava/io/FileDescriptor;I)V"), 849dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes NATIVE_METHOD(Posix, lseek, "(Ljava/io/FileDescriptor;JI)J"), 85047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes NATIVE_METHOD(Posix, lstat, "(Ljava/lang/String;)Llibcore/io/StructStat;"), 8510f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes NATIVE_METHOD(Posix, mincore, "(JJ[B)V"), 852c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes NATIVE_METHOD(Posix, mkdir, "(Ljava/lang/String;I)V"), 8537e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes NATIVE_METHOD(Posix, mlock, "(JJ)V"), 8547e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes NATIVE_METHOD(Posix, mmap, "(JJIILjava/io/FileDescriptor;J)J"), 8557e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes NATIVE_METHOD(Posix, msync, "(JJI)V"), 8567e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes NATIVE_METHOD(Posix, munlock, "(JJ)V"), 8577e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes NATIVE_METHOD(Posix, munmap, "(JJ)V"), 8580ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes NATIVE_METHOD(Posix, open, "(Ljava/lang/String;II)Ljava/io/FileDescriptor;"), 85941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes NATIVE_METHOD(Posix, pipe, "()[Ljava/io/FileDescriptor;"), 8600568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes NATIVE_METHOD(Posix, readBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"), 861bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes NATIVE_METHOD(Posix, readv, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"), 862c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes NATIVE_METHOD(Posix, remove, "(Ljava/lang/String;)V"), 863a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes NATIVE_METHOD(Posix, rename, "(Ljava/lang/String;Ljava/lang/String;)V"), 8648b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes NATIVE_METHOD(Posix, sendfile, "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Llibcore/util/MutableLong;J)J"), 865c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes NATIVE_METHOD(Posix, setsockoptByte, "(Ljava/io/FileDescriptor;III)V"), 866b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes NATIVE_METHOD(Posix, setsockoptIfreq, "(Ljava/io/FileDescriptor;IILjava/lang/String;)V"), 867454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes NATIVE_METHOD(Posix, setsockoptInt, "(Ljava/io/FileDescriptor;III)V"), 868b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes NATIVE_METHOD(Posix, setsockoptIpMreqn, "(Ljava/io/FileDescriptor;III)V"), 869438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes NATIVE_METHOD(Posix, setsockoptGroupReq, "(Ljava/io/FileDescriptor;IILlibcore/io/StructGroupReq;)V"), 870c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes NATIVE_METHOD(Posix, setsockoptLinger, "(Ljava/io/FileDescriptor;IILlibcore/io/StructLinger;)V"), 871c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes NATIVE_METHOD(Posix, setsockoptTimeval, "(Ljava/io/FileDescriptor;IILlibcore/io/StructTimeval;)V"), 87259e4744d27231f260271dbbca406e0cc39768116Elliott Hughes NATIVE_METHOD(Posix, shutdown, "(Ljava/io/FileDescriptor;I)V"), 873454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes NATIVE_METHOD(Posix, socket, "(III)Ljava/io/FileDescriptor;"), 87447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes NATIVE_METHOD(Posix, stat, "(Ljava/lang/String;)Llibcore/io/StructStat;"), 87559fa7163774d6930a174bc038414a4b780581957Elliott Hughes NATIVE_METHOD(Posix, statfs, "(Ljava/lang/String;)Llibcore/io/StructStatFs;"), 876ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes NATIVE_METHOD(Posix, strerror, "(I)Ljava/lang/String;"), 877a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes NATIVE_METHOD(Posix, symlink, "(Ljava/lang/String;Ljava/lang/String;)V"), 8786fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes NATIVE_METHOD(Posix, sysconf, "(I)J"), 8797341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes NATIVE_METHOD(Posix, uname, "()Llibcore/io/StructUtsname;"), 8800568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes NATIVE_METHOD(Posix, writeBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"), 881bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes NATIVE_METHOD(Posix, writev, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"), 882ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes}; 883ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesint register_libcore_io_Posix(JNIEnv* env) { 884ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes return jniRegisterNativeMethods(env, "libcore/io/Posix", gMethods, NELEM(gMethods)); 885ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes} 886