libcore_io_Linux.cpp revision bb5816aa1626eb2f6263bd21479600b114c8a1bd
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>
40d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes#include <pwd.h>
41a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes#include <signal.h>
42ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include <stdlib.h>
43461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes#include <sys/ioctl.h>
447e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes#include <sys/mman.h>
458b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes#include <sys/sendfile.h>
4659e4744d27231f260271dbbca406e0cc39768116Elliott Hughes#include <sys/socket.h>
471c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes#include <sys/socket.h>
4847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes#include <sys/stat.h>
490a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include <sys/time.h>
500ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <sys/types.h>
51bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes#include <sys/uio.h>
527341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#include <sys/utsname.h>
5359fa7163774d6930a174bc038414a4b780581957Elliott Hughes#include <sys/vfs.h> // Bionic doesn't have <sys/statvfs.h>
549e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes#include <sys/wait.h>
55bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig#include <termios.h>
560ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <unistd.h>
57ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
58d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes#define TO_JAVA_STRING(NAME, EXP) \
59d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes        jstring NAME = env->NewStringUTF(EXP); \
60d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes        if (NAME == NULL) return NULL;
61d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes
621c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughesstruct addrinfo_deleter {
631c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    void operator()(addrinfo* p) const {
641c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        if (p != NULL) { // bionic's freeaddrinfo(3) crashes when passed NULL.
651c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            freeaddrinfo(p);
661c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        }
671c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
681c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes};
691c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
70996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes/**
711124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes * Used to retry networking system calls that can return EINTR. Unlike TEMP_FAILURE_RETRY,
721124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes * this also handles the case where the reason for failure is that another thread called
731124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes * Socket.close. This macro also throws exceptions on failure.
74996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes *
75996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * Returns the result of 'exp', though a Java exception will be pending if the result is -1.
76996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes */
771124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes#define NET_FAILURE_RETRY(jni_env, return_type, syscall_name, java_fd, ...) ({ \
781124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes    return_type _rc = -1; \
79996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    do { \
80996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes        { \
811124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes            int _fd = jniGetFDFromFileDescriptor(jni_env, java_fd); \
821124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes            AsynchronousSocketCloseMonitor _monitor(_fd); \
831124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes            _rc = syscall_name(_fd, __VA_ARGS__); \
84996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes        } \
85996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes        if (_rc == -1) { \
861124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes            if (jniGetFDFromFileDescriptor(jni_env, java_fd) == -1) { \
871124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes                jniThrowException(jni_env, "java/net/SocketException", "Socket closed"); \
88996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes                break; \
89996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes            } else if (errno != EINTR) { \
901124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes                /* TODO: with a format string we could show the arguments too, like strace(1). */ \
911124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes                throwErrnoException(jni_env, # syscall_name); \
92996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes                break; \
93996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes            } \
94996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes        } \
95996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    } while (_rc == -1); \
96996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    _rc; })
97996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes
984f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwException(JNIEnv* env, jclass exceptionClass, jmethodID ctor3, jmethodID ctor2,
994f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        const char* functionName, int error) {
100ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    jthrowable cause = NULL;
101ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (env->ExceptionCheck()) {
102ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        cause = env->ExceptionOccurred();
103ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        env->ExceptionClear();
104ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
105ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
1064f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName));
1074f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    if (detailMessage.get() == NULL) {
108f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        // Not really much we can do here. We're probably dead in the water,
109f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        // but let's try to stumble on...
110f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        env->ExceptionClear();
111f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    }
112f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes
113ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    jobject exception;
114ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (cause != NULL) {
1154f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        exception = env->NewObject(exceptionClass, ctor3, detailMessage.get(), error, cause);
116ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    } else {
1174f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        exception = env->NewObject(exceptionClass, ctor2, detailMessage.get(), error);
118ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
119ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    env->Throw(reinterpret_cast<jthrowable>(exception));
120dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes}
121dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes
1224f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwErrnoException(JNIEnv* env, const char* functionName) {
1234f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    int error = errno;
1244f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    static jmethodID ctor3 = env->GetMethodID(JniConstants::errnoExceptionClass,
1254f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V");
1264f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    static jmethodID ctor2 = env->GetMethodID(JniConstants::errnoExceptionClass,
1274f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            "<init>", "(Ljava/lang/String;I)V");
1284f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    throwException(env, JniConstants::errnoExceptionClass, ctor3, ctor2, functionName, error);
1294f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes}
1304f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes
1314f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwGaiException(JNIEnv* env, const char* functionName, int error) {
13275cf14944d476670f6f915e5efd849e238a16250Elliott Hughes    if (errno != 0) {
13375cf14944d476670f6f915e5efd849e238a16250Elliott Hughes        // EAI_SYSTEM should mean "look at errno instead", but both glibc and bionic seem to
13475cf14944d476670f6f915e5efd849e238a16250Elliott Hughes        // mess this up. In particular, if you don't have INTERNET permission, errno will be EACCES
13575cf14944d476670f6f915e5efd849e238a16250Elliott Hughes        // but you'll get EAI_NONAME or EAI_NODATA. So we want our GaiException to have a
13675cf14944d476670f6f915e5efd849e238a16250Elliott Hughes        // potentially-relevant ErrnoException as its cause even if error != EAI_SYSTEM.
13775cf14944d476670f6f915e5efd849e238a16250Elliott Hughes        // http://code.google.com/p/android/issues/detail?id=15722
1384f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        throwErrnoException(env, functionName);
1394f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        // Deliberately fall through to throw another exception...
1404f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    }
1414f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    static jmethodID ctor3 = env->GetMethodID(JniConstants::gaiExceptionClass,
1424f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V");
1434f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    static jmethodID ctor2 = env->GetMethodID(JniConstants::gaiExceptionClass,
1444f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            "<init>", "(Ljava/lang/String;I)V");
1454f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    throwException(env, JniConstants::gaiExceptionClass, ctor3, ctor2, functionName, error);
1464f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes}
1474f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes
148dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughestemplate <typename rc_t>
1497e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic rc_t throwIfMinusOne(JNIEnv* env, const char* name, rc_t rc) {
150dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == rc_t(-1)) {
1517e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, name);
152dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    }
153dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    return rc;
15447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
15547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
1568dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughestemplate <typename ScopedT>
157bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesclass IoVec {
158bbac92e691de7d570928ddfba639067978e55b06Elliott Hughespublic:
159bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    IoVec(JNIEnv* env, size_t bufferCount) : mEnv(env), mBufferCount(bufferCount) {
160bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
161bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
162bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    bool init(jobjectArray javaBuffers, jintArray javaOffsets, jintArray javaByteCounts) {
163bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        // We can't delete our local references until after the I/O, so make sure we have room.
164bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (mEnv->PushLocalFrame(mBufferCount + 16) < 0) {
165bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            return false;
166bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
167bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        ScopedIntArrayRO offsets(mEnv, javaOffsets);
168bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (offsets.get() == NULL) {
169bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            return false;
170bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
171bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        ScopedIntArrayRO byteCounts(mEnv, javaByteCounts);
172bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (byteCounts.get() == NULL) {
173bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            return false;
174bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
175bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        // TODO: Linux actually has a 1024 buffer limit. glibc works around this, and we should too.
176d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes        // TODO: you can query the limit at runtime with sysconf(_SC_IOV_MAX).
177bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        for (size_t i = 0; i < mBufferCount; ++i) {
178e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes            jobject buffer = mEnv->GetObjectArrayElement(javaBuffers, i); // We keep this local ref.
1798dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes            mScopedBuffers.push_back(new ScopedT(mEnv, buffer));
1808dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes            jbyte* ptr = const_cast<jbyte*>(mScopedBuffers.back()->get());
181bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            if (ptr == NULL) {
182bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                return false;
183bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            }
184bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            struct iovec iov;
185bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            iov.iov_base = reinterpret_cast<void*>(ptr + offsets[i]);
186bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            iov.iov_len = byteCounts[i];
187bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            mIoVec.push_back(iov);
188bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
189bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return true;
190bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
191bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
192bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    ~IoVec() {
1938dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes        for (size_t i = 0; i < mScopedBuffers.size(); ++i) {
1948dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes            delete mScopedBuffers[i];
195bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
196bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        mEnv->PopLocalFrame(NULL);
197bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
198bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
199bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    iovec* get() {
200bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return &mIoVec[0];
201bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
202bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
203bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    size_t size() {
204bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return mBufferCount;
205bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
206bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
207bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesprivate:
208bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    JNIEnv* mEnv;
209bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    size_t mBufferCount;
210bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    std::vector<iovec> mIoVec;
2118dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes    std::vector<ScopedT*> mScopedBuffers;
212bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes};
213bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
21423ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughesstatic jobject makeSocketAddress(JNIEnv* env, const sockaddr_storage* ss) {
21523ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes    // TODO: support AF_UNIX and AF_UNSPEC (and other families?)
21623ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes    jint port;
21723ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes    jobject inetAddress = sockaddrToInetAddress(env, ss, &port);
2180a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (inetAddress == NULL) {
2190a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
2200a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
2210a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::inetSocketAddressClass, "<init>",
2220a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes            "(Ljava/net/InetAddress;I)V");
2230a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return env->NewObject(JniConstants::inetSocketAddressClass, ctor, inetAddress, port);
2240a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
2250a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
226d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesstatic jobject makeStructPasswd(JNIEnv* env, const struct passwd& pw) {
227d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    TO_JAVA_STRING(pw_name, pw.pw_name);
228d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    TO_JAVA_STRING(pw_dir, pw.pw_dir);
229d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    TO_JAVA_STRING(pw_shell, pw.pw_shell);
230d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structPasswdClass, "<init>",
231d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes            "(Ljava/lang/String;IILjava/lang/String;Ljava/lang/String;)V");
232d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    return env->NewObject(JniConstants::structPasswdClass, ctor,
233d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes            pw_name, static_cast<jint>(pw.pw_uid), static_cast<jint>(pw.pw_gid), pw_dir, pw_shell);
234d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes}
235d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes
23647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject makeStructStat(JNIEnv* env, const struct stat& sb) {
23747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structStatClass, "<init>",
23847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes            "(JJIJIIJJJJJJJ)V");
23947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return env->NewObject(JniConstants::structStatClass, ctor,
24059fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_dev), static_cast<jlong>(sb.st_ino),
24159fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jint>(sb.st_mode), static_cast<jlong>(sb.st_nlink),
24259fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jint>(sb.st_uid), static_cast<jint>(sb.st_gid),
24359fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_rdev), static_cast<jlong>(sb.st_size),
24459fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_atime), static_cast<jlong>(sb.st_mtime),
24559fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_ctime), static_cast<jlong>(sb.st_blksize),
24659fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_blocks));
24759fa7163774d6930a174bc038414a4b780581957Elliott Hughes}
24859fa7163774d6930a174bc038414a4b780581957Elliott Hughes
24959fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject makeStructStatFs(JNIEnv* env, const struct statfs& sb) {
25059fa7163774d6930a174bc038414a4b780581957Elliott Hughes    STATIC_ASSERT(sizeof(sb.f_bavail) == sizeof(jlong), statfs_not_64_bit);
25159fa7163774d6930a174bc038414a4b780581957Elliott Hughes    STATIC_ASSERT(sizeof(sb.f_bfree) == sizeof(jlong), statfs_not_64_bit);
25259fa7163774d6930a174bc038414a4b780581957Elliott Hughes    STATIC_ASSERT(sizeof(sb.f_blocks) == sizeof(jlong), statfs_not_64_bit);
25359fa7163774d6930a174bc038414a4b780581957Elliott Hughes
25459fa7163774d6930a174bc038414a4b780581957Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structStatFsClass, "<init>",
25559fa7163774d6930a174bc038414a4b780581957Elliott Hughes            "(JJJJJJJJ)V");
25659fa7163774d6930a174bc038414a4b780581957Elliott Hughes    return env->NewObject(JniConstants::structStatFsClass, ctor, static_cast<jlong>(sb.f_bsize),
25759fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_blocks), static_cast<jlong>(sb.f_bfree),
25859fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_bavail), static_cast<jlong>(sb.f_files),
25959fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_ffree), static_cast<jlong>(sb.f_namelen),
26059fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_frsize));
26147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
26247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
2630a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructLinger(JNIEnv* env, const struct linger& l) {
2640a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structLingerClass, "<init>", "(II)V");
2650a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return env->NewObject(JniConstants::structLingerClass, ctor, l.l_onoff, l.l_linger);
2660a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
2670a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
2680a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructTimeval(JNIEnv* env, const struct timeval& tv) {
2690a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structTimevalClass, "<init>", "(JJ)V");
2700a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return env->NewObject(JniConstants::structTimevalClass, ctor,
2710a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes            static_cast<jlong>(tv.tv_sec), static_cast<jlong>(tv.tv_usec));
2720a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
2730a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
2747341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject makeStructUtsname(JNIEnv* env, const struct utsname& buf) {
275d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    TO_JAVA_STRING(sysname, buf.sysname);
276d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    TO_JAVA_STRING(nodename, buf.nodename);
277d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    TO_JAVA_STRING(release, buf.release);
278d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    TO_JAVA_STRING(version, buf.version);
279d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    TO_JAVA_STRING(machine, buf.machine);
2807341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structUtsnameClass, "<init>",
2817341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes            "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
2827341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    return env->NewObject(JniConstants::structUtsnameClass, ctor,
2837341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes            sysname, nodename, release, version, machine);
2847341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes};
2857341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
286a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic bool fillIfreq(JNIEnv* env, jstring javaInterfaceName, struct ifreq& req) {
287a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    ScopedUtfChars interfaceName(env, javaInterfaceName);
288a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    if (interfaceName.c_str() == NULL) {
289a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes        return false;
290a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    }
291a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    memset(&req, 0, sizeof(req));
292a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    strncpy(req.ifr_name, interfaceName.c_str(), sizeof(req.ifr_name));
293a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    req.ifr_name[sizeof(req.ifr_name) - 1] = '\0';
294a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    return true;
295a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes}
296a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes
297553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughesstatic bool fillInetSocketAddress(JNIEnv* env, jint rc, jobject javaInetSocketAddress, const sockaddr_storage* ss) {
298553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    if (rc == -1 || javaInetSocketAddress == NULL) {
299553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes        return true;
300553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    }
301553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    // Fill out the passed-in InetSocketAddress with the sender's IP address and port number.
302553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    jint port;
303553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    jobject sender = sockaddrToInetAddress(env, ss, &port);
304553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    if (sender == NULL) {
305553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes        return false;
306553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    }
307553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    static jfieldID addressFid = env->GetFieldID(JniConstants::inetSocketAddressClass, "addr", "Ljava/net/InetAddress;");
308553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    static jfieldID portFid = env->GetFieldID(JniConstants::inetSocketAddressClass, "port", "I");
309553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    env->SetObjectField(javaInetSocketAddress, addressFid, sender);
310553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    env->SetIntField(javaInetSocketAddress, portFid, port);
311553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    return true;
312553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes}
313553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes
31447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject doStat(JNIEnv* env, jstring javaPath, bool isLstat) {
31547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    ScopedUtfChars path(env, javaPath);
31647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    if (path.c_str() == NULL) {
31747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
31847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
31947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    struct stat sb;
32047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = isLstat ? TEMP_FAILURE_RETRY(lstat(path.c_str(), &sb))
32147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes                     : TEMP_FAILURE_RETRY(stat(path.c_str(), &sb));
322dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
3237e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, isLstat ? "lstat" : "stat");
32447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
32547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
32647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return makeStructStat(env, sb);
327ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
328ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
329d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesclass Passwd {
330d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughespublic:
331d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    Passwd(JNIEnv* env) : mEnv(env), mResult(NULL) {
332d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes        mBufferSize = sysconf(_SC_GETPW_R_SIZE_MAX);
333d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes        if (mBufferSize == -1UL) {
334d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes            // We're probably on bionic, where 1KiB should be enough for anyone.
335d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes            // TODO: fix bionic to return 1024 like glibc.
336d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes            mBufferSize = 1024;
337d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes        }
338d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes        mBuffer.reset(new char[mBufferSize]);
339d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    }
340d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes
341d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    jobject getpwnam(const char* name) {
342d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes        return process("getpwnam_r", getpwnam_r(name, &mPwd, mBuffer.get(), mBufferSize, &mResult));
343d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    }
344d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes
345d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    jobject getpwuid(uid_t uid) {
346d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes        return process("getpwuid_r", getpwuid_r(uid, &mPwd, mBuffer.get(), mBufferSize, &mResult));
347d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    }
348d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes
349d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    struct passwd* get() {
350d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes        return mResult;
351d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    }
352d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes
353d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesprivate:
354d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    jobject process(const char* syscall, int error) {
355d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes        if (mResult == NULL) {
356d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes            errno = error;
357d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes            throwErrnoException(mEnv, syscall);
358d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes            return NULL;
359d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes        }
360d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes        return makeStructPasswd(mEnv, *mResult);
361d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    }
362d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes
363d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    JNIEnv* mEnv;
364d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    UniquePtr<char[]> mBuffer;
365d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    size_t mBufferSize;
366d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    struct passwd mPwd;
367d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    struct passwd* mResult;
368d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes};
369d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes
370553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughesstatic jobject Posix_accept(JNIEnv* env, jobject, jobject javaFd, jobject javaInetSocketAddress) {
371553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    sockaddr_storage ss;
372553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    socklen_t sl = sizeof(ss);
373553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    memset(&ss, 0, sizeof(ss));
374553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    sockaddr* peer = (javaInetSocketAddress != NULL) ? reinterpret_cast<sockaddr*>(&ss) : NULL;
375553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    socklen_t* peerLength = (javaInetSocketAddress != NULL) ? &sl : 0;
3761124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes    jint clientFd = NET_FAILURE_RETRY(env, int, accept, javaFd, peer, peerLength);
377553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    if (clientFd == -1 || !fillInetSocketAddress(env, clientFd, javaInetSocketAddress, &ss)) {
378553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes        close(clientFd);
379553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes        return NULL;
380553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    }
381553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    return (clientFd != -1) ? jniCreateFileDescriptor(env, clientFd) : NULL;
382553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes}
383553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes
384ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jboolean Posix_access(JNIEnv* env, jobject, jstring javaPath, jint mode) {
385ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    ScopedUtfChars path(env, javaPath);
386ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (path.c_str() == NULL) {
387ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        return JNI_FALSE;
388ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
38947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = TEMP_FAILURE_RETRY(access(path.c_str(), mode));
390dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
3917e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "access");
392dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    }
393ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return (rc == 0);
394ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
395ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
396da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughesstatic void Posix_bind(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) {
397da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes    sockaddr_storage ss;
398da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes    if (!inetAddressToSockaddr(env, javaAddress, port, &ss)) {
399da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes        return;
400da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes    }
401da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes    const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss);
4021124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes    // We don't need the return value because we'll already have thrown.
4031124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes    (void) NET_FAILURE_RETRY(env, int, bind, javaFd, sa, sizeof(sockaddr_storage));
404da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes}
405da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes
406b7190190e0ef8de883c952efb319ce7748831faaElliott Hughesstatic void Posix_chmod(JNIEnv* env, jobject, jstring javaPath, jint mode) {
407b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    ScopedUtfChars path(env, javaPath);
408b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    if (path.c_str() == NULL) {
409b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes        return;
410b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    }
411b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    throwIfMinusOne(env, "chmod", TEMP_FAILURE_RETRY(chmod(path.c_str(), mode)));
412b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes}
413b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes
41444f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughesstatic void Posix_chown(JNIEnv* env, jobject, jstring javaPath, jint uid, jint gid) {
41544f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes    ScopedUtfChars path(env, javaPath);
41644f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes    if (path.c_str() == NULL) {
41744f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes        return;
41844f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes    }
41944f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes    throwIfMinusOne(env, "chown", TEMP_FAILURE_RETRY(chown(path.c_str(), uid, gid)));
42044f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes}
42144f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes
422462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughesstatic void Posix_close(JNIEnv* env, jobject, jobject javaFd) {
423462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // Get the FileDescriptor's 'fd' field and clear it.
424462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // We need to do this before we can throw an IOException (http://b/3222087).
425462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
426462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    jniSetFileDescriptorOfFD(env, javaFd, -1);
427462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes
428462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // Even if close(2) fails with EINTR, the fd will have been closed.
429462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone else's fd.
430462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
431462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    throwIfMinusOne(env, "close", close(fd));
432462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes}
433462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes
434996bf79565ac88402920bd826d6f85952c83be20Elliott Hughesstatic void Posix_connect(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) {
435996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    sockaddr_storage ss;
436996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    if (!inetAddressToSockaddr(env, javaAddress, port, &ss)) {
437996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes        return;
438996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    }
439996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss);
4401124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes    // We don't need the return value because we'll already have thrown.
4411124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes    (void) NET_FAILURE_RETRY(env, int, connect, javaFd, sa, sizeof(sockaddr_storage));
442996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes}
443996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes
444396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jobject Posix_dup(JNIEnv* env, jobject, jobject javaOldFd) {
445396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    int oldFd = jniGetFDFromFileDescriptor(env, javaOldFd);
446396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    int newFd = throwIfMinusOne(env, "dup", TEMP_FAILURE_RETRY(dup(oldFd)));
447396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    return (newFd != -1) ? jniCreateFileDescriptor(env, newFd) : NULL;
448396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
449396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
450396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jobject Posix_dup2(JNIEnv* env, jobject, jobject javaOldFd, jint newFd) {
451396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    int oldFd = jniGetFDFromFileDescriptor(env, javaOldFd);
452396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    int fd = throwIfMinusOne(env, "dup2", TEMP_FAILURE_RETRY(dup2(oldFd, newFd)));
453396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    return (fd != -1) ? jniCreateFileDescriptor(env, fd) : NULL;
454396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
455396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
456ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jobjectArray Posix_environ(JNIEnv* env, jobject) {
457ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    extern char** environ; // Standard, but not in any header file.
458ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return toStringArray(env, environ);
459ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
460ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
46144f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughesstatic void Posix_fchmod(JNIEnv* env, jobject, jobject javaFd, jint mode) {
46244f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
46344f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes    throwIfMinusOne(env, "fchmod", TEMP_FAILURE_RETRY(fchmod(fd, mode)));
46444f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes}
46544f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes
46644f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughesstatic void Posix_fchown(JNIEnv* env, jobject, jobject javaFd, jint uid, jint gid) {
46744f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
46844f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes    throwIfMinusOne(env, "fchown", TEMP_FAILURE_RETRY(fchown(fd, uid, gid)));
46944f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes}
47044f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes
471fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlVoid(JNIEnv* env, jobject, jobject javaFd, jint cmd) {
472fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
473fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd)));
474fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes}
475fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
476fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlLong(JNIEnv* env, jobject, jobject javaFd, jint cmd, jlong arg) {
477fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
478fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, arg)));
479fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes}
480fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
481fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlFlock(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaFlock) {
482fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID typeFid = env->GetFieldID(JniConstants::structFlockClass, "l_type", "S");
483fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID whenceFid = env->GetFieldID(JniConstants::structFlockClass, "l_whence", "S");
484fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID startFid = env->GetFieldID(JniConstants::structFlockClass, "l_start", "J");
485fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID lenFid = env->GetFieldID(JniConstants::structFlockClass, "l_len", "J");
486fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID pidFid = env->GetFieldID(JniConstants::structFlockClass, "l_pid", "I");
487fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
488fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    struct flock64 lock;
489fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    memset(&lock, 0, sizeof(lock));
490fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_type = env->GetShortField(javaFlock, typeFid);
491fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_whence = env->GetShortField(javaFlock, whenceFid);
492fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_start = env->GetLongField(javaFlock, startFid);
493fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_len = env->GetLongField(javaFlock, lenFid);
494fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_pid = env->GetIntField(javaFlock, pidFid);
495fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
496fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
497fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int rc = throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, &lock)));
498fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    if (rc != -1) {
499fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetShortField(javaFlock, typeFid, lock.l_type);
500fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetShortField(javaFlock, whenceFid, lock.l_whence);
501fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetLongField(javaFlock, startFid, lock.l_start);
502fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetLongField(javaFlock, lenFid, lock.l_len);
503fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetIntField(javaFlock, pidFid, lock.l_pid);
504fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    }
505fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    return rc;
506fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes}
507fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
50852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fdatasync(JNIEnv* env, jobject, jobject javaFd) {
50952724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
5107e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "fdatasync", TEMP_FAILURE_RETRY(fdatasync(fd)));
51152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes}
51252724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes
51347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_fstat(JNIEnv* env, jobject, jobject javaFd) {
51447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
51547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    struct stat sb;
51647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = TEMP_FAILURE_RETRY(fstat(fd, &sb));
517dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
5187e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "fstat");
51947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
52047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
52147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return makeStructStat(env, sb);
52247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
52347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
52459fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject Posix_fstatfs(JNIEnv* env, jobject, jobject javaFd) {
52559fa7163774d6930a174bc038414a4b780581957Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
52659fa7163774d6930a174bc038414a4b780581957Elliott Hughes    struct statfs sb;
52759fa7163774d6930a174bc038414a4b780581957Elliott Hughes    int rc = TEMP_FAILURE_RETRY(fstatfs(fd, &sb));
52859fa7163774d6930a174bc038414a4b780581957Elliott Hughes    if (rc == -1) {
52959fa7163774d6930a174bc038414a4b780581957Elliott Hughes        throwErrnoException(env, "fstatfs");
53059fa7163774d6930a174bc038414a4b780581957Elliott Hughes        return NULL;
53159fa7163774d6930a174bc038414a4b780581957Elliott Hughes    }
53259fa7163774d6930a174bc038414a4b780581957Elliott Hughes    return makeStructStatFs(env, sb);
53359fa7163774d6930a174bc038414a4b780581957Elliott Hughes}
53459fa7163774d6930a174bc038414a4b780581957Elliott Hughes
53552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fsync(JNIEnv* env, jobject, jobject javaFd) {
53652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
5377e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "fsync", TEMP_FAILURE_RETRY(fsync(fd)));
538f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes}
539f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes
540f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughesstatic void Posix_ftruncate(JNIEnv* env, jobject, jobject javaFd, jlong length) {
541f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
5427e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "ftruncate", TEMP_FAILURE_RETRY(ftruncate64(fd, length)));
54352724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes}
54452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes
5454f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic jstring Posix_gai_strerror(JNIEnv* env, jobject, jint error) {
5464f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    return env->NewStringUTF(gai_strerror(error));
5474f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes}
5484f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes
5491c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughesstatic jobjectArray Posix_getaddrinfo(JNIEnv* env, jobject, jstring javaNode, jobject javaHints) {
5501c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    ScopedUtfChars node(env, javaNode);
5511c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    if (node.c_str() == NULL) {
5521c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return NULL;
5531c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
5541c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
5551c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    static jfieldID flagsFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_flags", "I");
5561c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    static jfieldID familyFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_family", "I");
5571c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    static jfieldID socktypeFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_socktype", "I");
5581c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    static jfieldID protocolFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_protocol", "I");
5591c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
5601c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    addrinfo hints;
5611c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    memset(&hints, 0, sizeof(hints));
5621c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    hints.ai_flags = env->GetIntField(javaHints, flagsFid);
5631c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    hints.ai_family = env->GetIntField(javaHints, familyFid);
5641c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    hints.ai_socktype = env->GetIntField(javaHints, socktypeFid);
5651c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    hints.ai_protocol = env->GetIntField(javaHints, protocolFid);
5661c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
5671c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    addrinfo* addressList = NULL;
56875cf14944d476670f6f915e5efd849e238a16250Elliott Hughes    errno = 0;
5691c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    int rc = getaddrinfo(node.c_str(), NULL, &hints, &addressList);
5701c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    UniquePtr<addrinfo, addrinfo_deleter> addressListDeleter(addressList);
5711c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    if (rc != 0) {
5721c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        throwGaiException(env, "getaddrinfo", rc);
5731c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return NULL;
5741c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
5751c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
5761c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    // Count results so we know how to size the output array.
5771c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    int addressCount = 0;
5781c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    for (addrinfo* ai = addressList; ai != NULL; ai = ai->ai_next) {
5791c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) {
5801c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            ++addressCount;
5811c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        } else {
582679cf68b607e9b4a3beb8bcdee06868ae583386fSteve Block            ALOGE("getaddrinfo unexpected ai_family %i", ai->ai_family);
5831c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        }
5841c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
5851c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    if (addressCount == 0) {
5861c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return NULL;
5871c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
5881c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
5891c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    // Prepare output array.
5901c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    jobjectArray result = env->NewObjectArray(addressCount, JniConstants::inetAddressClass, NULL);
5911c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    if (result == NULL) {
5921c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return NULL;
5931c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
5941c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
5951c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    // Examine returned addresses one by one, save them in the output array.
5961c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    int index = 0;
5971c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    for (addrinfo* ai = addressList; ai != NULL; ai = ai->ai_next) {
5981c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) {
5991c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            // Unknown address family. Skip this address.
600679cf68b607e9b4a3beb8bcdee06868ae583386fSteve Block            ALOGE("getaddrinfo unexpected ai_family %i", ai->ai_family);
6011c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            continue;
6021c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        }
6031c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
6041c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        // Convert each IP address into a Java byte array.
6051c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        sockaddr_storage* address = reinterpret_cast<sockaddr_storage*>(ai->ai_addr);
60623ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes        ScopedLocalRef<jobject> inetAddress(env, sockaddrToInetAddress(env, address, NULL));
6071c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        if (inetAddress.get() == NULL) {
6081c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            return NULL;
6091c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        }
6101c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        env->SetObjectArrayElement(result, index, inetAddress.get());
6111c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        ++index;
6121c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
6131c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    return result;
6141c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes}
6151c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
616396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getegid(JNIEnv*, jobject) {
617396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    return getegid();
618396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
619396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
620396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_geteuid(JNIEnv*, jobject) {
621396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    return geteuid();
622396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
623396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
624396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getgid(JNIEnv*, jobject) {
625396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    return getgid();
626396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
627396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
628ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic jstring Posix_getenv(JNIEnv* env, jobject, jstring javaName) {
629ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    ScopedUtfChars name(env, javaName);
630ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    if (name.c_str() == NULL) {
631ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes        return NULL;
632ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    }
633ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    return env->NewStringUTF(getenv(name.c_str()));
634ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes}
635ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
6364f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic jstring Posix_getnameinfo(JNIEnv* env, jobject, jobject javaAddress, jint flags) {
6374f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    sockaddr_storage ss;
6388ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes    if (!inetAddressToSockaddrVerbatim(env, javaAddress, 0, &ss)) {
6394f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        return NULL;
6404f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    }
6414f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    // TODO: bionic's getnameinfo(3) seems to want its length parameter to be exactly
6424f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    // sizeof(sockaddr_in) for an IPv4 address and sizeof (sockaddr_in6) for an
6434f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    // IPv6 address. Fix getnameinfo so it accepts sizeof(sockaddr_storage), and
6444f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    // then remove this hack.
6454f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    socklen_t size = (ss.ss_family == AF_INET) ? sizeof(sockaddr_in) : sizeof(sockaddr_in6);
6464f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    char buf[NI_MAXHOST]; // NI_MAXHOST is longer than INET6_ADDRSTRLEN.
64775cf14944d476670f6f915e5efd849e238a16250Elliott Hughes    errno = 0;
6484f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    int rc = getnameinfo(reinterpret_cast<sockaddr*>(&ss), size, buf, sizeof(buf), NULL, 0, flags);
6494f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    if (rc != 0) {
6504f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        throwGaiException(env, "getnameinfo", rc);
6514f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        return NULL;
6524f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    }
6534f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    return env->NewStringUTF(buf);
6544f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes}
6554f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes
656396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getpid(JNIEnv*, jobject) {
657396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    return getpid();
658396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
659396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
660396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getppid(JNIEnv*, jobject) {
661396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    return getppid();
662396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
663396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
664d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesstatic jobject Posix_getpwnam(JNIEnv* env, jobject, jstring javaName) {
665d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    ScopedUtfChars name(env, javaName);
666d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    if (name.c_str() == NULL) {
667d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes        return NULL;
668d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    }
669d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    return Passwd(env).getpwnam(name.c_str());
670d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes}
671d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes
672d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesstatic jobject Posix_getpwuid(JNIEnv* env, jobject, jint uid) {
673d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    return Passwd(env).getpwuid(uid);
674d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes}
675d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes
6760a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockname(JNIEnv* env, jobject, jobject javaFd) {
6770a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
6780a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr_storage ss;
6790a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr* sa = reinterpret_cast<sockaddr*>(&ss);
6800a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t byteCount = sizeof(ss);
6810a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&ss, 0, byteCount);
6820a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockname(fd, sa, &byteCount));
6830a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
6840a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockname");
6850a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
6860a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
6870a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return makeSocketAddress(env, &ss);
6880a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
6890a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
6900a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
6910a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
6920a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    u_char result = 0;
6930a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(result);
6940a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size)));
6950a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return result;
6960a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
6970a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
6980a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptInAddr(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
6990a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
7000a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr_storage ss;
7010a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&ss, 0, sizeof(ss));
7020a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    ss.ss_family = AF_INET; // This is only for the IPv4-only IP_MULTICAST_IF.
7030a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr_in* sa = reinterpret_cast<sockaddr_in*>(&ss);
7040a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(sa->sin_addr);
7050a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &sa->sin_addr, &size));
7060a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
7070a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockopt");
7080a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
7090a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
71023ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes    return sockaddrToInetAddress(env, &ss, NULL);
7110a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
7120a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
7130a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
7140a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
7150a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    jint result = 0;
7160a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(result);
7170a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size)));
7180a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return result;
7190a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
7200a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
7210a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
7220a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
7230a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    struct linger l;
7240a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(l);
7250a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&l, 0, size);
7260a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &l, &size));
7270a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
7280a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockopt");
7290a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
7300a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
7310a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return makeStructLinger(env, l);
7320a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
7330a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
7340a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
7350a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
7360a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    struct timeval tv;
7370a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(tv);
7380a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&tv, 0, size);
7390a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &tv, &size));
7400a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
7410a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockopt");
7420a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
7430a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
7440a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return makeStructTimeval(env, tv);
7450a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
7460a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
747396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getuid(JNIEnv*, jobject) {
748396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    return getuid();
749396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
750396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
751a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic jstring Posix_if_indextoname(JNIEnv* env, jobject, jint index) {
752a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    char buf[IF_NAMESIZE];
753a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    char* name = if_indextoname(index, buf);
754a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    // if_indextoname(3) returns NULL on failure, which will come out of NewStringUTF unscathed.
755a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    // There's no useful information in errno, so we don't bother throwing. Callers can null-check.
756a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    return env->NewStringUTF(name);
757a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes}
758a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes
759fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughesstatic jobject Posix_inet_pton(JNIEnv* env, jobject, jint family, jstring javaName) {
7601c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    ScopedUtfChars name(env, javaName);
7611c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    if (name.c_str() == NULL) {
7621c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return NULL;
7631c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
7641c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    sockaddr_storage ss;
7651c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    memset(&ss, 0, sizeof(ss));
766fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes    // sockaddr_in and sockaddr_in6 are at the same address, so we can use either here.
767fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes    void* dst = &reinterpret_cast<sockaddr_in*>(&ss)->sin_addr;
768fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes    if (inet_pton(family, name.c_str(), dst) != 1) {
7691c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return NULL;
7701c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
771fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes    ss.ss_family = family;
77223ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes    return sockaddrToInetAddress(env, &ss, NULL);
7731c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes}
7741c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
775a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic jobject Posix_ioctlInetAddress(JNIEnv* env, jobject, jobject javaFd, jint cmd, jstring javaInterfaceName) {
776a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    struct ifreq req;
777a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    if (!fillIfreq(env, javaInterfaceName, req)) {
778a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes        return NULL;
779a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    }
780a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
781a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &req)));
782a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    if (rc == -1) {
783a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes        return NULL;
784a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    }
78523ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes    return sockaddrToInetAddress(env, reinterpret_cast<sockaddr_storage*>(&req.ifr_addr), NULL);
786a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes}
787a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes
7888b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jint Posix_ioctlInt(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaArg) {
7898b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    // This is complicated because ioctls may return their result by updating their argument
7908b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    // or via their return value, so we need to support both.
791461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
7928b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    static jfieldID valueFid = env->GetFieldID(JniConstants::mutableIntClass, "value", "I");
7938b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    jint arg = env->GetIntField(javaArg, valueFid);
7948b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &arg)));
7958b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    if (!env->ExceptionCheck()) {
7968b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        env->SetIntField(javaArg, valueFid, arg);
7978b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    }
7988b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    return rc;
799461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes}
800461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes
8019a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughesstatic jboolean Posix_isatty(JNIEnv* env, jobject, jobject javaFd) {
8029a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
803bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig    return TEMP_FAILURE_RETRY(isatty(fd)) == 1;
8049a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes}
8059a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes
806a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughesstatic void Posix_kill(JNIEnv* env, jobject, jint pid, jint sig) {
807a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes    throwIfMinusOne(env, "kill", TEMP_FAILURE_RETRY(kill(pid, sig)));
808a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes}
809a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes
81044f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughesstatic void Posix_lchown(JNIEnv* env, jobject, jstring javaPath, jint uid, jint gid) {
81144f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes    ScopedUtfChars path(env, javaPath);
81244f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes    if (path.c_str() == NULL) {
81344f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes        return;
81444f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes    }
81544f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes    throwIfMinusOne(env, "lchown", TEMP_FAILURE_RETRY(lchown(path.c_str(), uid, gid)));
81644f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes}
81744f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes
818e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughesstatic void Posix_listen(JNIEnv* env, jobject, jobject javaFd, jint backlog) {
819e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
820e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes    throwIfMinusOne(env, "listen", TEMP_FAILURE_RETRY(listen(fd, backlog)));
821e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes}
822e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes
823dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughesstatic jlong Posix_lseek(JNIEnv* env, jobject, jobject javaFd, jlong offset, jint whence) {
824dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
8257e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    return throwIfMinusOne(env, "lseek", TEMP_FAILURE_RETRY(lseek64(fd, offset, whence)));
826dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes}
827dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes
82847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_lstat(JNIEnv* env, jobject, jstring javaPath) {
82947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return doStat(env, javaPath, true);
83047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
83147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
8320f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughesstatic void Posix_mincore(JNIEnv* env, jobject, jlong address, jlong byteCount, jbyteArray javaVector) {
8330f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    ScopedByteArrayRW vector(env, javaVector);
8340f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    if (vector.get() == NULL) {
8350f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes        return;
8360f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    }
8370f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
8380f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    unsigned char* vec = reinterpret_cast<unsigned char*>(vector.get());
8390f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    throwIfMinusOne(env, "mincore", TEMP_FAILURE_RETRY(mincore(ptr, byteCount, vec)));
8400f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes}
8410f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes
842c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_mkdir(JNIEnv* env, jobject, jstring javaPath, jint mode) {
843c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    ScopedUtfChars path(env, javaPath);
844c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    if (path.c_str() == NULL) {
845c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes        return;
846c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    }
847c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    throwIfMinusOne(env, "mkdir", TEMP_FAILURE_RETRY(mkdir(path.c_str(), mode)));
848c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes}
849c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes
8507e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_mlock(JNIEnv* env, jobject, jlong address, jlong byteCount) {
8517e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
8527e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    throwIfMinusOne(env, "mlock", TEMP_FAILURE_RETRY(mlock(ptr, byteCount)));
8537e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes}
8547e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes
8557e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic jlong Posix_mmap(JNIEnv* env, jobject, jlong address, jlong byteCount, jint prot, jint flags, jobject javaFd, jlong offset) {
8567e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
8577e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* suggestedPtr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
8587e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = mmap(suggestedPtr, byteCount, prot, flags, fd, offset);
8597e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    if (ptr == MAP_FAILED) {
8607e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "mmap");
8617e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    }
8627e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    return static_cast<jlong>(reinterpret_cast<uintptr_t>(ptr));
8637e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
8647e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
8657e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_msync(JNIEnv* env, jobject, jlong address, jlong byteCount, jint flags) {
8667e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
8677e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "msync", TEMP_FAILURE_RETRY(msync(ptr, byteCount, flags)));
8687e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
8697e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
8707e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_munlock(JNIEnv* env, jobject, jlong address, jlong byteCount) {
8717e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
8727e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    throwIfMinusOne(env, "munlock", TEMP_FAILURE_RETRY(munlock(ptr, byteCount)));
8737e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes}
8747e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes
8757e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_munmap(JNIEnv* env, jobject, jlong address, jlong byteCount) {
8767e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
8777e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "munmap", TEMP_FAILURE_RETRY(munmap(ptr, byteCount)));
8787e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
8797e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
8800ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughesstatic jobject Posix_open(JNIEnv* env, jobject, jstring javaPath, jint flags, jint mode) {
8810ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    ScopedUtfChars path(env, javaPath);
8820ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    if (path.c_str() == NULL) {
8830ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes        return NULL;
8840ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    }
8850ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    int fd = throwIfMinusOne(env, "open", TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode)));
8860ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL;
8870ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes}
8880ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes
88941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughesstatic jobjectArray Posix_pipe(JNIEnv* env, jobject) {
89041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    int fds[2];
89141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    throwIfMinusOne(env, "pipe", TEMP_FAILURE_RETRY(pipe(&fds[0])));
89241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    jobjectArray result = env->NewObjectArray(2, JniConstants::fileDescriptorClass, NULL);
89341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    if (result == NULL) {
89441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        return NULL;
89541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    }
89641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    for (int i = 0; i < 2; ++i) {
89741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        ScopedLocalRef<jobject> fd(env, jniCreateFileDescriptor(env, fds[i]));
89841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        if (fd.get() == NULL) {
89941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes            return NULL;
90041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        }
90141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        env->SetObjectArrayElement(result, i, fd.get());
90241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        if (env->ExceptionCheck()) {
90341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes            return NULL;
90441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        }
90541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    }
90641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    return result;
90741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes}
90841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes
90970c820401677ca251ad09ac64cc23c760764e75dElliott Hughesstatic jint Posix_poll(JNIEnv* env, jobject, jobjectArray javaStructs, jint timeoutMs) {
91070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    static jfieldID fdFid = env->GetFieldID(JniConstants::structPollfdClass, "fd", "Ljava/io/FileDescriptor;");
91170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    static jfieldID eventsFid = env->GetFieldID(JniConstants::structPollfdClass, "events", "S");
91270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    static jfieldID reventsFid = env->GetFieldID(JniConstants::structPollfdClass, "revents", "S");
91370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes
91470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    // Turn the Java libcore.io.StructPollfd[] into a C++ struct pollfd[].
91570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    size_t arrayLength = env->GetArrayLength(javaStructs);
91670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    UniquePtr<struct pollfd[]> fds(new struct pollfd[arrayLength]);
91770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    memset(fds.get(), 0, sizeof(struct pollfd) * arrayLength);
91870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    size_t count = 0; // Some trailing array elements may be irrelevant. (See below.)
91970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    for (size_t i = 0; i < arrayLength; ++i) {
92070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        ScopedLocalRef<jobject> javaStruct(env, env->GetObjectArrayElement(javaStructs, i));
92170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        if (javaStruct.get() == NULL) {
92270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes            break; // We allow trailing nulls in the array for caller convenience.
92370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        }
92470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        ScopedLocalRef<jobject> javaFd(env, env->GetObjectField(javaStruct.get(), fdFid));
92570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        if (javaFd.get() == NULL) {
92670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes            break; // We also allow callers to just clear the fd field (this is what Selector does).
92770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        }
92870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        fds[count].fd = jniGetFDFromFileDescriptor(env, javaFd.get());
92970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        fds[count].events = env->GetShortField(javaStruct.get(), eventsFid);
93070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        ++count;
93170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    }
93270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes
933796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes    // Since we don't know which fds -- if any -- are sockets, be conservative and register
934796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes    // all fds for asynchronous socket close monitoring.
935796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes    std::vector<AsynchronousSocketCloseMonitor*> monitors;
936796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes    for (size_t i = 0; i < count; ++i) {
937796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes        monitors.push_back(new AsynchronousSocketCloseMonitor(fds[i].fd));
938796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes    }
939796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes    int rc = poll(fds.get(), count, timeoutMs);
940796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes    for (size_t i = 0; i < monitors.size(); ++i) {
941796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes        delete monitors[i];
942796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes    }
94370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    if (rc == -1) {
94470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        throwErrnoException(env, "poll");
94570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        return -1;
94670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    }
94770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes
94870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    // Update the revents fields in the Java libcore.io.StructPollfd[].
94998b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes    for (size_t i = 0; i < count; ++i) {
95070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        ScopedLocalRef<jobject> javaStruct(env, env->GetObjectArrayElement(javaStructs, i));
95198b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes        if (javaStruct.get() == NULL) {
95298b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes            return -1;
95398b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes        }
95470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        env->SetShortField(javaStruct.get(), reventsFid, fds[i].revents);
95570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    }
95670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    return rc;
95770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes}
95870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes
959e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughesstatic jint Posix_preadBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount, jlong offset) {
960e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    ScopedBytesRW bytes(env, javaBytes);
961e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    if (bytes.get() == NULL) {
962e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return -1;
963e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    }
964e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
965e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    return throwIfMinusOne(env, "pread", TEMP_FAILURE_RETRY(pread64(fd, bytes.get() + byteOffset, byteCount, offset)));
966e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes}
967e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes
968e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughesstatic jint Posix_pwriteBytes(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount, jlong offset) {
969e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    ScopedBytesRO bytes(env, javaBytes);
970e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    if (bytes.get() == NULL) {
971e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return -1;
972e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    }
973e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
974e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    return throwIfMinusOne(env, "pwrite", TEMP_FAILURE_RETRY(pwrite64(fd, bytes.get() + byteOffset, byteCount, offset)));
975e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes}
976e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes
9770568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughesstatic jint Posix_readBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount) {
9780568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes    ScopedBytesRW bytes(env, javaBytes);
97926c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    if (bytes.get() == NULL) {
98026c7025a7a919044771fb89031161bd26fe03032Elliott Hughes        return -1;
98126c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    }
98226c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
9830568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes    return throwIfMinusOne(env, "read", TEMP_FAILURE_RETRY(read(fd, bytes.get() + byteOffset, byteCount)));
98426c7025a7a919044771fb89031161bd26fe03032Elliott Hughes}
98526c7025a7a919044771fb89031161bd26fe03032Elliott Hughes
986bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_readv(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) {
9878dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes    IoVec<ScopedBytesRW> ioVec(env, env->GetArrayLength(buffers));
988bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    if (!ioVec.init(buffers, offsets, byteCounts)) {
989bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return -1;
990bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
991bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
992bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    return throwIfMinusOne(env, "readv", TEMP_FAILURE_RETRY(readv(fd, ioVec.get(), ioVec.size())));
993bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes}
994bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
99523ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughesstatic jint Posix_recvfromBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount, jint flags, jobject javaInetSocketAddress) {
99623ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes    ScopedBytesRW bytes(env, javaBytes);
99723ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes    if (bytes.get() == NULL) {
99823ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes        return -1;
99923ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes    }
100023ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes    sockaddr_storage ss;
100123ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes    socklen_t sl = sizeof(ss);
100223ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes    memset(&ss, 0, sizeof(ss));
100323ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes    sockaddr* from = (javaInetSocketAddress != NULL) ? reinterpret_cast<sockaddr*>(&ss) : NULL;
100423ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes    socklen_t* fromLength = (javaInetSocketAddress != NULL) ? &sl : 0;
10051124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes    jint recvCount = NET_FAILURE_RETRY(env, ssize_t, recvfrom, javaFd, bytes.get() + byteOffset, byteCount, flags, from, fromLength);
1006553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    fillInetSocketAddress(env, recvCount, javaInetSocketAddress, &ss);
100723ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes    return recvCount;
100823ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes}
100923ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes
1010c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_remove(JNIEnv* env, jobject, jstring javaPath) {
1011c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    ScopedUtfChars path(env, javaPath);
1012c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    if (path.c_str() == NULL) {
1013c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes        return;
1014c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    }
1015c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    throwIfMinusOne(env, "remove", TEMP_FAILURE_RETRY(remove(path.c_str())));
1016c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes}
1017c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes
1018a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_rename(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) {
1019a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars oldPath(env, javaOldPath);
1020a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (oldPath.c_str() == NULL) {
1021a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
1022a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
1023a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars newPath(env, javaNewPath);
1024a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (newPath.c_str() == NULL) {
1025a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
1026a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
1027a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    throwIfMinusOne(env, "rename", TEMP_FAILURE_RETRY(rename(oldPath.c_str(), newPath.c_str())));
1028a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes}
1029a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes
10308b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jlong Posix_sendfile(JNIEnv* env, jobject, jobject javaOutFd, jobject javaInFd, jobject javaOffset, jlong byteCount) {
10318b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    int outFd = jniGetFDFromFileDescriptor(env, javaOutFd);
10328b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    int inFd = jniGetFDFromFileDescriptor(env, javaInFd);
10338b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    static jfieldID valueFid = env->GetFieldID(JniConstants::mutableLongClass, "value", "J");
10348b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    off_t offset = 0;
10358b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    off_t* offsetPtr = NULL;
10368b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    if (javaOffset != NULL) {
10378b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        // TODO: fix bionic so we can have a 64-bit off_t!
10388b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        offset = env->GetLongField(javaOffset, valueFid);
10398b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        offsetPtr = &offset;
10408b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    }
10418b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    jlong result = throwIfMinusOne(env, "sendfile", TEMP_FAILURE_RETRY(sendfile(outFd, inFd, offsetPtr, byteCount)));
10428b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    if (javaOffset != NULL) {
10438b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        env->SetLongField(javaOffset, valueFid, offset);
10448b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    }
10458b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    return result;
10468b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes}
10478b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes
104890d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughesstatic jint Posix_sendtoBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount, jint flags, jobject javaInetAddress, jint port) {
104990d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes    ScopedBytesRO bytes(env, javaBytes);
105090d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes    if (bytes.get() == NULL) {
105190d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes        return -1;
105290d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes    }
105390d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes    sockaddr_storage ss;
105490d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes    if (javaInetAddress != NULL && !inetAddressToSockaddr(env, javaInetAddress, port, &ss)) {
105590d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes        return -1;
105690d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes    }
105790d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes    const sockaddr* to = (javaInetAddress != NULL) ? reinterpret_cast<const sockaddr*>(&ss) : NULL;
105890d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes    socklen_t toLength = (javaInetAddress != NULL) ? sizeof(ss) : 0;
10591124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes    return NET_FAILURE_RETRY(env, ssize_t, sendto, javaFd, bytes.get() + byteOffset, byteCount, flags, to, toLength);
106090d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes}
106190d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes
1062396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_setegid(JNIEnv* env, jobject, jint egid) {
1063396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    throwIfMinusOne(env, "setegid", TEMP_FAILURE_RETRY(setegid(egid)));
1064396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
1065396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
1066396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_seteuid(JNIEnv* env, jobject, jint euid) {
1067396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    throwIfMinusOne(env, "seteuid", TEMP_FAILURE_RETRY(seteuid(euid)));
1068396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
1069396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
1070396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_setgid(JNIEnv* env, jobject, jint gid) {
1071396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    throwIfMinusOne(env, "setgid", TEMP_FAILURE_RETRY(setgid(gid)));
1072396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
1073396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
1074c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) {
1075c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
1076c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    u_char byte = value;
1077c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &byte, sizeof(byte))));
1078c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes}
1079c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes
1080a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic void Posix_setsockoptIfreq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jstring javaInterfaceName) {
1081a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    struct ifreq req;
1082a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    if (!fillIfreq(env, javaInterfaceName, req)) {
1083b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes        return;
1084b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    }
1085b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
1086b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req))));
1087b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes}
1088b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes
1089454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic void Posix_setsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) {
1090454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
1091454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))));
1092454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes}
1093454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes
1094b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughesstatic void Posix_setsockoptIpMreqn(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) {
1095b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    ip_mreqn req;
1096b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    memset(&req, 0, sizeof(req));
1097b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    req.imr_ifindex = value;
1098b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
1099b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req))));
1100b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes}
1101b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes
1102438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughesstatic void Posix_setsockoptGroupReq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaGroupReq) {
11038ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes    struct group_req req;
11048ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes    memset(&req, 0, sizeof(req));
1105438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes
1106438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    static jfieldID grInterfaceFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_interface", "I");
11078ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes    req.gr_interface = env->GetIntField(javaGroupReq, grInterfaceFid);
1108438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    // Get the IPv4 or IPv6 multicast address to join or leave.
1109438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    static jfieldID grGroupFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_group", "Ljava/net/InetAddress;");
1110e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes    ScopedLocalRef<jobject> javaGroup(env, env->GetObjectField(javaGroupReq, grGroupFid));
11118ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes    if (!inetAddressToSockaddrVerbatim(env, javaGroup.get(), 0, &req.gr_group)) {
1112438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        return;
1113438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    }
1114438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes
1115438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
11168ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes    int rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req)));
1117438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    if (rc == -1 && errno == EINVAL) {
1118438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        // Maybe we're a 32-bit binary talking to a 64-bit kernel?
1119438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        // glibc doesn't automatically handle this.
11209634b569a92841f32268907c88c408f22cb00486Elliott Hughes        struct group_req64 {
1121438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes            uint32_t gr_interface;
1122438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes            uint32_t my_padding;
1123438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes            sockaddr_storage gr_group;
1124438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        };
11258ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes        group_req64 req64;
11268ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes        req64.gr_interface = req.gr_interface;
11278ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes        memcpy(&req64.gr_group, &req.gr_group, sizeof(req.gr_group));
11288ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes        rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req64, sizeof(req64)));
1129438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    }
1130438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    throwIfMinusOne(env, "setsockopt", rc);
1131438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes}
1132438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes
1133c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaLinger) {
1134c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID lOnoffFid = env->GetFieldID(JniConstants::structLingerClass, "l_onoff", "I");
1135c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID lLingerFid = env->GetFieldID(JniConstants::structLingerClass, "l_linger", "I");
1136c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
1137c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    struct linger value;
1138c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    value.l_onoff = env->GetIntField(javaLinger, lOnoffFid);
1139c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    value.l_linger = env->GetIntField(javaLinger, lLingerFid);
1140c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))));
1141c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes}
1142c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes
1143c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaTimeval) {
1144c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID tvSecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_sec", "J");
1145c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID tvUsecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_usec", "J");
1146c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
1147c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    struct timeval value;
114839fd5e164f93ab821841aa4d1fac09055c102b8bElliott Hughes    value.tv_sec = env->GetLongField(javaTimeval, tvSecFid);
114939fd5e164f93ab821841aa4d1fac09055c102b8bElliott Hughes    value.tv_usec = env->GetLongField(javaTimeval, tvUsecFid);
1150c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))));
1151c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes}
1152c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes
1153396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_setuid(JNIEnv* env, jobject, jint uid) {
1154396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    throwIfMinusOne(env, "setuid", TEMP_FAILURE_RETRY(setuid(uid)));
1155396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes}
1156396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes
115759e4744d27231f260271dbbca406e0cc39768116Elliott Hughesstatic void Posix_shutdown(JNIEnv* env, jobject, jobject javaFd, jint how) {
115859e4744d27231f260271dbbca406e0cc39768116Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
115959e4744d27231f260271dbbca406e0cc39768116Elliott Hughes    throwIfMinusOne(env, "shutdown", TEMP_FAILURE_RETRY(shutdown(fd, how)));
116059e4744d27231f260271dbbca406e0cc39768116Elliott Hughes}
116159e4744d27231f260271dbbca406e0cc39768116Elliott Hughes
1162454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic jobject Posix_socket(JNIEnv* env, jobject, jint domain, jint type, jint protocol) {
1163454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    int fd = throwIfMinusOne(env, "socket", TEMP_FAILURE_RETRY(socket(domain, type, protocol)));
1164454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL;
1165454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes}
1166454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes
116747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_stat(JNIEnv* env, jobject, jstring javaPath) {
116847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return doStat(env, javaPath, false);
116947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
117047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
11710ade5524d972b9dfd69b0126aa4c35319c1b7fa4Elliott Hughesstatic jobject Posix_statfs(JNIEnv* env, jobject, jstring javaPath) {
117259fa7163774d6930a174bc038414a4b780581957Elliott Hughes    ScopedUtfChars path(env, javaPath);
117359fa7163774d6930a174bc038414a4b780581957Elliott Hughes    if (path.c_str() == NULL) {
117459fa7163774d6930a174bc038414a4b780581957Elliott Hughes        return NULL;
117559fa7163774d6930a174bc038414a4b780581957Elliott Hughes    }
117659fa7163774d6930a174bc038414a4b780581957Elliott Hughes    struct statfs sb;
117759fa7163774d6930a174bc038414a4b780581957Elliott Hughes    int rc = TEMP_FAILURE_RETRY(statfs(path.c_str(), &sb));
117859fa7163774d6930a174bc038414a4b780581957Elliott Hughes    if (rc == -1) {
117959fa7163774d6930a174bc038414a4b780581957Elliott Hughes        throwErrnoException(env, "statfs");
118059fa7163774d6930a174bc038414a4b780581957Elliott Hughes        return NULL;
118159fa7163774d6930a174bc038414a4b780581957Elliott Hughes    }
118259fa7163774d6930a174bc038414a4b780581957Elliott Hughes    return makeStructStatFs(env, sb);
118359fa7163774d6930a174bc038414a4b780581957Elliott Hughes}
118459fa7163774d6930a174bc038414a4b780581957Elliott Hughes
1185ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jstring Posix_strerror(JNIEnv* env, jobject, jint errnum) {
118652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    char buffer[BUFSIZ];
1187ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    const char* message = jniStrError(errnum, buffer, sizeof(buffer));
1188ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return env->NewStringUTF(message);
1189ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
1190ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
1191a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_symlink(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) {
1192a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars oldPath(env, javaOldPath);
1193a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (oldPath.c_str() == NULL) {
1194a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
1195a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
1196a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars newPath(env, javaNewPath);
1197a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (newPath.c_str() == NULL) {
1198a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
1199a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
1200a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    throwIfMinusOne(env, "symlink", TEMP_FAILURE_RETRY(symlink(oldPath.c_str(), newPath.c_str())));
1201a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes}
1202a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes
12036fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughesstatic jlong Posix_sysconf(JNIEnv* env, jobject, jint name) {
12046fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    // Since -1 is a valid result from sysconf(3), detecting failure is a little more awkward.
12056fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    errno = 0;
12066fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    long result = sysconf(name);
12076fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    if (result == -1L && errno == EINVAL) {
12087e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "sysconf");
12096fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    }
12106fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    return result;
12116fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes}
12126fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes
1213bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestigstatic void Posix_tcdrain(JNIEnv* env, jobject, jobject javaFd) {
1214bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig    int fd = jniGetFDFromFileDescriptor(env, javaFd);
1215bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig    throwIfMinusOne(env, "tcdrain", TEMP_FAILURE_RETRY(tcdrain(fd)));
1216bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig}
1217bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig
121844f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughesstatic jint Posix_umask(JNIEnv*, jint mask) {
121944f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes    return umask(mask);
122044f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes}
122144f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes
12227341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject Posix_uname(JNIEnv* env, jobject) {
12237341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    struct utsname buf;
12249b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes    if (TEMP_FAILURE_RETRY(uname(&buf)) == -1) {
12259b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes        return NULL; // Can't happen.
12267341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    }
12277341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    return makeStructUtsname(env, buf);
12287341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes}
12297341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
12309e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughesstatic jint Posix_waitpid(JNIEnv* env, jobject, jint pid, jobject javaStatus, jint options) {
12319e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes    int status;
12329e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes    int rc = throwIfMinusOne(env, "waitpid", TEMP_FAILURE_RETRY(waitpid(pid, &status, options)));
12339e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes    if (rc != -1) {
12349e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes        static jfieldID valueFid = env->GetFieldID(JniConstants::mutableIntClass, "value", "I");
12359e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes        env->SetIntField(javaStatus, valueFid, status);
12369e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes    }
12379e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes    return rc;
12389e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes}
12399e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes
12400568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughesstatic jint Posix_writeBytes(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount) {
12410568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes    ScopedBytesRO bytes(env, javaBytes);
124278c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    if (bytes.get() == NULL) {
124378c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes        return -1;
124478c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    }
124578c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
12460568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes    return throwIfMinusOne(env, "write", TEMP_FAILURE_RETRY(write(fd, bytes.get() + byteOffset, byteCount)));
124778c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes}
124878c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes
1249bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_writev(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) {
12508dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes    IoVec<ScopedBytesRO> ioVec(env, env->GetArrayLength(buffers));
1251bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    if (!ioVec.init(buffers, offsets, byteCounts)) {
1252bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return -1;
1253bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
1254bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
1255bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    return throwIfMinusOne(env, "writev", TEMP_FAILURE_RETRY(writev(fd, ioVec.get(), ioVec.size())));
1256bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes}
1257bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
1258ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic JNINativeMethod gMethods[] = {
1259553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes    NATIVE_METHOD(Posix, accept, "(Ljava/io/FileDescriptor;Ljava/net/InetSocketAddress;)Ljava/io/FileDescriptor;"),
1260ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, access, "(Ljava/lang/String;I)Z"),
1261da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes    NATIVE_METHOD(Posix, bind, "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)V"),
1262b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    NATIVE_METHOD(Posix, chmod, "(Ljava/lang/String;I)V"),
126344f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes    NATIVE_METHOD(Posix, chown, "(Ljava/lang/String;II)V"),
1264462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    NATIVE_METHOD(Posix, close, "(Ljava/io/FileDescriptor;)V"),
1265996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    NATIVE_METHOD(Posix, connect, "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)V"),
1266396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, dup, "(Ljava/io/FileDescriptor;)Ljava/io/FileDescriptor;"),
1267396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, dup2, "(Ljava/io/FileDescriptor;I)Ljava/io/FileDescriptor;"),
1268ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, environ, "()[Ljava/lang/String;"),
126944f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes    NATIVE_METHOD(Posix, fchmod, "(Ljava/io/FileDescriptor;I)V"),
127044f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes    NATIVE_METHOD(Posix, fchown, "(Ljava/io/FileDescriptor;II)V"),
1271fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    NATIVE_METHOD(Posix, fcntlVoid, "(Ljava/io/FileDescriptor;I)I"),
1272fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    NATIVE_METHOD(Posix, fcntlLong, "(Ljava/io/FileDescriptor;IJ)I"),
1273fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    NATIVE_METHOD(Posix, fcntlFlock, "(Ljava/io/FileDescriptor;ILlibcore/io/StructFlock;)I"),
127452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    NATIVE_METHOD(Posix, fdatasync, "(Ljava/io/FileDescriptor;)V"),
127547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, fstat, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStat;"),
127659fa7163774d6930a174bc038414a4b780581957Elliott Hughes    NATIVE_METHOD(Posix, fstatfs, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStatFs;"),
127752724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    NATIVE_METHOD(Posix, fsync, "(Ljava/io/FileDescriptor;)V"),
1278f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    NATIVE_METHOD(Posix, ftruncate, "(Ljava/io/FileDescriptor;J)V"),
12794f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    NATIVE_METHOD(Posix, gai_strerror, "(I)Ljava/lang/String;"),
12801c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    NATIVE_METHOD(Posix, getaddrinfo, "(Ljava/lang/String;Llibcore/io/StructAddrinfo;)[Ljava/net/InetAddress;"),
1281396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, getegid, "()I"),
1282396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, geteuid, "()I"),
1283396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, getgid, "()I"),
1284ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    NATIVE_METHOD(Posix, getenv, "(Ljava/lang/String;)Ljava/lang/String;"),
12854f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    NATIVE_METHOD(Posix, getnameinfo, "(Ljava/net/InetAddress;I)Ljava/lang/String;"),
1286396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, getpid, "()I"),
1287396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, getppid, "()I"),
1288d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    NATIVE_METHOD(Posix, getpwnam, "(Ljava/lang/String;)Llibcore/io/StructPasswd;"),
1289d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes    NATIVE_METHOD(Posix, getpwuid, "(I)Llibcore/io/StructPasswd;"),
12900a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockname, "(Ljava/io/FileDescriptor;)Ljava/net/SocketAddress;"),
12910a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptByte, "(Ljava/io/FileDescriptor;II)I"),
12920a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptInAddr, "(Ljava/io/FileDescriptor;II)Ljava/net/InetAddress;"),
12930a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptInt, "(Ljava/io/FileDescriptor;II)I"),
12940a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptLinger, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructLinger;"),
12950a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptTimeval, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructTimeval;"),
1296396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, getuid, "()I"),
1297a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    NATIVE_METHOD(Posix, if_indextoname, "(I)Ljava/lang/String;"),
1298fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes    NATIVE_METHOD(Posix, inet_pton, "(ILjava/lang/String;)Ljava/net/InetAddress;"),
1299a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    NATIVE_METHOD(Posix, ioctlInetAddress, "(Ljava/io/FileDescriptor;ILjava/lang/String;)Ljava/net/InetAddress;"),
13008b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    NATIVE_METHOD(Posix, ioctlInt, "(Ljava/io/FileDescriptor;ILlibcore/util/MutableInt;)I"),
13019a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    NATIVE_METHOD(Posix, isatty, "(Ljava/io/FileDescriptor;)Z"),
1302a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes    NATIVE_METHOD(Posix, kill, "(II)V"),
130344f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes    NATIVE_METHOD(Posix, lchown, "(Ljava/lang/String;II)V"),
1304e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes    NATIVE_METHOD(Posix, listen, "(Ljava/io/FileDescriptor;I)V"),
1305dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    NATIVE_METHOD(Posix, lseek, "(Ljava/io/FileDescriptor;JI)J"),
130647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, lstat, "(Ljava/lang/String;)Llibcore/io/StructStat;"),
13070f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    NATIVE_METHOD(Posix, mincore, "(JJ[B)V"),
1308c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    NATIVE_METHOD(Posix, mkdir, "(Ljava/lang/String;I)V"),
13097e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    NATIVE_METHOD(Posix, mlock, "(JJ)V"),
13107e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, mmap, "(JJIILjava/io/FileDescriptor;J)J"),
13117e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, msync, "(JJI)V"),
13127e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    NATIVE_METHOD(Posix, munlock, "(JJ)V"),
13137e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, munmap, "(JJ)V"),
13140ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    NATIVE_METHOD(Posix, open, "(Ljava/lang/String;II)Ljava/io/FileDescriptor;"),
131541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    NATIVE_METHOD(Posix, pipe, "()[Ljava/io/FileDescriptor;"),
131670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    NATIVE_METHOD(Posix, poll, "([Llibcore/io/StructPollfd;I)I"),
1317e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    NATIVE_METHOD(Posix, preadBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIJ)I"),
1318e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    NATIVE_METHOD(Posix, pwriteBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIJ)I"),
13190568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes    NATIVE_METHOD(Posix, readBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"),
1320bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    NATIVE_METHOD(Posix, readv, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"),
132123ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes    NATIVE_METHOD(Posix, recvfromBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIILjava/net/InetSocketAddress;)I"),
1322c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    NATIVE_METHOD(Posix, remove, "(Ljava/lang/String;)V"),
1323a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    NATIVE_METHOD(Posix, rename, "(Ljava/lang/String;Ljava/lang/String;)V"),
13248b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    NATIVE_METHOD(Posix, sendfile, "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Llibcore/util/MutableLong;J)J"),
132590d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes    NATIVE_METHOD(Posix, sendtoBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIILjava/net/InetAddress;I)I"),
1326396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, setegid, "(I)V"),
1327396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, seteuid, "(I)V"),
1328396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, setgid, "(I)V"),
1329c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    NATIVE_METHOD(Posix, setsockoptByte, "(Ljava/io/FileDescriptor;III)V"),
1330b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    NATIVE_METHOD(Posix, setsockoptIfreq, "(Ljava/io/FileDescriptor;IILjava/lang/String;)V"),
1331454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    NATIVE_METHOD(Posix, setsockoptInt, "(Ljava/io/FileDescriptor;III)V"),
1332b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    NATIVE_METHOD(Posix, setsockoptIpMreqn, "(Ljava/io/FileDescriptor;III)V"),
1333438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    NATIVE_METHOD(Posix, setsockoptGroupReq, "(Ljava/io/FileDescriptor;IILlibcore/io/StructGroupReq;)V"),
1334c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    NATIVE_METHOD(Posix, setsockoptLinger, "(Ljava/io/FileDescriptor;IILlibcore/io/StructLinger;)V"),
1335c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    NATIVE_METHOD(Posix, setsockoptTimeval, "(Ljava/io/FileDescriptor;IILlibcore/io/StructTimeval;)V"),
1336396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes    NATIVE_METHOD(Posix, setuid, "(I)V"),
133759e4744d27231f260271dbbca406e0cc39768116Elliott Hughes    NATIVE_METHOD(Posix, shutdown, "(Ljava/io/FileDescriptor;I)V"),
1338454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    NATIVE_METHOD(Posix, socket, "(III)Ljava/io/FileDescriptor;"),
133947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, stat, "(Ljava/lang/String;)Llibcore/io/StructStat;"),
134059fa7163774d6930a174bc038414a4b780581957Elliott Hughes    NATIVE_METHOD(Posix, statfs, "(Ljava/lang/String;)Llibcore/io/StructStatFs;"),
1341ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, strerror, "(I)Ljava/lang/String;"),
1342a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    NATIVE_METHOD(Posix, symlink, "(Ljava/lang/String;Ljava/lang/String;)V"),
13436fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    NATIVE_METHOD(Posix, sysconf, "(I)J"),
1344bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig    NATIVE_METHOD(Posix, tcdrain, "(Ljava/io/FileDescriptor;)V"),
134544f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes    NATIVE_METHOD(Posix, umask, "(I)I"),
13467341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    NATIVE_METHOD(Posix, uname, "()Llibcore/io/StructUtsname;"),
13479e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes    NATIVE_METHOD(Posix, waitpid, "(ILlibcore/util/MutableInt;I)I"),
13480568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes    NATIVE_METHOD(Posix, writeBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"),
1349bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    NATIVE_METHOD(Posix, writev, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"),
1350ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes};
13517cd6760f7045d771faae8080a8c6150bf678f679Elliott Hughesvoid register_libcore_io_Posix(JNIEnv* env) {
13527cd6760f7045d771faae8080a8c6150bf678f679Elliott Hughes    jniRegisterNativeMethods(env, "libcore/io/Posix", gMethods, NELEM(gMethods));
1353ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes}
1354