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

Commit 727f36c6 authored by Android (Google) Code Review's avatar Android (Google) Code Review
Browse files

Merge change 4265 into donut

* changes:
  process: Fix bug where if a thread exited while we were changing its control     group, we'd bail out and report an exception. This situation is *not*     an error.
parents b0d2493a 1fd0ec73
Loading
Loading
Loading
Loading
+16 −24
Original line number Diff line number Diff line
@@ -50,8 +50,6 @@ pid_t gettid() { return syscall(__NR_gettid);}
#undef __KERNEL__
#endif

#define ENABLE_CGROUP_DEBUG 0

/*
 * List of cgroup names which map to ANDROID_TGROUP_ values in Thread.h
 * and Process.java
@@ -205,40 +203,32 @@ static int add_pid_to_cgroup(int pid, int grp)
    sprintf(path, "/dev/cpuctl/%s/tasks",
           (cgroup_names[grp] ? cgroup_names[grp] : ""));

    if ((fd = open(path, O_WRONLY)) < 0) {
        LOGE("Error opening '%s' (%s)", path, strerror(errno));
    if ((fd = open(path, O_WRONLY)) < 0)
        return -1;
    }

    sprintf(text, "%d", pid);
    if (write(fd, text, strlen(text)) < 0) {
        LOGE("Error writing to '%s' (%s)", path, strerror(errno));
        close(fd);
        return -1;
    }

    close(fd);

#if ENABLE_CGROUP_DEBUG
    LOGD("Pid %d sucessfully added to '%s'", pid, path);
#endif
    return 0;
}

void android_os_Process_setThreadGroup(JNIEnv* env, jobject clazz, int pid, jint grp)
{
#if ENABLE_CGROUP_DEBUG
    LOGD("android_os_Process_setThreadGroup(%d, %d)", pid, grp);
#endif

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

    if (add_pid_to_cgroup(pid, grp))
    if (add_pid_to_cgroup(pid, grp)) {
        // If the thread exited on us, don't generate an exception
        if (errno != ESRCH && errno != ENOENT)
            signalExceptionForGroupError(env, clazz, errno);
    }
}

void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jint grp) 
{
@@ -247,10 +237,6 @@ void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jin
    char proc_path[255];
    struct dirent *de;

#if ENABLE_CGROUP_DEBUG
    LOGD("android_os_Process_setProcessGroup(%d, %d)", pid, grp);
#endif

    if (grp > ANDROID_TGROUP_MAX || grp < 0) { 
        signalExceptionForGroupError(env, clazz, EINVAL);
        return;
@@ -258,6 +244,8 @@ void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jin

    sprintf(proc_path, "/proc/%d/task", pid);
    if (!(d = opendir(proc_path))) {
        // If the process exited on us, don't generate an exception
        if (errno != ENOENT)
            signalExceptionForGroupError(env, clazz, errno);
        return;
    }
@@ -265,12 +253,16 @@ void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jin
    while ((de = readdir(d))) {
        if (de->d_name[0] == '.')
            continue;

        if (add_pid_to_cgroup(atoi(de->d_name), grp)) {
            // If the thread exited on us, ignore it and keep going
            if (errno != ESRCH && errno != ENOENT) {
                signalExceptionForGroupError(env, clazz, errno);
                closedir(d);
                return;
            }
        }
    }
    closedir(d);
}