libcore_io_Linux.cpp revision a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3c
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> 34a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes#include <signal.h> 35ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include <stdlib.h> 36461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes#include <sys/ioctl.h> 377e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes#include <sys/mman.h> 388b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes#include <sys/sendfile.h> 3959e4744d27231f260271dbbca406e0cc39768116Elliott Hughes#include <sys/socket.h> 4047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes#include <sys/stat.h> 410a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include <sys/time.h> 420ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <sys/types.h> 43bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes#include <sys/uio.h> 447341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#include <sys/utsname.h> 4559fa7163774d6930a174bc038414a4b780581957Elliott Hughes#include <sys/vfs.h> // Bionic doesn't have <sys/statvfs.h> 460ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <unistd.h> 47ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 484f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwException(JNIEnv* env, jclass exceptionClass, jmethodID ctor3, jmethodID ctor2, 494f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes const char* functionName, int error) { 50ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes jthrowable cause = NULL; 51ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes if (env->ExceptionCheck()) { 52ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes cause = env->ExceptionOccurred(); 53ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes env->ExceptionClear(); 54ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } 55ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 564f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName)); 574f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes if (detailMessage.get() == NULL) { 58f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes // Not really much we can do here. We're probably dead in the water, 59f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes // but let's try to stumble on... 60f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes env->ExceptionClear(); 61f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes } 62f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes 63ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes jobject exception; 64ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes if (cause != NULL) { 654f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes exception = env->NewObject(exceptionClass, ctor3, detailMessage.get(), error, cause); 66ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } else { 674f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes exception = env->NewObject(exceptionClass, ctor2, detailMessage.get(), error); 68ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } 69ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes env->Throw(reinterpret_cast<jthrowable>(exception)); 70dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes} 71dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes 724f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwErrnoException(JNIEnv* env, const char* functionName) { 734f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes int error = errno; 744f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes static jmethodID ctor3 = env->GetMethodID(JniConstants::errnoExceptionClass, 754f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V"); 764f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes static jmethodID ctor2 = env->GetMethodID(JniConstants::errnoExceptionClass, 774f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes "<init>", "(Ljava/lang/String;I)V"); 784f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwException(env, JniConstants::errnoExceptionClass, ctor3, ctor2, functionName, error); 794f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 804f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 814f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwGaiException(JNIEnv* env, const char* functionName, int error) { 824f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes if (error == EAI_SYSTEM) { 834f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // EAI_SYSTEM means "look at errno instead", so we want our GaiException to have the 844f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // relevant ErrnoException as its cause. 854f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwErrnoException(env, functionName); 864f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // Deliberately fall through to throw another exception... 874f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes } 884f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes static jmethodID ctor3 = env->GetMethodID(JniConstants::gaiExceptionClass, 894f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V"); 904f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes static jmethodID ctor2 = env->GetMethodID(JniConstants::gaiExceptionClass, 914f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes "<init>", "(Ljava/lang/String;I)V"); 924f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwException(env, JniConstants::gaiExceptionClass, ctor3, ctor2, functionName, error); 934f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 944f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 95dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughestemplate <typename rc_t> 967e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic rc_t throwIfMinusOne(JNIEnv* env, const char* name, rc_t rc) { 97dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == rc_t(-1)) { 987e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, name); 99dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes } 100dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes return rc; 10147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 10247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 1038dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughestemplate <typename ScopedT> 104bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesclass IoVec { 105bbac92e691de7d570928ddfba639067978e55b06Elliott Hughespublic: 106bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes IoVec(JNIEnv* env, size_t bufferCount) : mEnv(env), mBufferCount(bufferCount) { 107bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 108bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 109bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes bool init(jobjectArray javaBuffers, jintArray javaOffsets, jintArray javaByteCounts) { 110bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes // We can't delete our local references until after the I/O, so make sure we have room. 111bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (mEnv->PushLocalFrame(mBufferCount + 16) < 0) { 112bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 113bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 114bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes ScopedIntArrayRO offsets(mEnv, javaOffsets); 115bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (offsets.get() == NULL) { 116bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 117bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 118bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes ScopedIntArrayRO byteCounts(mEnv, javaByteCounts); 119bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (byteCounts.get() == NULL) { 120bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 121bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 122bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes // TODO: Linux actually has a 1024 buffer limit. glibc works around this, and we should too. 123bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes for (size_t i = 0; i < mBufferCount; ++i) { 124bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes jobject buffer = mEnv->GetObjectArrayElement(javaBuffers, i); 1258dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes mScopedBuffers.push_back(new ScopedT(mEnv, buffer)); 1268dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes jbyte* ptr = const_cast<jbyte*>(mScopedBuffers.back()->get()); 127bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (ptr == NULL) { 128bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 129bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 130bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes struct iovec iov; 131bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes iov.iov_base = reinterpret_cast<void*>(ptr + offsets[i]); 132bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes iov.iov_len = byteCounts[i]; 133bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes mIoVec.push_back(iov); 134bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 135bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return true; 136bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 137bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 138bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes ~IoVec() { 1398dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes for (size_t i = 0; i < mScopedBuffers.size(); ++i) { 1408dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes delete mScopedBuffers[i]; 141bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 142bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes mEnv->PopLocalFrame(NULL); 143bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 144bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 145bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes iovec* get() { 146bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return &mIoVec[0]; 147bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 148bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 149bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes size_t size() { 150bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return mBufferCount; 151bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 152bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 153bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesprivate: 154bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes JNIEnv* mEnv; 155bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes size_t mBufferCount; 156bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes std::vector<iovec> mIoVec; 1578dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes std::vector<ScopedT*> mScopedBuffers; 158bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes}; 159bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 1600a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeInetSocketAddress(JNIEnv* env, const sockaddr_storage* ss, int port) { 1610a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes jobject inetAddress = socketAddressToInetAddress(env, ss); 1620a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (inetAddress == NULL) { 1630a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 1640a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 1650a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::inetSocketAddressClass, "<init>", 1660a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes "(Ljava/net/InetAddress;I)V"); 1670a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return env->NewObject(JniConstants::inetSocketAddressClass, ctor, inetAddress, port); 1680a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 1690a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 1700a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeSocketAddress(JNIEnv* env, const sockaddr_storage* ss) { 1710a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (ss->ss_family == AF_INET) { 1720a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int port = ntohs(reinterpret_cast<const sockaddr_in*>(ss)->sin_port); 1730a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return makeInetSocketAddress(env, ss, port); 1740a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } else if (ss->ss_family == AF_INET6) { 1750a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int port = ntohs(reinterpret_cast<const sockaddr_in6*>(ss)->sin6_port); 1760a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return makeInetSocketAddress(env, ss, port); 1770a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 1780a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes // TODO: support AF_UNIX and AF_UNSPEC, and have some other behavior for other families 1790a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 1800a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 1810a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 18247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject makeStructStat(JNIEnv* env, const struct stat& sb) { 18347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structStatClass, "<init>", 18447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes "(JJIJIIJJJJJJJ)V"); 18547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return env->NewObject(JniConstants::structStatClass, ctor, 18659fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_dev), static_cast<jlong>(sb.st_ino), 18759fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jint>(sb.st_mode), static_cast<jlong>(sb.st_nlink), 18859fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jint>(sb.st_uid), static_cast<jint>(sb.st_gid), 18959fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_rdev), static_cast<jlong>(sb.st_size), 19059fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_atime), static_cast<jlong>(sb.st_mtime), 19159fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_ctime), static_cast<jlong>(sb.st_blksize), 19259fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_blocks)); 19359fa7163774d6930a174bc038414a4b780581957Elliott Hughes} 19459fa7163774d6930a174bc038414a4b780581957Elliott Hughes 19559fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject makeStructStatFs(JNIEnv* env, const struct statfs& sb) { 19659fa7163774d6930a174bc038414a4b780581957Elliott Hughes STATIC_ASSERT(sizeof(sb.f_bavail) == sizeof(jlong), statfs_not_64_bit); 19759fa7163774d6930a174bc038414a4b780581957Elliott Hughes STATIC_ASSERT(sizeof(sb.f_bfree) == sizeof(jlong), statfs_not_64_bit); 19859fa7163774d6930a174bc038414a4b780581957Elliott Hughes STATIC_ASSERT(sizeof(sb.f_blocks) == sizeof(jlong), statfs_not_64_bit); 19959fa7163774d6930a174bc038414a4b780581957Elliott Hughes 20059fa7163774d6930a174bc038414a4b780581957Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structStatFsClass, "<init>", 20159fa7163774d6930a174bc038414a4b780581957Elliott Hughes "(JJJJJJJJ)V"); 20259fa7163774d6930a174bc038414a4b780581957Elliott Hughes return env->NewObject(JniConstants::structStatFsClass, ctor, static_cast<jlong>(sb.f_bsize), 20359fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.f_blocks), static_cast<jlong>(sb.f_bfree), 20459fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.f_bavail), static_cast<jlong>(sb.f_files), 20559fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.f_ffree), static_cast<jlong>(sb.f_namelen), 20659fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.f_frsize)); 20747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 20847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 2090a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructLinger(JNIEnv* env, const struct linger& l) { 2100a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structLingerClass, "<init>", "(II)V"); 2110a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return env->NewObject(JniConstants::structLingerClass, ctor, l.l_onoff, l.l_linger); 2120a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 2130a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 2140a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructTimeval(JNIEnv* env, const struct timeval& tv) { 2150a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structTimevalClass, "<init>", "(JJ)V"); 2160a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return env->NewObject(JniConstants::structTimevalClass, ctor, 2170a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static_cast<jlong>(tv.tv_sec), static_cast<jlong>(tv.tv_usec)); 2180a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 2190a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 2207341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject makeStructUtsname(JNIEnv* env, const struct utsname& buf) { 2217341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#define TO_JAVA_STRING(NAME) \ 2227341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes jstring NAME = env->NewStringUTF(buf. NAME); \ 2237341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes if (NAME == NULL) return NULL; 2247341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes 2257341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes TO_JAVA_STRING(sysname); 2267341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes TO_JAVA_STRING(nodename); 2277341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes TO_JAVA_STRING(release); 2287341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes TO_JAVA_STRING(version); 2297341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes TO_JAVA_STRING(machine); 2307341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#undef TO_JAVA_STRING 2317341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes 2327341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structUtsnameClass, "<init>", 2337341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); 2347341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes return env->NewObject(JniConstants::structUtsnameClass, ctor, 2357341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes sysname, nodename, release, version, machine); 2367341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes}; 2377341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes 238a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic bool fillIfreq(JNIEnv* env, jstring javaInterfaceName, struct ifreq& req) { 239a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes ScopedUtfChars interfaceName(env, javaInterfaceName); 240a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (interfaceName.c_str() == NULL) { 241a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return false; 242a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes } 243a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes memset(&req, 0, sizeof(req)); 244a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes strncpy(req.ifr_name, interfaceName.c_str(), sizeof(req.ifr_name)); 245a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes req.ifr_name[sizeof(req.ifr_name) - 1] = '\0'; 246a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return true; 247a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes} 248a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes 24947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject doStat(JNIEnv* env, jstring javaPath, bool isLstat) { 25047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes ScopedUtfChars path(env, javaPath); 25147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes if (path.c_str() == NULL) { 25247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return NULL; 25347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes } 25447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes struct stat sb; 25547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int rc = isLstat ? TEMP_FAILURE_RETRY(lstat(path.c_str(), &sb)) 25647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes : TEMP_FAILURE_RETRY(stat(path.c_str(), &sb)); 257dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == -1) { 2587e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, isLstat ? "lstat" : "stat"); 25947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return NULL; 26047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes } 26147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return makeStructStat(env, sb); 262ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 263ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 264ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jboolean Posix_access(JNIEnv* env, jobject, jstring javaPath, jint mode) { 265ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes ScopedUtfChars path(env, javaPath); 266ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes if (path.c_str() == NULL) { 267ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return JNI_FALSE; 268ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } 26947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int rc = TEMP_FAILURE_RETRY(access(path.c_str(), mode)); 270dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == -1) { 2717e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "access"); 272dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes } 273ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return (rc == 0); 274ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 275ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 276b7190190e0ef8de883c952efb319ce7748831faaElliott Hughesstatic void Posix_chmod(JNIEnv* env, jobject, jstring javaPath, jint mode) { 277b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes ScopedUtfChars path(env, javaPath); 278b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes if (path.c_str() == NULL) { 279b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes return; 280b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes } 281b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes throwIfMinusOne(env, "chmod", TEMP_FAILURE_RETRY(chmod(path.c_str(), mode))); 282b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes} 283b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes 284462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughesstatic void Posix_close(JNIEnv* env, jobject, jobject javaFd) { 285462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // Get the FileDescriptor's 'fd' field and clear it. 286462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // We need to do this before we can throw an IOException (http://b/3222087). 287462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 288462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes jniSetFileDescriptorOfFD(env, javaFd, -1); 289462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes 290462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // Even if close(2) fails with EINTR, the fd will have been closed. 291462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone else's fd. 292462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html 293462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes throwIfMinusOne(env, "close", close(fd)); 294462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes} 295462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes 296ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jobjectArray Posix_environ(JNIEnv* env, jobject) { 297ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes extern char** environ; // Standard, but not in any header file. 298ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return toStringArray(env, environ); 299ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 300ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 301fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlVoid(JNIEnv* env, jobject, jobject javaFd, jint cmd) { 302fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 303fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd))); 304fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes} 305fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 306fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlLong(JNIEnv* env, jobject, jobject javaFd, jint cmd, jlong arg) { 307fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 308fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, arg))); 309fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes} 310fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 311fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlFlock(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaFlock) { 312fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID typeFid = env->GetFieldID(JniConstants::structFlockClass, "l_type", "S"); 313fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID whenceFid = env->GetFieldID(JniConstants::structFlockClass, "l_whence", "S"); 314fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID startFid = env->GetFieldID(JniConstants::structFlockClass, "l_start", "J"); 315fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID lenFid = env->GetFieldID(JniConstants::structFlockClass, "l_len", "J"); 316fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID pidFid = env->GetFieldID(JniConstants::structFlockClass, "l_pid", "I"); 317fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 318fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes struct flock64 lock; 319fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes memset(&lock, 0, sizeof(lock)); 320fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_type = env->GetShortField(javaFlock, typeFid); 321fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_whence = env->GetShortField(javaFlock, whenceFid); 322fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_start = env->GetLongField(javaFlock, startFid); 323fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_len = env->GetLongField(javaFlock, lenFid); 324fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_pid = env->GetIntField(javaFlock, pidFid); 325fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 326fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 327fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes int rc = throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, &lock))); 328fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes if (rc != -1) { 329fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetShortField(javaFlock, typeFid, lock.l_type); 330fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetShortField(javaFlock, whenceFid, lock.l_whence); 331fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetLongField(javaFlock, startFid, lock.l_start); 332fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetLongField(javaFlock, lenFid, lock.l_len); 333fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetIntField(javaFlock, pidFid, lock.l_pid); 334fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes } 335fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes return rc; 336fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes} 337fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 33852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fdatasync(JNIEnv* env, jobject, jobject javaFd) { 33952724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 3407e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "fdatasync", TEMP_FAILURE_RETRY(fdatasync(fd))); 34152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes} 34252724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes 34347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_fstat(JNIEnv* env, jobject, jobject javaFd) { 34447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 34547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes struct stat sb; 34647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int rc = TEMP_FAILURE_RETRY(fstat(fd, &sb)); 347dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == -1) { 3487e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "fstat"); 34947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return NULL; 35047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes } 35147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return makeStructStat(env, sb); 35247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 35347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 35459fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject Posix_fstatfs(JNIEnv* env, jobject, jobject javaFd) { 35559fa7163774d6930a174bc038414a4b780581957Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 35659fa7163774d6930a174bc038414a4b780581957Elliott Hughes struct statfs sb; 35759fa7163774d6930a174bc038414a4b780581957Elliott Hughes int rc = TEMP_FAILURE_RETRY(fstatfs(fd, &sb)); 35859fa7163774d6930a174bc038414a4b780581957Elliott Hughes if (rc == -1) { 35959fa7163774d6930a174bc038414a4b780581957Elliott Hughes throwErrnoException(env, "fstatfs"); 36059fa7163774d6930a174bc038414a4b780581957Elliott Hughes return NULL; 36159fa7163774d6930a174bc038414a4b780581957Elliott Hughes } 36259fa7163774d6930a174bc038414a4b780581957Elliott Hughes return makeStructStatFs(env, sb); 36359fa7163774d6930a174bc038414a4b780581957Elliott Hughes} 36459fa7163774d6930a174bc038414a4b780581957Elliott Hughes 36552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fsync(JNIEnv* env, jobject, jobject javaFd) { 36652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 3677e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "fsync", TEMP_FAILURE_RETRY(fsync(fd))); 368f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes} 369f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes 370f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughesstatic void Posix_ftruncate(JNIEnv* env, jobject, jobject javaFd, jlong length) { 371f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 3727e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "ftruncate", TEMP_FAILURE_RETRY(ftruncate64(fd, length))); 37352724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes} 37452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes 3754f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic jstring Posix_gai_strerror(JNIEnv* env, jobject, jint error) { 3764f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return env->NewStringUTF(gai_strerror(error)); 3774f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 3784f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 379ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic jstring Posix_getenv(JNIEnv* env, jobject, jstring javaName) { 380ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes ScopedUtfChars name(env, javaName); 381ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes if (name.c_str() == NULL) { 382ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes return NULL; 383ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes } 384ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes return env->NewStringUTF(getenv(name.c_str())); 385ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes} 386ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 3874f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic jstring Posix_getnameinfo(JNIEnv* env, jobject, jobject javaAddress, jint flags) { 3884f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes sockaddr_storage ss; 3894f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes if (!inetAddressToSocketAddress(env, javaAddress, 0, &ss)) { 3904f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return NULL; 3914f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes } 3924f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // TODO: bionic's getnameinfo(3) seems to want its length parameter to be exactly 3934f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // sizeof(sockaddr_in) for an IPv4 address and sizeof (sockaddr_in6) for an 3944f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // IPv6 address. Fix getnameinfo so it accepts sizeof(sockaddr_storage), and 3954f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // then remove this hack. 3964f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes socklen_t size = (ss.ss_family == AF_INET) ? sizeof(sockaddr_in) : sizeof(sockaddr_in6); 3974f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes char buf[NI_MAXHOST]; // NI_MAXHOST is longer than INET6_ADDRSTRLEN. 3984f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes int rc = getnameinfo(reinterpret_cast<sockaddr*>(&ss), size, buf, sizeof(buf), NULL, 0, flags); 3994f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes if (rc != 0) { 4004f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwGaiException(env, "getnameinfo", rc); 4014f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return NULL; 4024f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes } 4034f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return env->NewStringUTF(buf); 4044f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 4054f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 4060a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockname(JNIEnv* env, jobject, jobject javaFd) { 4070a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 4080a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes sockaddr_storage ss; 4090a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes sockaddr* sa = reinterpret_cast<sockaddr*>(&ss); 4100a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t byteCount = sizeof(ss); 4110a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes memset(&ss, 0, byteCount); 4120a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int rc = TEMP_FAILURE_RETRY(getsockname(fd, sa, &byteCount)); 4130a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (rc == -1) { 4140a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwErrnoException(env, "getsockname"); 4150a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 4160a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 4170a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return makeSocketAddress(env, &ss); 4180a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 4190a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 4200a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 4210a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 4220a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes u_char result = 0; 4230a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(result); 4240a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size))); 4250a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return result; 4260a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 4270a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 4280a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptInAddr(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 4290a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 4300a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes sockaddr_storage ss; 4310a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes memset(&ss, 0, sizeof(ss)); 4320a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes ss.ss_family = AF_INET; // This is only for the IPv4-only IP_MULTICAST_IF. 4330a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes sockaddr_in* sa = reinterpret_cast<sockaddr_in*>(&ss); 4340a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(sa->sin_addr); 4350a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &sa->sin_addr, &size)); 4360a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (rc == -1) { 4370a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwErrnoException(env, "getsockopt"); 4380a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 4390a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 4400a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return socketAddressToInetAddress(env, &ss); 4410a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 4420a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 4430a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 4440a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 4450a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes jint result = 0; 4460a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(result); 4470a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size))); 4480a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return result; 4490a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 4500a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 4510a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 4520a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 4530a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes struct linger l; 4540a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(l); 4550a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes memset(&l, 0, size); 4560a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &l, &size)); 4570a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (rc == -1) { 4580a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwErrnoException(env, "getsockopt"); 4590a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 4600a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 4610a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return makeStructLinger(env, l); 4620a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 4630a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 4640a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 4650a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 4660a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes struct timeval tv; 4670a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(tv); 4680a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes memset(&tv, 0, size); 4690a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &tv, &size)); 4700a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (rc == -1) { 4710a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwErrnoException(env, "getsockopt"); 4720a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 4730a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 4740a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return makeStructTimeval(env, tv); 4750a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 4760a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 477a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic jstring Posix_if_indextoname(JNIEnv* env, jobject, jint index) { 478a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes char buf[IF_NAMESIZE]; 479a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes char* name = if_indextoname(index, buf); 480a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes // if_indextoname(3) returns NULL on failure, which will come out of NewStringUTF unscathed. 481a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes // There's no useful information in errno, so we don't bother throwing. Callers can null-check. 482a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return env->NewStringUTF(name); 483a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes} 484a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes 485a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic jobject Posix_ioctlInetAddress(JNIEnv* env, jobject, jobject javaFd, jint cmd, jstring javaInterfaceName) { 486a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes struct ifreq req; 487a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (!fillIfreq(env, javaInterfaceName, req)) { 488a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return NULL; 489a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes } 490a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 491a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &req))); 492a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (rc == -1) { 493a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return NULL; 494a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes } 495a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return socketAddressToInetAddress(env, reinterpret_cast<sockaddr_storage*>(&req.ifr_addr)); 496a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes} 497a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes 4988b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jint Posix_ioctlInt(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaArg) { 4998b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes // This is complicated because ioctls may return their result by updating their argument 5008b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes // or via their return value, so we need to support both. 501461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 5028b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes static jfieldID valueFid = env->GetFieldID(JniConstants::mutableIntClass, "value", "I"); 5038b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes jint arg = env->GetIntField(javaArg, valueFid); 5048b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &arg))); 5058b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes if (!env->ExceptionCheck()) { 5068b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes env->SetIntField(javaArg, valueFid, arg); 5078b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes } 5088b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes return rc; 509461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes} 510461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes 5119a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughesstatic jboolean Posix_isatty(JNIEnv* env, jobject, jobject javaFd) { 5129a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 5139a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes return TEMP_FAILURE_RETRY(isatty(fd)) == 0; 5149a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes} 5159a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes 516a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughesstatic void Posix_kill(JNIEnv* env, jobject, jint pid, jint sig) { 517a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes throwIfMinusOne(env, "kill", TEMP_FAILURE_RETRY(kill(pid, sig))); 518a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes} 519a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes 520e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughesstatic void Posix_listen(JNIEnv* env, jobject, jobject javaFd, jint backlog) { 521e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 522e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes throwIfMinusOne(env, "listen", TEMP_FAILURE_RETRY(listen(fd, backlog))); 523e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes} 524e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes 525dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughesstatic jlong Posix_lseek(JNIEnv* env, jobject, jobject javaFd, jlong offset, jint whence) { 526dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 5277e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes return throwIfMinusOne(env, "lseek", TEMP_FAILURE_RETRY(lseek64(fd, offset, whence))); 528dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes} 529dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes 53047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_lstat(JNIEnv* env, jobject, jstring javaPath) { 53147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return doStat(env, javaPath, true); 53247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 53347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 5340f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughesstatic void Posix_mincore(JNIEnv* env, jobject, jlong address, jlong byteCount, jbyteArray javaVector) { 5350f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes ScopedByteArrayRW vector(env, javaVector); 5360f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes if (vector.get() == NULL) { 5370f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes return; 5380f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes } 5390f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 5400f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes unsigned char* vec = reinterpret_cast<unsigned char*>(vector.get()); 5410f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes throwIfMinusOne(env, "mincore", TEMP_FAILURE_RETRY(mincore(ptr, byteCount, vec))); 5420f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes} 5430f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes 544c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_mkdir(JNIEnv* env, jobject, jstring javaPath, jint mode) { 545c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes ScopedUtfChars path(env, javaPath); 546c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes if (path.c_str() == NULL) { 547c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes return; 548c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes } 549c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes throwIfMinusOne(env, "mkdir", TEMP_FAILURE_RETRY(mkdir(path.c_str(), mode))); 550c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes} 551c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes 5527e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_mlock(JNIEnv* env, jobject, jlong address, jlong byteCount) { 5537e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 5547e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes throwIfMinusOne(env, "mlock", TEMP_FAILURE_RETRY(mlock(ptr, byteCount))); 5557e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes} 5567e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes 5577e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic jlong Posix_mmap(JNIEnv* env, jobject, jlong address, jlong byteCount, jint prot, jint flags, jobject javaFd, jlong offset) { 5587e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 5597e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* suggestedPtr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 5607e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* ptr = mmap(suggestedPtr, byteCount, prot, flags, fd, offset); 5617e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes if (ptr == MAP_FAILED) { 5627e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "mmap"); 5637e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes } 5647e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes return static_cast<jlong>(reinterpret_cast<uintptr_t>(ptr)); 5657e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes} 5667e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes 5677e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_msync(JNIEnv* env, jobject, jlong address, jlong byteCount, jint flags) { 5687e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 5697e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "msync", TEMP_FAILURE_RETRY(msync(ptr, byteCount, flags))); 5707e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes} 5717e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes 5727e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_munlock(JNIEnv* env, jobject, jlong address, jlong byteCount) { 5737e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 5747e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes throwIfMinusOne(env, "munlock", TEMP_FAILURE_RETRY(munlock(ptr, byteCount))); 5757e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes} 5767e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes 5777e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_munmap(JNIEnv* env, jobject, jlong address, jlong byteCount) { 5787e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 5797e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "munmap", TEMP_FAILURE_RETRY(munmap(ptr, byteCount))); 5807e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes} 5817e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes 5820ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughesstatic jobject Posix_open(JNIEnv* env, jobject, jstring javaPath, jint flags, jint mode) { 5830ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes ScopedUtfChars path(env, javaPath); 5840ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes if (path.c_str() == NULL) { 5850ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes return NULL; 5860ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes } 5870ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes int fd = throwIfMinusOne(env, "open", TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode))); 5880ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL; 5890ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes} 5900ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes 59141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughesstatic jobjectArray Posix_pipe(JNIEnv* env, jobject) { 59241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes int fds[2]; 59341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes throwIfMinusOne(env, "pipe", TEMP_FAILURE_RETRY(pipe(&fds[0]))); 59441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes jobjectArray result = env->NewObjectArray(2, JniConstants::fileDescriptorClass, NULL); 59541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes if (result == NULL) { 59641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return NULL; 59741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 59841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes for (int i = 0; i < 2; ++i) { 59941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes ScopedLocalRef<jobject> fd(env, jniCreateFileDescriptor(env, fds[i])); 60041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes if (fd.get() == NULL) { 60141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return NULL; 60241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 60341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes env->SetObjectArrayElement(result, i, fd.get()); 60441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes if (env->ExceptionCheck()) { 60541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return NULL; 60641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 60741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 60841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return result; 60941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes} 61041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes 6110568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughesstatic jint Posix_readBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount) { 6120568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes ScopedBytesRW bytes(env, javaBytes); 61326c7025a7a919044771fb89031161bd26fe03032Elliott Hughes if (bytes.get() == NULL) { 61426c7025a7a919044771fb89031161bd26fe03032Elliott Hughes return -1; 61526c7025a7a919044771fb89031161bd26fe03032Elliott Hughes } 61626c7025a7a919044771fb89031161bd26fe03032Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 6170568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes return throwIfMinusOne(env, "read", TEMP_FAILURE_RETRY(read(fd, bytes.get() + byteOffset, byteCount))); 61826c7025a7a919044771fb89031161bd26fe03032Elliott Hughes} 61926c7025a7a919044771fb89031161bd26fe03032Elliott Hughes 620bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_readv(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) { 6218dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes IoVec<ScopedBytesRW> ioVec(env, env->GetArrayLength(buffers)); 622bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (!ioVec.init(buffers, offsets, byteCounts)) { 623bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return -1; 624bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 625bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 626bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return throwIfMinusOne(env, "readv", TEMP_FAILURE_RETRY(readv(fd, ioVec.get(), ioVec.size()))); 627bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes} 628bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 629c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_remove(JNIEnv* env, jobject, jstring javaPath) { 630c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes ScopedUtfChars path(env, javaPath); 631c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes if (path.c_str() == NULL) { 632c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes return; 633c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes } 634c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes throwIfMinusOne(env, "remove", TEMP_FAILURE_RETRY(remove(path.c_str()))); 635c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes} 636c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes 637a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_rename(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) { 638a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars oldPath(env, javaOldPath); 639a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (oldPath.c_str() == NULL) { 640a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 641a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 642a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars newPath(env, javaNewPath); 643a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (newPath.c_str() == NULL) { 644a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 645a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 646a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes throwIfMinusOne(env, "rename", TEMP_FAILURE_RETRY(rename(oldPath.c_str(), newPath.c_str()))); 647a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes} 648a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes 6498b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jlong Posix_sendfile(JNIEnv* env, jobject, jobject javaOutFd, jobject javaInFd, jobject javaOffset, jlong byteCount) { 6508b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes int outFd = jniGetFDFromFileDescriptor(env, javaOutFd); 6518b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes int inFd = jniGetFDFromFileDescriptor(env, javaInFd); 6528b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes static jfieldID valueFid = env->GetFieldID(JniConstants::mutableLongClass, "value", "J"); 6538b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes off_t offset = 0; 6548b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes off_t* offsetPtr = NULL; 6558b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes if (javaOffset != NULL) { 6568b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes // TODO: fix bionic so we can have a 64-bit off_t! 6578b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes offset = env->GetLongField(javaOffset, valueFid); 6588b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes offsetPtr = &offset; 6598b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes } 6608b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes jlong result = throwIfMinusOne(env, "sendfile", TEMP_FAILURE_RETRY(sendfile(outFd, inFd, offsetPtr, byteCount))); 6618b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes if (javaOffset != NULL) { 6628b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes env->SetLongField(javaOffset, valueFid, offset); 6638b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes } 6648b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes return result; 6658b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes} 6668b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes 667c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 668c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 669c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes u_char byte = value; 670c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &byte, sizeof(byte)))); 671c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes} 672c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes 673a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic void Posix_setsockoptIfreq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jstring javaInterfaceName) { 674a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes struct ifreq req; 675a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (!fillIfreq(env, javaInterfaceName, req)) { 676b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes return; 677b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes } 678b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 679b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req)))); 680b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes} 681b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes 682454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic void Posix_setsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 683454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 684454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 685454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes} 686454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes 687b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughesstatic void Posix_setsockoptIpMreqn(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 688b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes ip_mreqn req; 689b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes memset(&req, 0, sizeof(req)); 690b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes req.imr_ifindex = value; 691b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 692b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req)))); 693b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes} 694b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes 695438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughesstatic void Posix_setsockoptGroupReq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaGroupReq) { 696438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes struct group_req value; 697438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes 698438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes static jfieldID grInterfaceFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_interface", "I"); 699438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes value.gr_interface = env->GetIntField(javaGroupReq, grInterfaceFid); 700438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes // Get the IPv4 or IPv6 multicast address to join or leave. 701438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes static jfieldID grGroupFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_group", "Ljava/net/InetAddress;"); 702438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes jobject javaGroup = env->GetObjectField(javaGroupReq, grGroupFid); 703438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes if (!inetAddressToSocketAddress(env, javaGroup, 0, &value.gr_group)) { 704438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes return; 705438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes } 706438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes 707438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 708438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes int rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))); 709438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes if (rc == -1 && errno == EINVAL) { 710438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes // Maybe we're a 32-bit binary talking to a 64-bit kernel? 711438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes // glibc doesn't automatically handle this. 712438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes struct GCC_HIDDEN group_req64 { 713438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes uint32_t gr_interface; 714438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes uint32_t my_padding; 715438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes sockaddr_storage gr_group; 716438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes }; 717438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes group_req64 value64; 718438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes value64.gr_interface = value.gr_interface; 719438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes memcpy(&value64.gr_group, &value.gr_group, sizeof(value.gr_group)); 720438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value64, sizeof(value64))); 721438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes } 722438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes throwIfMinusOne(env, "setsockopt", rc); 723438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes} 724438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes 725c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaLinger) { 726c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID lOnoffFid = env->GetFieldID(JniConstants::structLingerClass, "l_onoff", "I"); 727c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID lLingerFid = env->GetFieldID(JniConstants::structLingerClass, "l_linger", "I"); 728c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 729c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes struct linger value; 730c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes value.l_onoff = env->GetIntField(javaLinger, lOnoffFid); 731c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes value.l_linger = env->GetIntField(javaLinger, lLingerFid); 732c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 733c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes} 734c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes 735c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaTimeval) { 736c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID tvSecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_sec", "J"); 737c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID tvUsecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_usec", "J"); 738c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 739c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes struct timeval value; 740c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes value.tv_sec = env->GetIntField(javaTimeval, tvSecFid); 741c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes value.tv_usec = env->GetIntField(javaTimeval, tvUsecFid); 742c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 743c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes} 744c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes 74559e4744d27231f260271dbbca406e0cc39768116Elliott Hughesstatic void Posix_shutdown(JNIEnv* env, jobject, jobject javaFd, jint how) { 74659e4744d27231f260271dbbca406e0cc39768116Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 74759e4744d27231f260271dbbca406e0cc39768116Elliott Hughes throwIfMinusOne(env, "shutdown", TEMP_FAILURE_RETRY(shutdown(fd, how))); 74859e4744d27231f260271dbbca406e0cc39768116Elliott Hughes} 74959e4744d27231f260271dbbca406e0cc39768116Elliott Hughes 750454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic jobject Posix_socket(JNIEnv* env, jobject, jint domain, jint type, jint protocol) { 751454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes int fd = throwIfMinusOne(env, "socket", TEMP_FAILURE_RETRY(socket(domain, type, protocol))); 752454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL; 753454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes} 754454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes 75547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_stat(JNIEnv* env, jobject, jstring javaPath) { 75647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return doStat(env, javaPath, false); 75747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 75847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 7590ade5524d972b9dfd69b0126aa4c35319c1b7fa4Elliott Hughesstatic jobject Posix_statfs(JNIEnv* env, jobject, jstring javaPath) { 76059fa7163774d6930a174bc038414a4b780581957Elliott Hughes ScopedUtfChars path(env, javaPath); 76159fa7163774d6930a174bc038414a4b780581957Elliott Hughes if (path.c_str() == NULL) { 76259fa7163774d6930a174bc038414a4b780581957Elliott Hughes return NULL; 76359fa7163774d6930a174bc038414a4b780581957Elliott Hughes } 76459fa7163774d6930a174bc038414a4b780581957Elliott Hughes struct statfs sb; 76559fa7163774d6930a174bc038414a4b780581957Elliott Hughes int rc = TEMP_FAILURE_RETRY(statfs(path.c_str(), &sb)); 76659fa7163774d6930a174bc038414a4b780581957Elliott Hughes if (rc == -1) { 76759fa7163774d6930a174bc038414a4b780581957Elliott Hughes throwErrnoException(env, "statfs"); 76859fa7163774d6930a174bc038414a4b780581957Elliott Hughes return NULL; 76959fa7163774d6930a174bc038414a4b780581957Elliott Hughes } 77059fa7163774d6930a174bc038414a4b780581957Elliott Hughes return makeStructStatFs(env, sb); 77159fa7163774d6930a174bc038414a4b780581957Elliott Hughes} 77259fa7163774d6930a174bc038414a4b780581957Elliott Hughes 773ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jstring Posix_strerror(JNIEnv* env, jobject, jint errnum) { 77452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes char buffer[BUFSIZ]; 775ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes const char* message = jniStrError(errnum, buffer, sizeof(buffer)); 776ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return env->NewStringUTF(message); 777ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 778ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 779a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_symlink(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) { 780a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars oldPath(env, javaOldPath); 781a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (oldPath.c_str() == NULL) { 782a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 783a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 784a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars newPath(env, javaNewPath); 785a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (newPath.c_str() == NULL) { 786a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 787a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 788a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes throwIfMinusOne(env, "symlink", TEMP_FAILURE_RETRY(symlink(oldPath.c_str(), newPath.c_str()))); 789a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes} 790a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes 7916fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughesstatic jlong Posix_sysconf(JNIEnv* env, jobject, jint name) { 7926fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes // Since -1 is a valid result from sysconf(3), detecting failure is a little more awkward. 7936fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes errno = 0; 7946fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes long result = sysconf(name); 7956fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes if (result == -1L && errno == EINVAL) { 7967e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "sysconf"); 7976fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes } 7986fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes return result; 7996fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes} 8006fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes 8017341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject Posix_uname(JNIEnv* env, jobject) { 8027341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes struct utsname buf; 8037341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes if (throwIfMinusOne(env, "uname", TEMP_FAILURE_RETRY(uname(&buf))) == -1) { 8047341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes return NULL; 8057341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes } 8067341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes return makeStructUtsname(env, buf); 8077341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes} 8087341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes 8090568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughesstatic jint Posix_writeBytes(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount) { 8100568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes ScopedBytesRO bytes(env, javaBytes); 81178c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes if (bytes.get() == NULL) { 81278c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes return -1; 81378c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes } 81478c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 8150568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes return throwIfMinusOne(env, "write", TEMP_FAILURE_RETRY(write(fd, bytes.get() + byteOffset, byteCount))); 81678c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes} 81778c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes 818bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_writev(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) { 8198dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes IoVec<ScopedBytesRO> ioVec(env, env->GetArrayLength(buffers)); 820bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (!ioVec.init(buffers, offsets, byteCounts)) { 821bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return -1; 822bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 823bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 824bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return throwIfMinusOne(env, "writev", TEMP_FAILURE_RETRY(writev(fd, ioVec.get(), ioVec.size()))); 825bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes} 826bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 827ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic JNINativeMethod gMethods[] = { 828ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes NATIVE_METHOD(Posix, access, "(Ljava/lang/String;I)Z"), 829b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes NATIVE_METHOD(Posix, chmod, "(Ljava/lang/String;I)V"), 830462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes NATIVE_METHOD(Posix, close, "(Ljava/io/FileDescriptor;)V"), 831ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes NATIVE_METHOD(Posix, environ, "()[Ljava/lang/String;"), 832fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes NATIVE_METHOD(Posix, fcntlVoid, "(Ljava/io/FileDescriptor;I)I"), 833fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes NATIVE_METHOD(Posix, fcntlLong, "(Ljava/io/FileDescriptor;IJ)I"), 834fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes NATIVE_METHOD(Posix, fcntlFlock, "(Ljava/io/FileDescriptor;ILlibcore/io/StructFlock;)I"), 83552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes NATIVE_METHOD(Posix, fdatasync, "(Ljava/io/FileDescriptor;)V"), 83647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes NATIVE_METHOD(Posix, fstat, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStat;"), 83759fa7163774d6930a174bc038414a4b780581957Elliott Hughes NATIVE_METHOD(Posix, fstatfs, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStatFs;"), 83852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes NATIVE_METHOD(Posix, fsync, "(Ljava/io/FileDescriptor;)V"), 839f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes NATIVE_METHOD(Posix, ftruncate, "(Ljava/io/FileDescriptor;J)V"), 8404f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes NATIVE_METHOD(Posix, gai_strerror, "(I)Ljava/lang/String;"), 841ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes NATIVE_METHOD(Posix, getenv, "(Ljava/lang/String;)Ljava/lang/String;"), 8424f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes NATIVE_METHOD(Posix, getnameinfo, "(Ljava/net/InetAddress;I)Ljava/lang/String;"), 8430a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockname, "(Ljava/io/FileDescriptor;)Ljava/net/SocketAddress;"), 8440a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptByte, "(Ljava/io/FileDescriptor;II)I"), 8450a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptInAddr, "(Ljava/io/FileDescriptor;II)Ljava/net/InetAddress;"), 8460a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptInt, "(Ljava/io/FileDescriptor;II)I"), 8470a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptLinger, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructLinger;"), 8480a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptTimeval, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructTimeval;"), 849a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes NATIVE_METHOD(Posix, if_indextoname, "(I)Ljava/lang/String;"), 850a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes NATIVE_METHOD(Posix, ioctlInetAddress, "(Ljava/io/FileDescriptor;ILjava/lang/String;)Ljava/net/InetAddress;"), 8518b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes NATIVE_METHOD(Posix, ioctlInt, "(Ljava/io/FileDescriptor;ILlibcore/util/MutableInt;)I"), 8529a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes NATIVE_METHOD(Posix, isatty, "(Ljava/io/FileDescriptor;)Z"), 853a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes NATIVE_METHOD(Posix, kill, "(II)V"), 854e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes NATIVE_METHOD(Posix, listen, "(Ljava/io/FileDescriptor;I)V"), 855dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes NATIVE_METHOD(Posix, lseek, "(Ljava/io/FileDescriptor;JI)J"), 85647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes NATIVE_METHOD(Posix, lstat, "(Ljava/lang/String;)Llibcore/io/StructStat;"), 8570f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes NATIVE_METHOD(Posix, mincore, "(JJ[B)V"), 858c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes NATIVE_METHOD(Posix, mkdir, "(Ljava/lang/String;I)V"), 8597e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes NATIVE_METHOD(Posix, mlock, "(JJ)V"), 8607e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes NATIVE_METHOD(Posix, mmap, "(JJIILjava/io/FileDescriptor;J)J"), 8617e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes NATIVE_METHOD(Posix, msync, "(JJI)V"), 8627e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes NATIVE_METHOD(Posix, munlock, "(JJ)V"), 8637e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes NATIVE_METHOD(Posix, munmap, "(JJ)V"), 8640ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes NATIVE_METHOD(Posix, open, "(Ljava/lang/String;II)Ljava/io/FileDescriptor;"), 86541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes NATIVE_METHOD(Posix, pipe, "()[Ljava/io/FileDescriptor;"), 8660568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes NATIVE_METHOD(Posix, readBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"), 867bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes NATIVE_METHOD(Posix, readv, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"), 868c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes NATIVE_METHOD(Posix, remove, "(Ljava/lang/String;)V"), 869a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes NATIVE_METHOD(Posix, rename, "(Ljava/lang/String;Ljava/lang/String;)V"), 8708b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes NATIVE_METHOD(Posix, sendfile, "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Llibcore/util/MutableLong;J)J"), 871c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes NATIVE_METHOD(Posix, setsockoptByte, "(Ljava/io/FileDescriptor;III)V"), 872b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes NATIVE_METHOD(Posix, setsockoptIfreq, "(Ljava/io/FileDescriptor;IILjava/lang/String;)V"), 873454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes NATIVE_METHOD(Posix, setsockoptInt, "(Ljava/io/FileDescriptor;III)V"), 874b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes NATIVE_METHOD(Posix, setsockoptIpMreqn, "(Ljava/io/FileDescriptor;III)V"), 875438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes NATIVE_METHOD(Posix, setsockoptGroupReq, "(Ljava/io/FileDescriptor;IILlibcore/io/StructGroupReq;)V"), 876c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes NATIVE_METHOD(Posix, setsockoptLinger, "(Ljava/io/FileDescriptor;IILlibcore/io/StructLinger;)V"), 877c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes NATIVE_METHOD(Posix, setsockoptTimeval, "(Ljava/io/FileDescriptor;IILlibcore/io/StructTimeval;)V"), 87859e4744d27231f260271dbbca406e0cc39768116Elliott Hughes NATIVE_METHOD(Posix, shutdown, "(Ljava/io/FileDescriptor;I)V"), 879454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes NATIVE_METHOD(Posix, socket, "(III)Ljava/io/FileDescriptor;"), 88047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes NATIVE_METHOD(Posix, stat, "(Ljava/lang/String;)Llibcore/io/StructStat;"), 88159fa7163774d6930a174bc038414a4b780581957Elliott Hughes NATIVE_METHOD(Posix, statfs, "(Ljava/lang/String;)Llibcore/io/StructStatFs;"), 882ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes NATIVE_METHOD(Posix, strerror, "(I)Ljava/lang/String;"), 883a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes NATIVE_METHOD(Posix, symlink, "(Ljava/lang/String;Ljava/lang/String;)V"), 8846fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes NATIVE_METHOD(Posix, sysconf, "(I)J"), 8857341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes NATIVE_METHOD(Posix, uname, "()Llibcore/io/StructUtsname;"), 8860568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes NATIVE_METHOD(Posix, writeBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"), 887bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes NATIVE_METHOD(Posix, writev, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"), 888ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes}; 889ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesint register_libcore_io_Posix(JNIEnv* env) { 890ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes return jniRegisterNativeMethods(env, "libcore/io/Posix", gMethods, NELEM(gMethods)); 891ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes} 892