diff options
author | Elliott Hughes <enh@google.com> | 2011-04-08 14:10:28 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2011-04-08 15:01:19 -0700 |
commit | 69a017bc1d1649350f830dfada5c6ed5eac0b770 (patch) | |
tree | 6ecc6d9658272b268ce931d417930e2ea1bfa3f7 /core/jni/android_net_LocalSocketImpl.cpp | |
parent | 5008e92d1fd573d926cd55c39ca723a6fbdf7c4b (diff) | |
download | frameworks_base-69a017bc1d1649350f830dfada5c6ed5eac0b770.zip frameworks_base-69a017bc1d1649350f830dfada5c6ed5eac0b770.tar.gz frameworks_base-69a017bc1d1649350f830dfada5c6ed5eac0b770.tar.bz2 |
More JNI exception-throwing cleanup.
There are a few (unimportant) bug fixes here. There were several attempts to
throw exceptions in situations where there's already a pending exception.
There were also cases where the code was wrong; it was checking for a NULL
return from Get*ArrayElements and throwing NPE, but passing NULL is an error
that causes a crash and a NULL return means an exception has already been
thrown. I didn't want to get into the Scoped* classes just yet, but that
was by far the easiest way to fix this.
Change-Id: I0b31160ee51b96e82539f6514b8412b149dba7c3
Diffstat (limited to 'core/jni/android_net_LocalSocketImpl.cpp')
-rw-r--r-- | core/jni/android_net_LocalSocketImpl.cpp | 88 |
1 files changed, 44 insertions, 44 deletions
diff --git a/core/jni/android_net_LocalSocketImpl.cpp b/core/jni/android_net_LocalSocketImpl.cpp index e58794b..c8add70 100644 --- a/core/jni/android_net_LocalSocketImpl.cpp +++ b/core/jni/android_net_LocalSocketImpl.cpp @@ -99,9 +99,9 @@ socket_connect_local(JNIEnv *env, jobject object, #define DEFAULT_BACKLOG 4 -/* private native void bindLocal(FileDescriptor fd, String name, namespace) - * throws IOException; - */ +/* private native void bindLocal(FileDescriptor fd, String name, namespace) + * throws IOException; + */ static void socket_bind_local (JNIEnv *env, jobject object, jobject fileDescriptor, @@ -113,7 +113,7 @@ socket_bind_local (JNIEnv *env, jobject object, jobject fileDescriptor, if (name == NULL) { - jniThrowException(env, "java/lang/NullPointerException", NULL); + jniThrowNullPointerException(env, NULL); } fd = jniGetFDFromFileDescriptor(env, fileDescriptor); @@ -127,7 +127,7 @@ socket_bind_local (JNIEnv *env, jobject object, jobject fileDescriptor, ret = socket_local_server_bind(fd, nameUtf8, namespaceId); env->ReleaseStringUTFChars(name, nameUtf8); - + if (ret < 0) { jniThrowIOException(env, errno); return; @@ -166,14 +166,14 @@ socket_accept (JNIEnv *env, jobject object, jobject fileDescriptor, jobject s) struct sockaddr address; struct sockaddr_un un_address; } sa; - + int ret; int retFD; int fd; socklen_t addrlen; if (s == NULL) { - jniThrowException(env, "java/lang/NullPointerException", NULL); + jniThrowNullPointerException(env, NULL); return NULL; } @@ -345,7 +345,7 @@ static void socket_setOption( struct timeval timeout; timeout.tv_sec = intValue / 1000; timeout.tv_usec = (intValue % 1000) * 1000; - + ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout, sizeof(timeout)); @@ -353,7 +353,7 @@ static void socket_setOption( ret = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (void *)&timeout, sizeof(timeout)); } - + break; } default: { @@ -372,7 +372,7 @@ static void socket_setOption( } } -static jint socket_available (JNIEnv *env, jobject object, +static jint socket_available (JNIEnv *env, jobject object, jobject fileDescriptor) { int fd; @@ -398,7 +398,7 @@ static jint socket_available (JNIEnv *env, jobject object, return (jint)avail; #else // there appears to be a bionic bug that prevents this version from working. - + ssize_t ret; struct msghdr msg; @@ -408,7 +408,7 @@ static jint socket_available (JNIEnv *env, jobject object, ret = recvmsg(fd, &msg, MSG_PEEK | MSG_DONTWAIT | MSG_NOSIGNAL); } while (ret < 0 && errno == EINTR); - + // MSG_PEEK returns 0 on EOF and EWOULDBLOCK on none available if (ret < 0 && errno == EWOULDBLOCK) { return 0; @@ -427,7 +427,7 @@ static void socket_close (JNIEnv *env, jobject object, jobject fileDescriptor) int err; if (fileDescriptor == NULL) { - jniThrowException(env, "java/lang/NullPointerException", NULL); + jniThrowNullPointerException(env, NULL); return; } @@ -448,7 +448,7 @@ static void socket_close (JNIEnv *env, jobject object, jobject fileDescriptor) } /** - * Processes ancillary data, handling only + * Processes ancillary data, handling only * SCM_RIGHTS. Creates appropriate objects and sets appropriate * fields in the LocalSocketImpl object. Returns 0 on success * or -1 if an exception was thrown. @@ -457,7 +457,7 @@ static int socket_process_cmsg(JNIEnv *env, jobject thisJ, struct msghdr * pMsg) { struct cmsghdr *cmsgptr; - for (cmsgptr = CMSG_FIRSTHDR(pMsg); + for (cmsgptr = CMSG_FIRSTHDR(pMsg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(pMsg, cmsgptr)) { if (cmsgptr->cmsg_level != SOL_SOCKET) { @@ -467,11 +467,11 @@ static int socket_process_cmsg(JNIEnv *env, jobject thisJ, struct msghdr * pMsg) if (cmsgptr->cmsg_type == SCM_RIGHTS) { int *pDescriptors = (int *)CMSG_DATA(cmsgptr); jobjectArray fdArray; - int count + int count = ((cmsgptr->cmsg_len - CMSG_LEN(0)) / sizeof(int)); if (count < 0) { - jniThrowException(env, "java/io/IOException", + jniThrowException(env, "java/io/IOException", "invalid cmsg length"); } @@ -482,7 +482,7 @@ static int socket_process_cmsg(JNIEnv *env, jobject thisJ, struct msghdr * pMsg) } for (int i = 0; i < count; i++) { - jobject fdObject + jobject fdObject = jniCreateFileDescriptor(env, pDescriptors[i]); if (env->ExceptionOccurred() != NULL) { @@ -514,7 +514,7 @@ static int socket_process_cmsg(JNIEnv *env, jobject thisJ, struct msghdr * pMsg) * Returns the length of normal data read, or -1 if an exception has * been thrown in this function. */ -static ssize_t socket_read_all(JNIEnv *env, jobject thisJ, int fd, +static ssize_t socket_read_all(JNIEnv *env, jobject thisJ, int fd, void *buffer, size_t len) { ssize_t ret; @@ -554,7 +554,7 @@ static ssize_t socket_read_all(JNIEnv *env, jobject thisJ, int fd, if ((msg.msg_flags & (MSG_CTRUNC | MSG_OOB | MSG_ERRQUEUE)) != 0) { // To us, any of the above flags are a fatal error - jniThrowException(env, "java/io/IOException", + jniThrowException(env, "java/io/IOException", "Unexpected error or truncation during recvmsg()"); return -1; @@ -580,7 +580,7 @@ static int socket_write_all(JNIEnv *env, jobject object, int fd, unsigned char *buffer = (unsigned char *)buf; memset(&msg, 0, sizeof(msg)); - jobjectArray outboundFds + jobjectArray outboundFds = (jobjectArray)env->GetObjectField( object, field_outboundFileDescriptors); @@ -632,7 +632,7 @@ static int socket_write_all(JNIEnv *env, jobject object, int fd, msg.msg_iov = &iv; msg.msg_iovlen = 1; - + do { ret = sendmsg(fd, &msg, MSG_NOSIGNAL); } while (ret < 0 && errno == EINTR); @@ -658,7 +658,7 @@ static jint socket_read (JNIEnv *env, jobject object, jobject fileDescriptor) int err; if (fileDescriptor == NULL) { - jniThrowException(env, "java/lang/NullPointerException", NULL); + jniThrowNullPointerException(env, NULL); return (jint)-1; } @@ -685,7 +685,7 @@ static jint socket_read (JNIEnv *env, jobject object, jobject fileDescriptor) return (jint)buf; } -static jint socket_readba (JNIEnv *env, jobject object, +static jint socket_readba (JNIEnv *env, jobject object, jbyteArray buffer, jint off, jint len, jobject fileDescriptor) { int fd; @@ -693,7 +693,7 @@ static jint socket_readba (JNIEnv *env, jobject object, int ret; if (fileDescriptor == NULL || buffer == NULL) { - jniThrowException(env, "java/lang/NullPointerException", NULL); + jniThrowNullPointerException(env, NULL); return (jint)-1; } @@ -720,7 +720,7 @@ static jint socket_readba (JNIEnv *env, jobject object, return (jint)-1; } - ret = socket_read_all(env, object, + ret = socket_read_all(env, object, fd, byteBuffer + off, len); // A return of -1 above means an exception is pending @@ -730,14 +730,14 @@ static jint socket_readba (JNIEnv *env, jobject object, return (jint) ((ret == 0) ? -1 : ret); } -static void socket_write (JNIEnv *env, jobject object, +static void socket_write (JNIEnv *env, jobject object, jint b, jobject fileDescriptor) { int fd; int err; if (fileDescriptor == NULL) { - jniThrowException(env, "java/lang/NullPointerException", NULL); + jniThrowNullPointerException(env, NULL); return; } @@ -752,7 +752,7 @@ static void socket_write (JNIEnv *env, jobject object, // A return of -1 above means an exception is pending } -static void socket_writeba (JNIEnv *env, jobject object, +static void socket_writeba (JNIEnv *env, jobject object, jbyteArray buffer, jint off, jint len, jobject fileDescriptor) { int fd; @@ -760,7 +760,7 @@ static void socket_writeba (JNIEnv *env, jobject object, jbyte* byteBuffer; if (fileDescriptor == NULL || buffer == NULL) { - jniThrowException(env, "java/lang/NullPointerException", NULL); + jniThrowNullPointerException(env, NULL); return; } @@ -782,7 +782,7 @@ static void socket_writeba (JNIEnv *env, jobject object, return; } - err = socket_write_all(env, object, fd, + err = socket_write_all(env, object, fd, byteBuffer + off, len); // A return of -1 above means an exception is pending @@ -790,14 +790,14 @@ static void socket_writeba (JNIEnv *env, jobject object, env->ReleaseByteArrayElements(buffer, byteBuffer, JNI_ABORT); } -static jobject socket_get_peer_credentials(JNIEnv *env, +static jobject socket_get_peer_credentials(JNIEnv *env, jobject object, jobject fileDescriptor) { int err; int fd; if (fileDescriptor == NULL) { - jniThrowException(env, "java/lang/NullPointerException", NULL); + jniThrowNullPointerException(env, NULL); return NULL; } @@ -812,7 +812,7 @@ static jobject socket_get_peer_credentials(JNIEnv *env, memset(&creds, 0, sizeof(creds)); socklen_t szCreds = sizeof(creds); - err = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &creds, &szCreds); + err = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &creds, &szCreds); if (err < 0) { jniThrowIOException(env, errno); @@ -823,20 +823,20 @@ static jobject socket_get_peer_credentials(JNIEnv *env, return NULL; } - return env->NewObject(class_Credentials, method_CredentialsInit, + return env->NewObject(class_Credentials, method_CredentialsInit, creds.pid, creds.uid, creds.gid); } #if 0 //TODO change this to return an instance of LocalSocketAddress -static jobject socket_getSockName(JNIEnv *env, +static jobject socket_getSockName(JNIEnv *env, jobject object, jobject fileDescriptor) { int err; int fd; if (fileDescriptor == NULL) { - jniThrowException(env, "java/lang/NullPointerException", NULL); + jniThrowNullPointerException(env, NULL); return NULL; } @@ -898,10 +898,10 @@ static JNINativeMethod gMethods[] = { {"readba_native", "([BIILjava/io/FileDescriptor;)I", (void*) socket_readba}, {"writeba_native", "([BIILjava/io/FileDescriptor;)V", (void*) socket_writeba}, {"write_native", "(ILjava/io/FileDescriptor;)V", (void*) socket_write}, - {"getPeerCredentials_native", - "(Ljava/io/FileDescriptor;)Landroid/net/Credentials;", + {"getPeerCredentials_native", + "(Ljava/io/FileDescriptor;)Landroid/net/Credentials;", (void*) socket_get_peer_credentials} - //,{"getSockName_native", "(Ljava/io/FileDescriptor;)Ljava/lang/String;", + //,{"getSockName_native", "(Ljava/io/FileDescriptor;)Ljava/lang/String;", // (void *) socket_getSockName} }; @@ -916,14 +916,14 @@ int register_android_net_LocalSocketImpl(JNIEnv *env) goto error; } - field_inboundFileDescriptors = env->GetFieldID(clazz, + field_inboundFileDescriptors = env->GetFieldID(clazz, "inboundFileDescriptors", "[Ljava/io/FileDescriptor;"); if (field_inboundFileDescriptors == NULL) { goto error; } - field_outboundFileDescriptors = env->GetFieldID(clazz, + field_outboundFileDescriptors = env->GetFieldID(clazz, "outboundFileDescriptors", "[Ljava/io/FileDescriptor;"); if (field_outboundFileDescriptors == NULL) { @@ -931,7 +931,7 @@ int register_android_net_LocalSocketImpl(JNIEnv *env) } class_Credentials = env->FindClass("android/net/Credentials"); - + if (class_Credentials == NULL) { goto error; } @@ -946,7 +946,7 @@ int register_android_net_LocalSocketImpl(JNIEnv *env) class_FileDescriptor = (jclass)env->NewGlobalRef(class_FileDescriptor); - method_CredentialsInit + method_CredentialsInit = env->GetMethodID(class_Credentials, "<init>", "(III)V"); if (method_CredentialsInit == NULL) { |