libcore_io_Linux.cpp revision 454a95f6a28855aa3c88d168b15a45bf315efc99
1ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes/*
2ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * Copyright (C) 2011 The Android Open Source Project
3ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes *
4ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * you may not use this file except in compliance with the License.
6ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * You may obtain a copy of the License at
7ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes *
8ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes *
10ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * Unless required by applicable law or agreed to in writing, software
11ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * See the License for the specific language governing permissions and
14ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * limitations under the License.
15ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes */
16ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
17ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#define LOG_TAG "Posix"
18ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
19ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "JNIHelp.h"
20ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "JniConstants.h"
21ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include "JniException.h"
220f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes#include "ScopedPrimitiveArray.h"
23ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "ScopedUtfChars.h"
2459fa7163774d6930a174bc038414a4b780581957Elliott Hughes#include "StaticAssert.h"
25ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include "toStringArray.h"
26ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
27ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include <errno.h>
280ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <fcntl.h>
29ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include <stdlib.h>
30461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes#include <sys/ioctl.h>
317e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes#include <sys/mman.h>
328b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes#include <sys/sendfile.h>
3359e4744d27231f260271dbbca406e0cc39768116Elliott Hughes#include <sys/socket.h>
3447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes#include <sys/stat.h>
350ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <sys/types.h>
36bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes#include <sys/uio.h>
377341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#include <sys/utsname.h>
3859fa7163774d6930a174bc038414a4b780581957Elliott Hughes#include <sys/vfs.h> // Bionic doesn't have <sys/statvfs.h>
390ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <unistd.h>
40ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
417e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void throwErrnoException(JNIEnv* env, const char* name) {
427e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    int errnum = errno;
437e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
44ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    jthrowable cause = NULL;
45ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (env->ExceptionCheck()) {
46ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        cause = env->ExceptionOccurred();
47ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        env->ExceptionClear();
48ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
49ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
50f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    ScopedLocalRef<jstring> javaName(env, env->NewStringUTF(name));
51f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    if (javaName.get() == NULL) {
52f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        // Not really much we can do here. We're probably dead in the water,
53f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        // but let's try to stumble on...
54f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        env->ExceptionClear();
55f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    }
56f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes
57ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    jobject exception;
58ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (cause != NULL) {
59f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        static jmethodID ctor = env->GetMethodID(JniConstants::errnoExceptionClass, "<init>",
60f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes                "(Ljava/lang/String;ILjava/lang/Throwable;)V");
61f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        exception = env->NewObject(JniConstants::errnoExceptionClass, ctor,
62f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes                javaName.get(), errnum, cause);
63ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    } else {
64f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        static jmethodID ctor = env->GetMethodID(JniConstants::errnoExceptionClass, "<init>",
65f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes                "(Ljava/lang/String;I)V");
66f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        exception = env->NewObject(JniConstants::errnoExceptionClass, ctor, javaName.get(), errnum);
67ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
68ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    env->Throw(reinterpret_cast<jthrowable>(exception));
69dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes}
70dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes
71dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughestemplate <typename rc_t>
727e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic rc_t throwIfMinusOne(JNIEnv* env, const char* name, rc_t rc) {
73dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == rc_t(-1)) {
747e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, name);
75dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    }
76dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    return rc;
7747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
7847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
79bbac92e691de7d570928ddfba639067978e55b06Elliott Hughestemplate <typename T>
80bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesclass IoVec {
81bbac92e691de7d570928ddfba639067978e55b06Elliott Hughespublic:
82bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    IoVec(JNIEnv* env, size_t bufferCount) : mEnv(env), mBufferCount(bufferCount) {
83bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
84bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
85bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    bool init(jobjectArray javaBuffers, jintArray javaOffsets, jintArray javaByteCounts) {
86bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        // We can't delete our local references until after the I/O, so make sure we have room.
87bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (mEnv->PushLocalFrame(mBufferCount + 16) < 0) {
88bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            return false;
89bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
90bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        ScopedIntArrayRO offsets(mEnv, javaOffsets);
91bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (offsets.get() == NULL) {
92bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            return false;
93bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
94bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        ScopedIntArrayRO byteCounts(mEnv, javaByteCounts);
95bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (byteCounts.get() == NULL) {
96bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            return false;
97bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
98bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        // TODO: Linux actually has a 1024 buffer limit. glibc works around this, and we should too.
99bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        for (size_t i = 0; i < mBufferCount; ++i) {
100bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            jobject buffer = mEnv->GetObjectArrayElement(javaBuffers, i);
101bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            jbyte* ptr;
102bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            if (mEnv->IsInstanceOf(buffer, JniConstants::byteArrayClass)) {
103bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                // We need to pin the array for the duration.
104bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                jbyteArray byteArray = reinterpret_cast<jbyteArray>(buffer);
105bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                mScopedByteArrays.push_back(new T(mEnv, byteArray));
106bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                ptr = const_cast<jbyte*>(mScopedByteArrays.back()->get());
107bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            } else {
108bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                // A direct ByteBuffer is easier.
109bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                ptr = reinterpret_cast<jbyte*>(mEnv->GetDirectBufferAddress(buffer));
110bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            }
111bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            if (ptr == NULL) {
112bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes                return false;
113bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            }
114bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            struct iovec iov;
115bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            iov.iov_base = reinterpret_cast<void*>(ptr + offsets[i]);
116bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            iov.iov_len = byteCounts[i];
117bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            mIoVec.push_back(iov);
118bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
119bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return true;
120bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
121bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
122bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    ~IoVec() {
123bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        for (size_t i = 0; i < mScopedByteArrays.size(); ++i) {
124bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            delete mScopedByteArrays[i];
125bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        }
126bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        mEnv->PopLocalFrame(NULL);
127bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
128bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
129bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    iovec* get() {
130bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return &mIoVec[0];
131bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
132bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
133bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    size_t size() {
134bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return mBufferCount;
135bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
136bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
137bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesprivate:
138bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    JNIEnv* mEnv;
139bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    size_t mBufferCount;
140bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    std::vector<iovec> mIoVec;
141bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    std::vector<T*> mScopedByteArrays;
142bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes};
143bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
14447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject makeStructStat(JNIEnv* env, const struct stat& sb) {
14547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structStatClass, "<init>",
14647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes            "(JJIJIIJJJJJJJ)V");
14747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return env->NewObject(JniConstants::structStatClass, ctor,
14859fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_dev), static_cast<jlong>(sb.st_ino),
14959fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jint>(sb.st_mode), static_cast<jlong>(sb.st_nlink),
15059fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jint>(sb.st_uid), static_cast<jint>(sb.st_gid),
15159fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_rdev), static_cast<jlong>(sb.st_size),
15259fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_atime), static_cast<jlong>(sb.st_mtime),
15359fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_ctime), static_cast<jlong>(sb.st_blksize),
15459fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.st_blocks));
15559fa7163774d6930a174bc038414a4b780581957Elliott Hughes}
15659fa7163774d6930a174bc038414a4b780581957Elliott Hughes
15759fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject makeStructStatFs(JNIEnv* env, const struct statfs& sb) {
15859fa7163774d6930a174bc038414a4b780581957Elliott Hughes    STATIC_ASSERT(sizeof(sb.f_bavail) == sizeof(jlong), statfs_not_64_bit);
15959fa7163774d6930a174bc038414a4b780581957Elliott Hughes    STATIC_ASSERT(sizeof(sb.f_bfree) == sizeof(jlong), statfs_not_64_bit);
16059fa7163774d6930a174bc038414a4b780581957Elliott Hughes    STATIC_ASSERT(sizeof(sb.f_blocks) == sizeof(jlong), statfs_not_64_bit);
16159fa7163774d6930a174bc038414a4b780581957Elliott Hughes
16259fa7163774d6930a174bc038414a4b780581957Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structStatFsClass, "<init>",
16359fa7163774d6930a174bc038414a4b780581957Elliott Hughes            "(JJJJJJJJ)V");
16459fa7163774d6930a174bc038414a4b780581957Elliott Hughes    return env->NewObject(JniConstants::structStatFsClass, ctor, static_cast<jlong>(sb.f_bsize),
16559fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_blocks), static_cast<jlong>(sb.f_bfree),
16659fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_bavail), static_cast<jlong>(sb.f_files),
16759fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_ffree), static_cast<jlong>(sb.f_namelen),
16859fa7163774d6930a174bc038414a4b780581957Elliott Hughes            static_cast<jlong>(sb.f_frsize));
16947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
17047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
1717341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject makeStructUtsname(JNIEnv* env, const struct utsname& buf) {
1727341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#define TO_JAVA_STRING(NAME) \
1737341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes        jstring NAME = env->NewStringUTF(buf. NAME); \
1747341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes        if (NAME == NULL) return NULL;
1757341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
1767341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(sysname);
1777341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(nodename);
1787341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(release);
1797341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(version);
1807341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    TO_JAVA_STRING(machine);
1817341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#undef TO_JAVA_STRING
1827341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
1837341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structUtsnameClass, "<init>",
1847341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes            "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
1857341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    return env->NewObject(JniConstants::structUtsnameClass, ctor,
1867341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes            sysname, nodename, release, version, machine);
1877341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes};
1887341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
18947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject doStat(JNIEnv* env, jstring javaPath, bool isLstat) {
19047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    ScopedUtfChars path(env, javaPath);
19147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    if (path.c_str() == NULL) {
19247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
19347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
19447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    struct stat sb;
19547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = isLstat ? TEMP_FAILURE_RETRY(lstat(path.c_str(), &sb))
19647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes                     : TEMP_FAILURE_RETRY(stat(path.c_str(), &sb));
197dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
1987e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, isLstat ? "lstat" : "stat");
19947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
20047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
20147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return makeStructStat(env, sb);
202ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
203ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
204ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jboolean Posix_access(JNIEnv* env, jobject, jstring javaPath, jint mode) {
205ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    ScopedUtfChars path(env, javaPath);
206ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (path.c_str() == NULL) {
207ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        return JNI_FALSE;
208ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
20947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = TEMP_FAILURE_RETRY(access(path.c_str(), mode));
210dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
2117e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "access");
212dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    }
213ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return (rc == 0);
214ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
215ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
216b7190190e0ef8de883c952efb319ce7748831faaElliott Hughesstatic void Posix_chmod(JNIEnv* env, jobject, jstring javaPath, jint mode) {
217b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    ScopedUtfChars path(env, javaPath);
218b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    if (path.c_str() == NULL) {
219b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes        return;
220b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    }
221b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    throwIfMinusOne(env, "chmod", TEMP_FAILURE_RETRY(chmod(path.c_str(), mode)));
222b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes}
223b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes
224ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jobjectArray Posix_environ(JNIEnv* env, jobject) {
225ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    extern char** environ; // Standard, but not in any header file.
226ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return toStringArray(env, environ);
227ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
228ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
229fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlVoid(JNIEnv* env, jobject, jobject javaFd, jint cmd) {
230fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
231fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd)));
232fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes}
233fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
234fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlLong(JNIEnv* env, jobject, jobject javaFd, jint cmd, jlong arg) {
235fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
236fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, arg)));
237fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes}
238fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
239fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlFlock(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaFlock) {
240fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID typeFid = env->GetFieldID(JniConstants::structFlockClass, "l_type", "S");
241fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID whenceFid = env->GetFieldID(JniConstants::structFlockClass, "l_whence", "S");
242fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID startFid = env->GetFieldID(JniConstants::structFlockClass, "l_start", "J");
243fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID lenFid = env->GetFieldID(JniConstants::structFlockClass, "l_len", "J");
244fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    static jfieldID pidFid = env->GetFieldID(JniConstants::structFlockClass, "l_pid", "I");
245fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
246fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    struct flock64 lock;
247fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    memset(&lock, 0, sizeof(lock));
248fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_type = env->GetShortField(javaFlock, typeFid);
249fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_whence = env->GetShortField(javaFlock, whenceFid);
250fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_start = env->GetLongField(javaFlock, startFid);
251fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_len = env->GetLongField(javaFlock, lenFid);
252fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    lock.l_pid = env->GetIntField(javaFlock, pidFid);
253fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
254fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
255fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    int rc = throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, &lock)));
256fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    if (rc != -1) {
257fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetShortField(javaFlock, typeFid, lock.l_type);
258fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetShortField(javaFlock, whenceFid, lock.l_whence);
259fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetLongField(javaFlock, startFid, lock.l_start);
260fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetLongField(javaFlock, lenFid, lock.l_len);
261fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        env->SetIntField(javaFlock, pidFid, lock.l_pid);
262fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    }
263fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    return rc;
264fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes}
265fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
26652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fdatasync(JNIEnv* env, jobject, jobject javaFd) {
26752724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
2687e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "fdatasync", TEMP_FAILURE_RETRY(fdatasync(fd)));
26952724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes}
27052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes
27147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_fstat(JNIEnv* env, jobject, jobject javaFd) {
27247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
27347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    struct stat sb;
27447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = TEMP_FAILURE_RETRY(fstat(fd, &sb));
275dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
2767e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "fstat");
27747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
27847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
27947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return makeStructStat(env, sb);
28047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
28147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
28259fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject Posix_fstatfs(JNIEnv* env, jobject, jobject javaFd) {
28359fa7163774d6930a174bc038414a4b780581957Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
28459fa7163774d6930a174bc038414a4b780581957Elliott Hughes    struct statfs sb;
28559fa7163774d6930a174bc038414a4b780581957Elliott Hughes    int rc = TEMP_FAILURE_RETRY(fstatfs(fd, &sb));
28659fa7163774d6930a174bc038414a4b780581957Elliott Hughes    if (rc == -1) {
28759fa7163774d6930a174bc038414a4b780581957Elliott Hughes        throwErrnoException(env, "fstatfs");
28859fa7163774d6930a174bc038414a4b780581957Elliott Hughes        return NULL;
28959fa7163774d6930a174bc038414a4b780581957Elliott Hughes    }
29059fa7163774d6930a174bc038414a4b780581957Elliott Hughes    return makeStructStatFs(env, sb);
29159fa7163774d6930a174bc038414a4b780581957Elliott Hughes}
29259fa7163774d6930a174bc038414a4b780581957Elliott Hughes
29352724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fsync(JNIEnv* env, jobject, jobject javaFd) {
29452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
2957e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "fsync", TEMP_FAILURE_RETRY(fsync(fd)));
296f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes}
297f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes
298f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughesstatic void Posix_ftruncate(JNIEnv* env, jobject, jobject javaFd, jlong length) {
299f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
3007e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "ftruncate", TEMP_FAILURE_RETRY(ftruncate64(fd, length)));
30152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes}
30252724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes
303ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic jstring Posix_getenv(JNIEnv* env, jobject, jstring javaName) {
304ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    ScopedUtfChars name(env, javaName);
305ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    if (name.c_str() == NULL) {
306ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes        return NULL;
307ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    }
308ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    return env->NewStringUTF(getenv(name.c_str()));
309ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes}
310ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
3118b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jint Posix_ioctlInt(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaArg) {
3128b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    // This is complicated because ioctls may return their result by updating their argument
3138b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    // or via their return value, so we need to support both.
314461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
3158b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    static jfieldID valueFid = env->GetFieldID(JniConstants::mutableIntClass, "value", "I");
3168b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    jint arg = env->GetIntField(javaArg, valueFid);
3178b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &arg)));
3188b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    if (!env->ExceptionCheck()) {
3198b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        env->SetIntField(javaArg, valueFid, arg);
3208b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    }
3218b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    return rc;
322461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes}
323461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes
3249a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughesstatic jboolean Posix_isatty(JNIEnv* env, jobject, jobject javaFd) {
3259a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
3269a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    return TEMP_FAILURE_RETRY(isatty(fd)) == 0;
3279a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes}
3289a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes
329e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughesstatic void Posix_listen(JNIEnv* env, jobject, jobject javaFd, jint backlog) {
330e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
331e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes    throwIfMinusOne(env, "listen", TEMP_FAILURE_RETRY(listen(fd, backlog)));
332e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes}
333e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes
334dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughesstatic jlong Posix_lseek(JNIEnv* env, jobject, jobject javaFd, jlong offset, jint whence) {
335dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
3367e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    return throwIfMinusOne(env, "lseek", TEMP_FAILURE_RETRY(lseek64(fd, offset, whence)));
337dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes}
338dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes
33947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_lstat(JNIEnv* env, jobject, jstring javaPath) {
34047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return doStat(env, javaPath, true);
34147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
34247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
3430f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughesstatic void Posix_mincore(JNIEnv* env, jobject, jlong address, jlong byteCount, jbyteArray javaVector) {
3440f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    ScopedByteArrayRW vector(env, javaVector);
3450f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    if (vector.get() == NULL) {
3460f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes        return;
3470f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    }
3480f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
3490f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    unsigned char* vec = reinterpret_cast<unsigned char*>(vector.get());
3500f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    throwIfMinusOne(env, "mincore", TEMP_FAILURE_RETRY(mincore(ptr, byteCount, vec)));
3510f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes}
3520f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes
353c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_mkdir(JNIEnv* env, jobject, jstring javaPath, jint mode) {
354c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    ScopedUtfChars path(env, javaPath);
355c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    if (path.c_str() == NULL) {
356c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes        return;
357c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    }
358c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    throwIfMinusOne(env, "mkdir", TEMP_FAILURE_RETRY(mkdir(path.c_str(), mode)));
359c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes}
360c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes
3617e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_mlock(JNIEnv* env, jobject, jlong address, jlong byteCount) {
3627e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
3637e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    throwIfMinusOne(env, "mlock", TEMP_FAILURE_RETRY(mlock(ptr, byteCount)));
3647e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes}
3657e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes
3667e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic jlong Posix_mmap(JNIEnv* env, jobject, jlong address, jlong byteCount, jint prot, jint flags, jobject javaFd, jlong offset) {
3677e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
3687e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* suggestedPtr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
3697e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = mmap(suggestedPtr, byteCount, prot, flags, fd, offset);
3707e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    if (ptr == MAP_FAILED) {
3717e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "mmap");
3727e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    }
3737e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    return static_cast<jlong>(reinterpret_cast<uintptr_t>(ptr));
3747e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
3757e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
3767e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_msync(JNIEnv* env, jobject, jlong address, jlong byteCount, jint flags) {
3777e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
3787e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "msync", TEMP_FAILURE_RETRY(msync(ptr, byteCount, flags)));
3797e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
3807e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
3817e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_munlock(JNIEnv* env, jobject, jlong address, jlong byteCount) {
3827e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
3837e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    throwIfMinusOne(env, "munlock", TEMP_FAILURE_RETRY(munlock(ptr, byteCount)));
3847e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes}
3857e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes
3867e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_munmap(JNIEnv* env, jobject, jlong address, jlong byteCount) {
3877e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
3887e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "munmap", TEMP_FAILURE_RETRY(munmap(ptr, byteCount)));
3897e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
3907e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
3910ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughesstatic jobject Posix_open(JNIEnv* env, jobject, jstring javaPath, jint flags, jint mode) {
3920ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    ScopedUtfChars path(env, javaPath);
3930ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    if (path.c_str() == NULL) {
3940ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes        return NULL;
3950ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    }
3960ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    int fd = throwIfMinusOne(env, "open", TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode)));
3970ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL;
3980ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes}
3990ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes
40041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughesstatic jobjectArray Posix_pipe(JNIEnv* env, jobject) {
40141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    int fds[2];
40241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    throwIfMinusOne(env, "pipe", TEMP_FAILURE_RETRY(pipe(&fds[0])));
40341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    jobjectArray result = env->NewObjectArray(2, JniConstants::fileDescriptorClass, NULL);
40441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    if (result == NULL) {
40541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        return NULL;
40641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    }
40741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    for (int i = 0; i < 2; ++i) {
40841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        ScopedLocalRef<jobject> fd(env, jniCreateFileDescriptor(env, fds[i]));
40941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        if (fd.get() == NULL) {
41041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes            return NULL;
41141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        }
41241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        env->SetObjectArrayElement(result, i, fd.get());
41341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        if (env->ExceptionCheck()) {
41441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes            return NULL;
41541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes        }
41641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    }
41741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    return result;
41841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes}
41941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes
42026c7025a7a919044771fb89031161bd26fe03032Elliott Hughesstatic jint Posix_read(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount) {
42126c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
42226c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    ScopedByteArrayRW bytes(env, javaBytes);
42326c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    if (bytes.get() == NULL) {
42426c7025a7a919044771fb89031161bd26fe03032Elliott Hughes        return -1;
42526c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    }
42626c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    return throwIfMinusOne(env, "read", TEMP_FAILURE_RETRY(read(fd, bytes.get() + byteOffset, byteCount)));
42726c7025a7a919044771fb89031161bd26fe03032Elliott Hughes}
42826c7025a7a919044771fb89031161bd26fe03032Elliott Hughes
42926c7025a7a919044771fb89031161bd26fe03032Elliott Hughesstatic jint Posix_readDirectBuffer(JNIEnv* env, jobject, jobject javaFd, jobject byteBuffer, jint position, jint remaining) {
43026c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
43126c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    jbyte* ptr = reinterpret_cast<jbyte*>(env->GetDirectBufferAddress(byteBuffer));
43226c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    return throwIfMinusOne(env, "read", TEMP_FAILURE_RETRY(read(fd, ptr + position, remaining)));
43326c7025a7a919044771fb89031161bd26fe03032Elliott Hughes}
43426c7025a7a919044771fb89031161bd26fe03032Elliott Hughes
435bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_readv(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) {
436bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    IoVec<ScopedByteArrayRW> ioVec(env, env->GetArrayLength(buffers));
437bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    if (!ioVec.init(buffers, offsets, byteCounts)) {
438bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return -1;
439bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
440bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
441bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    return throwIfMinusOne(env, "readv", TEMP_FAILURE_RETRY(readv(fd, ioVec.get(), ioVec.size())));
442bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes}
443bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
444c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_remove(JNIEnv* env, jobject, jstring javaPath) {
445c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    ScopedUtfChars path(env, javaPath);
446c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    if (path.c_str() == NULL) {
447c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes        return;
448c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    }
449c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    throwIfMinusOne(env, "remove", TEMP_FAILURE_RETRY(remove(path.c_str())));
450c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes}
451c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes
452a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_rename(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) {
453a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars oldPath(env, javaOldPath);
454a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (oldPath.c_str() == NULL) {
455a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
456a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
457a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars newPath(env, javaNewPath);
458a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (newPath.c_str() == NULL) {
459a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
460a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
461a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    throwIfMinusOne(env, "rename", TEMP_FAILURE_RETRY(rename(oldPath.c_str(), newPath.c_str())));
462a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes}
463a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes
4648b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jlong Posix_sendfile(JNIEnv* env, jobject, jobject javaOutFd, jobject javaInFd, jobject javaOffset, jlong byteCount) {
4658b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    int outFd = jniGetFDFromFileDescriptor(env, javaOutFd);
4668b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    int inFd = jniGetFDFromFileDescriptor(env, javaInFd);
4678b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    static jfieldID valueFid = env->GetFieldID(JniConstants::mutableLongClass, "value", "J");
4688b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    off_t offset = 0;
4698b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    off_t* offsetPtr = NULL;
4708b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    if (javaOffset != NULL) {
4718b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        // TODO: fix bionic so we can have a 64-bit off_t!
4728b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        offset = env->GetLongField(javaOffset, valueFid);
4738b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        offsetPtr = &offset;
4748b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    }
4758b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    jlong result = throwIfMinusOne(env, "sendfile", TEMP_FAILURE_RETRY(sendfile(outFd, inFd, offsetPtr, byteCount)));
4768b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    if (javaOffset != NULL) {
4778b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        env->SetLongField(javaOffset, valueFid, offset);
4788b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    }
4798b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    return result;
4808b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes}
4818b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes
482454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic void Posix_setsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) {
483454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
484454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))));
485454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes}
486454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes
48759e4744d27231f260271dbbca406e0cc39768116Elliott Hughesstatic void Posix_shutdown(JNIEnv* env, jobject, jobject javaFd, jint how) {
48859e4744d27231f260271dbbca406e0cc39768116Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
48959e4744d27231f260271dbbca406e0cc39768116Elliott Hughes    throwIfMinusOne(env, "shutdown", TEMP_FAILURE_RETRY(shutdown(fd, how)));
49059e4744d27231f260271dbbca406e0cc39768116Elliott Hughes}
49159e4744d27231f260271dbbca406e0cc39768116Elliott Hughes
492454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic jobject Posix_socket(JNIEnv* env, jobject, jint domain, jint type, jint protocol) {
493454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    int fd = throwIfMinusOne(env, "socket", TEMP_FAILURE_RETRY(socket(domain, type, protocol)));
494454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL;
495454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes}
496454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes
49747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_stat(JNIEnv* env, jobject, jstring javaPath) {
49847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return doStat(env, javaPath, false);
49947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
50047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
50159fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject Posix_statfs(JNIEnv* env, jstring javaPath) {
50259fa7163774d6930a174bc038414a4b780581957Elliott Hughes    ScopedUtfChars path(env, javaPath);
50359fa7163774d6930a174bc038414a4b780581957Elliott Hughes    if (path.c_str() == NULL) {
50459fa7163774d6930a174bc038414a4b780581957Elliott Hughes        return NULL;
50559fa7163774d6930a174bc038414a4b780581957Elliott Hughes    }
50659fa7163774d6930a174bc038414a4b780581957Elliott Hughes    struct statfs sb;
50759fa7163774d6930a174bc038414a4b780581957Elliott Hughes    int rc = TEMP_FAILURE_RETRY(statfs(path.c_str(), &sb));
50859fa7163774d6930a174bc038414a4b780581957Elliott Hughes    if (rc == -1) {
50959fa7163774d6930a174bc038414a4b780581957Elliott Hughes        throwErrnoException(env, "statfs");
51059fa7163774d6930a174bc038414a4b780581957Elliott Hughes        return NULL;
51159fa7163774d6930a174bc038414a4b780581957Elliott Hughes    }
51259fa7163774d6930a174bc038414a4b780581957Elliott Hughes    return makeStructStatFs(env, sb);
51359fa7163774d6930a174bc038414a4b780581957Elliott Hughes}
51459fa7163774d6930a174bc038414a4b780581957Elliott Hughes
515ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jstring Posix_strerror(JNIEnv* env, jobject, jint errnum) {
51652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    char buffer[BUFSIZ];
517ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    const char* message = jniStrError(errnum, buffer, sizeof(buffer));
518ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return env->NewStringUTF(message);
519ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
520ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
521a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_symlink(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) {
522a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars oldPath(env, javaOldPath);
523a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (oldPath.c_str() == NULL) {
524a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
525a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
526a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    ScopedUtfChars newPath(env, javaNewPath);
527a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    if (newPath.c_str() == NULL) {
528a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes        return;
529a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    }
530a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    throwIfMinusOne(env, "symlink", TEMP_FAILURE_RETRY(symlink(oldPath.c_str(), newPath.c_str())));
531a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes}
532a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes
5336fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughesstatic jlong Posix_sysconf(JNIEnv* env, jobject, jint name) {
5346fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    // Since -1 is a valid result from sysconf(3), detecting failure is a little more awkward.
5356fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    errno = 0;
5366fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    long result = sysconf(name);
5376fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    if (result == -1L && errno == EINVAL) {
5387e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "sysconf");
5396fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    }
5406fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    return result;
5416fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes}
5426fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes
5437341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject Posix_uname(JNIEnv* env, jobject) {
5447341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    struct utsname buf;
5457341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    if (throwIfMinusOne(env, "uname", TEMP_FAILURE_RETRY(uname(&buf))) == -1) {
5467341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes        return NULL;
5477341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    }
5487341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    return makeStructUtsname(env, buf);
5497341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes}
5507341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes
55178c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughesstatic jint Posix_write(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount) {
55278c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
55378c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    ScopedByteArrayRO bytes(env, javaBytes);
55478c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    if (bytes.get() == NULL) {
55578c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes        return -1;
55678c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    }
55778c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    return throwIfMinusOne(env, "write", TEMP_FAILURE_RETRY(write(fd, bytes.get() + byteOffset, byteCount)));
55878c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes}
55978c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes
56078c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughesstatic jint Posix_writeDirectBuffer(JNIEnv* env, jobject, jobject javaFd, jobject byteBuffer, jint position, jint remaining) {
56178c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
56278c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    jbyte* ptr = reinterpret_cast<jbyte*>(env->GetDirectBufferAddress(byteBuffer));
56378c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    return throwIfMinusOne(env, "write", TEMP_FAILURE_RETRY(write(fd, ptr + position, remaining)));
56478c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes}
56578c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes
566bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_writev(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) {
567bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    IoVec<ScopedByteArrayRO> ioVec(env, env->GetArrayLength(buffers));
568bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    if (!ioVec.init(buffers, offsets, byteCounts)) {
569bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return -1;
570bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
571bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
572bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    return throwIfMinusOne(env, "writev", TEMP_FAILURE_RETRY(writev(fd, ioVec.get(), ioVec.size())));
573bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes}
574bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
575ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic JNINativeMethod gMethods[] = {
576ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, access, "(Ljava/lang/String;I)Z"),
577b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes    NATIVE_METHOD(Posix, chmod, "(Ljava/lang/String;I)V"),
578ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, environ, "()[Ljava/lang/String;"),
579fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    NATIVE_METHOD(Posix, fcntlVoid, "(Ljava/io/FileDescriptor;I)I"),
580fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    NATIVE_METHOD(Posix, fcntlLong, "(Ljava/io/FileDescriptor;IJ)I"),
581fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    NATIVE_METHOD(Posix, fcntlFlock, "(Ljava/io/FileDescriptor;ILlibcore/io/StructFlock;)I"),
58252724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    NATIVE_METHOD(Posix, fdatasync, "(Ljava/io/FileDescriptor;)V"),
58347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, fstat, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStat;"),
58459fa7163774d6930a174bc038414a4b780581957Elliott Hughes    NATIVE_METHOD(Posix, fstatfs, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStatFs;"),
58552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    NATIVE_METHOD(Posix, fsync, "(Ljava/io/FileDescriptor;)V"),
586f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    NATIVE_METHOD(Posix, ftruncate, "(Ljava/io/FileDescriptor;J)V"),
587ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    NATIVE_METHOD(Posix, getenv, "(Ljava/lang/String;)Ljava/lang/String;"),
5888b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    NATIVE_METHOD(Posix, ioctlInt, "(Ljava/io/FileDescriptor;ILlibcore/util/MutableInt;)I"),
5899a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    NATIVE_METHOD(Posix, isatty, "(Ljava/io/FileDescriptor;)Z"),
590e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes    NATIVE_METHOD(Posix, listen, "(Ljava/io/FileDescriptor;I)V"),
591dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    NATIVE_METHOD(Posix, lseek, "(Ljava/io/FileDescriptor;JI)J"),
59247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, lstat, "(Ljava/lang/String;)Llibcore/io/StructStat;"),
5930f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes    NATIVE_METHOD(Posix, mincore, "(JJ[B)V"),
594c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    NATIVE_METHOD(Posix, mkdir, "(Ljava/lang/String;I)V"),
5957e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    NATIVE_METHOD(Posix, mlock, "(JJ)V"),
5967e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, mmap, "(JJIILjava/io/FileDescriptor;J)J"),
5977e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, msync, "(JJI)V"),
5987e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    NATIVE_METHOD(Posix, munlock, "(JJ)V"),
5997e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, munmap, "(JJ)V"),
6000ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    NATIVE_METHOD(Posix, open, "(Ljava/lang/String;II)Ljava/io/FileDescriptor;"),
60141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes    NATIVE_METHOD(Posix, pipe, "()[Ljava/io/FileDescriptor;"),
60226c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    NATIVE_METHOD(Posix, read, "(Ljava/io/FileDescriptor;[BII)I"),
60326c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    NATIVE_METHOD(Posix, readDirectBuffer, "(Ljava/io/FileDescriptor;Ljava/nio/ByteBuffer;II)I"),
604bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    NATIVE_METHOD(Posix, readv, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"),
605c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes    NATIVE_METHOD(Posix, remove, "(Ljava/lang/String;)V"),
606a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    NATIVE_METHOD(Posix, rename, "(Ljava/lang/String;Ljava/lang/String;)V"),
6078b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes    NATIVE_METHOD(Posix, sendfile, "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Llibcore/util/MutableLong;J)J"),
608454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    NATIVE_METHOD(Posix, setsockoptInt, "(Ljava/io/FileDescriptor;III)V"),
60959e4744d27231f260271dbbca406e0cc39768116Elliott Hughes    NATIVE_METHOD(Posix, shutdown, "(Ljava/io/FileDescriptor;I)V"),
610454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes    NATIVE_METHOD(Posix, socket, "(III)Ljava/io/FileDescriptor;"),
61147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, stat, "(Ljava/lang/String;)Llibcore/io/StructStat;"),
61259fa7163774d6930a174bc038414a4b780581957Elliott Hughes    NATIVE_METHOD(Posix, statfs, "(Ljava/lang/String;)Llibcore/io/StructStatFs;"),
613ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, strerror, "(I)Ljava/lang/String;"),
614a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes    NATIVE_METHOD(Posix, symlink, "(Ljava/lang/String;Ljava/lang/String;)V"),
6156fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    NATIVE_METHOD(Posix, sysconf, "(I)J"),
6167341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes    NATIVE_METHOD(Posix, uname, "()Llibcore/io/StructUtsname;"),
61778c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    NATIVE_METHOD(Posix, write, "(Ljava/io/FileDescriptor;[BII)I"),
61878c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    NATIVE_METHOD(Posix, writeDirectBuffer, "(Ljava/io/FileDescriptor;Ljava/nio/ByteBuffer;II)I"),
619bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    NATIVE_METHOD(Posix, writev, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"),
620ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes};
621ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesint register_libcore_io_Posix(JNIEnv* env) {
622ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    return jniRegisterNativeMethods(env, "libcore/io/Posix", gMethods, NELEM(gMethods));
623ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes}
624