Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5cd79d07 authored by Elliott Hughes's avatar Elliott Hughes Committed by Gerrit Code Review
Browse files

Merge "Move the remaining ZygoteInit native code over to Java."

parents 93241de7 26b56e62
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -993,8 +993,8 @@ class ZygoteConnection {
    private void setChildPgid(int pid) {
        // Try to move the new child into the peer's process group.
        try {
            ZygoteInit.setpgid(pid, ZygoteInit.getpgid(peer.getPid()));
        } catch (IOException ex) {
            Os.setpgid(pid, Os.getpgid(peer.getPid()));
        } catch (ErrnoException ex) {
            // This exception is expected in the case where
            // the peer is not in our session
            // TODO get rid of this log message in the case where
+12 −58
Original line number Diff line number Diff line
@@ -229,26 +229,6 @@ public class ZygoteInit {
    private static final int ROOT_UID = 0;
    private static final int ROOT_GID = 0;

    /**
     * Sets effective user ID.
     */
    private static void setEffectiveUser(int uid) {
        int errno = setreuid(ROOT_UID, uid);
        if (errno != 0) {
            Log.e(TAG, "setreuid() failed. errno: " + errno);
        }
    }

    /**
     * Sets effective group ID.
     */
    private static void setEffectiveGroup(int gid) {
        int errno = setregid(ROOT_GID, gid);
        if (errno != 0) {
            Log.e(TAG, "setregid() failed. errno: " + errno);
        }
    }

    static void preload() {
        Log.d(TAG, "begin preload");
        preloadClasses();
@@ -296,8 +276,12 @@ public class ZygoteInit {
        long startTime = SystemClock.uptimeMillis();

        // Drop root perms while running static initializers.
        setEffectiveGroup(UNPRIVILEGED_GID);
        setEffectiveUser(UNPRIVILEGED_UID);
        try {
            Os.setregid(ROOT_GID, UNPRIVILEGED_GID);
            Os.setreuid(ROOT_UID, UNPRIVILEGED_UID);
        } catch (ErrnoException ex) {
            throw new RuntimeException("Failed to drop root", ex);
        }

        // Alter the target heap utilization.  With explicit GCs this
        // is not likely to have any effect.
@@ -352,8 +336,12 @@ public class ZygoteInit {
            runtime.preloadDexCaches();

            // Bring back root. We'll need it later.
            setEffectiveUser(ROOT_UID);
            setEffectiveGroup(ROOT_GID);
            try {
                Os.setreuid(ROOT_UID, ROOT_UID);
                Os.setregid(ROOT_GID, ROOT_GID);
            } catch (ErrnoException ex) {
                throw new RuntimeException("Failed to restore root", ex);
            }
        }
    }

@@ -737,40 +725,6 @@ public class ZygoteInit {
        }
    }

    /**
     * The Linux syscall "setreuid()"
     * @param ruid real uid
     * @param euid effective uid
     * @return 0 on success, non-zero errno on fail
     */
    static native int setreuid(int ruid, int euid);

    /**
     * The Linux syscall "setregid()"
     * @param rgid real gid
     * @param egid effective gid
     * @return 0 on success, non-zero errno on fail
     */
    static native int setregid(int rgid, int egid);

    /**
     * Invokes the linux syscall "setpgid"
     *
     * @param pid pid to change
     * @param pgid new process group of pid
     * @return 0 on success or non-zero errno on fail
     */
    static native int setpgid(int pid, int pgid);

    /**
     * Invokes the linux syscall "getpgid"
     *
     * @param pid pid to query
     * @return pgid of pid in question
     * @throws IOException on error
     */
    static native int getpgid(int pid) throws IOException;

    /**
     * Class not instantiable.
     */
+0 −1
Original line number Diff line number Diff line
@@ -156,7 +156,6 @@ LOCAL_SRC_FILES:= \
    android_server_NetworkManagementSocketTagger.cpp \
    android_server_Watchdog.cpp \
    android_ddm_DdmHandleNativeHeap.cpp \
    com_android_internal_os_ZygoteInit.cpp \
    android_backup_BackupDataInput.cpp \
    android_backup_BackupDataOutput.cpp \
    android_backup_FileBackupHelperBase.cpp \
+0 −2
Original line number Diff line number Diff line
@@ -162,7 +162,6 @@ extern int register_android_text_AndroidBidi(JNIEnv *env);
extern int register_android_opengl_classes(JNIEnv *env);
extern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env);
extern int register_android_server_NetworkManagementSocketTagger(JNIEnv* env);
extern int register_com_android_internal_os_ZygoteInit(JNIEnv* env);
extern int register_android_backup_BackupDataInput(JNIEnv *env);
extern int register_android_backup_BackupDataOutput(JNIEnv *env);
extern int register_android_backup_FileBackupHelperBase(JNIEnv *env);
@@ -1321,7 +1320,6 @@ static const RegJNIRec gRegJNI[] = {
    REG_JNI(register_android_net_NetworkUtils),
    REG_JNI(register_android_net_TrafficStats),
    REG_JNI(register_android_os_MemoryFile),
    REG_JNI(register_com_android_internal_os_ZygoteInit),
    REG_JNI(register_com_android_internal_os_Zygote),
    REG_JNI(register_com_android_internal_util_VirtualRefBasePtr),
    REG_JNI(register_android_hardware_Camera),
+0 −111
Original line number Diff line number Diff line
/*
 * Copyright (C) 2007 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 "Zygote"

#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <utils/misc.h>
#include <errno.h>
#include <sys/select.h>

#include "jni.h"
#include <JNIHelp.h>
#include "core_jni_helpers.h"

#include <sys/capability.h>
#include <sys/prctl.h>

namespace android {

/*
 * In class com.android.internal.os.ZygoteInit:
 * private static native boolean setreuid(int ruid, int euid)
 */
static jint com_android_internal_os_ZygoteInit_setreuid(
    JNIEnv* env, jobject clazz, jint ruid, jint euid)
{
    if (setreuid(ruid, euid) < 0) {
        return errno;
    }
    return 0;
}

/*
 * In class com.android.internal.os.ZygoteInit:
 * private static native int setregid(int rgid, int egid)
 */
static jint com_android_internal_os_ZygoteInit_setregid(
    JNIEnv* env, jobject clazz, jint rgid, jint egid)
{
    if (setregid(rgid, egid) < 0) {
        return errno;
    }
    return 0;
}

/*
 * In class com.android.internal.os.ZygoteInit:
 * private static native int setpgid(int rgid, int egid)
 */
static jint com_android_internal_os_ZygoteInit_setpgid(
    JNIEnv* env, jobject clazz, jint pid, jint pgid)
{
    if (setpgid(pid, pgid) < 0) {
        return errno;
    }
    return 0;
}

/*
 * In class com.android.internal.os.ZygoteInit:
 * private static native int getpgid(int pid)
 */
static jint com_android_internal_os_ZygoteInit_getpgid(
    JNIEnv* env, jobject clazz, jint pid)
{
    pid_t ret;
    ret = getpgid(pid);

    if (ret < 0) {
        jniThrowIOException(env, errno);
    }

    return ret;
}

/*
 * JNI registration.
 */
static JNINativeMethod gMethods[] = {
    /* name, signature, funcPtr */
    { "setreuid", "(II)I",
      (void*) com_android_internal_os_ZygoteInit_setreuid },
    { "setregid", "(II)I",
      (void*) com_android_internal_os_ZygoteInit_setregid },
    { "setpgid", "(II)I",
      (void *) com_android_internal_os_ZygoteInit_setpgid },
    { "getpgid", "(I)I",
      (void *) com_android_internal_os_ZygoteInit_getpgid },
};
int register_com_android_internal_os_ZygoteInit(JNIEnv* env)
{
    return RegisterMethodsOrDie(env,
            "com/android/internal/os/ZygoteInit", gMethods, NELEM(gMethods));
}

}; // namespace android