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

Commit 01b4bd4b authored by Xiang Wang's avatar Xiang Wang Committed by Android (Google) Code Review
Browse files

Merge "Add support for ADPF hint session tid cleanup" into main

parents 3b20234f f63a6af0
Loading
Loading
Loading
Loading
+52 −0
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ import libcore.io.IoUtils;
import java.io.FileDescriptor;
import java.io.IOException;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeoutException;

/**
@@ -588,6 +589,8 @@ public class Process {
     **/
    public static final int THREAD_GROUP_RESTRICTED = 7;

    /** @hide */
    public static final int SIGNAL_DEFAULT = 0;
    public static final int SIGNAL_QUIT = 3;
    public static final int SIGNAL_KILL = 9;
    public static final int SIGNAL_USR1 = 10;
@@ -1437,6 +1440,49 @@ public class Process {
        sendSignal(pid, SIGNAL_KILL);
    }

    /**
     * Check the tgid and tid pair to see if the tid still exists and belong to the tgid.
     *
     * TOCTOU warning: the status of the tid can change at the time this method returns. This should
     * be used in very rare cases such as checking if a (tid, tgid) pair that is known to exist
     * recently no longer exists now. As the possibility of the same tid to be reused under the same
     * tgid during a short window is rare. And even if it happens the caller logic should be robust
     * to handle it without error.
     *
     * @throws IllegalArgumentException if tgid or tid is not positive.
     * @throws SecurityException if the caller doesn't have the permission, this method is expected
     *                           to be used by system process with {@link #SYSTEM_UID} because it
     *                           internally uses tkill(2).
     * @throws NoSuchElementException if the Linux process with pid as the tid has exited or it
     *                                doesn't belong to the tgid.
     * @hide
     */
    public static final void checkTid(int tgid, int tid)
            throws IllegalArgumentException, SecurityException, NoSuchElementException {
        sendTgSignalThrows(tgid, tid, SIGNAL_DEFAULT);
    }

    /**
     * Check if the pid still exists.
     *
     * TOCTOU warning: the status of the pid can change at the time this method returns. This should
     * be used in very rare cases such as checking if a pid that belongs to an isolated process of a
     * uid known to exist recently no longer exists now. As the possibility of the same pid to be
     * reused again under the same uid during a short window is rare. And even if it happens the
     * caller logic should be robust to handle it without error.
     *
     * @throws IllegalArgumentException if pid is not positive.
     * @throws SecurityException if the caller doesn't have the permission, this method is expected
     *                           to be used by system process with {@link #SYSTEM_UID} because it
     *                           internally uses kill(2).
     * @throws NoSuchElementException if the Linux process with the pid has exited.
     * @hide
     */
    public static final void checkPid(int pid)
            throws IllegalArgumentException, SecurityException, NoSuchElementException {
        sendSignalThrows(pid, SIGNAL_DEFAULT);
    }

    /** @hide */
    public static final native int setUid(int uid);

@@ -1451,6 +1497,12 @@ public class Process {
     */
    public static final native void sendSignal(int pid, int signal);

    private static native void sendSignalThrows(int pid, int signal)
            throws IllegalArgumentException, SecurityException, NoSuchElementException;

    private static native void sendTgSignalThrows(int pid, int tgid, int signal)
            throws IllegalArgumentException, SecurityException, NoSuchElementException;

    /**
     * @hide
     * Private impl for avoiding a log message...  DO NOT USE without doing
+37 −0
Original line number Diff line number Diff line
@@ -1137,6 +1137,41 @@ void android_os_Process_sendSignalQuiet(JNIEnv* env, jobject clazz, jint pid, ji
    }
}

void android_os_Process_sendSignalThrows(JNIEnv* env, jobject clazz, jint pid, jint sig) {
    if (pid <= 0) {
        jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", "Invalid argument: pid(%d)",
                             pid);
        return;
    }
    int ret = kill(pid, sig);
    if (ret < 0) {
        if (errno == ESRCH) {
            jniThrowExceptionFmt(env, "java/util/NoSuchElementException",
                                 "Process with pid %d not found", pid);
        } else {
            signalExceptionForError(env, errno, pid);
        }
    }
}

void android_os_Process_sendTgSignalThrows(JNIEnv* env, jobject clazz, jint tgid, jint tid,
                                           jint sig) {
    if (tgid <= 0 || tid <= 0) {
        jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
                             "Invalid argument: tgid(%d), tid(%d)", tid, tgid);
        return;
    }
    int ret = tgkill(tgid, tid, sig);
    if (ret < 0) {
        if (errno == ESRCH) {
            jniThrowExceptionFmt(env, "java/util/NoSuchElementException",
                                 "Process with tid %d and tgid %d not found", tid, tgid);
        } else {
            signalExceptionForError(env, errno, tid);
        }
    }
}

static jlong android_os_Process_getElapsedCpuTime(JNIEnv* env, jobject clazz)
{
    struct timespec ts;
@@ -1357,6 +1392,8 @@ static const JNINativeMethod methods[] = {
        {"setGid", "(I)I", (void*)android_os_Process_setGid},
        {"sendSignal", "(II)V", (void*)android_os_Process_sendSignal},
        {"sendSignalQuiet", "(II)V", (void*)android_os_Process_sendSignalQuiet},
        {"sendSignalThrows", "(II)V", (void*)android_os_Process_sendSignalThrows},
        {"sendTgSignalThrows", "(III)V", (void*)android_os_Process_sendTgSignalThrows},
        {"setProcessFrozen", "(IIZ)V", (void*)android_os_Process_setProcessFrozen},
        {"getFreeMemory", "()J", (void*)android_os_Process_getFreeMemory},
        {"getTotalMemory", "()J", (void*)android_os_Process_getTotalMemory},
+1 −0
Original line number Diff line number Diff line
@@ -242,6 +242,7 @@ java_library_static {
        "apache-commons-math",
        "backstage_power_flags_lib",
        "notification_flags_lib",
        "power_hint_flags_lib",
        "biometrics_flags_lib",
        "am_flags_lib",
        "com_android_server_accessibility_flags_lib",
+12 −0
Original line number Diff line number Diff line
aconfig_declarations {
    name: "power_hint_flags",
    package: "com.android.server.power.hint",
    srcs: [
        "flags.aconfig",
    ],
}

java_aconfig_library {
    name: "power_hint_flags_lib",
    aconfig_declarations: "power_hint_flags",
}
+340 −38

File changed.

Preview size limit exceeded, changes collapsed.

Loading