libcore_io_Linux.cpp revision 438cb9e440d250c8aa5daf4fae0c400dce8b1499
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>
300a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include <netinet/in.h>
31ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include <stdlib.h>
32461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes#include <sys/ioctl.h>
337e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes#include <sys/mman.h>
348b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes#include <sys/sendfile.h>
3559e4744d27231f260271dbbca406e0cc39768116Elliott Hughes#include <sys/socket.h>
3647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes#include <sys/stat.h>
370a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include <sys/time.h>
380ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <sys/types.h>
39bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes#include <sys/uio.h>
407341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#include <sys/utsname.h>
4159fa7163774d6930a174bc038414a4b780581957Elliott Hughes#include <sys/vfs.h> // Bionic doesn't have <sys/statvfs.h>
420ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <unistd.h>
43ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
447e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void throwErrnoException(JNIEnv* env, const char* name) {
457e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    int errnum = errno;
467e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
47ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    jthrowable cause = NULL;
48ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (env->ExceptionCheck()) {
49ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        cause = env->ExceptionOccurred();
50ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        env->ExceptionClear();
51ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
52ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
53f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    ScopedLocalRef<jstring> javaName(env, env->NewStringUTF(name));
54f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    if (javaName.get() == NULL) {
55f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        // Not really much we can do here. We're probably dead in the water,
56f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        // but let's try to stumble on...
57f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        env->ExceptionClear();
58f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    }
59f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes
60ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    jobject exception;
61ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (cause != NULL) {
62f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        static jmethodID ctor = env->GetMethodID(JniConstants::errnoExceptionClass, "<init>",
63f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes                "(Ljava/lang/String;ILjava/lang/Throwable;)V");
64f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        exception = env->NewObject(JniConstants::errnoExceptionClass, ctor,
65f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes                javaName.get(), errnum, cause);
66ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    } else {
67f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        static jmethodID ctor = env->GetMethodID(JniConstants::errnoExceptionClass, "<init>",
68f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes                "(Ljava/lang/String;I)V");
69f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        exception = env->NewObject(JniConstants::errnoExceptionClass, ctor, javaName.get(), errnum);
70ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
71ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    env->Throw(reinterpret_cast<jthrowable>(exception));
72dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes}
73dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes
74dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughestemplate <typename rc_t>
757e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic rc_t throwIfMinusOne(JNIEnv* env, const char* name, rc_t rc) {
76dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == rc_t(-1)) {
777e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, name);
78dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    }
79dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    return rc;
8047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
8147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
82bbac92e691de7d570928ddfba639067978e55b06Elliott Hughestemplate <typename T>
83bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesclass IoVec {
84bbac92e691de7d570928ddfba639067978e55b06Elliott Hughespublic:
85bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    IoVec(JNIEnv* env, size_t bufferCount) : mEnv(env), mBufferCount(bufferCount) {
86bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
87bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
88bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    bool init(jobjectArray javaBuffers, jintArray javaOffsets, jintArray javaByteCounts) {
89bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        // We can't delete our local references until after the I/O, so make sure we have room.
90bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (mEnv->PushLocalFrame(mBufferCount + 16) < 0) {
91bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            return false;
92bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
93bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        ScopedIntArrayRO offsets(mEnv, javaOffsets);
94bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (offsets.get() == NULL) {
95bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            return false;
96bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
97bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        ScopedIntArrayRO byteCounts(mEnv, javaByteCounts);
98bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (byteCounts.get() == NULL) {
99bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            return false;
100bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
101bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        // TODO: Linux actually has a 1024 buffer limit. glibc works around this, and we should too.
102bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        for (size_t i = 0; i < mBufferCount; ++i) {
103bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            jobject buffer = mEnv->GetObjectArrayElement(javaBuffers, i);
104bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            jbyte* ptr;
105bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            if (mEnv->IsInstanceOf(buffer, JniConstants::byteArrayClass)) {
106bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                // We need to pin the array for the duration.
107bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                jbyteArray byteArray = reinterpret_cast<jbyteArray>(buffer);
108bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                mScopedByteArrays.push_back(new T(mEnv, byteArray));
109bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                ptr = const_cast<jbyte*>(mScopedByteArrays.back()->get());
110bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            } else {
111bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                // A direct ByteBuffer is easier.
112bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                ptr = reinterpret_cast<jbyte*>(mEnv->GetDirectBufferAddress(buffer));
113bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            }
114bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            if (ptr == NULL) {
115bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                return false;
116bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            }
117bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            struct iovec iov;
118bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            iov.iov_base = reinterpret_cast<void*>(ptr + offsets[i]);
119bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            iov.iov_len = byteCounts[i];
120bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            mIoVec.push_back(iov);
121bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
122bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return true;
123bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
124bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
125bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    ~IoVec() {
126bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        for (size_t i = 0; i < mScopedByteArrays.size(); ++i) {
127bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            delete mScopedByteArrays[i];
128bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
129bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        mEnv->PopLocalFrame(NULL);
130bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
131bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
132bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    iovec* get() {
133bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return &mIoVec[0];
134bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
135bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
136bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    size_t size() {
137bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return mBufferCount;
138bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
139bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
140bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesprivate:
141bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    JNIEnv* mEnv;
142bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    size_t mBufferCount;
143bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    std::vector<iovec> mIoVec;
144bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    std::vector<T*> mScopedByteArrays;
145bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes};
146bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
1470a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeInetSocketAddress(JNIEnv* env, const sockaddr_storage* ss, int port) {
1480a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    jobject inetAddress = socketAddressToInetAddress(env, ss);
1490a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (inetAddress == NULL) {
1500a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
1510a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
1520a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::inetSocketAddressClass, "<init>",
1530a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes            "(Ljava/net/InetAddress;I)V");
1540a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return env->NewObject(JniConstants::inetSocketAddressClass, ctor, inetAddress, port);
1550a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
1560a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
1570a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeSocketAddress(JNIEnv* env, const sockaddr_storage* ss) {
1580a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (ss->ss_family == AF_INET) {
1590a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        int port = ntohs(reinterpret_cast<const sockaddr_in*>(ss)->sin_port);
1600a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return makeInetSocketAddress(env, ss, port);
1610a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    } else if (ss->ss_family == AF_INET6) {
1620a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        int port = ntohs(reinterpret_cast<const sockaddr_in6*>(ss)->sin6_port);
1630a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return makeInetSocketAddress(env, ss, port);
1640a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
1650a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    // TODO: support AF_UNIX and AF_UNSPEC, and have some other behavior for other families
1660a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return NULL;
1670a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
1680a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
16947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject makeStructStat(JNIEnv* env, const struct stat& sb) {
17047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structStatClass, "<init>",
17147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes            "(JJIJIIJJJJJJJ)V");
17247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return env->NewObject(JniConstants::structStatClass, ctor,
17359fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_dev), static_cast<jlong>(sb.st_ino),
17459fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jint>(sb.st_mode), static_cast<jlong>(sb.st_nlink),
17559fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jint>(sb.st_uid), static_cast<jint>(sb.st_gid),
17659fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_rdev), static_cast<jlong>(sb.st_size),
17759fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_atime), static_cast<jlong>(sb.st_mtime),
17859fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_ctime), static_cast<jlong>(sb.st_blksize),
17959fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_blocks));
18059fa7163774d6930a174bc038414a4b780581957Elliott Hughes}
18159fa7163774d6930a174bc038414a4b780581957Elliott Hughes
18259fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject makeStructStatFs(JNIEnv* env, const struct statfs& sb) {
18359fa7163774d6930a174bc038414a4b780581957Elliott Hughes    STATIC_ASSERT(sizeof(sb.f_bavail) == sizeof(jlong), statfs_not_64_bit);
18459fa7163774d6930a174bc038414a4b780581957Elliott Hughes    STATIC_ASSERT(sizeof(sb.f_bfree) == sizeof(jlong), statfs_not_64_bit);
18559fa7163774d6930a174bc038414a4b780581957Elliott Hughes    STATIC_ASSERT(sizeof(sb.f_blocks) == sizeof(jlong), statfs_not_64_bit);
18659fa7163774d6930a174bc038414a4b780581957Elliott Hughes
18759fa7163774d6930a174bc038414a4b780581957Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structStatFsClass, "<init>",
18859fa7163774d6930a174bc038414a4b780581957Elliott Hughes            "(JJJJJJJJ)V");
18959fa7163774d6930a174bc038414a4b780581957Elliott Hughes    return env->NewObject(JniConstants::structStatFsClass, ctor, static_cast<jlong>(sb.f_bsize),
19059fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_blocks), static_cast<jlong>(sb.f_bfree),
19159fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_bavail), static_cast<jlong>(sb.f_files),
19259fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_ffree), static_cast<jlong>(sb.f_namelen),
19359fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_frsize));
19447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
19547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
1960a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructLinger(JNIEnv* env, const struct linger& l) {
1970a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structLingerClass, "<init>", "(II)V");
1980a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return env->NewObject(JniConstants::structLingerClass, ctor, l.l_onoff, l.l_linger);
1990a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
2000a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
2010a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructTimeval(JNIEnv* env, const struct timeval& tv) {
2020a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structTimevalClass, "<init>", "(JJ)V");
2030a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return env->NewObject(JniConstants::structTimevalClass, ctor,
2040a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes            static_cast<jlong>(tv.tv_sec), static_cast<jlong>(tv.tv_usec));
2050a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
2060a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
2077341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject makeStructUtsname(JNIEnv* env, const struct utsname& buf) {
2087341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#define TO_JAVA_STRING(NAME) \
2097341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes        jstring NAME = env->NewStringUTF(buf. NAME); \
2107341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes        if (NAME == NULL) return NULL;
2117341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
2127341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(sysname);
2137341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(nodename);
2147341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(release);
2157341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(version);
2167341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(machine);
2177341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#undef TO_JAVA_STRING
2187341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
2197341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structUtsnameClass, "<init>",
2207341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes            "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
2217341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    return env->NewObject(JniConstants::structUtsnameClass, ctor,
2227341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes            sysname, nodename, release, version, machine);
2237341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes};
2247341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
22547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject doStat(JNIEnv* env, jstring javaPath, bool isLstat) {
22647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    ScopedUtfChars path(env, javaPath);
22747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    if (path.c_str() == NULL) {
22847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
22947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
23047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    struct stat sb;
23147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = isLstat ? TEMP_FAILURE_RETRY(lstat(path.c_str(), &sb))
23247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes                     : TEMP_FAILURE_RETRY(stat(path.c_str(), &sb));
233dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
2347e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, isLstat ? "lstat" : "stat");
23547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
23647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
23747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return makeStructStat(env, sb);
238ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
239ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
240ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jboolean Posix_access(JNIEnv* env, jobject, jstring javaPath, jint mode) {
241ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    ScopedUtfChars path(env, javaPath);
242ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (path.c_str() == NULL) {
243ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        return JNI_FALSE;
244ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
24547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = TEMP_FAILURE_RETRY(access(path.c_str(), mode));
246dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
2477e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "access");
248dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    }
249ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return (rc == 0);
250ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
251ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
252b7190190e0ef8de883c952efb319ce7748831faaElliott Hughesstatic void Posix_chmod(JNIEnv* env, jobject, jstring javaPath, jint mode) {
253b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    ScopedUtfChars path(env, javaPath);
254b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    if (path.c_str() == NULL) {
255b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes        return;
256b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    }
257b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    throwIfMinusOne(env, "chmod", TEMP_FAILURE_RETRY(chmod(path.c_str(), mode)));
258b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes}
259b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes
260462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughesstatic void Posix_close(JNIEnv* env, jobject, jobject javaFd) {
261462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // Get the FileDescriptor's 'fd' field and clear it.
262462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // We need to do this before we can throw an IOException (http://b/3222087).
263462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
264462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    jniSetFileDescriptorOfFD(env, javaFd, -1);
265462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes
266462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // Even if close(2) fails with EINTR, the fd will have been closed.
267462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone else's fd.
268462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
269462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    throwIfMinusOne(env, "close", close(fd));
270462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes}
271462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes
272ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jobjectArray Posix_environ(JNIEnv* env, jobject) {
273ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    extern char** environ; // Standard, but not in any header file.
274ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return toStringArray(env, environ);
275ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
276ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
277fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlVoid(JNIEnv* env, jobject, jobject javaFd, jint cmd) {
278fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
279fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd)));
280fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes}
281fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
282fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlLong(JNIEnv* env, jobject, jobject javaFd, jint cmd, jlong arg) {
283fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
284fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, arg)));
285fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes}
286fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
287fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlFlock(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaFlock) {
288fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID typeFid = env->GetFieldID(JniConstants::structFlockClass, "l_type", "S");
289fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID whenceFid = env->GetFieldID(JniConstants::structFlockClass, "l_whence", "S");
290fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID startFid = env->GetFieldID(JniConstants::structFlockClass, "l_start", "J");
291fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID lenFid = env->GetFieldID(JniConstants::structFlockClass, "l_len", "J");
292fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID pidFid = env->GetFieldID(JniConstants::structFlockClass, "l_pid", "I");
293fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
294fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    struct flock64 lock;
295fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    memset(&lock, 0, sizeof(lock));
296fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_type = env->GetShortField(javaFlock, typeFid);
297fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_whence = env->GetShortField(javaFlock, whenceFid);
298fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_start = env->GetLongField(javaFlock, startFid);
299fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_len = env->GetLongField(javaFlock, lenFid);
300fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_pid = env->GetIntField(javaFlock, pidFid);
301fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
302fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
303fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int rc = throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, &lock)));
304fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    if (rc != -1) {
305fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetShortField(javaFlock, typeFid, lock.l_type);
306fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetShortField(javaFlock, whenceFid, lock.l_whence);
307fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetLongField(javaFlock, startFid, lock.l_start);
308fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetLongField(javaFlock, lenFid, lock.l_len);
309fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetIntField(javaFlock, pidFid, lock.l_pid);
310fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    }
311fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    return rc;
312fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes}
313fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
31452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fdatasync(JNIEnv* env, jobject, jobject javaFd) {
31552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
3167e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "fdatasync", TEMP_FAILURE_RETRY(fdatasync(fd)));
31752724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes}
31852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes
31947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_fstat(JNIEnv* env, jobject, jobject javaFd) {
32047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
32147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    struct stat sb;
32247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = TEMP_FAILURE_RETRY(fstat(fd, &sb));
323dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
3247e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "fstat");
32547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
32647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
32747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return makeStructStat(env, sb);
32847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
32947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
33059fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject Posix_fstatfs(JNIEnv* env, jobject, jobject javaFd) {
33159fa7163774d6930a174bc038414a4b780581957Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
33259fa7163774d6930a174bc038414a4b780581957Elliott Hughes    struct statfs sb;
33359fa7163774d6930a174bc038414a4b780581957Elliott Hughes    int rc = TEMP_FAILURE_RETRY(fstatfs(fd, &sb));
33459fa7163774d6930a174bc038414a4b780581957Elliott Hughes    if (rc == -1) {
33559fa7163774d6930a174bc038414a4b780581957Elliott Hughes        throwErrnoException(env, "fstatfs");
33659fa7163774d6930a174bc038414a4b780581957Elliott Hughes        return NULL;
33759fa7163774d6930a174bc038414a4b780581957Elliott Hughes    }
33859fa7163774d6930a174bc038414a4b780581957Elliott Hughes    return makeStructStatFs(env, sb);
33959fa7163774d6930a174bc038414a4b780581957Elliott Hughes}
34059fa7163774d6930a174bc038414a4b780581957Elliott Hughes
34152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fsync(JNIEnv* env, jobject, jobject javaFd) {
34252724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
3437e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "fsync", TEMP_FAILURE_RETRY(fsync(fd)));
344f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes}
345f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes
346f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughesstatic void Posix_ftruncate(JNIEnv* env, jobject, jobject javaFd, jlong length) {
347f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
3487e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "ftruncate", TEMP_FAILURE_RETRY(ftruncate64(fd, length)));
34952724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes}
35052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes
351ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic jstring Posix_getenv(JNIEnv* env, jobject, jstring javaName) {
352ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    ScopedUtfChars name(env, javaName);
353ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    if (name.c_str() == NULL) {
354ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes        return NULL;
355ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    }
356ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    return env->NewStringUTF(getenv(name.c_str()));
357ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes}
358ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
3590a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockname(JNIEnv* env, jobject, jobject javaFd) {
3600a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
3610a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr_storage ss;
3620a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr* sa = reinterpret_cast<sockaddr*>(&ss);
3630a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t byteCount = sizeof(ss);
3640a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&ss, 0, byteCount);
3650a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockname(fd, sa, &byteCount));
3660a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
3670a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockname");
3680a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
3690a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
3700a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return makeSocketAddress(env, &ss);
3710a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
3720a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
3730a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
3740a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
3750a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    u_char result = 0;
3760a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(result);
3770a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size)));
3780a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return result;
3790a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
3800a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
3810a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptInAddr(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
3820a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
3830a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr_storage ss;
3840a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&ss, 0, sizeof(ss));
3850a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    ss.ss_family = AF_INET; // This is only for the IPv4-only IP_MULTICAST_IF.
3860a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr_in* sa = reinterpret_cast<sockaddr_in*>(&ss);
3870a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(sa->sin_addr);
3880a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &sa->sin_addr, &size));
3890a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
3900a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockopt");
3910a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
3920a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
3930a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return socketAddressToInetAddress(env, &ss);
3940a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
3950a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
3960a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
3970a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
3980a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    jint result = 0;
3990a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(result);
4000a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size)));
4010a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return result;
4020a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
4030a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
4040a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
4050a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4060a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    struct linger l;
4070a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(l);
4080a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&l, 0, size);
4090a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &l, &size));
4100a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
4110a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockopt");
4120a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
4130a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
4140a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return makeStructLinger(env, l);
4150a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
4160a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
4170a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
4180a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4190a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    struct timeval tv;
4200a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(tv);
4210a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&tv, 0, size);
4220a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &tv, &size));
4230a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
4240a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockopt");
4250a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
4260a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
4270a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return makeStructTimeval(env, tv);
4280a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
4290a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
4308b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jint Posix_ioctlInt(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaArg) {
4318b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    // This is complicated because ioctls may return their result by updating their argument
4328b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    // or via their return value, so we need to support both.
433461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4348b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    static jfieldID valueFid = env->GetFieldID(JniConstants::mutableIntClass, "value", "I");
4358b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    jint arg = env->GetIntField(javaArg, valueFid);
4368b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &arg)));
4378b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    if (!env->ExceptionCheck()) {
4388b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        env->SetIntField(javaArg, valueFid, arg);
4398b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    }
4408b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    return rc;
441461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes}
442461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes
4439a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughesstatic jboolean Posix_isatty(JNIEnv* env, jobject, jobject javaFd) {
4449a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4459a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    return TEMP_FAILURE_RETRY(isatty(fd)) == 0;
4469a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes}
4479a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes
448e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughesstatic void Posix_listen(JNIEnv* env, jobject, jobject javaFd, jint backlog) {
449e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
450e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes    throwIfMinusOne(env, "listen", TEMP_FAILURE_RETRY(listen(fd, backlog)));
451e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes}
452e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes
453dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughesstatic jlong Posix_lseek(JNIEnv* env, jobject, jobject javaFd, jlong offset, jint whence) {
454dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4557e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    return throwIfMinusOne(env, "lseek", TEMP_FAILURE_RETRY(lseek64(fd, offset, whence)));
456dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes}
457dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes
45847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_lstat(JNIEnv* env, jobject, jstring javaPath) {
45947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return doStat(env, javaPath, true);
46047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
46147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
4620f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughesstatic void Posix_mincore(JNIEnv* env, jobject, jlong address, jlong byteCount, jbyteArray javaVector) {
4630f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    ScopedByteArrayRW vector(env, javaVector);
4640f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    if (vector.get() == NULL) {
4650f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes        return;
4660f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    }
4670f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
4680f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    unsigned char* vec = reinterpret_cast<unsigned char*>(vector.get());
4690f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    throwIfMinusOne(env, "mincore", TEMP_FAILURE_RETRY(mincore(ptr, byteCount, vec)));
4700f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes}
4710f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes
472c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_mkdir(JNIEnv* env, jobject, jstring javaPath, jint mode) {
473c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    ScopedUtfChars path(env, javaPath);
474c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    if (path.c_str() == NULL) {
475c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes        return;
476c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    }
477c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    throwIfMinusOne(env, "mkdir", TEMP_FAILURE_RETRY(mkdir(path.c_str(), mode)));
478c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes}
479c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes
4807e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_mlock(JNIEnv* env, jobject, jlong address, jlong byteCount) {
4817e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
4827e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    throwIfMinusOne(env, "mlock", TEMP_FAILURE_RETRY(mlock(ptr, byteCount)));
4837e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes}
4847e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes
4857e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic jlong Posix_mmap(JNIEnv* env, jobject, jlong address, jlong byteCount, jint prot, jint flags, jobject javaFd, jlong offset) {
4867e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4877e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* suggestedPtr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
4887e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = mmap(suggestedPtr, byteCount, prot, flags, fd, offset);
4897e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    if (ptr == MAP_FAILED) {
4907e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "mmap");
4917e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    }
4927e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    return static_cast<jlong>(reinterpret_cast<uintptr_t>(ptr));
4937e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
4947e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
4957e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_msync(JNIEnv* env, jobject, jlong address, jlong byteCount, jint flags) {
4967e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
4977e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "msync", TEMP_FAILURE_RETRY(msync(ptr, byteCount, flags)));
4987e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
4997e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
5007e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_munlock(JNIEnv* env, jobject, jlong address, jlong byteCount) {
5017e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
5027e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    throwIfMinusOne(env, "munlock", TEMP_FAILURE_RETRY(munlock(ptr, byteCount)));
5037e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes}
5047e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes
5057e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_munmap(JNIEnv* env, jobject, jlong address, jlong byteCount) {
5067e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
5077e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "munmap", TEMP_FAILURE_RETRY(munmap(ptr, byteCount)));
5087e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
5097e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
5100ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughesstatic jobject Posix_open(JNIEnv* env, jobject, jstring javaPath, jint flags, jint mode) {
5110ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    ScopedUtfChars path(env, javaPath);
5120ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    if (path.c_str() == NULL) {
5130ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes        return NULL;
5140ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    }
5150ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    int fd = throwIfMinusOne(env, "open", TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode)));
5160ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL;
5170ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes}
5180ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes
51941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughesstatic jobjectArray Posix_pipe(JNIEnv* env, jobject) {
52041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    int fds[2];
52141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    throwIfMinusOne(env, "pipe", TEMP_FAILURE_RETRY(pipe(&fds[0])));
52241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    jobjectArray result = env->NewObjectArray(2, JniConstants::fileDescriptorClass, NULL);
52341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    if (result == NULL) {
52441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        return NULL;
52541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    }
52641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    for (int i = 0; i < 2; ++i) {
52741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        ScopedLocalRef<jobject> fd(env, jniCreateFileDescriptor(env, fds[i]));
52841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        if (fd.get() == NULL) {
52941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes            return NULL;
53041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        }
53141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        env->SetObjectArrayElement(result, i, fd.get());
53241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        if (env->ExceptionCheck()) {
53341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes            return NULL;
53441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        }
53541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    }
53641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    return result;
53741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes}
53841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes
53926c7025a7a919044771fb89031161bd26fe03032Elliott Hughesstatic jint Posix_read(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount) {
54026c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
54126c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    ScopedByteArrayRW bytes(env, javaBytes);
54226c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    if (bytes.get() == NULL) {
54326c7025a7a919044771fb89031161bd26fe03032Elliott Hughes        return -1;
54426c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    }
54526c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    return throwIfMinusOne(env, "read", TEMP_FAILURE_RETRY(read(fd, bytes.get() + byteOffset, byteCount)));
54626c7025a7a919044771fb89031161bd26fe03032Elliott Hughes}
54726c7025a7a919044771fb89031161bd26fe03032Elliott Hughes
54826c7025a7a919044771fb89031161bd26fe03032Elliott Hughesstatic jint Posix_readDirectBuffer(JNIEnv* env, jobject, jobject javaFd, jobject byteBuffer, jint position, jint remaining) {
54926c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
55026c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    jbyte* ptr = reinterpret_cast<jbyte*>(env->GetDirectBufferAddress(byteBuffer));
55126c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    return throwIfMinusOne(env, "read", TEMP_FAILURE_RETRY(read(fd, ptr + position, remaining)));
55226c7025a7a919044771fb89031161bd26fe03032Elliott Hughes}
55326c7025a7a919044771fb89031161bd26fe03032Elliott Hughes
554bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_readv(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) {
555bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    IoVec<ScopedByteArrayRW> ioVec(env, env->GetArrayLength(buffers));
556bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    if (!ioVec.init(buffers, offsets, byteCounts)) {
557bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return -1;
558bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
559bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
560bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    return throwIfMinusOne(env, "readv", TEMP_FAILURE_RETRY(readv(fd, ioVec.get(), ioVec.size())));
561bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes}
562bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
563c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_remove(JNIEnv* env, jobject, jstring javaPath) {
564c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    ScopedUtfChars path(env, javaPath);
565c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    if (path.c_str() == NULL) {
566c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes        return;
567c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    }
568c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    throwIfMinusOne(env, "remove", TEMP_FAILURE_RETRY(remove(path.c_str())));
569c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes}
570c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes
571a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_rename(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) {
572a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars oldPath(env, javaOldPath);
573a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (oldPath.c_str() == NULL) {
574a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
575a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
576a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars newPath(env, javaNewPath);
577a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (newPath.c_str() == NULL) {
578a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
579a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
580a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    throwIfMinusOne(env, "rename", TEMP_FAILURE_RETRY(rename(oldPath.c_str(), newPath.c_str())));
581a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes}
582a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes
5838b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jlong Posix_sendfile(JNIEnv* env, jobject, jobject javaOutFd, jobject javaInFd, jobject javaOffset, jlong byteCount) {
5848b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    int outFd = jniGetFDFromFileDescriptor(env, javaOutFd);
5858b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    int inFd = jniGetFDFromFileDescriptor(env, javaInFd);
5868b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    static jfieldID valueFid = env->GetFieldID(JniConstants::mutableLongClass, "value", "J");
5878b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    off_t offset = 0;
5888b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    off_t* offsetPtr = NULL;
5898b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    if (javaOffset != NULL) {
5908b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        // TODO: fix bionic so we can have a 64-bit off_t!
5918b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        offset = env->GetLongField(javaOffset, valueFid);
5928b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        offsetPtr = &offset;
5938b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    }
5948b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    jlong result = throwIfMinusOne(env, "sendfile", TEMP_FAILURE_RETRY(sendfile(outFd, inFd, offsetPtr, byteCount)));
5958b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    if (javaOffset != NULL) {
5968b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        env->SetLongField(javaOffset, valueFid, offset);
5978b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    }
5988b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    return result;
5998b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes}
6008b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes
601c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) {
602c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
603c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    u_char byte = value;
604c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &byte, sizeof(byte))));
605c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes}
606c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes
607454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic void Posix_setsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) {
608454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
609454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))));
610454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes}
611454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes
612438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughesstatic void Posix_setsockoptGroupReq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaGroupReq) {
613438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    struct group_req value;
614438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes
615438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    static jfieldID grInterfaceFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_interface", "I");
616438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    value.gr_interface = env->GetIntField(javaGroupReq, grInterfaceFid);
617438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    // Get the IPv4 or IPv6 multicast address to join or leave.
618438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    static jfieldID grGroupFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_group", "Ljava/net/InetAddress;");
619438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    jobject javaGroup = env->GetObjectField(javaGroupReq, grGroupFid);
620438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    if (!inetAddressToSocketAddress(env, javaGroup, 0, &value.gr_group)) {
621438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        return;
622438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    }
623438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes
624438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
625438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    int rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)));
626438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    if (rc == -1 && errno == EINVAL) {
627438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        // Maybe we're a 32-bit binary talking to a 64-bit kernel?
628438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        // glibc doesn't automatically handle this.
629438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        struct GCC_HIDDEN group_req64 {
630438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes            uint32_t gr_interface;
631438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes            uint32_t my_padding;
632438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes            sockaddr_storage gr_group;
633438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        };
634438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        group_req64 value64;
635438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        value64.gr_interface = value.gr_interface;
636438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        memcpy(&value64.gr_group, &value.gr_group, sizeof(value.gr_group));
637438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value64, sizeof(value64)));
638438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    }
639438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    throwIfMinusOne(env, "setsockopt", rc);
640438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes}
641438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes
642c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaLinger) {
643c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID lOnoffFid = env->GetFieldID(JniConstants::structLingerClass, "l_onoff", "I");
644c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID lLingerFid = env->GetFieldID(JniConstants::structLingerClass, "l_linger", "I");
645c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
646c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    struct linger value;
647c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    value.l_onoff = env->GetIntField(javaLinger, lOnoffFid);
648c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    value.l_linger = env->GetIntField(javaLinger, lLingerFid);
649c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))));
650c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes}
651c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes
652c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaTimeval) {
653c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID tvSecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_sec", "J");
654c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID tvUsecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_usec", "J");
655c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
656c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    struct timeval value;
657c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    value.tv_sec = env->GetIntField(javaTimeval, tvSecFid);
658c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    value.tv_usec = env->GetIntField(javaTimeval, tvUsecFid);
659c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))));
660c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes}
661c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes
66259e4744d27231f260271dbbca406e0cc39768116Elliott Hughesstatic void Posix_shutdown(JNIEnv* env, jobject, jobject javaFd, jint how) {
66359e4744d27231f260271dbbca406e0cc39768116Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
66459e4744d27231f260271dbbca406e0cc39768116Elliott Hughes    throwIfMinusOne(env, "shutdown", TEMP_FAILURE_RETRY(shutdown(fd, how)));
66559e4744d27231f260271dbbca406e0cc39768116Elliott Hughes}
66659e4744d27231f260271dbbca406e0cc39768116Elliott Hughes
667454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic jobject Posix_socket(JNIEnv* env, jobject, jint domain, jint type, jint protocol) {
668454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    int fd = throwIfMinusOne(env, "socket", TEMP_FAILURE_RETRY(socket(domain, type, protocol)));
669454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL;
670454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes}
671454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes
67247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_stat(JNIEnv* env, jobject, jstring javaPath) {
67347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return doStat(env, javaPath, false);
67447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
67547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
6760ade5524d972b9dfd69b0126aa4c35319c1b7fa4Elliott Hughesstatic jobject Posix_statfs(JNIEnv* env, jobject, jstring javaPath) {
67759fa7163774d6930a174bc038414a4b780581957Elliott Hughes    ScopedUtfChars path(env, javaPath);
67859fa7163774d6930a174bc038414a4b780581957Elliott Hughes    if (path.c_str() == NULL) {
67959fa7163774d6930a174bc038414a4b780581957Elliott Hughes        return NULL;
68059fa7163774d6930a174bc038414a4b780581957Elliott Hughes    }
68159fa7163774d6930a174bc038414a4b780581957Elliott Hughes    struct statfs sb;
68259fa7163774d6930a174bc038414a4b780581957Elliott Hughes    int rc = TEMP_FAILURE_RETRY(statfs(path.c_str(), &sb));
68359fa7163774d6930a174bc038414a4b780581957Elliott Hughes    if (rc == -1) {
68459fa7163774d6930a174bc038414a4b780581957Elliott Hughes        throwErrnoException(env, "statfs");
68559fa7163774d6930a174bc038414a4b780581957Elliott Hughes        return NULL;
68659fa7163774d6930a174bc038414a4b780581957Elliott Hughes    }
68759fa7163774d6930a174bc038414a4b780581957Elliott Hughes    return makeStructStatFs(env, sb);
68859fa7163774d6930a174bc038414a4b780581957Elliott Hughes}
68959fa7163774d6930a174bc038414a4b780581957Elliott Hughes
690ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jstring Posix_strerror(JNIEnv* env, jobject, jint errnum) {
69152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    char buffer[BUFSIZ];
692ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    const char* message = jniStrError(errnum, buffer, sizeof(buffer));
693ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return env->NewStringUTF(message);
694ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
695ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
696a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_symlink(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) {
697a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars oldPath(env, javaOldPath);
698a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (oldPath.c_str() == NULL) {
699a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
700a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
701a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars newPath(env, javaNewPath);
702a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (newPath.c_str() == NULL) {
703a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
704a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
705a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    throwIfMinusOne(env, "symlink", TEMP_FAILURE_RETRY(symlink(oldPath.c_str(), newPath.c_str())));
706a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes}
707a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes
7086fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughesstatic jlong Posix_sysconf(JNIEnv* env, jobject, jint name) {
7096fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    // Since -1 is a valid result from sysconf(3), detecting failure is a little more awkward.
7106fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    errno = 0;
7116fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    long result = sysconf(name);
7126fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    if (result == -1L && errno == EINVAL) {
7137e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "sysconf");
7146fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    }
7156fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    return result;
7166fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes}
7176fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes
7187341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject Posix_uname(JNIEnv* env, jobject) {
7197341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    struct utsname buf;
7207341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    if (throwIfMinusOne(env, "uname", TEMP_FAILURE_RETRY(uname(&buf))) == -1) {
7217341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes        return NULL;
7227341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    }
7237341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    return makeStructUtsname(env, buf);
7247341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes}
7257341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
72678c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughesstatic jint Posix_write(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount) {
72778c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
72878c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    ScopedByteArrayRO bytes(env, javaBytes);
72978c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    if (bytes.get() == NULL) {
73078c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes        return -1;
73178c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    }
73278c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    return throwIfMinusOne(env, "write", TEMP_FAILURE_RETRY(write(fd, bytes.get() + byteOffset, byteCount)));
73378c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes}
73478c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes
73578c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughesstatic jint Posix_writeDirectBuffer(JNIEnv* env, jobject, jobject javaFd, jobject byteBuffer, jint position, jint remaining) {
73678c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
73778c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    jbyte* ptr = reinterpret_cast<jbyte*>(env->GetDirectBufferAddress(byteBuffer));
73878c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    return throwIfMinusOne(env, "write", TEMP_FAILURE_RETRY(write(fd, ptr + position, remaining)));
73978c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes}
74078c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes
741bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_writev(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) {
742bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    IoVec<ScopedByteArrayRO> ioVec(env, env->GetArrayLength(buffers));
743bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    if (!ioVec.init(buffers, offsets, byteCounts)) {
744bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return -1;
745bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
746bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
747bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    return throwIfMinusOne(env, "writev", TEMP_FAILURE_RETRY(writev(fd, ioVec.get(), ioVec.size())));
748bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes}
749bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
750ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic JNINativeMethod gMethods[] = {
751ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, access, "(Ljava/lang/String;I)Z"),
752b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    NATIVE_METHOD(Posix, chmod, "(Ljava/lang/String;I)V"),
753462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    NATIVE_METHOD(Posix, close, "(Ljava/io/FileDescriptor;)V"),
754ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, environ, "()[Ljava/lang/String;"),
755fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    NATIVE_METHOD(Posix, fcntlVoid, "(Ljava/io/FileDescriptor;I)I"),
756fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    NATIVE_METHOD(Posix, fcntlLong, "(Ljava/io/FileDescriptor;IJ)I"),
757fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    NATIVE_METHOD(Posix, fcntlFlock, "(Ljava/io/FileDescriptor;ILlibcore/io/StructFlock;)I"),
75852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    NATIVE_METHOD(Posix, fdatasync, "(Ljava/io/FileDescriptor;)V"),
75947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, fstat, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStat;"),
76059fa7163774d6930a174bc038414a4b780581957Elliott Hughes    NATIVE_METHOD(Posix, fstatfs, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStatFs;"),
76152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    NATIVE_METHOD(Posix, fsync, "(Ljava/io/FileDescriptor;)V"),
762f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    NATIVE_METHOD(Posix, ftruncate, "(Ljava/io/FileDescriptor;J)V"),
763ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    NATIVE_METHOD(Posix, getenv, "(Ljava/lang/String;)Ljava/lang/String;"),
7640a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockname, "(Ljava/io/FileDescriptor;)Ljava/net/SocketAddress;"),
7650a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptByte, "(Ljava/io/FileDescriptor;II)I"),
7660a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptInAddr, "(Ljava/io/FileDescriptor;II)Ljava/net/InetAddress;"),
7670a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptInt, "(Ljava/io/FileDescriptor;II)I"),
7680a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptLinger, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructLinger;"),
7690a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptTimeval, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructTimeval;"),
7708b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    NATIVE_METHOD(Posix, ioctlInt, "(Ljava/io/FileDescriptor;ILlibcore/util/MutableInt;)I"),
7719a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    NATIVE_METHOD(Posix, isatty, "(Ljava/io/FileDescriptor;)Z"),
772e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes    NATIVE_METHOD(Posix, listen, "(Ljava/io/FileDescriptor;I)V"),
773dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    NATIVE_METHOD(Posix, lseek, "(Ljava/io/FileDescriptor;JI)J"),
77447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, lstat, "(Ljava/lang/String;)Llibcore/io/StructStat;"),
7750f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    NATIVE_METHOD(Posix, mincore, "(JJ[B)V"),
776c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    NATIVE_METHOD(Posix, mkdir, "(Ljava/lang/String;I)V"),
7777e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    NATIVE_METHOD(Posix, mlock, "(JJ)V"),
7787e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, mmap, "(JJIILjava/io/FileDescriptor;J)J"),
7797e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, msync, "(JJI)V"),
7807e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    NATIVE_METHOD(Posix, munlock, "(JJ)V"),
7817e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, munmap, "(JJ)V"),
7820ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    NATIVE_METHOD(Posix, open, "(Ljava/lang/String;II)Ljava/io/FileDescriptor;"),
78341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    NATIVE_METHOD(Posix, pipe, "()[Ljava/io/FileDescriptor;"),
78426c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    NATIVE_METHOD(Posix, read, "(Ljava/io/FileDescriptor;[BII)I"),
78526c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    NATIVE_METHOD(Posix, readDirectBuffer, "(Ljava/io/FileDescriptor;Ljava/nio/ByteBuffer;II)I"),
786bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    NATIVE_METHOD(Posix, readv, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"),
787c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    NATIVE_METHOD(Posix, remove, "(Ljava/lang/String;)V"),
788a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    NATIVE_METHOD(Posix, rename, "(Ljava/lang/String;Ljava/lang/String;)V"),
7898b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    NATIVE_METHOD(Posix, sendfile, "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Llibcore/util/MutableLong;J)J"),
790c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    NATIVE_METHOD(Posix, setsockoptByte, "(Ljava/io/FileDescriptor;III)V"),
791454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    NATIVE_METHOD(Posix, setsockoptInt, "(Ljava/io/FileDescriptor;III)V"),
792438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    NATIVE_METHOD(Posix, setsockoptGroupReq, "(Ljava/io/FileDescriptor;IILlibcore/io/StructGroupReq;)V"),
793c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    NATIVE_METHOD(Posix, setsockoptLinger, "(Ljava/io/FileDescriptor;IILlibcore/io/StructLinger;)V"),
794c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    NATIVE_METHOD(Posix, setsockoptTimeval, "(Ljava/io/FileDescriptor;IILlibcore/io/StructTimeval;)V"),
79559e4744d27231f260271dbbca406e0cc39768116Elliott Hughes    NATIVE_METHOD(Posix, shutdown, "(Ljava/io/FileDescriptor;I)V"),
796454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    NATIVE_METHOD(Posix, socket, "(III)Ljava/io/FileDescriptor;"),
79747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, stat, "(Ljava/lang/String;)Llibcore/io/StructStat;"),
79859fa7163774d6930a174bc038414a4b780581957Elliott Hughes    NATIVE_METHOD(Posix, statfs, "(Ljava/lang/String;)Llibcore/io/StructStatFs;"),
799ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, strerror, "(I)Ljava/lang/String;"),
800a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    NATIVE_METHOD(Posix, symlink, "(Ljava/lang/String;Ljava/lang/String;)V"),
8016fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    NATIVE_METHOD(Posix, sysconf, "(I)J"),
8027341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    NATIVE_METHOD(Posix, uname, "()Llibcore/io/StructUtsname;"),
80378c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    NATIVE_METHOD(Posix, write, "(Ljava/io/FileDescriptor;[BII)I"),
80478c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    NATIVE_METHOD(Posix, writeDirectBuffer, "(Ljava/io/FileDescriptor;Ljava/nio/ByteBuffer;II)I"),
805bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    NATIVE_METHOD(Posix, writev, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"),
806ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes};
807ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesint register_libcore_io_Posix(JNIEnv* env) {
808ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    return jniRegisterNativeMethods(env, "libcore/io/Posix", gMethods, NELEM(gMethods));
809ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes}
810