libcore_io_Linux.cpp revision d4419fce71d11ec8494525eca65e54d1aab51de6
1ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes/* 2ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * Copyright (C) 2011 The Android Open Source Project 3ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * 4ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 5ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * you may not use this file except in compliance with the License. 6ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * You may obtain a copy of the License at 7ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * 8ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 9ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * 10ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * Unless required by applicable law or agreed to in writing, software 11ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 12ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * See the License for the specific language governing permissions and 14ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * limitations under the License. 15ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes */ 16ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 17ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#define LOG_TAG "Posix" 18ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 19996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes#include "AsynchronousSocketCloseMonitor.h" 20ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "JNIHelp.h" 21ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "JniConstants.h" 22ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include "JniException.h" 230a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include "NetworkUtilities.h" 240568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes#include "ScopedBytes.h" 25e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes#include "ScopedLocalRef.h" 260f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes#include "ScopedPrimitiveArray.h" 27ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "ScopedUtfChars.h" 2859fa7163774d6930a174bc038414a4b780581957Elliott Hughes#include "StaticAssert.h" 291c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes#include "UniquePtr.h" 30ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include "toStringArray.h" 31ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 321c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes#include <arpa/inet.h> 33ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include <errno.h> 340ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <fcntl.h> 35b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes#include <net/if.h> 364f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes#include <netdb.h> 370a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include <netinet/in.h> 381c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes#include <netinet/in.h> 3970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes#include <poll.h> 40d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes#include <pwd.h> 41a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes#include <signal.h> 42ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include <stdlib.h> 43461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes#include <sys/ioctl.h> 447e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes#include <sys/mman.h> 458b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes#include <sys/sendfile.h> 4659e4744d27231f260271dbbca406e0cc39768116Elliott Hughes#include <sys/socket.h> 471c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes#include <sys/socket.h> 4847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes#include <sys/stat.h> 490a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include <sys/time.h> 500ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <sys/types.h> 51bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes#include <sys/uio.h> 527341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#include <sys/utsname.h> 5359fa7163774d6930a174bc038414a4b780581957Elliott Hughes#include <sys/vfs.h> // Bionic doesn't have <sys/statvfs.h> 540ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <unistd.h> 55ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 56d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes#define TO_JAVA_STRING(NAME, EXP) \ 57d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes jstring NAME = env->NewStringUTF(EXP); \ 58d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes if (NAME == NULL) return NULL; 59d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 601c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughesstruct addrinfo_deleter { 611c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes void operator()(addrinfo* p) const { 621c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (p != NULL) { // bionic's freeaddrinfo(3) crashes when passed NULL. 631c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes freeaddrinfo(p); 641c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 651c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 661c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes}; 671c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 68996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes/** 69996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * Used to retry syscalls that can return EINTR. This differs from TEMP_FAILURE_RETRY in that 70996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * it also considers the case where the reason for failure is that another thread called 71996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * Socket.close. 72996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * 73996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * Assumes 'JNIEnv* env' and 'jobject javaFd' (which is a java.io.FileDescriptor) are in scope. 74996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * 75996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * Returns the result of 'exp', though a Java exception will be pending if the result is -1. 76996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * 77996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * Correct usage looks like this: 78996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * 79996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * void Posix_syscall(JNIEnv* env, jobject javaFd, ...) { 80996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * ... 81996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * int fd; 82996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * NET_FAILURE_RETRY("syscall", syscall(fd, ...)); // Throws on error. 83996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * } 84996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes */ 85996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes#define NET_FAILURE_RETRY(syscall_name, exp) ({ \ 86996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes typeof (exp) _rc = -1; \ 87996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes do { \ 88996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes { \ 89996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes fd = jniGetFDFromFileDescriptor(env, javaFd); \ 90996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes AsynchronousSocketCloseMonitor monitor(fd); \ 91996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes _rc = (exp); \ 92996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes } \ 93996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes if (_rc == -1) { \ 94996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes if (jniGetFDFromFileDescriptor(env, javaFd) == -1) { \ 95996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes jniThrowException(env, "java/net/SocketException", "Socket closed"); \ 96996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes break; \ 97996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes } else if (errno != EINTR) { \ 98996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes throwErrnoException(env, syscall_name); \ 99996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes break; \ 100996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes } \ 101996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes } \ 102996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes } while (_rc == -1); \ 103996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes _rc; }) 104996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes 1054f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwException(JNIEnv* env, jclass exceptionClass, jmethodID ctor3, jmethodID ctor2, 1064f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes const char* functionName, int error) { 107ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes jthrowable cause = NULL; 108ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes if (env->ExceptionCheck()) { 109ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes cause = env->ExceptionOccurred(); 110ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes env->ExceptionClear(); 111ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } 112ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 1134f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName)); 1144f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes if (detailMessage.get() == NULL) { 115f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes // Not really much we can do here. We're probably dead in the water, 116f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes // but let's try to stumble on... 117f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes env->ExceptionClear(); 118f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes } 119f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes 120ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes jobject exception; 121ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes if (cause != NULL) { 1224f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes exception = env->NewObject(exceptionClass, ctor3, detailMessage.get(), error, cause); 123ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } else { 1244f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes exception = env->NewObject(exceptionClass, ctor2, detailMessage.get(), error); 125ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } 126ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes env->Throw(reinterpret_cast<jthrowable>(exception)); 127dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes} 128dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes 1294f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwErrnoException(JNIEnv* env, const char* functionName) { 1304f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes int error = errno; 1314f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes static jmethodID ctor3 = env->GetMethodID(JniConstants::errnoExceptionClass, 1324f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V"); 1334f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes static jmethodID ctor2 = env->GetMethodID(JniConstants::errnoExceptionClass, 1344f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes "<init>", "(Ljava/lang/String;I)V"); 1354f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwException(env, JniConstants::errnoExceptionClass, ctor3, ctor2, functionName, error); 1364f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 1374f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 1384f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwGaiException(JNIEnv* env, const char* functionName, int error) { 1394f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes if (error == EAI_SYSTEM) { 1404f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // EAI_SYSTEM means "look at errno instead", so we want our GaiException to have the 1414f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // relevant ErrnoException as its cause. 1424f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwErrnoException(env, functionName); 1434f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // Deliberately fall through to throw another exception... 1444f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes } 1454f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes static jmethodID ctor3 = env->GetMethodID(JniConstants::gaiExceptionClass, 1464f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V"); 1474f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes static jmethodID ctor2 = env->GetMethodID(JniConstants::gaiExceptionClass, 1484f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes "<init>", "(Ljava/lang/String;I)V"); 1494f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwException(env, JniConstants::gaiExceptionClass, ctor3, ctor2, functionName, error); 1504f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 1514f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 152dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughestemplate <typename rc_t> 1537e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic rc_t throwIfMinusOne(JNIEnv* env, const char* name, rc_t rc) { 154dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == rc_t(-1)) { 1557e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, name); 156dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes } 157dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes return rc; 15847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 15947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 1608dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughestemplate <typename ScopedT> 161bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesclass IoVec { 162bbac92e691de7d570928ddfba639067978e55b06Elliott Hughespublic: 163bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes IoVec(JNIEnv* env, size_t bufferCount) : mEnv(env), mBufferCount(bufferCount) { 164bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 165bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 166bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes bool init(jobjectArray javaBuffers, jintArray javaOffsets, jintArray javaByteCounts) { 167bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes // We can't delete our local references until after the I/O, so make sure we have room. 168bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (mEnv->PushLocalFrame(mBufferCount + 16) < 0) { 169bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 170bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 171bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes ScopedIntArrayRO offsets(mEnv, javaOffsets); 172bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (offsets.get() == NULL) { 173bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 174bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 175bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes ScopedIntArrayRO byteCounts(mEnv, javaByteCounts); 176bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (byteCounts.get() == NULL) { 177bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 178bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 179bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes // TODO: Linux actually has a 1024 buffer limit. glibc works around this, and we should too. 180d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes // TODO: you can query the limit at runtime with sysconf(_SC_IOV_MAX). 181bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes for (size_t i = 0; i < mBufferCount; ++i) { 182e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes jobject buffer = mEnv->GetObjectArrayElement(javaBuffers, i); // We keep this local ref. 1838dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes mScopedBuffers.push_back(new ScopedT(mEnv, buffer)); 1848dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes jbyte* ptr = const_cast<jbyte*>(mScopedBuffers.back()->get()); 185bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (ptr == NULL) { 186bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 187bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 188bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes struct iovec iov; 189bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes iov.iov_base = reinterpret_cast<void*>(ptr + offsets[i]); 190bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes iov.iov_len = byteCounts[i]; 191bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes mIoVec.push_back(iov); 192bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 193bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return true; 194bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 195bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 196bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes ~IoVec() { 1978dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes for (size_t i = 0; i < mScopedBuffers.size(); ++i) { 1988dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes delete mScopedBuffers[i]; 199bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 200bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes mEnv->PopLocalFrame(NULL); 201bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 202bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 203bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes iovec* get() { 204bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return &mIoVec[0]; 205bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 206bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 207bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes size_t size() { 208bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return mBufferCount; 209bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 210bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 211bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesprivate: 212bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes JNIEnv* mEnv; 213bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes size_t mBufferCount; 214bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes std::vector<iovec> mIoVec; 2158dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes std::vector<ScopedT*> mScopedBuffers; 216bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes}; 217bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 2180a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeInetSocketAddress(JNIEnv* env, const sockaddr_storage* ss, int port) { 2190a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes jobject inetAddress = socketAddressToInetAddress(env, ss); 2200a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (inetAddress == NULL) { 2210a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 2220a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 2230a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::inetSocketAddressClass, "<init>", 2240a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes "(Ljava/net/InetAddress;I)V"); 2250a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return env->NewObject(JniConstants::inetSocketAddressClass, ctor, inetAddress, port); 2260a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 2270a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 2280a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeSocketAddress(JNIEnv* env, const sockaddr_storage* ss) { 2290a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (ss->ss_family == AF_INET) { 2300a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int port = ntohs(reinterpret_cast<const sockaddr_in*>(ss)->sin_port); 2310a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return makeInetSocketAddress(env, ss, port); 2320a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } else if (ss->ss_family == AF_INET6) { 2330a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int port = ntohs(reinterpret_cast<const sockaddr_in6*>(ss)->sin6_port); 2340a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return makeInetSocketAddress(env, ss, port); 2350a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 2360a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes // TODO: support AF_UNIX and AF_UNSPEC, and have some other behavior for other families 2370a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 2380a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 2390a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 240d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesstatic jobject makeStructPasswd(JNIEnv* env, const struct passwd& pw) { 241d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(pw_name, pw.pw_name); 242d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(pw_dir, pw.pw_dir); 243d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(pw_shell, pw.pw_shell); 244d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structPasswdClass, "<init>", 245d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes "(Ljava/lang/String;IILjava/lang/String;Ljava/lang/String;)V"); 246d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return env->NewObject(JniConstants::structPasswdClass, ctor, 247d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes pw_name, static_cast<jint>(pw.pw_uid), static_cast<jint>(pw.pw_gid), pw_dir, pw_shell); 248d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes} 249d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 25047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject makeStructStat(JNIEnv* env, const struct stat& sb) { 25147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structStatClass, "<init>", 25247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes "(JJIJIIJJJJJJJ)V"); 25347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return env->NewObject(JniConstants::structStatClass, ctor, 25459fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_dev), static_cast<jlong>(sb.st_ino), 25559fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jint>(sb.st_mode), static_cast<jlong>(sb.st_nlink), 25659fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jint>(sb.st_uid), static_cast<jint>(sb.st_gid), 25759fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_rdev), static_cast<jlong>(sb.st_size), 25859fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_atime), static_cast<jlong>(sb.st_mtime), 25959fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_ctime), static_cast<jlong>(sb.st_blksize), 26059fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_blocks)); 26159fa7163774d6930a174bc038414a4b780581957Elliott Hughes} 26259fa7163774d6930a174bc038414a4b780581957Elliott Hughes 26359fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject makeStructStatFs(JNIEnv* env, const struct statfs& sb) { 26459fa7163774d6930a174bc038414a4b780581957Elliott Hughes STATIC_ASSERT(sizeof(sb.f_bavail) == sizeof(jlong), statfs_not_64_bit); 26559fa7163774d6930a174bc038414a4b780581957Elliott Hughes STATIC_ASSERT(sizeof(sb.f_bfree) == sizeof(jlong), statfs_not_64_bit); 26659fa7163774d6930a174bc038414a4b780581957Elliott Hughes STATIC_ASSERT(sizeof(sb.f_blocks) == sizeof(jlong), statfs_not_64_bit); 26759fa7163774d6930a174bc038414a4b780581957Elliott Hughes 26859fa7163774d6930a174bc038414a4b780581957Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structStatFsClass, "<init>", 26959fa7163774d6930a174bc038414a4b780581957Elliott Hughes "(JJJJJJJJ)V"); 27059fa7163774d6930a174bc038414a4b780581957Elliott Hughes return env->NewObject(JniConstants::structStatFsClass, ctor, static_cast<jlong>(sb.f_bsize), 27159fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.f_blocks), static_cast<jlong>(sb.f_bfree), 27259fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.f_bavail), static_cast<jlong>(sb.f_files), 27359fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.f_ffree), static_cast<jlong>(sb.f_namelen), 27459fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.f_frsize)); 27547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 27647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 2770a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructLinger(JNIEnv* env, const struct linger& l) { 2780a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structLingerClass, "<init>", "(II)V"); 2790a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return env->NewObject(JniConstants::structLingerClass, ctor, l.l_onoff, l.l_linger); 2800a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 2810a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 2820a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructTimeval(JNIEnv* env, const struct timeval& tv) { 2830a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structTimevalClass, "<init>", "(JJ)V"); 2840a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return env->NewObject(JniConstants::structTimevalClass, ctor, 2850a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static_cast<jlong>(tv.tv_sec), static_cast<jlong>(tv.tv_usec)); 2860a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 2870a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 2887341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject makeStructUtsname(JNIEnv* env, const struct utsname& buf) { 289d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(sysname, buf.sysname); 290d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(nodename, buf.nodename); 291d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(release, buf.release); 292d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(version, buf.version); 293d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(machine, buf.machine); 2947341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structUtsnameClass, "<init>", 2957341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); 2967341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes return env->NewObject(JniConstants::structUtsnameClass, ctor, 2977341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes sysname, nodename, release, version, machine); 2987341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes}; 2997341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes 300a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic bool fillIfreq(JNIEnv* env, jstring javaInterfaceName, struct ifreq& req) { 301a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes ScopedUtfChars interfaceName(env, javaInterfaceName); 302a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (interfaceName.c_str() == NULL) { 303a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return false; 304a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes } 305a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes memset(&req, 0, sizeof(req)); 306a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes strncpy(req.ifr_name, interfaceName.c_str(), sizeof(req.ifr_name)); 307a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes req.ifr_name[sizeof(req.ifr_name) - 1] = '\0'; 308a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return true; 309a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes} 310a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes 31147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject doStat(JNIEnv* env, jstring javaPath, bool isLstat) { 31247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes ScopedUtfChars path(env, javaPath); 31347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes if (path.c_str() == NULL) { 31447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return NULL; 31547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes } 31647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes struct stat sb; 31747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int rc = isLstat ? TEMP_FAILURE_RETRY(lstat(path.c_str(), &sb)) 31847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes : TEMP_FAILURE_RETRY(stat(path.c_str(), &sb)); 319dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == -1) { 3207e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, isLstat ? "lstat" : "stat"); 32147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return NULL; 32247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes } 32347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return makeStructStat(env, sb); 324ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 325ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 326d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesclass Passwd { 327d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughespublic: 328d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes Passwd(JNIEnv* env) : mEnv(env), mResult(NULL) { 329d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes mBufferSize = sysconf(_SC_GETPW_R_SIZE_MAX); 330d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes if (mBufferSize == -1UL) { 331d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes // We're probably on bionic, where 1KiB should be enough for anyone. 332d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes // TODO: fix bionic to return 1024 like glibc. 333d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes mBufferSize = 1024; 334d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 335d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes mBuffer.reset(new char[mBufferSize]); 336d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 337d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 338d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes jobject getpwnam(const char* name) { 339d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return process("getpwnam_r", getpwnam_r(name, &mPwd, mBuffer.get(), mBufferSize, &mResult)); 340d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 341d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 342d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes jobject getpwuid(uid_t uid) { 343d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return process("getpwuid_r", getpwuid_r(uid, &mPwd, mBuffer.get(), mBufferSize, &mResult)); 344d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 345d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 346d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes struct passwd* get() { 347d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return mResult; 348d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 349d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 350d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesprivate: 351d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes jobject process(const char* syscall, int error) { 352d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes if (mResult == NULL) { 353d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes errno = error; 354d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes throwErrnoException(mEnv, syscall); 355d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return NULL; 356d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 357d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return makeStructPasswd(mEnv, *mResult); 358d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 359d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 360d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes JNIEnv* mEnv; 361d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes UniquePtr<char[]> mBuffer; 362d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes size_t mBufferSize; 363d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes struct passwd mPwd; 364d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes struct passwd* mResult; 365d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes}; 366d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 367ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jboolean Posix_access(JNIEnv* env, jobject, jstring javaPath, jint mode) { 368ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes ScopedUtfChars path(env, javaPath); 369ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes if (path.c_str() == NULL) { 370ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return JNI_FALSE; 371ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } 37247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int rc = TEMP_FAILURE_RETRY(access(path.c_str(), mode)); 373dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == -1) { 3747e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "access"); 375dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes } 376ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return (rc == 0); 377ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 378ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 379da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughesstatic void Posix_bind(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) { 380da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes sockaddr_storage ss; 381da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes if (!inetAddressToSockaddr(env, javaAddress, port, &ss)) { 382da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes return; 383da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes } 384996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes int fd; 385da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss); 386996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes NET_FAILURE_RETRY("bind", bind(fd, sa, sizeof(sockaddr_storage))); 387da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes} 388da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes 389b7190190e0ef8de883c952efb319ce7748831faaElliott Hughesstatic void Posix_chmod(JNIEnv* env, jobject, jstring javaPath, jint mode) { 390b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes ScopedUtfChars path(env, javaPath); 391b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes if (path.c_str() == NULL) { 392b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes return; 393b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes } 394b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes throwIfMinusOne(env, "chmod", TEMP_FAILURE_RETRY(chmod(path.c_str(), mode))); 395b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes} 396b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes 397462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughesstatic void Posix_close(JNIEnv* env, jobject, jobject javaFd) { 398462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // Get the FileDescriptor's 'fd' field and clear it. 399462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // We need to do this before we can throw an IOException (http://b/3222087). 400462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 401462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes jniSetFileDescriptorOfFD(env, javaFd, -1); 402462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes 403462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // Even if close(2) fails with EINTR, the fd will have been closed. 404462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone else's fd. 405462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html 406462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes throwIfMinusOne(env, "close", close(fd)); 407462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes} 408462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes 409996bf79565ac88402920bd826d6f85952c83be20Elliott Hughesstatic void Posix_connect(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) { 410996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes sockaddr_storage ss; 411996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes if (!inetAddressToSockaddr(env, javaAddress, port, &ss)) { 412996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes return; 413996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes } 414996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes int fd; 415996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss); 416996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes NET_FAILURE_RETRY("connect", connect(fd, sa, sizeof(sockaddr_storage))); 417996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes} 418996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes 419396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jobject Posix_dup(JNIEnv* env, jobject, jobject javaOldFd) { 420396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes int oldFd = jniGetFDFromFileDescriptor(env, javaOldFd); 421396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes int newFd = throwIfMinusOne(env, "dup", TEMP_FAILURE_RETRY(dup(oldFd))); 422396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return (newFd != -1) ? jniCreateFileDescriptor(env, newFd) : NULL; 423396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 424396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 425396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jobject Posix_dup2(JNIEnv* env, jobject, jobject javaOldFd, jint newFd) { 426396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes int oldFd = jniGetFDFromFileDescriptor(env, javaOldFd); 427396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes int fd = throwIfMinusOne(env, "dup2", TEMP_FAILURE_RETRY(dup2(oldFd, newFd))); 428396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return (fd != -1) ? jniCreateFileDescriptor(env, fd) : NULL; 429396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 430396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 431ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jobjectArray Posix_environ(JNIEnv* env, jobject) { 432ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes extern char** environ; // Standard, but not in any header file. 433ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return toStringArray(env, environ); 434ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 435ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 436fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlVoid(JNIEnv* env, jobject, jobject javaFd, jint cmd) { 437fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 438fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd))); 439fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes} 440fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 441fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlLong(JNIEnv* env, jobject, jobject javaFd, jint cmd, jlong arg) { 442fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 443fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, arg))); 444fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes} 445fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 446fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlFlock(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaFlock) { 447fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID typeFid = env->GetFieldID(JniConstants::structFlockClass, "l_type", "S"); 448fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID whenceFid = env->GetFieldID(JniConstants::structFlockClass, "l_whence", "S"); 449fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID startFid = env->GetFieldID(JniConstants::structFlockClass, "l_start", "J"); 450fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID lenFid = env->GetFieldID(JniConstants::structFlockClass, "l_len", "J"); 451fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID pidFid = env->GetFieldID(JniConstants::structFlockClass, "l_pid", "I"); 452fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 453fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes struct flock64 lock; 454fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes memset(&lock, 0, sizeof(lock)); 455fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_type = env->GetShortField(javaFlock, typeFid); 456fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_whence = env->GetShortField(javaFlock, whenceFid); 457fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_start = env->GetLongField(javaFlock, startFid); 458fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_len = env->GetLongField(javaFlock, lenFid); 459fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_pid = env->GetIntField(javaFlock, pidFid); 460fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 461fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 462fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes int rc = throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, &lock))); 463fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes if (rc != -1) { 464fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetShortField(javaFlock, typeFid, lock.l_type); 465fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetShortField(javaFlock, whenceFid, lock.l_whence); 466fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetLongField(javaFlock, startFid, lock.l_start); 467fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetLongField(javaFlock, lenFid, lock.l_len); 468fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetIntField(javaFlock, pidFid, lock.l_pid); 469fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes } 470fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes return rc; 471fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes} 472fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 47352724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fdatasync(JNIEnv* env, jobject, jobject javaFd) { 47452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 4757e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "fdatasync", TEMP_FAILURE_RETRY(fdatasync(fd))); 47652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes} 47752724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes 47847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_fstat(JNIEnv* env, jobject, jobject javaFd) { 47947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 48047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes struct stat sb; 48147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int rc = TEMP_FAILURE_RETRY(fstat(fd, &sb)); 482dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == -1) { 4837e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "fstat"); 48447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return NULL; 48547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes } 48647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return makeStructStat(env, sb); 48747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 48847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 48959fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject Posix_fstatfs(JNIEnv* env, jobject, jobject javaFd) { 49059fa7163774d6930a174bc038414a4b780581957Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 49159fa7163774d6930a174bc038414a4b780581957Elliott Hughes struct statfs sb; 49259fa7163774d6930a174bc038414a4b780581957Elliott Hughes int rc = TEMP_FAILURE_RETRY(fstatfs(fd, &sb)); 49359fa7163774d6930a174bc038414a4b780581957Elliott Hughes if (rc == -1) { 49459fa7163774d6930a174bc038414a4b780581957Elliott Hughes throwErrnoException(env, "fstatfs"); 49559fa7163774d6930a174bc038414a4b780581957Elliott Hughes return NULL; 49659fa7163774d6930a174bc038414a4b780581957Elliott Hughes } 49759fa7163774d6930a174bc038414a4b780581957Elliott Hughes return makeStructStatFs(env, sb); 49859fa7163774d6930a174bc038414a4b780581957Elliott Hughes} 49959fa7163774d6930a174bc038414a4b780581957Elliott Hughes 50052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fsync(JNIEnv* env, jobject, jobject javaFd) { 50152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 5027e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "fsync", TEMP_FAILURE_RETRY(fsync(fd))); 503f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes} 504f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes 505f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughesstatic void Posix_ftruncate(JNIEnv* env, jobject, jobject javaFd, jlong length) { 506f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 5077e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "ftruncate", TEMP_FAILURE_RETRY(ftruncate64(fd, length))); 50852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes} 50952724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes 5104f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic jstring Posix_gai_strerror(JNIEnv* env, jobject, jint error) { 5114f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return env->NewStringUTF(gai_strerror(error)); 5124f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 5134f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 5141c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughesstatic jobjectArray Posix_getaddrinfo(JNIEnv* env, jobject, jstring javaNode, jobject javaHints) { 5151c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes ScopedUtfChars node(env, javaNode); 5161c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (node.c_str() == NULL) { 5171c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 5181c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 5191c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 5201c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes static jfieldID flagsFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_flags", "I"); 5211c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes static jfieldID familyFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_family", "I"); 5221c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes static jfieldID socktypeFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_socktype", "I"); 5231c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes static jfieldID protocolFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_protocol", "I"); 5241c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 5251c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes addrinfo hints; 5261c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes memset(&hints, 0, sizeof(hints)); 5271c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes hints.ai_flags = env->GetIntField(javaHints, flagsFid); 5281c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes hints.ai_family = env->GetIntField(javaHints, familyFid); 5291c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes hints.ai_socktype = env->GetIntField(javaHints, socktypeFid); 5301c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes hints.ai_protocol = env->GetIntField(javaHints, protocolFid); 5311c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 5321c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes addrinfo* addressList = NULL; 5331c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes int rc = getaddrinfo(node.c_str(), NULL, &hints, &addressList); 5341c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes UniquePtr<addrinfo, addrinfo_deleter> addressListDeleter(addressList); 5351c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (rc != 0) { 5361c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes throwGaiException(env, "getaddrinfo", rc); 5371c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 5381c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 5391c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 5401c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes // Count results so we know how to size the output array. 5411c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes int addressCount = 0; 5421c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes for (addrinfo* ai = addressList; ai != NULL; ai = ai->ai_next) { 5431c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) { 5441c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes ++addressCount; 5451c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } else { 5461c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes LOGE("getaddrinfo unexpected ai_family %i", ai->ai_family); 5471c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 5481c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 5491c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (addressCount == 0) { 5501c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 5511c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 5521c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 5531c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes // Prepare output array. 5541c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes jobjectArray result = env->NewObjectArray(addressCount, JniConstants::inetAddressClass, NULL); 5551c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (result == NULL) { 5561c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 5571c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 5581c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 5591c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes // Examine returned addresses one by one, save them in the output array. 5601c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes int index = 0; 5611c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes for (addrinfo* ai = addressList; ai != NULL; ai = ai->ai_next) { 5621c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) { 5631c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes // Unknown address family. Skip this address. 5641c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes LOGE("getaddrinfo unexpected ai_family %i", ai->ai_family); 5651c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes continue; 5661c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 5671c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 5681c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes // Convert each IP address into a Java byte array. 5691c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes sockaddr_storage* address = reinterpret_cast<sockaddr_storage*>(ai->ai_addr); 5701c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes ScopedLocalRef<jobject> inetAddress(env, socketAddressToInetAddress(env, address)); 5711c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (inetAddress.get() == NULL) { 5721c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 5731c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 5741c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes env->SetObjectArrayElement(result, index, inetAddress.get()); 5751c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes ++index; 5761c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 5771c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return result; 5781c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes} 5791c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 580396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getegid(JNIEnv*, jobject) { 581396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return getegid(); 582396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 583396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 584396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_geteuid(JNIEnv*, jobject) { 585396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return geteuid(); 586396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 587396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 588396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getgid(JNIEnv*, jobject) { 589396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return getgid(); 590396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 591396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 592ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic jstring Posix_getenv(JNIEnv* env, jobject, jstring javaName) { 593ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes ScopedUtfChars name(env, javaName); 594ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes if (name.c_str() == NULL) { 595ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes return NULL; 596ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes } 597ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes return env->NewStringUTF(getenv(name.c_str())); 598ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes} 599ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 6004f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic jstring Posix_getnameinfo(JNIEnv* env, jobject, jobject javaAddress, jint flags) { 6014f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes sockaddr_storage ss; 602da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes if (!inetAddressToSockaddr_getnameinfo(env, javaAddress, 0, &ss)) { 6034f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return NULL; 6044f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes } 6054f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // TODO: bionic's getnameinfo(3) seems to want its length parameter to be exactly 6064f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // sizeof(sockaddr_in) for an IPv4 address and sizeof (sockaddr_in6) for an 6074f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // IPv6 address. Fix getnameinfo so it accepts sizeof(sockaddr_storage), and 6084f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // then remove this hack. 6094f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes socklen_t size = (ss.ss_family == AF_INET) ? sizeof(sockaddr_in) : sizeof(sockaddr_in6); 6104f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes char buf[NI_MAXHOST]; // NI_MAXHOST is longer than INET6_ADDRSTRLEN. 6114f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes int rc = getnameinfo(reinterpret_cast<sockaddr*>(&ss), size, buf, sizeof(buf), NULL, 0, flags); 6124f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes if (rc != 0) { 6134f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwGaiException(env, "getnameinfo", rc); 6144f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return NULL; 6154f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes } 6164f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return env->NewStringUTF(buf); 6174f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 6184f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 619396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getpid(JNIEnv*, jobject) { 620396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return getpid(); 621396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 622396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 623396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getppid(JNIEnv*, jobject) { 624396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return getppid(); 625396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 626396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 627d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesstatic jobject Posix_getpwnam(JNIEnv* env, jobject, jstring javaName) { 628d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes ScopedUtfChars name(env, javaName); 629d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes if (name.c_str() == NULL) { 630d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return NULL; 631d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 632d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return Passwd(env).getpwnam(name.c_str()); 633d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes} 634d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 635d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesstatic jobject Posix_getpwuid(JNIEnv* env, jobject, jint uid) { 636d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return Passwd(env).getpwuid(uid); 637d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes} 638d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 6390a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockname(JNIEnv* env, jobject, jobject javaFd) { 6400a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 6410a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes sockaddr_storage ss; 6420a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes sockaddr* sa = reinterpret_cast<sockaddr*>(&ss); 6430a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t byteCount = sizeof(ss); 6440a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes memset(&ss, 0, byteCount); 6450a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int rc = TEMP_FAILURE_RETRY(getsockname(fd, sa, &byteCount)); 6460a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (rc == -1) { 6470a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwErrnoException(env, "getsockname"); 6480a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 6490a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 6500a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return makeSocketAddress(env, &ss); 6510a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 6520a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 6530a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 6540a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 6550a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes u_char result = 0; 6560a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(result); 6570a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size))); 6580a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return result; 6590a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 6600a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 6610a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptInAddr(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 6620a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 6630a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes sockaddr_storage ss; 6640a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes memset(&ss, 0, sizeof(ss)); 6650a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes ss.ss_family = AF_INET; // This is only for the IPv4-only IP_MULTICAST_IF. 6660a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes sockaddr_in* sa = reinterpret_cast<sockaddr_in*>(&ss); 6670a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(sa->sin_addr); 6680a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &sa->sin_addr, &size)); 6690a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (rc == -1) { 6700a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwErrnoException(env, "getsockopt"); 6710a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 6720a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 6730a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return socketAddressToInetAddress(env, &ss); 6740a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 6750a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 6760a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 6770a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 6780a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes jint result = 0; 6790a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(result); 6800a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size))); 6810a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return result; 6820a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 6830a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 6840a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 6850a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 6860a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes struct linger l; 6870a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(l); 6880a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes memset(&l, 0, size); 6890a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &l, &size)); 6900a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (rc == -1) { 6910a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwErrnoException(env, "getsockopt"); 6920a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 6930a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 6940a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return makeStructLinger(env, l); 6950a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 6960a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 6970a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 6980a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 6990a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes struct timeval tv; 7000a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(tv); 7010a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes memset(&tv, 0, size); 7020a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &tv, &size)); 7030a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (rc == -1) { 7040a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwErrnoException(env, "getsockopt"); 7050a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 7060a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 7070a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return makeStructTimeval(env, tv); 7080a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 7090a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 710396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getuid(JNIEnv*, jobject) { 711396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return getuid(); 712396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 713396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 714a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic jstring Posix_if_indextoname(JNIEnv* env, jobject, jint index) { 715a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes char buf[IF_NAMESIZE]; 716a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes char* name = if_indextoname(index, buf); 717a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes // if_indextoname(3) returns NULL on failure, which will come out of NewStringUTF unscathed. 718a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes // There's no useful information in errno, so we don't bother throwing. Callers can null-check. 719a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return env->NewStringUTF(name); 720a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes} 721a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes 7221c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughesstatic jobject Posix_inet_aton(JNIEnv* env, jobject, jstring javaName) { 7231c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes ScopedUtfChars name(env, javaName); 7241c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (name.c_str() == NULL) { 7251c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 7261c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 7271c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes sockaddr_storage ss; 7281c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes memset(&ss, 0, sizeof(ss)); 7291c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(&ss); 7301c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (inet_aton(name.c_str(), &sin->sin_addr) == 0) { 7311c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 7321c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 7331c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes sin->sin_family = AF_INET; // inet_aton only supports IPv4. 7341c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return socketAddressToInetAddress(env, &ss); 7351c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes} 7361c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 737a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic jobject Posix_ioctlInetAddress(JNIEnv* env, jobject, jobject javaFd, jint cmd, jstring javaInterfaceName) { 738a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes struct ifreq req; 739a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (!fillIfreq(env, javaInterfaceName, req)) { 740a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return NULL; 741a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes } 742a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 743a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &req))); 744a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (rc == -1) { 745a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return NULL; 746a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes } 747a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return socketAddressToInetAddress(env, reinterpret_cast<sockaddr_storage*>(&req.ifr_addr)); 748a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes} 749a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes 7508b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jint Posix_ioctlInt(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaArg) { 7518b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes // This is complicated because ioctls may return their result by updating their argument 7528b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes // or via their return value, so we need to support both. 753461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 7548b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes static jfieldID valueFid = env->GetFieldID(JniConstants::mutableIntClass, "value", "I"); 7558b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes jint arg = env->GetIntField(javaArg, valueFid); 7568b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &arg))); 7578b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes if (!env->ExceptionCheck()) { 7588b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes env->SetIntField(javaArg, valueFid, arg); 7598b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes } 7608b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes return rc; 761461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes} 762461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes 7639a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughesstatic jboolean Posix_isatty(JNIEnv* env, jobject, jobject javaFd) { 7649a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 7659a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes return TEMP_FAILURE_RETRY(isatty(fd)) == 0; 7669a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes} 7679a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes 768a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughesstatic void Posix_kill(JNIEnv* env, jobject, jint pid, jint sig) { 769a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes throwIfMinusOne(env, "kill", TEMP_FAILURE_RETRY(kill(pid, sig))); 770a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes} 771a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes 772e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughesstatic void Posix_listen(JNIEnv* env, jobject, jobject javaFd, jint backlog) { 773e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 774e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes throwIfMinusOne(env, "listen", TEMP_FAILURE_RETRY(listen(fd, backlog))); 775e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes} 776e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes 777dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughesstatic jlong Posix_lseek(JNIEnv* env, jobject, jobject javaFd, jlong offset, jint whence) { 778dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 7797e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes return throwIfMinusOne(env, "lseek", TEMP_FAILURE_RETRY(lseek64(fd, offset, whence))); 780dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes} 781dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes 78247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_lstat(JNIEnv* env, jobject, jstring javaPath) { 78347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return doStat(env, javaPath, true); 78447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 78547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 7860f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughesstatic void Posix_mincore(JNIEnv* env, jobject, jlong address, jlong byteCount, jbyteArray javaVector) { 7870f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes ScopedByteArrayRW vector(env, javaVector); 7880f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes if (vector.get() == NULL) { 7890f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes return; 7900f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes } 7910f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 7920f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes unsigned char* vec = reinterpret_cast<unsigned char*>(vector.get()); 7930f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes throwIfMinusOne(env, "mincore", TEMP_FAILURE_RETRY(mincore(ptr, byteCount, vec))); 7940f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes} 7950f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes 796c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_mkdir(JNIEnv* env, jobject, jstring javaPath, jint mode) { 797c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes ScopedUtfChars path(env, javaPath); 798c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes if (path.c_str() == NULL) { 799c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes return; 800c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes } 801c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes throwIfMinusOne(env, "mkdir", TEMP_FAILURE_RETRY(mkdir(path.c_str(), mode))); 802c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes} 803c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes 8047e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_mlock(JNIEnv* env, jobject, jlong address, jlong byteCount) { 8057e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 8067e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes throwIfMinusOne(env, "mlock", TEMP_FAILURE_RETRY(mlock(ptr, byteCount))); 8077e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes} 8087e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes 8097e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic jlong Posix_mmap(JNIEnv* env, jobject, jlong address, jlong byteCount, jint prot, jint flags, jobject javaFd, jlong offset) { 8107e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 8117e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* suggestedPtr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 8127e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* ptr = mmap(suggestedPtr, byteCount, prot, flags, fd, offset); 8137e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes if (ptr == MAP_FAILED) { 8147e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "mmap"); 8157e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes } 8167e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes return static_cast<jlong>(reinterpret_cast<uintptr_t>(ptr)); 8177e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes} 8187e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes 8197e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_msync(JNIEnv* env, jobject, jlong address, jlong byteCount, jint flags) { 8207e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 8217e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "msync", TEMP_FAILURE_RETRY(msync(ptr, byteCount, flags))); 8227e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes} 8237e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes 8247e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_munlock(JNIEnv* env, jobject, jlong address, jlong byteCount) { 8257e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 8267e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes throwIfMinusOne(env, "munlock", TEMP_FAILURE_RETRY(munlock(ptr, byteCount))); 8277e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes} 8287e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes 8297e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_munmap(JNIEnv* env, jobject, jlong address, jlong byteCount) { 8307e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 8317e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "munmap", TEMP_FAILURE_RETRY(munmap(ptr, byteCount))); 8327e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes} 8337e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes 8340ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughesstatic jobject Posix_open(JNIEnv* env, jobject, jstring javaPath, jint flags, jint mode) { 8350ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes ScopedUtfChars path(env, javaPath); 8360ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes if (path.c_str() == NULL) { 8370ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes return NULL; 8380ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes } 8390ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes int fd = throwIfMinusOne(env, "open", TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode))); 8400ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL; 8410ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes} 8420ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes 84341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughesstatic jobjectArray Posix_pipe(JNIEnv* env, jobject) { 84441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes int fds[2]; 84541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes throwIfMinusOne(env, "pipe", TEMP_FAILURE_RETRY(pipe(&fds[0]))); 84641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes jobjectArray result = env->NewObjectArray(2, JniConstants::fileDescriptorClass, NULL); 84741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes if (result == NULL) { 84841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return NULL; 84941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 85041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes for (int i = 0; i < 2; ++i) { 85141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes ScopedLocalRef<jobject> fd(env, jniCreateFileDescriptor(env, fds[i])); 85241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes if (fd.get() == NULL) { 85341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return NULL; 85441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 85541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes env->SetObjectArrayElement(result, i, fd.get()); 85641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes if (env->ExceptionCheck()) { 85741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return NULL; 85841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 85941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 86041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return result; 86141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes} 86241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes 86370c820401677ca251ad09ac64cc23c760764e75dElliott Hughesstatic jint Posix_poll(JNIEnv* env, jobject, jobjectArray javaStructs, jint timeoutMs) { 86470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes static jfieldID fdFid = env->GetFieldID(JniConstants::structPollfdClass, "fd", "Ljava/io/FileDescriptor;"); 86570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes static jfieldID eventsFid = env->GetFieldID(JniConstants::structPollfdClass, "events", "S"); 86670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes static jfieldID reventsFid = env->GetFieldID(JniConstants::structPollfdClass, "revents", "S"); 86770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 86870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes // Turn the Java libcore.io.StructPollfd[] into a C++ struct pollfd[]. 86970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes size_t arrayLength = env->GetArrayLength(javaStructs); 87070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes UniquePtr<struct pollfd[]> fds(new struct pollfd[arrayLength]); 87170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes memset(fds.get(), 0, sizeof(struct pollfd) * arrayLength); 87270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes size_t count = 0; // Some trailing array elements may be irrelevant. (See below.) 87370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes for (size_t i = 0; i < arrayLength; ++i) { 87470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes ScopedLocalRef<jobject> javaStruct(env, env->GetObjectArrayElement(javaStructs, i)); 87570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes if (javaStruct.get() == NULL) { 87670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes break; // We allow trailing nulls in the array for caller convenience. 87770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 87870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes ScopedLocalRef<jobject> javaFd(env, env->GetObjectField(javaStruct.get(), fdFid)); 87970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes if (javaFd.get() == NULL) { 88070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes break; // We also allow callers to just clear the fd field (this is what Selector does). 88170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 88270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes fds[count].fd = jniGetFDFromFileDescriptor(env, javaFd.get()); 88370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes fds[count].events = env->GetShortField(javaStruct.get(), eventsFid); 88470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes ++count; 88570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 88670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 88770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes int rc = TEMP_FAILURE_RETRY(poll(fds.get(), count, timeoutMs)); 88870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes if (rc == -1) { 88970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes throwErrnoException(env, "poll"); 89070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes return -1; 89170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 89270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 89370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes // Update the revents fields in the Java libcore.io.StructPollfd[]. 89498b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes for (size_t i = 0; i < count; ++i) { 89570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes ScopedLocalRef<jobject> javaStruct(env, env->GetObjectArrayElement(javaStructs, i)); 89698b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes if (javaStruct.get() == NULL) { 89798b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes return -1; 89898b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes } 89970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes env->SetShortField(javaStruct.get(), reventsFid, fds[i].revents); 90070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 90170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes return rc; 90270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes} 90370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 9040568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughesstatic jint Posix_readBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount) { 9050568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes ScopedBytesRW bytes(env, javaBytes); 90626c7025a7a919044771fb89031161bd26fe03032Elliott Hughes if (bytes.get() == NULL) { 90726c7025a7a919044771fb89031161bd26fe03032Elliott Hughes return -1; 90826c7025a7a919044771fb89031161bd26fe03032Elliott Hughes } 90926c7025a7a919044771fb89031161bd26fe03032Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 9100568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes return throwIfMinusOne(env, "read", TEMP_FAILURE_RETRY(read(fd, bytes.get() + byteOffset, byteCount))); 91126c7025a7a919044771fb89031161bd26fe03032Elliott Hughes} 91226c7025a7a919044771fb89031161bd26fe03032Elliott Hughes 913bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_readv(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) { 9148dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes IoVec<ScopedBytesRW> ioVec(env, env->GetArrayLength(buffers)); 915bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (!ioVec.init(buffers, offsets, byteCounts)) { 916bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return -1; 917bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 918bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 919bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return throwIfMinusOne(env, "readv", TEMP_FAILURE_RETRY(readv(fd, ioVec.get(), ioVec.size()))); 920bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes} 921bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 922c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_remove(JNIEnv* env, jobject, jstring javaPath) { 923c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes ScopedUtfChars path(env, javaPath); 924c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes if (path.c_str() == NULL) { 925c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes return; 926c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes } 927c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes throwIfMinusOne(env, "remove", TEMP_FAILURE_RETRY(remove(path.c_str()))); 928c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes} 929c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes 930a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_rename(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) { 931a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars oldPath(env, javaOldPath); 932a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (oldPath.c_str() == NULL) { 933a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 934a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 935a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars newPath(env, javaNewPath); 936a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (newPath.c_str() == NULL) { 937a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 938a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 939a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes throwIfMinusOne(env, "rename", TEMP_FAILURE_RETRY(rename(oldPath.c_str(), newPath.c_str()))); 940a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes} 941a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes 9428b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jlong Posix_sendfile(JNIEnv* env, jobject, jobject javaOutFd, jobject javaInFd, jobject javaOffset, jlong byteCount) { 9438b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes int outFd = jniGetFDFromFileDescriptor(env, javaOutFd); 9448b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes int inFd = jniGetFDFromFileDescriptor(env, javaInFd); 9458b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes static jfieldID valueFid = env->GetFieldID(JniConstants::mutableLongClass, "value", "J"); 9468b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes off_t offset = 0; 9478b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes off_t* offsetPtr = NULL; 9488b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes if (javaOffset != NULL) { 9498b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes // TODO: fix bionic so we can have a 64-bit off_t! 9508b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes offset = env->GetLongField(javaOffset, valueFid); 9518b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes offsetPtr = &offset; 9528b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes } 9538b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes jlong result = throwIfMinusOne(env, "sendfile", TEMP_FAILURE_RETRY(sendfile(outFd, inFd, offsetPtr, byteCount))); 9548b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes if (javaOffset != NULL) { 9558b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes env->SetLongField(javaOffset, valueFid, offset); 9568b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes } 9578b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes return result; 9588b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes} 9598b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes 960396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_setegid(JNIEnv* env, jobject, jint egid) { 961396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes throwIfMinusOne(env, "setegid", TEMP_FAILURE_RETRY(setegid(egid))); 962396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 963396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 964396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_seteuid(JNIEnv* env, jobject, jint euid) { 965396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes throwIfMinusOne(env, "seteuid", TEMP_FAILURE_RETRY(seteuid(euid))); 966396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 967396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 968396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_setgid(JNIEnv* env, jobject, jint gid) { 969396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes throwIfMinusOne(env, "setgid", TEMP_FAILURE_RETRY(setgid(gid))); 970396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 971396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 972c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 973c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 974c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes u_char byte = value; 975c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &byte, sizeof(byte)))); 976c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes} 977c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes 978a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic void Posix_setsockoptIfreq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jstring javaInterfaceName) { 979a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes struct ifreq req; 980a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (!fillIfreq(env, javaInterfaceName, req)) { 981b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes return; 982b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes } 983b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 984b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req)))); 985b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes} 986b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes 987454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic void Posix_setsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 988454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 989454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 990454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes} 991454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes 992b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughesstatic void Posix_setsockoptIpMreqn(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 993b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes ip_mreqn req; 994b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes memset(&req, 0, sizeof(req)); 995b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes req.imr_ifindex = value; 996b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 997b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req)))); 998b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes} 999b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes 1000438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughesstatic void Posix_setsockoptGroupReq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaGroupReq) { 1001438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes struct group_req value; 1002438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes 1003438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes static jfieldID grInterfaceFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_interface", "I"); 1004438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes value.gr_interface = env->GetIntField(javaGroupReq, grInterfaceFid); 1005438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes // Get the IPv4 or IPv6 multicast address to join or leave. 1006438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes static jfieldID grGroupFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_group", "Ljava/net/InetAddress;"); 1007e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes ScopedLocalRef<jobject> javaGroup(env, env->GetObjectField(javaGroupReq, grGroupFid)); 1008e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes if (!inetAddressToSockaddr(env, javaGroup.get(), 0, &value.gr_group)) { 1009438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes return; 1010438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes } 1011438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes 1012438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1013438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes int rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))); 1014438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes if (rc == -1 && errno == EINVAL) { 1015438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes // Maybe we're a 32-bit binary talking to a 64-bit kernel? 1016438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes // glibc doesn't automatically handle this. 1017438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes struct GCC_HIDDEN group_req64 { 1018438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes uint32_t gr_interface; 1019438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes uint32_t my_padding; 1020438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes sockaddr_storage gr_group; 1021438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes }; 1022438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes group_req64 value64; 1023438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes value64.gr_interface = value.gr_interface; 1024438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes memcpy(&value64.gr_group, &value.gr_group, sizeof(value.gr_group)); 1025438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value64, sizeof(value64))); 1026438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes } 1027438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes throwIfMinusOne(env, "setsockopt", rc); 1028438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes} 1029438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes 1030c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaLinger) { 1031c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID lOnoffFid = env->GetFieldID(JniConstants::structLingerClass, "l_onoff", "I"); 1032c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID lLingerFid = env->GetFieldID(JniConstants::structLingerClass, "l_linger", "I"); 1033c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1034c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes struct linger value; 1035c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes value.l_onoff = env->GetIntField(javaLinger, lOnoffFid); 1036c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes value.l_linger = env->GetIntField(javaLinger, lLingerFid); 1037c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 1038c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes} 1039c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes 1040c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaTimeval) { 1041c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID tvSecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_sec", "J"); 1042c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID tvUsecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_usec", "J"); 1043c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1044c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes struct timeval value; 1045c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes value.tv_sec = env->GetIntField(javaTimeval, tvSecFid); 1046c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes value.tv_usec = env->GetIntField(javaTimeval, tvUsecFid); 1047c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 1048c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes} 1049c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes 1050396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_setuid(JNIEnv* env, jobject, jint uid) { 1051396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes throwIfMinusOne(env, "setuid", TEMP_FAILURE_RETRY(setuid(uid))); 1052396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 1053396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 105459e4744d27231f260271dbbca406e0cc39768116Elliott Hughesstatic void Posix_shutdown(JNIEnv* env, jobject, jobject javaFd, jint how) { 105559e4744d27231f260271dbbca406e0cc39768116Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 105659e4744d27231f260271dbbca406e0cc39768116Elliott Hughes throwIfMinusOne(env, "shutdown", TEMP_FAILURE_RETRY(shutdown(fd, how))); 105759e4744d27231f260271dbbca406e0cc39768116Elliott Hughes} 105859e4744d27231f260271dbbca406e0cc39768116Elliott Hughes 1059454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic jobject Posix_socket(JNIEnv* env, jobject, jint domain, jint type, jint protocol) { 1060454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes int fd = throwIfMinusOne(env, "socket", TEMP_FAILURE_RETRY(socket(domain, type, protocol))); 1061454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL; 1062454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes} 1063454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes 106447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_stat(JNIEnv* env, jobject, jstring javaPath) { 106547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return doStat(env, javaPath, false); 106647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 106747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 10680ade5524d972b9dfd69b0126aa4c35319c1b7fa4Elliott Hughesstatic jobject Posix_statfs(JNIEnv* env, jobject, jstring javaPath) { 106959fa7163774d6930a174bc038414a4b780581957Elliott Hughes ScopedUtfChars path(env, javaPath); 107059fa7163774d6930a174bc038414a4b780581957Elliott Hughes if (path.c_str() == NULL) { 107159fa7163774d6930a174bc038414a4b780581957Elliott Hughes return NULL; 107259fa7163774d6930a174bc038414a4b780581957Elliott Hughes } 107359fa7163774d6930a174bc038414a4b780581957Elliott Hughes struct statfs sb; 107459fa7163774d6930a174bc038414a4b780581957Elliott Hughes int rc = TEMP_FAILURE_RETRY(statfs(path.c_str(), &sb)); 107559fa7163774d6930a174bc038414a4b780581957Elliott Hughes if (rc == -1) { 107659fa7163774d6930a174bc038414a4b780581957Elliott Hughes throwErrnoException(env, "statfs"); 107759fa7163774d6930a174bc038414a4b780581957Elliott Hughes return NULL; 107859fa7163774d6930a174bc038414a4b780581957Elliott Hughes } 107959fa7163774d6930a174bc038414a4b780581957Elliott Hughes return makeStructStatFs(env, sb); 108059fa7163774d6930a174bc038414a4b780581957Elliott Hughes} 108159fa7163774d6930a174bc038414a4b780581957Elliott Hughes 1082ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jstring Posix_strerror(JNIEnv* env, jobject, jint errnum) { 108352724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes char buffer[BUFSIZ]; 1084ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes const char* message = jniStrError(errnum, buffer, sizeof(buffer)); 1085ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return env->NewStringUTF(message); 1086ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 1087ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 1088a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_symlink(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) { 1089a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars oldPath(env, javaOldPath); 1090a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (oldPath.c_str() == NULL) { 1091a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 1092a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 1093a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars newPath(env, javaNewPath); 1094a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (newPath.c_str() == NULL) { 1095a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 1096a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 1097a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes throwIfMinusOne(env, "symlink", TEMP_FAILURE_RETRY(symlink(oldPath.c_str(), newPath.c_str()))); 1098a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes} 1099a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes 11006fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughesstatic jlong Posix_sysconf(JNIEnv* env, jobject, jint name) { 11016fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes // Since -1 is a valid result from sysconf(3), detecting failure is a little more awkward. 11026fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes errno = 0; 11036fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes long result = sysconf(name); 11046fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes if (result == -1L && errno == EINVAL) { 11057e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "sysconf"); 11066fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes } 11076fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes return result; 11086fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes} 11096fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes 11107341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject Posix_uname(JNIEnv* env, jobject) { 11117341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes struct utsname buf; 11127341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes if (throwIfMinusOne(env, "uname", TEMP_FAILURE_RETRY(uname(&buf))) == -1) { 11137341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes return NULL; 11147341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes } 11157341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes return makeStructUtsname(env, buf); 11167341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes} 11177341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes 11180568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughesstatic jint Posix_writeBytes(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount) { 11190568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes ScopedBytesRO bytes(env, javaBytes); 112078c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes if (bytes.get() == NULL) { 112178c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes return -1; 112278c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes } 112378c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 11240568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes return throwIfMinusOne(env, "write", TEMP_FAILURE_RETRY(write(fd, bytes.get() + byteOffset, byteCount))); 112578c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes} 112678c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes 1127bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_writev(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) { 11288dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes IoVec<ScopedBytesRO> ioVec(env, env->GetArrayLength(buffers)); 1129bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (!ioVec.init(buffers, offsets, byteCounts)) { 1130bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return -1; 1131bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 1132bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1133bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return throwIfMinusOne(env, "writev", TEMP_FAILURE_RETRY(writev(fd, ioVec.get(), ioVec.size()))); 1134bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes} 1135bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 1136ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic JNINativeMethod gMethods[] = { 1137ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes NATIVE_METHOD(Posix, access, "(Ljava/lang/String;I)Z"), 1138da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes NATIVE_METHOD(Posix, bind, "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)V"), 1139b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes NATIVE_METHOD(Posix, chmod, "(Ljava/lang/String;I)V"), 1140462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes NATIVE_METHOD(Posix, close, "(Ljava/io/FileDescriptor;)V"), 1141996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes NATIVE_METHOD(Posix, connect, "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)V"), 1142396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, dup, "(Ljava/io/FileDescriptor;)Ljava/io/FileDescriptor;"), 1143396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, dup2, "(Ljava/io/FileDescriptor;I)Ljava/io/FileDescriptor;"), 1144ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes NATIVE_METHOD(Posix, environ, "()[Ljava/lang/String;"), 1145fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes NATIVE_METHOD(Posix, fcntlVoid, "(Ljava/io/FileDescriptor;I)I"), 1146fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes NATIVE_METHOD(Posix, fcntlLong, "(Ljava/io/FileDescriptor;IJ)I"), 1147fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes NATIVE_METHOD(Posix, fcntlFlock, "(Ljava/io/FileDescriptor;ILlibcore/io/StructFlock;)I"), 114852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes NATIVE_METHOD(Posix, fdatasync, "(Ljava/io/FileDescriptor;)V"), 114947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes NATIVE_METHOD(Posix, fstat, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStat;"), 115059fa7163774d6930a174bc038414a4b780581957Elliott Hughes NATIVE_METHOD(Posix, fstatfs, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStatFs;"), 115152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes NATIVE_METHOD(Posix, fsync, "(Ljava/io/FileDescriptor;)V"), 1152f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes NATIVE_METHOD(Posix, ftruncate, "(Ljava/io/FileDescriptor;J)V"), 11534f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes NATIVE_METHOD(Posix, gai_strerror, "(I)Ljava/lang/String;"), 11541c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes NATIVE_METHOD(Posix, getaddrinfo, "(Ljava/lang/String;Llibcore/io/StructAddrinfo;)[Ljava/net/InetAddress;"), 1155396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, getegid, "()I"), 1156396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, geteuid, "()I"), 1157396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, getgid, "()I"), 1158ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes NATIVE_METHOD(Posix, getenv, "(Ljava/lang/String;)Ljava/lang/String;"), 11594f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes NATIVE_METHOD(Posix, getnameinfo, "(Ljava/net/InetAddress;I)Ljava/lang/String;"), 1160396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, getpid, "()I"), 1161396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, getppid, "()I"), 1162d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes NATIVE_METHOD(Posix, getpwnam, "(Ljava/lang/String;)Llibcore/io/StructPasswd;"), 1163d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes NATIVE_METHOD(Posix, getpwuid, "(I)Llibcore/io/StructPasswd;"), 11640a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockname, "(Ljava/io/FileDescriptor;)Ljava/net/SocketAddress;"), 11650a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptByte, "(Ljava/io/FileDescriptor;II)I"), 11660a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptInAddr, "(Ljava/io/FileDescriptor;II)Ljava/net/InetAddress;"), 11670a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptInt, "(Ljava/io/FileDescriptor;II)I"), 11680a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptLinger, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructLinger;"), 11690a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptTimeval, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructTimeval;"), 1170396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, getuid, "()I"), 1171a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes NATIVE_METHOD(Posix, if_indextoname, "(I)Ljava/lang/String;"), 11721c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes NATIVE_METHOD(Posix, inet_aton, "(Ljava/lang/String;)Ljava/net/InetAddress;"), 1173a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes NATIVE_METHOD(Posix, ioctlInetAddress, "(Ljava/io/FileDescriptor;ILjava/lang/String;)Ljava/net/InetAddress;"), 11748b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes NATIVE_METHOD(Posix, ioctlInt, "(Ljava/io/FileDescriptor;ILlibcore/util/MutableInt;)I"), 11759a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes NATIVE_METHOD(Posix, isatty, "(Ljava/io/FileDescriptor;)Z"), 1176a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes NATIVE_METHOD(Posix, kill, "(II)V"), 1177e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes NATIVE_METHOD(Posix, listen, "(Ljava/io/FileDescriptor;I)V"), 1178dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes NATIVE_METHOD(Posix, lseek, "(Ljava/io/FileDescriptor;JI)J"), 117947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes NATIVE_METHOD(Posix, lstat, "(Ljava/lang/String;)Llibcore/io/StructStat;"), 11800f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes NATIVE_METHOD(Posix, mincore, "(JJ[B)V"), 1181c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes NATIVE_METHOD(Posix, mkdir, "(Ljava/lang/String;I)V"), 11827e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes NATIVE_METHOD(Posix, mlock, "(JJ)V"), 11837e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes NATIVE_METHOD(Posix, mmap, "(JJIILjava/io/FileDescriptor;J)J"), 11847e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes NATIVE_METHOD(Posix, msync, "(JJI)V"), 11857e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes NATIVE_METHOD(Posix, munlock, "(JJ)V"), 11867e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes NATIVE_METHOD(Posix, munmap, "(JJ)V"), 11870ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes NATIVE_METHOD(Posix, open, "(Ljava/lang/String;II)Ljava/io/FileDescriptor;"), 118841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes NATIVE_METHOD(Posix, pipe, "()[Ljava/io/FileDescriptor;"), 118970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes NATIVE_METHOD(Posix, poll, "([Llibcore/io/StructPollfd;I)I"), 11900568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes NATIVE_METHOD(Posix, readBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"), 1191bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes NATIVE_METHOD(Posix, readv, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"), 1192c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes NATIVE_METHOD(Posix, remove, "(Ljava/lang/String;)V"), 1193a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes NATIVE_METHOD(Posix, rename, "(Ljava/lang/String;Ljava/lang/String;)V"), 11948b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes NATIVE_METHOD(Posix, sendfile, "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Llibcore/util/MutableLong;J)J"), 1195396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, setegid, "(I)V"), 1196396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, seteuid, "(I)V"), 1197396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, setgid, "(I)V"), 1198c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes NATIVE_METHOD(Posix, setsockoptByte, "(Ljava/io/FileDescriptor;III)V"), 1199b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes NATIVE_METHOD(Posix, setsockoptIfreq, "(Ljava/io/FileDescriptor;IILjava/lang/String;)V"), 1200454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes NATIVE_METHOD(Posix, setsockoptInt, "(Ljava/io/FileDescriptor;III)V"), 1201b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes NATIVE_METHOD(Posix, setsockoptIpMreqn, "(Ljava/io/FileDescriptor;III)V"), 1202438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes NATIVE_METHOD(Posix, setsockoptGroupReq, "(Ljava/io/FileDescriptor;IILlibcore/io/StructGroupReq;)V"), 1203c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes NATIVE_METHOD(Posix, setsockoptLinger, "(Ljava/io/FileDescriptor;IILlibcore/io/StructLinger;)V"), 1204c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes NATIVE_METHOD(Posix, setsockoptTimeval, "(Ljava/io/FileDescriptor;IILlibcore/io/StructTimeval;)V"), 1205396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, setuid, "(I)V"), 120659e4744d27231f260271dbbca406e0cc39768116Elliott Hughes NATIVE_METHOD(Posix, shutdown, "(Ljava/io/FileDescriptor;I)V"), 1207454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes NATIVE_METHOD(Posix, socket, "(III)Ljava/io/FileDescriptor;"), 120847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes NATIVE_METHOD(Posix, stat, "(Ljava/lang/String;)Llibcore/io/StructStat;"), 120959fa7163774d6930a174bc038414a4b780581957Elliott Hughes NATIVE_METHOD(Posix, statfs, "(Ljava/lang/String;)Llibcore/io/StructStatFs;"), 1210ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes NATIVE_METHOD(Posix, strerror, "(I)Ljava/lang/String;"), 1211a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes NATIVE_METHOD(Posix, symlink, "(Ljava/lang/String;Ljava/lang/String;)V"), 12126fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes NATIVE_METHOD(Posix, sysconf, "(I)J"), 12137341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes NATIVE_METHOD(Posix, uname, "()Llibcore/io/StructUtsname;"), 12140568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes NATIVE_METHOD(Posix, writeBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"), 1215bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes NATIVE_METHOD(Posix, writev, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"), 1216ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes}; 1217ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesint register_libcore_io_Posix(JNIEnv* env) { 1218ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes return jniRegisterNativeMethods(env, "libcore/io/Posix", gMethods, NELEM(gMethods)); 1219ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes} 1220