libcore_io_Linux.cpp revision 763f8ed6195707d0c30bfae1ca8a3bb886b746cc
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" 246c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes#include "Portability.h" 250568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes#include "ScopedBytes.h" 26e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes#include "ScopedLocalRef.h" 270f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes#include "ScopedPrimitiveArray.h" 28ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "ScopedUtfChars.h" 2959fa7163774d6930a174bc038414a4b780581957Elliott Hughes#include "StaticAssert.h" 301c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes#include "UniquePtr.h" 31ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include "toStringArray.h" 32ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 331c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes#include <arpa/inet.h> 34ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include <errno.h> 350ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <fcntl.h> 36b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes#include <net/if.h> 374f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes#include <netdb.h> 380a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include <netinet/in.h> 391c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes#include <netinet/in.h> 4070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes#include <poll.h> 41d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes#include <pwd.h> 42a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes#include <signal.h> 43ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include <stdlib.h> 44461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes#include <sys/ioctl.h> 457e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes#include <sys/mman.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> 539e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes#include <sys/wait.h> 54bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig#include <termios.h> 550ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <unistd.h> 56ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 57d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes#define TO_JAVA_STRING(NAME, EXP) \ 58d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes jstring NAME = env->NewStringUTF(EXP); \ 59d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes if (NAME == NULL) return NULL; 60d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 611c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughesstruct addrinfo_deleter { 621c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes void operator()(addrinfo* p) const { 631c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (p != NULL) { // bionic's freeaddrinfo(3) crashes when passed NULL. 641c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes freeaddrinfo(p); 651c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 661c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 671c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes}; 681c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 69996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes/** 701124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes * Used to retry networking system calls that can return EINTR. Unlike TEMP_FAILURE_RETRY, 711124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes * this also handles the case where the reason for failure is that another thread called 721124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes * Socket.close. This macro also throws exceptions on failure. 73996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * 74996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * Returns the result of 'exp', though a Java exception will be pending if the result is -1. 75996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes */ 761124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes#define NET_FAILURE_RETRY(jni_env, return_type, syscall_name, java_fd, ...) ({ \ 771124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes return_type _rc = -1; \ 78996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes do { \ 79996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes { \ 801124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes int _fd = jniGetFDFromFileDescriptor(jni_env, java_fd); \ 811124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes AsynchronousSocketCloseMonitor _monitor(_fd); \ 821124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes _rc = syscall_name(_fd, __VA_ARGS__); \ 83996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes } \ 84996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes if (_rc == -1) { \ 851124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes if (jniGetFDFromFileDescriptor(jni_env, java_fd) == -1) { \ 861124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes jniThrowException(jni_env, "java/net/SocketException", "Socket closed"); \ 87996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes break; \ 88996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes } else if (errno != EINTR) { \ 891124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes /* TODO: with a format string we could show the arguments too, like strace(1). */ \ 901124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes throwErrnoException(jni_env, # syscall_name); \ 91996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes break; \ 92996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes } \ 93996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes } \ 94996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes } while (_rc == -1); \ 95996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes _rc; }) 96996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes 974f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwException(JNIEnv* env, jclass exceptionClass, jmethodID ctor3, jmethodID ctor2, 984f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes const char* functionName, int error) { 99ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes jthrowable cause = NULL; 100ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes if (env->ExceptionCheck()) { 101ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes cause = env->ExceptionOccurred(); 102ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes env->ExceptionClear(); 103ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } 104ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 1054f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName)); 1064f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes if (detailMessage.get() == NULL) { 107f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes // Not really much we can do here. We're probably dead in the water, 108f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes // but let's try to stumble on... 109f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes env->ExceptionClear(); 110f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes } 111f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes 112ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes jobject exception; 113ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes if (cause != NULL) { 1144f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes exception = env->NewObject(exceptionClass, ctor3, detailMessage.get(), error, cause); 115ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } else { 1164f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes exception = env->NewObject(exceptionClass, ctor2, detailMessage.get(), error); 117ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } 118ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes env->Throw(reinterpret_cast<jthrowable>(exception)); 119dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes} 120dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes 1214f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwErrnoException(JNIEnv* env, const char* functionName) { 1224f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes int error = errno; 1234f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes static jmethodID ctor3 = env->GetMethodID(JniConstants::errnoExceptionClass, 1244f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V"); 1254f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes static jmethodID ctor2 = env->GetMethodID(JniConstants::errnoExceptionClass, 1264f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes "<init>", "(Ljava/lang/String;I)V"); 1274f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwException(env, JniConstants::errnoExceptionClass, ctor3, ctor2, functionName, error); 1284f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 1294f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 1304f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwGaiException(JNIEnv* env, const char* functionName, int error) { 131c1cee0db58ec7bc61d5caac79915d744862cbe75Elliott Hughes // Cache the methods ids before we throw, so we don't call GetMethodID with a pending exception. 132c1cee0db58ec7bc61d5caac79915d744862cbe75Elliott Hughes static jmethodID ctor3 = env->GetMethodID(JniConstants::gaiExceptionClass, "<init>", 133c1cee0db58ec7bc61d5caac79915d744862cbe75Elliott Hughes "(Ljava/lang/String;ILjava/lang/Throwable;)V"); 134c1cee0db58ec7bc61d5caac79915d744862cbe75Elliott Hughes static jmethodID ctor2 = env->GetMethodID(JniConstants::gaiExceptionClass, "<init>", 135c1cee0db58ec7bc61d5caac79915d744862cbe75Elliott Hughes "(Ljava/lang/String;I)V"); 136c1cee0db58ec7bc61d5caac79915d744862cbe75Elliott Hughes if (errno != 0) { 13775cf14944d476670f6f915e5efd849e238a16250Elliott Hughes // EAI_SYSTEM should mean "look at errno instead", but both glibc and bionic seem to 13875cf14944d476670f6f915e5efd849e238a16250Elliott Hughes // mess this up. In particular, if you don't have INTERNET permission, errno will be EACCES 13975cf14944d476670f6f915e5efd849e238a16250Elliott Hughes // but you'll get EAI_NONAME or EAI_NODATA. So we want our GaiException to have a 14075cf14944d476670f6f915e5efd849e238a16250Elliott Hughes // potentially-relevant ErrnoException as its cause even if error != EAI_SYSTEM. 14175cf14944d476670f6f915e5efd849e238a16250Elliott Hughes // http://code.google.com/p/android/issues/detail?id=15722 1424f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwErrnoException(env, functionName); 1434f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // Deliberately fall through to throw another exception... 1444f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes } 1454f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwException(env, JniConstants::gaiExceptionClass, ctor3, ctor2, functionName, error); 1464f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 1474f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 148dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughestemplate <typename rc_t> 1497e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic rc_t throwIfMinusOne(JNIEnv* env, const char* name, rc_t rc) { 150dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == rc_t(-1)) { 1517e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, name); 152dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes } 153dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes return rc; 15447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 15547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 1568dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughestemplate <typename ScopedT> 157bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesclass IoVec { 158bbac92e691de7d570928ddfba639067978e55b06Elliott Hughespublic: 159bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes IoVec(JNIEnv* env, size_t bufferCount) : mEnv(env), mBufferCount(bufferCount) { 160bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 161bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 162bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes bool init(jobjectArray javaBuffers, jintArray javaOffsets, jintArray javaByteCounts) { 163bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes // We can't delete our local references until after the I/O, so make sure we have room. 164bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (mEnv->PushLocalFrame(mBufferCount + 16) < 0) { 165bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 166bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 167bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes ScopedIntArrayRO offsets(mEnv, javaOffsets); 168bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (offsets.get() == NULL) { 169bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 170bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 171bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes ScopedIntArrayRO byteCounts(mEnv, javaByteCounts); 172bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (byteCounts.get() == NULL) { 173bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 174bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 175bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes // TODO: Linux actually has a 1024 buffer limit. glibc works around this, and we should too. 176d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes // TODO: you can query the limit at runtime with sysconf(_SC_IOV_MAX). 177bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes for (size_t i = 0; i < mBufferCount; ++i) { 178e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes jobject buffer = mEnv->GetObjectArrayElement(javaBuffers, i); // We keep this local ref. 1798dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes mScopedBuffers.push_back(new ScopedT(mEnv, buffer)); 1808dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes jbyte* ptr = const_cast<jbyte*>(mScopedBuffers.back()->get()); 181bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (ptr == NULL) { 182bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 183bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 184bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes struct iovec iov; 185bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes iov.iov_base = reinterpret_cast<void*>(ptr + offsets[i]); 186bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes iov.iov_len = byteCounts[i]; 187bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes mIoVec.push_back(iov); 188bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 189bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return true; 190bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 191bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 192bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes ~IoVec() { 1938dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes for (size_t i = 0; i < mScopedBuffers.size(); ++i) { 1948dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes delete mScopedBuffers[i]; 195bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 196bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes mEnv->PopLocalFrame(NULL); 197bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 198bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 199bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes iovec* get() { 200bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return &mIoVec[0]; 201bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 202bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 203bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes size_t size() { 204bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return mBufferCount; 205bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 206bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 207bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesprivate: 208bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes JNIEnv* mEnv; 209bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes size_t mBufferCount; 210bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes std::vector<iovec> mIoVec; 2118dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes std::vector<ScopedT*> mScopedBuffers; 212bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes}; 213bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 214482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughesstatic jobject makeSocketAddress(JNIEnv* env, const sockaddr_storage& ss) { 21523ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes jint port; 21623ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes jobject inetAddress = sockaddrToInetAddress(env, ss, &port); 2170a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (inetAddress == NULL) { 2180a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 2190a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 2200a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::inetSocketAddressClass, "<init>", 2210a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes "(Ljava/net/InetAddress;I)V"); 2220a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return env->NewObject(JniConstants::inetSocketAddressClass, ctor, inetAddress, port); 2230a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 2240a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 225d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesstatic jobject makeStructPasswd(JNIEnv* env, const struct passwd& pw) { 226d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(pw_name, pw.pw_name); 227d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(pw_dir, pw.pw_dir); 228d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(pw_shell, pw.pw_shell); 229d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structPasswdClass, "<init>", 230d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes "(Ljava/lang/String;IILjava/lang/String;Ljava/lang/String;)V"); 231d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return env->NewObject(JniConstants::structPasswdClass, ctor, 232d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes pw_name, static_cast<jint>(pw.pw_uid), static_cast<jint>(pw.pw_gid), pw_dir, pw_shell); 233d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes} 234d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 23547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject makeStructStat(JNIEnv* env, const struct stat& sb) { 23647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structStatClass, "<init>", 23747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes "(JJIJIIJJJJJJJ)V"); 23847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return env->NewObject(JniConstants::structStatClass, ctor, 23959fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_dev), static_cast<jlong>(sb.st_ino), 24059fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jint>(sb.st_mode), static_cast<jlong>(sb.st_nlink), 24159fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jint>(sb.st_uid), static_cast<jint>(sb.st_gid), 24259fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_rdev), static_cast<jlong>(sb.st_size), 24359fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_atime), static_cast<jlong>(sb.st_mtime), 24459fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_ctime), static_cast<jlong>(sb.st_blksize), 24559fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_blocks)); 24659fa7163774d6930a174bc038414a4b780581957Elliott Hughes} 24759fa7163774d6930a174bc038414a4b780581957Elliott Hughes 24859fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject makeStructStatFs(JNIEnv* env, const struct statfs& sb) { 2496c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes#if defined(__APPLE__) 2506c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes // Mac OS has no f_namelen field in struct statfs. 2516c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes jlong max_name_length = 255; // __DARWIN_MAXNAMLEN 2526c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes#else 2536c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes // Until Mac OS 10.7, these were 32-bit fields. 25459fa7163774d6930a174bc038414a4b780581957Elliott Hughes STATIC_ASSERT(sizeof(sb.f_bavail) == sizeof(jlong), statfs_not_64_bit); 25559fa7163774d6930a174bc038414a4b780581957Elliott Hughes STATIC_ASSERT(sizeof(sb.f_bfree) == sizeof(jlong), statfs_not_64_bit); 25659fa7163774d6930a174bc038414a4b780581957Elliott Hughes STATIC_ASSERT(sizeof(sb.f_blocks) == sizeof(jlong), statfs_not_64_bit); 25759fa7163774d6930a174bc038414a4b780581957Elliott Hughes 2586c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes jlong max_name_length = static_cast<jlong>(sb.f_namelen); 2596c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes#endif 2606c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes 26159fa7163774d6930a174bc038414a4b780581957Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structStatFsClass, "<init>", 26259fa7163774d6930a174bc038414a4b780581957Elliott Hughes "(JJJJJJJJ)V"); 26359fa7163774d6930a174bc038414a4b780581957Elliott Hughes return env->NewObject(JniConstants::structStatFsClass, ctor, static_cast<jlong>(sb.f_bsize), 26459fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.f_blocks), static_cast<jlong>(sb.f_bfree), 26559fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.f_bavail), static_cast<jlong>(sb.f_files), 2666c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes static_cast<jlong>(sb.f_ffree), max_name_length, 26759fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.f_frsize)); 26847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 26947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 2700a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructLinger(JNIEnv* env, const struct linger& l) { 2710a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structLingerClass, "<init>", "(II)V"); 2720a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return env->NewObject(JniConstants::structLingerClass, ctor, l.l_onoff, l.l_linger); 2730a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 2740a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 2750a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructTimeval(JNIEnv* env, const struct timeval& tv) { 2760a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structTimevalClass, "<init>", "(JJ)V"); 2770a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return env->NewObject(JniConstants::structTimevalClass, ctor, 2780a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static_cast<jlong>(tv.tv_sec), static_cast<jlong>(tv.tv_usec)); 2790a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 2800a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 281482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughesstatic jobject makeStructUcred(JNIEnv* env, const struct ucred& u) { 282482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structUcredClass, "<init>", "(III)V"); 283482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return env->NewObject(JniConstants::structUcredClass, ctor, u.pid, u.uid, u.gid); 284482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes} 285482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes 2867341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject makeStructUtsname(JNIEnv* env, const struct utsname& buf) { 287d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(sysname, buf.sysname); 288d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(nodename, buf.nodename); 289d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(release, buf.release); 290d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(version, buf.version); 291d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(machine, buf.machine); 2927341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structUtsnameClass, "<init>", 2937341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); 2947341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes return env->NewObject(JniConstants::structUtsnameClass, ctor, 2957341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes sysname, nodename, release, version, machine); 2967341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes}; 2977341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes 298a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic bool fillIfreq(JNIEnv* env, jstring javaInterfaceName, struct ifreq& req) { 299a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes ScopedUtfChars interfaceName(env, javaInterfaceName); 300a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (interfaceName.c_str() == NULL) { 301a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return false; 302a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes } 303a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes memset(&req, 0, sizeof(req)); 304a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes strncpy(req.ifr_name, interfaceName.c_str(), sizeof(req.ifr_name)); 305a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes req.ifr_name[sizeof(req.ifr_name) - 1] = '\0'; 306a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return true; 307a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes} 308a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes 309482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughesstatic bool fillInetSocketAddress(JNIEnv* env, jint rc, jobject javaInetSocketAddress, const sockaddr_storage& ss) { 310553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes if (rc == -1 || javaInetSocketAddress == NULL) { 311553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes return true; 312553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes } 313553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes // Fill out the passed-in InetSocketAddress with the sender's IP address and port number. 314553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes jint port; 315553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes jobject sender = sockaddrToInetAddress(env, ss, &port); 316553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes if (sender == NULL) { 317553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes return false; 318553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes } 319553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes static jfieldID addressFid = env->GetFieldID(JniConstants::inetSocketAddressClass, "addr", "Ljava/net/InetAddress;"); 320553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes static jfieldID portFid = env->GetFieldID(JniConstants::inetSocketAddressClass, "port", "I"); 321553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes env->SetObjectField(javaInetSocketAddress, addressFid, sender); 322553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes env->SetIntField(javaInetSocketAddress, portFid, port); 323553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes return true; 324553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes} 325553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes 32647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject doStat(JNIEnv* env, jstring javaPath, bool isLstat) { 32747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes ScopedUtfChars path(env, javaPath); 32847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes if (path.c_str() == NULL) { 32947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return NULL; 33047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes } 33147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes struct stat sb; 33247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int rc = isLstat ? TEMP_FAILURE_RETRY(lstat(path.c_str(), &sb)) 33347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes : TEMP_FAILURE_RETRY(stat(path.c_str(), &sb)); 334dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == -1) { 3357e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, isLstat ? "lstat" : "stat"); 33647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return NULL; 33747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes } 33847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return makeStructStat(env, sb); 339ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 340ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 341482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughesstatic jobject doGetSockName(JNIEnv* env, jobject javaFd, bool is_sockname) { 342482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 343482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes sockaddr_storage ss; 344482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes sockaddr* sa = reinterpret_cast<sockaddr*>(&ss); 345482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes socklen_t byteCount = sizeof(ss); 346482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes memset(&ss, 0, byteCount); 347482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes int rc = is_sockname ? TEMP_FAILURE_RETRY(getsockname(fd, sa, &byteCount)) 348482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes : TEMP_FAILURE_RETRY(getpeername(fd, sa, &byteCount)); 349482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes if (rc == -1) { 350482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes throwErrnoException(env, is_sockname ? "getsockname" : "getpeername"); 351482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return NULL; 352482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes } 353482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return makeSocketAddress(env, ss); 354482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes} 355482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes 356d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesclass Passwd { 357d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughespublic: 358d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes Passwd(JNIEnv* env) : mEnv(env), mResult(NULL) { 359d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes mBufferSize = sysconf(_SC_GETPW_R_SIZE_MAX); 360d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes if (mBufferSize == -1UL) { 361d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes // We're probably on bionic, where 1KiB should be enough for anyone. 362d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes // TODO: fix bionic to return 1024 like glibc. 363d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes mBufferSize = 1024; 364d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 365d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes mBuffer.reset(new char[mBufferSize]); 366d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 367d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 368d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes jobject getpwnam(const char* name) { 369d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return process("getpwnam_r", getpwnam_r(name, &mPwd, mBuffer.get(), mBufferSize, &mResult)); 370d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 371d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 372d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes jobject getpwuid(uid_t uid) { 373d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return process("getpwuid_r", getpwuid_r(uid, &mPwd, mBuffer.get(), mBufferSize, &mResult)); 374d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 375d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 376d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes struct passwd* get() { 377d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return mResult; 378d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 379d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 380d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesprivate: 381d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes jobject process(const char* syscall, int error) { 382d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes if (mResult == NULL) { 383d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes errno = error; 384d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes throwErrnoException(mEnv, syscall); 385d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return NULL; 386d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 387d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return makeStructPasswd(mEnv, *mResult); 388d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 389d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 390d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes JNIEnv* mEnv; 391d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes UniquePtr<char[]> mBuffer; 392d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes size_t mBufferSize; 393d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes struct passwd mPwd; 394d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes struct passwd* mResult; 395d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes}; 396d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 397553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughesstatic jobject Posix_accept(JNIEnv* env, jobject, jobject javaFd, jobject javaInetSocketAddress) { 398553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes sockaddr_storage ss; 399553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes socklen_t sl = sizeof(ss); 400553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes memset(&ss, 0, sizeof(ss)); 401553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes sockaddr* peer = (javaInetSocketAddress != NULL) ? reinterpret_cast<sockaddr*>(&ss) : NULL; 402553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes socklen_t* peerLength = (javaInetSocketAddress != NULL) ? &sl : 0; 4031124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes jint clientFd = NET_FAILURE_RETRY(env, int, accept, javaFd, peer, peerLength); 404482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes if (clientFd == -1 || !fillInetSocketAddress(env, clientFd, javaInetSocketAddress, ss)) { 405553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes close(clientFd); 406553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes return NULL; 407553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes } 408553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes return (clientFd != -1) ? jniCreateFileDescriptor(env, clientFd) : NULL; 409553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes} 410553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes 411ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jboolean Posix_access(JNIEnv* env, jobject, jstring javaPath, jint mode) { 412ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes ScopedUtfChars path(env, javaPath); 413ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes if (path.c_str() == NULL) { 414ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return JNI_FALSE; 415ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } 41647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int rc = TEMP_FAILURE_RETRY(access(path.c_str(), mode)); 417dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == -1) { 4187e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "access"); 419dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes } 420ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return (rc == 0); 421ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 422ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 423da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughesstatic void Posix_bind(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) { 424da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes sockaddr_storage ss; 425482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes socklen_t sa_len; 426482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes if (!inetAddressToSockaddr(env, javaAddress, port, ss, sa_len)) { 427da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes return; 428da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes } 429da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss); 4301124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes // We don't need the return value because we'll already have thrown. 431482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes (void) NET_FAILURE_RETRY(env, int, bind, javaFd, sa, sa_len); 432da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes} 433da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes 434b7190190e0ef8de883c952efb319ce7748831faaElliott Hughesstatic void Posix_chmod(JNIEnv* env, jobject, jstring javaPath, jint mode) { 435b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes ScopedUtfChars path(env, javaPath); 436b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes if (path.c_str() == NULL) { 437b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes return; 438b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes } 439b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes throwIfMinusOne(env, "chmod", TEMP_FAILURE_RETRY(chmod(path.c_str(), mode))); 440b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes} 441b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes 44244f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughesstatic void Posix_chown(JNIEnv* env, jobject, jstring javaPath, jint uid, jint gid) { 44344f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes ScopedUtfChars path(env, javaPath); 44444f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes if (path.c_str() == NULL) { 44544f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes return; 44644f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes } 44744f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes throwIfMinusOne(env, "chown", TEMP_FAILURE_RETRY(chown(path.c_str(), uid, gid))); 44844f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes} 44944f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes 450462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughesstatic void Posix_close(JNIEnv* env, jobject, jobject javaFd) { 451462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // Get the FileDescriptor's 'fd' field and clear it. 452462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // We need to do this before we can throw an IOException (http://b/3222087). 453462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 454462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes jniSetFileDescriptorOfFD(env, javaFd, -1); 455462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes 456462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // Even if close(2) fails with EINTR, the fd will have been closed. 457462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone else's fd. 458462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html 459462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes throwIfMinusOne(env, "close", close(fd)); 460462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes} 461462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes 462996bf79565ac88402920bd826d6f85952c83be20Elliott Hughesstatic void Posix_connect(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) { 463996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes sockaddr_storage ss; 464482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes socklen_t sa_len; 465482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes if (!inetAddressToSockaddr(env, javaAddress, port, ss, sa_len)) { 466996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes return; 467996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes } 468996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss); 4691124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes // We don't need the return value because we'll already have thrown. 470482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes (void) NET_FAILURE_RETRY(env, int, connect, javaFd, sa, sa_len); 471996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes} 472996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes 473396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jobject Posix_dup(JNIEnv* env, jobject, jobject javaOldFd) { 474396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes int oldFd = jniGetFDFromFileDescriptor(env, javaOldFd); 475396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes int newFd = throwIfMinusOne(env, "dup", TEMP_FAILURE_RETRY(dup(oldFd))); 476396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return (newFd != -1) ? jniCreateFileDescriptor(env, newFd) : NULL; 477396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 478396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 479396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jobject Posix_dup2(JNIEnv* env, jobject, jobject javaOldFd, jint newFd) { 480396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes int oldFd = jniGetFDFromFileDescriptor(env, javaOldFd); 481396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes int fd = throwIfMinusOne(env, "dup2", TEMP_FAILURE_RETRY(dup2(oldFd, newFd))); 482396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return (fd != -1) ? jniCreateFileDescriptor(env, fd) : NULL; 483396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 484396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 485ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jobjectArray Posix_environ(JNIEnv* env, jobject) { 486ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes extern char** environ; // Standard, but not in any header file. 487ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return toStringArray(env, environ); 488ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 489ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 490798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevichstatic void Posix_execve(JNIEnv* env, jobject, jstring javaFilename, jobjectArray javaArgv, jobjectArray javaEnvp) { 491798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich ScopedUtfChars path(env, javaFilename); 492798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich if (path.c_str() == NULL) { 493798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich return; 494798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich } 495798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich 496798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich char** argv = convertStrings(env, javaArgv); 497798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich char** envp = convertStrings(env, javaEnvp); 498798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich execve(path.c_str(), argv, envp); 499798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich 500798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich freeStrings(env, javaArgv, argv); 501798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich freeStrings(env, javaEnvp, envp); 502798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich throwErrnoException(env, "execve"); 503798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich} 504798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich 505798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevichstatic void Posix_execv(JNIEnv* env, jobject, jstring javaFilename, jobjectArray javaArgv) { 506798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich ScopedUtfChars path(env, javaFilename); 507798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich if (path.c_str() == NULL) { 508798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich return; 509798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich } 510798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich 511798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich char** argv = convertStrings(env, javaArgv); 512798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich execv(path.c_str(), argv); 513798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich 514798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich freeStrings(env, javaArgv, argv); 515798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich throwErrnoException(env, "execv"); 516798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich} 517798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich 51844f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughesstatic void Posix_fchmod(JNIEnv* env, jobject, jobject javaFd, jint mode) { 51944f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 52044f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes throwIfMinusOne(env, "fchmod", TEMP_FAILURE_RETRY(fchmod(fd, mode))); 52144f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes} 52244f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes 52344f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughesstatic void Posix_fchown(JNIEnv* env, jobject, jobject javaFd, jint uid, jint gid) { 52444f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 52544f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes throwIfMinusOne(env, "fchown", TEMP_FAILURE_RETRY(fchown(fd, uid, gid))); 52644f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes} 52744f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes 528fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlVoid(JNIEnv* env, jobject, jobject javaFd, jint cmd) { 529fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 530fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd))); 531fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes} 532fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 533fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlLong(JNIEnv* env, jobject, jobject javaFd, jint cmd, jlong arg) { 534fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 535fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, arg))); 536fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes} 537fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 538fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlFlock(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaFlock) { 539fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID typeFid = env->GetFieldID(JniConstants::structFlockClass, "l_type", "S"); 540fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID whenceFid = env->GetFieldID(JniConstants::structFlockClass, "l_whence", "S"); 541fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID startFid = env->GetFieldID(JniConstants::structFlockClass, "l_start", "J"); 542fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID lenFid = env->GetFieldID(JniConstants::structFlockClass, "l_len", "J"); 543fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID pidFid = env->GetFieldID(JniConstants::structFlockClass, "l_pid", "I"); 544fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 545fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes struct flock64 lock; 546fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes memset(&lock, 0, sizeof(lock)); 547fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_type = env->GetShortField(javaFlock, typeFid); 548fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_whence = env->GetShortField(javaFlock, whenceFid); 549fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_start = env->GetLongField(javaFlock, startFid); 550fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_len = env->GetLongField(javaFlock, lenFid); 551fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_pid = env->GetIntField(javaFlock, pidFid); 552fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 553fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 554fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes int rc = throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, &lock))); 555fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes if (rc != -1) { 556fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetShortField(javaFlock, typeFid, lock.l_type); 557fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetShortField(javaFlock, whenceFid, lock.l_whence); 558fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetLongField(javaFlock, startFid, lock.l_start); 559fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetLongField(javaFlock, lenFid, lock.l_len); 560fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetIntField(javaFlock, pidFid, lock.l_pid); 561fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes } 562fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes return rc; 563fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes} 564fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 56552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fdatasync(JNIEnv* env, jobject, jobject javaFd) { 56652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 5677e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "fdatasync", TEMP_FAILURE_RETRY(fdatasync(fd))); 56852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes} 56952724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes 57047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_fstat(JNIEnv* env, jobject, jobject javaFd) { 57147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 57247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes struct stat sb; 57347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int rc = TEMP_FAILURE_RETRY(fstat(fd, &sb)); 574dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == -1) { 5757e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "fstat"); 57647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return NULL; 57747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes } 57847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return makeStructStat(env, sb); 57947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 58047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 58159fa7163774d6930a174bc038414a4b780581957Elliott Hughesstatic jobject Posix_fstatfs(JNIEnv* env, jobject, jobject javaFd) { 58259fa7163774d6930a174bc038414a4b780581957Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 58359fa7163774d6930a174bc038414a4b780581957Elliott Hughes struct statfs sb; 58459fa7163774d6930a174bc038414a4b780581957Elliott Hughes int rc = TEMP_FAILURE_RETRY(fstatfs(fd, &sb)); 58559fa7163774d6930a174bc038414a4b780581957Elliott Hughes if (rc == -1) { 58659fa7163774d6930a174bc038414a4b780581957Elliott Hughes throwErrnoException(env, "fstatfs"); 58759fa7163774d6930a174bc038414a4b780581957Elliott Hughes return NULL; 58859fa7163774d6930a174bc038414a4b780581957Elliott Hughes } 58959fa7163774d6930a174bc038414a4b780581957Elliott Hughes return makeStructStatFs(env, sb); 59059fa7163774d6930a174bc038414a4b780581957Elliott Hughes} 59159fa7163774d6930a174bc038414a4b780581957Elliott Hughes 59252724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fsync(JNIEnv* env, jobject, jobject javaFd) { 59352724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 5947e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "fsync", TEMP_FAILURE_RETRY(fsync(fd))); 595f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes} 596f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes 597f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughesstatic void Posix_ftruncate(JNIEnv* env, jobject, jobject javaFd, jlong length) { 598f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 5997e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "ftruncate", TEMP_FAILURE_RETRY(ftruncate64(fd, length))); 60052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes} 60152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes 6024f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic jstring Posix_gai_strerror(JNIEnv* env, jobject, jint error) { 6034f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return env->NewStringUTF(gai_strerror(error)); 6044f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 6054f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 6061c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughesstatic jobjectArray Posix_getaddrinfo(JNIEnv* env, jobject, jstring javaNode, jobject javaHints) { 6071c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes ScopedUtfChars node(env, javaNode); 6081c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (node.c_str() == NULL) { 6091c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 6101c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 6111c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 6121c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes static jfieldID flagsFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_flags", "I"); 6131c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes static jfieldID familyFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_family", "I"); 6141c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes static jfieldID socktypeFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_socktype", "I"); 6151c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes static jfieldID protocolFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_protocol", "I"); 6161c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 6171c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes addrinfo hints; 6181c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes memset(&hints, 0, sizeof(hints)); 6191c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes hints.ai_flags = env->GetIntField(javaHints, flagsFid); 6201c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes hints.ai_family = env->GetIntField(javaHints, familyFid); 6211c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes hints.ai_socktype = env->GetIntField(javaHints, socktypeFid); 6221c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes hints.ai_protocol = env->GetIntField(javaHints, protocolFid); 6231c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 6241c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes addrinfo* addressList = NULL; 62575cf14944d476670f6f915e5efd849e238a16250Elliott Hughes errno = 0; 6261c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes int rc = getaddrinfo(node.c_str(), NULL, &hints, &addressList); 6271c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes UniquePtr<addrinfo, addrinfo_deleter> addressListDeleter(addressList); 6281c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (rc != 0) { 6291c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes throwGaiException(env, "getaddrinfo", rc); 6301c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 6311c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 6321c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 6331c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes // Count results so we know how to size the output array. 6341c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes int addressCount = 0; 6351c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes for (addrinfo* ai = addressList; ai != NULL; ai = ai->ai_next) { 6361c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) { 6371c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes ++addressCount; 6381c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } else { 639679cf68b607e9b4a3beb8bcdee06868ae583386fSteve Block ALOGE("getaddrinfo unexpected ai_family %i", ai->ai_family); 6401c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 6411c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 6421c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (addressCount == 0) { 6431c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 6441c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 6451c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 6461c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes // Prepare output array. 6471c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes jobjectArray result = env->NewObjectArray(addressCount, JniConstants::inetAddressClass, NULL); 6481c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (result == NULL) { 6491c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 6501c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 6511c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 6521c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes // Examine returned addresses one by one, save them in the output array. 6531c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes int index = 0; 6541c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes for (addrinfo* ai = addressList; ai != NULL; ai = ai->ai_next) { 6551c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) { 6561c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes // Unknown address family. Skip this address. 657679cf68b607e9b4a3beb8bcdee06868ae583386fSteve Block ALOGE("getaddrinfo unexpected ai_family %i", ai->ai_family); 6581c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes continue; 6591c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 6601c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 6611c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes // Convert each IP address into a Java byte array. 662482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes sockaddr_storage& address = *reinterpret_cast<sockaddr_storage*>(ai->ai_addr); 66323ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes ScopedLocalRef<jobject> inetAddress(env, sockaddrToInetAddress(env, address, NULL)); 6641c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (inetAddress.get() == NULL) { 6651c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 6661c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 6671c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes env->SetObjectArrayElement(result, index, inetAddress.get()); 6681c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes ++index; 6691c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 6701c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return result; 6711c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes} 6721c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 673396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getegid(JNIEnv*, jobject) { 674396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return getegid(); 675396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 676396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 677396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_geteuid(JNIEnv*, jobject) { 678396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return geteuid(); 679396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 680396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 681396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getgid(JNIEnv*, jobject) { 682396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return getgid(); 683396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 684396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 685ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic jstring Posix_getenv(JNIEnv* env, jobject, jstring javaName) { 686ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes ScopedUtfChars name(env, javaName); 687ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes if (name.c_str() == NULL) { 688ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes return NULL; 689ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes } 690ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes return env->NewStringUTF(getenv(name.c_str())); 691ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes} 692ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 6934f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic jstring Posix_getnameinfo(JNIEnv* env, jobject, jobject javaAddress, jint flags) { 6944f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes sockaddr_storage ss; 695482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes socklen_t sa_len; 696482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes if (!inetAddressToSockaddrVerbatim(env, javaAddress, 0, ss, sa_len)) { 6974f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return NULL; 6984f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes } 6994f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes char buf[NI_MAXHOST]; // NI_MAXHOST is longer than INET6_ADDRSTRLEN. 70075cf14944d476670f6f915e5efd849e238a16250Elliott Hughes errno = 0; 701482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes int rc = getnameinfo(reinterpret_cast<sockaddr*>(&ss), sa_len, buf, sizeof(buf), NULL, 0, flags); 7024f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes if (rc != 0) { 7034f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwGaiException(env, "getnameinfo", rc); 7044f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return NULL; 7054f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes } 7064f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return env->NewStringUTF(buf); 7074f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 7084f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 709482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughesstatic jobject Posix_getpeername(JNIEnv* env, jobject, jobject javaFd) { 710482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return doGetSockName(env, javaFd, false); 711482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes} 712482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes 713396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getpid(JNIEnv*, jobject) { 714396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return getpid(); 715396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 716396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 717396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getppid(JNIEnv*, jobject) { 718396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return getppid(); 719396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 720396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 721d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesstatic jobject Posix_getpwnam(JNIEnv* env, jobject, jstring javaName) { 722d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes ScopedUtfChars name(env, javaName); 723d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes if (name.c_str() == NULL) { 724d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return NULL; 725d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 726d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return Passwd(env).getpwnam(name.c_str()); 727d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes} 728d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 729d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesstatic jobject Posix_getpwuid(JNIEnv* env, jobject, jint uid) { 730d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return Passwd(env).getpwuid(uid); 731d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes} 732d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 7330a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockname(JNIEnv* env, jobject, jobject javaFd) { 734482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return doGetSockName(env, javaFd, true); 7350a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 7360a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 7370a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 7380a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 7390a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes u_char result = 0; 7400a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(result); 7410a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size))); 7420a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return result; 7430a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 7440a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 7450a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptInAddr(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 7460a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 7470a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes sockaddr_storage ss; 7480a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes memset(&ss, 0, sizeof(ss)); 7490a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes ss.ss_family = AF_INET; // This is only for the IPv4-only IP_MULTICAST_IF. 7500a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes sockaddr_in* sa = reinterpret_cast<sockaddr_in*>(&ss); 7510a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(sa->sin_addr); 7520a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &sa->sin_addr, &size)); 7530a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (rc == -1) { 7540a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwErrnoException(env, "getsockopt"); 7550a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 7560a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 757482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return sockaddrToInetAddress(env, ss, NULL); 7580a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 7590a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 7600a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 7610a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 7620a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes jint result = 0; 7630a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(result); 7640a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size))); 7650a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return result; 7660a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 7670a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 7680a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 7690a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 7700a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes struct linger l; 7710a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(l); 7720a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes memset(&l, 0, size); 7730a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &l, &size)); 7740a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (rc == -1) { 7750a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwErrnoException(env, "getsockopt"); 7760a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 7770a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 7780a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return makeStructLinger(env, l); 7790a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 7800a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 7810a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 7820a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 7830a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes struct timeval tv; 7840a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(tv); 7850a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes memset(&tv, 0, size); 7860a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &tv, &size)); 7870a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (rc == -1) { 7880a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwErrnoException(env, "getsockopt"); 7890a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 7900a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 7910a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return makeStructTimeval(env, tv); 7920a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 7930a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 794482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughesstatic jobject Posix_getsockoptUcred(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 795482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 796482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes struct ucred u; 797482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes socklen_t size = sizeof(u); 798482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes memset(&u, 0, size); 799482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &u, &size)); 800482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes if (rc == -1) { 801482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes throwErrnoException(env, "getsockopt"); 802482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return NULL; 803482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes } 804482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return makeStructUcred(env, u); 805482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes} 806482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes 807396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getuid(JNIEnv*, jobject) { 808396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return getuid(); 809396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 810396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 811a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic jstring Posix_if_indextoname(JNIEnv* env, jobject, jint index) { 812a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes char buf[IF_NAMESIZE]; 813a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes char* name = if_indextoname(index, buf); 814a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes // if_indextoname(3) returns NULL on failure, which will come out of NewStringUTF unscathed. 815a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes // There's no useful information in errno, so we don't bother throwing. Callers can null-check. 816a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return env->NewStringUTF(name); 817a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes} 818a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes 819fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughesstatic jobject Posix_inet_pton(JNIEnv* env, jobject, jint family, jstring javaName) { 8201c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes ScopedUtfChars name(env, javaName); 8211c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (name.c_str() == NULL) { 8221c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 8231c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 8241c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes sockaddr_storage ss; 8251c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes memset(&ss, 0, sizeof(ss)); 826fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // sockaddr_in and sockaddr_in6 are at the same address, so we can use either here. 827fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes void* dst = &reinterpret_cast<sockaddr_in*>(&ss)->sin_addr; 828fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes if (inet_pton(family, name.c_str(), dst) != 1) { 8291c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 8301c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 831fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes ss.ss_family = family; 832482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return sockaddrToInetAddress(env, ss, NULL); 8331c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes} 8341c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 835a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic jobject Posix_ioctlInetAddress(JNIEnv* env, jobject, jobject javaFd, jint cmd, jstring javaInterfaceName) { 836a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes struct ifreq req; 837a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (!fillIfreq(env, javaInterfaceName, req)) { 838a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return NULL; 839a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes } 840a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 841a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &req))); 842a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (rc == -1) { 843a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return NULL; 844a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes } 845482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return sockaddrToInetAddress(env, reinterpret_cast<sockaddr_storage&>(req.ifr_addr), NULL); 846a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes} 847a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes 8488b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jint Posix_ioctlInt(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaArg) { 8498b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes // This is complicated because ioctls may return their result by updating their argument 8508b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes // or via their return value, so we need to support both. 851461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 8528b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes static jfieldID valueFid = env->GetFieldID(JniConstants::mutableIntClass, "value", "I"); 8538b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes jint arg = env->GetIntField(javaArg, valueFid); 8548b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &arg))); 8558b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes if (!env->ExceptionCheck()) { 8568b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes env->SetIntField(javaArg, valueFid, arg); 8578b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes } 8588b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes return rc; 859461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes} 860461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes 8619a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughesstatic jboolean Posix_isatty(JNIEnv* env, jobject, jobject javaFd) { 8629a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 863bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig return TEMP_FAILURE_RETRY(isatty(fd)) == 1; 8649a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes} 8659a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes 866a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughesstatic void Posix_kill(JNIEnv* env, jobject, jint pid, jint sig) { 867a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes throwIfMinusOne(env, "kill", TEMP_FAILURE_RETRY(kill(pid, sig))); 868a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes} 869a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes 87044f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughesstatic void Posix_lchown(JNIEnv* env, jobject, jstring javaPath, jint uid, jint gid) { 87144f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes ScopedUtfChars path(env, javaPath); 87244f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes if (path.c_str() == NULL) { 87344f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes return; 87444f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes } 87544f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes throwIfMinusOne(env, "lchown", TEMP_FAILURE_RETRY(lchown(path.c_str(), uid, gid))); 87644f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes} 87744f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes 878e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughesstatic void Posix_listen(JNIEnv* env, jobject, jobject javaFd, jint backlog) { 879e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 880e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes throwIfMinusOne(env, "listen", TEMP_FAILURE_RETRY(listen(fd, backlog))); 881e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes} 882e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes 883dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughesstatic jlong Posix_lseek(JNIEnv* env, jobject, jobject javaFd, jlong offset, jint whence) { 884dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 8857e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes return throwIfMinusOne(env, "lseek", TEMP_FAILURE_RETRY(lseek64(fd, offset, whence))); 886dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes} 887dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes 88847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_lstat(JNIEnv* env, jobject, jstring javaPath) { 88947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return doStat(env, javaPath, true); 89047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 89147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 8920f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughesstatic void Posix_mincore(JNIEnv* env, jobject, jlong address, jlong byteCount, jbyteArray javaVector) { 8930f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes ScopedByteArrayRW vector(env, javaVector); 8940f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes if (vector.get() == NULL) { 8950f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes return; 8960f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes } 8970f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 8980f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes unsigned char* vec = reinterpret_cast<unsigned char*>(vector.get()); 8990f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes throwIfMinusOne(env, "mincore", TEMP_FAILURE_RETRY(mincore(ptr, byteCount, vec))); 9000f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes} 9010f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes 902c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_mkdir(JNIEnv* env, jobject, jstring javaPath, jint mode) { 903c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes ScopedUtfChars path(env, javaPath); 904c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes if (path.c_str() == NULL) { 905c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes return; 906c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes } 907c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes throwIfMinusOne(env, "mkdir", TEMP_FAILURE_RETRY(mkdir(path.c_str(), mode))); 908c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes} 909c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes 9107e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_mlock(JNIEnv* env, jobject, jlong address, jlong byteCount) { 9117e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 9127e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes throwIfMinusOne(env, "mlock", TEMP_FAILURE_RETRY(mlock(ptr, byteCount))); 9137e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes} 9147e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes 9157e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic jlong Posix_mmap(JNIEnv* env, jobject, jlong address, jlong byteCount, jint prot, jint flags, jobject javaFd, jlong offset) { 9167e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 9177e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* suggestedPtr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 9187e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* ptr = mmap(suggestedPtr, byteCount, prot, flags, fd, offset); 9197e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes if (ptr == MAP_FAILED) { 9207e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "mmap"); 9217e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes } 9227e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes return static_cast<jlong>(reinterpret_cast<uintptr_t>(ptr)); 9237e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes} 9247e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes 9257e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_msync(JNIEnv* env, jobject, jlong address, jlong byteCount, jint flags) { 9267e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 9277e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "msync", TEMP_FAILURE_RETRY(msync(ptr, byteCount, flags))); 9287e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes} 9297e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes 9307e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_munlock(JNIEnv* env, jobject, jlong address, jlong byteCount) { 9317e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 9327e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes throwIfMinusOne(env, "munlock", TEMP_FAILURE_RETRY(munlock(ptr, byteCount))); 9337e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes} 9347e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes 9357e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_munmap(JNIEnv* env, jobject, jlong address, jlong byteCount) { 9367e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 9377e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "munmap", TEMP_FAILURE_RETRY(munmap(ptr, byteCount))); 9387e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes} 9397e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes 9400ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughesstatic jobject Posix_open(JNIEnv* env, jobject, jstring javaPath, jint flags, jint mode) { 9410ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes ScopedUtfChars path(env, javaPath); 9420ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes if (path.c_str() == NULL) { 9430ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes return NULL; 9440ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes } 9450ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes int fd = throwIfMinusOne(env, "open", TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode))); 9460ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL; 9470ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes} 9480ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes 94941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughesstatic jobjectArray Posix_pipe(JNIEnv* env, jobject) { 95041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes int fds[2]; 95141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes throwIfMinusOne(env, "pipe", TEMP_FAILURE_RETRY(pipe(&fds[0]))); 95241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes jobjectArray result = env->NewObjectArray(2, JniConstants::fileDescriptorClass, NULL); 95341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes if (result == NULL) { 95441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return NULL; 95541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 95641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes for (int i = 0; i < 2; ++i) { 95741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes ScopedLocalRef<jobject> fd(env, jniCreateFileDescriptor(env, fds[i])); 95841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes if (fd.get() == NULL) { 95941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return NULL; 96041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 96141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes env->SetObjectArrayElement(result, i, fd.get()); 96241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes if (env->ExceptionCheck()) { 96341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return NULL; 96441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 96541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 96641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return result; 96741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes} 96841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes 96970c820401677ca251ad09ac64cc23c760764e75dElliott Hughesstatic jint Posix_poll(JNIEnv* env, jobject, jobjectArray javaStructs, jint timeoutMs) { 97070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes static jfieldID fdFid = env->GetFieldID(JniConstants::structPollfdClass, "fd", "Ljava/io/FileDescriptor;"); 97170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes static jfieldID eventsFid = env->GetFieldID(JniConstants::structPollfdClass, "events", "S"); 97270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes static jfieldID reventsFid = env->GetFieldID(JniConstants::structPollfdClass, "revents", "S"); 97370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 97470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes // Turn the Java libcore.io.StructPollfd[] into a C++ struct pollfd[]. 97570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes size_t arrayLength = env->GetArrayLength(javaStructs); 97670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes UniquePtr<struct pollfd[]> fds(new struct pollfd[arrayLength]); 97770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes memset(fds.get(), 0, sizeof(struct pollfd) * arrayLength); 97870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes size_t count = 0; // Some trailing array elements may be irrelevant. (See below.) 97970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes for (size_t i = 0; i < arrayLength; ++i) { 98070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes ScopedLocalRef<jobject> javaStruct(env, env->GetObjectArrayElement(javaStructs, i)); 98170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes if (javaStruct.get() == NULL) { 98270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes break; // We allow trailing nulls in the array for caller convenience. 98370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 98470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes ScopedLocalRef<jobject> javaFd(env, env->GetObjectField(javaStruct.get(), fdFid)); 98570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes if (javaFd.get() == NULL) { 98670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes break; // We also allow callers to just clear the fd field (this is what Selector does). 98770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 98870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes fds[count].fd = jniGetFDFromFileDescriptor(env, javaFd.get()); 98970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes fds[count].events = env->GetShortField(javaStruct.get(), eventsFid); 99070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes ++count; 99170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 99270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 993796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes // Since we don't know which fds -- if any -- are sockets, be conservative and register 994796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes // all fds for asynchronous socket close monitoring. 995796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes std::vector<AsynchronousSocketCloseMonitor*> monitors; 996796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes for (size_t i = 0; i < count; ++i) { 997796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes monitors.push_back(new AsynchronousSocketCloseMonitor(fds[i].fd)); 998796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes } 999796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes int rc = poll(fds.get(), count, timeoutMs); 1000796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes for (size_t i = 0; i < monitors.size(); ++i) { 1001796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes delete monitors[i]; 1002796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes } 100370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes if (rc == -1) { 100470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes throwErrnoException(env, "poll"); 100570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes return -1; 100670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 100770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 100870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes // Update the revents fields in the Java libcore.io.StructPollfd[]. 100998b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes for (size_t i = 0; i < count; ++i) { 101070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes ScopedLocalRef<jobject> javaStruct(env, env->GetObjectArrayElement(javaStructs, i)); 101198b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes if (javaStruct.get() == NULL) { 101298b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes return -1; 101398b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes } 101470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes env->SetShortField(javaStruct.get(), reventsFid, fds[i].revents); 101570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 101670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes return rc; 101770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes} 101870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 1019e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughesstatic jint Posix_preadBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount, jlong offset) { 1020e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes ScopedBytesRW bytes(env, javaBytes); 1021e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes if (bytes.get() == NULL) { 1022e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes return -1; 1023e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes } 1024e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1025e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes return throwIfMinusOne(env, "pread", TEMP_FAILURE_RETRY(pread64(fd, bytes.get() + byteOffset, byteCount, offset))); 1026e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes} 1027e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes 1028e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughesstatic jint Posix_pwriteBytes(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount, jlong offset) { 1029e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes ScopedBytesRO bytes(env, javaBytes); 1030e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes if (bytes.get() == NULL) { 1031e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes return -1; 1032e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes } 1033e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1034e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes return throwIfMinusOne(env, "pwrite", TEMP_FAILURE_RETRY(pwrite64(fd, bytes.get() + byteOffset, byteCount, offset))); 1035e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes} 1036e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes 10370568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughesstatic jint Posix_readBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount) { 10380568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes ScopedBytesRW bytes(env, javaBytes); 103926c7025a7a919044771fb89031161bd26fe03032Elliott Hughes if (bytes.get() == NULL) { 104026c7025a7a919044771fb89031161bd26fe03032Elliott Hughes return -1; 104126c7025a7a919044771fb89031161bd26fe03032Elliott Hughes } 104226c7025a7a919044771fb89031161bd26fe03032Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 10430568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes return throwIfMinusOne(env, "read", TEMP_FAILURE_RETRY(read(fd, bytes.get() + byteOffset, byteCount))); 104426c7025a7a919044771fb89031161bd26fe03032Elliott Hughes} 104526c7025a7a919044771fb89031161bd26fe03032Elliott Hughes 1046bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_readv(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) { 10478dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes IoVec<ScopedBytesRW> ioVec(env, env->GetArrayLength(buffers)); 1048bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (!ioVec.init(buffers, offsets, byteCounts)) { 1049bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return -1; 1050bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 1051bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1052bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return throwIfMinusOne(env, "readv", TEMP_FAILURE_RETRY(readv(fd, ioVec.get(), ioVec.size()))); 1053bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes} 1054bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 105523ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughesstatic jint Posix_recvfromBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount, jint flags, jobject javaInetSocketAddress) { 105623ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes ScopedBytesRW bytes(env, javaBytes); 105723ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes if (bytes.get() == NULL) { 105823ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes return -1; 105923ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes } 106023ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes sockaddr_storage ss; 106123ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes socklen_t sl = sizeof(ss); 106223ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes memset(&ss, 0, sizeof(ss)); 106323ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes sockaddr* from = (javaInetSocketAddress != NULL) ? reinterpret_cast<sockaddr*>(&ss) : NULL; 106423ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes socklen_t* fromLength = (javaInetSocketAddress != NULL) ? &sl : 0; 10651124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes jint recvCount = NET_FAILURE_RETRY(env, ssize_t, recvfrom, javaFd, bytes.get() + byteOffset, byteCount, flags, from, fromLength); 1066482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes fillInetSocketAddress(env, recvCount, javaInetSocketAddress, ss); 106723ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes return recvCount; 106823ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes} 106923ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes 1070c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_remove(JNIEnv* env, jobject, jstring javaPath) { 1071c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes ScopedUtfChars path(env, javaPath); 1072c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes if (path.c_str() == NULL) { 1073c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes return; 1074c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes } 1075c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes throwIfMinusOne(env, "remove", TEMP_FAILURE_RETRY(remove(path.c_str()))); 1076c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes} 1077c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes 1078a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_rename(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) { 1079a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars oldPath(env, javaOldPath); 1080a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (oldPath.c_str() == NULL) { 1081a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 1082a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 1083a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars newPath(env, javaNewPath); 1084a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (newPath.c_str() == NULL) { 1085a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 1086a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 1087a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes throwIfMinusOne(env, "rename", TEMP_FAILURE_RETRY(rename(oldPath.c_str(), newPath.c_str()))); 1088a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes} 1089a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes 10908b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jlong Posix_sendfile(JNIEnv* env, jobject, jobject javaOutFd, jobject javaInFd, jobject javaOffset, jlong byteCount) { 10918b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes int outFd = jniGetFDFromFileDescriptor(env, javaOutFd); 10928b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes int inFd = jniGetFDFromFileDescriptor(env, javaInFd); 10938b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes static jfieldID valueFid = env->GetFieldID(JniConstants::mutableLongClass, "value", "J"); 10948b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes off_t offset = 0; 10958b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes off_t* offsetPtr = NULL; 10968b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes if (javaOffset != NULL) { 10978b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes // TODO: fix bionic so we can have a 64-bit off_t! 10988b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes offset = env->GetLongField(javaOffset, valueFid); 10998b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes offsetPtr = &offset; 11008b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes } 11018b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes jlong result = throwIfMinusOne(env, "sendfile", TEMP_FAILURE_RETRY(sendfile(outFd, inFd, offsetPtr, byteCount))); 11028b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes if (javaOffset != NULL) { 11038b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes env->SetLongField(javaOffset, valueFid, offset); 11048b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes } 11058b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes return result; 11068b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes} 11078b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes 110890d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughesstatic jint Posix_sendtoBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount, jint flags, jobject javaInetAddress, jint port) { 110990d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes ScopedBytesRO bytes(env, javaBytes); 111090d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes if (bytes.get() == NULL) { 111190d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes return -1; 111290d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes } 111390d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes sockaddr_storage ss; 1114482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes socklen_t sa_len = 0; 1115482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes if (javaInetAddress != NULL && !inetAddressToSockaddr(env, javaInetAddress, port, ss, sa_len)) { 111690d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes return -1; 111790d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes } 111890d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes const sockaddr* to = (javaInetAddress != NULL) ? reinterpret_cast<const sockaddr*>(&ss) : NULL; 1119482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return NET_FAILURE_RETRY(env, ssize_t, sendto, javaFd, bytes.get() + byteOffset, byteCount, flags, to, sa_len); 112090d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes} 112190d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes 1122396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_setegid(JNIEnv* env, jobject, jint egid) { 1123396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes throwIfMinusOne(env, "setegid", TEMP_FAILURE_RETRY(setegid(egid))); 1124396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 1125396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 1126895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughesstatic void Posix_setenv(JNIEnv* env, jobject, jstring javaName, jstring javaValue, jboolean overwrite) { 1127895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes ScopedUtfChars name(env, javaName); 1128895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes if (name.c_str() == NULL) { 1129895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes return; 1130895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes } 1131895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes ScopedUtfChars value(env, javaValue); 1132895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes if (value.c_str() == NULL) { 1133895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes return; 1134895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes } 1135895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes throwIfMinusOne(env, "setenv", setenv(name.c_str(), value.c_str(), overwrite)); 1136895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes} 1137895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes 1138396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_seteuid(JNIEnv* env, jobject, jint euid) { 1139396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes throwIfMinusOne(env, "seteuid", TEMP_FAILURE_RETRY(seteuid(euid))); 1140396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 1141396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 1142396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_setgid(JNIEnv* env, jobject, jint gid) { 1143396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes throwIfMinusOne(env, "setgid", TEMP_FAILURE_RETRY(setgid(gid))); 1144396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 1145396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 11466c9b5377550a9649ed1532d1fcdfeba116c74eadElliott Hughesstatic jint Posix_setsid(JNIEnv* env, jobject) { 11476c9b5377550a9649ed1532d1fcdfeba116c74eadElliott Hughes return throwIfMinusOne(env, "setsid", TEMP_FAILURE_RETRY(setsid())); 11486c9b5377550a9649ed1532d1fcdfeba116c74eadElliott Hughes} 11496c9b5377550a9649ed1532d1fcdfeba116c74eadElliott Hughes 1150c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 1151c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1152c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes u_char byte = value; 1153c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &byte, sizeof(byte)))); 1154c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes} 1155c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes 1156a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic void Posix_setsockoptIfreq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jstring javaInterfaceName) { 1157a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes struct ifreq req; 1158a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (!fillIfreq(env, javaInterfaceName, req)) { 1159b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes return; 1160b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes } 1161b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1162b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req)))); 1163b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes} 1164b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes 1165454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic void Posix_setsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 1166454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1167454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 1168454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes} 1169454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes 11706c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED < 1070 11716c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes// Mac OS didn't support modern multicast APIs until 10.7. 11726c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughesstatic void Posix_setsockoptIpMreqn(JNIEnv*, jobject, jobject, jint, jint, jint) { abort(); } 11736c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughesstatic void Posix_setsockoptGroupReq(JNIEnv*, jobject, jobject, jint, jint, jobject) { abort(); } 11746c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes#else 1175b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughesstatic void Posix_setsockoptIpMreqn(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 1176b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes ip_mreqn req; 1177b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes memset(&req, 0, sizeof(req)); 1178b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes req.imr_ifindex = value; 1179b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1180b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req)))); 1181b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes} 1182b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes 1183438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughesstatic void Posix_setsockoptGroupReq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaGroupReq) { 11848ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes struct group_req req; 11858ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes memset(&req, 0, sizeof(req)); 1186438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes 1187438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes static jfieldID grInterfaceFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_interface", "I"); 11888ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes req.gr_interface = env->GetIntField(javaGroupReq, grInterfaceFid); 1189438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes // Get the IPv4 or IPv6 multicast address to join or leave. 1190438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes static jfieldID grGroupFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_group", "Ljava/net/InetAddress;"); 1191e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes ScopedLocalRef<jobject> javaGroup(env, env->GetObjectField(javaGroupReq, grGroupFid)); 1192482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes socklen_t sa_len; 1193482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes if (!inetAddressToSockaddrVerbatim(env, javaGroup.get(), 0, req.gr_group, sa_len)) { 1194438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes return; 1195438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes } 1196438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes 1197438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 11988ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes int rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req))); 1199438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes if (rc == -1 && errno == EINVAL) { 1200438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes // Maybe we're a 32-bit binary talking to a 64-bit kernel? 1201438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes // glibc doesn't automatically handle this. 12029634b569a92841f32268907c88c408f22cb00486Elliott Hughes struct group_req64 { 1203438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes uint32_t gr_interface; 1204438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes uint32_t my_padding; 1205438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes sockaddr_storage gr_group; 1206438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes }; 12078ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes group_req64 req64; 12088ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes req64.gr_interface = req.gr_interface; 12098ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes memcpy(&req64.gr_group, &req.gr_group, sizeof(req.gr_group)); 12108ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req64, sizeof(req64))); 1211438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes } 1212438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes throwIfMinusOne(env, "setsockopt", rc); 1213438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes} 12146c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes#endif 1215438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes 1216c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaLinger) { 1217c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID lOnoffFid = env->GetFieldID(JniConstants::structLingerClass, "l_onoff", "I"); 1218c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID lLingerFid = env->GetFieldID(JniConstants::structLingerClass, "l_linger", "I"); 1219c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1220c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes struct linger value; 1221c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes value.l_onoff = env->GetIntField(javaLinger, lOnoffFid); 1222c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes value.l_linger = env->GetIntField(javaLinger, lLingerFid); 1223c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 1224c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes} 1225c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes 1226c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaTimeval) { 1227c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID tvSecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_sec", "J"); 1228c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID tvUsecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_usec", "J"); 1229c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1230c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes struct timeval value; 123139fd5e164f93ab821841aa4d1fac09055c102b8bElliott Hughes value.tv_sec = env->GetLongField(javaTimeval, tvSecFid); 123239fd5e164f93ab821841aa4d1fac09055c102b8bElliott Hughes value.tv_usec = env->GetLongField(javaTimeval, tvUsecFid); 1233c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 1234c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes} 1235c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes 1236396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_setuid(JNIEnv* env, jobject, jint uid) { 1237396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes throwIfMinusOne(env, "setuid", TEMP_FAILURE_RETRY(setuid(uid))); 1238396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 1239396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 124059e4744d27231f260271dbbca406e0cc39768116Elliott Hughesstatic void Posix_shutdown(JNIEnv* env, jobject, jobject javaFd, jint how) { 124159e4744d27231f260271dbbca406e0cc39768116Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 124259e4744d27231f260271dbbca406e0cc39768116Elliott Hughes throwIfMinusOne(env, "shutdown", TEMP_FAILURE_RETRY(shutdown(fd, how))); 124359e4744d27231f260271dbbca406e0cc39768116Elliott Hughes} 124459e4744d27231f260271dbbca406e0cc39768116Elliott Hughes 1245454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic jobject Posix_socket(JNIEnv* env, jobject, jint domain, jint type, jint protocol) { 1246454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes int fd = throwIfMinusOne(env, "socket", TEMP_FAILURE_RETRY(socket(domain, type, protocol))); 1247454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL; 1248454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes} 1249454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes 12503218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughesstatic void Posix_socketpair(JNIEnv* env, jobject, jint domain, jint type, jint protocol, jobject javaFd1, jobject javaFd2) { 12513218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughes int fds[2]; 12523218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughes int rc = throwIfMinusOne(env, "socketpair", TEMP_FAILURE_RETRY(socketpair(domain, type, protocol, fds))); 12533218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughes if (rc != -1) { 12543218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughes jniSetFileDescriptorOfFD(env, javaFd1, fds[0]); 12553218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughes jniSetFileDescriptorOfFD(env, javaFd2, fds[1]); 12563218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughes } 12573218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughes} 12583218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughes 125947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_stat(JNIEnv* env, jobject, jstring javaPath) { 126047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return doStat(env, javaPath, false); 126147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 126247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 12630ade5524d972b9dfd69b0126aa4c35319c1b7fa4Elliott Hughesstatic jobject Posix_statfs(JNIEnv* env, jobject, jstring javaPath) { 126459fa7163774d6930a174bc038414a4b780581957Elliott Hughes ScopedUtfChars path(env, javaPath); 126559fa7163774d6930a174bc038414a4b780581957Elliott Hughes if (path.c_str() == NULL) { 126659fa7163774d6930a174bc038414a4b780581957Elliott Hughes return NULL; 126759fa7163774d6930a174bc038414a4b780581957Elliott Hughes } 126859fa7163774d6930a174bc038414a4b780581957Elliott Hughes struct statfs sb; 126959fa7163774d6930a174bc038414a4b780581957Elliott Hughes int rc = TEMP_FAILURE_RETRY(statfs(path.c_str(), &sb)); 127059fa7163774d6930a174bc038414a4b780581957Elliott Hughes if (rc == -1) { 127159fa7163774d6930a174bc038414a4b780581957Elliott Hughes throwErrnoException(env, "statfs"); 127259fa7163774d6930a174bc038414a4b780581957Elliott Hughes return NULL; 127359fa7163774d6930a174bc038414a4b780581957Elliott Hughes } 127459fa7163774d6930a174bc038414a4b780581957Elliott Hughes return makeStructStatFs(env, sb); 127559fa7163774d6930a174bc038414a4b780581957Elliott Hughes} 127659fa7163774d6930a174bc038414a4b780581957Elliott Hughes 1277ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jstring Posix_strerror(JNIEnv* env, jobject, jint errnum) { 127852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes char buffer[BUFSIZ]; 1279ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes const char* message = jniStrError(errnum, buffer, sizeof(buffer)); 1280ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return env->NewStringUTF(message); 1281ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 1282ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 1283763f8ed6195707d0c30bfae1ca8a3bb886b746ccElliott Hughesstatic jstring Posix_strsignal(JNIEnv* env, jobject, jint signal) { 1284763f8ed6195707d0c30bfae1ca8a3bb886b746ccElliott Hughes return env->NewStringUTF(strsignal(signal)); 1285763f8ed6195707d0c30bfae1ca8a3bb886b746ccElliott Hughes} 1286763f8ed6195707d0c30bfae1ca8a3bb886b746ccElliott Hughes 1287a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_symlink(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) { 1288a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars oldPath(env, javaOldPath); 1289a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (oldPath.c_str() == NULL) { 1290a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 1291a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 1292a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars newPath(env, javaNewPath); 1293a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (newPath.c_str() == NULL) { 1294a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 1295a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 1296a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes throwIfMinusOne(env, "symlink", TEMP_FAILURE_RETRY(symlink(oldPath.c_str(), newPath.c_str()))); 1297a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes} 1298a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes 12996fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughesstatic jlong Posix_sysconf(JNIEnv* env, jobject, jint name) { 13006fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes // Since -1 is a valid result from sysconf(3), detecting failure is a little more awkward. 13016fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes errno = 0; 13026fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes long result = sysconf(name); 13036fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes if (result == -1L && errno == EINVAL) { 13047e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "sysconf"); 13056fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes } 13066fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes return result; 13076fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes} 13086fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes 1309bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestigstatic void Posix_tcdrain(JNIEnv* env, jobject, jobject javaFd) { 1310bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig int fd = jniGetFDFromFileDescriptor(env, javaFd); 1311bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig throwIfMinusOne(env, "tcdrain", TEMP_FAILURE_RETRY(tcdrain(fd))); 1312bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig} 1313bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig 1314039f7599559fd7e48c354b99dcb94ff391f53349Elliott Hughesstatic void Posix_tcsendbreak(JNIEnv* env, jobject, jobject javaFd, jint duration) { 1315039f7599559fd7e48c354b99dcb94ff391f53349Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1316039f7599559fd7e48c354b99dcb94ff391f53349Elliott Hughes throwIfMinusOne(env, "tcsendbreak", TEMP_FAILURE_RETRY(tcsendbreak(fd, duration))); 1317039f7599559fd7e48c354b99dcb94ff391f53349Elliott Hughes} 1318039f7599559fd7e48c354b99dcb94ff391f53349Elliott Hughes 1319c79d3ef4dcd25f0e264bf957861e2632da93e62dNick Kralevichstatic jint Posix_umaskImpl(JNIEnv*, jobject, jint mask) { 132044f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes return umask(mask); 132144f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes} 132244f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes 13237341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject Posix_uname(JNIEnv* env, jobject) { 13247341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes struct utsname buf; 13259b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes if (TEMP_FAILURE_RETRY(uname(&buf)) == -1) { 13269b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes return NULL; // Can't happen. 13277341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes } 13287341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes return makeStructUtsname(env, buf); 13297341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes} 13307341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes 1331895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughesstatic void Posix_unsetenv(JNIEnv* env, jobject, jstring javaName) { 1332895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes ScopedUtfChars name(env, javaName); 1333895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes if (name.c_str() == NULL) { 1334895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes return; 1335895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes } 1336895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes throwIfMinusOne(env, "unsetenv", unsetenv(name.c_str())); 1337895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes} 1338895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes 13399e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughesstatic jint Posix_waitpid(JNIEnv* env, jobject, jint pid, jobject javaStatus, jint options) { 13409e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes int status; 13419e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes int rc = throwIfMinusOne(env, "waitpid", TEMP_FAILURE_RETRY(waitpid(pid, &status, options))); 13429e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes if (rc != -1) { 13439e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes static jfieldID valueFid = env->GetFieldID(JniConstants::mutableIntClass, "value", "I"); 13449e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes env->SetIntField(javaStatus, valueFid, status); 13459e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes } 13469e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes return rc; 13479e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes} 13489e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes 13490568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughesstatic jint Posix_writeBytes(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount) { 13500568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes ScopedBytesRO bytes(env, javaBytes); 135178c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes if (bytes.get() == NULL) { 135278c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes return -1; 135378c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes } 135478c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 13550568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes return throwIfMinusOne(env, "write", TEMP_FAILURE_RETRY(write(fd, bytes.get() + byteOffset, byteCount))); 135678c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes} 135778c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes 1358bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_writev(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) { 13598dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes IoVec<ScopedBytesRO> ioVec(env, env->GetArrayLength(buffers)); 1360bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (!ioVec.init(buffers, offsets, byteCounts)) { 1361bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return -1; 1362bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 1363bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1364bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return throwIfMinusOne(env, "writev", TEMP_FAILURE_RETRY(writev(fd, ioVec.get(), ioVec.size()))); 1365bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes} 1366bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 1367ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic JNINativeMethod gMethods[] = { 1368553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes NATIVE_METHOD(Posix, accept, "(Ljava/io/FileDescriptor;Ljava/net/InetSocketAddress;)Ljava/io/FileDescriptor;"), 1369ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes NATIVE_METHOD(Posix, access, "(Ljava/lang/String;I)Z"), 1370da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes NATIVE_METHOD(Posix, bind, "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)V"), 1371b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes NATIVE_METHOD(Posix, chmod, "(Ljava/lang/String;I)V"), 137244f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes NATIVE_METHOD(Posix, chown, "(Ljava/lang/String;II)V"), 1373462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes NATIVE_METHOD(Posix, close, "(Ljava/io/FileDescriptor;)V"), 1374996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes NATIVE_METHOD(Posix, connect, "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)V"), 1375396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, dup, "(Ljava/io/FileDescriptor;)Ljava/io/FileDescriptor;"), 1376396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, dup2, "(Ljava/io/FileDescriptor;I)Ljava/io/FileDescriptor;"), 1377ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes NATIVE_METHOD(Posix, environ, "()[Ljava/lang/String;"), 1378798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich NATIVE_METHOD(Posix, execv, "(Ljava/lang/String;[Ljava/lang/String;)V"), 1379798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich NATIVE_METHOD(Posix, execve, "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)V"), 138044f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes NATIVE_METHOD(Posix, fchmod, "(Ljava/io/FileDescriptor;I)V"), 138144f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes NATIVE_METHOD(Posix, fchown, "(Ljava/io/FileDescriptor;II)V"), 1382fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes NATIVE_METHOD(Posix, fcntlVoid, "(Ljava/io/FileDescriptor;I)I"), 1383fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes NATIVE_METHOD(Posix, fcntlLong, "(Ljava/io/FileDescriptor;IJ)I"), 1384fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes NATIVE_METHOD(Posix, fcntlFlock, "(Ljava/io/FileDescriptor;ILlibcore/io/StructFlock;)I"), 138552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes NATIVE_METHOD(Posix, fdatasync, "(Ljava/io/FileDescriptor;)V"), 138647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes NATIVE_METHOD(Posix, fstat, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStat;"), 138759fa7163774d6930a174bc038414a4b780581957Elliott Hughes NATIVE_METHOD(Posix, fstatfs, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStatFs;"), 138852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes NATIVE_METHOD(Posix, fsync, "(Ljava/io/FileDescriptor;)V"), 1389f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes NATIVE_METHOD(Posix, ftruncate, "(Ljava/io/FileDescriptor;J)V"), 13904f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes NATIVE_METHOD(Posix, gai_strerror, "(I)Ljava/lang/String;"), 13911c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes NATIVE_METHOD(Posix, getaddrinfo, "(Ljava/lang/String;Llibcore/io/StructAddrinfo;)[Ljava/net/InetAddress;"), 1392396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, getegid, "()I"), 1393396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, geteuid, "()I"), 1394396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, getgid, "()I"), 1395ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes NATIVE_METHOD(Posix, getenv, "(Ljava/lang/String;)Ljava/lang/String;"), 13964f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes NATIVE_METHOD(Posix, getnameinfo, "(Ljava/net/InetAddress;I)Ljava/lang/String;"), 1397482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes NATIVE_METHOD(Posix, getpeername, "(Ljava/io/FileDescriptor;)Ljava/net/SocketAddress;"), 1398396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, getpid, "()I"), 1399396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, getppid, "()I"), 1400d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes NATIVE_METHOD(Posix, getpwnam, "(Ljava/lang/String;)Llibcore/io/StructPasswd;"), 1401d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes NATIVE_METHOD(Posix, getpwuid, "(I)Llibcore/io/StructPasswd;"), 14020a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockname, "(Ljava/io/FileDescriptor;)Ljava/net/SocketAddress;"), 14030a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptByte, "(Ljava/io/FileDescriptor;II)I"), 14040a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptInAddr, "(Ljava/io/FileDescriptor;II)Ljava/net/InetAddress;"), 14050a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptInt, "(Ljava/io/FileDescriptor;II)I"), 14060a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptLinger, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructLinger;"), 14070a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptTimeval, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructTimeval;"), 1408482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes NATIVE_METHOD(Posix, getsockoptUcred, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructUcred;"), 1409396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, getuid, "()I"), 1410a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes NATIVE_METHOD(Posix, if_indextoname, "(I)Ljava/lang/String;"), 1411fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes NATIVE_METHOD(Posix, inet_pton, "(ILjava/lang/String;)Ljava/net/InetAddress;"), 1412a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes NATIVE_METHOD(Posix, ioctlInetAddress, "(Ljava/io/FileDescriptor;ILjava/lang/String;)Ljava/net/InetAddress;"), 14138b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes NATIVE_METHOD(Posix, ioctlInt, "(Ljava/io/FileDescriptor;ILlibcore/util/MutableInt;)I"), 14149a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes NATIVE_METHOD(Posix, isatty, "(Ljava/io/FileDescriptor;)Z"), 1415a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes NATIVE_METHOD(Posix, kill, "(II)V"), 141644f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes NATIVE_METHOD(Posix, lchown, "(Ljava/lang/String;II)V"), 1417e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes NATIVE_METHOD(Posix, listen, "(Ljava/io/FileDescriptor;I)V"), 1418dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes NATIVE_METHOD(Posix, lseek, "(Ljava/io/FileDescriptor;JI)J"), 141947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes NATIVE_METHOD(Posix, lstat, "(Ljava/lang/String;)Llibcore/io/StructStat;"), 14200f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes NATIVE_METHOD(Posix, mincore, "(JJ[B)V"), 1421c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes NATIVE_METHOD(Posix, mkdir, "(Ljava/lang/String;I)V"), 14227e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes NATIVE_METHOD(Posix, mlock, "(JJ)V"), 14237e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes NATIVE_METHOD(Posix, mmap, "(JJIILjava/io/FileDescriptor;J)J"), 14247e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes NATIVE_METHOD(Posix, msync, "(JJI)V"), 14257e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes NATIVE_METHOD(Posix, munlock, "(JJ)V"), 14267e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes NATIVE_METHOD(Posix, munmap, "(JJ)V"), 14270ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes NATIVE_METHOD(Posix, open, "(Ljava/lang/String;II)Ljava/io/FileDescriptor;"), 142841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes NATIVE_METHOD(Posix, pipe, "()[Ljava/io/FileDescriptor;"), 142970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes NATIVE_METHOD(Posix, poll, "([Llibcore/io/StructPollfd;I)I"), 1430e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes NATIVE_METHOD(Posix, preadBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIJ)I"), 1431e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes NATIVE_METHOD(Posix, pwriteBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIJ)I"), 14320568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes NATIVE_METHOD(Posix, readBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"), 1433bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes NATIVE_METHOD(Posix, readv, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"), 143423ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes NATIVE_METHOD(Posix, recvfromBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIILjava/net/InetSocketAddress;)I"), 1435c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes NATIVE_METHOD(Posix, remove, "(Ljava/lang/String;)V"), 1436a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes NATIVE_METHOD(Posix, rename, "(Ljava/lang/String;Ljava/lang/String;)V"), 14378b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes NATIVE_METHOD(Posix, sendfile, "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Llibcore/util/MutableLong;J)J"), 143890d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes NATIVE_METHOD(Posix, sendtoBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIILjava/net/InetAddress;I)I"), 1439396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, setegid, "(I)V"), 1440895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes NATIVE_METHOD(Posix, setenv, "(Ljava/lang/String;Ljava/lang/String;Z)V"), 1441396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, seteuid, "(I)V"), 1442396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, setgid, "(I)V"), 14436c9b5377550a9649ed1532d1fcdfeba116c74eadElliott Hughes NATIVE_METHOD(Posix, setsid, "()I"), 1444c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes NATIVE_METHOD(Posix, setsockoptByte, "(Ljava/io/FileDescriptor;III)V"), 1445b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes NATIVE_METHOD(Posix, setsockoptIfreq, "(Ljava/io/FileDescriptor;IILjava/lang/String;)V"), 1446454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes NATIVE_METHOD(Posix, setsockoptInt, "(Ljava/io/FileDescriptor;III)V"), 1447b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes NATIVE_METHOD(Posix, setsockoptIpMreqn, "(Ljava/io/FileDescriptor;III)V"), 1448438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes NATIVE_METHOD(Posix, setsockoptGroupReq, "(Ljava/io/FileDescriptor;IILlibcore/io/StructGroupReq;)V"), 1449c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes NATIVE_METHOD(Posix, setsockoptLinger, "(Ljava/io/FileDescriptor;IILlibcore/io/StructLinger;)V"), 1450c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes NATIVE_METHOD(Posix, setsockoptTimeval, "(Ljava/io/FileDescriptor;IILlibcore/io/StructTimeval;)V"), 1451396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, setuid, "(I)V"), 145259e4744d27231f260271dbbca406e0cc39768116Elliott Hughes NATIVE_METHOD(Posix, shutdown, "(Ljava/io/FileDescriptor;I)V"), 1453454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes NATIVE_METHOD(Posix, socket, "(III)Ljava/io/FileDescriptor;"), 14543218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughes NATIVE_METHOD(Posix, socketpair, "(IIILjava/io/FileDescriptor;Ljava/io/FileDescriptor;)V"), 145547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes NATIVE_METHOD(Posix, stat, "(Ljava/lang/String;)Llibcore/io/StructStat;"), 145659fa7163774d6930a174bc038414a4b780581957Elliott Hughes NATIVE_METHOD(Posix, statfs, "(Ljava/lang/String;)Llibcore/io/StructStatFs;"), 1457ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes NATIVE_METHOD(Posix, strerror, "(I)Ljava/lang/String;"), 1458763f8ed6195707d0c30bfae1ca8a3bb886b746ccElliott Hughes NATIVE_METHOD(Posix, strsignal, "(I)Ljava/lang/String;"), 1459a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes NATIVE_METHOD(Posix, symlink, "(Ljava/lang/String;Ljava/lang/String;)V"), 14606fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes NATIVE_METHOD(Posix, sysconf, "(I)J"), 1461bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig NATIVE_METHOD(Posix, tcdrain, "(Ljava/io/FileDescriptor;)V"), 1462039f7599559fd7e48c354b99dcb94ff391f53349Elliott Hughes NATIVE_METHOD(Posix, tcsendbreak, "(Ljava/io/FileDescriptor;I)V"), 1463c79d3ef4dcd25f0e264bf957861e2632da93e62dNick Kralevich NATIVE_METHOD(Posix, umaskImpl, "(I)I"), 14647341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes NATIVE_METHOD(Posix, uname, "()Llibcore/io/StructUtsname;"), 1465895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes NATIVE_METHOD(Posix, unsetenv, "(Ljava/lang/String;)V"), 14669e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes NATIVE_METHOD(Posix, waitpid, "(ILlibcore/util/MutableInt;I)I"), 14670568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes NATIVE_METHOD(Posix, writeBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"), 1468bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes NATIVE_METHOD(Posix, writev, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"), 1469ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes}; 14707cd6760f7045d771faae8080a8c6150bf678f679Elliott Hughesvoid register_libcore_io_Posix(JNIEnv* env) { 14717cd6760f7045d771faae8080a8c6150bf678f679Elliott Hughes jniRegisterNativeMethods(env, "libcore/io/Posix", gMethods, NELEM(gMethods)); 1472ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes} 1473