libcore_io_Linux.cpp revision efae3837b3eb79dea3400735fdcdd5a39020e0b0
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 19f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller#include "AsynchronousCloseMonitor.h" 2015c1945bb1810d061ea2d5b07a11e3ecc5ec8536Ruben Brunk#include "cutils/log.h" 21ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes#include "ExecStrings.h" 22ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "JNIHelp.h" 23ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "JniConstants.h" 24ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include "JniException.h" 250a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include "NetworkUtilities.h" 266c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes#include "Portability.h" 270b6b3e10459fc3a3f4dd280dab8d4a145f7bf2f0Elliott Hughes#include "readlink.h" 280568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes#include "ScopedBytes.h" 29e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes#include "ScopedLocalRef.h" 300f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes#include "ScopedPrimitiveArray.h" 31ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "ScopedUtfChars.h" 32ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include "toStringArray.h" 330b6b3e10459fc3a3f4dd280dab8d4a145f7bf2f0Elliott Hughes#include "UniquePtr.h" 34ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 351c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes#include <arpa/inet.h> 36ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include <errno.h> 370ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <fcntl.h> 38b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes#include <net/if.h> 394f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes#include <netdb.h> 400a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include <netinet/in.h> 4170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes#include <poll.h> 42d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes#include <pwd.h> 43a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes#include <signal.h> 44ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include <stdlib.h> 45461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes#include <sys/ioctl.h> 467e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes#include <sys/mman.h> 473deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers#ifndef __APPLE__ 485215e4c0db7530519981f1e505e6db82401802f2Nick Kralevich#include <sys/prctl.h> 493deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers#endif 5059e4744d27231f260271dbbca406e0cc39768116Elliott Hughes#include <sys/socket.h> 5147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes#include <sys/stat.h> 523deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers#ifdef __APPLE__ 533deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers#include <sys/statvfs.h> 543deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers#endif 55069525a39125c203b658c805685b6045a7d4dfebElliott Hughes#include <sys/syscall.h> 560a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes#include <sys/time.h> 570ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <sys/types.h> 58bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes#include <sys/uio.h> 597341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes#include <sys/utsname.h> 609e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes#include <sys/wait.h> 61bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig#include <termios.h> 620ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes#include <unistd.h> 63ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 64efae3837b3eb79dea3400735fdcdd5a39020e0b0Ian Rogers 65efae3837b3eb79dea3400735fdcdd5a39020e0b0Ian Rogers#ifndef __unused 6637dd0ac5767197bf7e3e90f1a5d4e54c8f1fe93dIan Rogers#define __unused __attribute__((__unused__)) 67efae3837b3eb79dea3400735fdcdd5a39020e0b0Ian Rogers#endif 6837dd0ac5767197bf7e3e90f1a5d4e54c8f1fe93dIan Rogers 69d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes#define TO_JAVA_STRING(NAME, EXP) \ 70d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes jstring NAME = env->NewStringUTF(EXP); \ 71d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes if (NAME == NULL) return NULL; 72d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 731c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughesstruct addrinfo_deleter { 741c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes void operator()(addrinfo* p) const { 751c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (p != NULL) { // bionic's freeaddrinfo(3) crashes when passed NULL. 761c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes freeaddrinfo(p); 771c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 781c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 791c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes}; 801c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 81996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes/** 82f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * Used to retry networking system calls that can be interrupted with a signal. Unlike 83f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * TEMP_FAILURE_RETRY, this also handles the case where 84f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * AsynchronousCloseMonitor::signalBlockedThreads(fd) is used to signal a close() or 85f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * Thread.interrupt(). Other signals that result in an EINTR result are ignored and the system call 86f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * is retried. 87996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes * 88f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * Returns the result of the system call though a Java exception will be pending if the result is 89f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * -1: a SocketException if signaled via AsynchronousCloseMonitor, or ErrnoException for other 90f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * failures. 91996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes */ 921124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes#define NET_FAILURE_RETRY(jni_env, return_type, syscall_name, java_fd, ...) ({ \ 931124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes return_type _rc = -1; \ 94996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes do { \ 95f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller bool _wasSignaled; \ 96f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller int _syscallErrno; \ 97996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes { \ 981124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes int _fd = jniGetFDFromFileDescriptor(jni_env, java_fd); \ 99f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller AsynchronousCloseMonitor _monitor(_fd); \ 1001124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes _rc = syscall_name(_fd, __VA_ARGS__); \ 101f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller _syscallErrno = errno; \ 102f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller _wasSignaled = _monitor.wasSignaled(); \ 103996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes } \ 104f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller if (_wasSignaled) { \ 105f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller jniThrowException(jni_env, "java/net/SocketException", "Socket closed"); \ 1065d7c0a378be3f7f505b30e634e7aabf072c4edd0Serguei Katkov _rc = -1; \ 107f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller break; \ 108f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller } \ 109f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller if (_rc == -1 && _syscallErrno != EINTR) { \ 110f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller /* TODO: with a format string we could show the arguments too, like strace(1). */ \ 111f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller throwErrnoException(jni_env, # syscall_name); \ 112f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller break; \ 113f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller } \ 114f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller } while (_rc == -1); /* _syscallErrno == EINTR && !_wasSignaled */ \ 115f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller _rc; }) 116f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller 117f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller/** 118f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * Used to retry system calls that can be interrupted with a signal. Unlike TEMP_FAILURE_RETRY, this 119f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * also handles the case where AsynchronousCloseMonitor::signalBlockedThreads(fd) is used to signal 120f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * a close() or Thread.interrupt(). Other signals that result in an EINTR result are ignored and the 121f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * system call is retried. 122f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * 123f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * Returns the result of the system call though a Java exception will be pending if the result is 124f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * -1: an IOException if the file descriptor is already closed, a InterruptedIOException if signaled 125f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * via AsynchronousCloseMonitor, or ErrnoException for other failures. 126f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller */ 127f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller#define IO_FAILURE_RETRY(jni_env, return_type, syscall_name, java_fd, ...) ({ \ 128f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller return_type _rc = -1; \ 129f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller int _fd = jniGetFDFromFileDescriptor(jni_env, java_fd); \ 130f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller if (_fd == -1) { \ 131f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller jniThrowException(jni_env, "java/io/IOException", "File descriptor closed"); \ 132f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller } else { \ 133f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller do { \ 134f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller bool _wasSignaled; \ 135f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller int _syscallErrno; \ 136f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller { \ 137f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller int _fd = jniGetFDFromFileDescriptor(jni_env, java_fd); \ 138f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller AsynchronousCloseMonitor _monitor(_fd); \ 139f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller _rc = syscall_name(_fd, __VA_ARGS__); \ 140f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller _syscallErrno = errno; \ 141f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller _wasSignaled = _monitor.wasSignaled(); \ 142f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller } \ 143f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller if (_wasSignaled) { \ 144f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller jniThrowException(jni_env, "java/io/InterruptedIOException", # syscall_name " interrupted"); \ 1455d7c0a378be3f7f505b30e634e7aabf072c4edd0Serguei Katkov _rc = -1; \ 146996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes break; \ 147f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller } \ 148f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller if (_rc == -1 && _syscallErrno != EINTR) { \ 1491124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes /* TODO: with a format string we could show the arguments too, like strace(1). */ \ 1501124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes throwErrnoException(jni_env, # syscall_name); \ 151996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes break; \ 152996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes } \ 153f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller } while (_rc == -1); /* && _syscallErrno == EINTR && !_wasSignaled */ \ 154f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller } \ 155996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes _rc; }) 156996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes 1574f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwException(JNIEnv* env, jclass exceptionClass, jmethodID ctor3, jmethodID ctor2, 1584f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes const char* functionName, int error) { 159ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes jthrowable cause = NULL; 160ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes if (env->ExceptionCheck()) { 161ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes cause = env->ExceptionOccurred(); 162ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes env->ExceptionClear(); 163ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } 164ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 1654f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName)); 1664f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes if (detailMessage.get() == NULL) { 167f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes // Not really much we can do here. We're probably dead in the water, 168f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes // but let's try to stumble on... 169f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes env->ExceptionClear(); 170f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes } 171f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes 172ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes jobject exception; 173ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes if (cause != NULL) { 1744f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes exception = env->NewObject(exceptionClass, ctor3, detailMessage.get(), error, cause); 175ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } else { 1764f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes exception = env->NewObject(exceptionClass, ctor2, detailMessage.get(), error); 177ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } 178ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes env->Throw(reinterpret_cast<jthrowable>(exception)); 179dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes} 180dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes 1814f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwErrnoException(JNIEnv* env, const char* functionName) { 1824f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes int error = errno; 1834f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes static jmethodID ctor3 = env->GetMethodID(JniConstants::errnoExceptionClass, 1844f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V"); 1854f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes static jmethodID ctor2 = env->GetMethodID(JniConstants::errnoExceptionClass, 1864f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes "<init>", "(Ljava/lang/String;I)V"); 1874f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwException(env, JniConstants::errnoExceptionClass, ctor3, ctor2, functionName, error); 1884f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 1894f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 1904f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic void throwGaiException(JNIEnv* env, const char* functionName, int error) { 191c1cee0db58ec7bc61d5caac79915d744862cbe75Elliott Hughes // Cache the methods ids before we throw, so we don't call GetMethodID with a pending exception. 192c1cee0db58ec7bc61d5caac79915d744862cbe75Elliott Hughes static jmethodID ctor3 = env->GetMethodID(JniConstants::gaiExceptionClass, "<init>", 193c1cee0db58ec7bc61d5caac79915d744862cbe75Elliott Hughes "(Ljava/lang/String;ILjava/lang/Throwable;)V"); 194c1cee0db58ec7bc61d5caac79915d744862cbe75Elliott Hughes static jmethodID ctor2 = env->GetMethodID(JniConstants::gaiExceptionClass, "<init>", 195c1cee0db58ec7bc61d5caac79915d744862cbe75Elliott Hughes "(Ljava/lang/String;I)V"); 196c1cee0db58ec7bc61d5caac79915d744862cbe75Elliott Hughes if (errno != 0) { 19775cf14944d476670f6f915e5efd849e238a16250Elliott Hughes // EAI_SYSTEM should mean "look at errno instead", but both glibc and bionic seem to 19875cf14944d476670f6f915e5efd849e238a16250Elliott Hughes // mess this up. In particular, if you don't have INTERNET permission, errno will be EACCES 19975cf14944d476670f6f915e5efd849e238a16250Elliott Hughes // but you'll get EAI_NONAME or EAI_NODATA. So we want our GaiException to have a 20075cf14944d476670f6f915e5efd849e238a16250Elliott Hughes // potentially-relevant ErrnoException as its cause even if error != EAI_SYSTEM. 20175cf14944d476670f6f915e5efd849e238a16250Elliott Hughes // http://code.google.com/p/android/issues/detail?id=15722 2024f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwErrnoException(env, functionName); 2034f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes // Deliberately fall through to throw another exception... 2044f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes } 2054f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwException(env, JniConstants::gaiExceptionClass, ctor3, ctor2, functionName, error); 2064f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 2074f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 208dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughestemplate <typename rc_t> 2097e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic rc_t throwIfMinusOne(JNIEnv* env, const char* name, rc_t rc) { 210dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == rc_t(-1)) { 2117e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, name); 212dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes } 213dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes return rc; 21447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 21547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 2168dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughestemplate <typename ScopedT> 217bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesclass IoVec { 218bbac92e691de7d570928ddfba639067978e55b06Elliott Hughespublic: 219bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes IoVec(JNIEnv* env, size_t bufferCount) : mEnv(env), mBufferCount(bufferCount) { 220bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 221bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 222bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes bool init(jobjectArray javaBuffers, jintArray javaOffsets, jintArray javaByteCounts) { 223bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes // We can't delete our local references until after the I/O, so make sure we have room. 224bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (mEnv->PushLocalFrame(mBufferCount + 16) < 0) { 225bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 226bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 227bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes ScopedIntArrayRO offsets(mEnv, javaOffsets); 228bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (offsets.get() == NULL) { 229bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 230bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 231bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes ScopedIntArrayRO byteCounts(mEnv, javaByteCounts); 232bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (byteCounts.get() == NULL) { 233bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 234bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 235bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes // TODO: Linux actually has a 1024 buffer limit. glibc works around this, and we should too. 236d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes // TODO: you can query the limit at runtime with sysconf(_SC_IOV_MAX). 237bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes for (size_t i = 0; i < mBufferCount; ++i) { 238e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes jobject buffer = mEnv->GetObjectArrayElement(javaBuffers, i); // We keep this local ref. 2398dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes mScopedBuffers.push_back(new ScopedT(mEnv, buffer)); 2408dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes jbyte* ptr = const_cast<jbyte*>(mScopedBuffers.back()->get()); 241bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (ptr == NULL) { 242bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return false; 243bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 244bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes struct iovec iov; 245bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes iov.iov_base = reinterpret_cast<void*>(ptr + offsets[i]); 246bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes iov.iov_len = byteCounts[i]; 247bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes mIoVec.push_back(iov); 248bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 249bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return true; 250bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 251bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 252bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes ~IoVec() { 2538dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes for (size_t i = 0; i < mScopedBuffers.size(); ++i) { 2548dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes delete mScopedBuffers[i]; 255bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 256bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes mEnv->PopLocalFrame(NULL); 257bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 258bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 259bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes iovec* get() { 260bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return &mIoVec[0]; 261bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 262bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 263bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes size_t size() { 264bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return mBufferCount; 265bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 266bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 267bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesprivate: 268bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes JNIEnv* mEnv; 269bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes size_t mBufferCount; 270bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes std::vector<iovec> mIoVec; 2718dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes std::vector<ScopedT*> mScopedBuffers; 272bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes}; 273bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 274482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughesstatic jobject makeSocketAddress(JNIEnv* env, const sockaddr_storage& ss) { 27523ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes jint port; 27623ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes jobject inetAddress = sockaddrToInetAddress(env, ss, &port); 2770a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (inetAddress == NULL) { 2780a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 2790a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 2800a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::inetSocketAddressClass, "<init>", 2810a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes "(Ljava/net/InetAddress;I)V"); 2820a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return env->NewObject(JniConstants::inetSocketAddressClass, ctor, inetAddress, port); 2830a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 2840a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 285d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesstatic jobject makeStructPasswd(JNIEnv* env, const struct passwd& pw) { 286d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(pw_name, pw.pw_name); 287d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(pw_dir, pw.pw_dir); 288d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(pw_shell, pw.pw_shell); 289d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structPasswdClass, "<init>", 290d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes "(Ljava/lang/String;IILjava/lang/String;Ljava/lang/String;)V"); 291d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return env->NewObject(JniConstants::structPasswdClass, ctor, 292d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes pw_name, static_cast<jint>(pw.pw_uid), static_cast<jint>(pw.pw_gid), pw_dir, pw_shell); 293d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes} 294d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 29547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject makeStructStat(JNIEnv* env, const struct stat& sb) { 29647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structStatClass, "<init>", 29747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes "(JJIJIIJJJJJJJ)V"); 29847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return env->NewObject(JniConstants::structStatClass, ctor, 29959fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_dev), static_cast<jlong>(sb.st_ino), 30059fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jint>(sb.st_mode), static_cast<jlong>(sb.st_nlink), 30159fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jint>(sb.st_uid), static_cast<jint>(sb.st_gid), 30259fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_rdev), static_cast<jlong>(sb.st_size), 30359fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_atime), static_cast<jlong>(sb.st_mtime), 30459fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_ctime), static_cast<jlong>(sb.st_blksize), 30559fa7163774d6930a174bc038414a4b780581957Elliott Hughes static_cast<jlong>(sb.st_blocks)); 30659fa7163774d6930a174bc038414a4b780581957Elliott Hughes} 30759fa7163774d6930a174bc038414a4b780581957Elliott Hughes 308721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughesstatic jobject makeStructStatVfs(JNIEnv* env, const struct statvfs& sb) { 3096c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes#if defined(__APPLE__) 3106c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes // Mac OS has no f_namelen field in struct statfs. 3116c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes jlong max_name_length = 255; // __DARWIN_MAXNAMLEN 3126c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes#else 313721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes jlong max_name_length = static_cast<jlong>(sb.f_namemax); 3146c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes#endif 3156c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes 316721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structStatVfsClass, "<init>", 317721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes "(JJJJJJJJJJJ)V"); 318721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes return env->NewObject(JniConstants::structStatVfsClass, ctor, 319721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes static_cast<jlong>(sb.f_bsize), 320721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes static_cast<jlong>(sb.f_frsize), 321721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes static_cast<jlong>(sb.f_blocks), 322721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes static_cast<jlong>(sb.f_bfree), 323721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes static_cast<jlong>(sb.f_bavail), 324721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes static_cast<jlong>(sb.f_files), 325721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes static_cast<jlong>(sb.f_ffree), 326721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes static_cast<jlong>(sb.f_favail), 327721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes static_cast<jlong>(sb.f_fsid), 328721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes static_cast<jlong>(sb.f_flag), 329721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes max_name_length); 33047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 33147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 3320a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructLinger(JNIEnv* env, const struct linger& l) { 3330a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structLingerClass, "<init>", "(II)V"); 3340a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return env->NewObject(JniConstants::structLingerClass, ctor, l.l_onoff, l.l_linger); 3350a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 3360a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 3370a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject makeStructTimeval(JNIEnv* env, const struct timeval& tv) { 3380a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structTimevalClass, "<init>", "(JJ)V"); 3390a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return env->NewObject(JniConstants::structTimevalClass, ctor, 3400a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes static_cast<jlong>(tv.tv_sec), static_cast<jlong>(tv.tv_usec)); 3410a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 3420a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 3433deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogersstatic jobject makeStructUcred(JNIEnv* env, const struct ucred& u __unused) { 3443deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers#ifdef __APPLE__ 3453deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers jniThrowException(env, "java/lang/UnsupportedOperationException", "unimplemented support for ucred on a Mac"); 3463deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers return NULL; 3473deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers#else 348482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structUcredClass, "<init>", "(III)V"); 349482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return env->NewObject(JniConstants::structUcredClass, ctor, u.pid, u.uid, u.gid); 3503deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers#endif 351482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes} 352482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes 3537341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject makeStructUtsname(JNIEnv* env, const struct utsname& buf) { 354d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(sysname, buf.sysname); 355d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(nodename, buf.nodename); 356d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(release, buf.release); 357d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(version, buf.version); 358d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes TO_JAVA_STRING(machine, buf.machine); 3597341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes static jmethodID ctor = env->GetMethodID(JniConstants::structUtsnameClass, "<init>", 3607341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); 3617341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes return env->NewObject(JniConstants::structUtsnameClass, ctor, 3627341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes sysname, nodename, release, version, machine); 3637341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes}; 3647341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes 365a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic bool fillIfreq(JNIEnv* env, jstring javaInterfaceName, struct ifreq& req) { 366a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes ScopedUtfChars interfaceName(env, javaInterfaceName); 367a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (interfaceName.c_str() == NULL) { 368a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return false; 369a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes } 370a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes memset(&req, 0, sizeof(req)); 371a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes strncpy(req.ifr_name, interfaceName.c_str(), sizeof(req.ifr_name)); 372a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes req.ifr_name[sizeof(req.ifr_name) - 1] = '\0'; 373a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return true; 374a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes} 375a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes 376482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughesstatic bool fillInetSocketAddress(JNIEnv* env, jint rc, jobject javaInetSocketAddress, const sockaddr_storage& ss) { 377553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes if (rc == -1 || javaInetSocketAddress == NULL) { 378553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes return true; 379553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes } 380553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes // Fill out the passed-in InetSocketAddress with the sender's IP address and port number. 381553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes jint port; 382553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes jobject sender = sockaddrToInetAddress(env, ss, &port); 383553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes if (sender == NULL) { 384553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes return false; 385553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes } 386553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes static jfieldID addressFid = env->GetFieldID(JniConstants::inetSocketAddressClass, "addr", "Ljava/net/InetAddress;"); 387553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes static jfieldID portFid = env->GetFieldID(JniConstants::inetSocketAddressClass, "port", "I"); 388553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes env->SetObjectField(javaInetSocketAddress, addressFid, sender); 389553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes env->SetIntField(javaInetSocketAddress, portFid, port); 390553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes return true; 391553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes} 392553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes 39347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject doStat(JNIEnv* env, jstring javaPath, bool isLstat) { 39447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes ScopedUtfChars path(env, javaPath); 39547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes if (path.c_str() == NULL) { 39647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return NULL; 39747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes } 39847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes struct stat sb; 39947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int rc = isLstat ? TEMP_FAILURE_RETRY(lstat(path.c_str(), &sb)) 40047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes : TEMP_FAILURE_RETRY(stat(path.c_str(), &sb)); 401dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == -1) { 4027e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, isLstat ? "lstat" : "stat"); 40347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return NULL; 40447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes } 40547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return makeStructStat(env, sb); 406ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 407ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 408482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughesstatic jobject doGetSockName(JNIEnv* env, jobject javaFd, bool is_sockname) { 409482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 410482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes sockaddr_storage ss; 411482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes sockaddr* sa = reinterpret_cast<sockaddr*>(&ss); 412482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes socklen_t byteCount = sizeof(ss); 413482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes memset(&ss, 0, byteCount); 414482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes int rc = is_sockname ? TEMP_FAILURE_RETRY(getsockname(fd, sa, &byteCount)) 415482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes : TEMP_FAILURE_RETRY(getpeername(fd, sa, &byteCount)); 416482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes if (rc == -1) { 417482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes throwErrnoException(env, is_sockname ? "getsockname" : "getpeername"); 418482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return NULL; 419482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes } 420482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return makeSocketAddress(env, ss); 421482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes} 422482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes 423d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesclass Passwd { 424d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughespublic: 425d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes Passwd(JNIEnv* env) : mEnv(env), mResult(NULL) { 426d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes mBufferSize = sysconf(_SC_GETPW_R_SIZE_MAX); 427d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes mBuffer.reset(new char[mBufferSize]); 428d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 429d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 430d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes jobject getpwnam(const char* name) { 431d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return process("getpwnam_r", getpwnam_r(name, &mPwd, mBuffer.get(), mBufferSize, &mResult)); 432d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 433d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 434d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes jobject getpwuid(uid_t uid) { 435d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return process("getpwuid_r", getpwuid_r(uid, &mPwd, mBuffer.get(), mBufferSize, &mResult)); 436d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 437d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 438d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes struct passwd* get() { 439d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return mResult; 440d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 441d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 442d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesprivate: 443d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes jobject process(const char* syscall, int error) { 444d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes if (mResult == NULL) { 445d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes errno = error; 446d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes throwErrnoException(mEnv, syscall); 447d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return NULL; 448d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 449d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return makeStructPasswd(mEnv, *mResult); 450d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 451d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 452d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes JNIEnv* mEnv; 453d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes UniquePtr<char[]> mBuffer; 454d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes size_t mBufferSize; 455d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes struct passwd mPwd; 456d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes struct passwd* mResult; 457d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes}; 458d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 459553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughesstatic jobject Posix_accept(JNIEnv* env, jobject, jobject javaFd, jobject javaInetSocketAddress) { 460553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes sockaddr_storage ss; 461553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes socklen_t sl = sizeof(ss); 462553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes memset(&ss, 0, sizeof(ss)); 463553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes sockaddr* peer = (javaInetSocketAddress != NULL) ? reinterpret_cast<sockaddr*>(&ss) : NULL; 464553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes socklen_t* peerLength = (javaInetSocketAddress != NULL) ? &sl : 0; 4651124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes jint clientFd = NET_FAILURE_RETRY(env, int, accept, javaFd, peer, peerLength); 466482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes if (clientFd == -1 || !fillInetSocketAddress(env, clientFd, javaInetSocketAddress, ss)) { 467553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes close(clientFd); 468553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes return NULL; 469553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes } 470553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes return (clientFd != -1) ? jniCreateFileDescriptor(env, clientFd) : NULL; 471553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes} 472553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes 473ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jboolean Posix_access(JNIEnv* env, jobject, jstring javaPath, jint mode) { 474ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes ScopedUtfChars path(env, javaPath); 475ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes if (path.c_str() == NULL) { 476ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return JNI_FALSE; 477ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes } 47847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int rc = TEMP_FAILURE_RETRY(access(path.c_str(), mode)); 479dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == -1) { 4807e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "access"); 481dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes } 482ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return (rc == 0); 483ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 484ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 485da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughesstatic void Posix_bind(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) { 486da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes sockaddr_storage ss; 487482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes socklen_t sa_len; 488482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes if (!inetAddressToSockaddr(env, javaAddress, port, ss, sa_len)) { 489da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes return; 490da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes } 491da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss); 4921124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes // We don't need the return value because we'll already have thrown. 493482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes (void) NET_FAILURE_RETRY(env, int, bind, javaFd, sa, sa_len); 494da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes} 495da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes 496b7190190e0ef8de883c952efb319ce7748831faaElliott Hughesstatic void Posix_chmod(JNIEnv* env, jobject, jstring javaPath, jint mode) { 497b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes ScopedUtfChars path(env, javaPath); 498b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes if (path.c_str() == NULL) { 499b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes return; 500b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes } 501b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes throwIfMinusOne(env, "chmod", TEMP_FAILURE_RETRY(chmod(path.c_str(), mode))); 502b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes} 503b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes 50444f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughesstatic void Posix_chown(JNIEnv* env, jobject, jstring javaPath, jint uid, jint gid) { 50544f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes ScopedUtfChars path(env, javaPath); 50644f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes if (path.c_str() == NULL) { 50744f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes return; 50844f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes } 50944f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes throwIfMinusOne(env, "chown", TEMP_FAILURE_RETRY(chown(path.c_str(), uid, gid))); 51044f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes} 51144f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes 512462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughesstatic void Posix_close(JNIEnv* env, jobject, jobject javaFd) { 513462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // Get the FileDescriptor's 'fd' field and clear it. 514462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // We need to do this before we can throw an IOException (http://b/3222087). 515462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 516462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes jniSetFileDescriptorOfFD(env, javaFd, -1); 517462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes 518462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // Even if close(2) fails with EINTR, the fd will have been closed. 519462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone else's fd. 520462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes // http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html 521462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes throwIfMinusOne(env, "close", close(fd)); 522462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes} 523462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes 524996bf79565ac88402920bd826d6f85952c83be20Elliott Hughesstatic void Posix_connect(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) { 525996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes sockaddr_storage ss; 526482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes socklen_t sa_len; 527482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes if (!inetAddressToSockaddr(env, javaAddress, port, ss, sa_len)) { 528996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes return; 529996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes } 530996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss); 5311124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes // We don't need the return value because we'll already have thrown. 532482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes (void) NET_FAILURE_RETRY(env, int, connect, javaFd, sa, sa_len); 533996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes} 534996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes 535396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jobject Posix_dup(JNIEnv* env, jobject, jobject javaOldFd) { 536396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes int oldFd = jniGetFDFromFileDescriptor(env, javaOldFd); 537396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes int newFd = throwIfMinusOne(env, "dup", TEMP_FAILURE_RETRY(dup(oldFd))); 538396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return (newFd != -1) ? jniCreateFileDescriptor(env, newFd) : NULL; 539396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 540396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 541396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jobject Posix_dup2(JNIEnv* env, jobject, jobject javaOldFd, jint newFd) { 542396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes int oldFd = jniGetFDFromFileDescriptor(env, javaOldFd); 543396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes int fd = throwIfMinusOne(env, "dup2", TEMP_FAILURE_RETRY(dup2(oldFd, newFd))); 544396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return (fd != -1) ? jniCreateFileDescriptor(env, fd) : NULL; 545396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 546396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 547ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jobjectArray Posix_environ(JNIEnv* env, jobject) { 548ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes extern char** environ; // Standard, but not in any header file. 549ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return toStringArray(env, environ); 550ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 551ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 552798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevichstatic void Posix_execve(JNIEnv* env, jobject, jstring javaFilename, jobjectArray javaArgv, jobjectArray javaEnvp) { 553798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich ScopedUtfChars path(env, javaFilename); 554798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich if (path.c_str() == NULL) { 555798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich return; 556798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich } 557798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich 558ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes ExecStrings argv(env, javaArgv); 559ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes ExecStrings envp(env, javaEnvp); 560ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes execve(path.c_str(), argv.get(), envp.get()); 561798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich 562798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich throwErrnoException(env, "execve"); 563798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich} 564798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich 565798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevichstatic void Posix_execv(JNIEnv* env, jobject, jstring javaFilename, jobjectArray javaArgv) { 566798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich ScopedUtfChars path(env, javaFilename); 567798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich if (path.c_str() == NULL) { 568798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich return; 569798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich } 570798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich 571ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes ExecStrings argv(env, javaArgv); 572ad9208affa02f92a6b85354a33123d51d80febe1Elliott Hughes execv(path.c_str(), argv.get()); 573798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich 574798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich throwErrnoException(env, "execv"); 575798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich} 576798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich 57744f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughesstatic void Posix_fchmod(JNIEnv* env, jobject, jobject javaFd, jint mode) { 57844f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 57944f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes throwIfMinusOne(env, "fchmod", TEMP_FAILURE_RETRY(fchmod(fd, mode))); 58044f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes} 58144f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes 58244f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughesstatic void Posix_fchown(JNIEnv* env, jobject, jobject javaFd, jint uid, jint gid) { 58344f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 58444f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes throwIfMinusOne(env, "fchown", TEMP_FAILURE_RETRY(fchown(fd, uid, gid))); 58544f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes} 58644f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes 587fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlVoid(JNIEnv* env, jobject, jobject javaFd, jint cmd) { 588fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 589fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd))); 590fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes} 591fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 592fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlLong(JNIEnv* env, jobject, jobject javaFd, jint cmd, jlong arg) { 593fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 594fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, arg))); 595fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes} 596fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 597fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesstatic jint Posix_fcntlFlock(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaFlock) { 598fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID typeFid = env->GetFieldID(JniConstants::structFlockClass, "l_type", "S"); 599fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID whenceFid = env->GetFieldID(JniConstants::structFlockClass, "l_whence", "S"); 600fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID startFid = env->GetFieldID(JniConstants::structFlockClass, "l_start", "J"); 601fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID lenFid = env->GetFieldID(JniConstants::structFlockClass, "l_len", "J"); 602fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes static jfieldID pidFid = env->GetFieldID(JniConstants::structFlockClass, "l_pid", "I"); 603fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 604fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes struct flock64 lock; 605fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes memset(&lock, 0, sizeof(lock)); 606fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_type = env->GetShortField(javaFlock, typeFid); 607fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_whence = env->GetShortField(javaFlock, whenceFid); 608fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_start = env->GetLongField(javaFlock, startFid); 609fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_len = env->GetLongField(javaFlock, lenFid); 610fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes lock.l_pid = env->GetIntField(javaFlock, pidFid); 611fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 6128add3229ceb13f2aa5d15b84cc59f5ecc6b90214Neil Fuller int rc = IO_FAILURE_RETRY(env, int, fcntl, javaFd, cmd, &lock); 613fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes if (rc != -1) { 614fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetShortField(javaFlock, typeFid, lock.l_type); 615fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetShortField(javaFlock, whenceFid, lock.l_whence); 616fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetLongField(javaFlock, startFid, lock.l_start); 617fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetLongField(javaFlock, lenFid, lock.l_len); 618fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes env->SetIntField(javaFlock, pidFid, lock.l_pid); 619fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes } 620fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes return rc; 621fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes} 622fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes 62352724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fdatasync(JNIEnv* env, jobject, jobject javaFd) { 62452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 6257e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "fdatasync", TEMP_FAILURE_RETRY(fdatasync(fd))); 62652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes} 62752724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes 62847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_fstat(JNIEnv* env, jobject, jobject javaFd) { 62947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 63047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes struct stat sb; 63147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes int rc = TEMP_FAILURE_RETRY(fstat(fd, &sb)); 632dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes if (rc == -1) { 6337e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "fstat"); 63447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return NULL; 63547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes } 63647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return makeStructStat(env, sb); 63747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 63847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 639721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughesstatic jobject Posix_fstatvfs(JNIEnv* env, jobject, jobject javaFd) { 64059fa7163774d6930a174bc038414a4b780581957Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 641721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes struct statvfs sb; 642721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes int rc = TEMP_FAILURE_RETRY(fstatvfs(fd, &sb)); 64359fa7163774d6930a174bc038414a4b780581957Elliott Hughes if (rc == -1) { 644721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes throwErrnoException(env, "fstatvfs"); 64559fa7163774d6930a174bc038414a4b780581957Elliott Hughes return NULL; 64659fa7163774d6930a174bc038414a4b780581957Elliott Hughes } 647721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes return makeStructStatVfs(env, sb); 64859fa7163774d6930a174bc038414a4b780581957Elliott Hughes} 64959fa7163774d6930a174bc038414a4b780581957Elliott Hughes 65052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fsync(JNIEnv* env, jobject, jobject javaFd) { 65152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 6527e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "fsync", TEMP_FAILURE_RETRY(fsync(fd))); 653f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes} 654f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes 655f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughesstatic void Posix_ftruncate(JNIEnv* env, jobject, jobject javaFd, jlong length) { 656f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 6577e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "ftruncate", TEMP_FAILURE_RETRY(ftruncate64(fd, length))); 65852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes} 65952724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes 6604f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic jstring Posix_gai_strerror(JNIEnv* env, jobject, jint error) { 6614f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return env->NewStringUTF(gai_strerror(error)); 6624f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 6634f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 6641c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughesstatic jobjectArray Posix_getaddrinfo(JNIEnv* env, jobject, jstring javaNode, jobject javaHints) { 6651c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes ScopedUtfChars node(env, javaNode); 6661c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (node.c_str() == NULL) { 6671c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 6681c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 6691c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 6701c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes static jfieldID flagsFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_flags", "I"); 6711c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes static jfieldID familyFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_family", "I"); 6721c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes static jfieldID socktypeFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_socktype", "I"); 6731c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes static jfieldID protocolFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_protocol", "I"); 6741c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 6751c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes addrinfo hints; 6761c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes memset(&hints, 0, sizeof(hints)); 6771c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes hints.ai_flags = env->GetIntField(javaHints, flagsFid); 6781c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes hints.ai_family = env->GetIntField(javaHints, familyFid); 6791c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes hints.ai_socktype = env->GetIntField(javaHints, socktypeFid); 6801c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes hints.ai_protocol = env->GetIntField(javaHints, protocolFid); 6811c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 6821c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes addrinfo* addressList = NULL; 68375cf14944d476670f6f915e5efd849e238a16250Elliott Hughes errno = 0; 6841c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes int rc = getaddrinfo(node.c_str(), NULL, &hints, &addressList); 6851c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes UniquePtr<addrinfo, addrinfo_deleter> addressListDeleter(addressList); 6861c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (rc != 0) { 6871c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes throwGaiException(env, "getaddrinfo", rc); 6881c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 6891c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 6901c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 6911c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes // Count results so we know how to size the output array. 6921c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes int addressCount = 0; 6931c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes for (addrinfo* ai = addressList; ai != NULL; ai = ai->ai_next) { 6941c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) { 6951c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes ++addressCount; 6961c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } else { 697679cf68b607e9b4a3beb8bcdee06868ae583386fSteve Block ALOGE("getaddrinfo unexpected ai_family %i", ai->ai_family); 6981c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 6991c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 7001c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (addressCount == 0) { 7011c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 7021c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 7031c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 7041c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes // Prepare output array. 7051c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes jobjectArray result = env->NewObjectArray(addressCount, JniConstants::inetAddressClass, NULL); 7061c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (result == NULL) { 7071c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 7081c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 7091c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 7101c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes // Examine returned addresses one by one, save them in the output array. 7111c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes int index = 0; 7121c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes for (addrinfo* ai = addressList; ai != NULL; ai = ai->ai_next) { 7131c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) { 7141c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes // Unknown address family. Skip this address. 715679cf68b607e9b4a3beb8bcdee06868ae583386fSteve Block ALOGE("getaddrinfo unexpected ai_family %i", ai->ai_family); 7161c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes continue; 7171c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 7181c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 7191c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes // Convert each IP address into a Java byte array. 720482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes sockaddr_storage& address = *reinterpret_cast<sockaddr_storage*>(ai->ai_addr); 72123ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes ScopedLocalRef<jobject> inetAddress(env, sockaddrToInetAddress(env, address, NULL)); 7221c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (inetAddress.get() == NULL) { 7231c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 7241c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 7251c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes env->SetObjectArrayElement(result, index, inetAddress.get()); 7261c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes ++index; 7271c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 7281c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return result; 7291c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes} 7301c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 731396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getegid(JNIEnv*, jobject) { 732396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return getegid(); 733396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 734396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 735396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_geteuid(JNIEnv*, jobject) { 736396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return geteuid(); 737396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 738396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 739396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getgid(JNIEnv*, jobject) { 740396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return getgid(); 741396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 742396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 743ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic jstring Posix_getenv(JNIEnv* env, jobject, jstring javaName) { 744ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes ScopedUtfChars name(env, javaName); 745ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes if (name.c_str() == NULL) { 746ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes return NULL; 747ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes } 748ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes return env->NewStringUTF(getenv(name.c_str())); 749ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes} 750ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 7514f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughesstatic jstring Posix_getnameinfo(JNIEnv* env, jobject, jobject javaAddress, jint flags) { 7524f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes sockaddr_storage ss; 753482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes socklen_t sa_len; 754482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes if (!inetAddressToSockaddrVerbatim(env, javaAddress, 0, ss, sa_len)) { 7554f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return NULL; 7564f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes } 7574f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes char buf[NI_MAXHOST]; // NI_MAXHOST is longer than INET6_ADDRSTRLEN. 75875cf14944d476670f6f915e5efd849e238a16250Elliott Hughes errno = 0; 759482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes int rc = getnameinfo(reinterpret_cast<sockaddr*>(&ss), sa_len, buf, sizeof(buf), NULL, 0, flags); 7604f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes if (rc != 0) { 7614f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes throwGaiException(env, "getnameinfo", rc); 7624f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return NULL; 7634f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes } 7644f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes return env->NewStringUTF(buf); 7654f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes} 7664f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes 767482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughesstatic jobject Posix_getpeername(JNIEnv* env, jobject, jobject javaFd) { 768482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return doGetSockName(env, javaFd, false); 769482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes} 770482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes 771396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getpid(JNIEnv*, jobject) { 772396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return getpid(); 773396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 774396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 775396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getppid(JNIEnv*, jobject) { 776396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return getppid(); 777396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 778396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 779d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesstatic jobject Posix_getpwnam(JNIEnv* env, jobject, jstring javaName) { 780d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes ScopedUtfChars name(env, javaName); 781d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes if (name.c_str() == NULL) { 782d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return NULL; 783d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes } 784d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return Passwd(env).getpwnam(name.c_str()); 785d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes} 786d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 787d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughesstatic jobject Posix_getpwuid(JNIEnv* env, jobject, jint uid) { 788d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes return Passwd(env).getpwuid(uid); 789d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes} 790d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes 7910a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockname(JNIEnv* env, jobject, jobject javaFd) { 792482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return doGetSockName(env, javaFd, true); 7930a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 7940a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 7950a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 7960a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 7970a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes u_char result = 0; 7980a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(result); 7990a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size))); 8000a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return result; 8010a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 8020a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 8030a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptInAddr(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 8040a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 8050a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes sockaddr_storage ss; 8060a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes memset(&ss, 0, sizeof(ss)); 8070a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes ss.ss_family = AF_INET; // This is only for the IPv4-only IP_MULTICAST_IF. 8080a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes sockaddr_in* sa = reinterpret_cast<sockaddr_in*>(&ss); 8090a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(sa->sin_addr); 8100a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &sa->sin_addr, &size)); 8110a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (rc == -1) { 8120a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwErrnoException(env, "getsockopt"); 8130a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 8140a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 815482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return sockaddrToInetAddress(env, ss, NULL); 8160a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 8170a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 8180a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jint Posix_getsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 8190a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 8200a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes jint result = 0; 8210a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(result); 8220a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size))); 8230a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return result; 8240a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 8250a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 8260a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 8270a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 8280a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes struct linger l; 8290a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(l); 8300a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes memset(&l, 0, size); 8310a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &l, &size)); 8320a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (rc == -1) { 8330a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwErrnoException(env, "getsockopt"); 8340a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 8350a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 8360a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return makeStructLinger(env, l); 8370a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 8380a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 8390a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesstatic jobject Posix_getsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 8400a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 8410a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes struct timeval tv; 8420a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes socklen_t size = sizeof(tv); 8430a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes memset(&tv, 0, size); 8440a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &tv, &size)); 8450a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes if (rc == -1) { 8460a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes throwErrnoException(env, "getsockopt"); 8470a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return NULL; 8480a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes } 8490a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes return makeStructTimeval(env, tv); 8500a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes} 8510a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes 852482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughesstatic jobject Posix_getsockoptUcred(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 853482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 854482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes struct ucred u; 855482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes socklen_t size = sizeof(u); 856482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes memset(&u, 0, size); 857482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &u, &size)); 858482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes if (rc == -1) { 859482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes throwErrnoException(env, "getsockopt"); 860482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return NULL; 861482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes } 862482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return makeStructUcred(env, u); 863482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes} 864482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes 8653deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogersstatic jint Posix_gettid(JNIEnv* env __unused, jobject) { 8663deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers#if defined(__APPLE__) 8673deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers uint64_t owner; 8683deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers int rc = pthread_threadid_np(NULL, &owner); // Requires Mac OS 10.6 8693deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers if (rc != 0) { 8703deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers throwErrnoException(env, "gettid"); 8713deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers return NULL; 8723deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers } 8733deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers return static_cast<jint>(owner); 8743deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers#else 875069525a39125c203b658c805685b6045a7d4dfebElliott Hughes // Neither bionic nor glibc exposes gettid(2). 876069525a39125c203b658c805685b6045a7d4dfebElliott Hughes return syscall(__NR_gettid); 8773deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers#endif 878069525a39125c203b658c805685b6045a7d4dfebElliott Hughes} 879069525a39125c203b658c805685b6045a7d4dfebElliott Hughes 880396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic jint Posix_getuid(JNIEnv*, jobject) { 881396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes return getuid(); 882396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 883396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 884a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic jstring Posix_if_indextoname(JNIEnv* env, jobject, jint index) { 885a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes char buf[IF_NAMESIZE]; 886a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes char* name = if_indextoname(index, buf); 887a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes // if_indextoname(3) returns NULL on failure, which will come out of NewStringUTF unscathed. 888a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes // There's no useful information in errno, so we don't bother throwing. Callers can null-check. 889a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return env->NewStringUTF(name); 890a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes} 891a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes 892fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughesstatic jobject Posix_inet_pton(JNIEnv* env, jobject, jint family, jstring javaName) { 8931c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes ScopedUtfChars name(env, javaName); 8941c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes if (name.c_str() == NULL) { 8951c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 8961c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 8971c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes sockaddr_storage ss; 8981c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes memset(&ss, 0, sizeof(ss)); 899fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // sockaddr_in and sockaddr_in6 are at the same address, so we can use either here. 900fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes void* dst = &reinterpret_cast<sockaddr_in*>(&ss)->sin_addr; 901fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes if (inet_pton(family, name.c_str(), dst) != 1) { 9021c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes return NULL; 9031c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes } 904fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes ss.ss_family = family; 905482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return sockaddrToInetAddress(env, ss, NULL); 9061c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes} 9071c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes 908a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic jobject Posix_ioctlInetAddress(JNIEnv* env, jobject, jobject javaFd, jint cmd, jstring javaInterfaceName) { 909a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes struct ifreq req; 910a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (!fillIfreq(env, javaInterfaceName, req)) { 911a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return NULL; 912a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes } 913a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 914a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &req))); 915a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (rc == -1) { 916a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes return NULL; 917a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes } 918482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return sockaddrToInetAddress(env, reinterpret_cast<sockaddr_storage&>(req.ifr_addr), NULL); 919a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes} 920a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes 9218b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jint Posix_ioctlInt(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaArg) { 9228b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes // This is complicated because ioctls may return their result by updating their argument 9238b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes // or via their return value, so we need to support both. 924461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 9258b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes static jfieldID valueFid = env->GetFieldID(JniConstants::mutableIntClass, "value", "I"); 9268b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes jint arg = env->GetIntField(javaArg, valueFid); 9278b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &arg))); 9288b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes if (!env->ExceptionCheck()) { 9298b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes env->SetIntField(javaArg, valueFid, arg); 9308b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes } 9318b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes return rc; 932461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes} 933461d0d860814c68154d8dd06d24f94118f33d28aElliott Hughes 9349a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughesstatic jboolean Posix_isatty(JNIEnv* env, jobject, jobject javaFd) { 9359a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 936bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig return TEMP_FAILURE_RETRY(isatty(fd)) == 1; 9379a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes} 9389a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes 939a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughesstatic void Posix_kill(JNIEnv* env, jobject, jint pid, jint sig) { 940a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes throwIfMinusOne(env, "kill", TEMP_FAILURE_RETRY(kill(pid, sig))); 941a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes} 942a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes 94344f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughesstatic void Posix_lchown(JNIEnv* env, jobject, jstring javaPath, jint uid, jint gid) { 94444f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes ScopedUtfChars path(env, javaPath); 94544f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes if (path.c_str() == NULL) { 94644f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes return; 94744f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes } 94844f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes throwIfMinusOne(env, "lchown", TEMP_FAILURE_RETRY(lchown(path.c_str(), uid, gid))); 94944f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes} 95044f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes 95104428d61d7000e17ab21d08a1d672c34eb68f6e2Elliott Hughesstatic void Posix_link(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) { 95204428d61d7000e17ab21d08a1d672c34eb68f6e2Elliott Hughes ScopedUtfChars oldPath(env, javaOldPath); 95304428d61d7000e17ab21d08a1d672c34eb68f6e2Elliott Hughes if (oldPath.c_str() == NULL) { 95404428d61d7000e17ab21d08a1d672c34eb68f6e2Elliott Hughes return; 95504428d61d7000e17ab21d08a1d672c34eb68f6e2Elliott Hughes } 95604428d61d7000e17ab21d08a1d672c34eb68f6e2Elliott Hughes ScopedUtfChars newPath(env, javaNewPath); 95704428d61d7000e17ab21d08a1d672c34eb68f6e2Elliott Hughes if (newPath.c_str() == NULL) { 95804428d61d7000e17ab21d08a1d672c34eb68f6e2Elliott Hughes return; 95904428d61d7000e17ab21d08a1d672c34eb68f6e2Elliott Hughes } 96004428d61d7000e17ab21d08a1d672c34eb68f6e2Elliott Hughes throwIfMinusOne(env, "link", TEMP_FAILURE_RETRY(link(oldPath.c_str(), newPath.c_str()))); 96104428d61d7000e17ab21d08a1d672c34eb68f6e2Elliott Hughes} 96204428d61d7000e17ab21d08a1d672c34eb68f6e2Elliott Hughes 963e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughesstatic void Posix_listen(JNIEnv* env, jobject, jobject javaFd, jint backlog) { 964e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 965e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes throwIfMinusOne(env, "listen", TEMP_FAILURE_RETRY(listen(fd, backlog))); 966e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes} 967e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes 968dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughesstatic jlong Posix_lseek(JNIEnv* env, jobject, jobject javaFd, jlong offset, jint whence) { 969dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 9707e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes return throwIfMinusOne(env, "lseek", TEMP_FAILURE_RETRY(lseek64(fd, offset, whence))); 971dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes} 972dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes 97347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_lstat(JNIEnv* env, jobject, jstring javaPath) { 97447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return doStat(env, javaPath, true); 97547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 97647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 9770f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughesstatic void Posix_mincore(JNIEnv* env, jobject, jlong address, jlong byteCount, jbyteArray javaVector) { 9780f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes ScopedByteArrayRW vector(env, javaVector); 9790f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes if (vector.get() == NULL) { 9800f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes return; 9810f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes } 9820f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 9830f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes unsigned char* vec = reinterpret_cast<unsigned char*>(vector.get()); 9840f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes throwIfMinusOne(env, "mincore", TEMP_FAILURE_RETRY(mincore(ptr, byteCount, vec))); 9850f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes} 9860f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes 987c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_mkdir(JNIEnv* env, jobject, jstring javaPath, jint mode) { 988c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes ScopedUtfChars path(env, javaPath); 989c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes if (path.c_str() == NULL) { 990c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes return; 991c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes } 992c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes throwIfMinusOne(env, "mkdir", TEMP_FAILURE_RETRY(mkdir(path.c_str(), mode))); 993c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes} 994c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes 995f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fullerstatic void Posix_mkfifo(JNIEnv* env, jobject, jstring javaPath, jint mode) { 996f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller ScopedUtfChars path(env, javaPath); 997f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller if (path.c_str() == NULL) { 998f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller return; 999f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller } 1000f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller throwIfMinusOne(env, "mkfifo", TEMP_FAILURE_RETRY(mkfifo(path.c_str(), mode))); 1001f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller} 1002f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller 10037e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_mlock(JNIEnv* env, jobject, jlong address, jlong byteCount) { 10047e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 10057e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes throwIfMinusOne(env, "mlock", TEMP_FAILURE_RETRY(mlock(ptr, byteCount))); 10067e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes} 10077e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes 10087e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic jlong Posix_mmap(JNIEnv* env, jobject, jlong address, jlong byteCount, jint prot, jint flags, jobject javaFd, jlong offset) { 10097e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 10107e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* suggestedPtr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 10117e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* ptr = mmap(suggestedPtr, byteCount, prot, flags, fd, offset); 10127e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes if (ptr == MAP_FAILED) { 10137e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "mmap"); 10147e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes } 10157e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes return static_cast<jlong>(reinterpret_cast<uintptr_t>(ptr)); 10167e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes} 10177e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes 10187e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_msync(JNIEnv* env, jobject, jlong address, jlong byteCount, jint flags) { 10197e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 10207e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "msync", TEMP_FAILURE_RETRY(msync(ptr, byteCount, flags))); 10217e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes} 10227e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes 10237e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_munlock(JNIEnv* env, jobject, jlong address, jlong byteCount) { 10247e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 10257e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes throwIfMinusOne(env, "munlock", TEMP_FAILURE_RETRY(munlock(ptr, byteCount))); 10267e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes} 10277e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes 10287e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_munmap(JNIEnv* env, jobject, jlong address, jlong byteCount) { 10297e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 10307e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwIfMinusOne(env, "munmap", TEMP_FAILURE_RETRY(munmap(ptr, byteCount))); 10317e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes} 10327e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes 10330ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughesstatic jobject Posix_open(JNIEnv* env, jobject, jstring javaPath, jint flags, jint mode) { 10340ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes ScopedUtfChars path(env, javaPath); 10350ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes if (path.c_str() == NULL) { 10360ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes return NULL; 10370ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes } 10380ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes int fd = throwIfMinusOne(env, "open", TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode))); 10390ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL; 10400ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes} 10410ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes 104241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughesstatic jobjectArray Posix_pipe(JNIEnv* env, jobject) { 104341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes int fds[2]; 104441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes throwIfMinusOne(env, "pipe", TEMP_FAILURE_RETRY(pipe(&fds[0]))); 104541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes jobjectArray result = env->NewObjectArray(2, JniConstants::fileDescriptorClass, NULL); 104641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes if (result == NULL) { 104741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return NULL; 104841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 104941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes for (int i = 0; i < 2; ++i) { 105041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes ScopedLocalRef<jobject> fd(env, jniCreateFileDescriptor(env, fds[i])); 105141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes if (fd.get() == NULL) { 105241f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return NULL; 105341f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 105441f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes env->SetObjectArrayElement(result, i, fd.get()); 105541f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes if (env->ExceptionCheck()) { 105641f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return NULL; 105741f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 105841f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes } 105941f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes return result; 106041f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes} 106141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes 106270c820401677ca251ad09ac64cc23c760764e75dElliott Hughesstatic jint Posix_poll(JNIEnv* env, jobject, jobjectArray javaStructs, jint timeoutMs) { 106370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes static jfieldID fdFid = env->GetFieldID(JniConstants::structPollfdClass, "fd", "Ljava/io/FileDescriptor;"); 106470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes static jfieldID eventsFid = env->GetFieldID(JniConstants::structPollfdClass, "events", "S"); 106570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes static jfieldID reventsFid = env->GetFieldID(JniConstants::structPollfdClass, "revents", "S"); 106670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 10675d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes // Turn the Java android.system.StructPollfd[] into a C++ struct pollfd[]. 106870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes size_t arrayLength = env->GetArrayLength(javaStructs); 106970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes UniquePtr<struct pollfd[]> fds(new struct pollfd[arrayLength]); 107070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes memset(fds.get(), 0, sizeof(struct pollfd) * arrayLength); 107170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes size_t count = 0; // Some trailing array elements may be irrelevant. (See below.) 107270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes for (size_t i = 0; i < arrayLength; ++i) { 107370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes ScopedLocalRef<jobject> javaStruct(env, env->GetObjectArrayElement(javaStructs, i)); 107470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes if (javaStruct.get() == NULL) { 107570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes break; // We allow trailing nulls in the array for caller convenience. 107670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 107770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes ScopedLocalRef<jobject> javaFd(env, env->GetObjectField(javaStruct.get(), fdFid)); 107870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes if (javaFd.get() == NULL) { 107970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes break; // We also allow callers to just clear the fd field (this is what Selector does). 108070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 108170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes fds[count].fd = jniGetFDFromFileDescriptor(env, javaFd.get()); 108270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes fds[count].events = env->GetShortField(javaStruct.get(), eventsFid); 108370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes ++count; 108470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 108570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 1086f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller std::vector<AsynchronousCloseMonitor*> monitors; 1087796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes for (size_t i = 0; i < count; ++i) { 1088f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller monitors.push_back(new AsynchronousCloseMonitor(fds[i].fd)); 1089796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes } 1090796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes int rc = poll(fds.get(), count, timeoutMs); 1091796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes for (size_t i = 0; i < monitors.size(); ++i) { 1092796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes delete monitors[i]; 1093796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes } 109470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes if (rc == -1) { 109570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes throwErrnoException(env, "poll"); 109670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes return -1; 109770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 109870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 10995d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes // Update the revents fields in the Java android.system.StructPollfd[]. 110098b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes for (size_t i = 0; i < count; ++i) { 110170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes ScopedLocalRef<jobject> javaStruct(env, env->GetObjectArrayElement(javaStructs, i)); 110298b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes if (javaStruct.get() == NULL) { 110398b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes return -1; 110498b23d4d9894d21be1eacf498f48aeb707ca1e41Elliott Hughes } 110570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes env->SetShortField(javaStruct.get(), reventsFid, fds[i].revents); 110670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 110770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes return rc; 110870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes} 110970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 11103deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogersstatic void Posix_posix_fallocate(JNIEnv* env, jobject, jobject javaFd __unused, 11113deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers jlong offset __unused, jlong length __unused) { 11123deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers#ifdef __APPLE__ 11133deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers jniThrowException(env, "java/lang/UnsupportedOperationException", 11143deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers "fallocate doesn't exist on a Mac"); 11153deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers#else 111611f07d30d2e344b48f132ec6ed105d85423052c2Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 111711f07d30d2e344b48f132ec6ed105d85423052c2Elliott Hughes errno = TEMP_FAILURE_RETRY(posix_fallocate64(fd, offset, length)); 111811f07d30d2e344b48f132ec6ed105d85423052c2Elliott Hughes if (errno != 0) { 111911f07d30d2e344b48f132ec6ed105d85423052c2Elliott Hughes throwErrnoException(env, "posix_fallocate"); 112011f07d30d2e344b48f132ec6ed105d85423052c2Elliott Hughes } 11213deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers#endif 112211f07d30d2e344b48f132ec6ed105d85423052c2Elliott Hughes} 112311f07d30d2e344b48f132ec6ed105d85423052c2Elliott Hughes 11243deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogersstatic jint Posix_prctl(JNIEnv* env, jobject, jint option __unused, jlong arg2 __unused, 11253deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers jlong arg3 __unused, jlong arg4 __unused, jlong arg5 __unused) { 11263deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers#ifdef __APPLE__ 11273deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers jniThrowException(env, "java/lang/UnsupportedOperationException", "prctl doesn't exist on a Mac"); 11283deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers return 0; 11293deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers#else 11305215e4c0db7530519981f1e505e6db82401802f2Nick Kralevich int result = prctl(static_cast<int>(option), 11315215e4c0db7530519981f1e505e6db82401802f2Nick Kralevich static_cast<unsigned long>(arg2), static_cast<unsigned long>(arg3), 11325215e4c0db7530519981f1e505e6db82401802f2Nick Kralevich static_cast<unsigned long>(arg4), static_cast<unsigned long>(arg5)); 11335215e4c0db7530519981f1e505e6db82401802f2Nick Kralevich return throwIfMinusOne(env, "prctl", result); 11343deabd220cf7b1ca0824987e90b8ecc07087f05aIan Rogers#endif 11355215e4c0db7530519981f1e505e6db82401802f2Nick Kralevich} 11365215e4c0db7530519981f1e505e6db82401802f2Nick Kralevich 1137e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughesstatic jint Posix_preadBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount, jlong offset) { 1138e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes ScopedBytesRW bytes(env, javaBytes); 1139e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes if (bytes.get() == NULL) { 1140e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes return -1; 1141e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes } 1142f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller return IO_FAILURE_RETRY(env, ssize_t, pread64, javaFd, bytes.get() + byteOffset, byteCount, offset); 1143e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes} 1144e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes 1145e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughesstatic jint Posix_pwriteBytes(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount, jlong offset) { 1146e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes ScopedBytesRO bytes(env, javaBytes); 1147e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes if (bytes.get() == NULL) { 1148e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes return -1; 1149e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes } 1150f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller return IO_FAILURE_RETRY(env, ssize_t, pwrite64, javaFd, bytes.get() + byteOffset, byteCount, offset); 1151e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes} 1152e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes 11530568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughesstatic jint Posix_readBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount) { 11540568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes ScopedBytesRW bytes(env, javaBytes); 115526c7025a7a919044771fb89031161bd26fe03032Elliott Hughes if (bytes.get() == NULL) { 115626c7025a7a919044771fb89031161bd26fe03032Elliott Hughes return -1; 115726c7025a7a919044771fb89031161bd26fe03032Elliott Hughes } 1158f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller return IO_FAILURE_RETRY(env, ssize_t, read, javaFd, bytes.get() + byteOffset, byteCount); 115926c7025a7a919044771fb89031161bd26fe03032Elliott Hughes} 116026c7025a7a919044771fb89031161bd26fe03032Elliott Hughes 11610b6b3e10459fc3a3f4dd280dab8d4a145f7bf2f0Elliott Hughesstatic jstring Posix_readlink(JNIEnv* env, jobject, jstring javaPath) { 11620b6b3e10459fc3a3f4dd280dab8d4a145f7bf2f0Elliott Hughes ScopedUtfChars path(env, javaPath); 11630b6b3e10459fc3a3f4dd280dab8d4a145f7bf2f0Elliott Hughes if (path.c_str() == NULL) { 11640b6b3e10459fc3a3f4dd280dab8d4a145f7bf2f0Elliott Hughes return NULL; 11650b6b3e10459fc3a3f4dd280dab8d4a145f7bf2f0Elliott Hughes } 11660b6b3e10459fc3a3f4dd280dab8d4a145f7bf2f0Elliott Hughes 11670b6b3e10459fc3a3f4dd280dab8d4a145f7bf2f0Elliott Hughes std::string result; 11680b6b3e10459fc3a3f4dd280dab8d4a145f7bf2f0Elliott Hughes if (!readlink(path.c_str(), result)) { 11690b6b3e10459fc3a3f4dd280dab8d4a145f7bf2f0Elliott Hughes throwErrnoException(env, "readlink"); 11700b6b3e10459fc3a3f4dd280dab8d4a145f7bf2f0Elliott Hughes return NULL; 11710b6b3e10459fc3a3f4dd280dab8d4a145f7bf2f0Elliott Hughes } 11720b6b3e10459fc3a3f4dd280dab8d4a145f7bf2f0Elliott Hughes return env->NewStringUTF(result.c_str()); 11730b6b3e10459fc3a3f4dd280dab8d4a145f7bf2f0Elliott Hughes} 11740b6b3e10459fc3a3f4dd280dab8d4a145f7bf2f0Elliott Hughes 1175bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_readv(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) { 11768dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes IoVec<ScopedBytesRW> ioVec(env, env->GetArrayLength(buffers)); 1177bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (!ioVec.init(buffers, offsets, byteCounts)) { 1178bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return -1; 1179bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 1180f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller return IO_FAILURE_RETRY(env, ssize_t, readv, javaFd, ioVec.get(), ioVec.size()); 1181bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes} 1182bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 118323ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughesstatic jint Posix_recvfromBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount, jint flags, jobject javaInetSocketAddress) { 118423ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes ScopedBytesRW bytes(env, javaBytes); 118523ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes if (bytes.get() == NULL) { 118623ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes return -1; 118723ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes } 118823ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes sockaddr_storage ss; 118923ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes socklen_t sl = sizeof(ss); 119023ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes memset(&ss, 0, sizeof(ss)); 119123ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes sockaddr* from = (javaInetSocketAddress != NULL) ? reinterpret_cast<sockaddr*>(&ss) : NULL; 119223ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes socklen_t* fromLength = (javaInetSocketAddress != NULL) ? &sl : 0; 11931124a814d698ed6fd68ac501f5dddfa103d656fdElliott Hughes jint recvCount = NET_FAILURE_RETRY(env, ssize_t, recvfrom, javaFd, bytes.get() + byteOffset, byteCount, flags, from, fromLength); 1194482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes fillInetSocketAddress(env, recvCount, javaInetSocketAddress, ss); 119523ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes return recvCount; 119623ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes} 119723ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes 1198c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughesstatic void Posix_remove(JNIEnv* env, jobject, jstring javaPath) { 1199c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes ScopedUtfChars path(env, javaPath); 1200c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes if (path.c_str() == NULL) { 1201c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes return; 1202c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes } 1203c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes throwIfMinusOne(env, "remove", TEMP_FAILURE_RETRY(remove(path.c_str()))); 1204c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes} 1205c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes 1206a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_rename(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) { 1207a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars oldPath(env, javaOldPath); 1208a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (oldPath.c_str() == NULL) { 1209a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 1210a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 1211a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars newPath(env, javaNewPath); 1212a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (newPath.c_str() == NULL) { 1213a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 1214a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 1215a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes throwIfMinusOne(env, "rename", TEMP_FAILURE_RETRY(rename(oldPath.c_str(), newPath.c_str()))); 1216a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes} 1217a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes 12188b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesstatic jlong Posix_sendfile(JNIEnv* env, jobject, jobject javaOutFd, jobject javaInFd, jobject javaOffset, jlong byteCount) { 12198b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes int outFd = jniGetFDFromFileDescriptor(env, javaOutFd); 12208b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes int inFd = jniGetFDFromFileDescriptor(env, javaInFd); 12218b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes static jfieldID valueFid = env->GetFieldID(JniConstants::mutableLongClass, "value", "J"); 12228b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes off_t offset = 0; 12238b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes off_t* offsetPtr = NULL; 12248b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes if (javaOffset != NULL) { 12258b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes // TODO: fix bionic so we can have a 64-bit off_t! 12268b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes offset = env->GetLongField(javaOffset, valueFid); 12278b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes offsetPtr = &offset; 12288b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes } 12298b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes jlong result = throwIfMinusOne(env, "sendfile", TEMP_FAILURE_RETRY(sendfile(outFd, inFd, offsetPtr, byteCount))); 12308b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes if (javaOffset != NULL) { 12318b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes env->SetLongField(javaOffset, valueFid, offset); 12328b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes } 12338b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes return result; 12348b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes} 12358b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes 123690d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughesstatic jint Posix_sendtoBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount, jint flags, jobject javaInetAddress, jint port) { 123790d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes ScopedBytesRO bytes(env, javaBytes); 123890d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes if (bytes.get() == NULL) { 123990d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes return -1; 124090d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes } 124190d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes sockaddr_storage ss; 1242482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes socklen_t sa_len = 0; 1243482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes if (javaInetAddress != NULL && !inetAddressToSockaddr(env, javaInetAddress, port, ss, sa_len)) { 124490d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes return -1; 124590d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes } 124690d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes const sockaddr* to = (javaInetAddress != NULL) ? reinterpret_cast<const sockaddr*>(&ss) : NULL; 1247482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes return NET_FAILURE_RETRY(env, ssize_t, sendto, javaFd, bytes.get() + byteOffset, byteCount, flags, to, sa_len); 124890d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes} 124990d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes 1250396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_setegid(JNIEnv* env, jobject, jint egid) { 1251396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes throwIfMinusOne(env, "setegid", TEMP_FAILURE_RETRY(setegid(egid))); 1252396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 1253396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 1254895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughesstatic void Posix_setenv(JNIEnv* env, jobject, jstring javaName, jstring javaValue, jboolean overwrite) { 1255895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes ScopedUtfChars name(env, javaName); 1256895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes if (name.c_str() == NULL) { 1257895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes return; 1258895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes } 1259895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes ScopedUtfChars value(env, javaValue); 1260895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes if (value.c_str() == NULL) { 1261895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes return; 1262895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes } 1263895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes throwIfMinusOne(env, "setenv", setenv(name.c_str(), value.c_str(), overwrite)); 1264895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes} 1265895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes 1266396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_seteuid(JNIEnv* env, jobject, jint euid) { 1267396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes throwIfMinusOne(env, "seteuid", TEMP_FAILURE_RETRY(seteuid(euid))); 1268396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 1269396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 1270396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_setgid(JNIEnv* env, jobject, jint gid) { 1271396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes throwIfMinusOne(env, "setgid", TEMP_FAILURE_RETRY(setgid(gid))); 1272396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 1273396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 12746c9b5377550a9649ed1532d1fcdfeba116c74eadElliott Hughesstatic jint Posix_setsid(JNIEnv* env, jobject) { 12756c9b5377550a9649ed1532d1fcdfeba116c74eadElliott Hughes return throwIfMinusOne(env, "setsid", TEMP_FAILURE_RETRY(setsid())); 12766c9b5377550a9649ed1532d1fcdfeba116c74eadElliott Hughes} 12776c9b5377550a9649ed1532d1fcdfeba116c74eadElliott Hughes 1278c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 1279c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1280c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes u_char byte = value; 1281c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &byte, sizeof(byte)))); 1282c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes} 1283c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes 1284a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughesstatic void Posix_setsockoptIfreq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jstring javaInterfaceName) { 1285a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes struct ifreq req; 1286a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes if (!fillIfreq(env, javaInterfaceName, req)) { 1287b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes return; 1288b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes } 1289b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1290b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req)))); 1291b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes} 1292b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes 1293454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic void Posix_setsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 1294454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1295454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 1296454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes} 1297454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes 12986c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED < 1070 12996c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes// Mac OS didn't support modern multicast APIs until 10.7. 13006c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughesstatic void Posix_setsockoptIpMreqn(JNIEnv*, jobject, jobject, jint, jint, jint) { abort(); } 13016c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughesstatic void Posix_setsockoptGroupReq(JNIEnv*, jobject, jobject, jint, jint, jobject) { abort(); } 1302df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fullerstatic void Posix_setsockoptGroupSourceReq(JNIEnv*, jobject, jobject, jint, jint, jobject) { abort(); } 13036c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes#else 1304b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughesstatic void Posix_setsockoptIpMreqn(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 1305b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes ip_mreqn req; 1306b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes memset(&req, 0, sizeof(req)); 1307b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes req.imr_ifindex = value; 1308b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1309b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req)))); 1310b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes} 1311b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes 1312438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughesstatic void Posix_setsockoptGroupReq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaGroupReq) { 13138ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes struct group_req req; 13148ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes memset(&req, 0, sizeof(req)); 1315438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes 1316438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes static jfieldID grInterfaceFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_interface", "I"); 13178ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes req.gr_interface = env->GetIntField(javaGroupReq, grInterfaceFid); 1318438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes // Get the IPv4 or IPv6 multicast address to join or leave. 1319438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes static jfieldID grGroupFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_group", "Ljava/net/InetAddress;"); 1320e9f12043a1e2a29c60779cdb55c58eb156963570Elliott Hughes ScopedLocalRef<jobject> javaGroup(env, env->GetObjectField(javaGroupReq, grGroupFid)); 1321482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes socklen_t sa_len; 1322482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes if (!inetAddressToSockaddrVerbatim(env, javaGroup.get(), 0, req.gr_group, sa_len)) { 1323438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes return; 1324438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes } 1325438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes 1326438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 13278ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes int rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req))); 1328438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes if (rc == -1 && errno == EINVAL) { 1329438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes // Maybe we're a 32-bit binary talking to a 64-bit kernel? 1330438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes // glibc doesn't automatically handle this. 1331df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller // http://sourceware.org/bugzilla/show_bug.cgi?id=12080 13329634b569a92841f32268907c88c408f22cb00486Elliott Hughes struct group_req64 { 1333438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes uint32_t gr_interface; 1334438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes uint32_t my_padding; 1335438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes sockaddr_storage gr_group; 1336438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes }; 13378ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes group_req64 req64; 13388ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes req64.gr_interface = req.gr_interface; 13398ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes memcpy(&req64.gr_group, &req.gr_group, sizeof(req.gr_group)); 13408ecbb3f6a89983adb1a085469befc70488f4f04fElliott Hughes rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req64, sizeof(req64))); 1341438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes } 1342438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes throwIfMinusOne(env, "setsockopt", rc); 1343438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes} 1344df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller 1345df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fullerstatic void Posix_setsockoptGroupSourceReq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaGroupSourceReq) { 1346df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller socklen_t sa_len; 1347df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller struct group_source_req req; 1348df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller memset(&req, 0, sizeof(req)); 1349df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller 1350df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller static jfieldID gsrInterfaceFid = env->GetFieldID(JniConstants::structGroupSourceReqClass, "gsr_interface", "I"); 1351df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller req.gsr_interface = env->GetIntField(javaGroupSourceReq, gsrInterfaceFid); 1352df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller // Get the IPv4 or IPv6 multicast address to join or leave. 1353df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller static jfieldID gsrGroupFid = env->GetFieldID(JniConstants::structGroupSourceReqClass, "gsr_group", "Ljava/net/InetAddress;"); 1354df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller ScopedLocalRef<jobject> javaGroup(env, env->GetObjectField(javaGroupSourceReq, gsrGroupFid)); 1355df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller if (!inetAddressToSockaddrVerbatim(env, javaGroup.get(), 0, req.gsr_group, sa_len)) { 1356df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller return; 1357df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller } 1358df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller 1359df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller // Get the IPv4 or IPv6 multicast address to add to the filter. 1360df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller static jfieldID gsrSourceFid = env->GetFieldID(JniConstants::structGroupSourceReqClass, "gsr_source", "Ljava/net/InetAddress;"); 1361df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller ScopedLocalRef<jobject> javaSource(env, env->GetObjectField(javaGroupSourceReq, gsrSourceFid)); 1362df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller if (!inetAddressToSockaddrVerbatim(env, javaSource.get(), 0, req.gsr_source, sa_len)) { 1363df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller return; 1364df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller } 1365df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller 1366df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller int fd = jniGetFDFromFileDescriptor(env, javaFd); 1367df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller int rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req))); 1368df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller if (rc == -1 && errno == EINVAL) { 1369df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller // Maybe we're a 32-bit binary talking to a 64-bit kernel? 1370df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller // glibc doesn't automatically handle this. 1371df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller // http://sourceware.org/bugzilla/show_bug.cgi?id=12080 1372df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller struct group_source_req64 { 1373df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller uint32_t gsr_interface; 1374df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller uint32_t my_padding; 1375df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller sockaddr_storage gsr_group; 1376df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller sockaddr_storage gsr_source; 1377df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller }; 1378df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller group_source_req64 req64; 1379df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller req64.gsr_interface = req.gsr_interface; 1380df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller memcpy(&req64.gsr_group, &req.gsr_group, sizeof(req.gsr_group)); 1381df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller memcpy(&req64.gsr_source, &req.gsr_source, sizeof(req.gsr_source)); 1382df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req64, sizeof(req64))); 1383df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller } 1384df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller throwIfMinusOne(env, "setsockopt", rc); 1385df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller} 13866c1e5f4ad36c1f51687aa2b059e998a7c2db2e36Elliott Hughes#endif 1387438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes 1388c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaLinger) { 1389c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID lOnoffFid = env->GetFieldID(JniConstants::structLingerClass, "l_onoff", "I"); 1390c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID lLingerFid = env->GetFieldID(JniConstants::structLingerClass, "l_linger", "I"); 1391c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1392c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes struct linger value; 1393c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes value.l_onoff = env->GetIntField(javaLinger, lOnoffFid); 1394c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes value.l_linger = env->GetIntField(javaLinger, lLingerFid); 1395c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 1396c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes} 1397c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes 1398c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughesstatic void Posix_setsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaTimeval) { 1399c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID tvSecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_sec", "J"); 1400c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes static jfieldID tvUsecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_usec", "J"); 1401c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1402c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes struct timeval value; 140339fd5e164f93ab821841aa4d1fac09055c102b8bElliott Hughes value.tv_sec = env->GetLongField(javaTimeval, tvSecFid); 140439fd5e164f93ab821841aa4d1fac09055c102b8bElliott Hughes value.tv_usec = env->GetLongField(javaTimeval, tvUsecFid); 1405c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 1406c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes} 1407c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes 1408396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughesstatic void Posix_setuid(JNIEnv* env, jobject, jint uid) { 1409396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes throwIfMinusOne(env, "setuid", TEMP_FAILURE_RETRY(setuid(uid))); 1410396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes} 1411396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes 141259e4744d27231f260271dbbca406e0cc39768116Elliott Hughesstatic void Posix_shutdown(JNIEnv* env, jobject, jobject javaFd, jint how) { 141359e4744d27231f260271dbbca406e0cc39768116Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 141459e4744d27231f260271dbbca406e0cc39768116Elliott Hughes throwIfMinusOne(env, "shutdown", TEMP_FAILURE_RETRY(shutdown(fd, how))); 141559e4744d27231f260271dbbca406e0cc39768116Elliott Hughes} 141659e4744d27231f260271dbbca406e0cc39768116Elliott Hughes 1417454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughesstatic jobject Posix_socket(JNIEnv* env, jobject, jint domain, jint type, jint protocol) { 1418454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes int fd = throwIfMinusOne(env, "socket", TEMP_FAILURE_RETRY(socket(domain, type, protocol))); 1419454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL; 1420454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes} 1421454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes 14223218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughesstatic void Posix_socketpair(JNIEnv* env, jobject, jint domain, jint type, jint protocol, jobject javaFd1, jobject javaFd2) { 14233218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughes int fds[2]; 14243218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughes int rc = throwIfMinusOne(env, "socketpair", TEMP_FAILURE_RETRY(socketpair(domain, type, protocol, fds))); 14253218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughes if (rc != -1) { 14263218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughes jniSetFileDescriptorOfFD(env, javaFd1, fds[0]); 14273218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughes jniSetFileDescriptorOfFD(env, javaFd2, fds[1]); 14283218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughes } 14293218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughes} 14303218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughes 143147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_stat(JNIEnv* env, jobject, jstring javaPath) { 143247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes return doStat(env, javaPath, false); 143347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes} 143447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes 1435721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughesstatic jobject Posix_statvfs(JNIEnv* env, jobject, jstring javaPath) { 143659fa7163774d6930a174bc038414a4b780581957Elliott Hughes ScopedUtfChars path(env, javaPath); 143759fa7163774d6930a174bc038414a4b780581957Elliott Hughes if (path.c_str() == NULL) { 143859fa7163774d6930a174bc038414a4b780581957Elliott Hughes return NULL; 143959fa7163774d6930a174bc038414a4b780581957Elliott Hughes } 1440721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes struct statvfs sb; 1441721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes int rc = TEMP_FAILURE_RETRY(statvfs(path.c_str(), &sb)); 144259fa7163774d6930a174bc038414a4b780581957Elliott Hughes if (rc == -1) { 1443721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes throwErrnoException(env, "statvfs"); 144459fa7163774d6930a174bc038414a4b780581957Elliott Hughes return NULL; 144559fa7163774d6930a174bc038414a4b780581957Elliott Hughes } 1446721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes return makeStructStatVfs(env, sb); 144759fa7163774d6930a174bc038414a4b780581957Elliott Hughes} 144859fa7163774d6930a174bc038414a4b780581957Elliott Hughes 1449ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jstring Posix_strerror(JNIEnv* env, jobject, jint errnum) { 145052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes char buffer[BUFSIZ]; 1451ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes const char* message = jniStrError(errnum, buffer, sizeof(buffer)); 1452ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes return env->NewStringUTF(message); 1453ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes} 1454ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes 1455763f8ed6195707d0c30bfae1ca8a3bb886b746ccElliott Hughesstatic jstring Posix_strsignal(JNIEnv* env, jobject, jint signal) { 1456763f8ed6195707d0c30bfae1ca8a3bb886b746ccElliott Hughes return env->NewStringUTF(strsignal(signal)); 1457763f8ed6195707d0c30bfae1ca8a3bb886b746ccElliott Hughes} 1458763f8ed6195707d0c30bfae1ca8a3bb886b746ccElliott Hughes 1459a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughesstatic void Posix_symlink(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) { 1460a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars oldPath(env, javaOldPath); 1461a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (oldPath.c_str() == NULL) { 1462a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 1463a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 1464a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes ScopedUtfChars newPath(env, javaNewPath); 1465a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes if (newPath.c_str() == NULL) { 1466a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes return; 1467a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 1468a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes throwIfMinusOne(env, "symlink", TEMP_FAILURE_RETRY(symlink(oldPath.c_str(), newPath.c_str()))); 1469a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes} 1470a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes 14716fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughesstatic jlong Posix_sysconf(JNIEnv* env, jobject, jint name) { 14726fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes // Since -1 is a valid result from sysconf(3), detecting failure is a little more awkward. 14736fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes errno = 0; 14746fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes long result = sysconf(name); 14756fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes if (result == -1L && errno == EINVAL) { 14767e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes throwErrnoException(env, "sysconf"); 14776fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes } 14786fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes return result; 14796fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes} 14806fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes 1481bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestigstatic void Posix_tcdrain(JNIEnv* env, jobject, jobject javaFd) { 1482bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig int fd = jniGetFDFromFileDescriptor(env, javaFd); 1483bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig throwIfMinusOne(env, "tcdrain", TEMP_FAILURE_RETRY(tcdrain(fd))); 1484bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig} 1485bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig 1486039f7599559fd7e48c354b99dcb94ff391f53349Elliott Hughesstatic void Posix_tcsendbreak(JNIEnv* env, jobject, jobject javaFd, jint duration) { 1487039f7599559fd7e48c354b99dcb94ff391f53349Elliott Hughes int fd = jniGetFDFromFileDescriptor(env, javaFd); 1488039f7599559fd7e48c354b99dcb94ff391f53349Elliott Hughes throwIfMinusOne(env, "tcsendbreak", TEMP_FAILURE_RETRY(tcsendbreak(fd, duration))); 1489039f7599559fd7e48c354b99dcb94ff391f53349Elliott Hughes} 1490039f7599559fd7e48c354b99dcb94ff391f53349Elliott Hughes 1491c79d3ef4dcd25f0e264bf957861e2632da93e62dNick Kralevichstatic jint Posix_umaskImpl(JNIEnv*, jobject, jint mask) { 149244f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes return umask(mask); 149344f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes} 149444f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes 14957341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughesstatic jobject Posix_uname(JNIEnv* env, jobject) { 14967341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes struct utsname buf; 14979b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes if (TEMP_FAILURE_RETRY(uname(&buf)) == -1) { 14989b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes return NULL; // Can't happen. 14997341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes } 15007341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes return makeStructUtsname(env, buf); 15017341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes} 15027341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes 1503895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughesstatic void Posix_unsetenv(JNIEnv* env, jobject, jstring javaName) { 1504895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes ScopedUtfChars name(env, javaName); 1505895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes if (name.c_str() == NULL) { 1506895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes return; 1507895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes } 1508895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes throwIfMinusOne(env, "unsetenv", unsetenv(name.c_str())); 1509895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes} 1510895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes 15119e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughesstatic jint Posix_waitpid(JNIEnv* env, jobject, jint pid, jobject javaStatus, jint options) { 15129e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes int status; 15139e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes int rc = throwIfMinusOne(env, "waitpid", TEMP_FAILURE_RETRY(waitpid(pid, &status, options))); 15149e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes if (rc != -1) { 15159e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes static jfieldID valueFid = env->GetFieldID(JniConstants::mutableIntClass, "value", "I"); 15169e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes env->SetIntField(javaStatus, valueFid, status); 15179e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes } 15189e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes return rc; 15199e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes} 15209e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes 15210568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughesstatic jint Posix_writeBytes(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount) { 15220568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes ScopedBytesRO bytes(env, javaBytes); 152378c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes if (bytes.get() == NULL) { 152478c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes return -1; 152578c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes } 1526f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller return IO_FAILURE_RETRY(env, ssize_t, write, javaFd, bytes.get() + byteOffset, byteCount); 152778c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes} 152878c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes 1529bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesstatic jint Posix_writev(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) { 15308dc754726bb5303c25e2c48decdf76d9323ee231Elliott Hughes IoVec<ScopedBytesRO> ioVec(env, env->GetArrayLength(buffers)); 1531bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes if (!ioVec.init(buffers, offsets, byteCounts)) { 1532bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes return -1; 1533bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes } 1534f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller return IO_FAILURE_RETRY(env, ssize_t, writev, javaFd, ioVec.get(), ioVec.size()); 1535bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes} 1536bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes 1537ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic JNINativeMethod gMethods[] = { 1538553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes NATIVE_METHOD(Posix, accept, "(Ljava/io/FileDescriptor;Ljava/net/InetSocketAddress;)Ljava/io/FileDescriptor;"), 1539ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes NATIVE_METHOD(Posix, access, "(Ljava/lang/String;I)Z"), 1540da15009528cc8300a6251f1d0931ac8657c9fc31Elliott Hughes NATIVE_METHOD(Posix, bind, "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)V"), 1541b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes NATIVE_METHOD(Posix, chmod, "(Ljava/lang/String;I)V"), 154244f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes NATIVE_METHOD(Posix, chown, "(Ljava/lang/String;II)V"), 1543462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes NATIVE_METHOD(Posix, close, "(Ljava/io/FileDescriptor;)V"), 1544996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes NATIVE_METHOD(Posix, connect, "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)V"), 1545396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, dup, "(Ljava/io/FileDescriptor;)Ljava/io/FileDescriptor;"), 1546396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, dup2, "(Ljava/io/FileDescriptor;I)Ljava/io/FileDescriptor;"), 1547ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes NATIVE_METHOD(Posix, environ, "()[Ljava/lang/String;"), 1548798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich NATIVE_METHOD(Posix, execv, "(Ljava/lang/String;[Ljava/lang/String;)V"), 1549798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich NATIVE_METHOD(Posix, execve, "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)V"), 155044f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes NATIVE_METHOD(Posix, fchmod, "(Ljava/io/FileDescriptor;I)V"), 155144f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes NATIVE_METHOD(Posix, fchown, "(Ljava/io/FileDescriptor;II)V"), 1552fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes NATIVE_METHOD(Posix, fcntlVoid, "(Ljava/io/FileDescriptor;I)I"), 1553fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes NATIVE_METHOD(Posix, fcntlLong, "(Ljava/io/FileDescriptor;IJ)I"), 15545d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, fcntlFlock, "(Ljava/io/FileDescriptor;ILandroid/system/StructFlock;)I"), 155552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes NATIVE_METHOD(Posix, fdatasync, "(Ljava/io/FileDescriptor;)V"), 15565d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, fstat, "(Ljava/io/FileDescriptor;)Landroid/system/StructStat;"), 15575d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, fstatvfs, "(Ljava/io/FileDescriptor;)Landroid/system/StructStatVfs;"), 155852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes NATIVE_METHOD(Posix, fsync, "(Ljava/io/FileDescriptor;)V"), 1559f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes NATIVE_METHOD(Posix, ftruncate, "(Ljava/io/FileDescriptor;J)V"), 15604f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes NATIVE_METHOD(Posix, gai_strerror, "(I)Ljava/lang/String;"), 15615d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, getaddrinfo, "(Ljava/lang/String;Landroid/system/StructAddrinfo;)[Ljava/net/InetAddress;"), 1562396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, getegid, "()I"), 1563396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, geteuid, "()I"), 1564396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, getgid, "()I"), 1565ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes NATIVE_METHOD(Posix, getenv, "(Ljava/lang/String;)Ljava/lang/String;"), 15664f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes NATIVE_METHOD(Posix, getnameinfo, "(Ljava/net/InetAddress;I)Ljava/lang/String;"), 1567482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes NATIVE_METHOD(Posix, getpeername, "(Ljava/io/FileDescriptor;)Ljava/net/SocketAddress;"), 1568396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, getpid, "()I"), 1569396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, getppid, "()I"), 15705d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, getpwnam, "(Ljava/lang/String;)Landroid/system/StructPasswd;"), 15715d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, getpwuid, "(I)Landroid/system/StructPasswd;"), 15720a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockname, "(Ljava/io/FileDescriptor;)Ljava/net/SocketAddress;"), 15730a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptByte, "(Ljava/io/FileDescriptor;II)I"), 15740a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptInAddr, "(Ljava/io/FileDescriptor;II)Ljava/net/InetAddress;"), 15750a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes NATIVE_METHOD(Posix, getsockoptInt, "(Ljava/io/FileDescriptor;II)I"), 15765d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, getsockoptLinger, "(Ljava/io/FileDescriptor;II)Landroid/system/StructLinger;"), 15775d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, getsockoptTimeval, "(Ljava/io/FileDescriptor;II)Landroid/system/StructTimeval;"), 15785d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, getsockoptUcred, "(Ljava/io/FileDescriptor;II)Landroid/system/StructUcred;"), 1579069525a39125c203b658c805685b6045a7d4dfebElliott Hughes NATIVE_METHOD(Posix, gettid, "()I"), 1580396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, getuid, "()I"), 1581a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes NATIVE_METHOD(Posix, if_indextoname, "(I)Ljava/lang/String;"), 1582fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes NATIVE_METHOD(Posix, inet_pton, "(ILjava/lang/String;)Ljava/net/InetAddress;"), 1583a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes NATIVE_METHOD(Posix, ioctlInetAddress, "(Ljava/io/FileDescriptor;ILjava/lang/String;)Ljava/net/InetAddress;"), 15845d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, ioctlInt, "(Ljava/io/FileDescriptor;ILandroid/util/MutableInt;)I"), 15859a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes NATIVE_METHOD(Posix, isatty, "(Ljava/io/FileDescriptor;)Z"), 1586a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes NATIVE_METHOD(Posix, kill, "(II)V"), 158744f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes NATIVE_METHOD(Posix, lchown, "(Ljava/lang/String;II)V"), 158804428d61d7000e17ab21d08a1d672c34eb68f6e2Elliott Hughes NATIVE_METHOD(Posix, link, "(Ljava/lang/String;Ljava/lang/String;)V"), 1589e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes NATIVE_METHOD(Posix, listen, "(Ljava/io/FileDescriptor;I)V"), 1590dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes NATIVE_METHOD(Posix, lseek, "(Ljava/io/FileDescriptor;JI)J"), 15915d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, lstat, "(Ljava/lang/String;)Landroid/system/StructStat;"), 15920f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes NATIVE_METHOD(Posix, mincore, "(JJ[B)V"), 1593c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes NATIVE_METHOD(Posix, mkdir, "(Ljava/lang/String;I)V"), 1594f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller NATIVE_METHOD(Posix, mkfifo, "(Ljava/lang/String;I)V"), 15957e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes NATIVE_METHOD(Posix, mlock, "(JJ)V"), 15967e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes NATIVE_METHOD(Posix, mmap, "(JJIILjava/io/FileDescriptor;J)J"), 15977e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes NATIVE_METHOD(Posix, msync, "(JJI)V"), 15987e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes NATIVE_METHOD(Posix, munlock, "(JJ)V"), 15997e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes NATIVE_METHOD(Posix, munmap, "(JJ)V"), 16000ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes NATIVE_METHOD(Posix, open, "(Ljava/lang/String;II)Ljava/io/FileDescriptor;"), 160141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes NATIVE_METHOD(Posix, pipe, "()[Ljava/io/FileDescriptor;"), 16025d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, poll, "([Landroid/system/StructPollfd;I)I"), 160311f07d30d2e344b48f132ec6ed105d85423052c2Elliott Hughes NATIVE_METHOD(Posix, posix_fallocate, "(Ljava/io/FileDescriptor;JJ)V"), 16045215e4c0db7530519981f1e505e6db82401802f2Nick Kralevich NATIVE_METHOD(Posix, prctl, "(IJJJJ)I"), 1605e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes NATIVE_METHOD(Posix, preadBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIJ)I"), 1606e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes NATIVE_METHOD(Posix, pwriteBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIJ)I"), 16070568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes NATIVE_METHOD(Posix, readBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"), 16080b6b3e10459fc3a3f4dd280dab8d4a145f7bf2f0Elliott Hughes NATIVE_METHOD(Posix, readlink, "(Ljava/lang/String;)Ljava/lang/String;"), 1609bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes NATIVE_METHOD(Posix, readv, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"), 161023ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes NATIVE_METHOD(Posix, recvfromBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIILjava/net/InetSocketAddress;)I"), 1611c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes NATIVE_METHOD(Posix, remove, "(Ljava/lang/String;)V"), 1612a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes NATIVE_METHOD(Posix, rename, "(Ljava/lang/String;Ljava/lang/String;)V"), 16135d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, sendfile, "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Landroid/util/MutableLong;J)J"), 161490d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes NATIVE_METHOD(Posix, sendtoBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIILjava/net/InetAddress;I)I"), 1615396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, setegid, "(I)V"), 1616895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes NATIVE_METHOD(Posix, setenv, "(Ljava/lang/String;Ljava/lang/String;Z)V"), 1617396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, seteuid, "(I)V"), 1618396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, setgid, "(I)V"), 16196c9b5377550a9649ed1532d1fcdfeba116c74eadElliott Hughes NATIVE_METHOD(Posix, setsid, "()I"), 1620c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes NATIVE_METHOD(Posix, setsockoptByte, "(Ljava/io/FileDescriptor;III)V"), 1621b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes NATIVE_METHOD(Posix, setsockoptIfreq, "(Ljava/io/FileDescriptor;IILjava/lang/String;)V"), 1622454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes NATIVE_METHOD(Posix, setsockoptInt, "(Ljava/io/FileDescriptor;III)V"), 1623b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes NATIVE_METHOD(Posix, setsockoptIpMreqn, "(Ljava/io/FileDescriptor;III)V"), 16245d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, setsockoptGroupReq, "(Ljava/io/FileDescriptor;IILandroid/system/StructGroupReq;)V"), 16255d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, setsockoptGroupSourceReq, "(Ljava/io/FileDescriptor;IILandroid/system/StructGroupSourceReq;)V"), 16265d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, setsockoptLinger, "(Ljava/io/FileDescriptor;IILandroid/system/StructLinger;)V"), 16275d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, setsockoptTimeval, "(Ljava/io/FileDescriptor;IILandroid/system/StructTimeval;)V"), 1628396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes NATIVE_METHOD(Posix, setuid, "(I)V"), 162959e4744d27231f260271dbbca406e0cc39768116Elliott Hughes NATIVE_METHOD(Posix, shutdown, "(Ljava/io/FileDescriptor;I)V"), 1630454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes NATIVE_METHOD(Posix, socket, "(III)Ljava/io/FileDescriptor;"), 16313218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughes NATIVE_METHOD(Posix, socketpair, "(IIILjava/io/FileDescriptor;Ljava/io/FileDescriptor;)V"), 16325d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, stat, "(Ljava/lang/String;)Landroid/system/StructStat;"), 16335d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, statvfs, "(Ljava/lang/String;)Landroid/system/StructStatVfs;"), 1634ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes NATIVE_METHOD(Posix, strerror, "(I)Ljava/lang/String;"), 1635763f8ed6195707d0c30bfae1ca8a3bb886b746ccElliott Hughes NATIVE_METHOD(Posix, strsignal, "(I)Ljava/lang/String;"), 1636a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes NATIVE_METHOD(Posix, symlink, "(Ljava/lang/String;Ljava/lang/String;)V"), 16376fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes NATIVE_METHOD(Posix, sysconf, "(I)J"), 1638bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig NATIVE_METHOD(Posix, tcdrain, "(Ljava/io/FileDescriptor;)V"), 1639039f7599559fd7e48c354b99dcb94ff391f53349Elliott Hughes NATIVE_METHOD(Posix, tcsendbreak, "(Ljava/io/FileDescriptor;I)V"), 1640c79d3ef4dcd25f0e264bf957861e2632da93e62dNick Kralevich NATIVE_METHOD(Posix, umaskImpl, "(I)I"), 16415d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, uname, "()Landroid/system/StructUtsname;"), 1642895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes NATIVE_METHOD(Posix, unsetenv, "(Ljava/lang/String;)V"), 16435d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughes NATIVE_METHOD(Posix, waitpid, "(ILandroid/util/MutableInt;I)I"), 16440568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes NATIVE_METHOD(Posix, writeBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"), 1645bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes NATIVE_METHOD(Posix, writev, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"), 1646ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes}; 16477cd6760f7045d771faae8080a8c6150bf678f679Elliott Hughesvoid register_libcore_io_Posix(JNIEnv* env) { 16487cd6760f7045d771faae8080a8c6150bf678f679Elliott Hughes jniRegisterNativeMethods(env, "libcore/io/Posix", gMethods, NELEM(gMethods)); 1649ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes} 1650