1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #define LOG_TAG "Posix" 18 19 #include "AsynchronousCloseMonitor.h" 20 #include "cutils/log.h" 21 #include "ExecStrings.h" 22 #include "JNIHelp.h" 23 #include "JniConstants.h" 24 #include "JniException.h" 25 #include "NetworkUtilities.h" 26 #include "Portability.h" 27 #include "readlink.h" 28 #include "ScopedBytes.h" 29 #include "ScopedLocalRef.h" 30 #include "ScopedPrimitiveArray.h" 31 #include "ScopedUtfChars.h" 32 #include "toStringArray.h" 33 34 #include <arpa/inet.h> 35 #include <errno.h> 36 #include <fcntl.h> 37 #include <linux/rtnetlink.h> 38 #include <net/if.h> 39 #include <netdb.h> 40 #include <netinet/in.h> 41 #include <netpacket/packet.h> 42 #include <poll.h> 43 #include <pwd.h> 44 #include <signal.h> 45 #include <stdlib.h> 46 #include <sys/ioctl.h> 47 #include <sys/mman.h> 48 #include <sys/prctl.h> 49 #include <sys/socket.h> 50 #include <sys/stat.h> 51 #include <sys/syscall.h> 52 #include <sys/time.h> 53 #include <sys/types.h> 54 #include <sys/uio.h> 55 #include <sys/un.h> 56 #include <sys/utsname.h> 57 #include <sys/wait.h> 58 #include <sys/xattr.h> 59 #include <termios.h> 60 #include <unistd.h> 61 #include <memory> 62 63 #ifndef __unused 64 #define __unused __attribute__((__unused__)) 65 #endif 66 67 #define TO_JAVA_STRING(NAME, EXP) \ 68 jstring NAME = env->NewStringUTF(EXP); \ 69 if (NAME == NULL) return NULL; 70 71 struct addrinfo_deleter { 72 void operator()(addrinfo* p) const { 73 if (p != NULL) { // bionic's freeaddrinfo(3) crashes when passed NULL. 74 freeaddrinfo(p); 75 } 76 } 77 }; 78 79 struct c_deleter { 80 void operator()(void* p) const { 81 free(p); 82 } 83 }; 84 85 static bool isIPv4MappedAddress(const sockaddr *sa) { 86 const sockaddr_in6 *sin6 = reinterpret_cast<const sockaddr_in6*>(sa); 87 return sa != NULL && sa->sa_family == AF_INET6 && 88 (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) || 89 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)); // We map 0.0.0.0 to ::, so :: is mapped. 90 } 91 92 /** 93 * Perform a socket operation that specifies an IP address, possibly falling back from specifying 94 * the address as an IPv4-mapped IPv6 address in a struct sockaddr_in6 to specifying it as an IPv4 95 * address in a struct sockaddr_in. 96 * 97 * This is needed because all sockets created by the java.net APIs are IPv6 sockets, and on those 98 * sockets, IPv4 operations use IPv4-mapped addresses stored in a struct sockaddr_in6. But sockets 99 * created using Posix.socket(AF_INET, ...) are IPv4 sockets and only support operations using IPv4 100 * socket addresses structures. 101 */ 102 #define NET_IPV4_FALLBACK(jni_env, return_type, syscall_name, java_fd, java_addr, port, null_addr_ok, args...) ({ \ 103 return_type _rc = -1; \ 104 do { \ 105 sockaddr_storage _ss; \ 106 socklen_t _salen; \ 107 if (java_addr == NULL && null_addr_ok) { \ 108 /* No IP address specified (e.g., sendto() on a connected socket). */ \ 109 _salen = 0; \ 110 } else if (!inetAddressToSockaddr(jni_env, java_addr, port, _ss, _salen)) { \ 111 /* Invalid socket address, return -1. inetAddressToSockaddr has already thrown. */ \ 112 break; \ 113 } \ 114 sockaddr* _sa = _salen ? reinterpret_cast<sockaddr*>(&_ss) : NULL; \ 115 /* inetAddressToSockaddr always returns an IPv6 sockaddr. Assume that java_fd was created \ 116 * by Java API calls, which always create IPv6 socket fds, and pass it in as is. */ \ 117 _rc = NET_FAILURE_RETRY(jni_env, return_type, syscall_name, java_fd, ##args, _sa, _salen); \ 118 if (_rc == -1 && errno == EAFNOSUPPORT && _salen && isIPv4MappedAddress(_sa)) { \ 119 /* We passed in an IPv4 address in an IPv6 sockaddr and the kernel told us that we got \ 120 * the address family wrong. Pass in the same address in an IPv4 sockaddr. */ \ 121 jni_env->ExceptionClear(); \ 122 if (!inetAddressToSockaddrVerbatim(jni_env, java_addr, port, _ss, _salen)) { \ 123 break; \ 124 } \ 125 _sa = reinterpret_cast<sockaddr*>(&_ss); \ 126 _rc = NET_FAILURE_RETRY(jni_env, return_type, syscall_name, java_fd, ##args, _sa, _salen); \ 127 } \ 128 } while (0); \ 129 _rc; }) \ 130 131 /** 132 * Used to retry networking system calls that can be interrupted with a signal. Unlike 133 * TEMP_FAILURE_RETRY, this also handles the case where 134 * AsynchronousCloseMonitor::signalBlockedThreads(fd) is used to signal a close() or 135 * Thread.interrupt(). Other signals that result in an EINTR result are ignored and the system call 136 * is retried. 137 * 138 * Returns the result of the system call though a Java exception will be pending if the result is 139 * -1: a SocketException if signaled via AsynchronousCloseMonitor, or ErrnoException for other 140 * failures. 141 */ 142 #define NET_FAILURE_RETRY(jni_env, return_type, syscall_name, java_fd, ...) ({ \ 143 return_type _rc = -1; \ 144 int _syscallErrno; \ 145 do { \ 146 bool _wasSignaled; \ 147 { \ 148 int _fd = jniGetFDFromFileDescriptor(jni_env, java_fd); \ 149 AsynchronousCloseMonitor _monitor(_fd); \ 150 _rc = syscall_name(_fd, __VA_ARGS__); \ 151 _syscallErrno = errno; \ 152 _wasSignaled = _monitor.wasSignaled(); \ 153 } \ 154 if (_wasSignaled) { \ 155 jniThrowException(jni_env, "java/net/SocketException", "Socket closed"); \ 156 _rc = -1; \ 157 break; \ 158 } \ 159 if (_rc == -1 && _syscallErrno != EINTR) { \ 160 /* TODO: with a format string we could show the arguments too, like strace(1). */ \ 161 throwErrnoException(jni_env, # syscall_name); \ 162 break; \ 163 } \ 164 } while (_rc == -1); /* _syscallErrno == EINTR && !_wasSignaled */ \ 165 if (_rc == -1) { \ 166 /* If the syscall failed, re-set errno: throwing an exception might have modified it. */ \ 167 errno = _syscallErrno; \ 168 } \ 169 _rc; }) 170 171 /** 172 * Used to retry system calls that can be interrupted with a signal. Unlike TEMP_FAILURE_RETRY, this 173 * also handles the case where AsynchronousCloseMonitor::signalBlockedThreads(fd) is used to signal 174 * a close() or Thread.interrupt(). Other signals that result in an EINTR result are ignored and the 175 * system call is retried. 176 * 177 * Returns the result of the system call though a Java exception will be pending if the result is 178 * -1: an IOException if the file descriptor is already closed, a InterruptedIOException if signaled 179 * via AsynchronousCloseMonitor, or ErrnoException for other failures. 180 */ 181 #define IO_FAILURE_RETRY(jni_env, return_type, syscall_name, java_fd, ...) ({ \ 182 return_type _rc = -1; \ 183 int _syscallErrno; \ 184 do { \ 185 bool _wasSignaled; \ 186 { \ 187 int _fd = jniGetFDFromFileDescriptor(jni_env, java_fd); \ 188 AsynchronousCloseMonitor _monitor(_fd); \ 189 _rc = syscall_name(_fd, __VA_ARGS__); \ 190 _syscallErrno = errno; \ 191 _wasSignaled = _monitor.wasSignaled(); \ 192 } \ 193 if (_wasSignaled) { \ 194 jniThrowException(jni_env, "java/io/InterruptedIOException", # syscall_name " interrupted"); \ 195 _rc = -1; \ 196 break; \ 197 } \ 198 if (_rc == -1 && _syscallErrno != EINTR) { \ 199 /* TODO: with a format string we could show the arguments too, like strace(1). */ \ 200 throwErrnoException(jni_env, # syscall_name); \ 201 break; \ 202 } \ 203 } while (_rc == -1); /* && _syscallErrno == EINTR && !_wasSignaled */ \ 204 if (_rc == -1) { \ 205 /* If the syscall failed, re-set errno: throwing an exception might have modified it. */ \ 206 errno = _syscallErrno; \ 207 } \ 208 _rc; }) 209 210 #define NULL_ADDR_OK true 211 #define NULL_ADDR_FORBIDDEN false 212 213 static void throwException(JNIEnv* env, jclass exceptionClass, jmethodID ctor3, jmethodID ctor2, 214 const char* functionName, int error) { 215 jthrowable cause = NULL; 216 if (env->ExceptionCheck()) { 217 cause = env->ExceptionOccurred(); 218 env->ExceptionClear(); 219 } 220 221 ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName)); 222 if (detailMessage.get() == NULL) { 223 // Not really much we can do here. We're probably dead in the water, 224 // but let's try to stumble on... 225 env->ExceptionClear(); 226 } 227 228 jobject exception; 229 if (cause != NULL) { 230 exception = env->NewObject(exceptionClass, ctor3, detailMessage.get(), error, cause); 231 } else { 232 exception = env->NewObject(exceptionClass, ctor2, detailMessage.get(), error); 233 } 234 env->Throw(reinterpret_cast<jthrowable>(exception)); 235 } 236 237 static void throwErrnoException(JNIEnv* env, const char* functionName) { 238 int error = errno; 239 static jmethodID ctor3 = env->GetMethodID(JniConstants::errnoExceptionClass, 240 "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V"); 241 static jmethodID ctor2 = env->GetMethodID(JniConstants::errnoExceptionClass, 242 "<init>", "(Ljava/lang/String;I)V"); 243 throwException(env, JniConstants::errnoExceptionClass, ctor3, ctor2, functionName, error); 244 } 245 246 static void throwGaiException(JNIEnv* env, const char* functionName, int error) { 247 // Cache the methods ids before we throw, so we don't call GetMethodID with a pending exception. 248 static jmethodID ctor3 = env->GetMethodID(JniConstants::gaiExceptionClass, "<init>", 249 "(Ljava/lang/String;ILjava/lang/Throwable;)V"); 250 static jmethodID ctor2 = env->GetMethodID(JniConstants::gaiExceptionClass, "<init>", 251 "(Ljava/lang/String;I)V"); 252 if (errno != 0) { 253 // EAI_SYSTEM should mean "look at errno instead", but both glibc and bionic seem to 254 // mess this up. In particular, if you don't have INTERNET permission, errno will be EACCES 255 // but you'll get EAI_NONAME or EAI_NODATA. So we want our GaiException to have a 256 // potentially-relevant ErrnoException as its cause even if error != EAI_SYSTEM. 257 // http://code.google.com/p/android/issues/detail?id=15722 258 throwErrnoException(env, functionName); 259 // Deliberately fall through to throw another exception... 260 } 261 throwException(env, JniConstants::gaiExceptionClass, ctor3, ctor2, functionName, error); 262 } 263 264 template <typename rc_t> 265 static rc_t throwIfMinusOne(JNIEnv* env, const char* name, rc_t rc) { 266 if (rc == rc_t(-1)) { 267 throwErrnoException(env, name); 268 } 269 return rc; 270 } 271 272 template <typename ScopedT> 273 class IoVec { 274 public: 275 IoVec(JNIEnv* env, size_t bufferCount) : mEnv(env), mBufferCount(bufferCount) { 276 } 277 278 bool init(jobjectArray javaBuffers, jintArray javaOffsets, jintArray javaByteCounts) { 279 // We can't delete our local references until after the I/O, so make sure we have room. 280 if (mEnv->PushLocalFrame(mBufferCount + 16) < 0) { 281 return false; 282 } 283 ScopedIntArrayRO offsets(mEnv, javaOffsets); 284 if (offsets.get() == NULL) { 285 return false; 286 } 287 ScopedIntArrayRO byteCounts(mEnv, javaByteCounts); 288 if (byteCounts.get() == NULL) { 289 return false; 290 } 291 // TODO: Linux actually has a 1024 buffer limit. glibc works around this, and we should too. 292 // TODO: you can query the limit at runtime with sysconf(_SC_IOV_MAX). 293 for (size_t i = 0; i < mBufferCount; ++i) { 294 jobject buffer = mEnv->GetObjectArrayElement(javaBuffers, i); // We keep this local ref. 295 mScopedBuffers.push_back(new ScopedT(mEnv, buffer)); 296 jbyte* ptr = const_cast<jbyte*>(mScopedBuffers.back()->get()); 297 if (ptr == NULL) { 298 return false; 299 } 300 struct iovec iov; 301 iov.iov_base = reinterpret_cast<void*>(ptr + offsets[i]); 302 iov.iov_len = byteCounts[i]; 303 mIoVec.push_back(iov); 304 } 305 return true; 306 } 307 308 ~IoVec() { 309 for (size_t i = 0; i < mScopedBuffers.size(); ++i) { 310 delete mScopedBuffers[i]; 311 } 312 mEnv->PopLocalFrame(NULL); 313 } 314 315 iovec* get() { 316 return &mIoVec[0]; 317 } 318 319 size_t size() { 320 return mBufferCount; 321 } 322 323 private: 324 JNIEnv* mEnv; 325 size_t mBufferCount; 326 std::vector<iovec> mIoVec; 327 std::vector<ScopedT*> mScopedBuffers; 328 }; 329 330 /** 331 * Returns a jbyteArray containing the sockaddr_un.sun_path from ss. As per unix(7) sa_len should be 332 * the length of ss as returned by getsockname(2), getpeername(2), or accept(2). 333 * If the returned array is of length 0 the sockaddr_un refers to an unnamed socket. 334 * A null pointer is returned in the event of an error. See unix(7) for more information. 335 */ 336 static jbyteArray getUnixSocketPath(JNIEnv* env, const sockaddr_storage& ss, 337 const socklen_t& sa_len) { 338 if (ss.ss_family != AF_UNIX) { 339 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", 340 "getUnixSocketPath unsupported ss_family: %i", ss.ss_family); 341 return NULL; 342 } 343 344 const struct sockaddr_un* un_addr = reinterpret_cast<const struct sockaddr_un*>(&ss); 345 // The length of sun_path is sa_len minus the length of the overhead (ss_family). 346 // See unix(7) for details. This calculation must match that of socket_make_sockaddr_un() in 347 // socket_local_client.c and javaUnixSocketAddressToSockaddr() to interoperate. 348 size_t pathLength = sa_len - offsetof(struct sockaddr_un, sun_path); 349 350 jbyteArray javaSunPath = env->NewByteArray(pathLength); 351 if (javaSunPath == NULL) { 352 return NULL; 353 } 354 355 if (pathLength > 0) { 356 env->SetByteArrayRegion(javaSunPath, 0, pathLength, 357 reinterpret_cast<const jbyte*>(&un_addr->sun_path)); 358 } 359 return javaSunPath; 360 } 361 362 static jobject makeSocketAddress(JNIEnv* env, const sockaddr_storage& ss, const socklen_t sa_len) { 363 if (ss.ss_family == AF_INET || ss.ss_family == AF_INET6) { 364 jint port; 365 jobject inetAddress = sockaddrToInetAddress(env, ss, &port); 366 if (inetAddress == NULL) { 367 return NULL; // Exception already thrown. 368 } 369 static jmethodID ctor = env->GetMethodID(JniConstants::inetSocketAddressClass, 370 "<init>", "(Ljava/net/InetAddress;I)V"); 371 return env->NewObject(JniConstants::inetSocketAddressClass, ctor, inetAddress, port); 372 } else if (ss.ss_family == AF_UNIX) { 373 static jmethodID ctor = env->GetMethodID(JniConstants::unixSocketAddressClass, 374 "<init>", "([B)V"); 375 376 jbyteArray javaSunPath = getUnixSocketPath(env, ss, sa_len); 377 if (!javaSunPath) { 378 return NULL; 379 } 380 return env->NewObject(JniConstants::unixSocketAddressClass, ctor, javaSunPath); 381 } else if (ss.ss_family == AF_NETLINK) { 382 const struct sockaddr_nl* nl_addr = reinterpret_cast<const struct sockaddr_nl*>(&ss); 383 static jmethodID ctor = env->GetMethodID(JniConstants::netlinkSocketAddressClass, 384 "<init>", "(II)V"); 385 return env->NewObject(JniConstants::netlinkSocketAddressClass, ctor, 386 static_cast<jint>(nl_addr->nl_pid), 387 static_cast<jint>(nl_addr->nl_groups)); 388 } else if (ss.ss_family == AF_PACKET) { 389 const struct sockaddr_ll* sll = reinterpret_cast<const struct sockaddr_ll*>(&ss); 390 static jmethodID ctor = env->GetMethodID(JniConstants::packetSocketAddressClass, 391 "<init>", "(SISB[B)V"); 392 ScopedLocalRef<jbyteArray> byteArray(env, env->NewByteArray(sll->sll_halen)); 393 if (byteArray.get() == NULL) { 394 return NULL; 395 } 396 env->SetByteArrayRegion(byteArray.get(), 0, sll->sll_halen, 397 reinterpret_cast<const jbyte*>(sll->sll_addr)); 398 jobject packetSocketAddress = env->NewObject(JniConstants::packetSocketAddressClass, ctor, 399 static_cast<jshort>(ntohs(sll->sll_protocol)), 400 static_cast<jint>(sll->sll_ifindex), 401 static_cast<jshort>(sll->sll_hatype), 402 static_cast<jbyte>(sll->sll_pkttype), 403 byteArray.get()); 404 return packetSocketAddress; 405 } 406 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", "unsupported ss_family: %d", 407 ss.ss_family); 408 return NULL; 409 } 410 411 static jobject makeStructPasswd(JNIEnv* env, const struct passwd& pw) { 412 TO_JAVA_STRING(pw_name, pw.pw_name); 413 TO_JAVA_STRING(pw_dir, pw.pw_dir); 414 TO_JAVA_STRING(pw_shell, pw.pw_shell); 415 static jmethodID ctor = env->GetMethodID(JniConstants::structPasswdClass, "<init>", 416 "(Ljava/lang/String;IILjava/lang/String;Ljava/lang/String;)V"); 417 return env->NewObject(JniConstants::structPasswdClass, ctor, 418 pw_name, static_cast<jint>(pw.pw_uid), static_cast<jint>(pw.pw_gid), pw_dir, pw_shell); 419 } 420 421 static jobject makeStructStat(JNIEnv* env, const struct stat& sb) { 422 static jmethodID ctor = env->GetMethodID(JniConstants::structStatClass, "<init>", 423 "(JJIJIIJJJJJJJ)V"); 424 return env->NewObject(JniConstants::structStatClass, ctor, 425 static_cast<jlong>(sb.st_dev), static_cast<jlong>(sb.st_ino), 426 static_cast<jint>(sb.st_mode), static_cast<jlong>(sb.st_nlink), 427 static_cast<jint>(sb.st_uid), static_cast<jint>(sb.st_gid), 428 static_cast<jlong>(sb.st_rdev), static_cast<jlong>(sb.st_size), 429 static_cast<jlong>(sb.st_atime), static_cast<jlong>(sb.st_mtime), 430 static_cast<jlong>(sb.st_ctime), static_cast<jlong>(sb.st_blksize), 431 static_cast<jlong>(sb.st_blocks)); 432 } 433 434 static jobject makeStructStatVfs(JNIEnv* env, const struct statvfs& sb) { 435 static jmethodID ctor = env->GetMethodID(JniConstants::structStatVfsClass, "<init>", 436 "(JJJJJJJJJJJ)V"); 437 return env->NewObject(JniConstants::structStatVfsClass, ctor, 438 static_cast<jlong>(sb.f_bsize), 439 static_cast<jlong>(sb.f_frsize), 440 static_cast<jlong>(sb.f_blocks), 441 static_cast<jlong>(sb.f_bfree), 442 static_cast<jlong>(sb.f_bavail), 443 static_cast<jlong>(sb.f_files), 444 static_cast<jlong>(sb.f_ffree), 445 static_cast<jlong>(sb.f_favail), 446 static_cast<jlong>(sb.f_fsid), 447 static_cast<jlong>(sb.f_flag), 448 static_cast<jlong>(sb.f_namemax)); 449 } 450 451 static jobject makeStructLinger(JNIEnv* env, const struct linger& l) { 452 static jmethodID ctor = env->GetMethodID(JniConstants::structLingerClass, "<init>", "(II)V"); 453 return env->NewObject(JniConstants::structLingerClass, ctor, l.l_onoff, l.l_linger); 454 } 455 456 static jobject makeStructTimeval(JNIEnv* env, const struct timeval& tv) { 457 static jmethodID ctor = env->GetMethodID(JniConstants::structTimevalClass, "<init>", "(JJ)V"); 458 return env->NewObject(JniConstants::structTimevalClass, ctor, 459 static_cast<jlong>(tv.tv_sec), static_cast<jlong>(tv.tv_usec)); 460 } 461 462 static jobject makeStructUcred(JNIEnv* env, const struct ucred& u __unused) { 463 static jmethodID ctor = env->GetMethodID(JniConstants::structUcredClass, "<init>", "(III)V"); 464 return env->NewObject(JniConstants::structUcredClass, ctor, u.pid, u.uid, u.gid); 465 } 466 467 static jobject makeStructUtsname(JNIEnv* env, const struct utsname& buf) { 468 TO_JAVA_STRING(sysname, buf.sysname); 469 TO_JAVA_STRING(nodename, buf.nodename); 470 TO_JAVA_STRING(release, buf.release); 471 TO_JAVA_STRING(version, buf.version); 472 TO_JAVA_STRING(machine, buf.machine); 473 static jmethodID ctor = env->GetMethodID(JniConstants::structUtsnameClass, "<init>", 474 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); 475 return env->NewObject(JniConstants::structUtsnameClass, ctor, 476 sysname, nodename, release, version, machine); 477 }; 478 479 static bool fillIfreq(JNIEnv* env, jstring javaInterfaceName, struct ifreq& req) { 480 ScopedUtfChars interfaceName(env, javaInterfaceName); 481 if (interfaceName.c_str() == NULL) { 482 return false; 483 } 484 memset(&req, 0, sizeof(req)); 485 strncpy(req.ifr_name, interfaceName.c_str(), sizeof(req.ifr_name)); 486 req.ifr_name[sizeof(req.ifr_name) - 1] = '\0'; 487 return true; 488 } 489 490 static bool fillUnixSocketAddress(JNIEnv* env, jobject javaUnixSocketAddress, 491 const sockaddr_storage& ss, const socklen_t& sa_len) { 492 if (javaUnixSocketAddress == NULL) { 493 return true; 494 } 495 jbyteArray javaSunPath = getUnixSocketPath(env, ss, sa_len); 496 if (!javaSunPath) { 497 return false; 498 } 499 500 static jfieldID sunPathFid = 501 env->GetFieldID(JniConstants::unixSocketAddressClass, "sun_path", "[B"); 502 env->SetObjectField(javaUnixSocketAddress, sunPathFid, javaSunPath); 503 return true; 504 } 505 506 static bool fillInetSocketAddress(JNIEnv* env, jobject javaInetSocketAddress, 507 const sockaddr_storage& ss) { 508 if (javaInetSocketAddress == NULL) { 509 return true; 510 } 511 // Fill out the passed-in InetSocketAddress with the sender's IP address and port number. 512 jint port; 513 jobject sender = sockaddrToInetAddress(env, ss, &port); 514 if (sender == NULL) { 515 return false; 516 } 517 static jfieldID holderFid = env->GetFieldID(JniConstants::inetSocketAddressClass, "holder", 518 "Ljava/net/InetSocketAddress$InetSocketAddressHolder;"); 519 jobject holder = env->GetObjectField(javaInetSocketAddress, holderFid); 520 521 static jfieldID addressFid = env->GetFieldID(JniConstants::inetSocketAddressHolderClass, 522 "addr", "Ljava/net/InetAddress;"); 523 static jfieldID portFid = env->GetFieldID(JniConstants::inetSocketAddressHolderClass, "port", "I"); 524 env->SetObjectField(holder, addressFid, sender); 525 env->SetIntField(holder, portFid, port); 526 return true; 527 } 528 529 static bool fillSocketAddress(JNIEnv* env, jobject javaSocketAddress, const sockaddr_storage& ss, 530 const socklen_t& sa_len) { 531 if (javaSocketAddress == NULL) { 532 return true; 533 } 534 535 if (env->IsInstanceOf(javaSocketAddress, JniConstants::inetSocketAddressClass)) { 536 return fillInetSocketAddress(env, javaSocketAddress, ss); 537 } else if (env->IsInstanceOf(javaSocketAddress, JniConstants::unixSocketAddressClass)) { 538 return fillUnixSocketAddress(env, javaSocketAddress, ss, sa_len); 539 } 540 jniThrowException(env, "java/lang/UnsupportedOperationException", 541 "unsupported SocketAddress subclass"); 542 return false; 543 544 } 545 546 static void javaInetSocketAddressToInetAddressAndPort( 547 JNIEnv* env, jobject javaInetSocketAddress, jobject& javaInetAddress, jint& port) { 548 static jfieldID holderFid = env->GetFieldID(JniConstants::inetSocketAddressClass, "holder", 549 "Ljava/net/InetSocketAddress$InetSocketAddressHolder;"); 550 jobject holder = env->GetObjectField(javaInetSocketAddress, holderFid); 551 552 static jfieldID addressFid = env->GetFieldID( 553 JniConstants::inetSocketAddressHolderClass, "addr", "Ljava/net/InetAddress;"); 554 static jfieldID portFid = env->GetFieldID(JniConstants::inetSocketAddressHolderClass, "port", "I"); 555 556 javaInetAddress = env->GetObjectField(holder, addressFid); 557 port = env->GetIntField(holder, portFid); 558 } 559 560 static bool javaInetSocketAddressToSockaddr( 561 JNIEnv* env, jobject javaSocketAddress, sockaddr_storage& ss, socklen_t& sa_len) { 562 jobject javaInetAddress; 563 jint port; 564 javaInetSocketAddressToInetAddressAndPort(env, javaSocketAddress, javaInetAddress, port); 565 return inetAddressToSockaddr(env, javaInetAddress, port, ss, sa_len); 566 } 567 568 static bool javaNetlinkSocketAddressToSockaddr( 569 JNIEnv* env, jobject javaSocketAddress, sockaddr_storage& ss, socklen_t& sa_len) { 570 static jfieldID nlPidFid = env->GetFieldID( 571 JniConstants::netlinkSocketAddressClass, "nlPortId", "I"); 572 static jfieldID nlGroupsFid = env->GetFieldID( 573 JniConstants::netlinkSocketAddressClass, "nlGroupsMask", "I"); 574 575 sockaddr_nl *nlAddr = reinterpret_cast<sockaddr_nl *>(&ss); 576 nlAddr->nl_family = AF_NETLINK; 577 nlAddr->nl_pid = env->GetIntField(javaSocketAddress, nlPidFid); 578 nlAddr->nl_groups = env->GetIntField(javaSocketAddress, nlGroupsFid); 579 sa_len = sizeof(sockaddr_nl); 580 return true; 581 } 582 583 static bool javaUnixSocketAddressToSockaddr( 584 JNIEnv* env, jobject javaUnixSocketAddress, sockaddr_storage& ss, socklen_t& sa_len) { 585 static jfieldID sunPathFid = env->GetFieldID( 586 JniConstants::unixSocketAddressClass, "sun_path", "[B"); 587 588 struct sockaddr_un* un_addr = reinterpret_cast<struct sockaddr_un*>(&ss); 589 memset (un_addr, 0, sizeof(sockaddr_un)); 590 un_addr->sun_family = AF_UNIX; 591 592 jbyteArray javaSunPath = (jbyteArray) env->GetObjectField(javaUnixSocketAddress, sunPathFid); 593 jsize pathLength = env->GetArrayLength(javaSunPath); 594 if ((size_t) pathLength > sizeof(sockaddr_un::sun_path)) { 595 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", 596 "sun_path too long: max=%i, is=%i", 597 sizeof(sockaddr_un::sun_path), pathLength); 598 return false; 599 } 600 env->GetByteArrayRegion(javaSunPath, 0, pathLength, (jbyte*) un_addr->sun_path); 601 // sa_len is sun_path plus the length of the overhead (ss_family_t). See unix(7) for 602 // details. This calculation must match that of socket_make_sockaddr_un() in 603 // socket_local_client.c and getUnixSocketPath() to interoperate. 604 sa_len = offsetof(struct sockaddr_un, sun_path) + pathLength; 605 return true; 606 } 607 608 static bool javaPacketSocketAddressToSockaddr( 609 JNIEnv* env, jobject javaSocketAddress, sockaddr_storage& ss, socklen_t& sa_len) { 610 static jfieldID protocolFid = env->GetFieldID( 611 JniConstants::packetSocketAddressClass, "sll_protocol", "S"); 612 static jfieldID ifindexFid = env->GetFieldID( 613 JniConstants::packetSocketAddressClass, "sll_ifindex", "I"); 614 static jfieldID hatypeFid = env->GetFieldID( 615 JniConstants::packetSocketAddressClass, "sll_hatype", "S"); 616 static jfieldID pkttypeFid = env->GetFieldID( 617 JniConstants::packetSocketAddressClass, "sll_pkttype", "B"); 618 static jfieldID addrFid = env->GetFieldID( 619 JniConstants::packetSocketAddressClass, "sll_addr", "[B"); 620 621 sockaddr_ll *sll = reinterpret_cast<sockaddr_ll *>(&ss); 622 sll->sll_family = AF_PACKET; 623 sll->sll_protocol = htons(env->GetShortField(javaSocketAddress, protocolFid)); 624 sll->sll_ifindex = env->GetIntField(javaSocketAddress, ifindexFid); 625 sll->sll_hatype = env->GetShortField(javaSocketAddress, hatypeFid); 626 sll->sll_pkttype = env->GetByteField(javaSocketAddress, pkttypeFid); 627 628 jbyteArray sllAddr = (jbyteArray) env->GetObjectField(javaSocketAddress, addrFid); 629 if (sllAddr == NULL) { 630 sll->sll_halen = 0; 631 memset(&sll->sll_addr, 0, sizeof(sll->sll_addr)); 632 } else { 633 jsize len = env->GetArrayLength(sllAddr); 634 if ((size_t) len > sizeof(sll->sll_addr)) { 635 len = sizeof(sll->sll_addr); 636 } 637 sll->sll_halen = len; 638 env->GetByteArrayRegion(sllAddr, 0, len, (jbyte*) sll->sll_addr); 639 } 640 sa_len = sizeof(sockaddr_ll); 641 return true; 642 } 643 644 static bool javaSocketAddressToSockaddr( 645 JNIEnv* env, jobject javaSocketAddress, sockaddr_storage& ss, socklen_t& sa_len) { 646 if (javaSocketAddress == NULL) { 647 jniThrowNullPointerException(env, NULL); 648 return false; 649 } 650 651 if (env->IsInstanceOf(javaSocketAddress, JniConstants::netlinkSocketAddressClass)) { 652 return javaNetlinkSocketAddressToSockaddr(env, javaSocketAddress, ss, sa_len); 653 } else if (env->IsInstanceOf(javaSocketAddress, JniConstants::inetSocketAddressClass)) { 654 return javaInetSocketAddressToSockaddr(env, javaSocketAddress, ss, sa_len); 655 } else if (env->IsInstanceOf(javaSocketAddress, JniConstants::packetSocketAddressClass)) { 656 return javaPacketSocketAddressToSockaddr(env, javaSocketAddress, ss, sa_len); 657 } else if (env->IsInstanceOf(javaSocketAddress, JniConstants::unixSocketAddressClass)) { 658 return javaUnixSocketAddressToSockaddr(env, javaSocketAddress, ss, sa_len); 659 } 660 jniThrowException(env, "java/lang/UnsupportedOperationException", 661 "unsupported SocketAddress subclass"); 662 return false; 663 } 664 665 static jobject doStat(JNIEnv* env, jstring javaPath, bool isLstat) { 666 ScopedUtfChars path(env, javaPath); 667 if (path.c_str() == NULL) { 668 return NULL; 669 } 670 struct stat sb; 671 int rc = isLstat ? TEMP_FAILURE_RETRY(lstat(path.c_str(), &sb)) 672 : TEMP_FAILURE_RETRY(stat(path.c_str(), &sb)); 673 if (rc == -1) { 674 throwErrnoException(env, isLstat ? "lstat" : "stat"); 675 return NULL; 676 } 677 return makeStructStat(env, sb); 678 } 679 680 static jobject doGetSockName(JNIEnv* env, jobject javaFd, bool is_sockname) { 681 int fd = jniGetFDFromFileDescriptor(env, javaFd); 682 sockaddr_storage ss; 683 sockaddr* sa = reinterpret_cast<sockaddr*>(&ss); 684 socklen_t byteCount = sizeof(ss); 685 memset(&ss, 0, byteCount); 686 int rc = is_sockname ? TEMP_FAILURE_RETRY(getsockname(fd, sa, &byteCount)) 687 : TEMP_FAILURE_RETRY(getpeername(fd, sa, &byteCount)); 688 if (rc == -1) { 689 throwErrnoException(env, is_sockname ? "getsockname" : "getpeername"); 690 return NULL; 691 } 692 return makeSocketAddress(env, ss, byteCount); 693 } 694 695 class Passwd { 696 public: 697 Passwd(JNIEnv* env) : mEnv(env), mResult(NULL) { 698 mBufferSize = sysconf(_SC_GETPW_R_SIZE_MAX); 699 mBuffer.reset(new char[mBufferSize]); 700 } 701 702 jobject getpwnam(const char* name) { 703 return process("getpwnam_r", getpwnam_r(name, &mPwd, mBuffer.get(), mBufferSize, &mResult)); 704 } 705 706 jobject getpwuid(uid_t uid) { 707 return process("getpwuid_r", getpwuid_r(uid, &mPwd, mBuffer.get(), mBufferSize, &mResult)); 708 } 709 710 struct passwd* get() { 711 return mResult; 712 } 713 714 private: 715 jobject process(const char* syscall, int error) { 716 if (mResult == NULL) { 717 errno = error; 718 throwErrnoException(mEnv, syscall); 719 return NULL; 720 } 721 return makeStructPasswd(mEnv, *mResult); 722 } 723 724 JNIEnv* mEnv; 725 std::unique_ptr<char[]> mBuffer; 726 size_t mBufferSize; 727 struct passwd mPwd; 728 struct passwd* mResult; 729 }; 730 731 static jobject Posix_accept(JNIEnv* env, jobject, jobject javaFd, jobject javaSocketAddress) { 732 sockaddr_storage ss; 733 socklen_t sl = sizeof(ss); 734 memset(&ss, 0, sizeof(ss)); 735 sockaddr* peer = (javaSocketAddress != NULL) ? reinterpret_cast<sockaddr*>(&ss) : NULL; 736 socklen_t* peerLength = (javaSocketAddress != NULL) ? &sl : 0; 737 jint clientFd = NET_FAILURE_RETRY(env, int, accept, javaFd, peer, peerLength); 738 if (clientFd == -1 || !fillSocketAddress(env, javaSocketAddress, ss, *peerLength)) { 739 close(clientFd); 740 return NULL; 741 } 742 return (clientFd != -1) ? jniCreateFileDescriptor(env, clientFd) : NULL; 743 } 744 745 static jboolean Posix_access(JNIEnv* env, jobject, jstring javaPath, jint mode) { 746 ScopedUtfChars path(env, javaPath); 747 if (path.c_str() == NULL) { 748 return JNI_FALSE; 749 } 750 int rc = TEMP_FAILURE_RETRY(access(path.c_str(), mode)); 751 if (rc == -1) { 752 throwErrnoException(env, "access"); 753 } 754 return (rc == 0); 755 } 756 757 static void Posix_bind(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) { 758 // We don't need the return value because we'll already have thrown. 759 (void) NET_IPV4_FALLBACK(env, int, bind, javaFd, javaAddress, port, NULL_ADDR_FORBIDDEN); 760 } 761 762 static void Posix_bindSocketAddress( 763 JNIEnv* env, jobject, jobject javaFd, jobject javaSocketAddress) { 764 sockaddr_storage ss; 765 socklen_t sa_len; 766 if (!javaSocketAddressToSockaddr(env, javaSocketAddress, ss, sa_len)) { 767 return; // Exception already thrown. 768 } 769 770 const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss); 771 // We don't need the return value because we'll already have thrown. 772 (void) NET_FAILURE_RETRY(env, int, bind, javaFd, sa, sa_len); 773 } 774 775 static void Posix_chmod(JNIEnv* env, jobject, jstring javaPath, jint mode) { 776 ScopedUtfChars path(env, javaPath); 777 if (path.c_str() == NULL) { 778 return; 779 } 780 throwIfMinusOne(env, "chmod", TEMP_FAILURE_RETRY(chmod(path.c_str(), mode))); 781 } 782 783 static void Posix_chown(JNIEnv* env, jobject, jstring javaPath, jint uid, jint gid) { 784 ScopedUtfChars path(env, javaPath); 785 if (path.c_str() == NULL) { 786 return; 787 } 788 throwIfMinusOne(env, "chown", TEMP_FAILURE_RETRY(chown(path.c_str(), uid, gid))); 789 } 790 791 static void Posix_close(JNIEnv* env, jobject, jobject javaFd) { 792 // Get the FileDescriptor's 'fd' field and clear it. 793 // We need to do this before we can throw an IOException (http://b/3222087). 794 int fd = jniGetFDFromFileDescriptor(env, javaFd); 795 jniSetFileDescriptorOfFD(env, javaFd, -1); 796 797 // Even if close(2) fails with EINTR, the fd will have been closed. 798 // Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone else's fd. 799 // http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html 800 throwIfMinusOne(env, "close", close(fd)); 801 } 802 803 static void Posix_connect(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) { 804 (void) NET_IPV4_FALLBACK(env, int, connect, javaFd, javaAddress, port, NULL_ADDR_FORBIDDEN); 805 } 806 807 static void Posix_connectSocketAddress( 808 JNIEnv* env, jobject, jobject javaFd, jobject javaSocketAddress) { 809 sockaddr_storage ss; 810 socklen_t sa_len; 811 if (!javaSocketAddressToSockaddr(env, javaSocketAddress, ss, sa_len)) { 812 return; // Exception already thrown. 813 } 814 815 const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss); 816 // We don't need the return value because we'll already have thrown. 817 (void) NET_FAILURE_RETRY(env, int, connect, javaFd, sa, sa_len); 818 } 819 820 static jobject Posix_dup(JNIEnv* env, jobject, jobject javaOldFd) { 821 int oldFd = jniGetFDFromFileDescriptor(env, javaOldFd); 822 int newFd = throwIfMinusOne(env, "dup", TEMP_FAILURE_RETRY(dup(oldFd))); 823 return (newFd != -1) ? jniCreateFileDescriptor(env, newFd) : NULL; 824 } 825 826 static jobject Posix_dup2(JNIEnv* env, jobject, jobject javaOldFd, jint newFd) { 827 int oldFd = jniGetFDFromFileDescriptor(env, javaOldFd); 828 int fd = throwIfMinusOne(env, "dup2", TEMP_FAILURE_RETRY(dup2(oldFd, newFd))); 829 return (fd != -1) ? jniCreateFileDescriptor(env, fd) : NULL; 830 } 831 832 static jobjectArray Posix_environ(JNIEnv* env, jobject) { 833 extern char** environ; // Standard, but not in any header file. 834 return toStringArray(env, environ); 835 } 836 837 static void Posix_execve(JNIEnv* env, jobject, jstring javaFilename, jobjectArray javaArgv, jobjectArray javaEnvp) { 838 ScopedUtfChars path(env, javaFilename); 839 if (path.c_str() == NULL) { 840 return; 841 } 842 843 ExecStrings argv(env, javaArgv); 844 ExecStrings envp(env, javaEnvp); 845 TEMP_FAILURE_RETRY(execve(path.c_str(), argv.get(), envp.get())); 846 847 throwErrnoException(env, "execve"); 848 } 849 850 static void Posix_execv(JNIEnv* env, jobject, jstring javaFilename, jobjectArray javaArgv) { 851 ScopedUtfChars path(env, javaFilename); 852 if (path.c_str() == NULL) { 853 return; 854 } 855 856 ExecStrings argv(env, javaArgv); 857 TEMP_FAILURE_RETRY(execv(path.c_str(), argv.get())); 858 859 throwErrnoException(env, "execv"); 860 } 861 862 static void Posix_fchmod(JNIEnv* env, jobject, jobject javaFd, jint mode) { 863 int fd = jniGetFDFromFileDescriptor(env, javaFd); 864 throwIfMinusOne(env, "fchmod", TEMP_FAILURE_RETRY(fchmod(fd, mode))); 865 } 866 867 static void Posix_fchown(JNIEnv* env, jobject, jobject javaFd, jint uid, jint gid) { 868 int fd = jniGetFDFromFileDescriptor(env, javaFd); 869 throwIfMinusOne(env, "fchown", TEMP_FAILURE_RETRY(fchown(fd, uid, gid))); 870 } 871 872 static jint Posix_fcntlFlock(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaFlock) { 873 static jfieldID typeFid = env->GetFieldID(JniConstants::structFlockClass, "l_type", "S"); 874 static jfieldID whenceFid = env->GetFieldID(JniConstants::structFlockClass, "l_whence", "S"); 875 static jfieldID startFid = env->GetFieldID(JniConstants::structFlockClass, "l_start", "J"); 876 static jfieldID lenFid = env->GetFieldID(JniConstants::structFlockClass, "l_len", "J"); 877 static jfieldID pidFid = env->GetFieldID(JniConstants::structFlockClass, "l_pid", "I"); 878 879 struct flock64 lock; 880 memset(&lock, 0, sizeof(lock)); 881 lock.l_type = env->GetShortField(javaFlock, typeFid); 882 lock.l_whence = env->GetShortField(javaFlock, whenceFid); 883 lock.l_start = env->GetLongField(javaFlock, startFid); 884 lock.l_len = env->GetLongField(javaFlock, lenFid); 885 lock.l_pid = env->GetIntField(javaFlock, pidFid); 886 887 int rc = IO_FAILURE_RETRY(env, int, fcntl, javaFd, cmd, &lock); 888 if (rc != -1) { 889 env->SetShortField(javaFlock, typeFid, lock.l_type); 890 env->SetShortField(javaFlock, whenceFid, lock.l_whence); 891 env->SetLongField(javaFlock, startFid, lock.l_start); 892 env->SetLongField(javaFlock, lenFid, lock.l_len); 893 env->SetIntField(javaFlock, pidFid, lock.l_pid); 894 } 895 return rc; 896 } 897 898 static jint Posix_fcntlInt(JNIEnv* env, jobject, jobject javaFd, jint cmd, jint arg) { 899 int fd = jniGetFDFromFileDescriptor(env, javaFd); 900 return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, arg))); 901 } 902 903 static jint Posix_fcntlVoid(JNIEnv* env, jobject, jobject javaFd, jint cmd) { 904 int fd = jniGetFDFromFileDescriptor(env, javaFd); 905 return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd))); 906 } 907 908 static void Posix_fdatasync(JNIEnv* env, jobject, jobject javaFd) { 909 int fd = jniGetFDFromFileDescriptor(env, javaFd); 910 throwIfMinusOne(env, "fdatasync", TEMP_FAILURE_RETRY(fdatasync(fd))); 911 } 912 913 static jobject Posix_fstat(JNIEnv* env, jobject, jobject javaFd) { 914 int fd = jniGetFDFromFileDescriptor(env, javaFd); 915 struct stat sb; 916 int rc = TEMP_FAILURE_RETRY(fstat(fd, &sb)); 917 if (rc == -1) { 918 throwErrnoException(env, "fstat"); 919 return NULL; 920 } 921 return makeStructStat(env, sb); 922 } 923 924 static jobject Posix_fstatvfs(JNIEnv* env, jobject, jobject javaFd) { 925 int fd = jniGetFDFromFileDescriptor(env, javaFd); 926 struct statvfs sb; 927 int rc = TEMP_FAILURE_RETRY(fstatvfs(fd, &sb)); 928 if (rc == -1) { 929 throwErrnoException(env, "fstatvfs"); 930 return NULL; 931 } 932 return makeStructStatVfs(env, sb); 933 } 934 935 static void Posix_fsync(JNIEnv* env, jobject, jobject javaFd) { 936 int fd = jniGetFDFromFileDescriptor(env, javaFd); 937 throwIfMinusOne(env, "fsync", TEMP_FAILURE_RETRY(fsync(fd))); 938 } 939 940 static void Posix_ftruncate(JNIEnv* env, jobject, jobject javaFd, jlong length) { 941 int fd = jniGetFDFromFileDescriptor(env, javaFd); 942 throwIfMinusOne(env, "ftruncate", TEMP_FAILURE_RETRY(ftruncate64(fd, length))); 943 } 944 945 static jstring Posix_gai_strerror(JNIEnv* env, jobject, jint error) { 946 return env->NewStringUTF(gai_strerror(error)); 947 } 948 949 static jobjectArray Posix_android_getaddrinfo(JNIEnv* env, jobject, jstring javaNode, 950 jobject javaHints, jint netId) { 951 ScopedUtfChars node(env, javaNode); 952 if (node.c_str() == NULL) { 953 return NULL; 954 } 955 956 static jfieldID flagsFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_flags", "I"); 957 static jfieldID familyFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_family", "I"); 958 static jfieldID socktypeFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_socktype", "I"); 959 static jfieldID protocolFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_protocol", "I"); 960 961 addrinfo hints; 962 memset(&hints, 0, sizeof(hints)); 963 hints.ai_flags = env->GetIntField(javaHints, flagsFid); 964 hints.ai_family = env->GetIntField(javaHints, familyFid); 965 hints.ai_socktype = env->GetIntField(javaHints, socktypeFid); 966 hints.ai_protocol = env->GetIntField(javaHints, protocolFid); 967 968 addrinfo* addressList = NULL; 969 errno = 0; 970 int rc = android_getaddrinfofornet(node.c_str(), NULL, &hints, netId, 0, &addressList); 971 std::unique_ptr<addrinfo, addrinfo_deleter> addressListDeleter(addressList); 972 if (rc != 0) { 973 throwGaiException(env, "android_getaddrinfo", rc); 974 return NULL; 975 } 976 977 // Count results so we know how to size the output array. 978 int addressCount = 0; 979 for (addrinfo* ai = addressList; ai != NULL; ai = ai->ai_next) { 980 if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) { 981 ++addressCount; 982 } else { 983 ALOGE("android_getaddrinfo unexpected ai_family %i", ai->ai_family); 984 } 985 } 986 if (addressCount == 0) { 987 return NULL; 988 } 989 990 // Prepare output array. 991 jobjectArray result = env->NewObjectArray(addressCount, JniConstants::inetAddressClass, NULL); 992 if (result == NULL) { 993 return NULL; 994 } 995 996 // Examine returned addresses one by one, save them in the output array. 997 int index = 0; 998 for (addrinfo* ai = addressList; ai != NULL; ai = ai->ai_next) { 999 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) { 1000 // Unknown address family. Skip this address. 1001 ALOGE("android_getaddrinfo unexpected ai_family %i", ai->ai_family); 1002 continue; 1003 } 1004 1005 // Convert each IP address into a Java byte array. 1006 sockaddr_storage& address = *reinterpret_cast<sockaddr_storage*>(ai->ai_addr); 1007 ScopedLocalRef<jobject> inetAddress(env, sockaddrToInetAddress(env, address, NULL)); 1008 if (inetAddress.get() == NULL) { 1009 return NULL; 1010 } 1011 env->SetObjectArrayElement(result, index, inetAddress.get()); 1012 ++index; 1013 } 1014 return result; 1015 } 1016 1017 static jint Posix_getegid(JNIEnv*, jobject) { 1018 return getegid(); 1019 } 1020 1021 static jint Posix_geteuid(JNIEnv*, jobject) { 1022 return geteuid(); 1023 } 1024 1025 static jint Posix_getgid(JNIEnv*, jobject) { 1026 return getgid(); 1027 } 1028 1029 static jstring Posix_getenv(JNIEnv* env, jobject, jstring javaName) { 1030 ScopedUtfChars name(env, javaName); 1031 if (name.c_str() == NULL) { 1032 return NULL; 1033 } 1034 return env->NewStringUTF(getenv(name.c_str())); 1035 } 1036 1037 static jstring Posix_getnameinfo(JNIEnv* env, jobject, jobject javaAddress, jint flags) { 1038 sockaddr_storage ss; 1039 socklen_t sa_len; 1040 if (!inetAddressToSockaddrVerbatim(env, javaAddress, 0, ss, sa_len)) { 1041 return NULL; 1042 } 1043 char buf[NI_MAXHOST]; // NI_MAXHOST is longer than INET6_ADDRSTRLEN. 1044 errno = 0; 1045 int rc = getnameinfo(reinterpret_cast<sockaddr*>(&ss), sa_len, buf, sizeof(buf), NULL, 0, flags); 1046 if (rc != 0) { 1047 throwGaiException(env, "getnameinfo", rc); 1048 return NULL; 1049 } 1050 return env->NewStringUTF(buf); 1051 } 1052 1053 static jobject Posix_getpeername(JNIEnv* env, jobject, jobject javaFd) { 1054 return doGetSockName(env, javaFd, false); 1055 } 1056 1057 static jint Posix_getpgid(JNIEnv* env, jobject, jint pid) { 1058 return throwIfMinusOne(env, "getpgid", TEMP_FAILURE_RETRY(getpgid(pid))); 1059 } 1060 1061 static jint Posix_getpid(JNIEnv*, jobject) { 1062 return TEMP_FAILURE_RETRY(getpid()); 1063 } 1064 1065 static jint Posix_getppid(JNIEnv*, jobject) { 1066 return TEMP_FAILURE_RETRY(getppid()); 1067 } 1068 1069 static jobject Posix_getpwnam(JNIEnv* env, jobject, jstring javaName) { 1070 ScopedUtfChars name(env, javaName); 1071 if (name.c_str() == NULL) { 1072 return NULL; 1073 } 1074 return Passwd(env).getpwnam(name.c_str()); 1075 } 1076 1077 static jobject Posix_getpwuid(JNIEnv* env, jobject, jint uid) { 1078 return Passwd(env).getpwuid(uid); 1079 } 1080 1081 static jobject Posix_getsockname(JNIEnv* env, jobject, jobject javaFd) { 1082 return doGetSockName(env, javaFd, true); 1083 } 1084 1085 static jint Posix_getsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 1086 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1087 u_char result = 0; 1088 socklen_t size = sizeof(result); 1089 throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size))); 1090 return result; 1091 } 1092 1093 static jobject Posix_getsockoptInAddr(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 1094 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1095 sockaddr_storage ss; 1096 memset(&ss, 0, sizeof(ss)); 1097 ss.ss_family = AF_INET; // This is only for the IPv4-only IP_MULTICAST_IF. 1098 sockaddr_in* sa = reinterpret_cast<sockaddr_in*>(&ss); 1099 socklen_t size = sizeof(sa->sin_addr); 1100 int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &sa->sin_addr, &size)); 1101 if (rc == -1) { 1102 throwErrnoException(env, "getsockopt"); 1103 return NULL; 1104 } 1105 return sockaddrToInetAddress(env, ss, NULL); 1106 } 1107 1108 static jint Posix_getsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 1109 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1110 jint result = 0; 1111 socklen_t size = sizeof(result); 1112 throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size))); 1113 return result; 1114 } 1115 1116 static jobject Posix_getsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 1117 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1118 struct linger l; 1119 socklen_t size = sizeof(l); 1120 memset(&l, 0, size); 1121 int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &l, &size)); 1122 if (rc == -1) { 1123 throwErrnoException(env, "getsockopt"); 1124 return NULL; 1125 } 1126 return makeStructLinger(env, l); 1127 } 1128 1129 static jobject Posix_getsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 1130 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1131 struct timeval tv; 1132 socklen_t size = sizeof(tv); 1133 memset(&tv, 0, size); 1134 int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &tv, &size)); 1135 if (rc == -1) { 1136 throwErrnoException(env, "getsockopt"); 1137 return NULL; 1138 } 1139 return makeStructTimeval(env, tv); 1140 } 1141 1142 static jobject Posix_getsockoptUcred(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 1143 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1144 struct ucred u; 1145 socklen_t size = sizeof(u); 1146 memset(&u, 0, size); 1147 int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &u, &size)); 1148 if (rc == -1) { 1149 throwErrnoException(env, "getsockopt"); 1150 return NULL; 1151 } 1152 return makeStructUcred(env, u); 1153 } 1154 1155 static jint Posix_gettid(JNIEnv* env __unused, jobject) { 1156 #if defined(__BIONIC__) 1157 return TEMP_FAILURE_RETRY(gettid()); 1158 #else 1159 return syscall(__NR_gettid); 1160 #endif 1161 } 1162 1163 static jint Posix_getuid(JNIEnv*, jobject) { 1164 return getuid(); 1165 } 1166 1167 static jint Posix_getxattr(JNIEnv* env, jobject, jstring javaPath, 1168 jstring javaName, jbyteArray javaOutValue) { 1169 ScopedUtfChars path(env, javaPath); 1170 if (path.c_str() == NULL) { 1171 return -1; 1172 } 1173 ScopedUtfChars name(env, javaName); 1174 if (name.c_str() == NULL) { 1175 return -1; 1176 } 1177 ScopedBytesRW outValue(env, javaOutValue); 1178 if (outValue.get() == NULL) { 1179 return -1; 1180 } 1181 size_t outValueLength = env->GetArrayLength(javaOutValue); 1182 ssize_t size = getxattr(path.c_str(), name.c_str(), outValue.get(), outValueLength); 1183 if (size < 0) { 1184 throwErrnoException(env, "getxattr"); 1185 } 1186 return size; 1187 } 1188 1189 static jstring Posix_if_indextoname(JNIEnv* env, jobject, jint index) { 1190 char buf[IF_NAMESIZE]; 1191 char* name = if_indextoname(index, buf); 1192 // if_indextoname(3) returns NULL on failure, which will come out of NewStringUTF unscathed. 1193 // There's no useful information in errno, so we don't bother throwing. Callers can null-check. 1194 return env->NewStringUTF(name); 1195 } 1196 1197 static jobject Posix_inet_pton(JNIEnv* env, jobject, jint family, jstring javaName) { 1198 ScopedUtfChars name(env, javaName); 1199 if (name.c_str() == NULL) { 1200 return NULL; 1201 } 1202 sockaddr_storage ss; 1203 memset(&ss, 0, sizeof(ss)); 1204 // sockaddr_in and sockaddr_in6 are at the same address, so we can use either here. 1205 void* dst = &reinterpret_cast<sockaddr_in*>(&ss)->sin_addr; 1206 if (inet_pton(family, name.c_str(), dst) != 1) { 1207 return NULL; 1208 } 1209 ss.ss_family = family; 1210 return sockaddrToInetAddress(env, ss, NULL); 1211 } 1212 1213 static jobject Posix_ioctlInetAddress(JNIEnv* env, jobject, jobject javaFd, jint cmd, jstring javaInterfaceName) { 1214 struct ifreq req; 1215 if (!fillIfreq(env, javaInterfaceName, req)) { 1216 return NULL; 1217 } 1218 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1219 int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &req))); 1220 if (rc == -1) { 1221 return NULL; 1222 } 1223 return sockaddrToInetAddress(env, reinterpret_cast<sockaddr_storage&>(req.ifr_addr), NULL); 1224 } 1225 1226 static jint Posix_ioctlInt(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaArg) { 1227 // This is complicated because ioctls may return their result by updating their argument 1228 // or via their return value, so we need to support both. 1229 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1230 static jfieldID valueFid = env->GetFieldID(JniConstants::mutableIntClass, "value", "I"); 1231 jint arg = env->GetIntField(javaArg, valueFid); 1232 int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &arg))); 1233 if (!env->ExceptionCheck()) { 1234 env->SetIntField(javaArg, valueFid, arg); 1235 } 1236 return rc; 1237 } 1238 1239 static jboolean Posix_isatty(JNIEnv* env, jobject, jobject javaFd) { 1240 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1241 return TEMP_FAILURE_RETRY(isatty(fd)) == 1; 1242 } 1243 1244 static void Posix_kill(JNIEnv* env, jobject, jint pid, jint sig) { 1245 throwIfMinusOne(env, "kill", TEMP_FAILURE_RETRY(kill(pid, sig))); 1246 } 1247 1248 static void Posix_lchown(JNIEnv* env, jobject, jstring javaPath, jint uid, jint gid) { 1249 ScopedUtfChars path(env, javaPath); 1250 if (path.c_str() == NULL) { 1251 return; 1252 } 1253 throwIfMinusOne(env, "lchown", TEMP_FAILURE_RETRY(lchown(path.c_str(), uid, gid))); 1254 } 1255 1256 static void Posix_link(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) { 1257 ScopedUtfChars oldPath(env, javaOldPath); 1258 if (oldPath.c_str() == NULL) { 1259 return; 1260 } 1261 ScopedUtfChars newPath(env, javaNewPath); 1262 if (newPath.c_str() == NULL) { 1263 return; 1264 } 1265 throwIfMinusOne(env, "link", TEMP_FAILURE_RETRY(link(oldPath.c_str(), newPath.c_str()))); 1266 } 1267 1268 static void Posix_listen(JNIEnv* env, jobject, jobject javaFd, jint backlog) { 1269 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1270 throwIfMinusOne(env, "listen", TEMP_FAILURE_RETRY(listen(fd, backlog))); 1271 } 1272 1273 static jlong Posix_lseek(JNIEnv* env, jobject, jobject javaFd, jlong offset, jint whence) { 1274 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1275 return throwIfMinusOne(env, "lseek", TEMP_FAILURE_RETRY(lseek64(fd, offset, whence))); 1276 } 1277 1278 static jobject Posix_lstat(JNIEnv* env, jobject, jstring javaPath) { 1279 return doStat(env, javaPath, true); 1280 } 1281 1282 static void Posix_mincore(JNIEnv* env, jobject, jlong address, jlong byteCount, jbyteArray javaVector) { 1283 ScopedByteArrayRW vector(env, javaVector); 1284 if (vector.get() == NULL) { 1285 return; 1286 } 1287 void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 1288 unsigned char* vec = reinterpret_cast<unsigned char*>(vector.get()); 1289 throwIfMinusOne(env, "mincore", TEMP_FAILURE_RETRY(mincore(ptr, byteCount, vec))); 1290 } 1291 1292 static void Posix_mkdir(JNIEnv* env, jobject, jstring javaPath, jint mode) { 1293 ScopedUtfChars path(env, javaPath); 1294 if (path.c_str() == NULL) { 1295 return; 1296 } 1297 throwIfMinusOne(env, "mkdir", TEMP_FAILURE_RETRY(mkdir(path.c_str(), mode))); 1298 } 1299 1300 static void Posix_mkfifo(JNIEnv* env, jobject, jstring javaPath, jint mode) { 1301 ScopedUtfChars path(env, javaPath); 1302 if (path.c_str() == NULL) { 1303 return; 1304 } 1305 throwIfMinusOne(env, "mkfifo", TEMP_FAILURE_RETRY(mkfifo(path.c_str(), mode))); 1306 } 1307 1308 static void Posix_mlock(JNIEnv* env, jobject, jlong address, jlong byteCount) { 1309 void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 1310 throwIfMinusOne(env, "mlock", TEMP_FAILURE_RETRY(mlock(ptr, byteCount))); 1311 } 1312 1313 static jlong Posix_mmap(JNIEnv* env, jobject, jlong address, jlong byteCount, jint prot, jint flags, jobject javaFd, jlong offset) { 1314 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1315 void* suggestedPtr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 1316 void* ptr = mmap(suggestedPtr, byteCount, prot, flags, fd, offset); 1317 if (ptr == MAP_FAILED) { 1318 throwErrnoException(env, "mmap"); 1319 } 1320 return static_cast<jlong>(reinterpret_cast<uintptr_t>(ptr)); 1321 } 1322 1323 static void Posix_msync(JNIEnv* env, jobject, jlong address, jlong byteCount, jint flags) { 1324 void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 1325 throwIfMinusOne(env, "msync", TEMP_FAILURE_RETRY(msync(ptr, byteCount, flags))); 1326 } 1327 1328 static void Posix_munlock(JNIEnv* env, jobject, jlong address, jlong byteCount) { 1329 void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 1330 throwIfMinusOne(env, "munlock", TEMP_FAILURE_RETRY(munlock(ptr, byteCount))); 1331 } 1332 1333 static void Posix_munmap(JNIEnv* env, jobject, jlong address, jlong byteCount) { 1334 void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 1335 throwIfMinusOne(env, "munmap", TEMP_FAILURE_RETRY(munmap(ptr, byteCount))); 1336 } 1337 1338 static jobject Posix_open(JNIEnv* env, jobject, jstring javaPath, jint flags, jint mode) { 1339 ScopedUtfChars path(env, javaPath); 1340 if (path.c_str() == NULL) { 1341 return NULL; 1342 } 1343 int fd = throwIfMinusOne(env, "open", TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode))); 1344 return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL; 1345 } 1346 1347 static jobjectArray Posix_pipe2(JNIEnv* env, jobject, jint flags __unused) { 1348 int fds[2]; 1349 throwIfMinusOne(env, "pipe2", TEMP_FAILURE_RETRY(pipe2(&fds[0], flags))); 1350 jobjectArray result = env->NewObjectArray(2, JniConstants::fileDescriptorClass, NULL); 1351 if (result == NULL) { 1352 return NULL; 1353 } 1354 for (int i = 0; i < 2; ++i) { 1355 ScopedLocalRef<jobject> fd(env, jniCreateFileDescriptor(env, fds[i])); 1356 if (fd.get() == NULL) { 1357 return NULL; 1358 } 1359 env->SetObjectArrayElement(result, i, fd.get()); 1360 if (env->ExceptionCheck()) { 1361 return NULL; 1362 } 1363 } 1364 return result; 1365 } 1366 1367 static jint Posix_poll(JNIEnv* env, jobject, jobjectArray javaStructs, jint timeoutMs) { 1368 static jfieldID fdFid = env->GetFieldID(JniConstants::structPollfdClass, "fd", "Ljava/io/FileDescriptor;"); 1369 static jfieldID eventsFid = env->GetFieldID(JniConstants::structPollfdClass, "events", "S"); 1370 static jfieldID reventsFid = env->GetFieldID(JniConstants::structPollfdClass, "revents", "S"); 1371 1372 // Turn the Java android.system.StructPollfd[] into a C++ struct pollfd[]. 1373 size_t arrayLength = env->GetArrayLength(javaStructs); 1374 std::unique_ptr<struct pollfd[]> fds(new struct pollfd[arrayLength]); 1375 memset(fds.get(), 0, sizeof(struct pollfd) * arrayLength); 1376 size_t count = 0; // Some trailing array elements may be irrelevant. (See below.) 1377 for (size_t i = 0; i < arrayLength; ++i) { 1378 ScopedLocalRef<jobject> javaStruct(env, env->GetObjectArrayElement(javaStructs, i)); 1379 if (javaStruct.get() == NULL) { 1380 break; // We allow trailing nulls in the array for caller convenience. 1381 } 1382 ScopedLocalRef<jobject> javaFd(env, env->GetObjectField(javaStruct.get(), fdFid)); 1383 if (javaFd.get() == NULL) { 1384 break; // We also allow callers to just clear the fd field (this is what Selector does). 1385 } 1386 fds[count].fd = jniGetFDFromFileDescriptor(env, javaFd.get()); 1387 fds[count].events = env->GetShortField(javaStruct.get(), eventsFid); 1388 ++count; 1389 } 1390 1391 std::vector<AsynchronousCloseMonitor*> monitors; 1392 for (size_t i = 0; i < count; ++i) { 1393 monitors.push_back(new AsynchronousCloseMonitor(fds[i].fd)); 1394 } 1395 1396 int rc; 1397 while (true) { 1398 timespec before; 1399 clock_gettime(CLOCK_MONOTONIC, &before); 1400 1401 rc = poll(fds.get(), count, timeoutMs); 1402 if (rc >= 0 || errno != EINTR) { 1403 break; 1404 } 1405 1406 // We got EINTR. Work out how much of the original timeout is still left. 1407 if (timeoutMs > 0) { 1408 timespec now; 1409 clock_gettime(CLOCK_MONOTONIC, &now); 1410 1411 timespec diff; 1412 diff.tv_sec = now.tv_sec - before.tv_sec; 1413 diff.tv_nsec = now.tv_nsec - before.tv_nsec; 1414 if (diff.tv_nsec < 0) { 1415 --diff.tv_sec; 1416 diff.tv_nsec += 1000000000; 1417 } 1418 1419 jint diffMs = diff.tv_sec * 1000 + diff.tv_nsec / 1000000; 1420 if (diffMs >= timeoutMs) { 1421 rc = 0; // We have less than 1ms left anyway, so just time out. 1422 break; 1423 } 1424 1425 timeoutMs -= diffMs; 1426 } 1427 } 1428 1429 for (size_t i = 0; i < monitors.size(); ++i) { 1430 delete monitors[i]; 1431 } 1432 if (rc == -1) { 1433 throwErrnoException(env, "poll"); 1434 return -1; 1435 } 1436 1437 // Update the revents fields in the Java android.system.StructPollfd[]. 1438 for (size_t i = 0; i < count; ++i) { 1439 ScopedLocalRef<jobject> javaStruct(env, env->GetObjectArrayElement(javaStructs, i)); 1440 if (javaStruct.get() == NULL) { 1441 return -1; 1442 } 1443 env->SetShortField(javaStruct.get(), reventsFid, fds[i].revents); 1444 } 1445 return rc; 1446 } 1447 1448 static void Posix_posix_fallocate(JNIEnv* env, jobject, jobject javaFd __unused, 1449 jlong offset __unused, jlong length __unused) { 1450 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1451 while ((errno = posix_fallocate64(fd, offset, length)) == EINTR) { 1452 } 1453 if (errno != 0) { 1454 throwErrnoException(env, "posix_fallocate"); 1455 } 1456 } 1457 1458 static jint Posix_prctl(JNIEnv* env, jobject, jint option __unused, jlong arg2 __unused, 1459 jlong arg3 __unused, jlong arg4 __unused, jlong arg5 __unused) { 1460 int result = TEMP_FAILURE_RETRY(prctl(static_cast<int>(option), 1461 static_cast<unsigned long>(arg2), 1462 static_cast<unsigned long>(arg3), 1463 static_cast<unsigned long>(arg4), 1464 static_cast<unsigned long>(arg5))); 1465 return throwIfMinusOne(env, "prctl", result); 1466 } 1467 1468 static jint Posix_preadBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount, jlong offset) { 1469 ScopedBytesRW bytes(env, javaBytes); 1470 if (bytes.get() == NULL) { 1471 return -1; 1472 } 1473 return IO_FAILURE_RETRY(env, ssize_t, pread64, javaFd, bytes.get() + byteOffset, byteCount, offset); 1474 } 1475 1476 static jint Posix_pwriteBytes(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount, jlong offset) { 1477 ScopedBytesRO bytes(env, javaBytes); 1478 if (bytes.get() == NULL) { 1479 return -1; 1480 } 1481 return IO_FAILURE_RETRY(env, ssize_t, pwrite64, javaFd, bytes.get() + byteOffset, byteCount, offset); 1482 } 1483 1484 static jint Posix_readBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount) { 1485 ScopedBytesRW bytes(env, javaBytes); 1486 if (bytes.get() == NULL) { 1487 return -1; 1488 } 1489 return IO_FAILURE_RETRY(env, ssize_t, read, javaFd, bytes.get() + byteOffset, byteCount); 1490 } 1491 1492 static jstring Posix_readlink(JNIEnv* env, jobject, jstring javaPath) { 1493 ScopedUtfChars path(env, javaPath); 1494 if (path.c_str() == NULL) { 1495 return NULL; 1496 } 1497 1498 std::string result; 1499 if (!readlink(path.c_str(), result)) { 1500 throwErrnoException(env, "readlink"); 1501 return NULL; 1502 } 1503 return env->NewStringUTF(result.c_str()); 1504 } 1505 1506 static jstring Posix_realpath(JNIEnv* env, jobject, jstring javaPath) { 1507 ScopedUtfChars path(env, javaPath); 1508 if (path.c_str() == NULL) { 1509 return NULL; 1510 } 1511 1512 std::unique_ptr<char, c_deleter> real_path(realpath(path.c_str(), nullptr)); 1513 if (real_path.get() == nullptr) { 1514 throwErrnoException(env, "realpath"); 1515 return NULL; 1516 } 1517 1518 return env->NewStringUTF(real_path.get()); 1519 } 1520 1521 static jint Posix_readv(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) { 1522 IoVec<ScopedBytesRW> ioVec(env, env->GetArrayLength(buffers)); 1523 if (!ioVec.init(buffers, offsets, byteCounts)) { 1524 return -1; 1525 } 1526 return IO_FAILURE_RETRY(env, ssize_t, readv, javaFd, ioVec.get(), ioVec.size()); 1527 } 1528 1529 static jint Posix_recvfromBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount, jint flags, jobject javaInetSocketAddress) { 1530 ScopedBytesRW bytes(env, javaBytes); 1531 if (bytes.get() == NULL) { 1532 return -1; 1533 } 1534 sockaddr_storage ss; 1535 socklen_t sl = sizeof(ss); 1536 memset(&ss, 0, sizeof(ss)); 1537 sockaddr* from = (javaInetSocketAddress != NULL) ? reinterpret_cast<sockaddr*>(&ss) : NULL; 1538 socklen_t* fromLength = (javaInetSocketAddress != NULL) ? &sl : 0; 1539 jint recvCount = NET_FAILURE_RETRY(env, ssize_t, recvfrom, javaFd, bytes.get() + byteOffset, byteCount, flags, from, fromLength); 1540 if (recvCount > 0) { 1541 fillInetSocketAddress(env, javaInetSocketAddress, ss); 1542 } 1543 return recvCount; 1544 } 1545 1546 static void Posix_remove(JNIEnv* env, jobject, jstring javaPath) { 1547 ScopedUtfChars path(env, javaPath); 1548 if (path.c_str() == NULL) { 1549 return; 1550 } 1551 throwIfMinusOne(env, "remove", TEMP_FAILURE_RETRY(remove(path.c_str()))); 1552 } 1553 1554 static void Posix_removexattr(JNIEnv* env, jobject, jstring javaPath, jstring javaName) { 1555 ScopedUtfChars path(env, javaPath); 1556 if (path.c_str() == NULL) { 1557 return; 1558 } 1559 ScopedUtfChars name(env, javaName); 1560 if (name.c_str() == NULL) { 1561 return; 1562 } 1563 1564 int res = removexattr(path.c_str(), name.c_str()); 1565 if (res < 0) { 1566 throwErrnoException(env, "removexattr"); 1567 } 1568 } 1569 1570 static void Posix_rename(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) { 1571 ScopedUtfChars oldPath(env, javaOldPath); 1572 if (oldPath.c_str() == NULL) { 1573 return; 1574 } 1575 ScopedUtfChars newPath(env, javaNewPath); 1576 if (newPath.c_str() == NULL) { 1577 return; 1578 } 1579 throwIfMinusOne(env, "rename", TEMP_FAILURE_RETRY(rename(oldPath.c_str(), newPath.c_str()))); 1580 } 1581 1582 static jlong Posix_sendfile(JNIEnv* env, jobject, jobject javaOutFd, jobject javaInFd, jobject javaOffset, jlong byteCount) { 1583 int outFd = jniGetFDFromFileDescriptor(env, javaOutFd); 1584 int inFd = jniGetFDFromFileDescriptor(env, javaInFd); 1585 static jfieldID valueFid = env->GetFieldID(JniConstants::mutableLongClass, "value", "J"); 1586 off_t offset = 0; 1587 off_t* offsetPtr = NULL; 1588 if (javaOffset != NULL) { 1589 // TODO: fix bionic so we can have a 64-bit off_t! 1590 offset = env->GetLongField(javaOffset, valueFid); 1591 offsetPtr = &offset; 1592 } 1593 jlong result = throwIfMinusOne(env, "sendfile", TEMP_FAILURE_RETRY(sendfile(outFd, inFd, offsetPtr, byteCount))); 1594 if (javaOffset != NULL) { 1595 env->SetLongField(javaOffset, valueFid, offset); 1596 } 1597 return result; 1598 } 1599 1600 static jint Posix_sendtoBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount, jint flags, jobject javaInetAddress, jint port) { 1601 ScopedBytesRO bytes(env, javaBytes); 1602 if (bytes.get() == NULL) { 1603 return -1; 1604 } 1605 1606 return NET_IPV4_FALLBACK(env, ssize_t, sendto, javaFd, javaInetAddress, port, 1607 NULL_ADDR_OK, bytes.get() + byteOffset, byteCount, flags); 1608 } 1609 1610 static jint Posix_sendtoBytesSocketAddress(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount, jint flags, jobject javaSocketAddress) { 1611 if (env->IsInstanceOf(javaSocketAddress, JniConstants::inetSocketAddressClass)) { 1612 // Use the InetAddress version so we get the benefit of NET_IPV4_FALLBACK. 1613 jobject javaInetAddress; 1614 jint port; 1615 javaInetSocketAddressToInetAddressAndPort(env, javaSocketAddress, javaInetAddress, port); 1616 return Posix_sendtoBytes(env, NULL, javaFd, javaBytes, byteOffset, byteCount, flags, 1617 javaInetAddress, port); 1618 } 1619 1620 ScopedBytesRO bytes(env, javaBytes); 1621 if (bytes.get() == NULL) { 1622 return -1; 1623 } 1624 1625 sockaddr_storage ss; 1626 socklen_t sa_len; 1627 if (!javaSocketAddressToSockaddr(env, javaSocketAddress, ss, sa_len)) { 1628 return -1; 1629 } 1630 1631 const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss); 1632 // We don't need the return value because we'll already have thrown. 1633 return NET_FAILURE_RETRY(env, ssize_t, sendto, javaFd, bytes.get() + byteOffset, byteCount, flags, sa, sa_len); 1634 } 1635 1636 static void Posix_setegid(JNIEnv* env, jobject, jint egid) { 1637 throwIfMinusOne(env, "setegid", TEMP_FAILURE_RETRY(setegid(egid))); 1638 } 1639 1640 static void Posix_setenv(JNIEnv* env, jobject, jstring javaName, jstring javaValue, jboolean overwrite) { 1641 ScopedUtfChars name(env, javaName); 1642 if (name.c_str() == NULL) { 1643 return; 1644 } 1645 ScopedUtfChars value(env, javaValue); 1646 if (value.c_str() == NULL) { 1647 return; 1648 } 1649 throwIfMinusOne(env, "setenv", setenv(name.c_str(), value.c_str(), overwrite)); 1650 } 1651 1652 static void Posix_seteuid(JNIEnv* env, jobject, jint euid) { 1653 throwIfMinusOne(env, "seteuid", TEMP_FAILURE_RETRY(seteuid(euid))); 1654 } 1655 1656 static void Posix_setgid(JNIEnv* env, jobject, jint gid) { 1657 throwIfMinusOne(env, "setgid", TEMP_FAILURE_RETRY(setgid(gid))); 1658 } 1659 1660 static void Posix_setpgid(JNIEnv* env, jobject, jint pid, int pgid) { 1661 throwIfMinusOne(env, "setpgid", TEMP_FAILURE_RETRY(setpgid(pid, pgid))); 1662 } 1663 1664 static void Posix_setregid(JNIEnv* env, jobject, jint rgid, int egid) { 1665 throwIfMinusOne(env, "setregid", TEMP_FAILURE_RETRY(setregid(rgid, egid))); 1666 } 1667 1668 static void Posix_setreuid(JNIEnv* env, jobject, jint ruid, int euid) { 1669 throwIfMinusOne(env, "setreuid", TEMP_FAILURE_RETRY(setreuid(ruid, euid))); 1670 } 1671 1672 static jint Posix_setsid(JNIEnv* env, jobject) { 1673 return throwIfMinusOne(env, "setsid", TEMP_FAILURE_RETRY(setsid())); 1674 } 1675 1676 static void Posix_setsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 1677 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1678 u_char byte = value; 1679 throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &byte, sizeof(byte)))); 1680 } 1681 1682 static void Posix_setsockoptIfreq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jstring javaInterfaceName) { 1683 struct ifreq req; 1684 if (!fillIfreq(env, javaInterfaceName, req)) { 1685 return; 1686 } 1687 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1688 throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req)))); 1689 } 1690 1691 static void Posix_setsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 1692 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1693 throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 1694 } 1695 1696 static void Posix_setsockoptIpMreqn(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 1697 ip_mreqn req; 1698 memset(&req, 0, sizeof(req)); 1699 req.imr_ifindex = value; 1700 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1701 throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req)))); 1702 } 1703 1704 static void Posix_setsockoptGroupReq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaGroupReq) { 1705 struct group_req req; 1706 memset(&req, 0, sizeof(req)); 1707 1708 static jfieldID grInterfaceFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_interface", "I"); 1709 req.gr_interface = env->GetIntField(javaGroupReq, grInterfaceFid); 1710 // Get the IPv4 or IPv6 multicast address to join or leave. 1711 static jfieldID grGroupFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_group", "Ljava/net/InetAddress;"); 1712 ScopedLocalRef<jobject> javaGroup(env, env->GetObjectField(javaGroupReq, grGroupFid)); 1713 socklen_t sa_len; 1714 if (!inetAddressToSockaddrVerbatim(env, javaGroup.get(), 0, req.gr_group, sa_len)) { 1715 return; 1716 } 1717 1718 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1719 int rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req))); 1720 if (rc == -1 && errno == EINVAL) { 1721 // Maybe we're a 32-bit binary talking to a 64-bit kernel? 1722 // glibc doesn't automatically handle this. 1723 // http://sourceware.org/bugzilla/show_bug.cgi?id=12080 1724 struct group_req64 { 1725 uint32_t gr_interface; 1726 uint32_t my_padding; 1727 sockaddr_storage gr_group; 1728 }; 1729 group_req64 req64; 1730 req64.gr_interface = req.gr_interface; 1731 memcpy(&req64.gr_group, &req.gr_group, sizeof(req.gr_group)); 1732 rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req64, sizeof(req64))); 1733 } 1734 throwIfMinusOne(env, "setsockopt", rc); 1735 } 1736 1737 static void Posix_setsockoptGroupSourceReq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaGroupSourceReq) { 1738 socklen_t sa_len; 1739 struct group_source_req req; 1740 memset(&req, 0, sizeof(req)); 1741 1742 static jfieldID gsrInterfaceFid = env->GetFieldID(JniConstants::structGroupSourceReqClass, "gsr_interface", "I"); 1743 req.gsr_interface = env->GetIntField(javaGroupSourceReq, gsrInterfaceFid); 1744 // Get the IPv4 or IPv6 multicast address to join or leave. 1745 static jfieldID gsrGroupFid = env->GetFieldID(JniConstants::structGroupSourceReqClass, "gsr_group", "Ljava/net/InetAddress;"); 1746 ScopedLocalRef<jobject> javaGroup(env, env->GetObjectField(javaGroupSourceReq, gsrGroupFid)); 1747 if (!inetAddressToSockaddrVerbatim(env, javaGroup.get(), 0, req.gsr_group, sa_len)) { 1748 return; 1749 } 1750 1751 // Get the IPv4 or IPv6 multicast address to add to the filter. 1752 static jfieldID gsrSourceFid = env->GetFieldID(JniConstants::structGroupSourceReqClass, "gsr_source", "Ljava/net/InetAddress;"); 1753 ScopedLocalRef<jobject> javaSource(env, env->GetObjectField(javaGroupSourceReq, gsrSourceFid)); 1754 if (!inetAddressToSockaddrVerbatim(env, javaSource.get(), 0, req.gsr_source, sa_len)) { 1755 return; 1756 } 1757 1758 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1759 int rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req))); 1760 if (rc == -1 && errno == EINVAL) { 1761 // Maybe we're a 32-bit binary talking to a 64-bit kernel? 1762 // glibc doesn't automatically handle this. 1763 // http://sourceware.org/bugzilla/show_bug.cgi?id=12080 1764 struct group_source_req64 { 1765 uint32_t gsr_interface; 1766 uint32_t my_padding; 1767 sockaddr_storage gsr_group; 1768 sockaddr_storage gsr_source; 1769 }; 1770 group_source_req64 req64; 1771 req64.gsr_interface = req.gsr_interface; 1772 memcpy(&req64.gsr_group, &req.gsr_group, sizeof(req.gsr_group)); 1773 memcpy(&req64.gsr_source, &req.gsr_source, sizeof(req.gsr_source)); 1774 rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req64, sizeof(req64))); 1775 } 1776 throwIfMinusOne(env, "setsockopt", rc); 1777 } 1778 1779 static void Posix_setsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaLinger) { 1780 static jfieldID lOnoffFid = env->GetFieldID(JniConstants::structLingerClass, "l_onoff", "I"); 1781 static jfieldID lLingerFid = env->GetFieldID(JniConstants::structLingerClass, "l_linger", "I"); 1782 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1783 struct linger value; 1784 value.l_onoff = env->GetIntField(javaLinger, lOnoffFid); 1785 value.l_linger = env->GetIntField(javaLinger, lLingerFid); 1786 throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 1787 } 1788 1789 static void Posix_setsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaTimeval) { 1790 static jfieldID tvSecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_sec", "J"); 1791 static jfieldID tvUsecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_usec", "J"); 1792 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1793 struct timeval value; 1794 value.tv_sec = env->GetLongField(javaTimeval, tvSecFid); 1795 value.tv_usec = env->GetLongField(javaTimeval, tvUsecFid); 1796 throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 1797 } 1798 1799 static void Posix_setuid(JNIEnv* env, jobject, jint uid) { 1800 throwIfMinusOne(env, "setuid", TEMP_FAILURE_RETRY(setuid(uid))); 1801 } 1802 1803 static void Posix_setxattr(JNIEnv* env, jobject, jstring javaPath, jstring javaName, 1804 jbyteArray javaValue, jint flags) { 1805 ScopedUtfChars path(env, javaPath); 1806 if (path.c_str() == NULL) { 1807 return; 1808 } 1809 ScopedUtfChars name(env, javaName); 1810 if (name.c_str() == NULL) { 1811 return; 1812 } 1813 ScopedBytesRO value(env, javaValue); 1814 if (value.get() == NULL) { 1815 return; 1816 } 1817 size_t valueLength = env->GetArrayLength(javaValue); 1818 int res = setxattr(path.c_str(), name.c_str(), value.get(), valueLength, flags); 1819 if (res < 0) { 1820 throwErrnoException(env, "setxattr"); 1821 } 1822 } 1823 1824 static void Posix_shutdown(JNIEnv* env, jobject, jobject javaFd, jint how) { 1825 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1826 throwIfMinusOne(env, "shutdown", TEMP_FAILURE_RETRY(shutdown(fd, how))); 1827 } 1828 1829 static jobject Posix_socket(JNIEnv* env, jobject, jint domain, jint type, jint protocol) { 1830 if (domain == AF_PACKET) { 1831 protocol = htons(protocol); // Packet sockets specify the protocol in host byte order. 1832 } 1833 int fd = throwIfMinusOne(env, "socket", TEMP_FAILURE_RETRY(socket(domain, type, protocol))); 1834 return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL; 1835 } 1836 1837 static void Posix_socketpair(JNIEnv* env, jobject, jint domain, jint type, jint protocol, jobject javaFd1, jobject javaFd2) { 1838 int fds[2]; 1839 int rc = throwIfMinusOne(env, "socketpair", TEMP_FAILURE_RETRY(socketpair(domain, type, protocol, fds))); 1840 if (rc != -1) { 1841 jniSetFileDescriptorOfFD(env, javaFd1, fds[0]); 1842 jniSetFileDescriptorOfFD(env, javaFd2, fds[1]); 1843 } 1844 } 1845 1846 static jobject Posix_stat(JNIEnv* env, jobject, jstring javaPath) { 1847 return doStat(env, javaPath, false); 1848 } 1849 1850 static jobject Posix_statvfs(JNIEnv* env, jobject, jstring javaPath) { 1851 ScopedUtfChars path(env, javaPath); 1852 if (path.c_str() == NULL) { 1853 return NULL; 1854 } 1855 struct statvfs sb; 1856 int rc = TEMP_FAILURE_RETRY(statvfs(path.c_str(), &sb)); 1857 if (rc == -1) { 1858 throwErrnoException(env, "statvfs"); 1859 return NULL; 1860 } 1861 return makeStructStatVfs(env, sb); 1862 } 1863 1864 static jstring Posix_strerror(JNIEnv* env, jobject, jint errnum) { 1865 char buffer[BUFSIZ]; 1866 const char* message = jniStrError(errnum, buffer, sizeof(buffer)); 1867 return env->NewStringUTF(message); 1868 } 1869 1870 static jstring Posix_strsignal(JNIEnv* env, jobject, jint signal) { 1871 return env->NewStringUTF(strsignal(signal)); 1872 } 1873 1874 static void Posix_symlink(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) { 1875 ScopedUtfChars oldPath(env, javaOldPath); 1876 if (oldPath.c_str() == NULL) { 1877 return; 1878 } 1879 ScopedUtfChars newPath(env, javaNewPath); 1880 if (newPath.c_str() == NULL) { 1881 return; 1882 } 1883 throwIfMinusOne(env, "symlink", TEMP_FAILURE_RETRY(symlink(oldPath.c_str(), newPath.c_str()))); 1884 } 1885 1886 static jlong Posix_sysconf(JNIEnv* env, jobject, jint name) { 1887 // Since -1 is a valid result from sysconf(3), detecting failure is a little more awkward. 1888 errno = 0; 1889 long result = sysconf(name); 1890 if (result == -1L && errno == EINVAL) { 1891 throwErrnoException(env, "sysconf"); 1892 } 1893 return result; 1894 } 1895 1896 static void Posix_tcdrain(JNIEnv* env, jobject, jobject javaFd) { 1897 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1898 throwIfMinusOne(env, "tcdrain", TEMP_FAILURE_RETRY(tcdrain(fd))); 1899 } 1900 1901 static void Posix_tcsendbreak(JNIEnv* env, jobject, jobject javaFd, jint duration) { 1902 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1903 throwIfMinusOne(env, "tcsendbreak", TEMP_FAILURE_RETRY(tcsendbreak(fd, duration))); 1904 } 1905 1906 static jint Posix_umaskImpl(JNIEnv*, jobject, jint mask) { 1907 return umask(mask); 1908 } 1909 1910 static jobject Posix_uname(JNIEnv* env, jobject) { 1911 struct utsname buf; 1912 if (TEMP_FAILURE_RETRY(uname(&buf)) == -1) { 1913 return NULL; // Can't happen. 1914 } 1915 return makeStructUtsname(env, buf); 1916 } 1917 1918 static void Posix_unlink(JNIEnv* env, jobject, jstring javaPathname) { 1919 ScopedUtfChars pathname(env, javaPathname); 1920 if (pathname.c_str() == NULL) { 1921 return; 1922 } 1923 throwIfMinusOne(env, "unlink", unlink(pathname.c_str())); 1924 } 1925 1926 static void Posix_unsetenv(JNIEnv* env, jobject, jstring javaName) { 1927 ScopedUtfChars name(env, javaName); 1928 if (name.c_str() == NULL) { 1929 return; 1930 } 1931 throwIfMinusOne(env, "unsetenv", unsetenv(name.c_str())); 1932 } 1933 1934 static jint Posix_waitpid(JNIEnv* env, jobject, jint pid, jobject javaStatus, jint options) { 1935 int status; 1936 int rc = throwIfMinusOne(env, "waitpid", TEMP_FAILURE_RETRY(waitpid(pid, &status, options))); 1937 if (rc != -1) { 1938 static jfieldID valueFid = env->GetFieldID(JniConstants::mutableIntClass, "value", "I"); 1939 env->SetIntField(javaStatus, valueFid, status); 1940 } 1941 return rc; 1942 } 1943 1944 static jint Posix_writeBytes(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount) { 1945 ScopedBytesRO bytes(env, javaBytes); 1946 if (bytes.get() == NULL) { 1947 return -1; 1948 } 1949 return IO_FAILURE_RETRY(env, ssize_t, write, javaFd, bytes.get() + byteOffset, byteCount); 1950 } 1951 1952 static jint Posix_writev(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) { 1953 IoVec<ScopedBytesRO> ioVec(env, env->GetArrayLength(buffers)); 1954 if (!ioVec.init(buffers, offsets, byteCounts)) { 1955 return -1; 1956 } 1957 return IO_FAILURE_RETRY(env, ssize_t, writev, javaFd, ioVec.get(), ioVec.size()); 1958 } 1959 1960 #define NATIVE_METHOD_OVERLOAD(className, functionName, signature, variant) \ 1961 { #functionName, signature, reinterpret_cast<void*>(className ## _ ## functionName ## variant) } 1962 1963 static JNINativeMethod gMethods[] = { 1964 NATIVE_METHOD(Posix, accept, "(Ljava/io/FileDescriptor;Ljava/net/SocketAddress;)Ljava/io/FileDescriptor;"), 1965 NATIVE_METHOD(Posix, access, "(Ljava/lang/String;I)Z"), 1966 NATIVE_METHOD(Posix, android_getaddrinfo, "(Ljava/lang/String;Landroid/system/StructAddrinfo;I)[Ljava/net/InetAddress;"), 1967 NATIVE_METHOD(Posix, bind, "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)V"), 1968 NATIVE_METHOD_OVERLOAD(Posix, bind, "(Ljava/io/FileDescriptor;Ljava/net/SocketAddress;)V", SocketAddress), 1969 NATIVE_METHOD(Posix, chmod, "(Ljava/lang/String;I)V"), 1970 NATIVE_METHOD(Posix, chown, "(Ljava/lang/String;II)V"), 1971 NATIVE_METHOD(Posix, close, "(Ljava/io/FileDescriptor;)V"), 1972 NATIVE_METHOD(Posix, connect, "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)V"), 1973 NATIVE_METHOD_OVERLOAD(Posix, connect, "(Ljava/io/FileDescriptor;Ljava/net/SocketAddress;)V", SocketAddress), 1974 NATIVE_METHOD(Posix, dup, "(Ljava/io/FileDescriptor;)Ljava/io/FileDescriptor;"), 1975 NATIVE_METHOD(Posix, dup2, "(Ljava/io/FileDescriptor;I)Ljava/io/FileDescriptor;"), 1976 NATIVE_METHOD(Posix, environ, "()[Ljava/lang/String;"), 1977 NATIVE_METHOD(Posix, execv, "(Ljava/lang/String;[Ljava/lang/String;)V"), 1978 NATIVE_METHOD(Posix, execve, "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)V"), 1979 NATIVE_METHOD(Posix, fchmod, "(Ljava/io/FileDescriptor;I)V"), 1980 NATIVE_METHOD(Posix, fchown, "(Ljava/io/FileDescriptor;II)V"), 1981 NATIVE_METHOD(Posix, fcntlFlock, "(Ljava/io/FileDescriptor;ILandroid/system/StructFlock;)I"), 1982 NATIVE_METHOD(Posix, fcntlInt, "(Ljava/io/FileDescriptor;II)I"), 1983 NATIVE_METHOD(Posix, fcntlVoid, "(Ljava/io/FileDescriptor;I)I"), 1984 NATIVE_METHOD(Posix, fdatasync, "(Ljava/io/FileDescriptor;)V"), 1985 NATIVE_METHOD(Posix, fstat, "(Ljava/io/FileDescriptor;)Landroid/system/StructStat;"), 1986 NATIVE_METHOD(Posix, fstatvfs, "(Ljava/io/FileDescriptor;)Landroid/system/StructStatVfs;"), 1987 NATIVE_METHOD(Posix, fsync, "(Ljava/io/FileDescriptor;)V"), 1988 NATIVE_METHOD(Posix, ftruncate, "(Ljava/io/FileDescriptor;J)V"), 1989 NATIVE_METHOD(Posix, gai_strerror, "(I)Ljava/lang/String;"), 1990 NATIVE_METHOD(Posix, getegid, "()I"), 1991 NATIVE_METHOD(Posix, geteuid, "()I"), 1992 NATIVE_METHOD(Posix, getgid, "()I"), 1993 NATIVE_METHOD(Posix, getenv, "(Ljava/lang/String;)Ljava/lang/String;"), 1994 NATIVE_METHOD(Posix, getnameinfo, "(Ljava/net/InetAddress;I)Ljava/lang/String;"), 1995 NATIVE_METHOD(Posix, getpeername, "(Ljava/io/FileDescriptor;)Ljava/net/SocketAddress;"), 1996 NATIVE_METHOD(Posix, getpgid, "(I)I"), 1997 NATIVE_METHOD(Posix, getpid, "()I"), 1998 NATIVE_METHOD(Posix, getppid, "()I"), 1999 NATIVE_METHOD(Posix, getpwnam, "(Ljava/lang/String;)Landroid/system/StructPasswd;"), 2000 NATIVE_METHOD(Posix, getpwuid, "(I)Landroid/system/StructPasswd;"), 2001 NATIVE_METHOD(Posix, getsockname, "(Ljava/io/FileDescriptor;)Ljava/net/SocketAddress;"), 2002 NATIVE_METHOD(Posix, getsockoptByte, "(Ljava/io/FileDescriptor;II)I"), 2003 NATIVE_METHOD(Posix, getsockoptInAddr, "(Ljava/io/FileDescriptor;II)Ljava/net/InetAddress;"), 2004 NATIVE_METHOD(Posix, getsockoptInt, "(Ljava/io/FileDescriptor;II)I"), 2005 NATIVE_METHOD(Posix, getsockoptLinger, "(Ljava/io/FileDescriptor;II)Landroid/system/StructLinger;"), 2006 NATIVE_METHOD(Posix, getsockoptTimeval, "(Ljava/io/FileDescriptor;II)Landroid/system/StructTimeval;"), 2007 NATIVE_METHOD(Posix, getsockoptUcred, "(Ljava/io/FileDescriptor;II)Landroid/system/StructUcred;"), 2008 NATIVE_METHOD(Posix, gettid, "()I"), 2009 NATIVE_METHOD(Posix, getuid, "()I"), 2010 NATIVE_METHOD(Posix, getxattr, "(Ljava/lang/String;Ljava/lang/String;[B)I"), 2011 NATIVE_METHOD(Posix, if_indextoname, "(I)Ljava/lang/String;"), 2012 NATIVE_METHOD(Posix, inet_pton, "(ILjava/lang/String;)Ljava/net/InetAddress;"), 2013 NATIVE_METHOD(Posix, ioctlInetAddress, "(Ljava/io/FileDescriptor;ILjava/lang/String;)Ljava/net/InetAddress;"), 2014 NATIVE_METHOD(Posix, ioctlInt, "(Ljava/io/FileDescriptor;ILandroid/util/MutableInt;)I"), 2015 NATIVE_METHOD(Posix, isatty, "(Ljava/io/FileDescriptor;)Z"), 2016 NATIVE_METHOD(Posix, kill, "(II)V"), 2017 NATIVE_METHOD(Posix, lchown, "(Ljava/lang/String;II)V"), 2018 NATIVE_METHOD(Posix, link, "(Ljava/lang/String;Ljava/lang/String;)V"), 2019 NATIVE_METHOD(Posix, listen, "(Ljava/io/FileDescriptor;I)V"), 2020 NATIVE_METHOD(Posix, lseek, "(Ljava/io/FileDescriptor;JI)J"), 2021 NATIVE_METHOD(Posix, lstat, "(Ljava/lang/String;)Landroid/system/StructStat;"), 2022 NATIVE_METHOD(Posix, mincore, "(JJ[B)V"), 2023 NATIVE_METHOD(Posix, mkdir, "(Ljava/lang/String;I)V"), 2024 NATIVE_METHOD(Posix, mkfifo, "(Ljava/lang/String;I)V"), 2025 NATIVE_METHOD(Posix, mlock, "(JJ)V"), 2026 NATIVE_METHOD(Posix, mmap, "(JJIILjava/io/FileDescriptor;J)J"), 2027 NATIVE_METHOD(Posix, msync, "(JJI)V"), 2028 NATIVE_METHOD(Posix, munlock, "(JJ)V"), 2029 NATIVE_METHOD(Posix, munmap, "(JJ)V"), 2030 NATIVE_METHOD(Posix, open, "(Ljava/lang/String;II)Ljava/io/FileDescriptor;"), 2031 NATIVE_METHOD(Posix, pipe2, "(I)[Ljava/io/FileDescriptor;"), 2032 NATIVE_METHOD(Posix, poll, "([Landroid/system/StructPollfd;I)I"), 2033 NATIVE_METHOD(Posix, posix_fallocate, "(Ljava/io/FileDescriptor;JJ)V"), 2034 NATIVE_METHOD(Posix, prctl, "(IJJJJ)I"), 2035 NATIVE_METHOD(Posix, preadBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIJ)I"), 2036 NATIVE_METHOD(Posix, pwriteBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIJ)I"), 2037 NATIVE_METHOD(Posix, readBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"), 2038 NATIVE_METHOD(Posix, readlink, "(Ljava/lang/String;)Ljava/lang/String;"), 2039 NATIVE_METHOD(Posix, realpath, "(Ljava/lang/String;)Ljava/lang/String;"), 2040 NATIVE_METHOD(Posix, readv, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"), 2041 NATIVE_METHOD(Posix, recvfromBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIILjava/net/InetSocketAddress;)I"), 2042 NATIVE_METHOD(Posix, remove, "(Ljava/lang/String;)V"), 2043 NATIVE_METHOD(Posix, removexattr, "(Ljava/lang/String;Ljava/lang/String;)V"), 2044 NATIVE_METHOD(Posix, rename, "(Ljava/lang/String;Ljava/lang/String;)V"), 2045 NATIVE_METHOD(Posix, sendfile, "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Landroid/util/MutableLong;J)J"), 2046 NATIVE_METHOD(Posix, sendtoBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIILjava/net/InetAddress;I)I"), 2047 NATIVE_METHOD_OVERLOAD(Posix, sendtoBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIILjava/net/SocketAddress;)I", SocketAddress), 2048 NATIVE_METHOD(Posix, setegid, "(I)V"), 2049 NATIVE_METHOD(Posix, setenv, "(Ljava/lang/String;Ljava/lang/String;Z)V"), 2050 NATIVE_METHOD(Posix, seteuid, "(I)V"), 2051 NATIVE_METHOD(Posix, setgid, "(I)V"), 2052 NATIVE_METHOD(Posix, setpgid, "(II)V"), 2053 NATIVE_METHOD(Posix, setregid, "(II)V"), 2054 NATIVE_METHOD(Posix, setreuid, "(II)V"), 2055 NATIVE_METHOD(Posix, setsid, "()I"), 2056 NATIVE_METHOD(Posix, setsockoptByte, "(Ljava/io/FileDescriptor;III)V"), 2057 NATIVE_METHOD(Posix, setsockoptIfreq, "(Ljava/io/FileDescriptor;IILjava/lang/String;)V"), 2058 NATIVE_METHOD(Posix, setsockoptInt, "(Ljava/io/FileDescriptor;III)V"), 2059 NATIVE_METHOD(Posix, setsockoptIpMreqn, "(Ljava/io/FileDescriptor;III)V"), 2060 NATIVE_METHOD(Posix, setsockoptGroupReq, "(Ljava/io/FileDescriptor;IILandroid/system/StructGroupReq;)V"), 2061 NATIVE_METHOD(Posix, setsockoptGroupSourceReq, "(Ljava/io/FileDescriptor;IILandroid/system/StructGroupSourceReq;)V"), 2062 NATIVE_METHOD(Posix, setsockoptLinger, "(Ljava/io/FileDescriptor;IILandroid/system/StructLinger;)V"), 2063 NATIVE_METHOD(Posix, setsockoptTimeval, "(Ljava/io/FileDescriptor;IILandroid/system/StructTimeval;)V"), 2064 NATIVE_METHOD(Posix, setuid, "(I)V"), 2065 NATIVE_METHOD(Posix, setxattr, "(Ljava/lang/String;Ljava/lang/String;[BI)V"), 2066 NATIVE_METHOD(Posix, shutdown, "(Ljava/io/FileDescriptor;I)V"), 2067 NATIVE_METHOD(Posix, socket, "(III)Ljava/io/FileDescriptor;"), 2068 NATIVE_METHOD(Posix, socketpair, "(IIILjava/io/FileDescriptor;Ljava/io/FileDescriptor;)V"), 2069 NATIVE_METHOD(Posix, stat, "(Ljava/lang/String;)Landroid/system/StructStat;"), 2070 NATIVE_METHOD(Posix, statvfs, "(Ljava/lang/String;)Landroid/system/StructStatVfs;"), 2071 NATIVE_METHOD(Posix, strerror, "(I)Ljava/lang/String;"), 2072 NATIVE_METHOD(Posix, strsignal, "(I)Ljava/lang/String;"), 2073 NATIVE_METHOD(Posix, symlink, "(Ljava/lang/String;Ljava/lang/String;)V"), 2074 NATIVE_METHOD(Posix, sysconf, "(I)J"), 2075 NATIVE_METHOD(Posix, tcdrain, "(Ljava/io/FileDescriptor;)V"), 2076 NATIVE_METHOD(Posix, tcsendbreak, "(Ljava/io/FileDescriptor;I)V"), 2077 NATIVE_METHOD(Posix, umaskImpl, "(I)I"), 2078 NATIVE_METHOD(Posix, uname, "()Landroid/system/StructUtsname;"), 2079 NATIVE_METHOD(Posix, unlink, "(Ljava/lang/String;)V"), 2080 NATIVE_METHOD(Posix, unsetenv, "(Ljava/lang/String;)V"), 2081 NATIVE_METHOD(Posix, waitpid, "(ILandroid/util/MutableInt;I)I"), 2082 NATIVE_METHOD(Posix, writeBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"), 2083 NATIVE_METHOD(Posix, writev, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"), 2084 }; 2085 void register_libcore_io_Posix(JNIEnv* env) { 2086 jniRegisterNativeMethods(env, "libcore/io/Posix", gMethods, NELEM(gMethods)); 2087 } 2088