Reimplement reopenStdio in Java.

Bug: 18719692
Change-Id: I4ed984cd6148c5cba75d46a60f32a0687a5bcac3
diff --git a/core/java/com/android/internal/os/ZygoteConnection.java b/core/java/com/android/internal/os/ZygoteConnection.java
index 2ad1321..e980acb 100644
--- a/core/java/com/android/internal/os/ZygoteConnection.java
+++ b/core/java/com/android/internal/os/ZygoteConnection.java
@@ -17,6 +17,9 @@
 package com.android.internal.os;
 
 import static android.system.OsConstants.O_CLOEXEC;
+import static android.system.OsConstants.STDERR_FILENO;
+import static android.system.OsConstants.STDIN_FILENO;
+import static android.system.OsConstants.STDOUT_FILENO;
 
 import android.net.Credentials;
 import android.net.LocalSocket;
@@ -856,14 +859,15 @@
 
         if (descriptors != null) {
             try {
-                ZygoteInit.reopenStdio(descriptors[0],
-                        descriptors[1], descriptors[2]);
+                Os.dup2(descriptors[0], STDIN_FILENO);
+                Os.dup2(descriptors[1], STDOUT_FILENO);
+                Os.dup2(descriptors[2], STDERR_FILENO);
 
                 for (FileDescriptor fd: descriptors) {
                     IoUtils.closeQuietly(fd);
                 }
                 newStderr = System.err;
-            } catch (IOException ex) {
+            } catch (ErrnoException ex) {
                 Log.e(TAG, "Error reopening stdio", ex);
             }
         }
diff --git a/core/java/com/android/internal/os/ZygoteInit.java b/core/java/com/android/internal/os/ZygoteInit.java
index 70982e1..55e3f4a 100644
--- a/core/java/com/android/internal/os/ZygoteInit.java
+++ b/core/java/com/android/internal/os/ZygoteInit.java
@@ -767,21 +767,6 @@
     static native int getpgid(int pid) throws IOException;
 
     /**
-     * Invokes the syscall dup2() to copy the specified descriptors into
-     * stdin, stdout, and stderr. The existing stdio descriptors will be
-     * closed and errors during close will be ignored. The specified
-     * descriptors will also remain open at their original descriptor numbers,
-     * so the caller may want to close the original descriptors.
-     *
-     * @param in new stdin
-     * @param out new stdout
-     * @param err new stderr
-     * @throws IOException
-     */
-    static native void reopenStdio(FileDescriptor in,
-            FileDescriptor out, FileDescriptor err) throws IOException;
-
-    /**
      * Invokes select() on the provider array of file descriptors (selecting
      * for readability only). Array elements of null are ignored.
      *
diff --git a/core/jni/com_android_internal_os_ZygoteInit.cpp b/core/jni/com_android_internal_os_ZygoteInit.cpp
index a2f3dabd..2b6fd11 100644
--- a/core/jni/com_android_internal_os_ZygoteInit.cpp
+++ b/core/jni/com_android_internal_os_ZygoteInit.cpp
@@ -88,43 +88,6 @@
     return ret;
 }
 
-static void com_android_internal_os_ZygoteInit_reopenStdio(JNIEnv* env,
-        jobject clazz, jobject in, jobject out, jobject errfd)
-{
-    int fd;
-    int err;
-
-    fd = jniGetFDFromFileDescriptor(env, in);
-
-    if  (env->ExceptionCheck()) {
-        return;
-    }
-
-    do {
-        err = dup2(fd, STDIN_FILENO);
-    } while (err < 0 && errno == EINTR);
-
-    fd = jniGetFDFromFileDescriptor(env, out);
-
-    if  (env->ExceptionCheck()) {
-        return;
-    }
-
-    do {
-        err = dup2(fd, STDOUT_FILENO);
-    } while (err < 0 && errno == EINTR);
-
-    fd = jniGetFDFromFileDescriptor(env, errfd);
-
-    if  (env->ExceptionCheck()) {
-        return;
-    }
-
-    do {
-        err = dup2(fd, STDERR_FILENO);
-    } while (err < 0 && errno == EINTR);
-}
-
 static jint com_android_internal_os_ZygoteInit_selectReadable (
         JNIEnv *env, jobject clazz, jobjectArray fds)
 {
@@ -205,10 +168,6 @@
       (void *) com_android_internal_os_ZygoteInit_setpgid },
     { "getpgid", "(I)I",
       (void *) com_android_internal_os_ZygoteInit_getpgid },
-    { "reopenStdio",
-        "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;"
-        "Ljava/io/FileDescriptor;)V",
-            (void *) com_android_internal_os_ZygoteInit_reopenStdio},
     { "selectReadable", "([Ljava/io/FileDescriptor;)I",
         (void *) com_android_internal_os_ZygoteInit_selectReadable },
 };