System: Close log sockets prior to a fork.

Calling __android_log_close sometimes isn't sufficient because
some libraries statically link in liblog. This may not be a complete
or a clean change, but it's sufficient to get whitelisting working
on Lollipop and Marshmallow.

bug: 30963384
Change-Id: I38cdfc17e9d942417ca57154776bd28e5b78b807
diff --git a/dalvik/src/main/java/dalvik/system/ZygoteHooks.java b/dalvik/src/main/java/dalvik/system/ZygoteHooks.java
index 11ea286..68013ed 100644
--- a/dalvik/src/main/java/dalvik/system/ZygoteHooks.java
+++ b/dalvik/src/main/java/dalvik/system/ZygoteHooks.java
@@ -38,6 +38,7 @@
         Daemons.stop();
         waitUntilAllThreadsStopped();
         token = nativePreFork();
+        System.closeLogSockets();
     }
 
     /**
diff --git a/luni/src/main/java/java/lang/System.java b/luni/src/main/java/java/lang/System.java
index 55ca762..f55cd74 100644
--- a/luni/src/main/java/java/lang/System.java
+++ b/luni/src/main/java/java/lang/System.java
@@ -1101,6 +1101,9 @@
         }
     }
 
+    /** @hide */
+    public static native void closeLogSockets();
+
     /**
      * Returns the platform specific file name format for the shared library
      * named by the argument. On Android, this would turn {@code "MyLibrary"} into
diff --git a/luni/src/main/native/java_lang_System.cpp b/luni/src/main/native/java_lang_System.cpp
index 944c0c3..fa1c1f3 100644
--- a/luni/src/main/native/java_lang_System.cpp
+++ b/luni/src/main/native/java_lang_System.cpp
@@ -62,6 +62,10 @@
     }
 }
 
+static void System_closeLogSockets(JNIEnv*, jclass) {
+  __android_log_close();
+}
+
 // Sets a field via JNI. Used for the standard streams, which are read-only otherwise.
 static void System_setFieldImpl(JNIEnv* env, jclass clazz,
         jstring javaName, jstring javaSignature, jobject object) {
@@ -133,6 +137,7 @@
 }
 
 static JNINativeMethod gMethods[] = {
+    NATIVE_METHOD(System, closeLogSockets, "()V"),
     NATIVE_METHOD(System, currentTimeMillis, "!()J"),
     NATIVE_METHOD(System, log, "(CLjava/lang/String;Ljava/lang/Throwable;)V"),
     NATIVE_METHOD(System, mapLibraryName, "(Ljava/lang/String;)Ljava/lang/String;"),