libcore_io_Linux.cpp revision 396a9c666da353b910b515d12eb1c43adfddb0c8
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
19996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes#include "AsynchronousSocketCloseMonitor.h"
20ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "JNIHelp.h"
21ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "JniConstants.h"
22ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include "JniException.h"
230a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include "NetworkUtilities.h"
240568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes#include "ScopedBytes.h"
25e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes#include "ScopedLocalRef.h"
260f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes#include "ScopedPrimitiveArray.h"
27ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "ScopedUtfChars.h"
2859fa7163774d6930a174bc038414a4b780581957Elliott Hughes#include "StaticAssert.h"
291c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes#include "UniquePtr.h"
30ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include "toStringArray.h"
31ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
321c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes#include <arpa/inet.h>
33ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include <errno.h>
340ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <fcntl.h>
35b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes#include <net/if.h>
364f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes#include <netdb.h>
370a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include <netinet/in.h>
381c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes#include <netinet/in.h>
3970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes#include <poll.h>
40a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes#include <signal.h>
41ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include <stdlib.h>
42461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes#include <sys/ioctl.h>
437e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes#include <sys/mman.h>
448b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes#include <sys/sendfile.h>
4559e4744d27231f260271dbbca406e0cc39768116Elliott Hughes#include <sys/socket.h>
461c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes#include <sys/socket.h>
4747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes#include <sys/stat.h>
480a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include <sys/time.h>
490ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <sys/types.h>
50bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes#include <sys/uio.h>
517341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#include <sys/utsname.h>
5259fa7163774d6930a174bc038414a4b780581957Elliott Hughes#include <sys/vfs.h> // Bionic doesn't have <sys/statvfs.h>
530ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <unistd.h>
54ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
551c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughesstruct addrinfo_deleter {
561c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    void operator()(addrinfo* p) const {
571c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        if (p != NULL) { // bionic's freeaddrinfo(3) crashes when passed NULL.
581c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            freeaddrinfo(p);
591c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        }
601c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
611c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes};
621c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
63996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes/**
64996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * Used to retry syscalls that can return EINTR. This differs from TEMP_FAILURE_RETRY in that
65996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * it also considers the case where the reason for failure is that another thread called
66996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * Socket.close.
67996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes *
68996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * Assumes 'JNIEnv* env' and 'jobject javaFd' (which is a java.io.FileDescriptor) are in scope.
69996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes *
70996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * Returns the result of 'exp', though a Java exception will be pending if the result is -1.
71996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes *
72996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * Correct usage looks like this:
73996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes *
74996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * void Posix_syscall(JNIEnv* env, jobject javaFd, ...) {
75996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes *     ...
76996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes *     int fd;
77996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes *     NET_FAILURE_RETRY("syscall", syscall(fd, ...)); // Throws on error.
78996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * }
79996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes */
80996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes#define NET_FAILURE_RETRY(syscall_name, exp) ({ \
81996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    typeof (exp) _rc = -1; \
82996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    do { \
83996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes        { \
84996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes            fd = jniGetFDFromFileDescriptor(env, javaFd); \
85996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes            AsynchronousSocketCloseMonitor monitor(fd); \
86996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes            _rc = (exp); \
87996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes        } \
88996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes        if (_rc == -1) { \
89996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes            if (jniGetFDFromFileDescriptor(env, javaFd) == -1) { \
90996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes                jniThrowException(env, "java/net/SocketException", "Socket closed"); \
91996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes                break; \
92996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes            } else if (errno != EINTR) { \
93996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes                throwErrnoException(env, syscall_name); \
94996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes                break; \
95996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes            } \
96996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes        } \
97996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    } while (_rc == -1); \
98996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    _rc; })
99996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes
1004f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwException(JNIEnv* env, jclass exceptionClass, jmethodID ctor3, jmethodID ctor2,
1014f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        const char* functionName, int error) {
102ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    jthrowable cause = NULL;
103ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (env->ExceptionCheck()) {
104ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        cause = env->ExceptionOccurred();
105ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        env->ExceptionClear();
106ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
107ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
1084f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName));
1094f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    if (detailMessage.get() == NULL) {
110f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        // Not really much we can do here. We're probably dead in the water,
111f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        // but let's try to stumble on...
112f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        env->ExceptionClear();
113f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    }
114f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes
115ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    jobject exception;
116ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (cause != NULL) {
1174f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        exception = env->NewObject(exceptionClass, ctor3, detailMessage.get(), error, cause);
118ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    } else {
1194f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        exception = env->NewObject(exceptionClass, ctor2, detailMessage.get(), error);
120ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
121ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    env->Throw(reinterpret_cast<jthrowable>(exception));
122dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes}
123dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes
1244f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwErrnoException(JNIEnv* env, const char* functionName) {
1254f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    int error = errno;
1264f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    static jmethodID ctor3 = env->GetMethodID(JniConstants::errnoExceptionClass,
1274f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V");
1284f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    static jmethodID ctor2 = env->GetMethodID(JniConstants::errnoExceptionClass,
1294f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            "<init>", "(Ljava/lang/String;I)V");
1304f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    throwException(env, JniConstants::errnoExceptionClass, ctor3, ctor2, functionName, error);
1314f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes}
1324f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes
1334f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwGaiException(JNIEnv* env, const char* functionName, int error) {
1344f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    if (error == EAI_SYSTEM) {
1354f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        // EAI_SYSTEM means "look at errno instead", so we want our GaiException to have the
1364f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        // relevant ErrnoException as its cause.
1374f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        throwErrnoException(env, functionName);
1384f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        // Deliberately fall through to throw another exception...
1394f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    }
1404f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    static jmethodID ctor3 = env->GetMethodID(JniConstants::gaiExceptionClass,
1414f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V");
1424f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    static jmethodID ctor2 = env->GetMethodID(JniConstants::gaiExceptionClass,
1434f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            "<init>", "(Ljava/lang/String;I)V");
1444f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    throwException(env, JniConstants::gaiExceptionClass, ctor3, ctor2, functionName, error);
1454f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes}
1464f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes
147dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughestemplate <typename rc_t>
1487e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic rc_t throwIfMinusOne(JNIEnv* env, const char* name, rc_t rc) {
149dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == rc_t(-1)) {
1507e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, name);
151dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    }
152dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    return rc;
15347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
15447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
1558dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughestemplate <typename ScopedT>
156bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesclass IoVec {
157bbac92e691de7d570928ddfba639067978e55b06Elliott Hughespublic:
158bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    IoVec(JNIEnv* env, size_t bufferCount) : mEnv(env), mBufferCount(bufferCount) {
159bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
160bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
161bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    bool init(jobjectArray javaBuffers, jintArray javaOffsets, jintArray javaByteCounts) {
162bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        // We can't delete our local references until after the I/O, so make sure we have room.
163bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (mEnv->PushLocalFrame(mBufferCount + 16) < 0) {
164bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            return false;
165bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
166bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        ScopedIntArrayRO offsets(mEnv, javaOffsets);
167bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (offsets.get() == NULL) {
168bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            return false;
169bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
170bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        ScopedIntArrayRO byteCounts(mEnv, javaByteCounts);
171bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (byteCounts.get() == NULL) {
172bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            return false;
173bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
174bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        // TODO: Linux actually has a 1024 buffer limit. glibc works around this, and we should too.
175bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        for (size_t i = 0; i < mBufferCount; ++i) {
176e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes            jobject buffer = mEnv->GetObjectArrayElement(javaBuffers, i); // We keep this local ref.
1778dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes            mScopedBuffers.push_back(new ScopedT(mEnv, buffer));
1788dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes            jbyte* ptr = const_cast<jbyte*>(mScopedBuffers.back()->get());
179bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            if (ptr == NULL) {
180bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                return false;
181bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            }
182bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            struct iovec iov;
183bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            iov.iov_base = reinterpret_cast<void*>(ptr + offsets[i]);
184bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            iov.iov_len = byteCounts[i];
185bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            mIoVec.push_back(iov);
186bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
187bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return true;
188bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
189bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
190bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    ~IoVec() {
1918dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes        for (size_t i = 0; i < mScopedBuffers.size(); ++i) {
1928dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes            delete mScopedBuffers[i];
193bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
194bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        mEnv->PopLocalFrame(NULL);
195bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
196bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
197bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    iovec* get() {
198bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return &mIoVec[0];
199bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
200bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
201bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    size_t size() {
202bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return mBufferCount;
203bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
204bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
205bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesprivate:
206bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    JNIEnv* mEnv;
207bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    size_t mBufferCount;
208bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    std::vector<iovec> mIoVec;
2098dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes    std::vector<ScopedT*> mScopedBuffers;
210bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes};
211bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
2120a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeInetSocketAddress(JNIEnv* env, const sockaddr_storage* ss, int port) {
2130a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    jobject inetAddress = socketAddressToInetAddress(env, ss);
2140a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (inetAddress == NULL) {
2150a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
2160a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
2170a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::inetSocketAddressClass, "<init>",
2180a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes            "(Ljava/net/InetAddress;I)V");
2190a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return env->NewObject(JniConstants::inetSocketAddressClass, ctor, inetAddress, port);
2200a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
2210a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
2220a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeSocketAddress(JNIEnv* env, const sockaddr_storage* ss) {
2230a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (ss->ss_family == AF_INET) {
2240a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        int port = ntohs(reinterpret_cast<const sockaddr_in*>(ss)->sin_port);
2250a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return makeInetSocketAddress(env, ss, port);
2260a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    } else if (ss->ss_family == AF_INET6) {
2270a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        int port = ntohs(reinterpret_cast<const sockaddr_in6*>(ss)->sin6_port);
2280a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return makeInetSocketAddress(env, ss, port);
2290a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
2300a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    // TODO: support AF_UNIX and AF_UNSPEC, and have some other behavior for other families
2310a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return NULL;
2320a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
2330a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
23447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject makeStructStat(JNIEnv* env, const struct stat& sb) {
23547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structStatClass, "<init>",
23647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes            "(JJIJIIJJJJJJJ)V");
23747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return env->NewObject(JniConstants::structStatClass, ctor,
23859fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_dev), static_cast<jlong>(sb.st_ino),
23959fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jint>(sb.st_mode), static_cast<jlong>(sb.st_nlink),
24059fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jint>(sb.st_uid), static_cast<jint>(sb.st_gid),
24159fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_rdev), static_cast<jlong>(sb.st_size),
24259fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_atime), static_cast<jlong>(sb.st_mtime),
24359fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_ctime), static_cast<jlong>(sb.st_blksize),
24459fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_blocks));
24559fa7163774d6930a174bc038414a4b780581957Elliott Hughes}
24659fa7163774d6930a174bc038414a4b780581957Elliott Hughes
24759fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject makeStructStatFs(JNIEnv* env, const struct statfs& sb) {
24859fa7163774d6930a174bc038414a4b780581957Elliott Hughes    STATIC_ASSERT(sizeof(sb.f_bavail) == sizeof(jlong), statfs_not_64_bit);
24959fa7163774d6930a174bc038414a4b780581957Elliott Hughes    STATIC_ASSERT(sizeof(sb.f_bfree) == sizeof(jlong), statfs_not_64_bit);
25059fa7163774d6930a174bc038414a4b780581957Elliott Hughes    STATIC_ASSERT(sizeof(sb.f_blocks) == sizeof(jlong), statfs_not_64_bit);
25159fa7163774d6930a174bc038414a4b780581957Elliott Hughes
25259fa7163774d6930a174bc038414a4b780581957Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structStatFsClass, "<init>",
25359fa7163774d6930a174bc038414a4b780581957Elliott Hughes            "(JJJJJJJJ)V");
25459fa7163774d6930a174bc038414a4b780581957Elliott Hughes    return env->NewObject(JniConstants::structStatFsClass, ctor, static_cast<jlong>(sb.f_bsize),
25559fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_blocks), static_cast<jlong>(sb.f_bfree),
25659fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_bavail), static_cast<jlong>(sb.f_files),
25759fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_ffree), static_cast<jlong>(sb.f_namelen),
25859fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_frsize));
25947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
26047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
2610a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructLinger(JNIEnv* env, const struct linger& l) {
2620a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structLingerClass, "<init>", "(II)V");
2630a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return env->NewObject(JniConstants::structLingerClass, ctor, l.l_onoff, l.l_linger);
2640a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
2650a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
2660a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructTimeval(JNIEnv* env, const struct timeval& tv) {
2670a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structTimevalClass, "<init>", "(JJ)V");
2680a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return env->NewObject(JniConstants::structTimevalClass, ctor,
2690a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes            static_cast<jlong>(tv.tv_sec), static_cast<jlong>(tv.tv_usec));
2700a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
2710a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
2727341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject makeStructUtsname(JNIEnv* env, const struct utsname& buf) {
2737341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#define TO_JAVA_STRING(NAME) \
2747341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes        jstring NAME = env->NewStringUTF(buf. NAME); \
2757341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes        if (NAME == NULL) return NULL;
2767341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
2777341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(sysname);
2787341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(nodename);
2797341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(release);
2807341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(version);
2817341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(machine);
2827341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#undef TO_JAVA_STRING
2837341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
2847341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structUtsnameClass, "<init>",
2857341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes            "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
2867341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    return env->NewObject(JniConstants::structUtsnameClass, ctor,
2877341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes            sysname, nodename, release, version, machine);
2887341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes};
2897341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
290a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic bool fillIfreq(JNIEnv* env, jstring javaInterfaceName, struct ifreq& req) {
291a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    ScopedUtfChars interfaceName(env, javaInterfaceName);
292a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    if (interfaceName.c_str() == NULL) {
293a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes        return false;
294a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    }
295a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    memset(&req, 0, sizeof(req));
296a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    strncpy(req.ifr_name, interfaceName.c_str(), sizeof(req.ifr_name));
297a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    req.ifr_name[sizeof(req.ifr_name) - 1] = '\0';
298a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    return true;
299a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes}
300a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes
30147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject doStat(JNIEnv* env, jstring javaPath, bool isLstat) {
30247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    ScopedUtfChars path(env, javaPath);
30347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    if (path.c_str() == NULL) {
30447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
30547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
30647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    struct stat sb;
30747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = isLstat ? TEMP_FAILURE_RETRY(lstat(path.c_str(), &sb))
30847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes                     : TEMP_FAILURE_RETRY(stat(path.c_str(), &sb));
309dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
3107e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, isLstat ? "lstat" : "stat");
31147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
31247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
31347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return makeStructStat(env, sb);
314ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
315ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
316ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jboolean Posix_access(JNIEnv* env, jobject, jstring javaPath, jint mode) {
317ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    ScopedUtfChars path(env, javaPath);
318ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (path.c_str() == NULL) {
319ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        return JNI_FALSE;
320ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
32147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = TEMP_FAILURE_RETRY(access(path.c_str(), mode));
322dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
3237e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "access");
324dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    }
325ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return (rc == 0);
326ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
327ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
328da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughesstatic void Posix_bind(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) {
329da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes    sockaddr_storage ss;
330da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes    if (!inetAddressToSockaddr(env, javaAddress, port, &ss)) {
331da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes        return;
332da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes    }
333996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    int fd;
334da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes    const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss);
335996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    NET_FAILURE_RETRY("bind", bind(fd, sa, sizeof(sockaddr_storage)));
336da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes}
337da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes
338b7190190e0ef8de883c952efb319ce7748831faaElliott Hughesstatic void Posix_chmod(JNIEnv* env, jobject, jstring javaPath, jint mode) {
339b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    ScopedUtfChars path(env, javaPath);
340b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    if (path.c_str() == NULL) {
341b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes        return;
342b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    }
343b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    throwIfMinusOne(env, "chmod", TEMP_FAILURE_RETRY(chmod(path.c_str(), mode)));
344b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes}
345b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes
346462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughesstatic void Posix_close(JNIEnv* env, jobject, jobject javaFd) {
347462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // Get the FileDescriptor's 'fd' field and clear it.
348462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // We need to do this before we can throw an IOException (http://b/3222087).
349462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
350462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    jniSetFileDescriptorOfFD(env, javaFd, -1);
351462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes
352462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // Even if close(2) fails with EINTR, the fd will have been closed.
353462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone else's fd.
354462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
355462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    throwIfMinusOne(env, "close", close(fd));
356462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes}
357462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes
358996bf79565ac88402920bd826d6f85952c83be20Elliott Hughesstatic void Posix_connect(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) {
359996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    sockaddr_storage ss;
360996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    if (!inetAddressToSockaddr(env, javaAddress, port, &ss)) {
361996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes        return;
362996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    }
363996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    int fd;
364996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss);
365996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    NET_FAILURE_RETRY("connect", connect(fd, sa, sizeof(sockaddr_storage)));
366996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes}
367996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes
368396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jobject Posix_dup(JNIEnv* env, jobject, jobject javaOldFd) {
369396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    int oldFd = jniGetFDFromFileDescriptor(env, javaOldFd);
370396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    int newFd = throwIfMinusOne(env, "dup", TEMP_FAILURE_RETRY(dup(oldFd)));
371396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    return (newFd != -1) ? jniCreateFileDescriptor(env, newFd) : NULL;
372396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
373396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
374396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jobject Posix_dup2(JNIEnv* env, jobject, jobject javaOldFd, jint newFd) {
375396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    int oldFd = jniGetFDFromFileDescriptor(env, javaOldFd);
376396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    int fd = throwIfMinusOne(env, "dup2", TEMP_FAILURE_RETRY(dup2(oldFd, newFd)));
377396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    return (fd != -1) ? jniCreateFileDescriptor(env, fd) : NULL;
378396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
379396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
380ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jobjectArray Posix_environ(JNIEnv* env, jobject) {
381ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    extern char** environ; // Standard, but not in any header file.
382ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return toStringArray(env, environ);
383ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
384ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
385fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlVoid(JNIEnv* env, jobject, jobject javaFd, jint cmd) {
386fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
387fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd)));
388fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes}
389fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
390fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlLong(JNIEnv* env, jobject, jobject javaFd, jint cmd, jlong arg) {
391fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
392fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, arg)));
393fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes}
394fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
395fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlFlock(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaFlock) {
396fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID typeFid = env->GetFieldID(JniConstants::structFlockClass, "l_type", "S");
397fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID whenceFid = env->GetFieldID(JniConstants::structFlockClass, "l_whence", "S");
398fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID startFid = env->GetFieldID(JniConstants::structFlockClass, "l_start", "J");
399fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID lenFid = env->GetFieldID(JniConstants::structFlockClass, "l_len", "J");
400fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID pidFid = env->GetFieldID(JniConstants::structFlockClass, "l_pid", "I");
401fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
402fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    struct flock64 lock;
403fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    memset(&lock, 0, sizeof(lock));
404fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_type = env->GetShortField(javaFlock, typeFid);
405fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_whence = env->GetShortField(javaFlock, whenceFid);
406fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_start = env->GetLongField(javaFlock, startFid);
407fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_len = env->GetLongField(javaFlock, lenFid);
408fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_pid = env->GetIntField(javaFlock, pidFid);
409fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
410fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
411fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int rc = throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, &lock)));
412fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    if (rc != -1) {
413fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetShortField(javaFlock, typeFid, lock.l_type);
414fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetShortField(javaFlock, whenceFid, lock.l_whence);
415fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetLongField(javaFlock, startFid, lock.l_start);
416fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetLongField(javaFlock, lenFid, lock.l_len);
417fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetIntField(javaFlock, pidFid, lock.l_pid);
418fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    }
419fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    return rc;
420fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes}
421fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
42252724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fdatasync(JNIEnv* env, jobject, jobject javaFd) {
42352724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4247e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "fdatasync", TEMP_FAILURE_RETRY(fdatasync(fd)));
42552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes}
42652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes
42747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_fstat(JNIEnv* env, jobject, jobject javaFd) {
42847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
42947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    struct stat sb;
43047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = TEMP_FAILURE_RETRY(fstat(fd, &sb));
431dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
4327e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "fstat");
43347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
43447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
43547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return makeStructStat(env, sb);
43647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
43747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
43859fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject Posix_fstatfs(JNIEnv* env, jobject, jobject javaFd) {
43959fa7163774d6930a174bc038414a4b780581957Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
44059fa7163774d6930a174bc038414a4b780581957Elliott Hughes    struct statfs sb;
44159fa7163774d6930a174bc038414a4b780581957Elliott Hughes    int rc = TEMP_FAILURE_RETRY(fstatfs(fd, &sb));
44259fa7163774d6930a174bc038414a4b780581957Elliott Hughes    if (rc == -1) {
44359fa7163774d6930a174bc038414a4b780581957Elliott Hughes        throwErrnoException(env, "fstatfs");
44459fa7163774d6930a174bc038414a4b780581957Elliott Hughes        return NULL;
44559fa7163774d6930a174bc038414a4b780581957Elliott Hughes    }
44659fa7163774d6930a174bc038414a4b780581957Elliott Hughes    return makeStructStatFs(env, sb);
44759fa7163774d6930a174bc038414a4b780581957Elliott Hughes}
44859fa7163774d6930a174bc038414a4b780581957Elliott Hughes
44952724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fsync(JNIEnv* env, jobject, jobject javaFd) {
45052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4517e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "fsync", TEMP_FAILURE_RETRY(fsync(fd)));
452f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes}
453f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes
454f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughesstatic void Posix_ftruncate(JNIEnv* env, jobject, jobject javaFd, jlong length) {
455f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4567e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "ftruncate", TEMP_FAILURE_RETRY(ftruncate64(fd, length)));
45752724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes}
45852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes
4594f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic jstring Posix_gai_strerror(JNIEnv* env, jobject, jint error) {
4604f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    return env->NewStringUTF(gai_strerror(error));
4614f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes}
4624f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes
4631c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughesstatic jobjectArray Posix_getaddrinfo(JNIEnv* env, jobject, jstring javaNode, jobject javaHints) {
4641c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    ScopedUtfChars node(env, javaNode);
4651c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    if (node.c_str() == NULL) {
4661c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return NULL;
4671c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
4681c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
4691c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    static jfieldID flagsFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_flags", "I");
4701c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    static jfieldID familyFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_family", "I");
4711c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    static jfieldID socktypeFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_socktype", "I");
4721c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    static jfieldID protocolFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_protocol", "I");
4731c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
4741c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    addrinfo hints;
4751c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    memset(&hints, 0, sizeof(hints));
4761c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    hints.ai_flags = env->GetIntField(javaHints, flagsFid);
4771c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    hints.ai_family = env->GetIntField(javaHints, familyFid);
4781c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    hints.ai_socktype = env->GetIntField(javaHints, socktypeFid);
4791c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    hints.ai_protocol = env->GetIntField(javaHints, protocolFid);
4801c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
4811c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    addrinfo* addressList = NULL;
4821c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    int rc = getaddrinfo(node.c_str(), NULL, &hints, &addressList);
4831c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    UniquePtr<addrinfo, addrinfo_deleter> addressListDeleter(addressList);
4841c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    if (rc != 0) {
4851c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        throwGaiException(env, "getaddrinfo", rc);
4861c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return NULL;
4871c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
4881c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
4891c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    // Count results so we know how to size the output array.
4901c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    int addressCount = 0;
4911c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    for (addrinfo* ai = addressList; ai != NULL; ai = ai->ai_next) {
4921c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) {
4931c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            ++addressCount;
4941c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        } else {
4951c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            LOGE("getaddrinfo unexpected ai_family %i", ai->ai_family);
4961c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        }
4971c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
4981c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    if (addressCount == 0) {
4991c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return NULL;
5001c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
5011c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
5021c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    // Prepare output array.
5031c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    jobjectArray result = env->NewObjectArray(addressCount, JniConstants::inetAddressClass, NULL);
5041c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    if (result == NULL) {
5051c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return NULL;
5061c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
5071c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
5081c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    // Examine returned addresses one by one, save them in the output array.
5091c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    int index = 0;
5101c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    for (addrinfo* ai = addressList; ai != NULL; ai = ai->ai_next) {
5111c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) {
5121c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            // Unknown address family. Skip this address.
5131c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            LOGE("getaddrinfo unexpected ai_family %i", ai->ai_family);
5141c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            continue;
5151c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        }
5161c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
5171c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        // Convert each IP address into a Java byte array.
5181c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        sockaddr_storage* address = reinterpret_cast<sockaddr_storage*>(ai->ai_addr);
5191c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        ScopedLocalRef<jobject> inetAddress(env, socketAddressToInetAddress(env, address));
5201c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        if (inetAddress.get() == NULL) {
5211c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            return NULL;
5221c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        }
5231c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        env->SetObjectArrayElement(result, index, inetAddress.get());
5241c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        ++index;
5251c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
5261c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    return result;
5271c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes}
5281c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
529396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getegid(JNIEnv*, jobject) {
530396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    return getegid();
531396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
532396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
533396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_geteuid(JNIEnv*, jobject) {
534396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    return geteuid();
535396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
536396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
537396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getgid(JNIEnv*, jobject) {
538396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    return getgid();
539396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
540396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
541ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic jstring Posix_getenv(JNIEnv* env, jobject, jstring javaName) {
542ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    ScopedUtfChars name(env, javaName);
543ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    if (name.c_str() == NULL) {
544ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes        return NULL;
545ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    }
546ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    return env->NewStringUTF(getenv(name.c_str()));
547ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes}
548ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
5494f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic jstring Posix_getnameinfo(JNIEnv* env, jobject, jobject javaAddress, jint flags) {
5504f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    sockaddr_storage ss;
551da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes    if (!inetAddressToSockaddr_getnameinfo(env, javaAddress, 0, &ss)) {
5524f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        return NULL;
5534f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    }
5544f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    // TODO: bionic's getnameinfo(3) seems to want its length parameter to be exactly
5554f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    // sizeof(sockaddr_in) for an IPv4 address and sizeof (sockaddr_in6) for an
5564f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    // IPv6 address. Fix getnameinfo so it accepts sizeof(sockaddr_storage), and
5574f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    // then remove this hack.
5584f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    socklen_t size = (ss.ss_family == AF_INET) ? sizeof(sockaddr_in) : sizeof(sockaddr_in6);
5594f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    char buf[NI_MAXHOST]; // NI_MAXHOST is longer than INET6_ADDRSTRLEN.
5604f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    int rc = getnameinfo(reinterpret_cast<sockaddr*>(&ss), size, buf, sizeof(buf), NULL, 0, flags);
5614f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    if (rc != 0) {
5624f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        throwGaiException(env, "getnameinfo", rc);
5634f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        return NULL;
5644f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    }
5654f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    return env->NewStringUTF(buf);
5664f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes}
5674f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes
568396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getpid(JNIEnv*, jobject) {
569396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    return getpid();
570396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
571396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
572396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getppid(JNIEnv*, jobject) {
573396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    return getppid();
574396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
575396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
5760a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockname(JNIEnv* env, jobject, jobject javaFd) {
5770a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
5780a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr_storage ss;
5790a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr* sa = reinterpret_cast<sockaddr*>(&ss);
5800a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t byteCount = sizeof(ss);
5810a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&ss, 0, byteCount);
5820a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockname(fd, sa, &byteCount));
5830a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
5840a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockname");
5850a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
5860a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
5870a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return makeSocketAddress(env, &ss);
5880a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
5890a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
5900a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
5910a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
5920a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    u_char result = 0;
5930a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(result);
5940a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size)));
5950a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return result;
5960a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
5970a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
5980a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptInAddr(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
5990a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
6000a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr_storage ss;
6010a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&ss, 0, sizeof(ss));
6020a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    ss.ss_family = AF_INET; // This is only for the IPv4-only IP_MULTICAST_IF.
6030a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr_in* sa = reinterpret_cast<sockaddr_in*>(&ss);
6040a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(sa->sin_addr);
6050a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &sa->sin_addr, &size));
6060a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
6070a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockopt");
6080a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
6090a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
6100a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return socketAddressToInetAddress(env, &ss);
6110a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
6120a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
6130a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
6140a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
6150a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    jint result = 0;
6160a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(result);
6170a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size)));
6180a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return result;
6190a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
6200a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
6210a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
6220a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
6230a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    struct linger l;
6240a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(l);
6250a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&l, 0, size);
6260a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &l, &size));
6270a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
6280a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockopt");
6290a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
6300a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
6310a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return makeStructLinger(env, l);
6320a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
6330a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
6340a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
6350a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
6360a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    struct timeval tv;
6370a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(tv);
6380a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&tv, 0, size);
6390a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &tv, &size));
6400a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
6410a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockopt");
6420a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
6430a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
6440a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return makeStructTimeval(env, tv);
6450a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
6460a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
647396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getuid(JNIEnv*, jobject) {
648396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    return getuid();
649396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
650396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
651a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic jstring Posix_if_indextoname(JNIEnv* env, jobject, jint index) {
652a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    char buf[IF_NAMESIZE];
653a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    char* name = if_indextoname(index, buf);
654a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    // if_indextoname(3) returns NULL on failure, which will come out of NewStringUTF unscathed.
655a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    // There's no useful information in errno, so we don't bother throwing. Callers can null-check.
656a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    return env->NewStringUTF(name);
657a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes}
658a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes
6591c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughesstatic jobject Posix_inet_aton(JNIEnv* env, jobject, jstring javaName) {
6601c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    ScopedUtfChars name(env, javaName);
6611c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    if (name.c_str() == NULL) {
6621c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return NULL;
6631c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
6641c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    sockaddr_storage ss;
6651c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    memset(&ss, 0, sizeof(ss));
6661c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(&ss);
6671c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    if (inet_aton(name.c_str(), &sin->sin_addr) == 0) {
6681c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return NULL;
6691c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
6701c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    sin->sin_family = AF_INET; // inet_aton only supports IPv4.
6711c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    return socketAddressToInetAddress(env, &ss);
6721c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes}
6731c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
674a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic jobject Posix_ioctlInetAddress(JNIEnv* env, jobject, jobject javaFd, jint cmd, jstring javaInterfaceName) {
675a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    struct ifreq req;
676a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    if (!fillIfreq(env, javaInterfaceName, req)) {
677a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes        return NULL;
678a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    }
679a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
680a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &req)));
681a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    if (rc == -1) {
682a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes        return NULL;
683a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    }
684a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    return socketAddressToInetAddress(env, reinterpret_cast<sockaddr_storage*>(&req.ifr_addr));
685a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes}
686a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes
6878b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jint Posix_ioctlInt(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaArg) {
6888b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    // This is complicated because ioctls may return their result by updating their argument
6898b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    // or via their return value, so we need to support both.
690461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
6918b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    static jfieldID valueFid = env->GetFieldID(JniConstants::mutableIntClass, "value", "I");
6928b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    jint arg = env->GetIntField(javaArg, valueFid);
6938b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &arg)));
6948b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    if (!env->ExceptionCheck()) {
6958b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        env->SetIntField(javaArg, valueFid, arg);
6968b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    }
6978b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    return rc;
698461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes}
699461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes
7009a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughesstatic jboolean Posix_isatty(JNIEnv* env, jobject, jobject javaFd) {
7019a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
7029a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    return TEMP_FAILURE_RETRY(isatty(fd)) == 0;
7039a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes}
7049a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes
705a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughesstatic void Posix_kill(JNIEnv* env, jobject, jint pid, jint sig) {
706a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes    throwIfMinusOne(env, "kill", TEMP_FAILURE_RETRY(kill(pid, sig)));
707a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes}
708a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes
709e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughesstatic void Posix_listen(JNIEnv* env, jobject, jobject javaFd, jint backlog) {
710e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
711e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes    throwIfMinusOne(env, "listen", TEMP_FAILURE_RETRY(listen(fd, backlog)));
712e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes}
713e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes
714dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughesstatic jlong Posix_lseek(JNIEnv* env, jobject, jobject javaFd, jlong offset, jint whence) {
715dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
7167e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    return throwIfMinusOne(env, "lseek", TEMP_FAILURE_RETRY(lseek64(fd, offset, whence)));
717dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes}
718dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes
71947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_lstat(JNIEnv* env, jobject, jstring javaPath) {
72047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return doStat(env, javaPath, true);
72147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
72247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
7230f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughesstatic void Posix_mincore(JNIEnv* env, jobject, jlong address, jlong byteCount, jbyteArray javaVector) {
7240f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    ScopedByteArrayRW vector(env, javaVector);
7250f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    if (vector.get() == NULL) {
7260f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes        return;
7270f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    }
7280f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
7290f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    unsigned char* vec = reinterpret_cast<unsigned char*>(vector.get());
7300f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    throwIfMinusOne(env, "mincore", TEMP_FAILURE_RETRY(mincore(ptr, byteCount, vec)));
7310f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes}
7320f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes
733c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_mkdir(JNIEnv* env, jobject, jstring javaPath, jint mode) {
734c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    ScopedUtfChars path(env, javaPath);
735c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    if (path.c_str() == NULL) {
736c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes        return;
737c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    }
738c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    throwIfMinusOne(env, "mkdir", TEMP_FAILURE_RETRY(mkdir(path.c_str(), mode)));
739c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes}
740c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes
7417e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_mlock(JNIEnv* env, jobject, jlong address, jlong byteCount) {
7427e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
7437e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    throwIfMinusOne(env, "mlock", TEMP_FAILURE_RETRY(mlock(ptr, byteCount)));
7447e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes}
7457e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes
7467e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic jlong Posix_mmap(JNIEnv* env, jobject, jlong address, jlong byteCount, jint prot, jint flags, jobject javaFd, jlong offset) {
7477e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
7487e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* suggestedPtr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
7497e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = mmap(suggestedPtr, byteCount, prot, flags, fd, offset);
7507e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    if (ptr == MAP_FAILED) {
7517e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "mmap");
7527e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    }
7537e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    return static_cast<jlong>(reinterpret_cast<uintptr_t>(ptr));
7547e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
7557e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
7567e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_msync(JNIEnv* env, jobject, jlong address, jlong byteCount, jint flags) {
7577e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
7587e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "msync", TEMP_FAILURE_RETRY(msync(ptr, byteCount, flags)));
7597e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
7607e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
7617e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_munlock(JNIEnv* env, jobject, jlong address, jlong byteCount) {
7627e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
7637e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    throwIfMinusOne(env, "munlock", TEMP_FAILURE_RETRY(munlock(ptr, byteCount)));
7647e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes}
7657e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes
7667e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_munmap(JNIEnv* env, jobject, jlong address, jlong byteCount) {
7677e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
7687e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "munmap", TEMP_FAILURE_RETRY(munmap(ptr, byteCount)));
7697e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
7707e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
7710ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughesstatic jobject Posix_open(JNIEnv* env, jobject, jstring javaPath, jint flags, jint mode) {
7720ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    ScopedUtfChars path(env, javaPath);
7730ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    if (path.c_str() == NULL) {
7740ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes        return NULL;
7750ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    }
7760ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    int fd = throwIfMinusOne(env, "open", TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode)));
7770ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL;
7780ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes}
7790ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes
78041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughesstatic jobjectArray Posix_pipe(JNIEnv* env, jobject) {
78141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    int fds[2];
78241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    throwIfMinusOne(env, "pipe", TEMP_FAILURE_RETRY(pipe(&fds[0])));
78341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    jobjectArray result = env->NewObjectArray(2, JniConstants::fileDescriptorClass, NULL);
78441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    if (result == NULL) {
78541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        return NULL;
78641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    }
78741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    for (int i = 0; i < 2; ++i) {
78841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        ScopedLocalRef<jobject> fd(env, jniCreateFileDescriptor(env, fds[i]));
78941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        if (fd.get() == NULL) {
79041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes            return NULL;
79141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        }
79241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        env->SetObjectArrayElement(result, i, fd.get());
79341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        if (env->ExceptionCheck()) {
79441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes            return NULL;
79541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        }
79641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    }
79741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    return result;
79841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes}
79941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes
80070c820401677ca251ad09ac64cc23c760764e75dElliott Hughesstatic jint Posix_poll(JNIEnv* env, jobject, jobjectArray javaStructs, jint timeoutMs) {
80170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    static jfieldID fdFid = env->GetFieldID(JniConstants::structPollfdClass, "fd", "Ljava/io/FileDescriptor;");
80270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    static jfieldID eventsFid = env->GetFieldID(JniConstants::structPollfdClass, "events", "S");
80370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    static jfieldID reventsFid = env->GetFieldID(JniConstants::structPollfdClass, "revents", "S");
80470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes
80570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    // Turn the Java libcore.io.StructPollfd[] into a C++ struct pollfd[].
80670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    size_t arrayLength = env->GetArrayLength(javaStructs);
80770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    UniquePtr<struct pollfd[]> fds(new struct pollfd[arrayLength]);
80870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    memset(fds.get(), 0, sizeof(struct pollfd) * arrayLength);
80970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    size_t count = 0; // Some trailing array elements may be irrelevant. (See below.)
81070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    for (size_t i = 0; i < arrayLength; ++i) {
81170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        ScopedLocalRef<jobject> javaStruct(env, env->GetObjectArrayElement(javaStructs, i));
81270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        if (javaStruct.get() == NULL) {
81370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes            break; // We allow trailing nulls in the array for caller convenience.
81470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        }
81570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        ScopedLocalRef<jobject> javaFd(env, env->GetObjectField(javaStruct.get(), fdFid));
81670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        if (javaFd.get() == NULL) {
81770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes            break; // We also allow callers to just clear the fd field (this is what Selector does).
81870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        }
81970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        fds[count].fd = jniGetFDFromFileDescriptor(env, javaFd.get());
82070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        fds[count].events = env->GetShortField(javaStruct.get(), eventsFid);
82170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        ++count;
82270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    }
82370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes
82470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    int rc = TEMP_FAILURE_RETRY(poll(fds.get(), count, timeoutMs));
82570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    if (rc == -1) {
82670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        throwErrnoException(env, "poll");
82770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        return -1;
82870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    }
82970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes
83070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    // Update the revents fields in the Java libcore.io.StructPollfd[].
83198b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes    for (size_t i = 0; i < count; ++i) {
83270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        ScopedLocalRef<jobject> javaStruct(env, env->GetObjectArrayElement(javaStructs, i));
83398b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes        if (javaStruct.get() == NULL) {
83498b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes            return -1;
83598b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes        }
83670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        env->SetShortField(javaStruct.get(), reventsFid, fds[i].revents);
83770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    }
83870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    return rc;
83970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes}
84070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes
8410568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughesstatic jint Posix_readBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount) {
8420568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes    ScopedBytesRW bytes(env, javaBytes);
84326c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    if (bytes.get() == NULL) {
84426c7025a7a919044771fb89031161bd26fe03032Elliott Hughes        return -1;
84526c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    }
84626c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
8470568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes    return throwIfMinusOne(env, "read", TEMP_FAILURE_RETRY(read(fd, bytes.get() + byteOffset, byteCount)));
84826c7025a7a919044771fb89031161bd26fe03032Elliott Hughes}
84926c7025a7a919044771fb89031161bd26fe03032Elliott Hughes
850bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_readv(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) {
8518dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes    IoVec<ScopedBytesRW> ioVec(env, env->GetArrayLength(buffers));
852bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    if (!ioVec.init(buffers, offsets, byteCounts)) {
853bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return -1;
854bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
855bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
856bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    return throwIfMinusOne(env, "readv", TEMP_FAILURE_RETRY(readv(fd, ioVec.get(), ioVec.size())));
857bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes}
858bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
859c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_remove(JNIEnv* env, jobject, jstring javaPath) {
860c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    ScopedUtfChars path(env, javaPath);
861c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    if (path.c_str() == NULL) {
862c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes        return;
863c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    }
864c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    throwIfMinusOne(env, "remove", TEMP_FAILURE_RETRY(remove(path.c_str())));
865c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes}
866c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes
867a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_rename(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) {
868a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars oldPath(env, javaOldPath);
869a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (oldPath.c_str() == NULL) {
870a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
871a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
872a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars newPath(env, javaNewPath);
873a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (newPath.c_str() == NULL) {
874a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
875a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
876a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    throwIfMinusOne(env, "rename", TEMP_FAILURE_RETRY(rename(oldPath.c_str(), newPath.c_str())));
877a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes}
878a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes
8798b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jlong Posix_sendfile(JNIEnv* env, jobject, jobject javaOutFd, jobject javaInFd, jobject javaOffset, jlong byteCount) {
8808b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    int outFd = jniGetFDFromFileDescriptor(env, javaOutFd);
8818b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    int inFd = jniGetFDFromFileDescriptor(env, javaInFd);
8828b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    static jfieldID valueFid = env->GetFieldID(JniConstants::mutableLongClass, "value", "J");
8838b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    off_t offset = 0;
8848b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    off_t* offsetPtr = NULL;
8858b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    if (javaOffset != NULL) {
8868b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        // TODO: fix bionic so we can have a 64-bit off_t!
8878b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        offset = env->GetLongField(javaOffset, valueFid);
8888b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        offsetPtr = &offset;
8898b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    }
8908b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    jlong result = throwIfMinusOne(env, "sendfile", TEMP_FAILURE_RETRY(sendfile(outFd, inFd, offsetPtr, byteCount)));
8918b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    if (javaOffset != NULL) {
8928b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        env->SetLongField(javaOffset, valueFid, offset);
8938b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    }
8948b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    return result;
8958b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes}
8968b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes
897396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_setegid(JNIEnv* env, jobject, jint egid) {
898396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    throwIfMinusOne(env, "setegid", TEMP_FAILURE_RETRY(setegid(egid)));
899396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
900396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
901396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_seteuid(JNIEnv* env, jobject, jint euid) {
902396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    throwIfMinusOne(env, "seteuid", TEMP_FAILURE_RETRY(seteuid(euid)));
903396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
904396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
905396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_setgid(JNIEnv* env, jobject, jint gid) {
906396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    throwIfMinusOne(env, "setgid", TEMP_FAILURE_RETRY(setgid(gid)));
907396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
908396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
909c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) {
910c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
911c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    u_char byte = value;
912c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &byte, sizeof(byte))));
913c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes}
914c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes
915a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic void Posix_setsockoptIfreq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jstring javaInterfaceName) {
916a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    struct ifreq req;
917a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    if (!fillIfreq(env, javaInterfaceName, req)) {
918b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes        return;
919b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    }
920b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
921b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req))));
922b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes}
923b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes
924454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic void Posix_setsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) {
925454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
926454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))));
927454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes}
928454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes
929b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughesstatic void Posix_setsockoptIpMreqn(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) {
930b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    ip_mreqn req;
931b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    memset(&req, 0, sizeof(req));
932b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    req.imr_ifindex = value;
933b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
934b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req))));
935b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes}
936b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes
937438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughesstatic void Posix_setsockoptGroupReq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaGroupReq) {
938438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    struct group_req value;
939438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes
940438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    static jfieldID grInterfaceFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_interface", "I");
941438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    value.gr_interface = env->GetIntField(javaGroupReq, grInterfaceFid);
942438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    // Get the IPv4 or IPv6 multicast address to join or leave.
943438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    static jfieldID grGroupFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_group", "Ljava/net/InetAddress;");
944e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes    ScopedLocalRef<jobject> javaGroup(env, env->GetObjectField(javaGroupReq, grGroupFid));
945e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes    if (!inetAddressToSockaddr(env, javaGroup.get(), 0, &value.gr_group)) {
946438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        return;
947438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    }
948438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes
949438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
950438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    int rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)));
951438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    if (rc == -1 && errno == EINVAL) {
952438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        // Maybe we're a 32-bit binary talking to a 64-bit kernel?
953438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        // glibc doesn't automatically handle this.
954438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        struct GCC_HIDDEN group_req64 {
955438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes            uint32_t gr_interface;
956438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes            uint32_t my_padding;
957438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes            sockaddr_storage gr_group;
958438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        };
959438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        group_req64 value64;
960438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        value64.gr_interface = value.gr_interface;
961438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        memcpy(&value64.gr_group, &value.gr_group, sizeof(value.gr_group));
962438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value64, sizeof(value64)));
963438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    }
964438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    throwIfMinusOne(env, "setsockopt", rc);
965438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes}
966438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes
967c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaLinger) {
968c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID lOnoffFid = env->GetFieldID(JniConstants::structLingerClass, "l_onoff", "I");
969c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID lLingerFid = env->GetFieldID(JniConstants::structLingerClass, "l_linger", "I");
970c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
971c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    struct linger value;
972c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    value.l_onoff = env->GetIntField(javaLinger, lOnoffFid);
973c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    value.l_linger = env->GetIntField(javaLinger, lLingerFid);
974c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))));
975c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes}
976c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes
977c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaTimeval) {
978c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID tvSecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_sec", "J");
979c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID tvUsecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_usec", "J");
980c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
981c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    struct timeval value;
982c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    value.tv_sec = env->GetIntField(javaTimeval, tvSecFid);
983c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    value.tv_usec = env->GetIntField(javaTimeval, tvUsecFid);
984c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))));
985c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes}
986c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes
987396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_setuid(JNIEnv* env, jobject, jint uid) {
988396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    throwIfMinusOne(env, "setuid", TEMP_FAILURE_RETRY(setuid(uid)));
989396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
990396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
99159e4744d27231f260271dbbca406e0cc39768116Elliott Hughesstatic void Posix_shutdown(JNIEnv* env, jobject, jobject javaFd, jint how) {
99259e4744d27231f260271dbbca406e0cc39768116Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
99359e4744d27231f260271dbbca406e0cc39768116Elliott Hughes    throwIfMinusOne(env, "shutdown", TEMP_FAILURE_RETRY(shutdown(fd, how)));
99459e4744d27231f260271dbbca406e0cc39768116Elliott Hughes}
99559e4744d27231f260271dbbca406e0cc39768116Elliott Hughes
996454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic jobject Posix_socket(JNIEnv* env, jobject, jint domain, jint type, jint protocol) {
997454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    int fd = throwIfMinusOne(env, "socket", TEMP_FAILURE_RETRY(socket(domain, type, protocol)));
998454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL;
999454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes}
1000454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes
100147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_stat(JNIEnv* env, jobject, jstring javaPath) {
100247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return doStat(env, javaPath, false);
100347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
100447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
10050ade5524d972b9dfd69b0126aa4c35319c1b7fa4Elliott Hughesstatic jobject Posix_statfs(JNIEnv* env, jobject, jstring javaPath) {
100659fa7163774d6930a174bc038414a4b780581957Elliott Hughes    ScopedUtfChars path(env, javaPath);
100759fa7163774d6930a174bc038414a4b780581957Elliott Hughes    if (path.c_str() == NULL) {
100859fa7163774d6930a174bc038414a4b780581957Elliott Hughes        return NULL;
100959fa7163774d6930a174bc038414a4b780581957Elliott Hughes    }
101059fa7163774d6930a174bc038414a4b780581957Elliott Hughes    struct statfs sb;
101159fa7163774d6930a174bc038414a4b780581957Elliott Hughes    int rc = TEMP_FAILURE_RETRY(statfs(path.c_str(), &sb));
101259fa7163774d6930a174bc038414a4b780581957Elliott Hughes    if (rc == -1) {
101359fa7163774d6930a174bc038414a4b780581957Elliott Hughes        throwErrnoException(env, "statfs");
101459fa7163774d6930a174bc038414a4b780581957Elliott Hughes        return NULL;
101559fa7163774d6930a174bc038414a4b780581957Elliott Hughes    }
101659fa7163774d6930a174bc038414a4b780581957Elliott Hughes    return makeStructStatFs(env, sb);
101759fa7163774d6930a174bc038414a4b780581957Elliott Hughes}
101859fa7163774d6930a174bc038414a4b780581957Elliott Hughes
1019ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jstring Posix_strerror(JNIEnv* env, jobject, jint errnum) {
102052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    char buffer[BUFSIZ];
1021ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    const char* message = jniStrError(errnum, buffer, sizeof(buffer));
1022ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return env->NewStringUTF(message);
1023ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
1024ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
1025a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_symlink(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) {
1026a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars oldPath(env, javaOldPath);
1027a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (oldPath.c_str() == NULL) {
1028a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
1029a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
1030a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars newPath(env, javaNewPath);
1031a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (newPath.c_str() == NULL) {
1032a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
1033a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
1034a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    throwIfMinusOne(env, "symlink", TEMP_FAILURE_RETRY(symlink(oldPath.c_str(), newPath.c_str())));
1035a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes}
1036a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes
10376fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughesstatic jlong Posix_sysconf(JNIEnv* env, jobject, jint name) {
10386fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    // Since -1 is a valid result from sysconf(3), detecting failure is a little more awkward.
10396fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    errno = 0;
10406fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    long result = sysconf(name);
10416fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    if (result == -1L && errno == EINVAL) {
10427e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "sysconf");
10436fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    }
10446fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    return result;
10456fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes}
10466fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes
10477341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject Posix_uname(JNIEnv* env, jobject) {
10487341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    struct utsname buf;
10497341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    if (throwIfMinusOne(env, "uname", TEMP_FAILURE_RETRY(uname(&buf))) == -1) {
10507341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes        return NULL;
10517341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    }
10527341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    return makeStructUtsname(env, buf);
10537341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes}
10547341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
10550568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughesstatic jint Posix_writeBytes(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount) {
10560568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes    ScopedBytesRO bytes(env, javaBytes);
105778c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    if (bytes.get() == NULL) {
105878c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes        return -1;
105978c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    }
106078c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
10610568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes    return throwIfMinusOne(env, "write", TEMP_FAILURE_RETRY(write(fd, bytes.get() + byteOffset, byteCount)));
106278c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes}
106378c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes
1064bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_writev(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) {
10658dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes    IoVec<ScopedBytesRO> ioVec(env, env->GetArrayLength(buffers));
1066bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    if (!ioVec.init(buffers, offsets, byteCounts)) {
1067bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return -1;
1068bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
1069bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
1070bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    return throwIfMinusOne(env, "writev", TEMP_FAILURE_RETRY(writev(fd, ioVec.get(), ioVec.size())));
1071bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes}
1072bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
1073ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic JNINativeMethod gMethods[] = {
1074ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, access, "(Ljava/lang/String;I)Z"),
1075da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes    NATIVE_METHOD(Posix, bind, "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)V"),
1076b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    NATIVE_METHOD(Posix, chmod, "(Ljava/lang/String;I)V"),
1077462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    NATIVE_METHOD(Posix, close, "(Ljava/io/FileDescriptor;)V"),
1078996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    NATIVE_METHOD(Posix, connect, "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)V"),
1079396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, dup, "(Ljava/io/FileDescriptor;)Ljava/io/FileDescriptor;"),
1080396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, dup2, "(Ljava/io/FileDescriptor;I)Ljava/io/FileDescriptor;"),
1081ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, environ, "()[Ljava/lang/String;"),
1082fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    NATIVE_METHOD(Posix, fcntlVoid, "(Ljava/io/FileDescriptor;I)I"),
1083fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    NATIVE_METHOD(Posix, fcntlLong, "(Ljava/io/FileDescriptor;IJ)I"),
1084fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    NATIVE_METHOD(Posix, fcntlFlock, "(Ljava/io/FileDescriptor;ILlibcore/io/StructFlock;)I"),
108552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    NATIVE_METHOD(Posix, fdatasync, "(Ljava/io/FileDescriptor;)V"),
108647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, fstat, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStat;"),
108759fa7163774d6930a174bc038414a4b780581957Elliott Hughes    NATIVE_METHOD(Posix, fstatfs, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStatFs;"),
108852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    NATIVE_METHOD(Posix, fsync, "(Ljava/io/FileDescriptor;)V"),
1089f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    NATIVE_METHOD(Posix, ftruncate, "(Ljava/io/FileDescriptor;J)V"),
10904f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    NATIVE_METHOD(Posix, gai_strerror, "(I)Ljava/lang/String;"),
10911c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    NATIVE_METHOD(Posix, getaddrinfo, "(Ljava/lang/String;Llibcore/io/StructAddrinfo;)[Ljava/net/InetAddress;"),
1092396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, getegid, "()I"),
1093396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, geteuid, "()I"),
1094396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, getgid, "()I"),
1095ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    NATIVE_METHOD(Posix, getenv, "(Ljava/lang/String;)Ljava/lang/String;"),
10964f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    NATIVE_METHOD(Posix, getnameinfo, "(Ljava/net/InetAddress;I)Ljava/lang/String;"),
1097396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, getpid, "()I"),
1098396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, getppid, "()I"),
10990a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockname, "(Ljava/io/FileDescriptor;)Ljava/net/SocketAddress;"),
11000a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptByte, "(Ljava/io/FileDescriptor;II)I"),
11010a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptInAddr, "(Ljava/io/FileDescriptor;II)Ljava/net/InetAddress;"),
11020a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptInt, "(Ljava/io/FileDescriptor;II)I"),
11030a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptLinger, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructLinger;"),
11040a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptTimeval, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructTimeval;"),
1105396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, getuid, "()I"),
1106a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    NATIVE_METHOD(Posix, if_indextoname, "(I)Ljava/lang/String;"),
11071c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    NATIVE_METHOD(Posix, inet_aton, "(Ljava/lang/String;)Ljava/net/InetAddress;"),
1108a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    NATIVE_METHOD(Posix, ioctlInetAddress, "(Ljava/io/FileDescriptor;ILjava/lang/String;)Ljava/net/InetAddress;"),
11098b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    NATIVE_METHOD(Posix, ioctlInt, "(Ljava/io/FileDescriptor;ILlibcore/util/MutableInt;)I"),
11109a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    NATIVE_METHOD(Posix, isatty, "(Ljava/io/FileDescriptor;)Z"),
1111a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes    NATIVE_METHOD(Posix, kill, "(II)V"),
1112e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes    NATIVE_METHOD(Posix, listen, "(Ljava/io/FileDescriptor;I)V"),
1113dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    NATIVE_METHOD(Posix, lseek, "(Ljava/io/FileDescriptor;JI)J"),
111447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, lstat, "(Ljava/lang/String;)Llibcore/io/StructStat;"),
11150f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    NATIVE_METHOD(Posix, mincore, "(JJ[B)V"),
1116c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    NATIVE_METHOD(Posix, mkdir, "(Ljava/lang/String;I)V"),
11177e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    NATIVE_METHOD(Posix, mlock, "(JJ)V"),
11187e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, mmap, "(JJIILjava/io/FileDescriptor;J)J"),
11197e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, msync, "(JJI)V"),
11207e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    NATIVE_METHOD(Posix, munlock, "(JJ)V"),
11217e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, munmap, "(JJ)V"),
11220ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    NATIVE_METHOD(Posix, open, "(Ljava/lang/String;II)Ljava/io/FileDescriptor;"),
112341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    NATIVE_METHOD(Posix, pipe, "()[Ljava/io/FileDescriptor;"),
112470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    NATIVE_METHOD(Posix, poll, "([Llibcore/io/StructPollfd;I)I"),
11250568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes    NATIVE_METHOD(Posix, readBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"),
1126bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    NATIVE_METHOD(Posix, readv, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"),
1127c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    NATIVE_METHOD(Posix, remove, "(Ljava/lang/String;)V"),
1128a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    NATIVE_METHOD(Posix, rename, "(Ljava/lang/String;Ljava/lang/String;)V"),
11298b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    NATIVE_METHOD(Posix, sendfile, "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Llibcore/util/MutableLong;J)J"),
1130396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, setegid, "(I)V"),
1131396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, seteuid, "(I)V"),
1132396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, setgid, "(I)V"),
1133c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    NATIVE_METHOD(Posix, setsockoptByte, "(Ljava/io/FileDescriptor;III)V"),
1134b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    NATIVE_METHOD(Posix, setsockoptIfreq, "(Ljava/io/FileDescriptor;IILjava/lang/String;)V"),
1135454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    NATIVE_METHOD(Posix, setsockoptInt, "(Ljava/io/FileDescriptor;III)V"),
1136b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    NATIVE_METHOD(Posix, setsockoptIpMreqn, "(Ljava/io/FileDescriptor;III)V"),
1137438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    NATIVE_METHOD(Posix, setsockoptGroupReq, "(Ljava/io/FileDescriptor;IILlibcore/io/StructGroupReq;)V"),
1138c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    NATIVE_METHOD(Posix, setsockoptLinger, "(Ljava/io/FileDescriptor;IILlibcore/io/StructLinger;)V"),
1139c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    NATIVE_METHOD(Posix, setsockoptTimeval, "(Ljava/io/FileDescriptor;IILlibcore/io/StructTimeval;)V"),
1140396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, setuid, "(I)V"),
114159e4744d27231f260271dbbca406e0cc39768116Elliott Hughes    NATIVE_METHOD(Posix, shutdown, "(Ljava/io/FileDescriptor;I)V"),
1142454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    NATIVE_METHOD(Posix, socket, "(III)Ljava/io/FileDescriptor;"),
114347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, stat, "(Ljava/lang/String;)Llibcore/io/StructStat;"),
114459fa7163774d6930a174bc038414a4b780581957Elliott Hughes    NATIVE_METHOD(Posix, statfs, "(Ljava/lang/String;)Llibcore/io/StructStatFs;"),
1145ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, strerror, "(I)Ljava/lang/String;"),
1146a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    NATIVE_METHOD(Posix, symlink, "(Ljava/lang/String;Ljava/lang/String;)V"),
11476fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    NATIVE_METHOD(Posix, sysconf, "(I)J"),
11487341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    NATIVE_METHOD(Posix, uname, "()Llibcore/io/StructUtsname;"),
11490568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes    NATIVE_METHOD(Posix, writeBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"),
1150bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    NATIVE_METHOD(Posix, writev, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"),
1151ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes};
1152ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesint register_libcore_io_Posix(JNIEnv* env) {
1153ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    return jniRegisterNativeMethods(env, "libcore/io/Posix", gMethods, NELEM(gMethods));
1154ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes}
1155