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 "JNIHelp.h" 21 #include "JniConstants.h" 22 #include "JniException.h" 23 #include "NetworkUtilities.h" 24 #include "Portability.h" 25 #include "ScopedBytes.h" 26 #include "ScopedLocalRef.h" 27 #include "ScopedPrimitiveArray.h" 28 #include "ScopedUtfChars.h" 29 #include "StaticAssert.h" 30 #include "UniquePtr.h" 31 #include "toStringArray.h" 32 33 #include <arpa/inet.h> 34 #include <errno.h> 35 #include <fcntl.h> 36 #include <net/if.h> 37 #include <netdb.h> 38 #include <netinet/in.h> 39 #include <netinet/in.h> 40 #include <poll.h> 41 #include <pwd.h> 42 #include <signal.h> 43 #include <stdlib.h> 44 #include <sys/ioctl.h> 45 #include <sys/mman.h> 46 #include <sys/socket.h> 47 #include <sys/socket.h> 48 #include <sys/stat.h> 49 #include <sys/time.h> 50 #include <sys/types.h> 51 #include <sys/uio.h> 52 #include <sys/utsname.h> 53 #include <sys/wait.h> 54 #include <termios.h> 55 #include <unistd.h> 56 57 #define TO_JAVA_STRING(NAME, EXP) \ 58 jstring NAME = env->NewStringUTF(EXP); \ 59 if (NAME == NULL) return NULL; 60 61 struct addrinfo_deleter { 62 void operator()(addrinfo* p) const { 63 if (p != NULL) { // bionic's freeaddrinfo(3) crashes when passed NULL. 64 freeaddrinfo(p); 65 } 66 } 67 }; 68 69 /** 70 * Used to retry networking system calls that can return EINTR. Unlike TEMP_FAILURE_RETRY, 71 * this also handles the case where the reason for failure is that another thread called 72 * Socket.close. This macro also throws exceptions on failure. 73 * 74 * Returns the result of 'exp', though a Java exception will be pending if the result is -1. 75 */ 76 #define NET_FAILURE_RETRY(jni_env, return_type, syscall_name, java_fd, ...) ({ \ 77 return_type _rc = -1; \ 78 do { \ 79 { \ 80 int _fd = jniGetFDFromFileDescriptor(jni_env, java_fd); \ 81 AsynchronousSocketCloseMonitor _monitor(_fd); \ 82 _rc = syscall_name(_fd, __VA_ARGS__); \ 83 } \ 84 if (_rc == -1) { \ 85 if (jniGetFDFromFileDescriptor(jni_env, java_fd) == -1) { \ 86 jniThrowException(jni_env, "java/net/SocketException", "Socket closed"); \ 87 break; \ 88 } else if (errno != EINTR) { \ 89 /* TODO: with a format string we could show the arguments too, like strace(1). */ \ 90 throwErrnoException(jni_env, # syscall_name); \ 91 break; \ 92 } \ 93 } \ 94 } while (_rc == -1); \ 95 _rc; }) 96 97 static void throwException(JNIEnv* env, jclass exceptionClass, jmethodID ctor3, jmethodID ctor2, 98 const char* functionName, int error) { 99 jthrowable cause = NULL; 100 if (env->ExceptionCheck()) { 101 cause = env->ExceptionOccurred(); 102 env->ExceptionClear(); 103 } 104 105 ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName)); 106 if (detailMessage.get() == NULL) { 107 // Not really much we can do here. We're probably dead in the water, 108 // but let's try to stumble on... 109 env->ExceptionClear(); 110 } 111 112 jobject exception; 113 if (cause != NULL) { 114 exception = env->NewObject(exceptionClass, ctor3, detailMessage.get(), error, cause); 115 } else { 116 exception = env->NewObject(exceptionClass, ctor2, detailMessage.get(), error); 117 } 118 env->Throw(reinterpret_cast<jthrowable>(exception)); 119 } 120 121 static void throwErrnoException(JNIEnv* env, const char* functionName) { 122 int error = errno; 123 static jmethodID ctor3 = env->GetMethodID(JniConstants::errnoExceptionClass, 124 "<init>", "(Ljava/lang/String;ILjava/lang/Throwable;)V"); 125 static jmethodID ctor2 = env->GetMethodID(JniConstants::errnoExceptionClass, 126 "<init>", "(Ljava/lang/String;I)V"); 127 throwException(env, JniConstants::errnoExceptionClass, ctor3, ctor2, functionName, error); 128 } 129 130 static void throwGaiException(JNIEnv* env, const char* functionName, int error) { 131 // Cache the methods ids before we throw, so we don't call GetMethodID with a pending exception. 132 static jmethodID ctor3 = env->GetMethodID(JniConstants::gaiExceptionClass, "<init>", 133 "(Ljava/lang/String;ILjava/lang/Throwable;)V"); 134 static jmethodID ctor2 = env->GetMethodID(JniConstants::gaiExceptionClass, "<init>", 135 "(Ljava/lang/String;I)V"); 136 if (errno != 0) { 137 // EAI_SYSTEM should mean "look at errno instead", but both glibc and bionic seem to 138 // mess this up. In particular, if you don't have INTERNET permission, errno will be EACCES 139 // but you'll get EAI_NONAME or EAI_NODATA. So we want our GaiException to have a 140 // potentially-relevant ErrnoException as its cause even if error != EAI_SYSTEM. 141 // http://code.google.com/p/android/issues/detail?id=15722 142 throwErrnoException(env, functionName); 143 // Deliberately fall through to throw another exception... 144 } 145 throwException(env, JniConstants::gaiExceptionClass, ctor3, ctor2, functionName, error); 146 } 147 148 template <typename rc_t> 149 static rc_t throwIfMinusOne(JNIEnv* env, const char* name, rc_t rc) { 150 if (rc == rc_t(-1)) { 151 throwErrnoException(env, name); 152 } 153 return rc; 154 } 155 156 template <typename ScopedT> 157 class IoVec { 158 public: 159 IoVec(JNIEnv* env, size_t bufferCount) : mEnv(env), mBufferCount(bufferCount) { 160 } 161 162 bool init(jobjectArray javaBuffers, jintArray javaOffsets, jintArray javaByteCounts) { 163 // We can't delete our local references until after the I/O, so make sure we have room. 164 if (mEnv->PushLocalFrame(mBufferCount + 16) < 0) { 165 return false; 166 } 167 ScopedIntArrayRO offsets(mEnv, javaOffsets); 168 if (offsets.get() == NULL) { 169 return false; 170 } 171 ScopedIntArrayRO byteCounts(mEnv, javaByteCounts); 172 if (byteCounts.get() == NULL) { 173 return false; 174 } 175 // TODO: Linux actually has a 1024 buffer limit. glibc works around this, and we should too. 176 // TODO: you can query the limit at runtime with sysconf(_SC_IOV_MAX). 177 for (size_t i = 0; i < mBufferCount; ++i) { 178 jobject buffer = mEnv->GetObjectArrayElement(javaBuffers, i); // We keep this local ref. 179 mScopedBuffers.push_back(new ScopedT(mEnv, buffer)); 180 jbyte* ptr = const_cast<jbyte*>(mScopedBuffers.back()->get()); 181 if (ptr == NULL) { 182 return false; 183 } 184 struct iovec iov; 185 iov.iov_base = reinterpret_cast<void*>(ptr + offsets[i]); 186 iov.iov_len = byteCounts[i]; 187 mIoVec.push_back(iov); 188 } 189 return true; 190 } 191 192 ~IoVec() { 193 for (size_t i = 0; i < mScopedBuffers.size(); ++i) { 194 delete mScopedBuffers[i]; 195 } 196 mEnv->PopLocalFrame(NULL); 197 } 198 199 iovec* get() { 200 return &mIoVec[0]; 201 } 202 203 size_t size() { 204 return mBufferCount; 205 } 206 207 private: 208 JNIEnv* mEnv; 209 size_t mBufferCount; 210 std::vector<iovec> mIoVec; 211 std::vector<ScopedT*> mScopedBuffers; 212 }; 213 214 static jobject makeSocketAddress(JNIEnv* env, const sockaddr_storage& ss) { 215 jint port; 216 jobject inetAddress = sockaddrToInetAddress(env, ss, &port); 217 if (inetAddress == NULL) { 218 return NULL; 219 } 220 static jmethodID ctor = env->GetMethodID(JniConstants::inetSocketAddressClass, "<init>", 221 "(Ljava/net/InetAddress;I)V"); 222 return env->NewObject(JniConstants::inetSocketAddressClass, ctor, inetAddress, port); 223 } 224 225 static jobject makeStructPasswd(JNIEnv* env, const struct passwd& pw) { 226 TO_JAVA_STRING(pw_name, pw.pw_name); 227 TO_JAVA_STRING(pw_dir, pw.pw_dir); 228 TO_JAVA_STRING(pw_shell, pw.pw_shell); 229 static jmethodID ctor = env->GetMethodID(JniConstants::structPasswdClass, "<init>", 230 "(Ljava/lang/String;IILjava/lang/String;Ljava/lang/String;)V"); 231 return env->NewObject(JniConstants::structPasswdClass, ctor, 232 pw_name, static_cast<jint>(pw.pw_uid), static_cast<jint>(pw.pw_gid), pw_dir, pw_shell); 233 } 234 235 static jobject makeStructStat(JNIEnv* env, const struct stat& sb) { 236 static jmethodID ctor = env->GetMethodID(JniConstants::structStatClass, "<init>", 237 "(JJIJIIJJJJJJJ)V"); 238 return env->NewObject(JniConstants::structStatClass, ctor, 239 static_cast<jlong>(sb.st_dev), static_cast<jlong>(sb.st_ino), 240 static_cast<jint>(sb.st_mode), static_cast<jlong>(sb.st_nlink), 241 static_cast<jint>(sb.st_uid), static_cast<jint>(sb.st_gid), 242 static_cast<jlong>(sb.st_rdev), static_cast<jlong>(sb.st_size), 243 static_cast<jlong>(sb.st_atime), static_cast<jlong>(sb.st_mtime), 244 static_cast<jlong>(sb.st_ctime), static_cast<jlong>(sb.st_blksize), 245 static_cast<jlong>(sb.st_blocks)); 246 } 247 248 static jobject makeStructStatFs(JNIEnv* env, const struct statfs& sb) { 249 #if defined(__APPLE__) 250 // Mac OS has no f_namelen field in struct statfs. 251 jlong max_name_length = 255; // __DARWIN_MAXNAMLEN 252 #else 253 // Until Mac OS 10.7, these were 32-bit fields. 254 STATIC_ASSERT(sizeof(sb.f_bavail) == sizeof(jlong), statfs_not_64_bit); 255 STATIC_ASSERT(sizeof(sb.f_bfree) == sizeof(jlong), statfs_not_64_bit); 256 STATIC_ASSERT(sizeof(sb.f_blocks) == sizeof(jlong), statfs_not_64_bit); 257 258 jlong max_name_length = static_cast<jlong>(sb.f_namelen); 259 #endif 260 261 static jmethodID ctor = env->GetMethodID(JniConstants::structStatFsClass, "<init>", 262 "(JJJJJJJJ)V"); 263 return env->NewObject(JniConstants::structStatFsClass, ctor, static_cast<jlong>(sb.f_bsize), 264 static_cast<jlong>(sb.f_blocks), static_cast<jlong>(sb.f_bfree), 265 static_cast<jlong>(sb.f_bavail), static_cast<jlong>(sb.f_files), 266 static_cast<jlong>(sb.f_ffree), max_name_length, 267 static_cast<jlong>(sb.f_frsize)); 268 } 269 270 static jobject makeStructLinger(JNIEnv* env, const struct linger& l) { 271 static jmethodID ctor = env->GetMethodID(JniConstants::structLingerClass, "<init>", "(II)V"); 272 return env->NewObject(JniConstants::structLingerClass, ctor, l.l_onoff, l.l_linger); 273 } 274 275 static jobject makeStructTimeval(JNIEnv* env, const struct timeval& tv) { 276 static jmethodID ctor = env->GetMethodID(JniConstants::structTimevalClass, "<init>", "(JJ)V"); 277 return env->NewObject(JniConstants::structTimevalClass, ctor, 278 static_cast<jlong>(tv.tv_sec), static_cast<jlong>(tv.tv_usec)); 279 } 280 281 static jobject makeStructUcred(JNIEnv* env, const struct ucred& u) { 282 static jmethodID ctor = env->GetMethodID(JniConstants::structUcredClass, "<init>", "(III)V"); 283 return env->NewObject(JniConstants::structUcredClass, ctor, u.pid, u.uid, u.gid); 284 } 285 286 static jobject makeStructUtsname(JNIEnv* env, const struct utsname& buf) { 287 TO_JAVA_STRING(sysname, buf.sysname); 288 TO_JAVA_STRING(nodename, buf.nodename); 289 TO_JAVA_STRING(release, buf.release); 290 TO_JAVA_STRING(version, buf.version); 291 TO_JAVA_STRING(machine, buf.machine); 292 static jmethodID ctor = env->GetMethodID(JniConstants::structUtsnameClass, "<init>", 293 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); 294 return env->NewObject(JniConstants::structUtsnameClass, ctor, 295 sysname, nodename, release, version, machine); 296 }; 297 298 static bool fillIfreq(JNIEnv* env, jstring javaInterfaceName, struct ifreq& req) { 299 ScopedUtfChars interfaceName(env, javaInterfaceName); 300 if (interfaceName.c_str() == NULL) { 301 return false; 302 } 303 memset(&req, 0, sizeof(req)); 304 strncpy(req.ifr_name, interfaceName.c_str(), sizeof(req.ifr_name)); 305 req.ifr_name[sizeof(req.ifr_name) - 1] = '\0'; 306 return true; 307 } 308 309 static bool fillInetSocketAddress(JNIEnv* env, jint rc, jobject javaInetSocketAddress, const sockaddr_storage& ss) { 310 if (rc == -1 || javaInetSocketAddress == NULL) { 311 return true; 312 } 313 // Fill out the passed-in InetSocketAddress with the sender's IP address and port number. 314 jint port; 315 jobject sender = sockaddrToInetAddress(env, ss, &port); 316 if (sender == NULL) { 317 return false; 318 } 319 static jfieldID addressFid = env->GetFieldID(JniConstants::inetSocketAddressClass, "addr", "Ljava/net/InetAddress;"); 320 static jfieldID portFid = env->GetFieldID(JniConstants::inetSocketAddressClass, "port", "I"); 321 env->SetObjectField(javaInetSocketAddress, addressFid, sender); 322 env->SetIntField(javaInetSocketAddress, portFid, port); 323 return true; 324 } 325 326 static jobject doStat(JNIEnv* env, jstring javaPath, bool isLstat) { 327 ScopedUtfChars path(env, javaPath); 328 if (path.c_str() == NULL) { 329 return NULL; 330 } 331 struct stat sb; 332 int rc = isLstat ? TEMP_FAILURE_RETRY(lstat(path.c_str(), &sb)) 333 : TEMP_FAILURE_RETRY(stat(path.c_str(), &sb)); 334 if (rc == -1) { 335 throwErrnoException(env, isLstat ? "lstat" : "stat"); 336 return NULL; 337 } 338 return makeStructStat(env, sb); 339 } 340 341 static jobject doGetSockName(JNIEnv* env, jobject javaFd, bool is_sockname) { 342 int fd = jniGetFDFromFileDescriptor(env, javaFd); 343 sockaddr_storage ss; 344 sockaddr* sa = reinterpret_cast<sockaddr*>(&ss); 345 socklen_t byteCount = sizeof(ss); 346 memset(&ss, 0, byteCount); 347 int rc = is_sockname ? TEMP_FAILURE_RETRY(getsockname(fd, sa, &byteCount)) 348 : TEMP_FAILURE_RETRY(getpeername(fd, sa, &byteCount)); 349 if (rc == -1) { 350 throwErrnoException(env, is_sockname ? "getsockname" : "getpeername"); 351 return NULL; 352 } 353 return makeSocketAddress(env, ss); 354 } 355 356 class Passwd { 357 public: 358 Passwd(JNIEnv* env) : mEnv(env), mResult(NULL) { 359 mBufferSize = sysconf(_SC_GETPW_R_SIZE_MAX); 360 if (mBufferSize == -1UL) { 361 // We're probably on bionic, where 1KiB should be enough for anyone. 362 // TODO: fix bionic to return 1024 like glibc. 363 mBufferSize = 1024; 364 } 365 mBuffer.reset(new char[mBufferSize]); 366 } 367 368 jobject getpwnam(const char* name) { 369 return process("getpwnam_r", getpwnam_r(name, &mPwd, mBuffer.get(), mBufferSize, &mResult)); 370 } 371 372 jobject getpwuid(uid_t uid) { 373 return process("getpwuid_r", getpwuid_r(uid, &mPwd, mBuffer.get(), mBufferSize, &mResult)); 374 } 375 376 struct passwd* get() { 377 return mResult; 378 } 379 380 private: 381 jobject process(const char* syscall, int error) { 382 if (mResult == NULL) { 383 errno = error; 384 throwErrnoException(mEnv, syscall); 385 return NULL; 386 } 387 return makeStructPasswd(mEnv, *mResult); 388 } 389 390 JNIEnv* mEnv; 391 UniquePtr<char[]> mBuffer; 392 size_t mBufferSize; 393 struct passwd mPwd; 394 struct passwd* mResult; 395 }; 396 397 static jobject Posix_accept(JNIEnv* env, jobject, jobject javaFd, jobject javaInetSocketAddress) { 398 sockaddr_storage ss; 399 socklen_t sl = sizeof(ss); 400 memset(&ss, 0, sizeof(ss)); 401 sockaddr* peer = (javaInetSocketAddress != NULL) ? reinterpret_cast<sockaddr*>(&ss) : NULL; 402 socklen_t* peerLength = (javaInetSocketAddress != NULL) ? &sl : 0; 403 jint clientFd = NET_FAILURE_RETRY(env, int, accept, javaFd, peer, peerLength); 404 if (clientFd == -1 || !fillInetSocketAddress(env, clientFd, javaInetSocketAddress, ss)) { 405 close(clientFd); 406 return NULL; 407 } 408 return (clientFd != -1) ? jniCreateFileDescriptor(env, clientFd) : NULL; 409 } 410 411 static jboolean Posix_access(JNIEnv* env, jobject, jstring javaPath, jint mode) { 412 ScopedUtfChars path(env, javaPath); 413 if (path.c_str() == NULL) { 414 return JNI_FALSE; 415 } 416 int rc = TEMP_FAILURE_RETRY(access(path.c_str(), mode)); 417 if (rc == -1) { 418 throwErrnoException(env, "access"); 419 } 420 return (rc == 0); 421 } 422 423 static void Posix_bind(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) { 424 sockaddr_storage ss; 425 socklen_t sa_len; 426 if (!inetAddressToSockaddr(env, javaAddress, port, ss, sa_len)) { 427 return; 428 } 429 const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss); 430 // We don't need the return value because we'll already have thrown. 431 (void) NET_FAILURE_RETRY(env, int, bind, javaFd, sa, sa_len); 432 } 433 434 static void Posix_chmod(JNIEnv* env, jobject, jstring javaPath, jint mode) { 435 ScopedUtfChars path(env, javaPath); 436 if (path.c_str() == NULL) { 437 return; 438 } 439 throwIfMinusOne(env, "chmod", TEMP_FAILURE_RETRY(chmod(path.c_str(), mode))); 440 } 441 442 static void Posix_chown(JNIEnv* env, jobject, jstring javaPath, jint uid, jint gid) { 443 ScopedUtfChars path(env, javaPath); 444 if (path.c_str() == NULL) { 445 return; 446 } 447 throwIfMinusOne(env, "chown", TEMP_FAILURE_RETRY(chown(path.c_str(), uid, gid))); 448 } 449 450 static void Posix_close(JNIEnv* env, jobject, jobject javaFd) { 451 // Get the FileDescriptor's 'fd' field and clear it. 452 // We need to do this before we can throw an IOException (http://b/3222087). 453 int fd = jniGetFDFromFileDescriptor(env, javaFd); 454 jniSetFileDescriptorOfFD(env, javaFd, -1); 455 456 // Even if close(2) fails with EINTR, the fd will have been closed. 457 // Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone else's fd. 458 // http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html 459 throwIfMinusOne(env, "close", close(fd)); 460 } 461 462 static void Posix_connect(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) { 463 sockaddr_storage ss; 464 socklen_t sa_len; 465 if (!inetAddressToSockaddr(env, javaAddress, port, ss, sa_len)) { 466 return; 467 } 468 const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss); 469 // We don't need the return value because we'll already have thrown. 470 (void) NET_FAILURE_RETRY(env, int, connect, javaFd, sa, sa_len); 471 } 472 473 static jobject Posix_dup(JNIEnv* env, jobject, jobject javaOldFd) { 474 int oldFd = jniGetFDFromFileDescriptor(env, javaOldFd); 475 int newFd = throwIfMinusOne(env, "dup", TEMP_FAILURE_RETRY(dup(oldFd))); 476 return (newFd != -1) ? jniCreateFileDescriptor(env, newFd) : NULL; 477 } 478 479 static jobject Posix_dup2(JNIEnv* env, jobject, jobject javaOldFd, jint newFd) { 480 int oldFd = jniGetFDFromFileDescriptor(env, javaOldFd); 481 int fd = throwIfMinusOne(env, "dup2", TEMP_FAILURE_RETRY(dup2(oldFd, newFd))); 482 return (fd != -1) ? jniCreateFileDescriptor(env, fd) : NULL; 483 } 484 485 static jobjectArray Posix_environ(JNIEnv* env, jobject) { 486 extern char** environ; // Standard, but not in any header file. 487 return toStringArray(env, environ); 488 } 489 490 static void Posix_execve(JNIEnv* env, jobject, jstring javaFilename, jobjectArray javaArgv, jobjectArray javaEnvp) { 491 ScopedUtfChars path(env, javaFilename); 492 if (path.c_str() == NULL) { 493 return; 494 } 495 496 char** argv = convertStrings(env, javaArgv); 497 char** envp = convertStrings(env, javaEnvp); 498 execve(path.c_str(), argv, envp); 499 500 freeStrings(env, javaArgv, argv); 501 freeStrings(env, javaEnvp, envp); 502 throwErrnoException(env, "execve"); 503 } 504 505 static void Posix_execv(JNIEnv* env, jobject, jstring javaFilename, jobjectArray javaArgv) { 506 ScopedUtfChars path(env, javaFilename); 507 if (path.c_str() == NULL) { 508 return; 509 } 510 511 char** argv = convertStrings(env, javaArgv); 512 execv(path.c_str(), argv); 513 514 freeStrings(env, javaArgv, argv); 515 throwErrnoException(env, "execv"); 516 } 517 518 static void Posix_fchmod(JNIEnv* env, jobject, jobject javaFd, jint mode) { 519 int fd = jniGetFDFromFileDescriptor(env, javaFd); 520 throwIfMinusOne(env, "fchmod", TEMP_FAILURE_RETRY(fchmod(fd, mode))); 521 } 522 523 static void Posix_fchown(JNIEnv* env, jobject, jobject javaFd, jint uid, jint gid) { 524 int fd = jniGetFDFromFileDescriptor(env, javaFd); 525 throwIfMinusOne(env, "fchown", TEMP_FAILURE_RETRY(fchown(fd, uid, gid))); 526 } 527 528 static jint Posix_fcntlVoid(JNIEnv* env, jobject, jobject javaFd, jint cmd) { 529 int fd = jniGetFDFromFileDescriptor(env, javaFd); 530 return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd))); 531 } 532 533 static jint Posix_fcntlLong(JNIEnv* env, jobject, jobject javaFd, jint cmd, jlong arg) { 534 int fd = jniGetFDFromFileDescriptor(env, javaFd); 535 return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, arg))); 536 } 537 538 static jint Posix_fcntlFlock(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaFlock) { 539 static jfieldID typeFid = env->GetFieldID(JniConstants::structFlockClass, "l_type", "S"); 540 static jfieldID whenceFid = env->GetFieldID(JniConstants::structFlockClass, "l_whence", "S"); 541 static jfieldID startFid = env->GetFieldID(JniConstants::structFlockClass, "l_start", "J"); 542 static jfieldID lenFid = env->GetFieldID(JniConstants::structFlockClass, "l_len", "J"); 543 static jfieldID pidFid = env->GetFieldID(JniConstants::structFlockClass, "l_pid", "I"); 544 545 struct flock64 lock; 546 memset(&lock, 0, sizeof(lock)); 547 lock.l_type = env->GetShortField(javaFlock, typeFid); 548 lock.l_whence = env->GetShortField(javaFlock, whenceFid); 549 lock.l_start = env->GetLongField(javaFlock, startFid); 550 lock.l_len = env->GetLongField(javaFlock, lenFid); 551 lock.l_pid = env->GetIntField(javaFlock, pidFid); 552 553 int fd = jniGetFDFromFileDescriptor(env, javaFd); 554 int rc = throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, &lock))); 555 if (rc != -1) { 556 env->SetShortField(javaFlock, typeFid, lock.l_type); 557 env->SetShortField(javaFlock, whenceFid, lock.l_whence); 558 env->SetLongField(javaFlock, startFid, lock.l_start); 559 env->SetLongField(javaFlock, lenFid, lock.l_len); 560 env->SetIntField(javaFlock, pidFid, lock.l_pid); 561 } 562 return rc; 563 } 564 565 static void Posix_fdatasync(JNIEnv* env, jobject, jobject javaFd) { 566 int fd = jniGetFDFromFileDescriptor(env, javaFd); 567 throwIfMinusOne(env, "fdatasync", TEMP_FAILURE_RETRY(fdatasync(fd))); 568 } 569 570 static jobject Posix_fstat(JNIEnv* env, jobject, jobject javaFd) { 571 int fd = jniGetFDFromFileDescriptor(env, javaFd); 572 struct stat sb; 573 int rc = TEMP_FAILURE_RETRY(fstat(fd, &sb)); 574 if (rc == -1) { 575 throwErrnoException(env, "fstat"); 576 return NULL; 577 } 578 return makeStructStat(env, sb); 579 } 580 581 static jobject Posix_fstatfs(JNIEnv* env, jobject, jobject javaFd) { 582 int fd = jniGetFDFromFileDescriptor(env, javaFd); 583 struct statfs sb; 584 int rc = TEMP_FAILURE_RETRY(fstatfs(fd, &sb)); 585 if (rc == -1) { 586 throwErrnoException(env, "fstatfs"); 587 return NULL; 588 } 589 return makeStructStatFs(env, sb); 590 } 591 592 static void Posix_fsync(JNIEnv* env, jobject, jobject javaFd) { 593 int fd = jniGetFDFromFileDescriptor(env, javaFd); 594 throwIfMinusOne(env, "fsync", TEMP_FAILURE_RETRY(fsync(fd))); 595 } 596 597 static void Posix_ftruncate(JNIEnv* env, jobject, jobject javaFd, jlong length) { 598 int fd = jniGetFDFromFileDescriptor(env, javaFd); 599 throwIfMinusOne(env, "ftruncate", TEMP_FAILURE_RETRY(ftruncate64(fd, length))); 600 } 601 602 static jstring Posix_gai_strerror(JNIEnv* env, jobject, jint error) { 603 return env->NewStringUTF(gai_strerror(error)); 604 } 605 606 static jobjectArray Posix_getaddrinfo(JNIEnv* env, jobject, jstring javaNode, jobject javaHints) { 607 ScopedUtfChars node(env, javaNode); 608 if (node.c_str() == NULL) { 609 return NULL; 610 } 611 612 static jfieldID flagsFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_flags", "I"); 613 static jfieldID familyFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_family", "I"); 614 static jfieldID socktypeFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_socktype", "I"); 615 static jfieldID protocolFid = env->GetFieldID(JniConstants::structAddrinfoClass, "ai_protocol", "I"); 616 617 addrinfo hints; 618 memset(&hints, 0, sizeof(hints)); 619 hints.ai_flags = env->GetIntField(javaHints, flagsFid); 620 hints.ai_family = env->GetIntField(javaHints, familyFid); 621 hints.ai_socktype = env->GetIntField(javaHints, socktypeFid); 622 hints.ai_protocol = env->GetIntField(javaHints, protocolFid); 623 624 addrinfo* addressList = NULL; 625 errno = 0; 626 int rc = getaddrinfo(node.c_str(), NULL, &hints, &addressList); 627 UniquePtr<addrinfo, addrinfo_deleter> addressListDeleter(addressList); 628 if (rc != 0) { 629 throwGaiException(env, "getaddrinfo", rc); 630 return NULL; 631 } 632 633 // Count results so we know how to size the output array. 634 int addressCount = 0; 635 for (addrinfo* ai = addressList; ai != NULL; ai = ai->ai_next) { 636 if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) { 637 ++addressCount; 638 } else { 639 ALOGE("getaddrinfo unexpected ai_family %i", ai->ai_family); 640 } 641 } 642 if (addressCount == 0) { 643 return NULL; 644 } 645 646 // Prepare output array. 647 jobjectArray result = env->NewObjectArray(addressCount, JniConstants::inetAddressClass, NULL); 648 if (result == NULL) { 649 return NULL; 650 } 651 652 // Examine returned addresses one by one, save them in the output array. 653 int index = 0; 654 for (addrinfo* ai = addressList; ai != NULL; ai = ai->ai_next) { 655 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) { 656 // Unknown address family. Skip this address. 657 ALOGE("getaddrinfo unexpected ai_family %i", ai->ai_family); 658 continue; 659 } 660 661 // Convert each IP address into a Java byte array. 662 sockaddr_storage& address = *reinterpret_cast<sockaddr_storage*>(ai->ai_addr); 663 ScopedLocalRef<jobject> inetAddress(env, sockaddrToInetAddress(env, address, NULL)); 664 if (inetAddress.get() == NULL) { 665 return NULL; 666 } 667 env->SetObjectArrayElement(result, index, inetAddress.get()); 668 ++index; 669 } 670 return result; 671 } 672 673 static jint Posix_getegid(JNIEnv*, jobject) { 674 return getegid(); 675 } 676 677 static jint Posix_geteuid(JNIEnv*, jobject) { 678 return geteuid(); 679 } 680 681 static jint Posix_getgid(JNIEnv*, jobject) { 682 return getgid(); 683 } 684 685 static jstring Posix_getenv(JNIEnv* env, jobject, jstring javaName) { 686 ScopedUtfChars name(env, javaName); 687 if (name.c_str() == NULL) { 688 return NULL; 689 } 690 return env->NewStringUTF(getenv(name.c_str())); 691 } 692 693 static jstring Posix_getnameinfo(JNIEnv* env, jobject, jobject javaAddress, jint flags) { 694 sockaddr_storage ss; 695 socklen_t sa_len; 696 if (!inetAddressToSockaddrVerbatim(env, javaAddress, 0, ss, sa_len)) { 697 return NULL; 698 } 699 char buf[NI_MAXHOST]; // NI_MAXHOST is longer than INET6_ADDRSTRLEN. 700 errno = 0; 701 int rc = getnameinfo(reinterpret_cast<sockaddr*>(&ss), sa_len, buf, sizeof(buf), NULL, 0, flags); 702 if (rc != 0) { 703 throwGaiException(env, "getnameinfo", rc); 704 return NULL; 705 } 706 return env->NewStringUTF(buf); 707 } 708 709 static jobject Posix_getpeername(JNIEnv* env, jobject, jobject javaFd) { 710 return doGetSockName(env, javaFd, false); 711 } 712 713 static jint Posix_getpid(JNIEnv*, jobject) { 714 return getpid(); 715 } 716 717 static jint Posix_getppid(JNIEnv*, jobject) { 718 return getppid(); 719 } 720 721 static jobject Posix_getpwnam(JNIEnv* env, jobject, jstring javaName) { 722 ScopedUtfChars name(env, javaName); 723 if (name.c_str() == NULL) { 724 return NULL; 725 } 726 return Passwd(env).getpwnam(name.c_str()); 727 } 728 729 static jobject Posix_getpwuid(JNIEnv* env, jobject, jint uid) { 730 return Passwd(env).getpwuid(uid); 731 } 732 733 static jobject Posix_getsockname(JNIEnv* env, jobject, jobject javaFd) { 734 return doGetSockName(env, javaFd, true); 735 } 736 737 static jint Posix_getsockoptByte(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 738 int fd = jniGetFDFromFileDescriptor(env, javaFd); 739 u_char result = 0; 740 socklen_t size = sizeof(result); 741 throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size))); 742 return result; 743 } 744 745 static jobject Posix_getsockoptInAddr(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 746 int fd = jniGetFDFromFileDescriptor(env, javaFd); 747 sockaddr_storage ss; 748 memset(&ss, 0, sizeof(ss)); 749 ss.ss_family = AF_INET; // This is only for the IPv4-only IP_MULTICAST_IF. 750 sockaddr_in* sa = reinterpret_cast<sockaddr_in*>(&ss); 751 socklen_t size = sizeof(sa->sin_addr); 752 int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &sa->sin_addr, &size)); 753 if (rc == -1) { 754 throwErrnoException(env, "getsockopt"); 755 return NULL; 756 } 757 return sockaddrToInetAddress(env, ss, NULL); 758 } 759 760 static jint Posix_getsockoptInt(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 761 int fd = jniGetFDFromFileDescriptor(env, javaFd); 762 jint result = 0; 763 socklen_t size = sizeof(result); 764 throwIfMinusOne(env, "getsockopt", TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &result, &size))); 765 return result; 766 } 767 768 static jobject Posix_getsockoptLinger(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 769 int fd = jniGetFDFromFileDescriptor(env, javaFd); 770 struct linger l; 771 socklen_t size = sizeof(l); 772 memset(&l, 0, size); 773 int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &l, &size)); 774 if (rc == -1) { 775 throwErrnoException(env, "getsockopt"); 776 return NULL; 777 } 778 return makeStructLinger(env, l); 779 } 780 781 static jobject Posix_getsockoptTimeval(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 782 int fd = jniGetFDFromFileDescriptor(env, javaFd); 783 struct timeval tv; 784 socklen_t size = sizeof(tv); 785 memset(&tv, 0, size); 786 int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &tv, &size)); 787 if (rc == -1) { 788 throwErrnoException(env, "getsockopt"); 789 return NULL; 790 } 791 return makeStructTimeval(env, tv); 792 } 793 794 static jobject Posix_getsockoptUcred(JNIEnv* env, jobject, jobject javaFd, jint level, jint option) { 795 int fd = jniGetFDFromFileDescriptor(env, javaFd); 796 struct ucred u; 797 socklen_t size = sizeof(u); 798 memset(&u, 0, size); 799 int rc = TEMP_FAILURE_RETRY(getsockopt(fd, level, option, &u, &size)); 800 if (rc == -1) { 801 throwErrnoException(env, "getsockopt"); 802 return NULL; 803 } 804 return makeStructUcred(env, u); 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_statfs(JNIEnv* env, jobject, jstring javaPath) { 1264 ScopedUtfChars path(env, javaPath); 1265 if (path.c_str() == NULL) { 1266 return NULL; 1267 } 1268 struct statfs sb; 1269 int rc = TEMP_FAILURE_RETRY(statfs(path.c_str(), &sb)); 1270 if (rc == -1) { 1271 throwErrnoException(env, "statfs"); 1272 return NULL; 1273 } 1274 return makeStructStatFs(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, fstatfs, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStatFs;"), 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, getuid, "()I"), 1410 NATIVE_METHOD(Posix, if_indextoname, "(I)Ljava/lang/String;"), 1411 NATIVE_METHOD(Posix, inet_pton, "(ILjava/lang/String;)Ljava/net/InetAddress;"), 1412 NATIVE_METHOD(Posix, ioctlInetAddress, "(Ljava/io/FileDescriptor;ILjava/lang/String;)Ljava/net/InetAddress;"), 1413 NATIVE_METHOD(Posix, ioctlInt, "(Ljava/io/FileDescriptor;ILlibcore/util/MutableInt;)I"), 1414 NATIVE_METHOD(Posix, isatty, "(Ljava/io/FileDescriptor;)Z"), 1415 NATIVE_METHOD(Posix, kill, "(II)V"), 1416 NATIVE_METHOD(Posix, lchown, "(Ljava/lang/String;II)V"), 1417 NATIVE_METHOD(Posix, listen, "(Ljava/io/FileDescriptor;I)V"), 1418 NATIVE_METHOD(Posix, lseek, "(Ljava/io/FileDescriptor;JI)J"), 1419 NATIVE_METHOD(Posix, lstat, "(Ljava/lang/String;)Llibcore/io/StructStat;"), 1420 NATIVE_METHOD(Posix, mincore, "(JJ[B)V"), 1421 NATIVE_METHOD(Posix, mkdir, "(Ljava/lang/String;I)V"), 1422 NATIVE_METHOD(Posix, mlock, "(JJ)V"), 1423 NATIVE_METHOD(Posix, mmap, "(JJIILjava/io/FileDescriptor;J)J"), 1424 NATIVE_METHOD(Posix, msync, "(JJI)V"), 1425 NATIVE_METHOD(Posix, munlock, "(JJ)V"), 1426 NATIVE_METHOD(Posix, munmap, "(JJ)V"), 1427 NATIVE_METHOD(Posix, open, "(Ljava/lang/String;II)Ljava/io/FileDescriptor;"), 1428 NATIVE_METHOD(Posix, pipe, "()[Ljava/io/FileDescriptor;"), 1429 NATIVE_METHOD(Posix, poll, "([Llibcore/io/StructPollfd;I)I"), 1430 NATIVE_METHOD(Posix, preadBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIJ)I"), 1431 NATIVE_METHOD(Posix, pwriteBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIJ)I"), 1432 NATIVE_METHOD(Posix, readBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"), 1433 NATIVE_METHOD(Posix, readv, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"), 1434 NATIVE_METHOD(Posix, recvfromBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIILjava/net/InetSocketAddress;)I"), 1435 NATIVE_METHOD(Posix, remove, "(Ljava/lang/String;)V"), 1436 NATIVE_METHOD(Posix, rename, "(Ljava/lang/String;Ljava/lang/String;)V"), 1437 NATIVE_METHOD(Posix, sendfile, "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Llibcore/util/MutableLong;J)J"), 1438 NATIVE_METHOD(Posix, sendtoBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIILjava/net/InetAddress;I)I"), 1439 NATIVE_METHOD(Posix, setegid, "(I)V"), 1440 NATIVE_METHOD(Posix, setenv, "(Ljava/lang/String;Ljava/lang/String;Z)V"), 1441 NATIVE_METHOD(Posix, seteuid, "(I)V"), 1442 NATIVE_METHOD(Posix, setgid, "(I)V"), 1443 NATIVE_METHOD(Posix, setsid, "()I"), 1444 NATIVE_METHOD(Posix, setsockoptByte, "(Ljava/io/FileDescriptor;III)V"), 1445 NATIVE_METHOD(Posix, setsockoptIfreq, "(Ljava/io/FileDescriptor;IILjava/lang/String;)V"), 1446 NATIVE_METHOD(Posix, setsockoptInt, "(Ljava/io/FileDescriptor;III)V"), 1447 NATIVE_METHOD(Posix, setsockoptIpMreqn, "(Ljava/io/FileDescriptor;III)V"), 1448 NATIVE_METHOD(Posix, setsockoptGroupReq, "(Ljava/io/FileDescriptor;IILlibcore/io/StructGroupReq;)V"), 1449 NATIVE_METHOD(Posix, setsockoptLinger, "(Ljava/io/FileDescriptor;IILlibcore/io/StructLinger;)V"), 1450 NATIVE_METHOD(Posix, setsockoptTimeval, "(Ljava/io/FileDescriptor;IILlibcore/io/StructTimeval;)V"), 1451 NATIVE_METHOD(Posix, setuid, "(I)V"), 1452 NATIVE_METHOD(Posix, shutdown, "(Ljava/io/FileDescriptor;I)V"), 1453 NATIVE_METHOD(Posix, socket, "(III)Ljava/io/FileDescriptor;"), 1454 NATIVE_METHOD(Posix, socketpair, "(IIILjava/io/FileDescriptor;Ljava/io/FileDescriptor;)V"), 1455 NATIVE_METHOD(Posix, stat, "(Ljava/lang/String;)Llibcore/io/StructStat;"), 1456 NATIVE_METHOD(Posix, statfs, "(Ljava/lang/String;)Llibcore/io/StructStatFs;"), 1457 NATIVE_METHOD(Posix, strerror, "(I)Ljava/lang/String;"), 1458 NATIVE_METHOD(Posix, strsignal, "(I)Ljava/lang/String;"), 1459 NATIVE_METHOD(Posix, symlink, "(Ljava/lang/String;Ljava/lang/String;)V"), 1460 NATIVE_METHOD(Posix, sysconf, "(I)J"), 1461 NATIVE_METHOD(Posix, tcdrain, "(Ljava/io/FileDescriptor;)V"), 1462 NATIVE_METHOD(Posix, tcsendbreak, "(Ljava/io/FileDescriptor;I)V"), 1463 NATIVE_METHOD(Posix, umaskImpl, "(I)I"), 1464 NATIVE_METHOD(Posix, uname, "()Llibcore/io/StructUtsname;"), 1465 NATIVE_METHOD(Posix, unsetenv, "(Ljava/lang/String;)V"), 1466 NATIVE_METHOD(Posix, waitpid, "(ILlibcore/util/MutableInt;I)I"), 1467 NATIVE_METHOD(Posix, writeBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"), 1468 NATIVE_METHOD(Posix, writev, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"), 1469 }; 1470 void register_libcore_io_Posix(JNIEnv* env) { 1471 jniRegisterNativeMethods(env, "libcore/io/Posix", gMethods, NELEM(gMethods)); 1472 } 1473