Switch ProcessManager and System over to C++.

Bug 2180063 will require changes to ProcessManager, so now's a good time
to make the switch in this directory.
diff --git a/luni-kernel/src/main/native/java_lang_ProcessManager.c b/luni-kernel/src/main/native/java_lang_ProcessManager.cpp
similarity index 87%
rename from luni-kernel/src/main/native/java_lang_ProcessManager.c
rename to luni-kernel/src/main/native/java_lang_ProcessManager.cpp
index 46e78f5..8aa793c 100644
--- a/luni-kernel/src/main/native/java_lang_ProcessManager.c
+++ b/luni-kernel/src/main/native/java_lang_ProcessManager.cpp
@@ -51,19 +51,10 @@
 #define WAIT_STATUS_STRANGE_ERRNO (-3) // observed an undocumented errno
 
 /** Closes a file descriptor. */
-static int closeNow(int fd) {
-    int result;
-    do {
-        result = close(fd);
-    } while (result == -1 && errno == EINTR);
-    return result;
-}
-
-/** Closes a file descriptor. */
 static void java_lang_ProcessManager_close(JNIEnv* env,
-        jclass clazz, jobject javaDescriptor) {
-    int fd = (*env)->GetIntField(env, javaDescriptor, descriptorField);
-    if (closeNow(fd) == -1) {
+        jclass, jobject javaDescriptor) {
+    int fd = env->GetIntField(javaDescriptor, descriptorField);
+    if (TEMP_FAILURE_RETRY(close(fd)) == -1) {
         jniThrowIOException(env, errno);
     }
 }
@@ -143,9 +134,8 @@
             }
         }
 
-        (*env)->CallVoidMethod(env, o, onExitMethod, pid, status);
-
-        if ((*env)->ExceptionOccurred(env)) {
+        env->CallVoidMethod(o, onExitMethod, pid, status);
+        if (env->ExceptionOccurred()) {
             /*
              * The callback threw, so break out of the loop and return,
              * letting the exception percolate up.
@@ -157,11 +147,11 @@
 
 /** Close all open fds > 2 (i.e. everything but stdin/out/err), != skipFd. */
 static void closeNonStandardFds(int skipFd) {
+    // TODO: rather than close all these non-open files, we could look in /proc/self/fd.
     struct rlimit rlimit;
     getrlimit(RLIMIT_NOFILE, &rlimit);
-
-    int fd;
-    for (fd = 3; fd < rlimit.rlim_max; fd++) {
+    const int max_fd = rlimit.rlim_max;
+    for (int fd = 3; fd < max_fd; ++fd) {
         if (fd != skipFd
 #ifdef ANDROID
                 && fd != androidSystemPropertiesFd
@@ -306,15 +296,14 @@
     }
 
     char** array = NULL;
-    jsize length = (*env)->GetArrayLength(env, javaArray);
+    jsize length = env->GetArrayLength(javaArray);
     array = (char**) malloc(sizeof(char*) * (length + 1));
     array[length] = 0;
     jsize index;
     for (index = 0; index < length; index++) {
-        jstring javaEntry = (jstring) (*env)->GetObjectArrayElement(
-                env, javaArray, index);
-        char* entry = (char*) (*env)->GetStringUTFChars(
-                env, javaEntry, NULL);
+        jstring javaEntry =
+                (jstring) env->GetObjectArrayElement(javaArray, index);
+        char* entry = (char*) env->GetStringUTFChars(javaEntry, NULL);
         array[index] = entry;
     }
 
@@ -327,12 +316,12 @@
         return;
     }
 
-    jsize length = (*env)->GetArrayLength(env, javaArray);
+    jsize length = env->GetArrayLength(javaArray);
     jsize index;
     for (index = 0; index < length; index++) {
-        jstring javaEntry = (jstring) (*env)->GetObjectArrayElement(
-                env, javaArray, index);
-        (*env)->ReleaseStringUTFChars(env, javaEntry, array[index]);
+        jstring javaEntry =
+                (jstring) env->GetObjectArrayElement(javaArray, index);
+        env->ReleaseStringUTFChars(javaEntry, array[index]);
     }
 
     free(array);
@@ -352,8 +341,7 @@
     // Extract working directory string.
     const char* workingDirectory = NULL;
     if (javaWorkingDirectory != NULL) {
-        workingDirectory = (const char*) (*env)->GetStringUTFChars(
-                env, javaWorkingDirectory, NULL);
+        workingDirectory = env->GetStringUTFChars(javaWorkingDirectory, NULL);
     }
 
     // Convert environment array.
@@ -364,22 +352,21 @@
             inDescriptor, outDescriptor, errDescriptor);
 
     // Temporarily clear exception so we can clean up.
-    jthrowable exception = (*env)->ExceptionOccurred(env);
-    (*env)->ExceptionClear(env);
+    jthrowable exception = env->ExceptionOccurred();
+    env->ExceptionClear();
 
     freeStrings(env, javaEnvironment, environment);
 
     // Clean up working directory string.
     if (javaWorkingDirectory != NULL) {
-        (*env)->ReleaseStringUTFChars(
-                env, javaWorkingDirectory, workingDirectory);
+        env->ReleaseStringUTFChars(javaWorkingDirectory, workingDirectory);
     }
 
     freeStrings(env, javaCommands, commands);
 
     // Re-throw exception if present.
     if (exception != NULL) {
-        if ((*env)->Throw(env, exception) < 0) {
+        if (env->Throw(exception) < 0) {
             LOGE("Error rethrowing exception!");
         }
     }
@@ -399,18 +386,16 @@
     }
 #endif
 
-    onExitMethod = (*env)->GetMethodID(env, clazz, "onExit", "(II)V");
+    onExitMethod = env->GetMethodID(clazz, "onExit", "(II)V");
     if (onExitMethod == NULL) {
         return;
     }
 
-    jclass fileDescriptorClass
-            = (*env)->FindClass(env, "java/io/FileDescriptor");
+    jclass fileDescriptorClass = env->FindClass("java/io/FileDescriptor");
     if (fileDescriptorClass == NULL) {
         return;
     }
-    descriptorField = (*env)->GetFieldID(env, fileDescriptorClass,
-            "descriptor", "I");
+    descriptorField = env->GetFieldID(fileDescriptorClass, "descriptor", "I");
     if (descriptorField == NULL) {
         return;
     }
diff --git a/luni-kernel/src/main/native/java_lang_System.c b/luni-kernel/src/main/native/java_lang_System.c
deleted file mode 100644
index 1c06a40..0000000
--- a/luni-kernel/src/main/native/java_lang_System.c
+++ /dev/null
@@ -1,106 +0,0 @@
-/*
- * Copyright (C) 2008 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.
- */
-
-#include "JNIHelp.h"
-
-#include <stdlib.h>
-#include <string.h>
-
-
-/*
- * public static native String getEnvByName(String name)
- *
- * (Calling it plain "getenv" might confuse GDB if you try to put a breakpoint
- * on the libc version.)
- */
-static jstring java_getEnvByName(JNIEnv* env, jclass clazz, jstring nameStr)
-{
-    jstring valueStr = NULL;
-
-    if (nameStr != NULL) {
-        const char* name;
-        const char* val;
-
-        name = (*env)->GetStringUTFChars(env, nameStr, NULL);
-        val = getenv(name);
-        if (val != NULL)
-            valueStr = (*env)->NewStringUTF(env, val);
-
-        (*env)->ReleaseStringUTFChars(env, nameStr, name);
-    } else {
-        jniThrowException(env, "java/lang/NullPointerException", NULL);
-    }
-
-    return valueStr;
-}
-
-/*
- * Pointer to complete environment, from Posix.
- */
-extern char** environ;
-
-/*
- * public static native String getEnvByIndex()
- *
- * (Calling it plain "getenv" might confuse GDB if you try to put a breakpoint
- * on the libc version.)
- */
-static jstring java_getEnvByIndex(JNIEnv* env, jclass clazz, jint index)
-{
-    jstring valueStr = NULL;
-
-    char* entry = environ[index];
-    if (entry != NULL) {
-        valueStr = (*env)->NewStringUTF(env, entry);
-    }
-
-    return valueStr;
-}
-
-/*
- * public static native String setFieldImpl()
- *
- * Sets a field via JNI. Used for the standard streams, which are r/o
- * otherwise.
- */
-static void java_setFieldImpl(JNIEnv* env, jclass clazz, jstring name, jstring sig, jobject object)
-{
-    const char* fieldName = (*env)->GetStringUTFChars(env, name, NULL);
-    const char* fieldSig = (*env)->GetStringUTFChars(env, sig, NULL);
-
-    jfieldID fieldID = (*env)->GetStaticFieldID(env, clazz, fieldName, fieldSig);
-    (*env)->SetStaticObjectField(env, clazz, fieldID, object);
-    
-    (*env)->ReleaseStringUTFChars(env, name, fieldName);
-    (*env)->ReleaseStringUTFChars(env, sig, fieldSig);
-}
-
-/*
- * JNI registration
- */
-static JNINativeMethod gMethods[] = {
-    /* name, signature, funcPtr */
-    { "getEnvByName",   "(Ljava/lang/String;)Ljava/lang/String;",  java_getEnvByName  },
-    { "getEnvByIndex",  "(I)Ljava/lang/String;",                   java_getEnvByIndex },
-    { "setFieldImpl",   "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V", java_setFieldImpl  },
-};
-
-int register_java_lang_System(JNIEnv* env)
-{
-	return jniRegisterNativeMethods(env, "java/lang/System",
-                gMethods, NELEM(gMethods));
-}
-
diff --git a/luni-kernel/src/main/native/java_lang_System.cpp b/luni-kernel/src/main/native/java_lang_System.cpp
new file mode 100644
index 0000000..d02481c
--- /dev/null
+++ b/luni-kernel/src/main/native/java_lang_System.cpp
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2008 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.
+ */
+
+#include "AndroidSystemNatives.h"
+#include "JNIHelp.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+
+/*
+ * public static native String getEnvByName(String name)
+ *
+ * (Calling it plain "getenv" might confuse GDB if you try to put a breakpoint
+ * on the libc version.)
+ */
+static jstring java_getEnvByName(JNIEnv* env, jclass, jstring nameStr) {
+    jstring valueStr = NULL;
+
+    if (nameStr != NULL) {
+        const char* name = env->GetStringUTFChars(nameStr, NULL);
+        const char* val = getenv(name);
+        if (val != NULL) {
+            valueStr = env->NewStringUTF(val);
+        }
+        env->ReleaseStringUTFChars(nameStr, name);
+    } else {
+        jniThrowException(env, "java/lang/NullPointerException", NULL);
+    }
+
+    return valueStr;
+}
+
+/*
+ * Pointer to complete environment, from Posix.
+ */
+extern char** environ;
+
+/*
+ * public static native String getEnvByIndex()
+ *
+ * (Calling it plain "getenv" might confuse GDB if you try to put a breakpoint
+ * on the libc version.)
+ */
+static jstring java_getEnvByIndex(JNIEnv* env, jclass, jint index) {
+    jstring valueStr = NULL;
+
+    char* entry = environ[index];
+    if (entry != NULL) {
+        valueStr = env->NewStringUTF(entry);
+    }
+
+    return valueStr;
+}
+
+/*
+ * public static native String setFieldImpl()
+ *
+ * Sets a field via JNI. Used for the standard streams, which are r/o
+ * otherwise.
+ */
+static void java_setFieldImpl(JNIEnv* env, jclass clazz,
+        jstring name, jstring sig, jobject object) {
+    const char* fieldName = env->GetStringUTFChars(name, NULL);
+    const char* fieldSig = env->GetStringUTFChars(sig, NULL);
+
+    jfieldID fieldID = env->GetStaticFieldID(clazz, fieldName, fieldSig);
+    env->SetStaticObjectField(clazz, fieldID, object);
+    
+    env->ReleaseStringUTFChars(name, fieldName);
+    env->ReleaseStringUTFChars(sig, fieldSig);
+}
+
+/*
+ * JNI registration
+ */
+static JNINativeMethod gMethods[] = {
+    /* name, signature, funcPtr */
+    { "getEnvByName",   "(Ljava/lang/String;)Ljava/lang/String;",                    (void*) java_getEnvByName  },
+    { "getEnvByIndex",  "(I)Ljava/lang/String;",                                     (void*) java_getEnvByIndex },
+    { "setFieldImpl",   "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V", (void*) java_setFieldImpl },
+};
+
+int register_java_lang_System(JNIEnv* env) {
+    return jniRegisterNativeMethods(env, "java/lang/System",
+            gMethods, NELEM(gMethods));
+}
diff --git a/luni-kernel/src/main/native/sub.mk b/luni-kernel/src/main/native/sub.mk
index cd4ce92..fcb7d0d 100644
--- a/luni-kernel/src/main/native/sub.mk
+++ b/luni-kernel/src/main/native/sub.mk
@@ -3,8 +3,8 @@
 # or BUILD_*_LIBRARY.
 
 LOCAL_SRC_FILES := \
-	java_lang_ProcessManager.c \
-	java_lang_System.c
+	java_lang_ProcessManager.cpp \
+	java_lang_System.cpp
 
 LOCAL_C_INCLUDES +=