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

Commit 3e458241 authored by San Mehat's avatar San Mehat
Browse files

process: Add setProcessGroup() hidden API call to set the cgroup of


a process and all its child threads.

Signed-off-by: default avatarSan Mehat <san@google.com>
parent 65ff54c4
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -604,6 +604,20 @@ public class Process {
     */
    public static final native void setThreadGroup(int tid, int group)
            throws IllegalArgumentException, SecurityException;
    /**
     * Sets the scheduling group for a process and all child threads
     * @hide
     * @param pid The indentifier of the process to change.
     * @param group The target group for this process.
     * 
     * @throws IllegalArgumentException Throws IllegalArgumentException if
     * <var>tid</var> does not exist.
     * @throws SecurityException Throws SecurityException if your process does
     * not have permission to modify the given thread, or to use the given
     * priority.
     */
    public static final native void setProcessGroup(int pid, int group)
            throws IllegalArgumentException, SecurityException;
    
    /**
     * Set the priority of the calling thread, based on Linux priorities.  See
+31 −0
Original line number Diff line number Diff line
@@ -236,6 +236,36 @@ void android_os_Process_setThreadGroup(JNIEnv* env, jobject clazz, int pid, jint
        signalExceptionForGroupError(env, clazz, errno);
}

void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jint grp) 
{
    DIR *d;
    FILE *fp;
    char proc_path[255];
    struct dirent *de;

    if (grp > ANDROID_TGROUP_MAX || grp < 0) { 
        signalExceptionForGroupError(env, clazz, EINVAL);
        return;
    }

    sprintf(proc_path, "/proc/%d/task", pid);
    if (!(d = opendir(proc_path))) {
        signalExceptionForGroupError(env, clazz, errno);
        return;
    }

    while ((de = readdir(d))) {
        if (de->d_name[0] == '.')
            continue;
        if (add_pid_to_cgroup(atoi(de->d_name), grp)) {
            signalExceptionForGroupError(env, clazz, errno);
            closedir(d);
            return;
        }
    }
    closedir(d);
}

void android_os_Process_setThreadPriority(JNIEnv* env, jobject clazz,
                                              jint pid, jint pri)
{
@@ -820,6 +850,7 @@ static const JNINativeMethod methods[] = {
    {"setThreadPriority",   "(I)V", (void*)android_os_Process_setCallingThreadPriority},
    {"getThreadPriority",   "(I)I", (void*)android_os_Process_getThreadPriority},
    {"setThreadGroup",      "(II)V", (void*)android_os_Process_setThreadGroup},
    {"setProcessGroup",      "(II)V", (void*)android_os_Process_setProcessGroup},
    {"setOomAdj",   "(II)Z", (void*)android_os_Process_setOomAdj},
    {"setArgV0",    "(Ljava/lang/String;)V", (void*)android_os_Process_setArgV0},
    {"setUid", "(I)I", (void*)android_os_Process_setUid},