diff options
author | Elliott Hughes <enh@google.com> | 2014-08-26 14:14:22 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2014-09-02 12:07:12 -0700 |
commit | f6cf9efb212e572dcd2e902ca461af6323793dbf (patch) | |
tree | 2d32d8eb09820f43c11257693113de584313e706 | |
parent | 0d9d721694762218aeb26472a208057d72efc97c (diff) | |
download | libcore-f6cf9efb212e572dcd2e902ca461af6323793dbf.zip libcore-f6cf9efb212e572dcd2e902ca461af6323793dbf.tar.gz libcore-f6cf9efb212e572dcd2e902ca461af6323793dbf.tar.bz2 |
Keep qtaguid quiet by not trying to untag non-sockets.
Bug: 17203955
Change-Id: I0999fc0ff295986b92e31568d96e321b9e7ffb2c
-rw-r--r-- | luni/src/main/java/java/io/FileDescriptor.java | 9 | ||||
-rw-r--r-- | luni/src/main/java/libcore/io/BlockGuardOs.java | 9 | ||||
-rw-r--r-- | luni/src/main/native/Register.cpp | 1 | ||||
-rw-r--r-- | luni/src/main/native/java_io_FileDescriptor.cpp | 35 | ||||
-rw-r--r-- | luni/src/main/native/sub.mk | 1 |
5 files changed, 51 insertions, 4 deletions
diff --git a/luni/src/main/java/java/io/FileDescriptor.java b/luni/src/main/java/java/io/FileDescriptor.java index cb38123..eba0e4d 100644 --- a/luni/src/main/java/java/io/FileDescriptor.java +++ b/luni/src/main/java/java/io/FileDescriptor.java @@ -105,6 +105,15 @@ public final class FileDescriptor { this.descriptor = fd; } + /** + * @hide internal use only + */ + public boolean isSocket() { + return isSocket(descriptor); + } + + private static native boolean isSocket(int fd); + @Override public String toString() { return "FileDescriptor[" + descriptor + "]"; } diff --git a/luni/src/main/java/libcore/io/BlockGuardOs.java b/luni/src/main/java/libcore/io/BlockGuardOs.java index 764e60a..b3dc74b 100644 --- a/luni/src/main/java/libcore/io/BlockGuardOs.java +++ b/luni/src/main/java/libcore/io/BlockGuardOs.java @@ -80,17 +80,18 @@ public class BlockGuardOs extends ForwardingOs { @Override public void close(FileDescriptor fd) throws ErrnoException { try { - // The usual case is that this _isn't_ a socket, so getsockopt will throw, - // and that's really expensive. Try to avoid asking if we don't care. - if ((BlockGuard.getThreadPolicy().getPolicyMask() & DISALLOW_NETWORK) != 0) { + // The usual case is that this _isn't_ a socket, so the getsockopt(2) call in + // isLingerSocket will throw, and that's really expensive. Try to avoid asking + // if we don't care. + if (fd.isSocket()) { if (isLingerSocket(fd)) { // If the fd is a socket with SO_LINGER set, we might block indefinitely. // We allow non-linger sockets so that apps can close their network // connections in methods like onDestroy which will run on the UI thread. BlockGuard.getThreadPolicy().onNetwork(); } + untagSocket(fd); } - untagSocket(fd); } catch (ErrnoException ignored) { // We're called via Socket.close (which doesn't ask for us to be called), so we // must not throw here, because Socket.close must not throw if asked to close an diff --git a/luni/src/main/native/Register.cpp b/luni/src/main/native/Register.cpp index 4aaa905..6a2c939 100644 --- a/luni/src/main/native/Register.cpp +++ b/luni/src/main/native/Register.cpp @@ -35,6 +35,7 @@ jint JNI_OnLoad(JavaVM* vm, void*) { #define REGISTER(FN) extern void FN(JNIEnv*); FN(env) REGISTER(register_android_system_OsConstants); REGISTER(register_java_io_File); + REGISTER(register_java_io_FileDescriptor); REGISTER(register_java_io_ObjectStreamClass); REGISTER(register_java_lang_Character); REGISTER(register_java_lang_Double); diff --git a/luni/src/main/native/java_io_FileDescriptor.cpp b/luni/src/main/native/java_io_FileDescriptor.cpp new file mode 100644 index 0000000..fe7e07e --- /dev/null +++ b/luni/src/main/native/java_io_FileDescriptor.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "FileDescriptor" + +#include "JniConstants.h" + +#include <sys/socket.h> +#include <sys/types.h> + +static jboolean FileDescriptor_isSocket(JNIEnv*, jclass, jint fd) { + int error; + socklen_t error_length = sizeof(error); + return TEMP_FAILURE_RETRY(getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &error_length)); +} + +static JNINativeMethod gMethods[] = { + NATIVE_METHOD(FileDescriptor, isSocket, "(I)Z"), +}; +void register_java_io_FileDescriptor(JNIEnv* env) { + jniRegisterNativeMethods(env, "java/io/FileDescriptor", gMethods, NELEM(gMethods)); +} diff --git a/luni/src/main/native/sub.mk b/luni/src/main/native/sub.mk index 5b581f3..079ecd2 100644 --- a/luni/src/main/native/sub.mk +++ b/luni/src/main/native/sub.mk @@ -15,6 +15,7 @@ LOCAL_SRC_FILES := \ canonicalize_path.cpp \ cbigint.cpp \ java_io_File.cpp \ + java_io_FileDescriptor.cpp \ java_io_ObjectStreamClass.cpp \ java_lang_Character.cpp \ java_lang_Double.cpp \ |