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 "AsynchronousSocketCloseMonitor.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 "ScopedBytes.h" 28 #include "ScopedLocalRef.h" 29 #include "ScopedPrimitiveArray.h" 30 #include "ScopedUtfChars.h" 31 #include "StaticAssert.h" 32 #include "UniquePtr.h" 33 #include "toStringArray.h" 34 35 #include <arpa/inet.h> 36 #include <errno.h> 37 #include <fcntl.h> 38 #include <net/if.h> 39 #include <netdb.h> 40 #include <netinet/in.h> 41 #include <poll.h> 42 #include <pwd.h> 43 #include <signal.h> 44 #include <stdlib.h> 45 #include <sys/ioctl.h> 46 #include <sys/mman.h> 47 #include <sys/socket.h> 48 #include <sys/stat.h> 49 #include <sys/syscall.h> 50 #include <sys/time.h> 51 #include <sys/types.h> 52 #include <sys/uio.h> 53 #include <sys/utsname.h> 54 #include <sys/wait.h> 55 #include <termios.h> 56 #include <unistd.h> 57 58 #define TO_JAVA_STRING(NAME, EXP) \ 59 jstring NAME = env->NewStringUTF(EXP); \ 60 if (NAME == NULL) return NULL; 61 62 struct addrinfo_deleter { 63 void operator()(addrinfo* p) const { 64 if (p != NULL) { // bionic's freeaddrinfo(3) crashes when passed NULL. 65 freeaddrinfo(p); 66 } 67 } 68 }; 69 70 /** 71 * Used to retry networking system calls that can return EINTR. Unlike TEMP_FAILURE_RETRY, 72 * this also handles the case where the reason for failure is that another thread called 73 * Socket.close. This macro also throws exceptions on failure. 74 * 75 * Returns the result of 'exp', though a Java exception will be pending if the result is -1. 76 */ 77 #define NET_FAILURE_RETRY(jni_env, return_type, syscall_name, java_fd, ...) ({ \ 78 return_type _rc = -1; \ 79 do { \ 80 { \ 81 int _fd = jniGetFDFromFileDescriptor(jni_env, java_fd); \ 82 AsynchronousSocketCloseMonitor _monitor(_fd); \ 83 _rc = syscall_name(_fd, __VA_ARGS__); \ 84 } \ 85 if (_rc == -1) { \ 86 if (jniGetFDFromFileDescriptor(jni_env, java_fd) == -1) { \ 87 jniThrowException(jni_env, "java/net/SocketException", "Socket closed"); \ 88 break; \ 89 } else if (errno != EINTR) { \ 90 /* TODO: with a format string we could show the arguments too, like strace(1). */ \ 91 throwErrnoException(jni_env, # syscall_name); \ 92 break; \ 93 } \ 94 } \ 95 } while (_rc == -1); \ 96 _rc; }) 97 98 static void throwException(JNIEnv* env, jclass exceptionClass, jmethodID ctor3, jmethodID ctor2, 99 const char* functionName, int error) { 100 jthrowable cause = NULL; 101 if (env->ExceptionCheck()) { 102 cause = env->ExceptionOccurred(); 103 env->ExceptionClear(); 104 } 105 106 ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName)); 107 if (detailMessage.get() == NULL) { 108 // Not really much we can do here. We're probably dead in the water, 109 // but let's try to stumble on... 110 env->ExceptionClear(); 111 } 112 113 jobject exception; 114 if (cause != NULL) { 115 exception = env->NewObject(exceptionClass, ctor3, detailMessage.get(), error, cause); 116 } else { 117 exception = env->NewObject(exceptionClass, ctor2, detailMessage.get(), error); 118 } 119 env->Throw(reinterpret_cast<jthrowable>(exception)); 120 } 121 122 static void throwErrnoException(JNIEnv* env, const char* functionName) { 123 int error = errno; 124 static jmethodID ctor3 = env->GetMethodID(JniConstants::errnoExceptionClass, 125 "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V"); 126 static jmethodID ctor2 = env->GetMethodID(JniConstants::errnoExceptionClass, 127 "<init>", "(Ljava/lang/String;I)V"); 128 throwException(env, JniConstants::errnoExceptionClass, ctor3, ctor2, functionName, error); 129 } 130 131 static void throwGaiException(JNIEnv* env, const char* functionName, int error) { 132 // Cache the methods ids before we throw, so we don't call GetMethodID with a pending exception. 133 static jmethodID ctor3 = env->GetMethodID(JniConstants::gaiExceptionClass, "<init>", 134 "(Ljava/lang/String;ILjava/lang/Throwable;)V"); 135 static jmethodID ctor2 = env->GetMethodID(JniConstants::gaiExceptionClass, "<init>", 136 "(Ljava/lang/String;I)V"); 137 if (errno != 0) { 138 // EAI_SYSTEM should mean "look at errno instead", but both glibc and bionic seem to 139 // mess this up. In particular, if you don't have INTERNET permission, errno will be EACCES 140 // but you'll get EAI_NONAME or EAI_NODATA. So we want our GaiException to have a 141 // potentially-relevant ErrnoException as its cause even if error != EAI_SYSTEM. 142 // http://code.google.com/p/android/issues/detail?id=15722 143 throwErrnoException(env, functionName); 144 // Deliberately fall through to throw another exception... 145 } 146 throwException(env, JniConstants::gaiExceptionClass, ctor3, ctor2, functionName, error); 147 } 148 149 template <typename rc_t> 150 static rc_t throwIfMinusOne(JNIEnv* env, const char* name, rc_t rc) { 151 if (rc == rc_t(-1)) { 152 throwErrnoException(env, name); 153 } 154 return rc; 155 } 156 157 template <typename ScopedT> 158 class IoVec { 159 public: 160 IoVec(JNIEnv* env, size_t bufferCount) : mEnv(env), mBufferCount(bufferCount) { 161 } 162 163 bool init(jobjectArray javaBuffers, jintArray javaOffsets, jintArray javaByteCounts) { 164 // We can't delete our local references until after the I/O, so make sure we have room. 165 if (mEnv->PushLocalFrame(mBufferCount + 16) < 0) { 166 return false; 167 } 168 ScopedIntArrayRO offsets(mEnv, javaOffsets); 169 if (offsets.get() == NULL) { 170 return false; 171 } 172 ScopedIntArrayRO byteCounts(mEnv, javaByteCounts); 173 if (byteCounts.get() == NULL) { 174 return false; 175 } 176 // TODO: Linux actually has a 1024 buffer limit. glibc works around this, and we should too. 177 // TODO: you can query the limit at runtime with sysconf(_SC_IOV_MAX). 178 for (size_t i = 0; i < mBufferCount; ++i) { 179 jobject buffer = mEnv->GetObjectArrayElement(javaBuffers, i); // We keep this local ref. 180 mScopedBuffers.push_back(new ScopedT(mEnv, buffer)); 181 jbyte* ptr = const_cast<jbyte*>(mScopedBuffers.back()->get()); 182 if (ptr == NULL) { 183 return false; 184 } 185 struct iovec iov; 186 iov.iov_base = reinterpret_cast<void*>(ptr + offsets[i]); 187 iov.iov_len = byteCounts[i]; 188 mIoVec.push_back(iov); 189 } 190 return true; 191 } 192 193 ~IoVec() { 194 for (size_t i = 0; i < mScopedBuffers.size(); ++i) { 195 delete mScopedBuffers[i]; 196 } 197 mEnv->PopLocalFrame(NULL); 198 } 199 200 iovec* get() { 201 return &mIoVec[0]; 202 } 203 204 size_t size() { 205 return mBufferCount; 206 } 207 208 private: 209 JNIEnv* mEnv; 210 size_t mBufferCount; 211 std::vector<iovec> mIoVec; 212 std::vector<ScopedT*> mScopedBuffers; 213 }; 214 215 static jobject makeSocketAddress(JNIEnv* env, const sockaddr_storage& ss) { 216 jint port; 217 jobject inetAddress = sockaddrToInetAddress(env, ss, &port); 218 if (inetAddress == NULL) { 219 return NULL; 220 } 221 static jmethodID ctor = env->GetMethodID(JniConstants::inetSocketAddressClass, "<init>", 222 "(Ljava/net/InetAddress;I)V"); 223 return env->NewObject(JniConstants::inetSocketAddressClass, ctor, inetAddress, port); 224 } 225 226 static jobject makeStructPasswd(JNIEnv* env, const struct passwd& pw) { 227 TO_JAVA_STRING(pw_name, pw.pw_name); 228 TO_JAVA_STRING(pw_dir, pw.pw_dir); 229 TO_JAVA_STRING(pw_shell, pw.pw_shell); 230 static jmethodID ctor = env->GetMethodID(JniConstants::structPasswdClass, "<init>", 231 "(Ljava/lang/String;IILjava/lang/String;Ljava/lang/String;)V"); 232 return env->NewObject(JniConstants::structPasswdClass, ctor, 233 pw_name, static_cast<jint>(pw.pw_uid), static_cast<jint>(pw.pw_gid), pw_dir, pw_shell); 234 } 235 236 static jobject makeStructStat(JNIEnv* env, const struct stat& sb) { 237 static jmethodID ctor = env->GetMethodID(JniConstants::structStatClass, "<init>", 238 "(JJIJIIJJJJJJJ)V"); 239 return env->NewObject(JniConstants::structStatClass, ctor, 240 static_cast<jlong>(sb.st_dev), static_cast<jlong>(sb.st_ino), 241 static_cast<jint>(sb.st_mode), static_cast<jlong>(sb.st_nlink), 242 static_cast<jint>(sb.st_uid), static_cast<jint>(sb.st_gid), 243 static_cast<jlong>(sb.st_rdev), static_cast<jlong>(sb.st_size), 244 static_cast<jlong>(sb.st_atime), static_cast<jlong>(sb.st_mtime), 245 static_cast<jlong>(sb.st_ctime), static_cast<jlong>(sb.st_blksize), 246 static_cast<jlong>(sb.st_blocks)); 247 } 248 249 static jobject makeStructStatVfs(JNIEnv* env, const struct statvfs& sb) { 250 #if defined(__APPLE__) 251 // Mac OS has no f_namelen field in struct statfs. 252 jlong max_name_length = 255; // __DARWIN_MAXNAMLEN 253 #else 254 jlong max_name_length = static_cast<jlong>(sb.f_namemax); 255 #endif 256 257 static jmethodID ctor = env->GetMethodID(JniConstants::structStatVfsClass, "<init>", 258 "(JJJJJJJJJJJ)V"); 259 return env->NewObject(JniConstants::structStatVfsClass, ctor, 260 static_cast<jlong>(sb.f_bsize), 261 static_cast<jlong>(sb.f_frsize), 262 static_cast<jlong>(sb.f_blocks), 263 static_cast<jlong>(sb.f_bfree), 264 static_cast<jlong>(sb.f_bavail), 265 static_cast<jlong>(sb.f_files), 266 static_cast<jlong>(sb.f_ffree), 267 static_cast<jlong>(sb.f_favail), 268 static_cast<jlong>(sb.f_fsid), 269 static_cast<jlong>(sb.f_flag), 270 max_name_length); 271 } 272 273 static jobject makeStructLinger(JNIEnv* env, const struct linger& l) { 274 static jmethodID ctor = env->GetMethodID(JniConstants::structLingerClass, "<init>", "(II)V"); 275 return env->NewObject(JniConstants::structLingerClass, ctor, l.l_onoff, l.l_linger); 276 } 277 278 static jobject makeStructTimeval(JNIEnv* env, const struct timeval& tv) { 279 static jmethodID ctor = env->GetMethodID(JniConstants::structTimevalClass, "<init>", "(JJ)V"); 280 return env->NewObject(JniConstants::structTimevalClass, ctor, 281 static_cast<jlong>(tv.tv_sec), static_cast<jlong>(tv.tv_usec)); 282 } 283 284 static jobject makeStructUcred(JNIEnv* env, const struct ucred& u) { 285 static jmethodID ctor = env->GetMethodID(JniConstants::structUcredClass, "<init>", "(III)V"); 286 return env->NewObject(JniConstants::structUcredClass, ctor, u.pid, u.uid, u.gid); 287 } 288 289 static jobject makeStructUtsname(JNIEnv* env, const struct utsname& buf) { 290 TO_JAVA_STRING(sysname, buf.sysname); 291 TO_JAVA_STRING(nodename, buf.nodename); 292 TO_JAVA_STRING(release, buf.release); 293 TO_JAVA_STRING(version, buf.version); 294 TO_JAVA_STRING(machine, buf.machine); 295 static jmethodID ctor = env->GetMethodID(JniConstants::structUtsnameClass, "<init>", 296 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); 297 return env->NewObject(JniConstants::structUtsnameClass, ctor, 298 sysname, nodename, release, version, machine); 299 }; 300 301 static bool fillIfreq(JNIEnv* env, jstring javaInterfaceName, struct ifreq& req) { 302 ScopedUtfChars interfaceName(env, javaInterfaceName); 303 if (interfaceName.c_str() == NULL) { 304 return false; 305 } 306 memset(&req, 0, sizeof(req)); 307 strncpy(req.ifr_name, interfaceName.c_str(), sizeof(req.ifr_name)); 308 req.ifr_name[sizeof(req.ifr_name) - 1] = '\0'; 309 return true; 310 } 311 312 static bool fillInetSocketAddress(JNIEnv* env, jint rc, jobject javaInetSocketAddress, const sockaddr_storage& ss) { 313 if (rc == -1 || javaInetSocketAddress == NULL) { 314 return true; 315 } 316 // Fill out the passed-in InetSocketAddress with the sender's IP address and port number. 317 jint port; 318 jobject sender = sockaddrToInetAddress(env, ss, &port); 319 if (sender == NULL) { 320 return false; 321 } 322 static jfieldID addressFid = env->GetFieldID(JniConstants::inetSocketAddressClass, "addr", "Ljava/net/InetAddress;"); 323 static jfieldID portFid = env->GetFieldID(JniConstants::inetSocketAddressClass, "port", "I"); 324 env->SetObjectField(javaInetSocketAddress, addressFid, sender); 325 env->SetIntField(javaInetSocketAddress, portFid, port); 326 return true; 327 } 328 329 static jobject doStat(JNIEnv* env, jstring javaPath, bool isLstat) { 330 ScopedUtfChars path(env, javaPath); 331 if (path.c_str() == NULL) { 332 return NULL; 333 } 334 struct stat sb; 335 int rc = isLstat ? TEMP_FAILURE_RETRY(lstat(path.c_str(), &sb)) 336 : TEMP_FAILURE_RETRY(stat(path.c_str(), &sb)); 337 if (rc == -1) { 338 throwErrnoException(env, isLstat ? "lstat" : "stat"); 339 return NULL; 340 } 341 return makeStructStat(env, sb); 342 } 343 344 static jobject doGetSockName(JNIEnv* env, jobject javaFd, bool is_sockname) { 345 int fd = jniGetFDFromFileDescriptor(env, javaFd); 346 sockaddr_storage ss; 347 sockaddr* sa = reinterpret_cast<sockaddr*>(&ss); 348 socklen_t byteCount = sizeof(ss); 349 memset(&ss, 0, byteCount); 350 int rc = is_sockname ? TEMP_FAILURE_RETRY(getsockname(fd, sa, &byteCount)) 351 : TEMP_FAILURE_RETRY(getpeername(fd, sa, &byteCount)); 352 if (rc == -1) { 353 throwErrnoException(env, is_sockname ? "getsockname" : "getpeername"); 354 return NULL; 355 } 356 return makeSocketAddress(env, ss); 357 } 358 359 class Passwd { 360 public: 361 Passwd(JNIEnv* env) : mEnv(env), mResult(NULL) { 362 mBufferSize = sysconf(_SC_GETPW_R_SIZE_MAX); 363 mBuffer.reset(new char[mBufferSize]); 364 } 365 366 jobject getpwnam(const char* name) { 367 return process("getpwnam_r", getpwnam_r(name, &mPwd, mBuffer.get(), mBufferSize, &mResult)); 368 } 369 370 jobject getpwuid(uid_t uid) { 371 return process("getpwuid_r", getpwuid_r(uid, &mPwd, mBuffer.get(), mBufferSize, &mResult)); 372 } 373 374 struct passwd* get() { 375 return mResult; 376 } 377 378 private: 379 jobject process(const char* syscall, int error) { 380 if (mResult == NULL) { 381 errno = error; 382 throwErrnoException(mEnv, syscall); 383 return NULL; 384 } 385 return makeStructPasswd(mEnv, *mResult); 386 } 387 388 JNIEnv* mEnv; 389 UniquePtr<char[]> mBuffer; 390 size_t mBufferSize; 391 struct passwd mPwd; 392 struct passwd* mResult; 393 }; 394 395 static jobject Posix_accept(JNIEnv* env, jobject, jobject javaFd, jobject javaInetSocketAddress) { 396 sockaddr_storage ss; 397 socklen_t sl = sizeof(ss); 398 memset(&ss, 0, sizeof(ss)); 399 sockaddr* peer = (javaInetSocketAddress != NULL) ? reinterpret_cast<sockaddr*>(&ss) : NULL; 400 socklen_t* peerLength = (javaInetSocketAddress != NULL) ? &sl : 0; 401 jint clientFd = NET_FAILURE_RETRY(env, int, accept, javaFd, peer, peerLength); 402 if (clientFd == -1 || !fillInetSocketAddress(env, clientFd, javaInetSocketAddress, ss)) { 403 close(clientFd); 404 return NULL; 405 } 406 return (clientFd != -1) ? jniCreateFileDescriptor(env, clientFd) : NULL; 407 } 408 409 static jboolean Posix_access(JNIEnv* env, jobject, jstring javaPath, jint mode) { 410 ScopedUtfChars path(env, javaPath); 411 if (path.c_str() == NULL) { 412 return JNI_FALSE; 413 } 414 int rc = TEMP_FAILURE_RETRY(access(path.c_str(), mode)); 415 if (rc == -1) { 416 throwErrnoException(env, "access"); 417 } 418 return (rc == 0); 419 } 420 421 static void Posix_bind(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) { 422 sockaddr_storage ss; 423 socklen_t sa_len; 424 if (!inetAddressToSockaddr(env, javaAddress, port, ss, sa_len)) { 425 return; 426 } 427 const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss); 428 // We don't need the return value because we'll already have thrown. 429 (void) NET_FAILURE_RETRY(env, int, bind, javaFd, sa, sa_len); 430 } 431 432 static void Posix_chmod(JNIEnv* env, jobject, jstring javaPath, jint mode) { 433 ScopedUtfChars path(env, javaPath); 434 if (path.c_str() == NULL) { 435 return; 436 } 437 throwIfMinusOne(env, "chmod", TEMP_FAILURE_RETRY(chmod(path.c_str(), mode))); 438 } 439 440 static void Posix_chown(JNIEnv* env, jobject, jstring javaPath, jint uid, jint gid) { 441 ScopedUtfChars path(env, javaPath); 442 if (path.c_str() == NULL) { 443 return; 444 } 445 throwIfMinusOne(env, "chown", TEMP_FAILURE_RETRY(chown(path.c_str(), uid, gid))); 446 } 447 448 static void Posix_close(JNIEnv* env, jobject, jobject javaFd) { 449 // Get the FileDescriptor's 'fd' field and clear it. 450 // We need to do this before we can throw an IOException (http://b/3222087). 451 int fd = jniGetFDFromFileDescriptor(env, javaFd); 452 jniSetFileDescriptorOfFD(env, javaFd, -1); 453 454 // Even if close(2) fails with EINTR, the fd will have been closed. 455 // Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone else's fd. 456 // http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html 457 throwIfMinusOne(env, "close", close(fd)); 458 } 459 460 static void Posix_connect(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) { 461 sockaddr_storage ss; 462 socklen_t sa_len; 463 if (!inetAddressToSockaddr(env, javaAddress, port, ss, sa_len)) { 464 return; 465 } 466 const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss); 467 // We don't need the return value because we'll already have thrown. 468 (void) NET_FAILURE_RETRY(env, int, connect, javaFd, sa, sa_len); 469 } 470 471 static jobject Posix_dup(JNIEnv* env, jobject, jobject javaOldFd) { 472 int oldFd = jniGetFDFromFileDescriptor(env, javaOldFd); 473 int newFd = throwIfMinusOne(env, "dup", TEMP_FAILURE_RETRY(dup(oldFd))); 474 return (newFd != -1) ? jniCreateFileDescriptor(env, newFd) : NULL; 475 } 476 477 static jobject Posix_dup2(JNIEnv* env, jobject, jobject javaOldFd, jint newFd) { 478 int oldFd = jniGetFDFromFileDescriptor(env, javaOldFd); 479 int fd = throwIfMinusOne(env, "dup2", TEMP_FAILURE_RETRY(dup2(oldFd, newFd))); 480 return (fd != -1) ? jniCreateFileDescriptor(env, fd) : NULL; 481 } 482 483 static jobjectArray Posix_environ(JNIEnv* env, jobject) { 484 extern char** environ; // Standard, but not in any header file. 485 return toStringArray(env, environ); 486 } 487 488 static void Posix_execve(JNIEnv* env, jobject, jstring javaFilename, jobjectArray javaArgv, jobjectArray javaEnvp) { 489 ScopedUtfChars path(env, javaFilename); 490 if (path.c_str() == NULL) { 491 return; 492 } 493 494 ExecStrings argv(env, javaArgv); 495 ExecStrings envp(env, javaEnvp); 496 execve(path.c_str(), argv.get(), envp.get()); 497 498 throwErrnoException(env, "execve"); 499 } 500 501 static void Posix_execv(JNIEnv* env, jobject, jstring javaFilename, jobjectArray javaArgv) { 502 ScopedUtfChars path(env, javaFilename); 503 if (path.c_str() == NULL) { 504 return; 505 } 506 507 ExecStrings argv(env, javaArgv); 508 execv(path.c_str(), argv.get()); 509 510 throwErrnoException(env, "execv"); 511 } 512 513 static void Posix_fchmod(JNIEnv* env, jobject, jobject javaFd, jint mode) { 514 int fd = jniGetFDFromFileDescriptor(env, javaFd); 515 throwIfMinusOne(env, "fchmod", TEMP_FAILURE_RETRY(fchmod(fd, mode))); 516 } 517 518 static void Posix_fchown(JNIEnv* env, jobject, jobject javaFd, jint uid, jint gid) { 519 int fd = jniGetFDFromFileDescriptor(env, javaFd); 520 throwIfMinusOne(env, "fchown", TEMP_FAILURE_RETRY(fchown(fd, uid, gid))); 521 } 522 523 static jint Posix_fcntlVoid(JNIEnv* env, jobject, jobject javaFd, jint cmd) { 524 int fd = jniGetFDFromFileDescriptor(env, javaFd); 525 return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd))); 526 } 527 528 static jint Posix_fcntlLong(JNIEnv* env, jobject, jobject javaFd, jint cmd, jlong arg) { 529 int fd = jniGetFDFromFileDescriptor(env, javaFd); 530 return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, arg))); 531 } 532 533 static jint Posix_fcntlFlock(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaFlock) { 534 static jfieldID typeFid = env->GetFieldID(JniConstants::structFlockClass, "l_type", "S"); 535 static jfieldID whenceFid = env->GetFieldID(JniConstants::structFlockClass, "l_whence", "S"); 536 static jfieldID startFid = env->GetFieldID(JniConstants::structFlockClass, "l_start", "J"); 537 static jfieldID lenFid = env->GetFieldID(JniConstants::structFlockClass, "l_len", "J"); 538 static jfieldID pidFid = env->GetFieldID(JniConstants::structFlockClass, "l_pid", "I"); 539 540 struct flock64 lock; 541 memset(&lock, 0, sizeof(lock)); 542 lock.l_type = env->GetShortField(javaFlock, typeFid); 543 lock.l_whence = env->GetShortField(javaFlock, whenceFid); 544 lock.l_start = env->GetLongField(javaFlock, startFid); 545 lock.l_len = env->GetLongField(javaFlock, lenFid); 546 lock.l_pid = env->GetIntField(javaFlock, pidFid); 547 548 int fd = jniGetFDFromFileDescriptor(env, javaFd); 549 int rc = throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, &lock))); 550 if (rc != -1) { 551 env->SetShortField(javaFlock, typeFid, lock.l_type); 552 env->SetShortField(javaFlock, whenceFid, lock.l_whence); 553 env->SetLongField(javaFlock, startFid, lock.l_start); 554 env->SetLongField(javaFlock, lenFid, lock.l_len); 555 env->SetIntField(javaFlock, pidFid, lock.l_pid); 556 } 557 return rc; 558 } 559 560 static void Posix_fdatasync(JNIEnv* env, jobject, jobject javaFd) { 561 int fd = jniGetFDFromFileDescriptor(env, javaFd); 562 throwIfMinusOne(env, "fdatasync", TEMP_FAILURE_RETRY(fdatasync(fd))); 563 } 564 565 static jobject Posix_fstat(JNIEnv* env, jobject, jobject javaFd) { 566 int fd = jniGetFDFromFileDescriptor(env, javaFd); 567 struct stat sb; 568 int rc = TEMP_FAILURE_RETRY(fstat(fd, &sb)); 569 if (rc == -1) { 570 throwErrnoException(env, "fstat"); 571 return NULL; 572 } 573 return makeStructStat(env, sb); 574 } 575 576 static jobject Posix_fstatvfs(JNIEnv* env, jobject, jobject javaFd) { 577 int fd = jniGetFDFromFileDescriptor(env, javaFd); 578 struct statvfs sb; 579 int rc = TEMP_FAILURE_RETRY(fstatvfs(fd, &sb)); 580 if (rc == -1) { 581 throwErrnoException(env, "fstatvfs"); 582 return NULL; 583 } 584 return makeStructStatVfs(env, sb); 585 } 586 587 static void Posix_fsync(JNIEnv* env, jobject, jobject javaFd) { 588 int fd = jniGetFDFromFileDescriptor(env, javaFd); 589 throwIfMinusOne(env, "fsync", TEMP_FAILURE_RETRY(fsync(fd))); 590 } 591 592 static void Posix_ftruncate(JNIEnv* env, jobject, jobject javaFd, jlong length) { 593 int fd = jniGetFDFromFileDescriptor(env, javaFd); 594 throwIfMinusOne(env, "ftruncate", TEMP_FAILURE_RETRY(ftruncate64(fd, length))); 595 } 596 597 static jstring Posix_gai_strerror(JNIEnv* env, jobject, jint error) { 598 return env->NewStringUTF(gai_strerror(error)); 599 } 600 601 static jobjectArray Posix_getaddrinfo(JNIEnv* env, jobject, jstring javaNode, jobject javaHints) { 602 ScopedUtfChars node(env, javaNode); 603 if (node.c_str() == NULL) { 604 return NULL; 605 } 606 607 static jfieldID flagsFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_flags", "I"); 608 static jfieldID familyFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_family", "I"); 609 static jfieldID socktypeFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_socktype", "I"); 610 static jfieldID protocolFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_protocol", "I"); 611 612 addrinfo hints; 613 memset(&hints, 0, sizeof(hints)); 614 hints.ai_flags = env->GetIntField(javaHints, flagsFid); 615 hints.ai_family = env->GetIntField(javaHints, familyFid); 616 hints.ai_socktype = env->GetIntField(javaHints, socktypeFid); 617 hints.ai_protocol = env->GetIntField(javaHints, protocolFid); 618 619 addrinfo* addressList = NULL; 620 errno = 0; 621 int rc = getaddrinfo(node.c_str(), NULL, &hints, &addressList); 622 UniquePtr<addrinfo, addrinfo_deleter> addressListDeleter(addressList); 623 if (rc != 0) { 624 throwGaiException(env, "getaddrinfo", rc); 625 return NULL; 626 } 627 628 // Count results so we know how to size the output array. 629 int addressCount = 0; 630 for (addrinfo* ai = addressList; ai != NULL; ai = ai->ai_next) { 631 if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) { 632 ++addressCount; 633 } else { 634 ALOGE("getaddrinfo unexpected ai_family %i", ai->ai_family); 635 } 636 } 637 if (addressCount == 0) { 638 return NULL; 639 } 640 641 // Prepare output array. 642 jobjectArray result = env->NewObjectArray(addressCount, JniConstants::inetAddressClass, NULL); 643 if (result == NULL) { 644 return NULL; 645 } 646 647 // Examine returned addresses one by one, save them in the output array. 648 int index = 0; 649 for (addrinfo* ai = addressList; ai != NULL; ai = ai->ai_next) { 650 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) { 651 // Unknown address family. Skip this address. 652 ALOGE("getaddrinfo unexpected ai_family %i", ai->ai_family); 653 continue; 654 } 655 656 // Convert each IP address into a Java byte array. 657 sockaddr_storage& address = *reinterpret_cast<sockaddr_storage*>(ai->ai_addr); 658 ScopedLocalRef<jobject> inetAddress(env, sockaddrToInetAddress(env, address, NULL)); 659 if (inetAddress.get() == NULL) { 660 return NULL; 661 } 662 env->SetObjectArrayElement(result, index, inetAddress.get()); 663 ++index; 664 } 665 return result; 666 } 667 668 static jint Posix_getegid(JNIEnv*, jobject) { 669 return getegid(); 670 } 671 672 static jint Posix_geteuid(JNIEnv*, jobject) { 673 return geteuid(); 674 } 675 676 static jint Posix_getgid(JNIEnv*, jobject) { 677 return getgid(); 678 } 679 680 static jstring Posix_getenv(JNIEnv* env, jobject, jstring javaName) { 681 ScopedUtfChars name(env, javaName); 682 if (name.c_str() == NULL) { 683 return NULL; 684 } 685 return env->NewStringUTF(getenv(name.c_str())); 686 } 687 688 static jstring Posix_getnameinfo(JNIEnv* env, jobject, jobject javaAddress, jint flags) { 689 sockaddr_storage ss; 690 socklen_t sa_len; 691 if (!inetAddressToSockaddrVerbatim(env, javaAddress, 0, ss, sa_len)) { 692 return NULL; 693 } 694 char buf[NI_MAXHOST]; // NI_MAXHOST is longer than INET6_ADDRSTRLEN. 695 errno = 0; 696 int rc = getnameinfo(reinterpret_cast<sockaddr*>(&ss), sa_len, buf, sizeof(buf), NULL, 0, flags); 697 if (rc != 0) { 698 throwGaiException(env, "getnameinfo", rc); 699 return NULL; 700 } 701 return env->NewStringUTF(buf); 702 } 703 704 static jobject Posix_getpeername(JNIEnv* env, jobject, jobject javaFd) { 705 return doGetSockName(env, javaFd, false); 706 } 707 708 static jint Posix_getpid(JNIEnv*, jobject) { 709 return getpid(); 710 } 711 712 static jint Posix_getppid(JNIEnv*, jobject) { 713 return getppid(); 714 } 715 716 static jobject Posix_getpwnam(JNIEnv* env, jobject, jstring javaName) { 717 ScopedUtfChars name(env, javaName); 718 if (name.c_str() == NULL) { 719 return NULL; 720 } 721 return Passwd(env).getpwnam(name.c_str()); 722 } 723 724 static jobject Posix_getpwuid(JNIEnv* env, jobject, jint uid) { 725 return Passwd(env).getpwuid(uid); 726 } 727 728 static jobject Posix_getsockname(JNIEnv* env, jobject, jobject javaFd) { 729 return doGetSockName(env, javaFd, true); 730 } 731 732 static jint Posix_getsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 733 int fd = jniGetFDFromFileDescriptor(env, javaFd); 734 u_char result = 0; 735 socklen_t size = sizeof(result); 736 throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size))); 737 return result; 738 } 739 740 static jobject Posix_getsockoptInAddr(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 741 int fd = jniGetFDFromFileDescriptor(env, javaFd); 742 sockaddr_storage ss; 743 memset(&ss, 0, sizeof(ss)); 744 ss.ss_family = AF_INET; // This is only for the IPv4-only IP_MULTICAST_IF. 745 sockaddr_in* sa = reinterpret_cast<sockaddr_in*>(&ss); 746 socklen_t size = sizeof(sa->sin_addr); 747 int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &sa->sin_addr, &size)); 748 if (rc == -1) { 749 throwErrnoException(env, "getsockopt"); 750 return NULL; 751 } 752 return sockaddrToInetAddress(env, ss, NULL); 753 } 754 755 static jint Posix_getsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 756 int fd = jniGetFDFromFileDescriptor(env, javaFd); 757 jint result = 0; 758 socklen_t size = sizeof(result); 759 throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size))); 760 return result; 761 } 762 763 static jobject Posix_getsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 764 int fd = jniGetFDFromFileDescriptor(env, javaFd); 765 struct linger l; 766 socklen_t size = sizeof(l); 767 memset(&l, 0, size); 768 int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &l, &size)); 769 if (rc == -1) { 770 throwErrnoException(env, "getsockopt"); 771 return NULL; 772 } 773 return makeStructLinger(env, l); 774 } 775 776 static jobject Posix_getsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 777 int fd = jniGetFDFromFileDescriptor(env, javaFd); 778 struct timeval tv; 779 socklen_t size = sizeof(tv); 780 memset(&tv, 0, size); 781 int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &tv, &size)); 782 if (rc == -1) { 783 throwErrnoException(env, "getsockopt"); 784 return NULL; 785 } 786 return makeStructTimeval(env, tv); 787 } 788 789 static jobject Posix_getsockoptUcred(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 790 int fd = jniGetFDFromFileDescriptor(env, javaFd); 791 struct ucred u; 792 socklen_t size = sizeof(u); 793 memset(&u, 0, size); 794 int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &u, &size)); 795 if (rc == -1) { 796 throwErrnoException(env, "getsockopt"); 797 return NULL; 798 } 799 return makeStructUcred(env, u); 800 } 801 802 static jint Posix_gettid(JNIEnv*, jobject) { 803 // Neither bionic nor glibc exposes gettid(2). 804 return syscall(__NR_gettid); 805 } 806 807 static jint Posix_getuid(JNIEnv*, jobject) { 808 return getuid(); 809 } 810 811 static jstring Posix_if_indextoname(JNIEnv* env, jobject, jint index) { 812 char buf[IF_NAMESIZE]; 813 char* name = if_indextoname(index, buf); 814 // if_indextoname(3) returns NULL on failure, which will come out of NewStringUTF unscathed. 815 // There's no useful information in errno, so we don't bother throwing. Callers can null-check. 816 return env->NewStringUTF(name); 817 } 818 819 static jobject Posix_inet_pton(JNIEnv* env, jobject, jint family, jstring javaName) { 820 ScopedUtfChars name(env, javaName); 821 if (name.c_str() == NULL) { 822 return NULL; 823 } 824 sockaddr_storage ss; 825 memset(&ss, 0, sizeof(ss)); 826 // sockaddr_in and sockaddr_in6 are at the same address, so we can use either here. 827 void* dst = &reinterpret_cast<sockaddr_in*>(&ss)->sin_addr; 828 if (inet_pton(family, name.c_str(), dst) != 1) { 829 return NULL; 830 } 831 ss.ss_family = family; 832 return sockaddrToInetAddress(env, ss, NULL); 833 } 834 835 static jobject Posix_ioctlInetAddress(JNIEnv* env, jobject, jobject javaFd, jint cmd, jstring javaInterfaceName) { 836 struct ifreq req; 837 if (!fillIfreq(env, javaInterfaceName, req)) { 838 return NULL; 839 } 840 int fd = jniGetFDFromFileDescriptor(env, javaFd); 841 int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &req))); 842 if (rc == -1) { 843 return NULL; 844 } 845 return sockaddrToInetAddress(env, reinterpret_cast<sockaddr_storage&>(req.ifr_addr), NULL); 846 } 847 848 static jint Posix_ioctlInt(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaArg) { 849 // This is complicated because ioctls may return their result by updating their argument 850 // or via their return value, so we need to support both. 851 int fd = jniGetFDFromFileDescriptor(env, javaFd); 852 static jfieldID valueFid = env->GetFieldID(JniConstants::mutableIntClass, "value", "I"); 853 jint arg = env->GetIntField(javaArg, valueFid); 854 int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &arg))); 855 if (!env->ExceptionCheck()) { 856 env->SetIntField(javaArg, valueFid, arg); 857 } 858 return rc; 859 } 860 861 static jboolean Posix_isatty(JNIEnv* env, jobject, jobject javaFd) { 862 int fd = jniGetFDFromFileDescriptor(env, javaFd); 863 return TEMP_FAILURE_RETRY(isatty(fd)) == 1; 864 } 865 866 static void Posix_kill(JNIEnv* env, jobject, jint pid, jint sig) { 867 throwIfMinusOne(env, "kill", TEMP_FAILURE_RETRY(kill(pid, sig))); 868 } 869 870 static void Posix_lchown(JNIEnv* env, jobject, jstring javaPath, jint uid, jint gid) { 871 ScopedUtfChars path(env, javaPath); 872 if (path.c_str() == NULL) { 873 return; 874 } 875 throwIfMinusOne(env, "lchown", TEMP_FAILURE_RETRY(lchown(path.c_str(), uid, gid))); 876 } 877 878 static void Posix_listen(JNIEnv* env, jobject, jobject javaFd, jint backlog) { 879 int fd = jniGetFDFromFileDescriptor(env, javaFd); 880 throwIfMinusOne(env, "listen", TEMP_FAILURE_RETRY(listen(fd, backlog))); 881 } 882 883 static jlong Posix_lseek(JNIEnv* env, jobject, jobject javaFd, jlong offset, jint whence) { 884 int fd = jniGetFDFromFileDescriptor(env, javaFd); 885 return throwIfMinusOne(env, "lseek", TEMP_FAILURE_RETRY(lseek64(fd, offset, whence))); 886 } 887 888 static jobject Posix_lstat(JNIEnv* env, jobject, jstring javaPath) { 889 return doStat(env, javaPath, true); 890 } 891 892 static void Posix_mincore(JNIEnv* env, jobject, jlong address, jlong byteCount, jbyteArray javaVector) { 893 ScopedByteArrayRW vector(env, javaVector); 894 if (vector.get() == NULL) { 895 return; 896 } 897 void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 898 unsigned char* vec = reinterpret_cast<unsigned char*>(vector.get()); 899 throwIfMinusOne(env, "mincore", TEMP_FAILURE_RETRY(mincore(ptr, byteCount, vec))); 900 } 901 902 static void Posix_mkdir(JNIEnv* env, jobject, jstring javaPath, jint mode) { 903 ScopedUtfChars path(env, javaPath); 904 if (path.c_str() == NULL) { 905 return; 906 } 907 throwIfMinusOne(env, "mkdir", TEMP_FAILURE_RETRY(mkdir(path.c_str(), mode))); 908 } 909 910 static void Posix_mlock(JNIEnv* env, jobject, jlong address, jlong byteCount) { 911 void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 912 throwIfMinusOne(env, "mlock", TEMP_FAILURE_RETRY(mlock(ptr, byteCount))); 913 } 914 915 static jlong Posix_mmap(JNIEnv* env, jobject, jlong address, jlong byteCount, jint prot, jint flags, jobject javaFd, jlong offset) { 916 int fd = jniGetFDFromFileDescriptor(env, javaFd); 917 void* suggestedPtr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 918 void* ptr = mmap(suggestedPtr, byteCount, prot, flags, fd, offset); 919 if (ptr == MAP_FAILED) { 920 throwErrnoException(env, "mmap"); 921 } 922 return static_cast<jlong>(reinterpret_cast<uintptr_t>(ptr)); 923 } 924 925 static void Posix_msync(JNIEnv* env, jobject, jlong address, jlong byteCount, jint flags) { 926 void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 927 throwIfMinusOne(env, "msync", TEMP_FAILURE_RETRY(msync(ptr, byteCount, flags))); 928 } 929 930 static void Posix_munlock(JNIEnv* env, jobject, jlong address, jlong byteCount) { 931 void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 932 throwIfMinusOne(env, "munlock", TEMP_FAILURE_RETRY(munlock(ptr, byteCount))); 933 } 934 935 static void Posix_munmap(JNIEnv* env, jobject, jlong address, jlong byteCount) { 936 void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address)); 937 throwIfMinusOne(env, "munmap", TEMP_FAILURE_RETRY(munmap(ptr, byteCount))); 938 } 939 940 static jobject Posix_open(JNIEnv* env, jobject, jstring javaPath, jint flags, jint mode) { 941 ScopedUtfChars path(env, javaPath); 942 if (path.c_str() == NULL) { 943 return NULL; 944 } 945 int fd = throwIfMinusOne(env, "open", TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode))); 946 return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL; 947 } 948 949 static jobjectArray Posix_pipe(JNIEnv* env, jobject) { 950 int fds[2]; 951 throwIfMinusOne(env, "pipe", TEMP_FAILURE_RETRY(pipe(&fds[0]))); 952 jobjectArray result = env->NewObjectArray(2, JniConstants::fileDescriptorClass, NULL); 953 if (result == NULL) { 954 return NULL; 955 } 956 for (int i = 0; i < 2; ++i) { 957 ScopedLocalRef<jobject> fd(env, jniCreateFileDescriptor(env, fds[i])); 958 if (fd.get() == NULL) { 959 return NULL; 960 } 961 env->SetObjectArrayElement(result, i, fd.get()); 962 if (env->ExceptionCheck()) { 963 return NULL; 964 } 965 } 966 return result; 967 } 968 969 static jint Posix_poll(JNIEnv* env, jobject, jobjectArray javaStructs, jint timeoutMs) { 970 static jfieldID fdFid = env->GetFieldID(JniConstants::structPollfdClass, "fd", "Ljava/io/FileDescriptor;"); 971 static jfieldID eventsFid = env->GetFieldID(JniConstants::structPollfdClass, "events", "S"); 972 static jfieldID reventsFid = env->GetFieldID(JniConstants::structPollfdClass, "revents", "S"); 973 974 // Turn the Java libcore.io.StructPollfd[] into a C++ struct pollfd[]. 975 size_t arrayLength = env->GetArrayLength(javaStructs); 976 UniquePtr<struct pollfd[]> fds(new struct pollfd[arrayLength]); 977 memset(fds.get(), 0, sizeof(struct pollfd) * arrayLength); 978 size_t count = 0; // Some trailing array elements may be irrelevant. (See below.) 979 for (size_t i = 0; i < arrayLength; ++i) { 980 ScopedLocalRef<jobject> javaStruct(env, env->GetObjectArrayElement(javaStructs, i)); 981 if (javaStruct.get() == NULL) { 982 break; // We allow trailing nulls in the array for caller convenience. 983 } 984 ScopedLocalRef<jobject> javaFd(env, env->GetObjectField(javaStruct.get(), fdFid)); 985 if (javaFd.get() == NULL) { 986 break; // We also allow callers to just clear the fd field (this is what Selector does). 987 } 988 fds[count].fd = jniGetFDFromFileDescriptor(env, javaFd.get()); 989 fds[count].events = env->GetShortField(javaStruct.get(), eventsFid); 990 ++count; 991 } 992 993 // Since we don't know which fds -- if any -- are sockets, be conservative and register 994 // all fds for asynchronous socket close monitoring. 995 std::vector<AsynchronousSocketCloseMonitor*> monitors; 996 for (size_t i = 0; i < count; ++i) { 997 monitors.push_back(new AsynchronousSocketCloseMonitor(fds[i].fd)); 998 } 999 int rc = poll(fds.get(), count, timeoutMs); 1000 for (size_t i = 0; i < monitors.size(); ++i) { 1001 delete monitors[i]; 1002 } 1003 if (rc == -1) { 1004 throwErrnoException(env, "poll"); 1005 return -1; 1006 } 1007 1008 // Update the revents fields in the Java libcore.io.StructPollfd[]. 1009 for (size_t i = 0; i < count; ++i) { 1010 ScopedLocalRef<jobject> javaStruct(env, env->GetObjectArrayElement(javaStructs, i)); 1011 if (javaStruct.get() == NULL) { 1012 return -1; 1013 } 1014 env->SetShortField(javaStruct.get(), reventsFid, fds[i].revents); 1015 } 1016 return rc; 1017 } 1018 1019 static jint Posix_preadBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount, jlong offset) { 1020 ScopedBytesRW bytes(env, javaBytes); 1021 if (bytes.get() == NULL) { 1022 return -1; 1023 } 1024 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1025 return throwIfMinusOne(env, "pread", TEMP_FAILURE_RETRY(pread64(fd, bytes.get() + byteOffset, byteCount, offset))); 1026 } 1027 1028 static jint Posix_pwriteBytes(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount, jlong offset) { 1029 ScopedBytesRO bytes(env, javaBytes); 1030 if (bytes.get() == NULL) { 1031 return -1; 1032 } 1033 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1034 return throwIfMinusOne(env, "pwrite", TEMP_FAILURE_RETRY(pwrite64(fd, bytes.get() + byteOffset, byteCount, offset))); 1035 } 1036 1037 static jint Posix_readBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount) { 1038 ScopedBytesRW bytes(env, javaBytes); 1039 if (bytes.get() == NULL) { 1040 return -1; 1041 } 1042 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1043 return throwIfMinusOne(env, "read", TEMP_FAILURE_RETRY(read(fd, bytes.get() + byteOffset, byteCount))); 1044 } 1045 1046 static jint Posix_readv(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) { 1047 IoVec<ScopedBytesRW> ioVec(env, env->GetArrayLength(buffers)); 1048 if (!ioVec.init(buffers, offsets, byteCounts)) { 1049 return -1; 1050 } 1051 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1052 return throwIfMinusOne(env, "readv", TEMP_FAILURE_RETRY(readv(fd, ioVec.get(), ioVec.size()))); 1053 } 1054 1055 static jint Posix_recvfromBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount, jint flags, jobject javaInetSocketAddress) { 1056 ScopedBytesRW bytes(env, javaBytes); 1057 if (bytes.get() == NULL) { 1058 return -1; 1059 } 1060 sockaddr_storage ss; 1061 socklen_t sl = sizeof(ss); 1062 memset(&ss, 0, sizeof(ss)); 1063 sockaddr* from = (javaInetSocketAddress != NULL) ? reinterpret_cast<sockaddr*>(&ss) : NULL; 1064 socklen_t* fromLength = (javaInetSocketAddress != NULL) ? &sl : 0; 1065 jint recvCount = NET_FAILURE_RETRY(env, ssize_t, recvfrom, javaFd, bytes.get() + byteOffset, byteCount, flags, from, fromLength); 1066 fillInetSocketAddress(env, recvCount, javaInetSocketAddress, ss); 1067 return recvCount; 1068 } 1069 1070 static void Posix_remove(JNIEnv* env, jobject, jstring javaPath) { 1071 ScopedUtfChars path(env, javaPath); 1072 if (path.c_str() == NULL) { 1073 return; 1074 } 1075 throwIfMinusOne(env, "remove", TEMP_FAILURE_RETRY(remove(path.c_str()))); 1076 } 1077 1078 static void Posix_rename(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) { 1079 ScopedUtfChars oldPath(env, javaOldPath); 1080 if (oldPath.c_str() == NULL) { 1081 return; 1082 } 1083 ScopedUtfChars newPath(env, javaNewPath); 1084 if (newPath.c_str() == NULL) { 1085 return; 1086 } 1087 throwIfMinusOne(env, "rename", TEMP_FAILURE_RETRY(rename(oldPath.c_str(), newPath.c_str()))); 1088 } 1089 1090 static jlong Posix_sendfile(JNIEnv* env, jobject, jobject javaOutFd, jobject javaInFd, jobject javaOffset, jlong byteCount) { 1091 int outFd = jniGetFDFromFileDescriptor(env, javaOutFd); 1092 int inFd = jniGetFDFromFileDescriptor(env, javaInFd); 1093 static jfieldID valueFid = env->GetFieldID(JniConstants::mutableLongClass, "value", "J"); 1094 off_t offset = 0; 1095 off_t* offsetPtr = NULL; 1096 if (javaOffset != NULL) { 1097 // TODO: fix bionic so we can have a 64-bit off_t! 1098 offset = env->GetLongField(javaOffset, valueFid); 1099 offsetPtr = &offset; 1100 } 1101 jlong result = throwIfMinusOne(env, "sendfile", TEMP_FAILURE_RETRY(sendfile(outFd, inFd, offsetPtr, byteCount))); 1102 if (javaOffset != NULL) { 1103 env->SetLongField(javaOffset, valueFid, offset); 1104 } 1105 return result; 1106 } 1107 1108 static jint Posix_sendtoBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount, jint flags, jobject javaInetAddress, jint port) { 1109 ScopedBytesRO bytes(env, javaBytes); 1110 if (bytes.get() == NULL) { 1111 return -1; 1112 } 1113 sockaddr_storage ss; 1114 socklen_t sa_len = 0; 1115 if (javaInetAddress != NULL && !inetAddressToSockaddr(env, javaInetAddress, port, ss, sa_len)) { 1116 return -1; 1117 } 1118 const sockaddr* to = (javaInetAddress != NULL) ? reinterpret_cast<const sockaddr*>(&ss) : NULL; 1119 return NET_FAILURE_RETRY(env, ssize_t, sendto, javaFd, bytes.get() + byteOffset, byteCount, flags, to, sa_len); 1120 } 1121 1122 static void Posix_setegid(JNIEnv* env, jobject, jint egid) { 1123 throwIfMinusOne(env, "setegid", TEMP_FAILURE_RETRY(setegid(egid))); 1124 } 1125 1126 static void Posix_setenv(JNIEnv* env, jobject, jstring javaName, jstring javaValue, jboolean overwrite) { 1127 ScopedUtfChars name(env, javaName); 1128 if (name.c_str() == NULL) { 1129 return; 1130 } 1131 ScopedUtfChars value(env, javaValue); 1132 if (value.c_str() == NULL) { 1133 return; 1134 } 1135 throwIfMinusOne(env, "setenv", setenv(name.c_str(), value.c_str(), overwrite)); 1136 } 1137 1138 static void Posix_seteuid(JNIEnv* env, jobject, jint euid) { 1139 throwIfMinusOne(env, "seteuid", TEMP_FAILURE_RETRY(seteuid(euid))); 1140 } 1141 1142 static void Posix_setgid(JNIEnv* env, jobject, jint gid) { 1143 throwIfMinusOne(env, "setgid", TEMP_FAILURE_RETRY(setgid(gid))); 1144 } 1145 1146 static jint Posix_setsid(JNIEnv* env, jobject) { 1147 return throwIfMinusOne(env, "setsid", TEMP_FAILURE_RETRY(setsid())); 1148 } 1149 1150 static void Posix_setsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 1151 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1152 u_char byte = value; 1153 throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &byte, sizeof(byte)))); 1154 } 1155 1156 static void Posix_setsockoptIfreq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jstring javaInterfaceName) { 1157 struct ifreq req; 1158 if (!fillIfreq(env, javaInterfaceName, req)) { 1159 return; 1160 } 1161 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1162 throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req)))); 1163 } 1164 1165 static void Posix_setsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 1166 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1167 throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 1168 } 1169 1170 #if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED < 1070 1171 // Mac OS didn't support modern multicast APIs until 10.7. 1172 static void Posix_setsockoptIpMreqn(JNIEnv*, jobject, jobject, jint, jint, jint) { abort(); } 1173 static void Posix_setsockoptGroupReq(JNIEnv*, jobject, jobject, jint, jint, jobject) { abort(); } 1174 #else 1175 static void Posix_setsockoptIpMreqn(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jint value) { 1176 ip_mreqn req; 1177 memset(&req, 0, sizeof(req)); 1178 req.imr_ifindex = value; 1179 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1180 throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req)))); 1181 } 1182 1183 static void Posix_setsockoptGroupReq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaGroupReq) { 1184 struct group_req req; 1185 memset(&req, 0, sizeof(req)); 1186 1187 static jfieldID grInterfaceFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_interface", "I"); 1188 req.gr_interface = env->GetIntField(javaGroupReq, grInterfaceFid); 1189 // Get the IPv4 or IPv6 multicast address to join or leave. 1190 static jfieldID grGroupFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_group", "Ljava/net/InetAddress;"); 1191 ScopedLocalRef<jobject> javaGroup(env, env->GetObjectField(javaGroupReq, grGroupFid)); 1192 socklen_t sa_len; 1193 if (!inetAddressToSockaddrVerbatim(env, javaGroup.get(), 0, req.gr_group, sa_len)) { 1194 return; 1195 } 1196 1197 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1198 int rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req))); 1199 if (rc == -1 && errno == EINVAL) { 1200 // Maybe we're a 32-bit binary talking to a 64-bit kernel? 1201 // glibc doesn't automatically handle this. 1202 struct group_req64 { 1203 uint32_t gr_interface; 1204 uint32_t my_padding; 1205 sockaddr_storage gr_group; 1206 }; 1207 group_req64 req64; 1208 req64.gr_interface = req.gr_interface; 1209 memcpy(&req64.gr_group, &req.gr_group, sizeof(req.gr_group)); 1210 rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req64, sizeof(req64))); 1211 } 1212 throwIfMinusOne(env, "setsockopt", rc); 1213 } 1214 #endif 1215 1216 static void Posix_setsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaLinger) { 1217 static jfieldID lOnoffFid = env->GetFieldID(JniConstants::structLingerClass, "l_onoff", "I"); 1218 static jfieldID lLingerFid = env->GetFieldID(JniConstants::structLingerClass, "l_linger", "I"); 1219 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1220 struct linger value; 1221 value.l_onoff = env->GetIntField(javaLinger, lOnoffFid); 1222 value.l_linger = env->GetIntField(javaLinger, lLingerFid); 1223 throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 1224 } 1225 1226 static void Posix_setsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaTimeval) { 1227 static jfieldID tvSecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_sec", "J"); 1228 static jfieldID tvUsecFid = env->GetFieldID(JniConstants::structTimevalClass, "tv_usec", "J"); 1229 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1230 struct timeval value; 1231 value.tv_sec = env->GetLongField(javaTimeval, tvSecFid); 1232 value.tv_usec = env->GetLongField(javaTimeval, tvUsecFid); 1233 throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value)))); 1234 } 1235 1236 static void Posix_setuid(JNIEnv* env, jobject, jint uid) { 1237 throwIfMinusOne(env, "setuid", TEMP_FAILURE_RETRY(setuid(uid))); 1238 } 1239 1240 static void Posix_shutdown(JNIEnv* env, jobject, jobject javaFd, jint how) { 1241 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1242 throwIfMinusOne(env, "shutdown", TEMP_FAILURE_RETRY(shutdown(fd, how))); 1243 } 1244 1245 static jobject Posix_socket(JNIEnv* env, jobject, jint domain, jint type, jint protocol) { 1246 int fd = throwIfMinusOne(env, "socket", TEMP_FAILURE_RETRY(socket(domain, type, protocol))); 1247 return fd != -1 ? jniCreateFileDescriptor(env, fd) : NULL; 1248 } 1249 1250 static void Posix_socketpair(JNIEnv* env, jobject, jint domain, jint type, jint protocol, jobject javaFd1, jobject javaFd2) { 1251 int fds[2]; 1252 int rc = throwIfMinusOne(env, "socketpair", TEMP_FAILURE_RETRY(socketpair(domain, type, protocol, fds))); 1253 if (rc != -1) { 1254 jniSetFileDescriptorOfFD(env, javaFd1, fds[0]); 1255 jniSetFileDescriptorOfFD(env, javaFd2, fds[1]); 1256 } 1257 } 1258 1259 static jobject Posix_stat(JNIEnv* env, jobject, jstring javaPath) { 1260 return doStat(env, javaPath, false); 1261 } 1262 1263 static jobject Posix_statvfs(JNIEnv* env, jobject, jstring javaPath) { 1264 ScopedUtfChars path(env, javaPath); 1265 if (path.c_str() == NULL) { 1266 return NULL; 1267 } 1268 struct statvfs sb; 1269 int rc = TEMP_FAILURE_RETRY(statvfs(path.c_str(), &sb)); 1270 if (rc == -1) { 1271 throwErrnoException(env, "statvfs"); 1272 return NULL; 1273 } 1274 return makeStructStatVfs(env, sb); 1275 } 1276 1277 static jstring Posix_strerror(JNIEnv* env, jobject, jint errnum) { 1278 char buffer[BUFSIZ]; 1279 const char* message = jniStrError(errnum, buffer, sizeof(buffer)); 1280 return env->NewStringUTF(message); 1281 } 1282 1283 static jstring Posix_strsignal(JNIEnv* env, jobject, jint signal) { 1284 return env->NewStringUTF(strsignal(signal)); 1285 } 1286 1287 static void Posix_symlink(JNIEnv* env, jobject, jstring javaOldPath, jstring javaNewPath) { 1288 ScopedUtfChars oldPath(env, javaOldPath); 1289 if (oldPath.c_str() == NULL) { 1290 return; 1291 } 1292 ScopedUtfChars newPath(env, javaNewPath); 1293 if (newPath.c_str() == NULL) { 1294 return; 1295 } 1296 throwIfMinusOne(env, "symlink", TEMP_FAILURE_RETRY(symlink(oldPath.c_str(), newPath.c_str()))); 1297 } 1298 1299 static jlong Posix_sysconf(JNIEnv* env, jobject, jint name) { 1300 // Since -1 is a valid result from sysconf(3), detecting failure is a little more awkward. 1301 errno = 0; 1302 long result = sysconf(name); 1303 if (result == -1L && errno == EINVAL) { 1304 throwErrnoException(env, "sysconf"); 1305 } 1306 return result; 1307 } 1308 1309 static void Posix_tcdrain(JNIEnv* env, jobject, jobject javaFd) { 1310 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1311 throwIfMinusOne(env, "tcdrain", TEMP_FAILURE_RETRY(tcdrain(fd))); 1312 } 1313 1314 static void Posix_tcsendbreak(JNIEnv* env, jobject, jobject javaFd, jint duration) { 1315 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1316 throwIfMinusOne(env, "tcsendbreak", TEMP_FAILURE_RETRY(tcsendbreak(fd, duration))); 1317 } 1318 1319 static jint Posix_umaskImpl(JNIEnv*, jobject, jint mask) { 1320 return umask(mask); 1321 } 1322 1323 static jobject Posix_uname(JNIEnv* env, jobject) { 1324 struct utsname buf; 1325 if (TEMP_FAILURE_RETRY(uname(&buf)) == -1) { 1326 return NULL; // Can't happen. 1327 } 1328 return makeStructUtsname(env, buf); 1329 } 1330 1331 static void Posix_unsetenv(JNIEnv* env, jobject, jstring javaName) { 1332 ScopedUtfChars name(env, javaName); 1333 if (name.c_str() == NULL) { 1334 return; 1335 } 1336 throwIfMinusOne(env, "unsetenv", unsetenv(name.c_str())); 1337 } 1338 1339 static jint Posix_waitpid(JNIEnv* env, jobject, jint pid, jobject javaStatus, jint options) { 1340 int status; 1341 int rc = throwIfMinusOne(env, "waitpid", TEMP_FAILURE_RETRY(waitpid(pid, &status, options))); 1342 if (rc != -1) { 1343 static jfieldID valueFid = env->GetFieldID(JniConstants::mutableIntClass, "value", "I"); 1344 env->SetIntField(javaStatus, valueFid, status); 1345 } 1346 return rc; 1347 } 1348 1349 static jint Posix_writeBytes(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount) { 1350 ScopedBytesRO bytes(env, javaBytes); 1351 if (bytes.get() == NULL) { 1352 return -1; 1353 } 1354 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1355 return throwIfMinusOne(env, "write", TEMP_FAILURE_RETRY(write(fd, bytes.get() + byteOffset, byteCount))); 1356 } 1357 1358 static jint Posix_writev(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) { 1359 IoVec<ScopedBytesRO> ioVec(env, env->GetArrayLength(buffers)); 1360 if (!ioVec.init(buffers, offsets, byteCounts)) { 1361 return -1; 1362 } 1363 int fd = jniGetFDFromFileDescriptor(env, javaFd); 1364 return throwIfMinusOne(env, "writev", TEMP_FAILURE_RETRY(writev(fd, ioVec.get(), ioVec.size()))); 1365 } 1366 1367 static JNINativeMethod gMethods[] = { 1368 NATIVE_METHOD(Posix, accept, "(Ljava/io/FileDescriptor;Ljava/net/InetSocketAddress;)Ljava/io/FileDescriptor;"), 1369 NATIVE_METHOD(Posix, access, "(Ljava/lang/String;I)Z"), 1370 NATIVE_METHOD(Posix, bind, "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)V"), 1371 NATIVE_METHOD(Posix, chmod, "(Ljava/lang/String;I)V"), 1372 NATIVE_METHOD(Posix, chown, "(Ljava/lang/String;II)V"), 1373 NATIVE_METHOD(Posix, close, "(Ljava/io/FileDescriptor;)V"), 1374 NATIVE_METHOD(Posix, connect, "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)V"), 1375 NATIVE_METHOD(Posix, dup, "(Ljava/io/FileDescriptor;)Ljava/io/FileDescriptor;"), 1376 NATIVE_METHOD(Posix, dup2, "(Ljava/io/FileDescriptor;I)Ljava/io/FileDescriptor;"), 1377 NATIVE_METHOD(Posix, environ, "()[Ljava/lang/String;"), 1378 NATIVE_METHOD(Posix, execv, "(Ljava/lang/String;[Ljava/lang/String;)V"), 1379 NATIVE_METHOD(Posix, execve, "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)V"), 1380 NATIVE_METHOD(Posix, fchmod, "(Ljava/io/FileDescriptor;I)V"), 1381 NATIVE_METHOD(Posix, fchown, "(Ljava/io/FileDescriptor;II)V"), 1382 NATIVE_METHOD(Posix, fcntlVoid, "(Ljava/io/FileDescriptor;I)I"), 1383 NATIVE_METHOD(Posix, fcntlLong, "(Ljava/io/FileDescriptor;IJ)I"), 1384 NATIVE_METHOD(Posix, fcntlFlock, "(Ljava/io/FileDescriptor;ILlibcore/io/StructFlock;)I"), 1385 NATIVE_METHOD(Posix, fdatasync, "(Ljava/io/FileDescriptor;)V"), 1386 NATIVE_METHOD(Posix, fstat, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStat;"), 1387 NATIVE_METHOD(Posix, fstatvfs, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStatVfs;"), 1388 NATIVE_METHOD(Posix, fsync, "(Ljava/io/FileDescriptor;)V"), 1389 NATIVE_METHOD(Posix, ftruncate, "(Ljava/io/FileDescriptor;J)V"), 1390 NATIVE_METHOD(Posix, gai_strerror, "(I)Ljava/lang/String;"), 1391 NATIVE_METHOD(Posix, getaddrinfo, "(Ljava/lang/String;Llibcore/io/StructAddrinfo;)[Ljava/net/InetAddress;"), 1392 NATIVE_METHOD(Posix, getegid, "()I"), 1393 NATIVE_METHOD(Posix, geteuid, "()I"), 1394 NATIVE_METHOD(Posix, getgid, "()I"), 1395 NATIVE_METHOD(Posix, getenv, "(Ljava/lang/String;)Ljava/lang/String;"), 1396 NATIVE_METHOD(Posix, getnameinfo, "(Ljava/net/InetAddress;I)Ljava/lang/String;"), 1397 NATIVE_METHOD(Posix, getpeername, "(Ljava/io/FileDescriptor;)Ljava/net/SocketAddress;"), 1398 NATIVE_METHOD(Posix, getpid, "()I"), 1399 NATIVE_METHOD(Posix, getppid, "()I"), 1400 NATIVE_METHOD(Posix, getpwnam, "(Ljava/lang/String;)Llibcore/io/StructPasswd;"), 1401 NATIVE_METHOD(Posix, getpwuid, "(I)Llibcore/io/StructPasswd;"), 1402 NATIVE_METHOD(Posix, getsockname, "(Ljava/io/FileDescriptor;)Ljava/net/SocketAddress;"), 1403 NATIVE_METHOD(Posix, getsockoptByte, "(Ljava/io/FileDescriptor;II)I"), 1404 NATIVE_METHOD(Posix, getsockoptInAddr, "(Ljava/io/FileDescriptor;II)Ljava/net/InetAddress;"), 1405 NATIVE_METHOD(Posix, getsockoptInt, "(Ljava/io/FileDescriptor;II)I"), 1406 NATIVE_METHOD(Posix, getsockoptLinger, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructLinger;"), 1407 NATIVE_METHOD(Posix, getsockoptTimeval, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructTimeval;"), 1408 NATIVE_METHOD(Posix, getsockoptUcred, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructUcred;"), 1409 NATIVE_METHOD(Posix, gettid, "()I"), 1410 NATIVE_METHOD(Posix, getuid, "()I"), 1411 NATIVE_METHOD(Posix, if_indextoname, "(I)Ljava/lang/String;"), 1412 NATIVE_METHOD(Posix, inet_pton, "(ILjava/lang/String;)Ljava/net/InetAddress;"), 1413 NATIVE_METHOD(Posix, ioctlInetAddress, "(Ljava/io/FileDescriptor;ILjava/lang/String;)Ljava/net/InetAddress;"), 1414 NATIVE_METHOD(Posix, ioctlInt, "(Ljava/io/FileDescriptor;ILlibcore/util/MutableInt;)I"), 1415 NATIVE_METHOD(Posix, isatty, "(Ljava/io/FileDescriptor;)Z"), 1416 NATIVE_METHOD(Posix, kill, "(II)V"), 1417 NATIVE_METHOD(Posix, lchown, "(Ljava/lang/String;II)V"), 1418 NATIVE_METHOD(Posix, listen, "(Ljava/io/FileDescriptor;I)V"), 1419 NATIVE_METHOD(Posix, lseek, "(Ljava/io/FileDescriptor;JI)J"), 1420 NATIVE_METHOD(Posix, lstat, "(Ljava/lang/String;)Llibcore/io/StructStat;"), 1421 NATIVE_METHOD(Posix, mincore, "(JJ[B)V"), 1422 NATIVE_METHOD(Posix, mkdir, "(Ljava/lang/String;I)V"), 1423 NATIVE_METHOD(Posix, mlock, "(JJ)V"), 1424 NATIVE_METHOD(Posix, mmap, "(JJIILjava/io/FileDescriptor;J)J"), 1425 NATIVE_METHOD(Posix, msync, "(JJI)V"), 1426 NATIVE_METHOD(Posix, munlock, "(JJ)V"), 1427 NATIVE_METHOD(Posix, munmap, "(JJ)V"), 1428 NATIVE_METHOD(Posix, open, "(Ljava/lang/String;II)Ljava/io/FileDescriptor;"), 1429 NATIVE_METHOD(Posix, pipe, "()[Ljava/io/FileDescriptor;"), 1430 NATIVE_METHOD(Posix, poll, "([Llibcore/io/StructPollfd;I)I"), 1431 NATIVE_METHOD(Posix, preadBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIJ)I"), 1432 NATIVE_METHOD(Posix, pwriteBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIJ)I"), 1433 NATIVE_METHOD(Posix, readBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"), 1434 NATIVE_METHOD(Posix, readv, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"), 1435 NATIVE_METHOD(Posix, recvfromBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIILjava/net/InetSocketAddress;)I"), 1436 NATIVE_METHOD(Posix, remove, "(Ljava/lang/String;)V"), 1437 NATIVE_METHOD(Posix, rename, "(Ljava/lang/String;Ljava/lang/String;)V"), 1438 NATIVE_METHOD(Posix, sendfile, "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Llibcore/util/MutableLong;J)J"), 1439 NATIVE_METHOD(Posix, sendtoBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIILjava/net/InetAddress;I)I"), 1440 NATIVE_METHOD(Posix, setegid, "(I)V"), 1441 NATIVE_METHOD(Posix, setenv, "(Ljava/lang/String;Ljava/lang/String;Z)V"), 1442 NATIVE_METHOD(Posix, seteuid, "(I)V"), 1443 NATIVE_METHOD(Posix, setgid, "(I)V"), 1444 NATIVE_METHOD(Posix, setsid, "()I"), 1445 NATIVE_METHOD(Posix, setsockoptByte, "(Ljava/io/FileDescriptor;III)V"), 1446 NATIVE_METHOD(Posix, setsockoptIfreq, "(Ljava/io/FileDescriptor;IILjava/lang/String;)V"), 1447 NATIVE_METHOD(Posix, setsockoptInt, "(Ljava/io/FileDescriptor;III)V"), 1448 NATIVE_METHOD(Posix, setsockoptIpMreqn, "(Ljava/io/FileDescriptor;III)V"), 1449 NATIVE_METHOD(Posix, setsockoptGroupReq, "(Ljava/io/FileDescriptor;IILlibcore/io/StructGroupReq;)V"), 1450 NATIVE_METHOD(Posix, setsockoptLinger, "(Ljava/io/FileDescriptor;IILlibcore/io/StructLinger;)V"), 1451 NATIVE_METHOD(Posix, setsockoptTimeval, "(Ljava/io/FileDescriptor;IILlibcore/io/StructTimeval;)V"), 1452 NATIVE_METHOD(Posix, setuid, "(I)V"), 1453 NATIVE_METHOD(Posix, shutdown, "(Ljava/io/FileDescriptor;I)V"), 1454 NATIVE_METHOD(Posix, socket, "(III)Ljava/io/FileDescriptor;"), 1455 NATIVE_METHOD(Posix, socketpair, "(IIILjava/io/FileDescriptor;Ljava/io/FileDescriptor;)V"), 1456 NATIVE_METHOD(Posix, stat, "(Ljava/lang/String;)Llibcore/io/StructStat;"), 1457 NATIVE_METHOD(Posix, statvfs, "(Ljava/lang/String;)Llibcore/io/StructStatVfs;"), 1458 NATIVE_METHOD(Posix, strerror, "(I)Ljava/lang/String;"), 1459 NATIVE_METHOD(Posix, strsignal, "(I)Ljava/lang/String;"), 1460 NATIVE_METHOD(Posix, symlink, "(Ljava/lang/String;Ljava/lang/String;)V"), 1461 NATIVE_METHOD(Posix, sysconf, "(I)J"), 1462 NATIVE_METHOD(Posix, tcdrain, "(Ljava/io/FileDescriptor;)V"), 1463 NATIVE_METHOD(Posix, tcsendbreak, "(Ljava/io/FileDescriptor;I)V"), 1464 NATIVE_METHOD(Posix, umaskImpl, "(I)I"), 1465 NATIVE_METHOD(Posix, uname, "()Llibcore/io/StructUtsname;"), 1466 NATIVE_METHOD(Posix, unsetenv, "(Ljava/lang/String;)V"), 1467 NATIVE_METHOD(Posix, waitpid, "(ILlibcore/util/MutableInt;I)I"), 1468 NATIVE_METHOD(Posix, writeBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"), 1469 NATIVE_METHOD(Posix, writev, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"), 1470 }; 1471 void register_libcore_io_Posix(JNIEnv* env) { 1472 jniRegisterNativeMethods(env, "libcore/io/Posix", gMethods, NELEM(gMethods)); 1473 } 1474