libcore_io_Linux.cpp revision 70c820401677ca251ad09ac64cc23c760764e75d
1ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes/*
2ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * Copyright (C) 2011 The Android Open Source Project
3ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes *
4ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * you may not use this file except in compliance with the License.
6ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * You may obtain a copy of the License at
7ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes *
8ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes *
10ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * Unless required by applicable law or agreed to in writing, software
11ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * See the License for the specific language governing permissions and
14ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * limitations under the License.
15ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes */
16ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
17ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#define LOG_TAG "Posix"
18ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
19996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes#include "AsynchronousSocketCloseMonitor.h"
20ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "JNIHelp.h"
21ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "JniConstants.h"
22ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include "JniException.h"
230a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include "NetworkUtilities.h"
240568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes#include "ScopedBytes.h"
25e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes#include "ScopedLocalRef.h"
260f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes#include "ScopedPrimitiveArray.h"
27ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "ScopedUtfChars.h"
2859fa7163774d6930a174bc038414a4b780581957Elliott Hughes#include "StaticAssert.h"
291c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes#include "UniquePtr.h"
30ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include "toStringArray.h"
31ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
321c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes#include <arpa/inet.h>
33ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include <errno.h>
340ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <fcntl.h>
35b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes#include <net/if.h>
364f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes#include <netdb.h>
370a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include <netinet/in.h>
381c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes#include <netinet/in.h>
3970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes#include <poll.h>
40a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes#include <signal.h>
41ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include <stdlib.h>
42461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes#include <sys/ioctl.h>
437e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes#include <sys/mman.h>
448b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes#include <sys/sendfile.h>
4559e4744d27231f260271dbbca406e0cc39768116Elliott Hughes#include <sys/socket.h>
461c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes#include <sys/socket.h>
4747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes#include <sys/stat.h>
480a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include <sys/time.h>
490ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <sys/types.h>
50bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes#include <sys/uio.h>
517341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#include <sys/utsname.h>
5259fa7163774d6930a174bc038414a4b780581957Elliott Hughes#include <sys/vfs.h> // Bionic doesn't have <sys/statvfs.h>
530ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <unistd.h>
54ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
551c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughesstruct addrinfo_deleter {
561c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    void operator()(addrinfo* p) const {
571c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        if (p != NULL) { // bionic's freeaddrinfo(3) crashes when passed NULL.
581c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            freeaddrinfo(p);
591c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        }
601c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
611c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes};
621c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
63996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes/**
64996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * Used to retry syscalls that can return EINTR. This differs from TEMP_FAILURE_RETRY in that
65996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * it also considers the case where the reason for failure is that another thread called
66996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * Socket.close.
67996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes *
68996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * Assumes 'JNIEnv* env' and 'jobject javaFd' (which is a java.io.FileDescriptor) are in scope.
69996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes *
70996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * Returns the result of 'exp', though a Java exception will be pending if the result is -1.
71996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes *
72996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * Correct usage looks like this:
73996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes *
74996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * void Posix_syscall(JNIEnv* env, jobject javaFd, ...) {
75996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes *     ...
76996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes *     int fd;
77996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes *     NET_FAILURE_RETRY("syscall", syscall(fd, ...)); // Throws on error.
78996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * }
79996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes */
80996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes#define NET_FAILURE_RETRY(syscall_name, exp) ({ \
81996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    typeof (exp) _rc = -1; \
82996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    do { \
83996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes        { \
84996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes            fd = jniGetFDFromFileDescriptor(env, javaFd); \
85996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes            AsynchronousSocketCloseMonitor monitor(fd); \
86996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes            _rc = (exp); \
87996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes        } \
88996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes        if (_rc == -1) { \
89996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes            if (jniGetFDFromFileDescriptor(env, javaFd) == -1) { \
90996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes                jniThrowException(env, "java/net/SocketException", "Socket closed"); \
91996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes                break; \
92996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes            } else if (errno != EINTR) { \
93996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes                throwErrnoException(env, syscall_name); \
94996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes                break; \
95996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes            } \
96996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes        } \
97996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    } while (_rc == -1); \
98996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    _rc; })
99996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes
1004f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwException(JNIEnv* env, jclass exceptionClass, jmethodID ctor3, jmethodID ctor2,
1014f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        const char* functionName, int error) {
102ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    jthrowable cause = NULL;
103ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (env->ExceptionCheck()) {
104ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        cause = env->ExceptionOccurred();
105ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        env->ExceptionClear();
106ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
107ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
1084f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName));
1094f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    if (detailMessage.get() == NULL) {
110f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        // Not really much we can do here. We're probably dead in the water,
111f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        // but let's try to stumble on...
112f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        env->ExceptionClear();
113f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    }
114f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes
115ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    jobject exception;
116ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (cause != NULL) {
1174f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        exception = env->NewObject(exceptionClass, ctor3, detailMessage.get(), error, cause);
118ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    } else {
1194f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        exception = env->NewObject(exceptionClass, ctor2, detailMessage.get(), error);
120ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
121ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    env->Throw(reinterpret_cast<jthrowable>(exception));
122dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes}
123dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes
1244f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwErrnoException(JNIEnv* env, const char* functionName) {
1254f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    int error = errno;
1264f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    static jmethodID ctor3 = env->GetMethodID(JniConstants::errnoExceptionClass,
1274f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V");
1284f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    static jmethodID ctor2 = env->GetMethodID(JniConstants::errnoExceptionClass,
1294f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            "<init>", "(Ljava/lang/String;I)V");
1304f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    throwException(env, JniConstants::errnoExceptionClass, ctor3, ctor2, functionName, error);
1314f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes}
1324f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes
1334f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwGaiException(JNIEnv* env, const char* functionName, int error) {
1344f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    if (error == EAI_SYSTEM) {
1354f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        // EAI_SYSTEM means "look at errno instead", so we want our GaiException to have the
1364f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        // relevant ErrnoException as its cause.
1374f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        throwErrnoException(env, functionName);
1384f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        // Deliberately fall through to throw another exception...
1394f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    }
1404f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    static jmethodID ctor3 = env->GetMethodID(JniConstants::gaiExceptionClass,
1414f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V");
1424f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    static jmethodID ctor2 = env->GetMethodID(JniConstants::gaiExceptionClass,
1434f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes            "<init>", "(Ljava/lang/String;I)V");
1444f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    throwException(env, JniConstants::gaiExceptionClass, ctor3, ctor2, functionName, error);
1454f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes}
1464f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes
147dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughestemplate <typename rc_t>
1487e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic rc_t throwIfMinusOne(JNIEnv* env, const char* name, rc_t rc) {
149dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == rc_t(-1)) {
1507e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, name);
151dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    }
152dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    return rc;
15347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
15447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
1558dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughestemplate <typename ScopedT>
156bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesclass IoVec {
157bbac92e691de7d570928ddfba639067978e55b06Elliott Hughespublic:
158bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    IoVec(JNIEnv* env, size_t bufferCount) : mEnv(env), mBufferCount(bufferCount) {
159bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
160bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
161bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    bool init(jobjectArray javaBuffers, jintArray javaOffsets, jintArray javaByteCounts) {
162bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        // We can't delete our local references until after the I/O, so make sure we have room.
163bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (mEnv->PushLocalFrame(mBufferCount + 16) < 0) {
164bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            return false;
165bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
166bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        ScopedIntArrayRO offsets(mEnv, javaOffsets);
167bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (offsets.get() == NULL) {
168bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            return false;
169bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
170bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        ScopedIntArrayRO byteCounts(mEnv, javaByteCounts);
171bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (byteCounts.get() == NULL) {
172bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            return false;
173bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
174bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        // TODO: Linux actually has a 1024 buffer limit. glibc works around this, and we should too.
175bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        for (size_t i = 0; i < mBufferCount; ++i) {
176e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes            jobject buffer = mEnv->GetObjectArrayElement(javaBuffers, i); // We keep this local ref.
1778dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes            mScopedBuffers.push_back(new ScopedT(mEnv, buffer));
1788dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes            jbyte* ptr = const_cast<jbyte*>(mScopedBuffers.back()->get());
179bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            if (ptr == NULL) {
180bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                return false;
181bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            }
182bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            struct iovec iov;
183bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            iov.iov_base = reinterpret_cast<void*>(ptr + offsets[i]);
184bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            iov.iov_len = byteCounts[i];
185bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            mIoVec.push_back(iov);
186bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
187bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return true;
188bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
189bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
190bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    ~IoVec() {
1918dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes        for (size_t i = 0; i < mScopedBuffers.size(); ++i) {
1928dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes            delete mScopedBuffers[i];
193bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
194bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        mEnv->PopLocalFrame(NULL);
195bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
196bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
197bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    iovec* get() {
198bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return &mIoVec[0];
199bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
200bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
201bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    size_t size() {
202bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return mBufferCount;
203bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
204bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
205bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesprivate:
206bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    JNIEnv* mEnv;
207bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    size_t mBufferCount;
208bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    std::vector<iovec> mIoVec;
2098dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes    std::vector<ScopedT*> mScopedBuffers;
210bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes};
211bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
2120a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeInetSocketAddress(JNIEnv* env, const sockaddr_storage* ss, int port) {
2130a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    jobject inetAddress = socketAddressToInetAddress(env, ss);
2140a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (inetAddress == NULL) {
2150a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
2160a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
2170a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::inetSocketAddressClass, "<init>",
2180a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes            "(Ljava/net/InetAddress;I)V");
2190a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return env->NewObject(JniConstants::inetSocketAddressClass, ctor, inetAddress, port);
2200a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
2210a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
2220a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeSocketAddress(JNIEnv* env, const sockaddr_storage* ss) {
2230a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (ss->ss_family == AF_INET) {
2240a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        int port = ntohs(reinterpret_cast<const sockaddr_in*>(ss)->sin_port);
2250a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return makeInetSocketAddress(env, ss, port);
2260a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    } else if (ss->ss_family == AF_INET6) {
2270a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        int port = ntohs(reinterpret_cast<const sockaddr_in6*>(ss)->sin6_port);
2280a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return makeInetSocketAddress(env, ss, port);
2290a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
2300a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    // TODO: support AF_UNIX and AF_UNSPEC, and have some other behavior for other families
2310a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return NULL;
2320a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
2330a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
23447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject makeStructStat(JNIEnv* env, const struct stat& sb) {
23547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structStatClass, "<init>",
23647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes            "(JJIJIIJJJJJJJ)V");
23747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return env->NewObject(JniConstants::structStatClass, ctor,
23859fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_dev), static_cast<jlong>(sb.st_ino),
23959fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jint>(sb.st_mode), static_cast<jlong>(sb.st_nlink),
24059fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jint>(sb.st_uid), static_cast<jint>(sb.st_gid),
24159fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_rdev), static_cast<jlong>(sb.st_size),
24259fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_atime), static_cast<jlong>(sb.st_mtime),
24359fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_ctime), static_cast<jlong>(sb.st_blksize),
24459fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_blocks));
24559fa7163774d6930a174bc038414a4b780581957Elliott Hughes}
24659fa7163774d6930a174bc038414a4b780581957Elliott Hughes
24759fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject makeStructStatFs(JNIEnv* env, const struct statfs& sb) {
24859fa7163774d6930a174bc038414a4b780581957Elliott Hughes    STATIC_ASSERT(sizeof(sb.f_bavail) == sizeof(jlong), statfs_not_64_bit);
24959fa7163774d6930a174bc038414a4b780581957Elliott Hughes    STATIC_ASSERT(sizeof(sb.f_bfree) == sizeof(jlong), statfs_not_64_bit);
25059fa7163774d6930a174bc038414a4b780581957Elliott Hughes    STATIC_ASSERT(sizeof(sb.f_blocks) == sizeof(jlong), statfs_not_64_bit);
25159fa7163774d6930a174bc038414a4b780581957Elliott Hughes
25259fa7163774d6930a174bc038414a4b780581957Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structStatFsClass, "<init>",
25359fa7163774d6930a174bc038414a4b780581957Elliott Hughes            "(JJJJJJJJ)V");
25459fa7163774d6930a174bc038414a4b780581957Elliott Hughes    return env->NewObject(JniConstants::structStatFsClass, ctor, static_cast<jlong>(sb.f_bsize),
25559fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_blocks), static_cast<jlong>(sb.f_bfree),
25659fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_bavail), static_cast<jlong>(sb.f_files),
25759fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_ffree), static_cast<jlong>(sb.f_namelen),
25859fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_frsize));
25947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
26047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
2610a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructLinger(JNIEnv* env, const struct linger& l) {
2620a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structLingerClass, "<init>", "(II)V");
2630a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return env->NewObject(JniConstants::structLingerClass, ctor, l.l_onoff, l.l_linger);
2640a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
2650a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
2660a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructTimeval(JNIEnv* env, const struct timeval& tv) {
2670a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structTimevalClass, "<init>", "(JJ)V");
2680a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return env->NewObject(JniConstants::structTimevalClass, ctor,
2690a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes            static_cast<jlong>(tv.tv_sec), static_cast<jlong>(tv.tv_usec));
2700a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
2710a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
2727341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject makeStructUtsname(JNIEnv* env, const struct utsname& buf) {
2737341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#define TO_JAVA_STRING(NAME) \
2747341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes        jstring NAME = env->NewStringUTF(buf. NAME); \
2757341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes        if (NAME == NULL) return NULL;
2767341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
2777341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(sysname);
2787341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(nodename);
2797341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(release);
2807341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(version);
2817341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(machine);
2827341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#undef TO_JAVA_STRING
2837341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
2847341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structUtsnameClass, "<init>",
2857341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes            "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
2867341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    return env->NewObject(JniConstants::structUtsnameClass, ctor,
2877341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes            sysname, nodename, release, version, machine);
2887341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes};
2897341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
290a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic bool fillIfreq(JNIEnv* env, jstring javaInterfaceName, struct ifreq& req) {
291a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    ScopedUtfChars interfaceName(env, javaInterfaceName);
292a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    if (interfaceName.c_str() == NULL) {
293a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes        return false;
294a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    }
295a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    memset(&req, 0, sizeof(req));
296a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    strncpy(req.ifr_name, interfaceName.c_str(), sizeof(req.ifr_name));
297a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    req.ifr_name[sizeof(req.ifr_name) - 1] = '\0';
298a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    return true;
299a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes}
300a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes
30147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject doStat(JNIEnv* env, jstring javaPath, bool isLstat) {
30247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    ScopedUtfChars path(env, javaPath);
30347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    if (path.c_str() == NULL) {
30447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
30547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
30647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    struct stat sb;
30747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = isLstat ? TEMP_FAILURE_RETRY(lstat(path.c_str(), &sb))
30847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes                     : TEMP_FAILURE_RETRY(stat(path.c_str(), &sb));
309dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
3107e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, isLstat ? "lstat" : "stat");
31147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
31247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
31347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return makeStructStat(env, sb);
314ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
315ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
316ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jboolean Posix_access(JNIEnv* env, jobject, jstring javaPath, jint mode) {
317ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    ScopedUtfChars path(env, javaPath);
318ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (path.c_str() == NULL) {
319ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        return JNI_FALSE;
320ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
32147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = TEMP_FAILURE_RETRY(access(path.c_str(), mode));
322dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
3237e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "access");
324dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    }
325ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return (rc == 0);
326ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
327ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
328da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughesstatic void Posix_bind(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) {
329da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes    sockaddr_storage ss;
330da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes    if (!inetAddressToSockaddr(env, javaAddress, port, &ss)) {
331da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes        return;
332da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes    }
333996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    int fd;
334da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes    const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss);
335996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    NET_FAILURE_RETRY("bind", bind(fd, sa, sizeof(sockaddr_storage)));
336da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes}
337da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes
338b7190190e0ef8de883c952efb319ce7748831faaElliott Hughesstatic void Posix_chmod(JNIEnv* env, jobject, jstring javaPath, jint mode) {
339b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    ScopedUtfChars path(env, javaPath);
340b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    if (path.c_str() == NULL) {
341b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes        return;
342b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    }
343b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    throwIfMinusOne(env, "chmod", TEMP_FAILURE_RETRY(chmod(path.c_str(), mode)));
344b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes}
345b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes
346462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughesstatic void Posix_close(JNIEnv* env, jobject, jobject javaFd) {
347462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // Get the FileDescriptor's 'fd' field and clear it.
348462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // We need to do this before we can throw an IOException (http://b/3222087).
349462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
350462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    jniSetFileDescriptorOfFD(env, javaFd, -1);
351462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes
352462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // Even if close(2) fails with EINTR, the fd will have been closed.
353462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone else's fd.
354462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    // http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
355462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    throwIfMinusOne(env, "close", close(fd));
356462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes}
357462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes
358996bf79565ac88402920bd826d6f85952c83be20Elliott Hughesstatic void Posix_connect(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) {
359996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    sockaddr_storage ss;
360996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    if (!inetAddressToSockaddr(env, javaAddress, port, &ss)) {
361996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes        return;
362996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    }
363996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    int fd;
364996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss);
365996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    NET_FAILURE_RETRY("connect", connect(fd, sa, sizeof(sockaddr_storage)));
366996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes}
367996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes
368ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jobjectArray Posix_environ(JNIEnv* env, jobject) {
369ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    extern char** environ; // Standard, but not in any header file.
370ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return toStringArray(env, environ);
371ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
372ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
373fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlVoid(JNIEnv* env, jobject, jobject javaFd, jint cmd) {
374fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
375fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd)));
376fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes}
377fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
378fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlLong(JNIEnv* env, jobject, jobject javaFd, jint cmd, jlong arg) {
379fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
380fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, arg)));
381fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes}
382fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
383fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlFlock(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaFlock) {
384fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID typeFid = env->GetFieldID(JniConstants::structFlockClass, "l_type", "S");
385fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID whenceFid = env->GetFieldID(JniConstants::structFlockClass, "l_whence", "S");
386fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID startFid = env->GetFieldID(JniConstants::structFlockClass, "l_start", "J");
387fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID lenFid = env->GetFieldID(JniConstants::structFlockClass, "l_len", "J");
388fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID pidFid = env->GetFieldID(JniConstants::structFlockClass, "l_pid", "I");
389fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
390fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    struct flock64 lock;
391fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    memset(&lock, 0, sizeof(lock));
392fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_type = env->GetShortField(javaFlock, typeFid);
393fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_whence = env->GetShortField(javaFlock, whenceFid);
394fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_start = env->GetLongField(javaFlock, startFid);
395fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_len = env->GetLongField(javaFlock, lenFid);
396fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_pid = env->GetIntField(javaFlock, pidFid);
397fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
398fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
399fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int rc = throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, &lock)));
400fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    if (rc != -1) {
401fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetShortField(javaFlock, typeFid, lock.l_type);
402fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetShortField(javaFlock, whenceFid, lock.l_whence);
403fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetLongField(javaFlock, startFid, lock.l_start);
404fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetLongField(javaFlock, lenFid, lock.l_len);
405fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetIntField(javaFlock, pidFid, lock.l_pid);
406fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    }
407fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    return rc;
408fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes}
409fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
41052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fdatasync(JNIEnv* env, jobject, jobject javaFd) {
41152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4127e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "fdatasync", TEMP_FAILURE_RETRY(fdatasync(fd)));
41352724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes}
41452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes
41547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_fstat(JNIEnv* env, jobject, jobject javaFd) {
41647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
41747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    struct stat sb;
41847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = TEMP_FAILURE_RETRY(fstat(fd, &sb));
419dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
4207e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "fstat");
42147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
42247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
42347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return makeStructStat(env, sb);
42447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
42547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
42659fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject Posix_fstatfs(JNIEnv* env, jobject, jobject javaFd) {
42759fa7163774d6930a174bc038414a4b780581957Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
42859fa7163774d6930a174bc038414a4b780581957Elliott Hughes    struct statfs sb;
42959fa7163774d6930a174bc038414a4b780581957Elliott Hughes    int rc = TEMP_FAILURE_RETRY(fstatfs(fd, &sb));
43059fa7163774d6930a174bc038414a4b780581957Elliott Hughes    if (rc == -1) {
43159fa7163774d6930a174bc038414a4b780581957Elliott Hughes        throwErrnoException(env, "fstatfs");
43259fa7163774d6930a174bc038414a4b780581957Elliott Hughes        return NULL;
43359fa7163774d6930a174bc038414a4b780581957Elliott Hughes    }
43459fa7163774d6930a174bc038414a4b780581957Elliott Hughes    return makeStructStatFs(env, sb);
43559fa7163774d6930a174bc038414a4b780581957Elliott Hughes}
43659fa7163774d6930a174bc038414a4b780581957Elliott Hughes
43752724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fsync(JNIEnv* env, jobject, jobject javaFd) {
43852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4397e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "fsync", TEMP_FAILURE_RETRY(fsync(fd)));
440f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes}
441f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes
442f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughesstatic void Posix_ftruncate(JNIEnv* env, jobject, jobject javaFd, jlong length) {
443f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
4447e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "ftruncate", TEMP_FAILURE_RETRY(ftruncate64(fd, length)));
44552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes}
44652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes
4474f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic jstring Posix_gai_strerror(JNIEnv* env, jobject, jint error) {
4484f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    return env->NewStringUTF(gai_strerror(error));
4494f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes}
4504f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes
4511c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughesstatic jobjectArray Posix_getaddrinfo(JNIEnv* env, jobject, jstring javaNode, jobject javaHints) {
4521c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    ScopedUtfChars node(env, javaNode);
4531c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    if (node.c_str() == NULL) {
4541c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return NULL;
4551c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
4561c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
4571c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    static jfieldID flagsFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_flags", "I");
4581c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    static jfieldID familyFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_family", "I");
4591c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    static jfieldID socktypeFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_socktype", "I");
4601c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    static jfieldID protocolFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_protocol", "I");
4611c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
4621c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    addrinfo hints;
4631c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    memset(&hints, 0, sizeof(hints));
4641c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    hints.ai_flags = env->GetIntField(javaHints, flagsFid);
4651c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    hints.ai_family = env->GetIntField(javaHints, familyFid);
4661c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    hints.ai_socktype = env->GetIntField(javaHints, socktypeFid);
4671c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    hints.ai_protocol = env->GetIntField(javaHints, protocolFid);
4681c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
4691c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    addrinfo* addressList = NULL;
4701c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    int rc = getaddrinfo(node.c_str(), NULL, &hints, &addressList);
4711c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    UniquePtr<addrinfo, addrinfo_deleter> addressListDeleter(addressList);
4721c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    if (rc != 0) {
4731c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        throwGaiException(env, "getaddrinfo", rc);
4741c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return NULL;
4751c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
4761c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
4771c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    // Count results so we know how to size the output array.
4781c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    int addressCount = 0;
4791c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    for (addrinfo* ai = addressList; ai != NULL; ai = ai->ai_next) {
4801c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) {
4811c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            ++addressCount;
4821c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        } else {
4831c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            LOGE("getaddrinfo unexpected ai_family %i", ai->ai_family);
4841c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        }
4851c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
4861c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    if (addressCount == 0) {
4871c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return NULL;
4881c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
4891c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
4901c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    // Prepare output array.
4911c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    jobjectArray result = env->NewObjectArray(addressCount, JniConstants::inetAddressClass, NULL);
4921c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    if (result == NULL) {
4931c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return NULL;
4941c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
4951c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
4961c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    // Examine returned addresses one by one, save them in the output array.
4971c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    int index = 0;
4981c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    for (addrinfo* ai = addressList; ai != NULL; ai = ai->ai_next) {
4991c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) {
5001c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            // Unknown address family. Skip this address.
5011c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            LOGE("getaddrinfo unexpected ai_family %i", ai->ai_family);
5021c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            continue;
5031c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        }
5041c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
5051c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        // Convert each IP address into a Java byte array.
5061c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        sockaddr_storage* address = reinterpret_cast<sockaddr_storage*>(ai->ai_addr);
5071c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        ScopedLocalRef<jobject> inetAddress(env, socketAddressToInetAddress(env, address));
5081c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        if (inetAddress.get() == NULL) {
5091c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes            return NULL;
5101c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        }
5111c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        env->SetObjectArrayElement(result, index, inetAddress.get());
5121c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        ++index;
5131c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
5141c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    return result;
5151c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes}
5161c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
517ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic jstring Posix_getenv(JNIEnv* env, jobject, jstring javaName) {
518ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    ScopedUtfChars name(env, javaName);
519ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    if (name.c_str() == NULL) {
520ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes        return NULL;
521ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    }
522ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    return env->NewStringUTF(getenv(name.c_str()));
523ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes}
524ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
5254f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic jstring Posix_getnameinfo(JNIEnv* env, jobject, jobject javaAddress, jint flags) {
5264f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    sockaddr_storage ss;
527da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes    if (!inetAddressToSockaddr_getnameinfo(env, javaAddress, 0, &ss)) {
5284f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        return NULL;
5294f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    }
5304f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    // TODO: bionic's getnameinfo(3) seems to want its length parameter to be exactly
5314f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    // sizeof(sockaddr_in) for an IPv4 address and sizeof (sockaddr_in6) for an
5324f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    // IPv6 address. Fix getnameinfo so it accepts sizeof(sockaddr_storage), and
5334f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    // then remove this hack.
5344f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    socklen_t size = (ss.ss_family == AF_INET) ? sizeof(sockaddr_in) : sizeof(sockaddr_in6);
5354f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    char buf[NI_MAXHOST]; // NI_MAXHOST is longer than INET6_ADDRSTRLEN.
5364f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    int rc = getnameinfo(reinterpret_cast<sockaddr*>(&ss), size, buf, sizeof(buf), NULL, 0, flags);
5374f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    if (rc != 0) {
5384f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        throwGaiException(env, "getnameinfo", rc);
5394f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes        return NULL;
5404f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    }
5414f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    return env->NewStringUTF(buf);
5424f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes}
5434f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes
5440a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockname(JNIEnv* env, jobject, jobject javaFd) {
5450a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
5460a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr_storage ss;
5470a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr* sa = reinterpret_cast<sockaddr*>(&ss);
5480a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t byteCount = sizeof(ss);
5490a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&ss, 0, byteCount);
5500a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockname(fd, sa, &byteCount));
5510a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
5520a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockname");
5530a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
5540a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
5550a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return makeSocketAddress(env, &ss);
5560a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
5570a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
5580a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
5590a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
5600a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    u_char result = 0;
5610a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(result);
5620a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size)));
5630a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return result;
5640a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
5650a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
5660a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptInAddr(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
5670a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
5680a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr_storage ss;
5690a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&ss, 0, sizeof(ss));
5700a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    ss.ss_family = AF_INET; // This is only for the IPv4-only IP_MULTICAST_IF.
5710a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    sockaddr_in* sa = reinterpret_cast<sockaddr_in*>(&ss);
5720a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(sa->sin_addr);
5730a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &sa->sin_addr, &size));
5740a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
5750a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockopt");
5760a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
5770a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
5780a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return socketAddressToInetAddress(env, &ss);
5790a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
5800a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
5810a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
5820a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
5830a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    jint result = 0;
5840a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(result);
5850a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size)));
5860a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return result;
5870a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
5880a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
5890a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
5900a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
5910a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    struct linger l;
5920a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(l);
5930a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&l, 0, size);
5940a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &l, &size));
5950a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
5960a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockopt");
5970a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
5980a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
5990a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return makeStructLinger(env, l);
6000a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
6010a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
6020a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) {
6030a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
6040a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    struct timeval tv;
6050a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    socklen_t size = sizeof(tv);
6060a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    memset(&tv, 0, size);
6070a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &tv, &size));
6080a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    if (rc == -1) {
6090a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        throwErrnoException(env, "getsockopt");
6100a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes        return NULL;
6110a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    }
6120a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    return makeStructTimeval(env, tv);
6130a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes}
6140a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes
615a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic jstring Posix_if_indextoname(JNIEnv* env, jobject, jint index) {
616a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    char buf[IF_NAMESIZE];
617a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    char* name = if_indextoname(index, buf);
618a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    // if_indextoname(3) returns NULL on failure, which will come out of NewStringUTF unscathed.
619a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    // There's no useful information in errno, so we don't bother throwing. Callers can null-check.
620a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    return env->NewStringUTF(name);
621a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes}
622a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes
6231c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughesstatic jobject Posix_inet_aton(JNIEnv* env, jobject, jstring javaName) {
6241c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    ScopedUtfChars name(env, javaName);
6251c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    if (name.c_str() == NULL) {
6261c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return NULL;
6271c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
6281c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    sockaddr_storage ss;
6291c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    memset(&ss, 0, sizeof(ss));
6301c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(&ss);
6311c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    if (inet_aton(name.c_str(), &sin->sin_addr) == 0) {
6321c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        return NULL;
6331c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    }
6341c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    sin->sin_family = AF_INET; // inet_aton only supports IPv4.
6351c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    return socketAddressToInetAddress(env, &ss);
6361c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes}
6371c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes
638a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic jobject Posix_ioctlInetAddress(JNIEnv* env, jobject, jobject javaFd, jint cmd, jstring javaInterfaceName) {
639a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    struct ifreq req;
640a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    if (!fillIfreq(env, javaInterfaceName, req)) {
641a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes        return NULL;
642a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    }
643a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
644a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &req)));
645a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    if (rc == -1) {
646a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes        return NULL;
647a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    }
648a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    return socketAddressToInetAddress(env, reinterpret_cast<sockaddr_storage*>(&req.ifr_addr));
649a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes}
650a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes
6518b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jint Posix_ioctlInt(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaArg) {
6528b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    // This is complicated because ioctls may return their result by updating their argument
6538b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    // or via their return value, so we need to support both.
654461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
6558b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    static jfieldID valueFid = env->GetFieldID(JniConstants::mutableIntClass, "value", "I");
6568b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    jint arg = env->GetIntField(javaArg, valueFid);
6578b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &arg)));
6588b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    if (!env->ExceptionCheck()) {
6598b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        env->SetIntField(javaArg, valueFid, arg);
6608b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    }
6618b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    return rc;
662461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes}
663461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes
6649a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughesstatic jboolean Posix_isatty(JNIEnv* env, jobject, jobject javaFd) {
6659a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
6669a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    return TEMP_FAILURE_RETRY(isatty(fd)) == 0;
6679a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes}
6689a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes
669a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughesstatic void Posix_kill(JNIEnv* env, jobject, jint pid, jint sig) {
670a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes    throwIfMinusOne(env, "kill", TEMP_FAILURE_RETRY(kill(pid, sig)));
671a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes}
672a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes
673e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughesstatic void Posix_listen(JNIEnv* env, jobject, jobject javaFd, jint backlog) {
674e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
675e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes    throwIfMinusOne(env, "listen", TEMP_FAILURE_RETRY(listen(fd, backlog)));
676e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes}
677e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes
678dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughesstatic jlong Posix_lseek(JNIEnv* env, jobject, jobject javaFd, jlong offset, jint whence) {
679dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
6807e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    return throwIfMinusOne(env, "lseek", TEMP_FAILURE_RETRY(lseek64(fd, offset, whence)));
681dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes}
682dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes
68347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_lstat(JNIEnv* env, jobject, jstring javaPath) {
68447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return doStat(env, javaPath, true);
68547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
68647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
6870f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughesstatic void Posix_mincore(JNIEnv* env, jobject, jlong address, jlong byteCount, jbyteArray javaVector) {
6880f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    ScopedByteArrayRW vector(env, javaVector);
6890f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    if (vector.get() == NULL) {
6900f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes        return;
6910f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    }
6920f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
6930f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    unsigned char* vec = reinterpret_cast<unsigned char*>(vector.get());
6940f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    throwIfMinusOne(env, "mincore", TEMP_FAILURE_RETRY(mincore(ptr, byteCount, vec)));
6950f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes}
6960f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes
697c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_mkdir(JNIEnv* env, jobject, jstring javaPath, jint mode) {
698c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    ScopedUtfChars path(env, javaPath);
699c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    if (path.c_str() == NULL) {
700c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes        return;
701c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    }
702c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    throwIfMinusOne(env, "mkdir", TEMP_FAILURE_RETRY(mkdir(path.c_str(), mode)));
703c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes}
704c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes
7057e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_mlock(JNIEnv* env, jobject, jlong address, jlong byteCount) {
7067e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
7077e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    throwIfMinusOne(env, "mlock", TEMP_FAILURE_RETRY(mlock(ptr, byteCount)));
7087e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes}
7097e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes
7107e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic jlong Posix_mmap(JNIEnv* env, jobject, jlong address, jlong byteCount, jint prot, jint flags, jobject javaFd, jlong offset) {
7117e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
7127e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* suggestedPtr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
7137e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = mmap(suggestedPtr, byteCount, prot, flags, fd, offset);
7147e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    if (ptr == MAP_FAILED) {
7157e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "mmap");
7167e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    }
7177e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    return static_cast<jlong>(reinterpret_cast<uintptr_t>(ptr));
7187e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
7197e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
7207e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_msync(JNIEnv* env, jobject, jlong address, jlong byteCount, jint flags) {
7217e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
7227e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "msync", TEMP_FAILURE_RETRY(msync(ptr, byteCount, flags)));
7237e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
7247e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
7257e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_munlock(JNIEnv* env, jobject, jlong address, jlong byteCount) {
7267e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
7277e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    throwIfMinusOne(env, "munlock", TEMP_FAILURE_RETRY(munlock(ptr, byteCount)));
7287e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes}
7297e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes
7307e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_munmap(JNIEnv* env, jobject, jlong address, jlong byteCount) {
7317e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
7327e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "munmap", TEMP_FAILURE_RETRY(munmap(ptr, byteCount)));
7337e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
7347e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
7350ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughesstatic jobject Posix_open(JNIEnv* env, jobject, jstring javaPath, jint flags, jint mode) {
7360ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    ScopedUtfChars path(env, javaPath);
7370ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    if (path.c_str() == NULL) {
7380ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes        return NULL;
7390ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    }
7400ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    int fd = throwIfMinusOne(env, "open", TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode)));
7410ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL;
7420ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes}
7430ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes
74441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughesstatic jobjectArray Posix_pipe(JNIEnv* env, jobject) {
74541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    int fds[2];
74641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    throwIfMinusOne(env, "pipe", TEMP_FAILURE_RETRY(pipe(&fds[0])));
74741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    jobjectArray result = env->NewObjectArray(2, JniConstants::fileDescriptorClass, NULL);
74841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    if (result == NULL) {
74941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        return NULL;
75041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    }
75141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    for (int i = 0; i < 2; ++i) {
75241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        ScopedLocalRef<jobject> fd(env, jniCreateFileDescriptor(env, fds[i]));
75341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        if (fd.get() == NULL) {
75441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes            return NULL;
75541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        }
75641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        env->SetObjectArrayElement(result, i, fd.get());
75741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        if (env->ExceptionCheck()) {
75841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes            return NULL;
75941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        }
76041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    }
76141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    return result;
76241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes}
76341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes
76470c820401677ca251ad09ac64cc23c760764e75dElliott Hughesstatic jint Posix_poll(JNIEnv* env, jobject, jobjectArray javaStructs, jint timeoutMs) {
76570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    static jfieldID fdFid = env->GetFieldID(JniConstants::structPollfdClass, "fd", "Ljava/io/FileDescriptor;");
76670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    static jfieldID eventsFid = env->GetFieldID(JniConstants::structPollfdClass, "events", "S");
76770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    static jfieldID reventsFid = env->GetFieldID(JniConstants::structPollfdClass, "revents", "S");
76870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes
76970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    // Turn the Java libcore.io.StructPollfd[] into a C++ struct pollfd[].
77070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    size_t arrayLength = env->GetArrayLength(javaStructs);
77170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    UniquePtr<struct pollfd[]> fds(new struct pollfd[arrayLength]);
77270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    memset(fds.get(), 0, sizeof(struct pollfd) * arrayLength);
77370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    size_t count = 0; // Some trailing array elements may be irrelevant. (See below.)
77470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    for (size_t i = 0; i < arrayLength; ++i) {
77570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        ScopedLocalRef<jobject> javaStruct(env, env->GetObjectArrayElement(javaStructs, i));
77670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        if (javaStruct.get() == NULL) {
77770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes            break; // We allow trailing nulls in the array for caller convenience.
77870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        }
77970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        ScopedLocalRef<jobject> javaFd(env, env->GetObjectField(javaStruct.get(), fdFid));
78070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        if (javaFd.get() == NULL) {
78170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes            break; // We also allow callers to just clear the fd field (this is what Selector does).
78270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        }
78370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        fds[count].fd = jniGetFDFromFileDescriptor(env, javaFd.get());
78470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        fds[count].events = env->GetShortField(javaStruct.get(), eventsFid);
78570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        ++count;
78670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    }
78770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes
78870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    int rc = TEMP_FAILURE_RETRY(poll(fds.get(), count, timeoutMs));
78970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    if (rc == -1) {
79070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        throwErrnoException(env, "poll");
79170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        return -1;
79270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    }
79370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes
79470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    // Update the revents fields in the Java libcore.io.StructPollfd[].
79570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    for (size_t i = 0; i < arrayLength; ++i) {
79670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        ScopedLocalRef<jobject> javaStruct(env, env->GetObjectArrayElement(javaStructs, i));
79770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes        env->SetShortField(javaStruct.get(), reventsFid, fds[i].revents);
79870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    }
79970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    return rc;
80070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes}
80170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes
8020568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughesstatic jint Posix_readBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount) {
8030568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes    ScopedBytesRW bytes(env, javaBytes);
80426c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    if (bytes.get() == NULL) {
80526c7025a7a919044771fb89031161bd26fe03032Elliott Hughes        return -1;
80626c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    }
80726c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
8080568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes    return throwIfMinusOne(env, "read", TEMP_FAILURE_RETRY(read(fd, bytes.get() + byteOffset, byteCount)));
80926c7025a7a919044771fb89031161bd26fe03032Elliott Hughes}
81026c7025a7a919044771fb89031161bd26fe03032Elliott Hughes
811bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_readv(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) {
8128dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes    IoVec<ScopedBytesRW> ioVec(env, env->GetArrayLength(buffers));
813bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    if (!ioVec.init(buffers, offsets, byteCounts)) {
814bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return -1;
815bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
816bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
817bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    return throwIfMinusOne(env, "readv", TEMP_FAILURE_RETRY(readv(fd, ioVec.get(), ioVec.size())));
818bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes}
819bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
820c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_remove(JNIEnv* env, jobject, jstring javaPath) {
821c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    ScopedUtfChars path(env, javaPath);
822c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    if (path.c_str() == NULL) {
823c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes        return;
824c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    }
825c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    throwIfMinusOne(env, "remove", TEMP_FAILURE_RETRY(remove(path.c_str())));
826c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes}
827c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes
828a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_rename(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) {
829a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars oldPath(env, javaOldPath);
830a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (oldPath.c_str() == NULL) {
831a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
832a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
833a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars newPath(env, javaNewPath);
834a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (newPath.c_str() == NULL) {
835a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
836a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
837a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    throwIfMinusOne(env, "rename", TEMP_FAILURE_RETRY(rename(oldPath.c_str(), newPath.c_str())));
838a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes}
839a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes
8408b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jlong Posix_sendfile(JNIEnv* env, jobject, jobject javaOutFd, jobject javaInFd, jobject javaOffset, jlong byteCount) {
8418b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    int outFd = jniGetFDFromFileDescriptor(env, javaOutFd);
8428b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    int inFd = jniGetFDFromFileDescriptor(env, javaInFd);
8438b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    static jfieldID valueFid = env->GetFieldID(JniConstants::mutableLongClass, "value", "J");
8448b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    off_t offset = 0;
8458b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    off_t* offsetPtr = NULL;
8468b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    if (javaOffset != NULL) {
8478b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        // TODO: fix bionic so we can have a 64-bit off_t!
8488b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        offset = env->GetLongField(javaOffset, valueFid);
8498b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        offsetPtr = &offset;
8508b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    }
8518b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    jlong result = throwIfMinusOne(env, "sendfile", TEMP_FAILURE_RETRY(sendfile(outFd, inFd, offsetPtr, byteCount)));
8528b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    if (javaOffset != NULL) {
8538b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        env->SetLongField(javaOffset, valueFid, offset);
8548b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    }
8558b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    return result;
8568b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes}
8578b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes
858c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) {
859c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
860c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    u_char byte = value;
861c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &byte, sizeof(byte))));
862c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes}
863c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes
864a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic void Posix_setsockoptIfreq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jstring javaInterfaceName) {
865a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    struct ifreq req;
866a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    if (!fillIfreq(env, javaInterfaceName, req)) {
867b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes        return;
868b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    }
869b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
870b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req))));
871b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes}
872b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes
873454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic void Posix_setsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) {
874454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
875454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))));
876454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes}
877454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes
878b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughesstatic void Posix_setsockoptIpMreqn(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) {
879b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    ip_mreqn req;
880b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    memset(&req, 0, sizeof(req));
881b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    req.imr_ifindex = value;
882b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
883b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req))));
884b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes}
885b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes
886438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughesstatic void Posix_setsockoptGroupReq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaGroupReq) {
887438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    struct group_req value;
888438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes
889438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    static jfieldID grInterfaceFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_interface", "I");
890438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    value.gr_interface = env->GetIntField(javaGroupReq, grInterfaceFid);
891438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    // Get the IPv4 or IPv6 multicast address to join or leave.
892438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    static jfieldID grGroupFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_group", "Ljava/net/InetAddress;");
893e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes    ScopedLocalRef<jobject> javaGroup(env, env->GetObjectField(javaGroupReq, grGroupFid));
894e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes    if (!inetAddressToSockaddr(env, javaGroup.get(), 0, &value.gr_group)) {
895438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        return;
896438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    }
897438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes
898438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
899438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    int rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)));
900438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    if (rc == -1 && errno == EINVAL) {
901438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        // Maybe we're a 32-bit binary talking to a 64-bit kernel?
902438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        // glibc doesn't automatically handle this.
903438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        struct GCC_HIDDEN group_req64 {
904438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes            uint32_t gr_interface;
905438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes            uint32_t my_padding;
906438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes            sockaddr_storage gr_group;
907438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        };
908438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        group_req64 value64;
909438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        value64.gr_interface = value.gr_interface;
910438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        memcpy(&value64.gr_group, &value.gr_group, sizeof(value.gr_group));
911438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes        rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value64, sizeof(value64)));
912438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    }
913438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    throwIfMinusOne(env, "setsockopt", rc);
914438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes}
915438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes
916c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaLinger) {
917c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID lOnoffFid = env->GetFieldID(JniConstants::structLingerClass, "l_onoff", "I");
918c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID lLingerFid = env->GetFieldID(JniConstants::structLingerClass, "l_linger", "I");
919c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
920c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    struct linger value;
921c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    value.l_onoff = env->GetIntField(javaLinger, lOnoffFid);
922c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    value.l_linger = env->GetIntField(javaLinger, lLingerFid);
923c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))));
924c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes}
925c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes
926c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaTimeval) {
927c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID tvSecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_sec", "J");
928c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    static jfieldID tvUsecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_usec", "J");
929c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
930c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    struct timeval value;
931c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    value.tv_sec = env->GetIntField(javaTimeval, tvSecFid);
932c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    value.tv_usec = env->GetIntField(javaTimeval, tvUsecFid);
933c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))));
934c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes}
935c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes
93659e4744d27231f260271dbbca406e0cc39768116Elliott Hughesstatic void Posix_shutdown(JNIEnv* env, jobject, jobject javaFd, jint how) {
93759e4744d27231f260271dbbca406e0cc39768116Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
93859e4744d27231f260271dbbca406e0cc39768116Elliott Hughes    throwIfMinusOne(env, "shutdown", TEMP_FAILURE_RETRY(shutdown(fd, how)));
93959e4744d27231f260271dbbca406e0cc39768116Elliott Hughes}
94059e4744d27231f260271dbbca406e0cc39768116Elliott Hughes
941454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic jobject Posix_socket(JNIEnv* env, jobject, jint domain, jint type, jint protocol) {
942454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    int fd = throwIfMinusOne(env, "socket", TEMP_FAILURE_RETRY(socket(domain, type, protocol)));
943454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL;
944454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes}
945454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes
94647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_stat(JNIEnv* env, jobject, jstring javaPath) {
94747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return doStat(env, javaPath, false);
94847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
94947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
9500ade5524d972b9dfd69b0126aa4c35319c1b7fa4Elliott Hughesstatic jobject Posix_statfs(JNIEnv* env, jobject, jstring javaPath) {
95159fa7163774d6930a174bc038414a4b780581957Elliott Hughes    ScopedUtfChars path(env, javaPath);
95259fa7163774d6930a174bc038414a4b780581957Elliott Hughes    if (path.c_str() == NULL) {
95359fa7163774d6930a174bc038414a4b780581957Elliott Hughes        return NULL;
95459fa7163774d6930a174bc038414a4b780581957Elliott Hughes    }
95559fa7163774d6930a174bc038414a4b780581957Elliott Hughes    struct statfs sb;
95659fa7163774d6930a174bc038414a4b780581957Elliott Hughes    int rc = TEMP_FAILURE_RETRY(statfs(path.c_str(), &sb));
95759fa7163774d6930a174bc038414a4b780581957Elliott Hughes    if (rc == -1) {
95859fa7163774d6930a174bc038414a4b780581957Elliott Hughes        throwErrnoException(env, "statfs");
95959fa7163774d6930a174bc038414a4b780581957Elliott Hughes        return NULL;
96059fa7163774d6930a174bc038414a4b780581957Elliott Hughes    }
96159fa7163774d6930a174bc038414a4b780581957Elliott Hughes    return makeStructStatFs(env, sb);
96259fa7163774d6930a174bc038414a4b780581957Elliott Hughes}
96359fa7163774d6930a174bc038414a4b780581957Elliott Hughes
964ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jstring Posix_strerror(JNIEnv* env, jobject, jint errnum) {
96552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    char buffer[BUFSIZ];
966ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    const char* message = jniStrError(errnum, buffer, sizeof(buffer));
967ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return env->NewStringUTF(message);
968ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
969ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
970a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_symlink(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) {
971a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars oldPath(env, javaOldPath);
972a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (oldPath.c_str() == NULL) {
973a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
974a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
975a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars newPath(env, javaNewPath);
976a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (newPath.c_str() == NULL) {
977a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
978a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
979a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    throwIfMinusOne(env, "symlink", TEMP_FAILURE_RETRY(symlink(oldPath.c_str(), newPath.c_str())));
980a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes}
981a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes
9826fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughesstatic jlong Posix_sysconf(JNIEnv* env, jobject, jint name) {
9836fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    // Since -1 is a valid result from sysconf(3), detecting failure is a little more awkward.
9846fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    errno = 0;
9856fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    long result = sysconf(name);
9866fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    if (result == -1L && errno == EINVAL) {
9877e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "sysconf");
9886fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    }
9896fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    return result;
9906fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes}
9916fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes
9927341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject Posix_uname(JNIEnv* env, jobject) {
9937341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    struct utsname buf;
9947341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    if (throwIfMinusOne(env, "uname", TEMP_FAILURE_RETRY(uname(&buf))) == -1) {
9957341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes        return NULL;
9967341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    }
9977341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    return makeStructUtsname(env, buf);
9987341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes}
9997341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
10000568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughesstatic jint Posix_writeBytes(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount) {
10010568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes    ScopedBytesRO bytes(env, javaBytes);
100278c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    if (bytes.get() == NULL) {
100378c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes        return -1;
100478c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    }
100578c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
10060568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes    return throwIfMinusOne(env, "write", TEMP_FAILURE_RETRY(write(fd, bytes.get() + byteOffset, byteCount)));
100778c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes}
100878c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes
1009bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_writev(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) {
10108dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes    IoVec<ScopedBytesRO> ioVec(env, env->GetArrayLength(buffers));
1011bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    if (!ioVec.init(buffers, offsets, byteCounts)) {
1012bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return -1;
1013bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
1014bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
1015bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    return throwIfMinusOne(env, "writev", TEMP_FAILURE_RETRY(writev(fd, ioVec.get(), ioVec.size())));
1016bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes}
1017bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
1018ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic JNINativeMethod gMethods[] = {
1019ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, access, "(Ljava/lang/String;I)Z"),
1020da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes    NATIVE_METHOD(Posix, bind, "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)V"),
1021b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    NATIVE_METHOD(Posix, chmod, "(Ljava/lang/String;I)V"),
1022462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes    NATIVE_METHOD(Posix, close, "(Ljava/io/FileDescriptor;)V"),
1023996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    NATIVE_METHOD(Posix, connect, "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)V"),
1024ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, environ, "()[Ljava/lang/String;"),
1025fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    NATIVE_METHOD(Posix, fcntlVoid, "(Ljava/io/FileDescriptor;I)I"),
1026fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    NATIVE_METHOD(Posix, fcntlLong, "(Ljava/io/FileDescriptor;IJ)I"),
1027fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    NATIVE_METHOD(Posix, fcntlFlock, "(Ljava/io/FileDescriptor;ILlibcore/io/StructFlock;)I"),
102852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    NATIVE_METHOD(Posix, fdatasync, "(Ljava/io/FileDescriptor;)V"),
102947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, fstat, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStat;"),
103059fa7163774d6930a174bc038414a4b780581957Elliott Hughes    NATIVE_METHOD(Posix, fstatfs, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStatFs;"),
103152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    NATIVE_METHOD(Posix, fsync, "(Ljava/io/FileDescriptor;)V"),
1032f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    NATIVE_METHOD(Posix, ftruncate, "(Ljava/io/FileDescriptor;J)V"),
10334f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    NATIVE_METHOD(Posix, gai_strerror, "(I)Ljava/lang/String;"),
10341c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    NATIVE_METHOD(Posix, getaddrinfo, "(Ljava/lang/String;Llibcore/io/StructAddrinfo;)[Ljava/net/InetAddress;"),
1035ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    NATIVE_METHOD(Posix, getenv, "(Ljava/lang/String;)Ljava/lang/String;"),
10364f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes    NATIVE_METHOD(Posix, getnameinfo, "(Ljava/net/InetAddress;I)Ljava/lang/String;"),
10370a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockname, "(Ljava/io/FileDescriptor;)Ljava/net/SocketAddress;"),
10380a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptByte, "(Ljava/io/FileDescriptor;II)I"),
10390a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptInAddr, "(Ljava/io/FileDescriptor;II)Ljava/net/InetAddress;"),
10400a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptInt, "(Ljava/io/FileDescriptor;II)I"),
10410a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptLinger, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructLinger;"),
10420a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes    NATIVE_METHOD(Posix, getsockoptTimeval, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructTimeval;"),
1043a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    NATIVE_METHOD(Posix, if_indextoname, "(I)Ljava/lang/String;"),
10441c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes    NATIVE_METHOD(Posix, inet_aton, "(Ljava/lang/String;)Ljava/net/InetAddress;"),
1045a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes    NATIVE_METHOD(Posix, ioctlInetAddress, "(Ljava/io/FileDescriptor;ILjava/lang/String;)Ljava/net/InetAddress;"),
10468b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    NATIVE_METHOD(Posix, ioctlInt, "(Ljava/io/FileDescriptor;ILlibcore/util/MutableInt;)I"),
10479a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    NATIVE_METHOD(Posix, isatty, "(Ljava/io/FileDescriptor;)Z"),
1048a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes    NATIVE_METHOD(Posix, kill, "(II)V"),
1049e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes    NATIVE_METHOD(Posix, listen, "(Ljava/io/FileDescriptor;I)V"),
1050dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    NATIVE_METHOD(Posix, lseek, "(Ljava/io/FileDescriptor;JI)J"),
105147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, lstat, "(Ljava/lang/String;)Llibcore/io/StructStat;"),
10520f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    NATIVE_METHOD(Posix, mincore, "(JJ[B)V"),
1053c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    NATIVE_METHOD(Posix, mkdir, "(Ljava/lang/String;I)V"),
10547e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    NATIVE_METHOD(Posix, mlock, "(JJ)V"),
10557e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, mmap, "(JJIILjava/io/FileDescriptor;J)J"),
10567e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, msync, "(JJI)V"),
10577e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    NATIVE_METHOD(Posix, munlock, "(JJ)V"),
10587e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, munmap, "(JJ)V"),
10590ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    NATIVE_METHOD(Posix, open, "(Ljava/lang/String;II)Ljava/io/FileDescriptor;"),
106041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    NATIVE_METHOD(Posix, pipe, "()[Ljava/io/FileDescriptor;"),
106170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes    NATIVE_METHOD(Posix, poll, "([Llibcore/io/StructPollfd;I)I"),
10620568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes    NATIVE_METHOD(Posix, readBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"),
1063bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    NATIVE_METHOD(Posix, readv, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"),
1064c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    NATIVE_METHOD(Posix, remove, "(Ljava/lang/String;)V"),
1065a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    NATIVE_METHOD(Posix, rename, "(Ljava/lang/String;Ljava/lang/String;)V"),
10668b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    NATIVE_METHOD(Posix, sendfile, "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Llibcore/util/MutableLong;J)J"),
1067c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    NATIVE_METHOD(Posix, setsockoptByte, "(Ljava/io/FileDescriptor;III)V"),
1068b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    NATIVE_METHOD(Posix, setsockoptIfreq, "(Ljava/io/FileDescriptor;IILjava/lang/String;)V"),
1069454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    NATIVE_METHOD(Posix, setsockoptInt, "(Ljava/io/FileDescriptor;III)V"),
1070b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes    NATIVE_METHOD(Posix, setsockoptIpMreqn, "(Ljava/io/FileDescriptor;III)V"),
1071438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes    NATIVE_METHOD(Posix, setsockoptGroupReq, "(Ljava/io/FileDescriptor;IILlibcore/io/StructGroupReq;)V"),
1072c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    NATIVE_METHOD(Posix, setsockoptLinger, "(Ljava/io/FileDescriptor;IILlibcore/io/StructLinger;)V"),
1073c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes    NATIVE_METHOD(Posix, setsockoptTimeval, "(Ljava/io/FileDescriptor;IILlibcore/io/StructTimeval;)V"),
107459e4744d27231f260271dbbca406e0cc39768116Elliott Hughes    NATIVE_METHOD(Posix, shutdown, "(Ljava/io/FileDescriptor;I)V"),
1075454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    NATIVE_METHOD(Posix, socket, "(III)Ljava/io/FileDescriptor;"),
107647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, stat, "(Ljava/lang/String;)Llibcore/io/StructStat;"),
107759fa7163774d6930a174bc038414a4b780581957Elliott Hughes    NATIVE_METHOD(Posix, statfs, "(Ljava/lang/String;)Llibcore/io/StructStatFs;"),
1078ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, strerror, "(I)Ljava/lang/String;"),
1079a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    NATIVE_METHOD(Posix, symlink, "(Ljava/lang/String;Ljava/lang/String;)V"),
10806fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    NATIVE_METHOD(Posix, sysconf, "(I)J"),
10817341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    NATIVE_METHOD(Posix, uname, "()Llibcore/io/StructUtsname;"),
10820568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes    NATIVE_METHOD(Posix, writeBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"),
1083bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    NATIVE_METHOD(Posix, writev, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"),
1084ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes};
1085ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesint register_libcore_io_Posix(JNIEnv* env) {
1086ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    return jniRegisterNativeMethods(env, "libcore/io/Posix", gMethods, NELEM(gMethods));
1087ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes}
1088