libcore_io_Linux.cpp revision 4f11ebea266eada830d507b8f011e811a8e5d7bc
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"
230f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes#include "ScopedPrimitiveArray.h"
24ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "ScopedUtfChars.h"
2559fa7163774d6930a174bc038414a4b780581957Elliott Hughes#include "StaticAssert.h"
26ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include "toStringArray.h"
27ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
28ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include <errno.h>
290ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <fcntl.h>
30b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes#include <net/if.h>
314f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes#include <netdb.h>
320a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include <netinet/in.h>
33ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include <stdlib.h>
34461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes#include <sys/ioctl.h>
357e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes#include <sys/mman.h>
368b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes#include <sys/sendfile.h>
3759e4744d27231f260271dbbca406e0cc39768116Elliott Hughes#include <sys/socket.h>
3847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes#include <sys/stat.h>
390a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include <sys/time.h>
400ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <sys/types.h>
41bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes#include <sys/uio.h>
427341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#include <sys/utsname.h>
4359fa7163774d6930a174bc038414a4b780581957Elliott Hughes#include <sys/vfs.h> // Bionic doesn't have <sys/statvfs.h>
440ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <unistd.h>
45ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
464f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwException(JNIEnv* env, jclass exceptionClass, jmethodID ctor3, jmethodID ctor2,
474f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        const char* functionName, int error) {
48ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    jthrowable cause = NULL;
49ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (env->ExceptionCheck()) {
50ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        cause = env->ExceptionOccurred();
51ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        env->ExceptionClear();
52ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
53ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
544f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName));
554f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    if (detailMessage.get() == NULL) {
56f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        // Not really much we can do here. We're probably dead in the water,
57f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        // but let's try to stumble on...
58f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        env->ExceptionClear();
59f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    }
60f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes
61ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    jobject exception;
62ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (cause != NULL) {
634f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        exception = env->NewObject(exceptionClass, ctor3, detailMessage.get(), error, cause);
64ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    } else {
654f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        exception = env->NewObject(exceptionClass, ctor2, detailMessage.get(), error);
66ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
67ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    env->Throw(reinterpret_cast<jthrowable>(exception));
68dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes}
69dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes
704f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwErrnoException(JNIEnv* env, const char* functionName) {
714f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    int error = errno;
724f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    static jmethodID ctor3 = env->GetMethodID(JniConstants::errnoExceptionClass,
734f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V");
744f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    static jmethodID ctor2 = env->GetMethodID(JniConstants::errnoExceptionClass,
754f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            "<init>", "(Ljava/lang/String;I)V");
764f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    throwException(env, JniConstants::errnoExceptionClass, ctor3, ctor2, functionName, error);
774f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes}
784f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes
794f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwGaiException(JNIEnv* env, const char* functionName, int error) {
804f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    if (error == EAI_SYSTEM) {
814f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        // EAI_SYSTEM means "look at errno instead", so we want our GaiException to have the
824f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        // relevant ErrnoException as its cause.
834f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        throwErrnoException(env, functionName);
844f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        // Deliberately fall through to throw another exception...
854f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    }
864f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    static jmethodID ctor3 = env->GetMethodID(JniConstants::gaiExceptionClass,
874f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V");
884f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    static jmethodID ctor2 = env->GetMethodID(JniConstants::gaiExceptionClass,
894f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            "<init>", "(Ljava/lang/String;I)V");
904f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    throwException(env, JniConstants::gaiExceptionClass, ctor3, ctor2, functionName, error);
914f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes}
924f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes
93dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughestemplate <typename rc_t>
947e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic rc_t throwIfMinusOne(JNIEnv* env, const char* name, rc_t rc) {
95dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == rc_t(-1)) {
967e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, name);
97dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    }
98dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    return rc;
9947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
10047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
101bbac92e691de7d570928ddfba639067978e55b06Elliott Hughestemplate <typename T>
102bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesclass IoVec {
103bbac92e691de7d570928ddfba639067978e55b06Elliott Hughespublic:
104bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    IoVec(JNIEnv* env, size_t bufferCount) : mEnv(env), mBufferCount(bufferCount) {
105bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
106bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
107bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    bool init(jobjectArray javaBuffers, jintArray javaOffsets, jintArray javaByteCounts) {
108bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        // We can't delete our local references until after the I/O, so make sure we have room.
109bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (mEnv->PushLocalFrame(mBufferCount + 16) < 0) {
110bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            return false;
111bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
112bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        ScopedIntArrayRO offsets(mEnv, javaOffsets);
113bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (offsets.get() == NULL) {
114bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            return false;
115bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
116bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        ScopedIntArrayRO byteCounts(mEnv, javaByteCounts);
117bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (byteCounts.get() == NULL) {
118bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            return false;
119bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
120bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        // TODO: Linux actually has a 1024 buffer limit. glibc works around this, and we should too.
121bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        for (size_t i = 0; i < mBufferCount; ++i) {
122bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            jobject buffer = mEnv->GetObjectArrayElement(javaBuffers, i);
123bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            jbyte* ptr;
124bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            if (mEnv->IsInstanceOf(buffer, JniConstants::byteArrayClass)) {
125bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                // We need to pin the array for the duration.
126bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                jbyteArray byteArray = reinterpret_cast<jbyteArray>(buffer);
127bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                mScopedByteArrays.push_back(new T(mEnv, byteArray));
128bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                ptr = const_cast<jbyte*>(mScopedByteArrays.back()->get());
129bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            } else {
130bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                // A direct ByteBuffer is easier.
131bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                ptr = reinterpret_cast<jbyte*>(mEnv->GetDirectBufferAddress(buffer));
132bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            }
133bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            if (ptr == NULL) {
134bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                return false;
135bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            }
136bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            struct iovec iov;
137bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            iov.iov_base = reinterpret_cast<void*>(ptr + offsets[i]);
138bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            iov.iov_len = byteCounts[i];
139bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            mIoVec.push_back(iov);
140bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
141bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return true;
142bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
143bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
144bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    ~IoVec() {
145bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        for (size_t i = 0; i < mScopedByteArrays.size(); ++i) {
146bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            delete mScopedByteArrays[i];
147bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
148bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        mEnv->PopLocalFrame(NULL);
149bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
150bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
151bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    iovec* get() {
152bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return &mIoVec[0];
153bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
154bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
155bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    size_t size() {
156bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return mBufferCount;
157bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
158bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
159bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesprivate:
160bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    JNIEnv* mEnv;
161bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    size_t mBufferCount;
162bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    std::vector<iovec> mIoVec;
163bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    std::vector<T*> mScopedByteArrays;
164bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes};
165bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
1660a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeInetSocketAddress(JNIEnv* env, const sockaddr_storage* ss, int port) {
1670a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    jobject inetAddress = socketAddressToInetAddress(env, ss);
1680a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (inetAddress == NULL) {
1690a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
1700a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
1710a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::inetSocketAddressClass, "<init>",
1720a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes            "(Ljava/net/InetAddress;I)V");
1730a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return env->NewObject(JniConstants::inetSocketAddressClass, ctor, inetAddress, port);
1740a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
1750a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
1760a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeSocketAddress(JNIEnv* env, const sockaddr_storage* ss) {
1770a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (ss->ss_family == AF_INET) {
1780a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        int port = ntohs(reinterpret_cast<const sockaddr_in*>(ss)->sin_port);
1790a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return makeInetSocketAddress(env, ss, port);
1800a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    } else if (ss->ss_family == AF_INET6) {
1810a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        int port = ntohs(reinterpret_cast<const sockaddr_in6*>(ss)->sin6_port);
1820a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return makeInetSocketAddress(env, ss, port);
1830a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
1840a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    // TODO: support AF_UNIX and AF_UNSPEC, and have some other behavior for other families
1850a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return NULL;
1860a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
1870a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
18847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject makeStructStat(JNIEnv* env, const struct stat& sb) {
18947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structStatClass, "<init>",
19047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes            "(JJIJIIJJJJJJJ)V");
19147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return env->NewObject(JniConstants::structStatClass, ctor,
19259fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_dev), static_cast<jlong>(sb.st_ino),
19359fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jint>(sb.st_mode), static_cast<jlong>(sb.st_nlink),
19459fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jint>(sb.st_uid), static_cast<jint>(sb.st_gid),
19559fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_rdev), static_cast<jlong>(sb.st_size),
19659fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_atime), static_cast<jlong>(sb.st_mtime),
19759fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_ctime), static_cast<jlong>(sb.st_blksize),
19859fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_blocks));
19959fa7163774d6930a174bc038414a4b780581957Elliott Hughes}
20059fa7163774d6930a174bc038414a4b780581957Elliott Hughes
20159fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject makeStructStatFs(JNIEnv* env, const struct statfs& sb) {
20259fa7163774d6930a174bc038414a4b780581957Elliott Hughes    STATIC_ASSERT(sizeof(sb.f_bavail) == sizeof(jlong), statfs_not_64_bit);
20359fa7163774d6930a174bc038414a4b780581957Elliott Hughes    STATIC_ASSERT(sizeof(sb.f_bfree) == sizeof(jlong), statfs_not_64_bit);
20459fa7163774d6930a174bc038414a4b780581957Elliott Hughes    STATIC_ASSERT(sizeof(sb.f_blocks) == sizeof(jlong), statfs_not_64_bit);
20559fa7163774d6930a174bc038414a4b780581957Elliott Hughes
20659fa7163774d6930a174bc038414a4b780581957Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structStatFsClass, "<init>",
20759fa7163774d6930a174bc038414a4b780581957Elliott Hughes            "(JJJJJJJJ)V");
20859fa7163774d6930a174bc038414a4b780581957Elliott Hughes    return env->NewObject(JniConstants::structStatFsClass, ctor, static_cast<jlong>(sb.f_bsize),
20959fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_blocks), static_cast<jlong>(sb.f_bfree),
21059fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_bavail), static_cast<jlong>(sb.f_files),
21159fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_ffree), static_cast<jlong>(sb.f_namelen),
21259fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_frsize));
21347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
21447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
2150a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructLinger(JNIEnv* env, const struct linger& l) {
2160a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structLingerClass, "<init>", "(II)V");
2170a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return env->NewObject(JniConstants::structLingerClass, ctor, l.l_onoff, l.l_linger);
2180a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
2190a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
2200a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructTimeval(JNIEnv* env, const struct timeval& tv) {
2210a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structTimevalClass, "<init>", "(JJ)V");
2220a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return env->NewObject(JniConstants::structTimevalClass, ctor,
2230a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes            static_cast<jlong>(tv.tv_sec), static_cast<jlong>(tv.tv_usec));
2240a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
2250a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
2267341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject makeStructUtsname(JNIEnv* env, const struct utsname& buf) {
2277341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#define TO_JAVA_STRING(NAME) \
2287341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes        jstring NAME = env->NewStringUTF(buf. NAME); \
2297341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes        if (NAME == NULL) return NULL;
2307341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
2317341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(sysname);
2327341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(nodename);
2337341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(release);
2347341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(version);
2357341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(machine);
2367341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#undef TO_JAVA_STRING
2377341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
2387341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structUtsnameClass, "<init>",
2397341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes            "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
2407341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    return env->NewObject(JniConstants::structUtsnameClass, ctor,
2417341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes            sysname, nodename, release, version, machine);
2427341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes};
2437341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
24447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject doStat(JNIEnv* env, jstring javaPath, bool isLstat) {
24547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    ScopedUtfChars path(env, javaPath);
24647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    if (path.c_str() == NULL) {
24747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
24847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
24947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    struct stat sb;
25047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = isLstat ? TEMP_FAILURE_RETRY(lstat(path.c_str(), &sb))
25147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes                     : TEMP_FAILURE_RETRY(stat(path.c_str(), &sb));
252dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
2537e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, isLstat ? "lstat" : "stat");
25447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
25547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
25647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return makeStructStat(env, sb);
257ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
258ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
259ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jboolean Posix_access(JNIEnv* env, jobject, jstring javaPath, jint mode) {
260ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    ScopedUtfChars path(env, javaPath);
261ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (path.c_str() == NULL) {
262ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        return JNI_FALSE;
263ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
26447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = TEMP_FAILURE_RETRY(access(path.c_str(), mode));
265dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
2667e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "access");
267dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    }
268ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return (rc == 0);
269ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
270ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
271b7190190e0ef8de883c952efb319ce7748831faaElliott Hughesstatic void Posix_chmod(JNIEnv* env, jobject, jstring javaPath, jint mode) {
272b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    ScopedUtfChars path(env, javaPath);
273b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    if (path.c_str() == NULL) {
274b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes        return;
275b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    }
276b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    throwIfMinusOne(env, "chmod", TEMP_FAILURE_RETRY(chmod(path.c_str(), mode)));
277b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes}
278b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes
279462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughesstatic void Posix_close(JNIEnv* env, jobject, jobject javaFd) {
280462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // Get the FileDescriptor's 'fd' field and clear it.
281462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // We need to do this before we can throw an IOException (http://b/3222087).
282462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
283462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    jniSetFileDescriptorOfFD(env, javaFd, -1);
284462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes
285462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // Even if close(2) fails with EINTR, the fd will have been closed.
286462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone else's fd.
287462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
288462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    throwIfMinusOne(env, "close", close(fd));
289462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes}
290462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes
291ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jobjectArray Posix_environ(JNIEnv* env, jobject) {
292ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    extern char** environ; // Standard, but not in any header file.
293ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return toStringArray(env, environ);
294ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
295ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
296fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlVoid(JNIEnv* env, jobject, jobject javaFd, jint cmd) {
297fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
298fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd)));
299fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes}
300fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
301fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlLong(JNIEnv* env, jobject, jobject javaFd, jint cmd, jlong arg) {
302fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
303fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, arg)));
304fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes}
305fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
306fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlFlock(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaFlock) {
307fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID typeFid = env->GetFieldID(JniConstants::structFlockClass, "l_type", "S");
308fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID whenceFid = env->GetFieldID(JniConstants::structFlockClass, "l_whence", "S");
309fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID startFid = env->GetFieldID(JniConstants::structFlockClass, "l_start", "J");
310fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID lenFid = env->GetFieldID(JniConstants::structFlockClass, "l_len", "J");
311fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID pidFid = env->GetFieldID(JniConstants::structFlockClass, "l_pid", "I");
312fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
313fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    struct flock64 lock;
314fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    memset(&lock, 0, sizeof(lock));
315fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_type = env->GetShortField(javaFlock, typeFid);
316fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_whence = env->GetShortField(javaFlock, whenceFid);
317fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_start = env->GetLongField(javaFlock, startFid);
318fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_len = env->GetLongField(javaFlock, lenFid);
319fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_pid = env->GetIntField(javaFlock, pidFid);
320fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
321fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
322fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int rc = throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, &lock)));
323fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    if (rc != -1) {
324fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetShortField(javaFlock, typeFid, lock.l_type);
325fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetShortField(javaFlock, whenceFid, lock.l_whence);
326fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetLongField(javaFlock, startFid, lock.l_start);
327fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetLongField(javaFlock, lenFid, lock.l_len);
328fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetIntField(javaFlock, pidFid, lock.l_pid);
329fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    }
330fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    return rc;
331fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes}
332fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
33352724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fdatasync(JNIEnv* env, jobject, jobject javaFd) {
33452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
3357e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "fdatasync", TEMP_FAILURE_RETRY(fdatasync(fd)));
33652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes}
33752724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes
33847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_fstat(JNIEnv* env, jobject, jobject javaFd) {
33947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
34047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    struct stat sb;
34147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = TEMP_FAILURE_RETRY(fstat(fd, &sb));
342dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
3437e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "fstat");
34447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
34547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
34647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return makeStructStat(env, sb);
34747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
34847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
34959fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject Posix_fstatfs(JNIEnv* env, jobject, jobject javaFd) {
35059fa7163774d6930a174bc038414a4b780581957Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
35159fa7163774d6930a174bc038414a4b780581957Elliott Hughes    struct statfs sb;
35259fa7163774d6930a174bc038414a4b780581957Elliott Hughes    int rc = TEMP_FAILURE_RETRY(fstatfs(fd, &sb));
35359fa7163774d6930a174bc038414a4b780581957Elliott Hughes    if (rc == -1) {
35459fa7163774d6930a174bc038414a4b780581957Elliott Hughes        throwErrnoException(env, "fstatfs");
35559fa7163774d6930a174bc038414a4b780581957Elliott Hughes        return NULL;
35659fa7163774d6930a174bc038414a4b780581957Elliott Hughes    }
35759fa7163774d6930a174bc038414a4b780581957Elliott Hughes    return makeStructStatFs(env, sb);
35859fa7163774d6930a174bc038414a4b780581957Elliott Hughes}
35959fa7163774d6930a174bc038414a4b780581957Elliott Hughes
36052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fsync(JNIEnv* env, jobject, jobject javaFd) {
36152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
3627e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "fsync", TEMP_FAILURE_RETRY(fsync(fd)));
363f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes}
364f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes
365f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughesstatic void Posix_ftruncate(JNIEnv* env, jobject, jobject javaFd, jlong length) {
366f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
3677e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "ftruncate", TEMP_FAILURE_RETRY(ftruncate64(fd, length)));
36852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes}
36952724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes
3704f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic jstring Posix_gai_strerror(JNIEnv* env, jobject, jint error) {
3714f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    return env->NewStringUTF(gai_strerror(error));
3724f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes}
3734f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes
374ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic jstring Posix_getenv(JNIEnv* env, jobject, jstring javaName) {
375ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    ScopedUtfChars name(env, javaName);
376ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    if (name.c_str() == NULL) {
377ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes        return NULL;
378ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    }
379ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    return env->NewStringUTF(getenv(name.c_str()));
380ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes}
381ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
3824f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic jstring Posix_getnameinfo(JNIEnv* env, jobject, jobject javaAddress, jint flags) {
3834f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    sockaddr_storage ss;
3844f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    if (!inetAddressToSocketAddress(env, javaAddress, 0, &ss)) {
3854f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        return NULL;
3864f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    }
3874f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    // TODO: bionic's getnameinfo(3) seems to want its length parameter to be exactly
3884f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    // sizeof(sockaddr_in) for an IPv4 address and sizeof (sockaddr_in6) for an
3894f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    // IPv6 address. Fix getnameinfo so it accepts sizeof(sockaddr_storage), and
3904f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    // then remove this hack.
3914f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    socklen_t size = (ss.ss_family == AF_INET) ? sizeof(sockaddr_in) : sizeof(sockaddr_in6);
3924f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    char buf[NI_MAXHOST]; // NI_MAXHOST is longer than INET6_ADDRSTRLEN.
3934f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    int rc = getnameinfo(reinterpret_cast<sockaddr*>(&ss), size, buf, sizeof(buf), NULL, 0, flags);
3944f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    if (rc != 0) {
3954f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        throwGaiException(env, "getnameinfo", rc);
3964f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        return NULL;
3974f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    }
3984f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    return env->NewStringUTF(buf);
3994f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes}
4004f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes
4010a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockname(JNIEnv* env, jobject, jobject javaFd) {
4020a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4030a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr_storage ss;
4040a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr* sa = reinterpret_cast<sockaddr*>(&ss);
4050a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t byteCount = sizeof(ss);
4060a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&ss, 0, byteCount);
4070a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockname(fd, sa, &byteCount));
4080a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
4090a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockname");
4100a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
4110a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
4120a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return makeSocketAddress(env, &ss);
4130a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
4140a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
4150a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
4160a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4170a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    u_char result = 0;
4180a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(result);
4190a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size)));
4200a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return result;
4210a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
4220a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
4230a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptInAddr(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
4240a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4250a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr_storage ss;
4260a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&ss, 0, sizeof(ss));
4270a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    ss.ss_family = AF_INET; // This is only for the IPv4-only IP_MULTICAST_IF.
4280a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr_in* sa = reinterpret_cast<sockaddr_in*>(&ss);
4290a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(sa->sin_addr);
4300a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &sa->sin_addr, &size));
4310a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
4320a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockopt");
4330a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
4340a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
4350a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return socketAddressToInetAddress(env, &ss);
4360a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
4370a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
4380a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
4390a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4400a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    jint result = 0;
4410a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(result);
4420a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size)));
4430a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return result;
4440a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
4450a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
4460a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
4470a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4480a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    struct linger l;
4490a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(l);
4500a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&l, 0, size);
4510a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &l, &size));
4520a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
4530a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockopt");
4540a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
4550a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
4560a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return makeStructLinger(env, l);
4570a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
4580a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
4590a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
4600a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4610a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    struct timeval tv;
4620a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(tv);
4630a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&tv, 0, size);
4640a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &tv, &size));
4650a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
4660a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockopt");
4670a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
4680a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
4690a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return makeStructTimeval(env, tv);
4700a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
4710a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
4728b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jint Posix_ioctlInt(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaArg) {
4738b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    // This is complicated because ioctls may return their result by updating their argument
4748b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    // or via their return value, so we need to support both.
475461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4768b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    static jfieldID valueFid = env->GetFieldID(JniConstants::mutableIntClass, "value", "I");
4778b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    jint arg = env->GetIntField(javaArg, valueFid);
4788b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &arg)));
4798b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    if (!env->ExceptionCheck()) {
4808b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        env->SetIntField(javaArg, valueFid, arg);
4818b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    }
4828b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    return rc;
483461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes}
484461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes
4859a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughesstatic jboolean Posix_isatty(JNIEnv* env, jobject, jobject javaFd) {
4869a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4879a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    return TEMP_FAILURE_RETRY(isatty(fd)) == 0;
4889a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes}
4899a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes
490e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughesstatic void Posix_listen(JNIEnv* env, jobject, jobject javaFd, jint backlog) {
491e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
492e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes    throwIfMinusOne(env, "listen", TEMP_FAILURE_RETRY(listen(fd, backlog)));
493e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes}
494e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes
495dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughesstatic jlong Posix_lseek(JNIEnv* env, jobject, jobject javaFd, jlong offset, jint whence) {
496dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4977e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    return throwIfMinusOne(env, "lseek", TEMP_FAILURE_RETRY(lseek64(fd, offset, whence)));
498dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes}
499dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes
50047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_lstat(JNIEnv* env, jobject, jstring javaPath) {
50147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return doStat(env, javaPath, true);
50247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
50347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
5040f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughesstatic void Posix_mincore(JNIEnv* env, jobject, jlong address, jlong byteCount, jbyteArray javaVector) {
5050f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    ScopedByteArrayRW vector(env, javaVector);
5060f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    if (vector.get() == NULL) {
5070f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes        return;
5080f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    }
5090f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
5100f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    unsigned char* vec = reinterpret_cast<unsigned char*>(vector.get());
5110f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    throwIfMinusOne(env, "mincore", TEMP_FAILURE_RETRY(mincore(ptr, byteCount, vec)));
5120f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes}
5130f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes
514c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_mkdir(JNIEnv* env, jobject, jstring javaPath, jint mode) {
515c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    ScopedUtfChars path(env, javaPath);
516c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    if (path.c_str() == NULL) {
517c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes        return;
518c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    }
519c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    throwIfMinusOne(env, "mkdir", TEMP_FAILURE_RETRY(mkdir(path.c_str(), mode)));
520c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes}
521c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes
5227e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_mlock(JNIEnv* env, jobject, jlong address, jlong byteCount) {
5237e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
5247e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    throwIfMinusOne(env, "mlock", TEMP_FAILURE_RETRY(mlock(ptr, byteCount)));
5257e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes}
5267e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes
5277e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic jlong Posix_mmap(JNIEnv* env, jobject, jlong address, jlong byteCount, jint prot, jint flags, jobject javaFd, jlong offset) {
5287e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
5297e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* suggestedPtr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
5307e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = mmap(suggestedPtr, byteCount, prot, flags, fd, offset);
5317e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    if (ptr == MAP_FAILED) {
5327e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "mmap");
5337e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    }
5347e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    return static_cast<jlong>(reinterpret_cast<uintptr_t>(ptr));
5357e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
5367e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
5377e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_msync(JNIEnv* env, jobject, jlong address, jlong byteCount, jint flags) {
5387e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
5397e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "msync", TEMP_FAILURE_RETRY(msync(ptr, byteCount, flags)));
5407e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
5417e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
5427e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_munlock(JNIEnv* env, jobject, jlong address, jlong byteCount) {
5437e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
5447e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    throwIfMinusOne(env, "munlock", TEMP_FAILURE_RETRY(munlock(ptr, byteCount)));
5457e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes}
5467e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes
5477e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_munmap(JNIEnv* env, jobject, jlong address, jlong byteCount) {
5487e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
5497e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "munmap", TEMP_FAILURE_RETRY(munmap(ptr, byteCount)));
5507e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
5517e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
5520ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughesstatic jobject Posix_open(JNIEnv* env, jobject, jstring javaPath, jint flags, jint mode) {
5530ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    ScopedUtfChars path(env, javaPath);
5540ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    if (path.c_str() == NULL) {
5550ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes        return NULL;
5560ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    }
5570ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    int fd = throwIfMinusOne(env, "open", TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode)));
5580ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL;
5590ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes}
5600ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes
56141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughesstatic jobjectArray Posix_pipe(JNIEnv* env, jobject) {
56241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    int fds[2];
56341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    throwIfMinusOne(env, "pipe", TEMP_FAILURE_RETRY(pipe(&fds[0])));
56441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    jobjectArray result = env->NewObjectArray(2, JniConstants::fileDescriptorClass, NULL);
56541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    if (result == NULL) {
56641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        return NULL;
56741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    }
56841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    for (int i = 0; i < 2; ++i) {
56941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        ScopedLocalRef<jobject> fd(env, jniCreateFileDescriptor(env, fds[i]));
57041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        if (fd.get() == NULL) {
57141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes            return NULL;
57241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        }
57341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        env->SetObjectArrayElement(result, i, fd.get());
57441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        if (env->ExceptionCheck()) {
57541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes            return NULL;
57641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        }
57741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    }
57841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    return result;
57941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes}
58041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes
58126c7025a7a919044771fb89031161bd26fe03032Elliott Hughesstatic jint Posix_read(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount) {
58226c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
58326c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    ScopedByteArrayRW bytes(env, javaBytes);
58426c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    if (bytes.get() == NULL) {
58526c7025a7a919044771fb89031161bd26fe03032Elliott Hughes        return -1;
58626c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    }
58726c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    return throwIfMinusOne(env, "read", TEMP_FAILURE_RETRY(read(fd, bytes.get() + byteOffset, byteCount)));
58826c7025a7a919044771fb89031161bd26fe03032Elliott Hughes}
58926c7025a7a919044771fb89031161bd26fe03032Elliott Hughes
59026c7025a7a919044771fb89031161bd26fe03032Elliott Hughesstatic jint Posix_readDirectBuffer(JNIEnv* env, jobject, jobject javaFd, jobject byteBuffer, jint position, jint remaining) {
59126c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
59226c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    jbyte* ptr = reinterpret_cast<jbyte*>(env->GetDirectBufferAddress(byteBuffer));
59326c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    return throwIfMinusOne(env, "read", TEMP_FAILURE_RETRY(read(fd, ptr + position, remaining)));
59426c7025a7a919044771fb89031161bd26fe03032Elliott Hughes}
59526c7025a7a919044771fb89031161bd26fe03032Elliott Hughes
596bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_readv(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) {
597bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    IoVec<ScopedByteArrayRW> ioVec(env, env->GetArrayLength(buffers));
598bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    if (!ioVec.init(buffers, offsets, byteCounts)) {
599bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return -1;
600bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
601bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
602bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    return throwIfMinusOne(env, "readv", TEMP_FAILURE_RETRY(readv(fd, ioVec.get(), ioVec.size())));
603bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes}
604bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
605c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_remove(JNIEnv* env, jobject, jstring javaPath) {
606c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    ScopedUtfChars path(env, javaPath);
607c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    if (path.c_str() == NULL) {
608c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes        return;
609c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    }
610c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    throwIfMinusOne(env, "remove", TEMP_FAILURE_RETRY(remove(path.c_str())));
611c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes}
612c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes
613a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_rename(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) {
614a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars oldPath(env, javaOldPath);
615a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (oldPath.c_str() == NULL) {
616a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
617a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
618a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars newPath(env, javaNewPath);
619a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (newPath.c_str() == NULL) {
620a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
621a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
622a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    throwIfMinusOne(env, "rename", TEMP_FAILURE_RETRY(rename(oldPath.c_str(), newPath.c_str())));
623a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes}
624a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes
6258b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jlong Posix_sendfile(JNIEnv* env, jobject, jobject javaOutFd, jobject javaInFd, jobject javaOffset, jlong byteCount) {
6268b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    int outFd = jniGetFDFromFileDescriptor(env, javaOutFd);
6278b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    int inFd = jniGetFDFromFileDescriptor(env, javaInFd);
6288b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    static jfieldID valueFid = env->GetFieldID(JniConstants::mutableLongClass, "value", "J");
6298b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    off_t offset = 0;
6308b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    off_t* offsetPtr = NULL;
6318b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    if (javaOffset != NULL) {
6328b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        // TODO: fix bionic so we can have a 64-bit off_t!
6338b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        offset = env->GetLongField(javaOffset, valueFid);
6348b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        offsetPtr = &offset;
6358b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    }
6368b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    jlong result = throwIfMinusOne(env, "sendfile", TEMP_FAILURE_RETRY(sendfile(outFd, inFd, offsetPtr, byteCount)));
6378b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    if (javaOffset != NULL) {
6388b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        env->SetLongField(javaOffset, valueFid, offset);
6398b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    }
6408b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    return result;
6418b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes}
6428b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes
643c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) {
644c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
645c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    u_char byte = value;
646c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &byte, sizeof(byte))));
647c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes}
648c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes
649b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughesstatic void Posix_setsockoptIfreq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jstring value) {
650b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    ScopedUtfChars interfaceName(env, value);
651b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    if (interfaceName.c_str() == NULL) {
652b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes        return;
653b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    }
654b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    struct ifreq req;
655b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    memset(&req, 0, sizeof(req));
656b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    strncpy(req.ifr_name, interfaceName.c_str(), sizeof(req.ifr_name));
657b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    req.ifr_name[sizeof(req.ifr_name) - 1] = '\0';
658b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
659b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req))));
660b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes}
661b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes
662454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic void Posix_setsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) {
663454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
664454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))));
665454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes}
666454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes
667b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughesstatic void Posix_setsockoptIpMreqn(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) {
668b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    ip_mreqn req;
669b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    memset(&req, 0, sizeof(req));
670b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    req.imr_ifindex = value;
671b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
672b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req))));
673b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes}
674b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes
675438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughesstatic void Posix_setsockoptGroupReq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaGroupReq) {
676438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    struct group_req value;
677438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes
678438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    static jfieldID grInterfaceFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_interface", "I");
679438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    value.gr_interface = env->GetIntField(javaGroupReq, grInterfaceFid);
680438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    // Get the IPv4 or IPv6 multicast address to join or leave.
681438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    static jfieldID grGroupFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_group", "Ljava/net/InetAddress;");
682438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    jobject javaGroup = env->GetObjectField(javaGroupReq, grGroupFid);
683438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    if (!inetAddressToSocketAddress(env, javaGroup, 0, &value.gr_group)) {
684438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        return;
685438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    }
686438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes
687438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
688438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    int rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)));
689438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    if (rc == -1 && errno == EINVAL) {
690438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        // Maybe we're a 32-bit binary talking to a 64-bit kernel?
691438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        // glibc doesn't automatically handle this.
692438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        struct GCC_HIDDEN group_req64 {
693438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes            uint32_t gr_interface;
694438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes            uint32_t my_padding;
695438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes            sockaddr_storage gr_group;
696438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        };
697438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        group_req64 value64;
698438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        value64.gr_interface = value.gr_interface;
699438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        memcpy(&value64.gr_group, &value.gr_group, sizeof(value.gr_group));
700438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value64, sizeof(value64)));
701438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    }
702438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    throwIfMinusOne(env, "setsockopt", rc);
703438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes}
704438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes
705c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaLinger) {
706c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID lOnoffFid = env->GetFieldID(JniConstants::structLingerClass, "l_onoff", "I");
707c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID lLingerFid = env->GetFieldID(JniConstants::structLingerClass, "l_linger", "I");
708c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
709c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    struct linger value;
710c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    value.l_onoff = env->GetIntField(javaLinger, lOnoffFid);
711c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    value.l_linger = env->GetIntField(javaLinger, lLingerFid);
712c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))));
713c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes}
714c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes
715c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaTimeval) {
716c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID tvSecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_sec", "J");
717c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID tvUsecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_usec", "J");
718c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
719c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    struct timeval value;
720c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    value.tv_sec = env->GetIntField(javaTimeval, tvSecFid);
721c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    value.tv_usec = env->GetIntField(javaTimeval, tvUsecFid);
722c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))));
723c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes}
724c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes
72559e4744d27231f260271dbbca406e0cc39768116Elliott Hughesstatic void Posix_shutdown(JNIEnv* env, jobject, jobject javaFd, jint how) {
72659e4744d27231f260271dbbca406e0cc39768116Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
72759e4744d27231f260271dbbca406e0cc39768116Elliott Hughes    throwIfMinusOne(env, "shutdown", TEMP_FAILURE_RETRY(shutdown(fd, how)));
72859e4744d27231f260271dbbca406e0cc39768116Elliott Hughes}
72959e4744d27231f260271dbbca406e0cc39768116Elliott Hughes
730454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic jobject Posix_socket(JNIEnv* env, jobject, jint domain, jint type, jint protocol) {
731454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    int fd = throwIfMinusOne(env, "socket", TEMP_FAILURE_RETRY(socket(domain, type, protocol)));
732454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL;
733454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes}
734454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes
73547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_stat(JNIEnv* env, jobject, jstring javaPath) {
73647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return doStat(env, javaPath, false);
73747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
73847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
7390ade5524d972b9dfd69b0126aa4c35319c1b7fa4Elliott Hughesstatic jobject Posix_statfs(JNIEnv* env, jobject, jstring javaPath) {
74059fa7163774d6930a174bc038414a4b780581957Elliott Hughes    ScopedUtfChars path(env, javaPath);
74159fa7163774d6930a174bc038414a4b780581957Elliott Hughes    if (path.c_str() == NULL) {
74259fa7163774d6930a174bc038414a4b780581957Elliott Hughes        return NULL;
74359fa7163774d6930a174bc038414a4b780581957Elliott Hughes    }
74459fa7163774d6930a174bc038414a4b780581957Elliott Hughes    struct statfs sb;
74559fa7163774d6930a174bc038414a4b780581957Elliott Hughes    int rc = TEMP_FAILURE_RETRY(statfs(path.c_str(), &sb));
74659fa7163774d6930a174bc038414a4b780581957Elliott Hughes    if (rc == -1) {
74759fa7163774d6930a174bc038414a4b780581957Elliott Hughes        throwErrnoException(env, "statfs");
74859fa7163774d6930a174bc038414a4b780581957Elliott Hughes        return NULL;
74959fa7163774d6930a174bc038414a4b780581957Elliott Hughes    }
75059fa7163774d6930a174bc038414a4b780581957Elliott Hughes    return makeStructStatFs(env, sb);
75159fa7163774d6930a174bc038414a4b780581957Elliott Hughes}
75259fa7163774d6930a174bc038414a4b780581957Elliott Hughes
753ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jstring Posix_strerror(JNIEnv* env, jobject, jint errnum) {
75452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    char buffer[BUFSIZ];
755ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    const char* message = jniStrError(errnum, buffer, sizeof(buffer));
756ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return env->NewStringUTF(message);
757ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
758ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
759a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_symlink(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) {
760a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars oldPath(env, javaOldPath);
761a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (oldPath.c_str() == NULL) {
762a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
763a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
764a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars newPath(env, javaNewPath);
765a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (newPath.c_str() == NULL) {
766a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
767a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
768a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    throwIfMinusOne(env, "symlink", TEMP_FAILURE_RETRY(symlink(oldPath.c_str(), newPath.c_str())));
769a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes}
770a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes
7716fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughesstatic jlong Posix_sysconf(JNIEnv* env, jobject, jint name) {
7726fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    // Since -1 is a valid result from sysconf(3), detecting failure is a little more awkward.
7736fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    errno = 0;
7746fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    long result = sysconf(name);
7756fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    if (result == -1L && errno == EINVAL) {
7767e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "sysconf");
7776fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    }
7786fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    return result;
7796fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes}
7806fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes
7817341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject Posix_uname(JNIEnv* env, jobject) {
7827341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    struct utsname buf;
7837341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    if (throwIfMinusOne(env, "uname", TEMP_FAILURE_RETRY(uname(&buf))) == -1) {
7847341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes        return NULL;
7857341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    }
7867341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    return makeStructUtsname(env, buf);
7877341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes}
7887341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
78978c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughesstatic jint Posix_write(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount) {
79078c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
79178c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    ScopedByteArrayRO bytes(env, javaBytes);
79278c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    if (bytes.get() == NULL) {
79378c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes        return -1;
79478c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    }
79578c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    return throwIfMinusOne(env, "write", TEMP_FAILURE_RETRY(write(fd, bytes.get() + byteOffset, byteCount)));
79678c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes}
79778c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes
79878c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughesstatic jint Posix_writeDirectBuffer(JNIEnv* env, jobject, jobject javaFd, jobject byteBuffer, jint position, jint remaining) {
79978c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
80078c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    jbyte* ptr = reinterpret_cast<jbyte*>(env->GetDirectBufferAddress(byteBuffer));
80178c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    return throwIfMinusOne(env, "write", TEMP_FAILURE_RETRY(write(fd, ptr + position, remaining)));
80278c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes}
80378c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes
804bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_writev(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) {
805bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    IoVec<ScopedByteArrayRO> ioVec(env, env->GetArrayLength(buffers));
806bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    if (!ioVec.init(buffers, offsets, byteCounts)) {
807bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return -1;
808bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
809bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
810bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    return throwIfMinusOne(env, "writev", TEMP_FAILURE_RETRY(writev(fd, ioVec.get(), ioVec.size())));
811bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes}
812bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
813ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic JNINativeMethod gMethods[] = {
814ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, access, "(Ljava/lang/String;I)Z"),
815b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    NATIVE_METHOD(Posix, chmod, "(Ljava/lang/String;I)V"),
816462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    NATIVE_METHOD(Posix, close, "(Ljava/io/FileDescriptor;)V"),
817ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, environ, "()[Ljava/lang/String;"),
818fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    NATIVE_METHOD(Posix, fcntlVoid, "(Ljava/io/FileDescriptor;I)I"),
819fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    NATIVE_METHOD(Posix, fcntlLong, "(Ljava/io/FileDescriptor;IJ)I"),
820fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    NATIVE_METHOD(Posix, fcntlFlock, "(Ljava/io/FileDescriptor;ILlibcore/io/StructFlock;)I"),
82152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    NATIVE_METHOD(Posix, fdatasync, "(Ljava/io/FileDescriptor;)V"),
82247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, fstat, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStat;"),
82359fa7163774d6930a174bc038414a4b780581957Elliott Hughes    NATIVE_METHOD(Posix, fstatfs, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStatFs;"),
82452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    NATIVE_METHOD(Posix, fsync, "(Ljava/io/FileDescriptor;)V"),
825f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    NATIVE_METHOD(Posix, ftruncate, "(Ljava/io/FileDescriptor;J)V"),
8264f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    NATIVE_METHOD(Posix, gai_strerror, "(I)Ljava/lang/String;"),
827ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    NATIVE_METHOD(Posix, getenv, "(Ljava/lang/String;)Ljava/lang/String;"),
8284f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    NATIVE_METHOD(Posix, getnameinfo, "(Ljava/net/InetAddress;I)Ljava/lang/String;"),
8290a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockname, "(Ljava/io/FileDescriptor;)Ljava/net/SocketAddress;"),
8300a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptByte, "(Ljava/io/FileDescriptor;II)I"),
8310a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptInAddr, "(Ljava/io/FileDescriptor;II)Ljava/net/InetAddress;"),
8320a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptInt, "(Ljava/io/FileDescriptor;II)I"),
8330a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptLinger, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructLinger;"),
8340a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptTimeval, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructTimeval;"),
8358b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    NATIVE_METHOD(Posix, ioctlInt, "(Ljava/io/FileDescriptor;ILlibcore/util/MutableInt;)I"),
8369a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    NATIVE_METHOD(Posix, isatty, "(Ljava/io/FileDescriptor;)Z"),
837e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes    NATIVE_METHOD(Posix, listen, "(Ljava/io/FileDescriptor;I)V"),
838dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    NATIVE_METHOD(Posix, lseek, "(Ljava/io/FileDescriptor;JI)J"),
83947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, lstat, "(Ljava/lang/String;)Llibcore/io/StructStat;"),
8400f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    NATIVE_METHOD(Posix, mincore, "(JJ[B)V"),
841c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    NATIVE_METHOD(Posix, mkdir, "(Ljava/lang/String;I)V"),
8427e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    NATIVE_METHOD(Posix, mlock, "(JJ)V"),
8437e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, mmap, "(JJIILjava/io/FileDescriptor;J)J"),
8447e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, msync, "(JJI)V"),
8457e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    NATIVE_METHOD(Posix, munlock, "(JJ)V"),
8467e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, munmap, "(JJ)V"),
8470ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    NATIVE_METHOD(Posix, open, "(Ljava/lang/String;II)Ljava/io/FileDescriptor;"),
84841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    NATIVE_METHOD(Posix, pipe, "()[Ljava/io/FileDescriptor;"),
84926c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    NATIVE_METHOD(Posix, read, "(Ljava/io/FileDescriptor;[BII)I"),
85026c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    NATIVE_METHOD(Posix, readDirectBuffer, "(Ljava/io/FileDescriptor;Ljava/nio/ByteBuffer;II)I"),
851bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    NATIVE_METHOD(Posix, readv, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"),
852c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    NATIVE_METHOD(Posix, remove, "(Ljava/lang/String;)V"),
853a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    NATIVE_METHOD(Posix, rename, "(Ljava/lang/String;Ljava/lang/String;)V"),
8548b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    NATIVE_METHOD(Posix, sendfile, "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Llibcore/util/MutableLong;J)J"),
855c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    NATIVE_METHOD(Posix, setsockoptByte, "(Ljava/io/FileDescriptor;III)V"),
856b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    NATIVE_METHOD(Posix, setsockoptIfreq, "(Ljava/io/FileDescriptor;IILjava/lang/String;)V"),
857454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    NATIVE_METHOD(Posix, setsockoptInt, "(Ljava/io/FileDescriptor;III)V"),
858b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    NATIVE_METHOD(Posix, setsockoptIpMreqn, "(Ljava/io/FileDescriptor;III)V"),
859438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    NATIVE_METHOD(Posix, setsockoptGroupReq, "(Ljava/io/FileDescriptor;IILlibcore/io/StructGroupReq;)V"),
860c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    NATIVE_METHOD(Posix, setsockoptLinger, "(Ljava/io/FileDescriptor;IILlibcore/io/StructLinger;)V"),
861c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    NATIVE_METHOD(Posix, setsockoptTimeval, "(Ljava/io/FileDescriptor;IILlibcore/io/StructTimeval;)V"),
86259e4744d27231f260271dbbca406e0cc39768116Elliott Hughes    NATIVE_METHOD(Posix, shutdown, "(Ljava/io/FileDescriptor;I)V"),
863454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    NATIVE_METHOD(Posix, socket, "(III)Ljava/io/FileDescriptor;"),
86447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, stat, "(Ljava/lang/String;)Llibcore/io/StructStat;"),
86559fa7163774d6930a174bc038414a4b780581957Elliott Hughes    NATIVE_METHOD(Posix, statfs, "(Ljava/lang/String;)Llibcore/io/StructStatFs;"),
866ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, strerror, "(I)Ljava/lang/String;"),
867a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    NATIVE_METHOD(Posix, symlink, "(Ljava/lang/String;Ljava/lang/String;)V"),
8686fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    NATIVE_METHOD(Posix, sysconf, "(I)J"),
8697341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    NATIVE_METHOD(Posix, uname, "()Llibcore/io/StructUtsname;"),
87078c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    NATIVE_METHOD(Posix, write, "(Ljava/io/FileDescriptor;[BII)I"),
87178c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    NATIVE_METHOD(Posix, writeDirectBuffer, "(Ljava/io/FileDescriptor;Ljava/nio/ByteBuffer;II)I"),
872bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    NATIVE_METHOD(Posix, writev, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"),
873ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes};
874ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesint register_libcore_io_Posix(JNIEnv* env) {
875ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    return jniRegisterNativeMethods(env, "libcore/io/Posix", gMethods, NELEM(gMethods));
876ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes}
877