blob: 1daa83d9f2498c59d84e71700054ae482607db7c [file] [log] [blame]
Elliott Hughesec617e22011-03-16 16:42:06 -07001/*
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
Elliott Hughes996bf792011-05-09 18:26:23 -070019#include "AsynchronousSocketCloseMonitor.h"
Elliott Hughesec617e22011-03-16 16:42:06 -070020#include "JNIHelp.h"
21#include "JniConstants.h"
Elliott Hughesddfdbb92011-03-16 19:09:22 -070022#include "JniException.h"
Elliott Hughes0a9d1ee2011-03-30 15:35:16 -070023#include "NetworkUtilities.h"
Elliott Hughes6c1e5f42013-01-25 15:10:58 -080024#include "Portability.h"
Elliott Hughes0568a632011-04-25 18:33:06 -070025#include "ScopedBytes.h"
Elliott Hughese9f12042011-05-11 11:48:46 -070026#include "ScopedLocalRef.h"
Elliott Hughes0f746ff52011-03-22 16:10:21 -070027#include "ScopedPrimitiveArray.h"
Elliott Hughesec617e22011-03-16 16:42:06 -070028#include "ScopedUtfChars.h"
Elliott Hughes59fa7162011-03-23 14:55:04 -070029#include "StaticAssert.h"
Elliott Hughes1c039d72011-05-04 16:04:04 -070030#include "UniquePtr.h"
Elliott Hughesddfdbb92011-03-16 19:09:22 -070031#include "toStringArray.h"
Elliott Hughesec617e22011-03-16 16:42:06 -070032
Elliott Hughes1c039d72011-05-04 16:04:04 -070033#include <arpa/inet.h>
Elliott Hughesddfdbb92011-03-16 19:09:22 -070034#include <errno.h>
Elliott Hughes0ac77ac2011-03-22 18:45:25 -070035#include <fcntl.h>
Elliott Hughesb9746662011-04-06 12:01:16 -070036#include <net/if.h>
Elliott Hughes4f11ebe2011-04-19 15:38:10 -070037#include <netdb.h>
Elliott Hughes0a9d1ee2011-03-30 15:35:16 -070038#include <netinet/in.h>
Elliott Hughes1c039d72011-05-04 16:04:04 -070039#include <netinet/in.h>
Elliott Hughes70c82042011-05-11 16:12:36 -070040#include <poll.h>
Elliott Hughesd4419fc2011-05-12 17:22:07 -070041#include <pwd.h>
Elliott Hughesa5fb7062011-04-28 15:36:47 -070042#include <signal.h>
Elliott Hughesec617e22011-03-16 16:42:06 -070043#include <stdlib.h>
Elliott Hughes461d0d82011-03-28 15:12:20 -070044#include <sys/ioctl.h>
Elliott Hughes7e25eff2011-03-18 18:39:29 -070045#include <sys/mman.h>
Elliott Hughes59e47442011-03-24 11:36:07 -070046#include <sys/socket.h>
Elliott Hughes1c039d72011-05-04 16:04:04 -070047#include <sys/socket.h>
Elliott Hughes47cb3382011-03-17 18:28:00 -070048#include <sys/stat.h>
Elliott Hughes0a9d1ee2011-03-30 15:35:16 -070049#include <sys/time.h>
Elliott Hughes0ac77ac2011-03-22 18:45:25 -070050#include <sys/types.h>
Elliott Hughesbbac92e2011-03-28 19:57:00 -070051#include <sys/uio.h>
Elliott Hughes7341b9e2011-03-24 13:51:38 -070052#include <sys/utsname.h>
Elliott Hughes9e67ca72011-05-19 15:25:10 -070053#include <sys/wait.h>
Johan Redestigbb5816a2012-01-10 17:48:01 +090054#include <termios.h>
Elliott Hughes0ac77ac2011-03-22 18:45:25 -070055#include <unistd.h>
Elliott Hughesec617e22011-03-16 16:42:06 -070056
Elliott Hughesd4419fc2011-05-12 17:22:07 -070057#define TO_JAVA_STRING(NAME, EXP) \
58 jstring NAME = env->NewStringUTF(EXP); \
59 if (NAME == NULL) return NULL;
60
Elliott Hughes1c039d72011-05-04 16:04:04 -070061struct 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
Elliott Hughes996bf792011-05-09 18:26:23 -070069/**
Elliott Hughes1124a812012-08-28 17:57:02 -070070 * 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.
Elliott Hughes996bf792011-05-09 18:26:23 -070073 *
74 * Returns the result of 'exp', though a Java exception will be pending if the result is -1.
Elliott Hughes996bf792011-05-09 18:26:23 -070075 */
Elliott Hughes1124a812012-08-28 17:57:02 -070076#define NET_FAILURE_RETRY(jni_env, return_type, syscall_name, java_fd, ...) ({ \
77 return_type _rc = -1; \
Elliott Hughes996bf792011-05-09 18:26:23 -070078 do { \
79 { \
Elliott Hughes1124a812012-08-28 17:57:02 -070080 int _fd = jniGetFDFromFileDescriptor(jni_env, java_fd); \
81 AsynchronousSocketCloseMonitor _monitor(_fd); \
82 _rc = syscall_name(_fd, __VA_ARGS__); \
Elliott Hughes996bf792011-05-09 18:26:23 -070083 } \
84 if (_rc == -1) { \
Elliott Hughes1124a812012-08-28 17:57:02 -070085 if (jniGetFDFromFileDescriptor(jni_env, java_fd) == -1) { \
86 jniThrowException(jni_env, "java/net/SocketException", "Socket closed"); \
Elliott Hughes996bf792011-05-09 18:26:23 -070087 break; \
88 } else if (errno != EINTR) { \
Elliott Hughes1124a812012-08-28 17:57:02 -070089 /* TODO: with a format string we could show the arguments too, like strace(1). */ \
90 throwErrnoException(jni_env, # syscall_name); \
Elliott Hughes996bf792011-05-09 18:26:23 -070091 break; \
92 } \
93 } \
94 } while (_rc == -1); \
95 _rc; })
96
Elliott Hughes4f11ebe2011-04-19 15:38:10 -070097static void throwException(JNIEnv* env, jclass exceptionClass, jmethodID ctor3, jmethodID ctor2,
98 const char* functionName, int error) {
Elliott Hughesddfdbb92011-03-16 19:09:22 -070099 jthrowable cause = NULL;
100 if (env->ExceptionCheck()) {
101 cause = env->ExceptionOccurred();
102 env->ExceptionClear();
103 }
104
Elliott Hughes4f11ebe2011-04-19 15:38:10 -0700105 ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName));
106 if (detailMessage.get() == NULL) {
Elliott Hughesf5333fd2011-03-18 14:13:08 -0700107 // 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
Elliott Hughesddfdbb92011-03-16 19:09:22 -0700112 jobject exception;
113 if (cause != NULL) {
Elliott Hughes4f11ebe2011-04-19 15:38:10 -0700114 exception = env->NewObject(exceptionClass, ctor3, detailMessage.get(), error, cause);
Elliott Hughesddfdbb92011-03-16 19:09:22 -0700115 } else {
Elliott Hughes4f11ebe2011-04-19 15:38:10 -0700116 exception = env->NewObject(exceptionClass, ctor2, detailMessage.get(), error);
Elliott Hughesddfdbb92011-03-16 19:09:22 -0700117 }
118 env->Throw(reinterpret_cast<jthrowable>(exception));
Elliott Hughesdedaccd2011-03-18 15:56:20 -0700119}
120
Elliott Hughes4f11ebe2011-04-19 15:38:10 -0700121static 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
130static void throwGaiException(JNIEnv* env, const char* functionName, int error) {
Elliott Hughesc1cee0d2013-01-18 16:59:02 -0800131 // 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) {
Elliott Hughes75cf1492012-02-15 17:05:50 -0800137 // 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
Elliott Hughes4f11ebe2011-04-19 15:38:10 -0700142 throwErrnoException(env, functionName);
143 // Deliberately fall through to throw another exception...
144 }
Elliott Hughes4f11ebe2011-04-19 15:38:10 -0700145 throwException(env, JniConstants::gaiExceptionClass, ctor3, ctor2, functionName, error);
146}
147
Elliott Hughesdedaccd2011-03-18 15:56:20 -0700148template <typename rc_t>
Elliott Hughes7e25eff2011-03-18 18:39:29 -0700149static rc_t throwIfMinusOne(JNIEnv* env, const char* name, rc_t rc) {
Elliott Hughesdedaccd2011-03-18 15:56:20 -0700150 if (rc == rc_t(-1)) {
Elliott Hughes7e25eff2011-03-18 18:39:29 -0700151 throwErrnoException(env, name);
Elliott Hughesdedaccd2011-03-18 15:56:20 -0700152 }
153 return rc;
Elliott Hughes47cb3382011-03-17 18:28:00 -0700154}
155
Elliott Hughes8dc75472011-04-26 10:10:46 -0700156template <typename ScopedT>
Elliott Hughesbbac92e2011-03-28 19:57:00 -0700157class IoVec {
158public:
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.
Elliott Hughesd4419fc2011-05-12 17:22:07 -0700176 // TODO: you can query the limit at runtime with sysconf(_SC_IOV_MAX).
Elliott Hughesbbac92e2011-03-28 19:57:00 -0700177 for (size_t i = 0; i < mBufferCount; ++i) {
Elliott Hughese9f12042011-05-11 11:48:46 -0700178 jobject buffer = mEnv->GetObjectArrayElement(javaBuffers, i); // We keep this local ref.
Elliott Hughes8dc75472011-04-26 10:10:46 -0700179 mScopedBuffers.push_back(new ScopedT(mEnv, buffer));
180 jbyte* ptr = const_cast<jbyte*>(mScopedBuffers.back()->get());
Elliott Hughesbbac92e2011-03-28 19:57:00 -0700181 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() {
Elliott Hughes8dc75472011-04-26 10:10:46 -0700193 for (size_t i = 0; i < mScopedBuffers.size(); ++i) {
194 delete mScopedBuffers[i];
Elliott Hughesbbac92e2011-03-28 19:57:00 -0700195 }
196 mEnv->PopLocalFrame(NULL);
197 }
198
199 iovec* get() {
200 return &mIoVec[0];
201 }
202
203 size_t size() {
204 return mBufferCount;
205 }
206
207private:
208 JNIEnv* mEnv;
209 size_t mBufferCount;
210 std::vector<iovec> mIoVec;
Elliott Hughes8dc75472011-04-26 10:10:46 -0700211 std::vector<ScopedT*> mScopedBuffers;
Elliott Hughesbbac92e2011-03-28 19:57:00 -0700212};
213
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700214static jobject makeSocketAddress(JNIEnv* env, const sockaddr_storage& ss) {
Elliott Hughes23ec0912011-05-18 18:21:17 -0700215 jint port;
216 jobject inetAddress = sockaddrToInetAddress(env, ss, &port);
Elliott Hughes0a9d1ee2011-03-30 15:35:16 -0700217 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
Elliott Hughesd4419fc2011-05-12 17:22:07 -0700225static 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
Elliott Hughes47cb3382011-03-17 18:28:00 -0700235static 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,
Elliott Hughes59fa7162011-03-23 14:55:04 -0700239 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
248static jobject makeStructStatFs(JNIEnv* env, const struct statfs& sb) {
Elliott Hughes6c1e5f42013-01-25 15:10:58 -0800249#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.
Elliott Hughes59fa7162011-03-23 14:55:04 -0700254 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
Elliott Hughes6c1e5f42013-01-25 15:10:58 -0800258 jlong max_name_length = static_cast<jlong>(sb.f_namelen);
259#endif
260
Elliott Hughes59fa7162011-03-23 14:55:04 -0700261 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),
Elliott Hughes6c1e5f42013-01-25 15:10:58 -0800266 static_cast<jlong>(sb.f_ffree), max_name_length,
Elliott Hughes59fa7162011-03-23 14:55:04 -0700267 static_cast<jlong>(sb.f_frsize));
Elliott Hughes47cb3382011-03-17 18:28:00 -0700268}
269
Elliott Hughes0a9d1ee2011-03-30 15:35:16 -0700270static 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
275static 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
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700281static 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
Elliott Hughes7341b9e2011-03-24 13:51:38 -0700286static jobject makeStructUtsname(JNIEnv* env, const struct utsname& buf) {
Elliott Hughesd4419fc2011-05-12 17:22:07 -0700287 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);
Elliott Hughes7341b9e2011-03-24 13:51:38 -0700292 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
Elliott Hughesa37e9712011-04-21 14:07:01 -0700298static 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
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700309static bool fillInetSocketAddress(JNIEnv* env, jint rc, jobject javaInetSocketAddress, const sockaddr_storage& ss) {
Elliott Hughes553d98a2011-05-19 13:50:09 -0700310 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
Elliott Hughes47cb3382011-03-17 18:28:00 -0700326static 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));
Elliott Hughesdedaccd2011-03-18 15:56:20 -0700334 if (rc == -1) {
Elliott Hughes7e25eff2011-03-18 18:39:29 -0700335 throwErrnoException(env, isLstat ? "lstat" : "stat");
Elliott Hughes47cb3382011-03-17 18:28:00 -0700336 return NULL;
337 }
338 return makeStructStat(env, sb);
Elliott Hughesddfdbb92011-03-16 19:09:22 -0700339}
340
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700341static 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
Elliott Hughesd4419fc2011-05-12 17:22:07 -0700356class Passwd {
357public:
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
380private:
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
Elliott Hughes553d98a2011-05-19 13:50:09 -0700397static 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));
Elliott Hughes553d98a2011-05-19 13:50:09 -0700401 sockaddr* peer = (javaInetSocketAddress != NULL) ? reinterpret_cast<sockaddr*>(&ss) : NULL;
402 socklen_t* peerLength = (javaInetSocketAddress != NULL) ? &sl : 0;
Elliott Hughes1124a812012-08-28 17:57:02 -0700403 jint clientFd = NET_FAILURE_RETRY(env, int, accept, javaFd, peer, peerLength);
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700404 if (clientFd == -1 || !fillInetSocketAddress(env, clientFd, javaInetSocketAddress, ss)) {
Elliott Hughes553d98a2011-05-19 13:50:09 -0700405 close(clientFd);
406 return NULL;
407 }
408 return (clientFd != -1) ? jniCreateFileDescriptor(env, clientFd) : NULL;
409}
410
Elliott Hughesddfdbb92011-03-16 19:09:22 -0700411static 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 }
Elliott Hughes47cb3382011-03-17 18:28:00 -0700416 int rc = TEMP_FAILURE_RETRY(access(path.c_str(), mode));
Elliott Hughesdedaccd2011-03-18 15:56:20 -0700417 if (rc == -1) {
Elliott Hughes7e25eff2011-03-18 18:39:29 -0700418 throwErrnoException(env, "access");
Elliott Hughesdedaccd2011-03-18 15:56:20 -0700419 }
Elliott Hughesddfdbb92011-03-16 19:09:22 -0700420 return (rc == 0);
421}
422
Elliott Hughesda150092011-05-09 14:00:48 -0700423static void Posix_bind(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) {
424 sockaddr_storage ss;
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700425 socklen_t sa_len;
426 if (!inetAddressToSockaddr(env, javaAddress, port, ss, sa_len)) {
Elliott Hughesda150092011-05-09 14:00:48 -0700427 return;
428 }
Elliott Hughesda150092011-05-09 14:00:48 -0700429 const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss);
Elliott Hughes1124a812012-08-28 17:57:02 -0700430 // We don't need the return value because we'll already have thrown.
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700431 (void) NET_FAILURE_RETRY(env, int, bind, javaFd, sa, sa_len);
Elliott Hughesda150092011-05-09 14:00:48 -0700432}
433
Elliott Hughesb7190192011-03-23 17:14:34 -0700434static 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
Elliott Hughes44f78752012-08-16 12:29:52 -0700442static 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
Elliott Hughes462bdac2011-03-30 10:43:48 -0700450static 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
Elliott Hughes996bf792011-05-09 18:26:23 -0700462static void Posix_connect(JNIEnv* env, jobject, jobject javaFd, jobject javaAddress, jint port) {
463 sockaddr_storage ss;
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700464 socklen_t sa_len;
465 if (!inetAddressToSockaddr(env, javaAddress, port, ss, sa_len)) {
Elliott Hughes996bf792011-05-09 18:26:23 -0700466 return;
467 }
Elliott Hughes996bf792011-05-09 18:26:23 -0700468 const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss);
Elliott Hughes1124a812012-08-28 17:57:02 -0700469 // We don't need the return value because we'll already have thrown.
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700470 (void) NET_FAILURE_RETRY(env, int, connect, javaFd, sa, sa_len);
Elliott Hughes996bf792011-05-09 18:26:23 -0700471}
472
Elliott Hughes396a9c62011-05-12 15:05:54 -0700473static 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
479static 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
Elliott Hughesddfdbb92011-03-16 19:09:22 -0700485static jobjectArray Posix_environ(JNIEnv* env, jobject) {
486 extern char** environ; // Standard, but not in any header file.
487 return toStringArray(env, environ);
488}
489
Nick Kralevich798f8552013-02-05 13:02:13 -0800490static 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
505static 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
Elliott Hughes44f78752012-08-16 12:29:52 -0700518static 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
523static 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
Elliott Hughesfc549a02011-03-24 16:38:05 -0700528static 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
533static 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
538static 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
Elliott Hughes52724d32011-03-17 12:13:09 -0700565static void Posix_fdatasync(JNIEnv* env, jobject, jobject javaFd) {
566 int fd = jniGetFDFromFileDescriptor(env, javaFd);
Elliott Hughes7e25eff2011-03-18 18:39:29 -0700567 throwIfMinusOne(env, "fdatasync", TEMP_FAILURE_RETRY(fdatasync(fd)));
Elliott Hughes52724d32011-03-17 12:13:09 -0700568}
569
Elliott Hughes47cb3382011-03-17 18:28:00 -0700570static 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));
Elliott Hughesdedaccd2011-03-18 15:56:20 -0700574 if (rc == -1) {
Elliott Hughes7e25eff2011-03-18 18:39:29 -0700575 throwErrnoException(env, "fstat");
Elliott Hughes47cb3382011-03-17 18:28:00 -0700576 return NULL;
577 }
578 return makeStructStat(env, sb);
579}
580
Elliott Hughes59fa7162011-03-23 14:55:04 -0700581static 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
Elliott Hughes52724d32011-03-17 12:13:09 -0700592static void Posix_fsync(JNIEnv* env, jobject, jobject javaFd) {
593 int fd = jniGetFDFromFileDescriptor(env, javaFd);
Elliott Hughes7e25eff2011-03-18 18:39:29 -0700594 throwIfMinusOne(env, "fsync", TEMP_FAILURE_RETRY(fsync(fd)));
Elliott Hughesf5333fd2011-03-18 14:13:08 -0700595}
596
597static void Posix_ftruncate(JNIEnv* env, jobject, jobject javaFd, jlong length) {
598 int fd = jniGetFDFromFileDescriptor(env, javaFd);
Elliott Hughes7e25eff2011-03-18 18:39:29 -0700599 throwIfMinusOne(env, "ftruncate", TEMP_FAILURE_RETRY(ftruncate64(fd, length)));
Elliott Hughes52724d32011-03-17 12:13:09 -0700600}
601
Elliott Hughes4f11ebe2011-04-19 15:38:10 -0700602static jstring Posix_gai_strerror(JNIEnv* env, jobject, jint error) {
603 return env->NewStringUTF(gai_strerror(error));
604}
605
Elliott Hughes1c039d72011-05-04 16:04:04 -0700606static 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;
Elliott Hughes75cf1492012-02-15 17:05:50 -0800625 errno = 0;
Elliott Hughes1c039d72011-05-04 16:04:04 -0700626 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 {
Steve Block679cf682012-01-08 10:18:49 +0000639 ALOGE("getaddrinfo unexpected ai_family %i", ai->ai_family);
Elliott Hughes1c039d72011-05-04 16:04:04 -0700640 }
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.
Steve Block679cf682012-01-08 10:18:49 +0000657 ALOGE("getaddrinfo unexpected ai_family %i", ai->ai_family);
Elliott Hughes1c039d72011-05-04 16:04:04 -0700658 continue;
659 }
660
661 // Convert each IP address into a Java byte array.
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700662 sockaddr_storage& address = *reinterpret_cast<sockaddr_storage*>(ai->ai_addr);
Elliott Hughes23ec0912011-05-18 18:21:17 -0700663 ScopedLocalRef<jobject> inetAddress(env, sockaddrToInetAddress(env, address, NULL));
Elliott Hughes1c039d72011-05-04 16:04:04 -0700664 if (inetAddress.get() == NULL) {
665 return NULL;
666 }
667 env->SetObjectArrayElement(result, index, inetAddress.get());
668 ++index;
669 }
670 return result;
671}
672
Elliott Hughes396a9c62011-05-12 15:05:54 -0700673static jint Posix_getegid(JNIEnv*, jobject) {
674 return getegid();
675}
676
677static jint Posix_geteuid(JNIEnv*, jobject) {
678 return geteuid();
679}
680
681static jint Posix_getgid(JNIEnv*, jobject) {
682 return getgid();
683}
684
Elliott Hughesec617e22011-03-16 16:42:06 -0700685static 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
Elliott Hughes4f11ebe2011-04-19 15:38:10 -0700693static jstring Posix_getnameinfo(JNIEnv* env, jobject, jobject javaAddress, jint flags) {
694 sockaddr_storage ss;
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700695 socklen_t sa_len;
696 if (!inetAddressToSockaddrVerbatim(env, javaAddress, 0, ss, sa_len)) {
Elliott Hughes4f11ebe2011-04-19 15:38:10 -0700697 return NULL;
698 }
Elliott Hughes4f11ebe2011-04-19 15:38:10 -0700699 char buf[NI_MAXHOST]; // NI_MAXHOST is longer than INET6_ADDRSTRLEN.
Elliott Hughes75cf1492012-02-15 17:05:50 -0800700 errno = 0;
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700701 int rc = getnameinfo(reinterpret_cast<sockaddr*>(&ss), sa_len, buf, sizeof(buf), NULL, 0, flags);
Elliott Hughes4f11ebe2011-04-19 15:38:10 -0700702 if (rc != 0) {
703 throwGaiException(env, "getnameinfo", rc);
704 return NULL;
705 }
706 return env->NewStringUTF(buf);
707}
708
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700709static jobject Posix_getpeername(JNIEnv* env, jobject, jobject javaFd) {
710 return doGetSockName(env, javaFd, false);
711}
712
Elliott Hughes396a9c62011-05-12 15:05:54 -0700713static jint Posix_getpid(JNIEnv*, jobject) {
714 return getpid();
715}
716
717static jint Posix_getppid(JNIEnv*, jobject) {
718 return getppid();
719}
720
Elliott Hughesd4419fc2011-05-12 17:22:07 -0700721static 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
729static jobject Posix_getpwuid(JNIEnv* env, jobject, jint uid) {
730 return Passwd(env).getpwuid(uid);
731}
732
Elliott Hughes0a9d1ee2011-03-30 15:35:16 -0700733static jobject Posix_getsockname(JNIEnv* env, jobject, jobject javaFd) {
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700734 return doGetSockName(env, javaFd, true);
Elliott Hughes0a9d1ee2011-03-30 15:35:16 -0700735}
736
737static 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
745static 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 }
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700757 return sockaddrToInetAddress(env, ss, NULL);
Elliott Hughes0a9d1ee2011-03-30 15:35:16 -0700758}
759
760static 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
768static 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
781static 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
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700794static 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
Elliott Hughes396a9c62011-05-12 15:05:54 -0700807static jint Posix_getuid(JNIEnv*, jobject) {
808 return getuid();
809}
810
Elliott Hughesa37e9712011-04-21 14:07:01 -0700811static 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
Elliott Hughesfc041ff2011-06-03 12:43:17 -0700819static jobject Posix_inet_pton(JNIEnv* env, jobject, jint family, jstring javaName) {
Elliott Hughes1c039d72011-05-04 16:04:04 -0700820 ScopedUtfChars name(env, javaName);
821 if (name.c_str() == NULL) {
822 return NULL;
823 }
824 sockaddr_storage ss;
825 memset(&ss, 0, sizeof(ss));
Elliott Hughesfc041ff2011-06-03 12:43:17 -0700826 // 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) {
Elliott Hughes1c039d72011-05-04 16:04:04 -0700829 return NULL;
830 }
Elliott Hughesfc041ff2011-06-03 12:43:17 -0700831 ss.ss_family = family;
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700832 return sockaddrToInetAddress(env, ss, NULL);
Elliott Hughes1c039d72011-05-04 16:04:04 -0700833}
834
Elliott Hughesa37e9712011-04-21 14:07:01 -0700835static 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 }
Elliott Hughes482a3fc2013-03-20 16:10:12 -0700845 return sockaddrToInetAddress(env, reinterpret_cast<sockaddr_storage&>(req.ifr_addr), NULL);
Elliott Hughesa37e9712011-04-21 14:07:01 -0700846}
847
Elliott Hughes8b15dcc2011-03-29 14:26:04 -0700848static 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.
Elliott Hughes461d0d82011-03-28 15:12:20 -0700851 int fd = jniGetFDFromFileDescriptor(env, javaFd);
Elliott Hughes8b15dcc2011-03-29 14:26:04 -0700852 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;
Elliott Hughes461d0d82011-03-28 15:12:20 -0700859}
860
Elliott Hughes9a3f3632011-03-18 16:30:53 -0700861static jboolean Posix_isatty(JNIEnv* env, jobject, jobject javaFd) {
862 int fd = jniGetFDFromFileDescriptor(env, javaFd);
Johan Redestigbb5816a2012-01-10 17:48:01 +0900863 return TEMP_FAILURE_RETRY(isatty(fd)) == 1;
Elliott Hughes9a3f3632011-03-18 16:30:53 -0700864}
865
Elliott Hughesa5fb7062011-04-28 15:36:47 -0700866static void Posix_kill(JNIEnv* env, jobject, jint pid, jint sig) {
867 throwIfMinusOne(env, "kill", TEMP_FAILURE_RETRY(kill(pid, sig)));
868}
869
Elliott Hughes44f78752012-08-16 12:29:52 -0700870static 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
Elliott Hughese1502d62011-03-24 11:57:08 -0700878static 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
Elliott Hughesdedaccd2011-03-18 15:56:20 -0700883static jlong Posix_lseek(JNIEnv* env, jobject, jobject javaFd, jlong offset, jint whence) {
884 int fd = jniGetFDFromFileDescriptor(env, javaFd);
Elliott Hughes7e25eff2011-03-18 18:39:29 -0700885 return throwIfMinusOne(env, "lseek", TEMP_FAILURE_RETRY(lseek64(fd, offset, whence)));
Elliott Hughesdedaccd2011-03-18 15:56:20 -0700886}
887
Elliott Hughes47cb3382011-03-17 18:28:00 -0700888static jobject Posix_lstat(JNIEnv* env, jobject, jstring javaPath) {
889 return doStat(env, javaPath, true);
890}
891
Elliott Hughes0f746ff52011-03-22 16:10:21 -0700892static 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
Elliott Hughesc7fa2072011-03-23 17:37:58 -0700902static 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
Elliott Hughes7e13c0f2011-03-18 18:52:08 -0700910static 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
Elliott Hughes7e25eff2011-03-18 18:39:29 -0700915static 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
925static 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
Elliott Hughes7e13c0f2011-03-18 18:52:08 -0700930static 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
Elliott Hughes7e25eff2011-03-18 18:39:29 -0700935static 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
Elliott Hughes0ac77ac2011-03-22 18:45:25 -0700940static 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
Elliott Hughes41f06052011-03-24 10:48:09 -0700949static 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
Elliott Hughes70c82042011-05-11 16:12:36 -0700969static 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
Elliott Hughes796f0d52012-02-16 14:35:26 -0800993 // 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 }
Elliott Hughes70c82042011-05-11 16:12:36 -07001003 if (rc == -1) {
1004 throwErrnoException(env, "poll");
1005 return -1;
1006 }
1007
1008 // Update the revents fields in the Java libcore.io.StructPollfd[].
Elliott Hughes98b23d42011-05-12 12:19:13 -07001009 for (size_t i = 0; i < count; ++i) {
Elliott Hughes70c82042011-05-11 16:12:36 -07001010 ScopedLocalRef<jobject> javaStruct(env, env->GetObjectArrayElement(javaStructs, i));
Elliott Hughes98b23d42011-05-12 12:19:13 -07001011 if (javaStruct.get() == NULL) {
1012 return -1;
1013 }
Elliott Hughes70c82042011-05-11 16:12:36 -07001014 env->SetShortField(javaStruct.get(), reventsFid, fds[i].revents);
1015 }
1016 return rc;
1017}
1018
Elliott Hughese3b6fa22011-06-09 15:30:18 -07001019static 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
1028static 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
Elliott Hughes0568a632011-04-25 18:33:06 -07001037static jint Posix_readBytes(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount) {
1038 ScopedBytesRW bytes(env, javaBytes);
Elliott Hughes26c70252011-03-25 17:07:28 -07001039 if (bytes.get() == NULL) {
1040 return -1;
1041 }
Elliott Hughes26c70252011-03-25 17:07:28 -07001042 int fd = jniGetFDFromFileDescriptor(env, javaFd);
Elliott Hughes0568a632011-04-25 18:33:06 -07001043 return throwIfMinusOne(env, "read", TEMP_FAILURE_RETRY(read(fd, bytes.get() + byteOffset, byteCount)));
Elliott Hughes26c70252011-03-25 17:07:28 -07001044}
1045
Elliott Hughesbbac92e2011-03-28 19:57:00 -07001046static jint Posix_readv(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) {
Elliott Hughes8dc75472011-04-26 10:10:46 -07001047 IoVec<ScopedBytesRW> ioVec(env, env->GetArrayLength(buffers));
Elliott Hughesbbac92e2011-03-28 19:57:00 -07001048 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
Elliott Hughes23ec0912011-05-18 18:21:17 -07001055static 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));
Elliott Hughes23ec0912011-05-18 18:21:17 -07001063 sockaddr* from = (javaInetSocketAddress != NULL) ? reinterpret_cast<sockaddr*>(&ss) : NULL;
1064 socklen_t* fromLength = (javaInetSocketAddress != NULL) ? &sl : 0;
Elliott Hughes1124a812012-08-28 17:57:02 -07001065 jint recvCount = NET_FAILURE_RETRY(env, ssize_t, recvfrom, javaFd, bytes.get() + byteOffset, byteCount, flags, from, fromLength);
Elliott Hughes482a3fc2013-03-20 16:10:12 -07001066 fillInetSocketAddress(env, recvCount, javaInetSocketAddress, ss);
Elliott Hughes23ec0912011-05-18 18:21:17 -07001067 return recvCount;
1068}
1069
Elliott Hughesc7fa2072011-03-23 17:37:58 -07001070static 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
Elliott Hughesa20cc6f2011-03-23 13:49:03 -07001078static 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
Elliott Hughes8b15dcc2011-03-29 14:26:04 -07001090static 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
Elliott Hughes90d96a42011-05-16 16:43:48 -07001108static 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;
Elliott Hughes482a3fc2013-03-20 16:10:12 -07001114 socklen_t sa_len = 0;
1115 if (javaInetAddress != NULL && !inetAddressToSockaddr(env, javaInetAddress, port, ss, sa_len)) {
Elliott Hughes90d96a42011-05-16 16:43:48 -07001116 return -1;
1117 }
Elliott Hughes90d96a42011-05-16 16:43:48 -07001118 const sockaddr* to = (javaInetAddress != NULL) ? reinterpret_cast<const sockaddr*>(&ss) : NULL;
Elliott Hughes482a3fc2013-03-20 16:10:12 -07001119 return NET_FAILURE_RETRY(env, ssize_t, sendto, javaFd, bytes.get() + byteOffset, byteCount, flags, to, sa_len);
Elliott Hughes90d96a42011-05-16 16:43:48 -07001120}
1121
Elliott Hughes396a9c62011-05-12 15:05:54 -07001122static void Posix_setegid(JNIEnv* env, jobject, jint egid) {
1123 throwIfMinusOne(env, "setegid", TEMP_FAILURE_RETRY(setegid(egid)));
1124}
1125
Elliott Hughes895a6132012-11-05 15:44:56 -08001126static 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
Elliott Hughes396a9c62011-05-12 15:05:54 -07001138static void Posix_seteuid(JNIEnv* env, jobject, jint euid) {
1139 throwIfMinusOne(env, "seteuid", TEMP_FAILURE_RETRY(seteuid(euid)));
1140}
1141
1142static void Posix_setgid(JNIEnv* env, jobject, jint gid) {
1143 throwIfMinusOne(env, "setgid", TEMP_FAILURE_RETRY(setgid(gid)));
1144}
1145
Elliott Hughes6c9b5372012-09-21 10:19:21 -07001146static jint Posix_setsid(JNIEnv* env, jobject) {
1147 return throwIfMinusOne(env, "setsid", TEMP_FAILURE_RETRY(setsid()));
1148}
1149
Elliott Hughesc63f0d42011-04-01 15:31:03 -07001150static 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
Elliott Hughesa37e9712011-04-21 14:07:01 -07001156static void Posix_setsockoptIfreq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jstring javaInterfaceName) {
1157 struct ifreq req;
1158 if (!fillIfreq(env, javaInterfaceName, req)) {
Elliott Hughesb9746662011-04-06 12:01:16 -07001159 return;
1160 }
Elliott Hughesb9746662011-04-06 12:01:16 -07001161 int fd = jniGetFDFromFileDescriptor(env, javaFd);
1162 throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req))));
1163}
1164
Elliott Hughes454a95f2011-03-29 18:00:14 -07001165static 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
Elliott Hughes6c1e5f42013-01-25 15:10:58 -08001170#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED < 1070
1171// Mac OS didn't support modern multicast APIs until 10.7.
1172static void Posix_setsockoptIpMreqn(JNIEnv*, jobject, jobject, jint, jint, jint) { abort(); }
1173static void Posix_setsockoptGroupReq(JNIEnv*, jobject, jobject, jint, jint, jobject) { abort(); }
1174#else
Elliott Hughesb9746662011-04-06 12:01:16 -07001175static 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
Elliott Hughes438cb9e2011-04-01 18:05:23 -07001183static void Posix_setsockoptGroupReq(JNIEnv* env, jobject, jobject javaFd, jint level, jint option, jobject javaGroupReq) {
Elliott Hughes8ecbb3f2011-05-26 15:11:52 -07001184 struct group_req req;
1185 memset(&req, 0, sizeof(req));
Elliott Hughes438cb9e2011-04-01 18:05:23 -07001186
1187 static jfieldID grInterfaceFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_interface", "I");
Elliott Hughes8ecbb3f2011-05-26 15:11:52 -07001188 req.gr_interface = env->GetIntField(javaGroupReq, grInterfaceFid);
Elliott Hughes438cb9e2011-04-01 18:05:23 -07001189 // Get the IPv4 or IPv6 multicast address to join or leave.
1190 static jfieldID grGroupFid = env->GetFieldID(JniConstants::structGroupReqClass, "gr_group", "Ljava/net/InetAddress;");
Elliott Hughese9f12042011-05-11 11:48:46 -07001191 ScopedLocalRef<jobject> javaGroup(env, env->GetObjectField(javaGroupReq, grGroupFid));
Elliott Hughes482a3fc2013-03-20 16:10:12 -07001192 socklen_t sa_len;
1193 if (!inetAddressToSockaddrVerbatim(env, javaGroup.get(), 0, req.gr_group, sa_len)) {
Elliott Hughes438cb9e2011-04-01 18:05:23 -07001194 return;
1195 }
1196
1197 int fd = jniGetFDFromFileDescriptor(env, javaFd);
Elliott Hughes8ecbb3f2011-05-26 15:11:52 -07001198 int rc = TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &req, sizeof(req)));
Elliott Hughes438cb9e2011-04-01 18:05:23 -07001199 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.
Elliott Hughes9634b562012-05-18 09:37:47 -07001202 struct group_req64 {
Elliott Hughes438cb9e2011-04-01 18:05:23 -07001203 uint32_t gr_interface;
1204 uint32_t my_padding;
1205 sockaddr_storage gr_group;
1206 };
Elliott Hughes8ecbb3f2011-05-26 15:11:52 -07001207 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)));
Elliott Hughes438cb9e2011-04-01 18:05:23 -07001211 }
1212 throwIfMinusOne(env, "setsockopt", rc);
1213}
Elliott Hughes6c1e5f42013-01-25 15:10:58 -08001214#endif
Elliott Hughes438cb9e2011-04-01 18:05:23 -07001215
Elliott Hughesc63f0d42011-04-01 15:31:03 -07001216static 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
1226static 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;
Elliott Hughes39fd5e12011-08-14 11:29:07 -07001231 value.tv_sec = env->GetLongField(javaTimeval, tvSecFid);
1232 value.tv_usec = env->GetLongField(javaTimeval, tvUsecFid);
Elliott Hughesc63f0d42011-04-01 15:31:03 -07001233 throwIfMinusOne(env, "setsockopt", TEMP_FAILURE_RETRY(setsockopt(fd, level, option, &value, sizeof(value))));
1234}
1235
Elliott Hughes396a9c62011-05-12 15:05:54 -07001236static void Posix_setuid(JNIEnv* env, jobject, jint uid) {
1237 throwIfMinusOne(env, "setuid", TEMP_FAILURE_RETRY(setuid(uid)));
1238}
1239
Elliott Hughes59e47442011-03-24 11:36:07 -07001240static 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
Elliott Hughes454a95f2011-03-29 18:00:14 -07001245static 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
Elliott Hughes32180822012-09-28 15:53:29 -07001250static 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
Elliott Hughes47cb3382011-03-17 18:28:00 -07001259static jobject Posix_stat(JNIEnv* env, jobject, jstring javaPath) {
1260 return doStat(env, javaPath, false);
1261}
1262
Elliott Hughes0ade5522011-03-31 11:09:25 -07001263static jobject Posix_statfs(JNIEnv* env, jobject, jstring javaPath) {
Elliott Hughes59fa7162011-03-23 14:55:04 -07001264 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
Elliott Hughesddfdbb92011-03-16 19:09:22 -07001277static jstring Posix_strerror(JNIEnv* env, jobject, jint errnum) {
Elliott Hughes52724d32011-03-17 12:13:09 -07001278 char buffer[BUFSIZ];
Elliott Hughesddfdbb92011-03-16 19:09:22 -07001279 const char* message = jniStrError(errnum, buffer, sizeof(buffer));
1280 return env->NewStringUTF(message);
1281}
1282
Elliott Hughes763f8ed2013-03-28 18:26:05 -07001283static jstring Posix_strsignal(JNIEnv* env, jobject, jint signal) {
1284 return env->NewStringUTF(strsignal(signal));
1285}
1286
Elliott Hughesa20cc6f2011-03-23 13:49:03 -07001287static 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
Elliott Hughes6fc1a0e2011-03-18 10:28:24 -07001299static 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) {
Elliott Hughes7e25eff2011-03-18 18:39:29 -07001304 throwErrnoException(env, "sysconf");
Elliott Hughes6fc1a0e2011-03-18 10:28:24 -07001305 }
1306 return result;
1307}
1308
Johan Redestigbb5816a2012-01-10 17:48:01 +09001309static 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
Elliott Hughes039f7592012-12-14 16:19:24 -08001314static 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
Nick Kralevichc79d3ef2012-09-27 13:40:04 -07001319static jint Posix_umaskImpl(JNIEnv*, jobject, jint mask) {
Elliott Hughes44f78752012-08-16 12:29:52 -07001320 return umask(mask);
1321}
1322
Elliott Hughes7341b9e2011-03-24 13:51:38 -07001323static jobject Posix_uname(JNIEnv* env, jobject) {
1324 struct utsname buf;
Elliott Hughes9b510df2011-05-27 16:48:46 -07001325 if (TEMP_FAILURE_RETRY(uname(&buf)) == -1) {
1326 return NULL; // Can't happen.
Elliott Hughes7341b9e2011-03-24 13:51:38 -07001327 }
1328 return makeStructUtsname(env, buf);
1329}
1330
Elliott Hughes895a6132012-11-05 15:44:56 -08001331static 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
Elliott Hughes9e67ca72011-05-19 15:25:10 -07001339static 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
Elliott Hughes0568a632011-04-25 18:33:06 -07001349static jint Posix_writeBytes(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount) {
1350 ScopedBytesRO bytes(env, javaBytes);
Elliott Hughes78c7cc52011-03-25 19:18:42 -07001351 if (bytes.get() == NULL) {
1352 return -1;
1353 }
Elliott Hughes78c7cc52011-03-25 19:18:42 -07001354 int fd = jniGetFDFromFileDescriptor(env, javaFd);
Elliott Hughes0568a632011-04-25 18:33:06 -07001355 return throwIfMinusOne(env, "write", TEMP_FAILURE_RETRY(write(fd, bytes.get() + byteOffset, byteCount)));
Elliott Hughes78c7cc52011-03-25 19:18:42 -07001356}
1357
Elliott Hughesbbac92e2011-03-28 19:57:00 -07001358static jint Posix_writev(JNIEnv* env, jobject, jobject javaFd, jobjectArray buffers, jintArray offsets, jintArray byteCounts) {
Elliott Hughes8dc75472011-04-26 10:10:46 -07001359 IoVec<ScopedBytesRO> ioVec(env, env->GetArrayLength(buffers));
Elliott Hughesbbac92e2011-03-28 19:57:00 -07001360 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
Elliott Hughesec617e22011-03-16 16:42:06 -07001367static JNINativeMethod gMethods[] = {
Elliott Hughes553d98a2011-05-19 13:50:09 -07001368 NATIVE_METHOD(Posix, accept, "(Ljava/io/FileDescriptor;Ljava/net/InetSocketAddress;)Ljava/io/FileDescriptor;"),
Elliott Hughesddfdbb92011-03-16 19:09:22 -07001369 NATIVE_METHOD(Posix, access, "(Ljava/lang/String;I)Z"),
Elliott Hughesda150092011-05-09 14:00:48 -07001370 NATIVE_METHOD(Posix, bind, "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)V"),
Elliott Hughesb7190192011-03-23 17:14:34 -07001371 NATIVE_METHOD(Posix, chmod, "(Ljava/lang/String;I)V"),
Elliott Hughes44f78752012-08-16 12:29:52 -07001372 NATIVE_METHOD(Posix, chown, "(Ljava/lang/String;II)V"),
Elliott Hughes462bdac2011-03-30 10:43:48 -07001373 NATIVE_METHOD(Posix, close, "(Ljava/io/FileDescriptor;)V"),
Elliott Hughes996bf792011-05-09 18:26:23 -07001374 NATIVE_METHOD(Posix, connect, "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)V"),
Elliott Hughes396a9c62011-05-12 15:05:54 -07001375 NATIVE_METHOD(Posix, dup, "(Ljava/io/FileDescriptor;)Ljava/io/FileDescriptor;"),
1376 NATIVE_METHOD(Posix, dup2, "(Ljava/io/FileDescriptor;I)Ljava/io/FileDescriptor;"),
Elliott Hughesddfdbb92011-03-16 19:09:22 -07001377 NATIVE_METHOD(Posix, environ, "()[Ljava/lang/String;"),
Nick Kralevich798f8552013-02-05 13:02:13 -08001378 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"),
Elliott Hughes44f78752012-08-16 12:29:52 -07001380 NATIVE_METHOD(Posix, fchmod, "(Ljava/io/FileDescriptor;I)V"),
1381 NATIVE_METHOD(Posix, fchown, "(Ljava/io/FileDescriptor;II)V"),
Elliott Hughesfc549a02011-03-24 16:38:05 -07001382 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"),
Elliott Hughes52724d32011-03-17 12:13:09 -07001385 NATIVE_METHOD(Posix, fdatasync, "(Ljava/io/FileDescriptor;)V"),
Elliott Hughes47cb3382011-03-17 18:28:00 -07001386 NATIVE_METHOD(Posix, fstat, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStat;"),
Elliott Hughes59fa7162011-03-23 14:55:04 -07001387 NATIVE_METHOD(Posix, fstatfs, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStatFs;"),
Elliott Hughes52724d32011-03-17 12:13:09 -07001388 NATIVE_METHOD(Posix, fsync, "(Ljava/io/FileDescriptor;)V"),
Elliott Hughesf5333fd2011-03-18 14:13:08 -07001389 NATIVE_METHOD(Posix, ftruncate, "(Ljava/io/FileDescriptor;J)V"),
Elliott Hughes4f11ebe2011-04-19 15:38:10 -07001390 NATIVE_METHOD(Posix, gai_strerror, "(I)Ljava/lang/String;"),
Elliott Hughes1c039d72011-05-04 16:04:04 -07001391 NATIVE_METHOD(Posix, getaddrinfo, "(Ljava/lang/String;Llibcore/io/StructAddrinfo;)[Ljava/net/InetAddress;"),
Elliott Hughes396a9c62011-05-12 15:05:54 -07001392 NATIVE_METHOD(Posix, getegid, "()I"),
1393 NATIVE_METHOD(Posix, geteuid, "()I"),
1394 NATIVE_METHOD(Posix, getgid, "()I"),
Elliott Hughesec617e22011-03-16 16:42:06 -07001395 NATIVE_METHOD(Posix, getenv, "(Ljava/lang/String;)Ljava/lang/String;"),
Elliott Hughes4f11ebe2011-04-19 15:38:10 -07001396 NATIVE_METHOD(Posix, getnameinfo, "(Ljava/net/InetAddress;I)Ljava/lang/String;"),
Elliott Hughes482a3fc2013-03-20 16:10:12 -07001397 NATIVE_METHOD(Posix, getpeername, "(Ljava/io/FileDescriptor;)Ljava/net/SocketAddress;"),
Elliott Hughes396a9c62011-05-12 15:05:54 -07001398 NATIVE_METHOD(Posix, getpid, "()I"),
1399 NATIVE_METHOD(Posix, getppid, "()I"),
Elliott Hughesd4419fc2011-05-12 17:22:07 -07001400 NATIVE_METHOD(Posix, getpwnam, "(Ljava/lang/String;)Llibcore/io/StructPasswd;"),
1401 NATIVE_METHOD(Posix, getpwuid, "(I)Llibcore/io/StructPasswd;"),
Elliott Hughes0a9d1ee2011-03-30 15:35:16 -07001402 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;"),
Elliott Hughes482a3fc2013-03-20 16:10:12 -07001408 NATIVE_METHOD(Posix, getsockoptUcred, "(Ljava/io/FileDescriptor;II)Llibcore/io/StructUcred;"),
Elliott Hughes396a9c62011-05-12 15:05:54 -07001409 NATIVE_METHOD(Posix, getuid, "()I"),
Elliott Hughesa37e9712011-04-21 14:07:01 -07001410 NATIVE_METHOD(Posix, if_indextoname, "(I)Ljava/lang/String;"),
Elliott Hughesfc041ff2011-06-03 12:43:17 -07001411 NATIVE_METHOD(Posix, inet_pton, "(ILjava/lang/String;)Ljava/net/InetAddress;"),
Elliott Hughesa37e9712011-04-21 14:07:01 -07001412 NATIVE_METHOD(Posix, ioctlInetAddress, "(Ljava/io/FileDescriptor;ILjava/lang/String;)Ljava/net/InetAddress;"),
Elliott Hughes8b15dcc2011-03-29 14:26:04 -07001413 NATIVE_METHOD(Posix, ioctlInt, "(Ljava/io/FileDescriptor;ILlibcore/util/MutableInt;)I"),
Elliott Hughes9a3f3632011-03-18 16:30:53 -07001414 NATIVE_METHOD(Posix, isatty, "(Ljava/io/FileDescriptor;)Z"),
Elliott Hughesa5fb7062011-04-28 15:36:47 -07001415 NATIVE_METHOD(Posix, kill, "(II)V"),
Elliott Hughes44f78752012-08-16 12:29:52 -07001416 NATIVE_METHOD(Posix, lchown, "(Ljava/lang/String;II)V"),
Elliott Hughese1502d62011-03-24 11:57:08 -07001417 NATIVE_METHOD(Posix, listen, "(Ljava/io/FileDescriptor;I)V"),
Elliott Hughesdedaccd2011-03-18 15:56:20 -07001418 NATIVE_METHOD(Posix, lseek, "(Ljava/io/FileDescriptor;JI)J"),
Elliott Hughes47cb3382011-03-17 18:28:00 -07001419 NATIVE_METHOD(Posix, lstat, "(Ljava/lang/String;)Llibcore/io/StructStat;"),
Elliott Hughes0f746ff52011-03-22 16:10:21 -07001420 NATIVE_METHOD(Posix, mincore, "(JJ[B)V"),
Elliott Hughesc7fa2072011-03-23 17:37:58 -07001421 NATIVE_METHOD(Posix, mkdir, "(Ljava/lang/String;I)V"),
Elliott Hughes7e13c0f2011-03-18 18:52:08 -07001422 NATIVE_METHOD(Posix, mlock, "(JJ)V"),
Elliott Hughes7e25eff2011-03-18 18:39:29 -07001423 NATIVE_METHOD(Posix, mmap, "(JJIILjava/io/FileDescriptor;J)J"),
1424 NATIVE_METHOD(Posix, msync, "(JJI)V"),
Elliott Hughes7e13c0f2011-03-18 18:52:08 -07001425 NATIVE_METHOD(Posix, munlock, "(JJ)V"),
Elliott Hughes7e25eff2011-03-18 18:39:29 -07001426 NATIVE_METHOD(Posix, munmap, "(JJ)V"),
Elliott Hughes0ac77ac2011-03-22 18:45:25 -07001427 NATIVE_METHOD(Posix, open, "(Ljava/lang/String;II)Ljava/io/FileDescriptor;"),
Elliott Hughes41f06052011-03-24 10:48:09 -07001428 NATIVE_METHOD(Posix, pipe, "()[Ljava/io/FileDescriptor;"),
Elliott Hughes70c82042011-05-11 16:12:36 -07001429 NATIVE_METHOD(Posix, poll, "([Llibcore/io/StructPollfd;I)I"),
Elliott Hughese3b6fa22011-06-09 15:30:18 -07001430 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"),
Elliott Hughes0568a632011-04-25 18:33:06 -07001432 NATIVE_METHOD(Posix, readBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"),
Elliott Hughesbbac92e2011-03-28 19:57:00 -07001433 NATIVE_METHOD(Posix, readv, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"),
Elliott Hughes23ec0912011-05-18 18:21:17 -07001434 NATIVE_METHOD(Posix, recvfromBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIILjava/net/InetSocketAddress;)I"),
Elliott Hughesc7fa2072011-03-23 17:37:58 -07001435 NATIVE_METHOD(Posix, remove, "(Ljava/lang/String;)V"),
Elliott Hughesa20cc6f2011-03-23 13:49:03 -07001436 NATIVE_METHOD(Posix, rename, "(Ljava/lang/String;Ljava/lang/String;)V"),
Elliott Hughes8b15dcc2011-03-29 14:26:04 -07001437 NATIVE_METHOD(Posix, sendfile, "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Llibcore/util/MutableLong;J)J"),
Elliott Hughes90d96a42011-05-16 16:43:48 -07001438 NATIVE_METHOD(Posix, sendtoBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIILjava/net/InetAddress;I)I"),
Elliott Hughes396a9c62011-05-12 15:05:54 -07001439 NATIVE_METHOD(Posix, setegid, "(I)V"),
Elliott Hughes895a6132012-11-05 15:44:56 -08001440 NATIVE_METHOD(Posix, setenv, "(Ljava/lang/String;Ljava/lang/String;Z)V"),
Elliott Hughes396a9c62011-05-12 15:05:54 -07001441 NATIVE_METHOD(Posix, seteuid, "(I)V"),
1442 NATIVE_METHOD(Posix, setgid, "(I)V"),
Elliott Hughes6c9b5372012-09-21 10:19:21 -07001443 NATIVE_METHOD(Posix, setsid, "()I"),
Elliott Hughesc63f0d42011-04-01 15:31:03 -07001444 NATIVE_METHOD(Posix, setsockoptByte, "(Ljava/io/FileDescriptor;III)V"),
Elliott Hughesb9746662011-04-06 12:01:16 -07001445 NATIVE_METHOD(Posix, setsockoptIfreq, "(Ljava/io/FileDescriptor;IILjava/lang/String;)V"),
Elliott Hughes454a95f2011-03-29 18:00:14 -07001446 NATIVE_METHOD(Posix, setsockoptInt, "(Ljava/io/FileDescriptor;III)V"),
Elliott Hughesb9746662011-04-06 12:01:16 -07001447 NATIVE_METHOD(Posix, setsockoptIpMreqn, "(Ljava/io/FileDescriptor;III)V"),
Elliott Hughes438cb9e2011-04-01 18:05:23 -07001448 NATIVE_METHOD(Posix, setsockoptGroupReq, "(Ljava/io/FileDescriptor;IILlibcore/io/StructGroupReq;)V"),
Elliott Hughesc63f0d42011-04-01 15:31:03 -07001449 NATIVE_METHOD(Posix, setsockoptLinger, "(Ljava/io/FileDescriptor;IILlibcore/io/StructLinger;)V"),
1450 NATIVE_METHOD(Posix, setsockoptTimeval, "(Ljava/io/FileDescriptor;IILlibcore/io/StructTimeval;)V"),
Elliott Hughes396a9c62011-05-12 15:05:54 -07001451 NATIVE_METHOD(Posix, setuid, "(I)V"),
Elliott Hughes59e47442011-03-24 11:36:07 -07001452 NATIVE_METHOD(Posix, shutdown, "(Ljava/io/FileDescriptor;I)V"),
Elliott Hughes454a95f2011-03-29 18:00:14 -07001453 NATIVE_METHOD(Posix, socket, "(III)Ljava/io/FileDescriptor;"),
Elliott Hughes32180822012-09-28 15:53:29 -07001454 NATIVE_METHOD(Posix, socketpair, "(IIILjava/io/FileDescriptor;Ljava/io/FileDescriptor;)V"),
Elliott Hughes47cb3382011-03-17 18:28:00 -07001455 NATIVE_METHOD(Posix, stat, "(Ljava/lang/String;)Llibcore/io/StructStat;"),
Elliott Hughes59fa7162011-03-23 14:55:04 -07001456 NATIVE_METHOD(Posix, statfs, "(Ljava/lang/String;)Llibcore/io/StructStatFs;"),
Elliott Hughesddfdbb92011-03-16 19:09:22 -07001457 NATIVE_METHOD(Posix, strerror, "(I)Ljava/lang/String;"),
Elliott Hughes763f8ed2013-03-28 18:26:05 -07001458 NATIVE_METHOD(Posix, strsignal, "(I)Ljava/lang/String;"),
Elliott Hughesa20cc6f2011-03-23 13:49:03 -07001459 NATIVE_METHOD(Posix, symlink, "(Ljava/lang/String;Ljava/lang/String;)V"),
Elliott Hughes6fc1a0e2011-03-18 10:28:24 -07001460 NATIVE_METHOD(Posix, sysconf, "(I)J"),
Johan Redestigbb5816a2012-01-10 17:48:01 +09001461 NATIVE_METHOD(Posix, tcdrain, "(Ljava/io/FileDescriptor;)V"),
Elliott Hughes039f7592012-12-14 16:19:24 -08001462 NATIVE_METHOD(Posix, tcsendbreak, "(Ljava/io/FileDescriptor;I)V"),
Nick Kralevichc79d3ef2012-09-27 13:40:04 -07001463 NATIVE_METHOD(Posix, umaskImpl, "(I)I"),
Elliott Hughes7341b9e2011-03-24 13:51:38 -07001464 NATIVE_METHOD(Posix, uname, "()Llibcore/io/StructUtsname;"),
Elliott Hughes895a6132012-11-05 15:44:56 -08001465 NATIVE_METHOD(Posix, unsetenv, "(Ljava/lang/String;)V"),
Elliott Hughes9e67ca72011-05-19 15:25:10 -07001466 NATIVE_METHOD(Posix, waitpid, "(ILlibcore/util/MutableInt;I)I"),
Elliott Hughes0568a632011-04-25 18:33:06 -07001467 NATIVE_METHOD(Posix, writeBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;II)I"),
Elliott Hughesbbac92e2011-03-28 19:57:00 -07001468 NATIVE_METHOD(Posix, writev, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"),
Elliott Hughesec617e22011-03-16 16:42:06 -07001469};
Elliott Hughes7cd67602012-05-03 17:21:04 -07001470void register_libcore_io_Posix(JNIEnv* env) {
1471 jniRegisterNativeMethods(env, "libcore/io/Posix", gMethods, NELEM(gMethods));
Elliott Hughesec617e22011-03-16 16:42:06 -07001472}