19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* //device/libs/android_runtime/android_util_Process.cpp
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** Copyright 2006, The Android Open Source Project
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes** Licensed under the Apache License, Version 2.0 (the "License");
669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes** you may not use this file except in compliance with the License.
769a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes** You may obtain a copy of the License at
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
969a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes**     http://www.apache.org/licenses/LICENSE-2.0
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
1169a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes** Unless required by applicable law or agreed to in writing, software
1269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes** distributed under the License is distributed on an "AS IS" BASIS,
1369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1469a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes** See the License for the specific language governing permissions and
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** limitations under the License.
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project*/
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define LOG_TAG "Process"
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
20cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen// To make sure cpu_set_t is included from sched.h
21cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen#define _GNU_SOURCE 1
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/Log.h>
230795272aa226f4e965968a03daddc53ce30b7cdaMathias Agopian#include <binder/IPCThreadState.h>
240795272aa226f4e965968a03daddc53ce30b7cdaMathias Agopian#include <binder/IServiceManager.h>
25a23fcd7be8e40078a913b1a99222cdd89229e67bNarayan Kamath#include <cutils/process_name.h>
26f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten#include <cutils/sched_policy.h>
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/String8.h>
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/Vector.h>
290769e550011d8f8a19e333efe1706ef0e6cc6a5fColin Cross#include <processgroup/processgroup.h>
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
31ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe#include "core_jni_helpers.h"
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "android_util_Binder.h"
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "JNIHelp.h"
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <dirent.h>
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <fcntl.h>
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <grp.h>
39c6a410164e1fd728cf7873493baacca7bc29548dMark Salyzyn#include <inttypes.h>
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <pwd.h>
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <signal.h>
42c6a410164e1fd728cf7873493baacca7bc29548dMark Salyzyn#include <sys/errno.h>
43c6a410164e1fd728cf7873493baacca7bc29548dMark Salyzyn#include <sys/resource.h>
44c6a410164e1fd728cf7873493baacca7bc29548dMark Salyzyn#include <sys/stat.h>
45c6a410164e1fd728cf7873493baacca7bc29548dMark Salyzyn#include <sys/types.h>
466af763bec7c3f4d50fee8dd0046409bb8a7fe8f6Glenn Kasten#include <unistd.h>
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
48160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate#define GUARD_THREAD_PRIORITY 0
49a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectusing namespace android;
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
520f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampestatic const bool kDebugPolicy = false;
530f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampestatic const bool kDebugProc = false;
540f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampe
55160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate#if GUARD_THREAD_PRIORITY
56160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher TateMutex gKeyCreateMutex;
57160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tatestatic pthread_key_t gBgKey = -1;
58160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate#endif
59160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate
60f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten// For both of these, err should be in the errno range (positive), not a status_t (negative)
61f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten
626793ac943afeb16642f477c43ddfd27e498db37bGlenn Kastenstatic void signalExceptionForPriorityError(JNIEnv* env, int err)
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    switch (err) {
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case EINVAL:
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case ESRCH:
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/IllegalArgumentException", "Given thread does not exist");
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case EPERM:
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/SecurityException", "No permission to modify given thread");
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case EACCES:
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/SecurityException", "No permission to set to given priority");
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        default:
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/RuntimeException", "Unknown error");
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
836793ac943afeb16642f477c43ddfd27e498db37bGlenn Kastenstatic void signalExceptionForGroupError(JNIEnv* env, int err)
84e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat{
85e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat    switch (err) {
86e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat        case EINVAL:
87e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
88e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            break;
89e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat        case ESRCH:
90e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            jniThrowException(env, "java/lang/IllegalArgumentException", "Given thread does not exist");
91e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            break;
92e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat        case EPERM:
93e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            jniThrowException(env, "java/lang/SecurityException", "No permission to modify given thread");
94e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            break;
95e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat        case EACCES:
96e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            jniThrowException(env, "java/lang/SecurityException", "No permission to set to given group");
97e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            break;
98e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat        default:
99e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            jniThrowException(env, "java/lang/RuntimeException", "Unknown error");
100e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            break;
101e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat    }
102e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat}
103e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjint android_os_Process_getUidForName(JNIEnv* env, jobject clazz, jstring name)
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (name == NULL) {
10769a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowNullPointerException(env, NULL);
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11069a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const jchar* str16 = env->GetStringCritical(name, 0);
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    String8 name8;
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (str16) {
1146698749dd4d4d6513b26aa9071af290b956b68a7Dan Albert        name8 = String8(reinterpret_cast<const char16_t*>(str16),
1156698749dd4d4d6513b26aa9071af290b956b68a7Dan Albert                        env->GetStringLength(name));
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseStringCritical(name, str16);
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const size_t N = name8.size();
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (N > 0) {
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const char* str = name8.string();
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        for (size_t i=0; i<N; i++) {
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (str[i] < '0' || str[i] > '9') {
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                struct passwd* pwd = getpwnam(str);
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (pwd == NULL) {
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    return -1;
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return pwd->pw_uid;
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return atoi(str);
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return -1;
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjint android_os_Process_getGidForName(JNIEnv* env, jobject clazz, jstring name)
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (name == NULL) {
13969a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowNullPointerException(env, NULL);
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const jchar* str16 = env->GetStringCritical(name, 0);
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    String8 name8;
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (str16) {
1466698749dd4d4d6513b26aa9071af290b956b68a7Dan Albert        name8 = String8(reinterpret_cast<const char16_t*>(str16),
1476698749dd4d4d6513b26aa9071af290b956b68a7Dan Albert                        env->GetStringLength(name));
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseStringCritical(name, str16);
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const size_t N = name8.size();
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (N > 0) {
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const char* str = name8.string();
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        for (size_t i=0; i<N; i++) {
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (str[i] < '0' || str[i] > '9') {
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                struct group* grp = getgrnam(str);
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (grp == NULL) {
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    return -1;
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return grp->gr_gid;
1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return atoi(str);
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return -1;
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
168f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kastenvoid android_os_Process_setThreadGroup(JNIEnv* env, jobject clazz, int tid, jint grp)
169e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat{
170c6a410164e1fd728cf7873493baacca7bc29548dMark Salyzyn    ALOGV("%s tid=%d grp=%" PRId32, __func__, tid, grp);
171f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten    SchedPolicy sp = (SchedPolicy) grp;
172f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten    int res = set_sched_policy(tid, sp);
173887f355f99ff83d568ef2885a4fdcaae475583dfDianne Hackborn    if (res != NO_ERROR) {
174f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten        signalExceptionForGroupError(env, -res);
175e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat    }
176e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat}
177e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat
17869a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughesvoid android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jint grp)
1793e458241d9930465a20a861ecb42744355d48e48San Mehat{
180c6a410164e1fd728cf7873493baacca7bc29548dMark Salyzyn    ALOGV("%s pid=%d grp=%" PRId32, __func__, pid, grp);
1813e458241d9930465a20a861ecb42744355d48e48San Mehat    DIR *d;
1823e458241d9930465a20a861ecb42744355d48e48San Mehat    char proc_path[255];
1833e458241d9930465a20a861ecb42744355d48e48San Mehat    struct dirent *de;
1843e458241d9930465a20a861ecb42744355d48e48San Mehat
185f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten    if ((grp == SP_FOREGROUND) || (grp > SP_MAX)) {
1866793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten        signalExceptionForGroupError(env, EINVAL);
1873e458241d9930465a20a861ecb42744355d48e48San Mehat        return;
1883e458241d9930465a20a861ecb42744355d48e48San Mehat    }
1893e458241d9930465a20a861ecb42744355d48e48San Mehat
190f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten    bool isDefault = false;
191f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten    if (grp < 0) {
192f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten        grp = SP_FOREGROUND;
193f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten        isDefault = true;
194f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten    }
195f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten    SchedPolicy sp = (SchedPolicy) grp;
196f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten
1970f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampe    if (kDebugPolicy) {
1980f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampe        char cmdline[32];
1990f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampe        int fd;
200a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat
2010f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampe        strcpy(cmdline, "unknown");
202a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat
2030f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampe        sprintf(proc_path, "/proc/%d/cmdline", pid);
2040f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampe        fd = open(proc_path, O_RDONLY);
2050f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampe        if (fd >= 0) {
2060f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampe            int rc = read(fd, cmdline, sizeof(cmdline)-1);
2070f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampe            cmdline[rc] = 0;
2080f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampe            close(fd);
2090f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampe        }
21069a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
2110f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampe        if (sp == SP_BACKGROUND) {
2120f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampe            ALOGD("setProcessGroup: vvv pid %d (%s)", pid, cmdline);
2130f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampe        } else {
2140f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampe            ALOGD("setProcessGroup: ^^^ pid %d (%s)", pid, cmdline);
2150f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampe        }
216a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat    }
2170f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampe
2183e458241d9930465a20a861ecb42744355d48e48San Mehat    sprintf(proc_path, "/proc/%d/task", pid);
2193e458241d9930465a20a861ecb42744355d48e48San Mehat    if (!(d = opendir(proc_path))) {
2201fd0ec738b0a2b97cc28701aa37b1a9869afc684San Mehat        // If the process exited on us, don't generate an exception
2211fd0ec738b0a2b97cc28701aa37b1a9869afc684San Mehat        if (errno != ENOENT)
2226793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten            signalExceptionForGroupError(env, errno);
2233e458241d9930465a20a861ecb42744355d48e48San Mehat        return;
2243e458241d9930465a20a861ecb42744355d48e48San Mehat    }
2253e458241d9930465a20a861ecb42744355d48e48San Mehat
2263e458241d9930465a20a861ecb42744355d48e48San Mehat    while ((de = readdir(d))) {
2277e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat        int t_pid;
2287e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat        int t_pri;
2297e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat
2303e458241d9930465a20a861ecb42744355d48e48San Mehat        if (de->d_name[0] == '.')
2313e458241d9930465a20a861ecb42744355d48e48San Mehat            continue;
2327e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat        t_pid = atoi(de->d_name);
2337e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat
2347e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat        if (!t_pid) {
2353762c311729fe9f3af085c14c5c1fb471d994c03Steve Block            ALOGE("Error getting pid for '%s'\n", de->d_name);
2367e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat            continue;
2377e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat        }
2381fd0ec738b0a2b97cc28701aa37b1a9869afc684San Mehat
23907b0465095bd9ab3412caefa4fcacbdc3825c64bGlenn Kasten        t_pri = getpriority(PRIO_PROCESS, t_pid);
24007b0465095bd9ab3412caefa4fcacbdc3825c64bGlenn Kasten
24107b0465095bd9ab3412caefa4fcacbdc3825c64bGlenn Kasten        if (t_pri <= ANDROID_PRIORITY_AUDIO) {
24207b0465095bd9ab3412caefa4fcacbdc3825c64bGlenn Kasten            int scheduler = sched_getscheduler(t_pid);
24307b0465095bd9ab3412caefa4fcacbdc3825c64bGlenn Kasten            if ((scheduler == SCHED_FIFO) || (scheduler == SCHED_RR)) {
2449e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray                // This task wants to stay in its current audio group so it can keep its budget
2459e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray                // don't update its cpuset or cgroup
24607b0465095bd9ab3412caefa4fcacbdc3825c64bGlenn Kasten                continue;
24707b0465095bd9ab3412caefa4fcacbdc3825c64bGlenn Kasten            }
24807b0465095bd9ab3412caefa4fcacbdc3825c64bGlenn Kasten        }
2497e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat
25007b0465095bd9ab3412caefa4fcacbdc3825c64bGlenn Kasten        if (isDefault) {
251f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten            if (t_pri >= ANDROID_PRIORITY_BACKGROUND) {
252f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten                // This task wants to stay at background
2539e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray                // update its cpuset so it doesn't only run on bg core(s)
2549e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray#ifdef ENABLE_CPUSETS
2559e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray                int err = set_cpuset_policy(t_pid, sp);
2569e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray                if (err != NO_ERROR) {
2579e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray                    signalExceptionForGroupError(env, -err);
2589e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray                    break;
2599e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray                }
2609e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray#endif
261f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten                continue;
262f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten            }
2637e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat        }
2649e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray        int err;
2659e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray#ifdef ENABLE_CPUSETS
2669e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray        // set both cpuset and cgroup for general threads
2679e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray        err = set_cpuset_policy(t_pid, sp);
2689e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray        if (err != NO_ERROR) {
2699e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray            signalExceptionForGroupError(env, -err);
2709e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray            break;
2719e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray        }
2729e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray#endif
27369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
2749e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray        err = set_sched_policy(t_pid, sp);
275f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten        if (err != NO_ERROR) {
276f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten            signalExceptionForGroupError(env, -err);
2770a42b811aea490a9a605b75f0320101f6eafd283San Mehat            break;
278242d65bf9faf1d2bc3468490e510551140e23462San Mehat        }
2799e41c7479ccaedb89a89a58079570ca0ee0e3727Tim Murray
2803e458241d9930465a20a861ecb42744355d48e48San Mehat    }
2813e458241d9930465a20a861ecb42744355d48e48San Mehat    closedir(d);
2823e458241d9930465a20a861ecb42744355d48e48San Mehat}
2833e458241d9930465a20a861ecb42744355d48e48San Mehat
2849e57c414f39e1a31349bc215635fdcfaf1902ceeJeff Sharkeyjint android_os_Process_getProcessGroup(JNIEnv* env, jobject clazz, jint pid)
2859e57c414f39e1a31349bc215635fdcfaf1902ceeJeff Sharkey{
2869e57c414f39e1a31349bc215635fdcfaf1902ceeJeff Sharkey    SchedPolicy sp;
2879e57c414f39e1a31349bc215635fdcfaf1902ceeJeff Sharkey    if (get_sched_policy(pid, &sp) != 0) {
2889e57c414f39e1a31349bc215635fdcfaf1902ceeJeff Sharkey        signalExceptionForGroupError(env, errno);
2899e57c414f39e1a31349bc215635fdcfaf1902ceeJeff Sharkey    }
2909e57c414f39e1a31349bc215635fdcfaf1902ceeJeff Sharkey    return (int) sp;
2919e57c414f39e1a31349bc215635fdcfaf1902ceeJeff Sharkey}
2929e57c414f39e1a31349bc215635fdcfaf1902ceeJeff Sharkey
293cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen#ifdef ENABLE_CPUSETS
294cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen/** Sample CPUset list format:
295cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen *  0-3,4,6-8
296cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen */
297cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenenstatic void parse_cpuset_cpus(char *cpus, cpu_set_t *cpu_set) {
298cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    unsigned int start, end, matched, i;
299cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    char *cpu_range = strtok(cpus, ",");
300cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    while (cpu_range != NULL) {
301cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        start = end = 0;
302cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        matched = sscanf(cpu_range, "%u-%u", &start, &end);
303cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        cpu_range = strtok(NULL, ",");
304cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        if (start >= CPU_SETSIZE) {
305cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen            ALOGE("parse_cpuset_cpus: ignoring CPU number larger than %d.", CPU_SETSIZE);
306cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen            continue;
307cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        } else if (end >= CPU_SETSIZE) {
308cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen            ALOGE("parse_cpuset_cpus: ignoring CPU numbers larger than %d.", CPU_SETSIZE);
309cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen            end = CPU_SETSIZE - 1;
310cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        }
311cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        if (matched == 1) {
312cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen            CPU_SET(start, cpu_set);
313cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        } else if (matched == 2) {
314cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen            for (i = start; i <= end; i++) {
315cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen                CPU_SET(i, cpu_set);
316cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen            }
317cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        } else {
318cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen            ALOGE("Failed to match cpus");
319cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        }
320cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    }
321cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    return;
322cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen}
323cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen
324cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen/**
325cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen * Stores the CPUs assigned to the cpuset corresponding to the
326cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen * SchedPolicy in the passed in cpu_set.
327cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen */
328cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenenstatic void get_cpuset_cores_for_policy(SchedPolicy policy, cpu_set_t *cpu_set)
329cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen{
330cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    FILE *file;
331cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    const char *filename;
332cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen
333cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    CPU_ZERO(cpu_set);
334cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen
335cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    switch (policy) {
336cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        case SP_BACKGROUND:
337cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen            filename = "/dev/cpuset/background/cpus";
338cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen            break;
339cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        case SP_FOREGROUND:
340cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        case SP_AUDIO_APP:
341cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        case SP_AUDIO_SYS:
342cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen            filename = "/dev/cpuset/foreground/cpus";
343cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen            break;
344cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        case SP_TOP_APP:
345cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen            filename = "/dev/cpuset/top-app/cpus";
346cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen            break;
347cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        default:
348cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen            filename = NULL;
349cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    }
350cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen
351cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    if (!filename) return;
352cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen
353cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    file = fopen(filename, "re");
354cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    if (file != NULL) {
355cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        // Parse cpus string
356cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        char *line = NULL;
357cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        size_t len = 0;
358cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        ssize_t num_read = getline(&line, &len, file);
359cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        fclose (file);
360cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        if (num_read > 0) {
361cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen            parse_cpuset_cpus(line, cpu_set);
362cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        } else {
363cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen            ALOGE("Failed to read %s", filename);
364cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        }
365cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        free(line);
366cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    }
367cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    return;
368cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen}
369cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen#endif
370cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen
371cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen
372cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen/**
373cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen * Determine CPU cores exclusively assigned to the
374cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen * cpuset corresponding to the SchedPolicy and store
375cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen * them in the passed in cpu_set_t
376cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen */
377cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenenvoid get_exclusive_cpuset_cores(SchedPolicy policy, cpu_set_t *cpu_set) {
378cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen#ifdef ENABLE_CPUSETS
379cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    int i;
380cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    cpu_set_t tmp_set;
381cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    get_cpuset_cores_for_policy(policy, cpu_set);
382cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    for (i = 0; i < SP_CNT; i++) {
383cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        if ((SchedPolicy) i == policy) continue;
384cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        get_cpuset_cores_for_policy((SchedPolicy)i, &tmp_set);
385cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        // First get cores exclusive to one set or the other
386cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        CPU_XOR(&tmp_set, cpu_set, &tmp_set);
387cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        // Then get the ones only in cpu_set
388cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        CPU_AND(cpu_set, cpu_set, &tmp_set);
389cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    }
390cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen#else
391cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    (void) policy;
392cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    CPU_ZERO(cpu_set);
393cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen#endif
394cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    return;
395cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen}
396cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen
397cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn CoenenjintArray android_os_Process_getExclusiveCores(JNIEnv* env, jobject clazz) {
398cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    SchedPolicy sp;
399cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    cpu_set_t cpu_set;
400cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    jintArray cpus;
401cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    int pid = getpid();
402cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    if (get_sched_policy(pid, &sp) != 0) {
403cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        signalExceptionForGroupError(env, errno);
404cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        return NULL;
405cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    }
406cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    get_exclusive_cpuset_cores(sp, &cpu_set);
407cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    int num_cpus = CPU_COUNT(&cpu_set);
408cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    cpus = env->NewIntArray(num_cpus);
409cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    if (cpus == NULL) {
410cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
411cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        return NULL;
412cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    }
413cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen
414cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    jint* cpu_elements = env->GetIntArrayElements(cpus, 0);
415cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    int count = 0;
416cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    for (int i = 0; i < CPU_SETSIZE && count < num_cpus; i++) {
417cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        if (CPU_ISSET(i, &cpu_set)) {
418cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen            cpu_elements[count++] = i;
419cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen        }
420cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    }
421cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen
422cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    env->ReleaseIntArrayElements(cpus, cpu_elements, 0);
423cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen    return cpus;
424cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen}
425cd4bdf3eb91c1cc2757d32a86fc90745d0dff990Martijn Coenen
426160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tatestatic void android_os_Process_setCanSelfBackground(JNIEnv* env, jobject clazz, jboolean bgOk) {
427160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    // Establishes the calling thread as illegal to put into the background.
428160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    // Typically used only for the system process's main looper.
429160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate#if GUARD_THREAD_PRIORITY
43006451fe081d5ae79121a6f301475c7042f7f3a5dElliott Hughes    ALOGV("Process.setCanSelfBackground(%d) : tid=%d", bgOk, gettid());
431160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    {
432160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate        Mutex::Autolock _l(gKeyCreateMutex);
433160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate        if (gBgKey == -1) {
434160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate            pthread_key_create(&gBgKey, NULL);
435160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate        }
436160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    }
437160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate
438160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    // inverted:  not-okay, we set a sentinel value
439160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    pthread_setspecific(gBgKey, (void*)(bgOk ? 0 : 0xbaad));
440160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate#endif
441160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate}
442160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate
4436793ac943afeb16642f477c43ddfd27e498db37bGlenn Kastenvoid android_os_Process_setThreadScheduler(JNIEnv* env, jclass clazz,
4446793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten                                              jint tid, jint policy, jint pri)
4456793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten{
44665b4a68669227a57696e0e7ed1e4ef8da2705d9eYabin Cui// linux has sched_setscheduler(), others don't.
44765b4a68669227a57696e0e7ed1e4ef8da2705d9eYabin Cui#if defined(__linux__)
4486793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten    struct sched_param param;
4496793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten    param.sched_priority = pri;
4506793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten    int rc = sched_setscheduler(tid, policy, &param);
4516793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten    if (rc) {
4526793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten        signalExceptionForPriorityError(env, errno);
4536793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten    }
454cc767191cfb675f744e0165608b0a4196aba2b37Glenn Kasten#else
455cc767191cfb675f744e0165608b0a4196aba2b37Glenn Kasten    signalExceptionForPriorityError(env, ENOSYS);
456cc767191cfb675f744e0165608b0a4196aba2b37Glenn Kasten#endif
4576793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten}
4586793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten
4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid android_os_Process_setThreadPriority(JNIEnv* env, jobject clazz,
4609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                              jint pid, jint pri)
4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
462160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate#if GUARD_THREAD_PRIORITY
463160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    // if we're putting the current thread into the background, check the TLS
464160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    // to make sure this thread isn't guarded.  If it is, raise an exception.
465160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    if (pri >= ANDROID_PRIORITY_BACKGROUND) {
46606451fe081d5ae79121a6f301475c7042f7f3a5dElliott Hughes        if (pid == gettid()) {
467160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate            void* bgOk = pthread_getspecific(gBgKey);
468160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate            if (bgOk == ((void*)0xbaad)) {
4693762c311729fe9f3af085c14c5c1fb471d994c03Steve Block                ALOGE("Thread marked fg-only put self in background!");
470160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate                jniThrowException(env, "java/lang/SecurityException", "May not put this thread into background");
471160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate                return;
472160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate            }
473160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate        }
474160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    }
475160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate#endif
476160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate
477887f355f99ff83d568ef2885a4fdcaae475583dfDianne Hackborn    int rc = androidSetThreadPriority(pid, pri);
478887f355f99ff83d568ef2885a4fdcaae475583dfDianne Hackborn    if (rc != 0) {
479887f355f99ff83d568ef2885a4fdcaae475583dfDianne Hackborn        if (rc == INVALID_OPERATION) {
4806793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten            signalExceptionForPriorityError(env, errno);
481887f355f99ff83d568ef2885a4fdcaae475583dfDianne Hackborn        } else {
4826793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten            signalExceptionForGroupError(env, errno);
483887f355f99ff83d568ef2885a4fdcaae475583dfDianne Hackborn        }
4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
48569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
486c6a410164e1fd728cf7873493baacca7bc29548dMark Salyzyn    //ALOGI("Setting priority of %" PRId32 ": %" PRId32 ", getpriority returns %d\n",
4879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //     pid, pri, getpriority(PRIO_PROCESS, pid));
4889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid android_os_Process_setCallingThreadPriority(JNIEnv* env, jobject clazz,
4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                        jint pri)
4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
49306451fe081d5ae79121a6f301475c7042f7f3a5dElliott Hughes    android_os_Process_setThreadPriority(env, clazz, gettid(), pri);
4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjint android_os_Process_getThreadPriority(JNIEnv* env, jobject clazz,
4979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                              jint pid)
4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    errno = 0;
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jint pri = getpriority(PRIO_PROCESS, pid);
5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (errno != 0) {
5026793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten        signalExceptionForPriorityError(env, errno);
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
504c6a410164e1fd728cf7873493baacca7bc29548dMark Salyzyn    //ALOGI("Returning priority of %" PRId32 ": %" PRId32 "\n", pid, pri);
5059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return pri;
5069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5085534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchandjboolean android_os_Process_setSwappiness(JNIEnv *env, jobject clazz,
5095534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand                                          jint pid, jboolean is_increased)
5105534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand{
5115534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand    char text[64];
5125534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand
5135534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand    if (is_increased) {
5145534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand        strcpy(text, "/sys/fs/cgroup/memory/sw/tasks");
5155534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand    } else {
5165534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand        strcpy(text, "/sys/fs/cgroup/memory/tasks");
5175534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand    }
5185534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand
5195534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand    struct stat st;
5205534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand    if (stat(text, &st) || !S_ISREG(st.st_mode)) {
5215534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand        return false;
5225534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand    }
5235534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand
5245534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand    int fd = open(text, O_WRONLY);
5255534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand    if (fd >= 0) {
526c6a410164e1fd728cf7873493baacca7bc29548dMark Salyzyn        sprintf(text, "%" PRId32, pid);
5275534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand        write(fd, text, strlen(text));
5285534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand        close(fd);
5295534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand    }
5305534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand
5315534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand    return true;
5325534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand}
5335534ba91ac0a0c9af822af62bcf92e2c5a8d6ec8Rom Lemarchand
5349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid android_os_Process_setArgV0(JNIEnv* env, jobject clazz, jstring name)
5359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
5369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (name == NULL) {
53769a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowNullPointerException(env, NULL);
5389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
5399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
54069a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const jchar* str = env->GetStringCritical(name, 0);
5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    String8 name8;
5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (str) {
5446698749dd4d4d6513b26aa9071af290b956b68a7Dan Albert        name8 = String8(reinterpret_cast<const char16_t*>(str),
5456698749dd4d4d6513b26aa9071af290b956b68a7Dan Albert                        env->GetStringLength(name));
5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseStringCritical(name, str);
5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (name8.size() > 0) {
550a23fcd7be8e40078a913b1a99222cdd89229e67bNarayan Kamath        const char* procName = name8.string();
551a23fcd7be8e40078a913b1a99222cdd89229e67bNarayan Kamath        set_process_name(procName);
552a23fcd7be8e40078a913b1a99222cdd89229e67bNarayan Kamath        AndroidRuntime::getRuntime()->setArgv0(procName);
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjint android_os_Process_setUid(JNIEnv* env, jobject clazz, jint uid)
5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return setuid(uid) == 0 ? 0 : errno;
5599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjint android_os_Process_setGid(JNIEnv* env, jobject clazz, jint uid)
5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return setgid(uid) == 0 ? 0 : errno;
5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic int pid_compare(const void* v1, const void* v2)
5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
568c6a410164e1fd728cf7873493baacca7bc29548dMark Salyzyn    //ALOGI("Compare %" PRId32 " vs %" PRId32 "\n", *((const jint*)v1), *((const jint*)v2));
5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return *((const jint*)v1) - *((const jint*)v2);
5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
572c367d48c55e5a3fa0df14fd62889e4bb6b63cb01Elliott Hughesstatic jlong getFreeMemoryImpl(const char* const sums[], const size_t sumsLen[], size_t num)
5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int fd = open("/proc/meminfo", O_RDONLY);
57569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (fd < 0) {
5778564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("Unable to open /proc/meminfo");
5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
58069a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    char buffer[256];
5829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const int len = read(fd, buffer, sizeof(buffer)-1);
5839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    close(fd);
58469a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (len < 0) {
5868564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("Unable to read /proc/meminfo");
5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    buffer[len] = 0;
5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
591c367d48c55e5a3fa0df14fd62889e4bb6b63cb01Elliott Hughes    size_t numFound = 0;
5920bca96bcbfe559f9330a01f723c5c9cba51ec05aMarco Nelissen    jlong mem = 0;
59369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
5949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    char* p = buffer;
59559325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn    while (*p && numFound < num) {
5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int i = 0;
5979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        while (sums[i]) {
5989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (strncmp(p, sums[i], sumsLen[i]) == 0) {
5999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                p += sumsLen[i];
6009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                while (*p == ' ') p++;
6019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                char* num = p;
6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                while (*p >= '0' && *p <= '9') p++;
6039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (*p != 0) {
6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    *p = 0;
6059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    p++;
6069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (*p == 0) p--;
6079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
6080bca96bcbfe559f9330a01f723c5c9cba51ec05aMarco Nelissen                mem += atoll(num) * 1024;
6099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                numFound++;
6109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                break;
6119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
6129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            i++;
6139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        p++;
6159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
61669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return numFound > 0 ? mem : -1;
6189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
62059325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackbornstatic jlong android_os_Process_getFreeMemory(JNIEnv* env, jobject clazz)
62159325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn{
62259325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn    static const char* const sums[] = { "MemFree:", "Cached:", NULL };
623c367d48c55e5a3fa0df14fd62889e4bb6b63cb01Elliott Hughes    static const size_t sumsLen[] = { strlen("MemFree:"), strlen("Cached:"), 0 };
62459325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn    return getFreeMemoryImpl(sums, sumsLen, 2);
62559325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn}
62659325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn
62759325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackbornstatic jlong android_os_Process_getTotalMemory(JNIEnv* env, jobject clazz)
62859325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn{
62959325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn    static const char* const sums[] = { "MemTotal:", NULL };
630c367d48c55e5a3fa0df14fd62889e4bb6b63cb01Elliott Hughes    static const size_t sumsLen[] = { strlen("MemTotal:"), 0 };
63159325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn    return getFreeMemoryImpl(sums, sumsLen, 1);
63259325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn}
63359325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn
6349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid android_os_Process_readProcLines(JNIEnv* env, jobject clazz, jstring fileStr,
6359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                      jobjectArray reqFields, jlongArray outFields)
6369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
6376215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block    //ALOGI("getMemInfo: %p %p", reqFields, outFields);
63869a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (fileStr == NULL || reqFields == NULL || outFields == NULL) {
64069a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowNullPointerException(env, NULL);
6419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
6429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
64369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const char* file8 = env->GetStringUTFChars(fileStr, NULL);
6459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (file8 == NULL) {
6469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
6479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    String8 file(file8);
6499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->ReleaseStringUTFChars(fileStr, file8);
65069a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jsize count = env->GetArrayLength(reqFields);
6529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (count > env->GetArrayLength(outFields)) {
6539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalArgumentException", "Array lengths differ");
6549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
6559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
65669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Vector<String8> fields;
6589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int i;
65969a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (i=0; i<count; i++) {
6619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jobject obj = env->GetObjectArrayElement(reqFields, i);
6629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (obj != NULL) {
6639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const char* str8 = env->GetStringUTFChars((jstring)obj, NULL);
6646215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block            //ALOGI("String at %d: %p = %s", i, obj, str8);
6659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (str8 == NULL) {
66669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes                jniThrowNullPointerException(env, "Element in reqFields");
6679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return;
6689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
6699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            fields.add(String8(str8));
6709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->ReleaseStringUTFChars((jstring)obj, str8);
6719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
67269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes            jniThrowNullPointerException(env, "Element in reqFields");
6739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
6749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
67669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jlong* sizesArray = env->GetLongArrayElements(outFields, 0);
6789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (sizesArray == NULL) {
6799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
6809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
68169a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
682c6a410164e1fd728cf7873493baacca7bc29548dMark Salyzyn    //ALOGI("Clearing %" PRId32 " sizes", count);
6839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (i=0; i<count; i++) {
6849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        sizesArray[i] = 0;
6859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
68669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int fd = open(file.string(), O_RDONLY);
68869a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (fd >= 0) {
6909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const size_t BUFFER_SIZE = 2048;
6919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        char* buffer = (char*)malloc(BUFFER_SIZE);
6929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int len = read(fd, buffer, BUFFER_SIZE-1);
6939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        close(fd);
69469a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (len < 0) {
6968564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("Unable to read %s", file.string());
6979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            len = 0;
6989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        buffer[len] = 0;
70069a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
7019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int foundCount = 0;
70269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
7039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        char* p = buffer;
7049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        while (*p && foundCount < count) {
7059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            bool skipToEol = true;
7066215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block            //ALOGI("Parsing at: %s", p);
7079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (i=0; i<count; i++) {
7089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                const String8& field = fields[i];
7099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (strncmp(p, field.string(), field.length()) == 0) {
7109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    p += field.length();
711add868cebaf62cffe96e79764ea0b7f2320a03ebAmith Yamasani                    while (*p == ' ' || *p == '\t') p++;
7129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    char* num = p;
7139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    while (*p >= '0' && *p <= '9') p++;
7149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    skipToEol = *p != '\n';
7159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (*p != 0) {
7169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        *p = 0;
7179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        p++;
7189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
7199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    char* end;
7209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    sizesArray[i] = strtoll(num, &end, 10);
721c6a410164e1fd728cf7873493baacca7bc29548dMark Salyzyn                    //ALOGI("Field %s = %" PRId64, field.string(), sizesArray[i]);
7229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    foundCount++;
7239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    break;
7249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
7259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
7269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (skipToEol) {
7279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                while (*p && *p != '\n') {
7289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    p++;
7299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
7309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (*p == '\n') {
7319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    p++;
7329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
7339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
7349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
73569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
7369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        free(buffer);
7379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else {
7388564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("Unable to open %s", file.string());
7399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
74069a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
7416215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block    //ALOGI("Done!");
7429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->ReleaseLongArrayElements(outFields, sizesArray, 0);
7439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
7449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectjintArray android_os_Process_getPids(JNIEnv* env, jobject clazz,
7469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                     jstring file, jintArray lastArray)
7479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
7489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (file == NULL) {
74969a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowNullPointerException(env, NULL);
7509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
7519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
75269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
7539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const char* file8 = env->GetStringUTFChars(file, NULL);
7549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (file8 == NULL) {
7559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
7569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
7579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
75869a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
7599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    DIR* dirp = opendir(file8);
76069a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
7619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->ReleaseStringUTFChars(file, file8);
76269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
7639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if(dirp == NULL) {
7649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
7659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
76669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
7679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jsize curCount = 0;
7689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jint* curData = NULL;
7699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (lastArray != NULL) {
7709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        curCount = env->GetArrayLength(lastArray);
7719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        curData = env->GetIntArrayElements(lastArray, 0);
7729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
77369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
7749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jint curPos = 0;
77569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
7769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    struct dirent* entry;
7779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    while ((entry=readdir(dirp)) != NULL) {
7789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const char* p = entry->d_name;
7799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        while (*p) {
7809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (*p < '0' || *p > '9') break;
7819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            p++;
7829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (*p != 0) continue;
78469a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
7859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        char* end;
7869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int pid = strtol(entry->d_name, &end, 10);
7876215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block        //ALOGI("File %s pid=%d\n", entry->d_name, pid);
7889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (curPos >= curCount) {
7899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jsize newCount = (curCount == 0) ? 10 : (curCount*2);
7909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jintArray newArray = env->NewIntArray(newCount);
7919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (newArray == NULL) {
7929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                closedir(dirp);
7939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
7949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return NULL;
7959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
7969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jint* newData = env->GetIntArrayElements(newArray, 0);
7979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (curData != NULL) {
7989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                memcpy(newData, curData, sizeof(jint)*curCount);
7999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                env->ReleaseIntArrayElements(lastArray, curData, 0);
8009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
8019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            lastArray = newArray;
8029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            curCount = newCount;
8039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            curData = newData;
8049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
80569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
8069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        curData[curPos] = pid;
8079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        curPos++;
8089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
80969a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
8109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    closedir(dirp);
81169a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
8129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (curData != NULL && curPos > 0) {
8139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        qsort(curData, curPos, sizeof(jint), pid_compare);
8149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
81569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
8169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    while (curPos < curCount) {
8179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        curData[curPos] = -1;
8189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        curPos++;
8199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
82069a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
8219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (curData != NULL) {
8229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseIntArrayElements(lastArray, curData, 0);
8239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
82469a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
8259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return lastArray;
8269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectenum {
8299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    PROC_TERM_MASK = 0xff,
8309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    PROC_ZERO_TERM = 0,
8319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    PROC_SPACE_TERM = ' ',
8329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    PROC_COMBINE = 0x100,
8339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    PROC_PARENS = 0x200,
83413ac041b9f21043bc7c848a743be618bfd7a67e9Dianne Hackborn    PROC_QUOTES = 0x400,
8354de5a3ac6655f76b67af38712ae5aeb6d7c15938Dianne Hackborn    PROC_CHAR = 0x800,
8369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    PROC_OUT_STRING = 0x1000,
8379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    PROC_OUT_LONG = 0x2000,
8389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    PROC_OUT_FLOAT = 0x4000,
8399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
8409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
841c64edde69d18498fb2954f71a546357b07ab996aEvan Millarjboolean android_os_Process_parseProcLineArray(JNIEnv* env, jobject clazz,
84269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        char* buffer, jint startIndex, jint endIndex, jintArray format,
843c64edde69d18498fb2954f71a546357b07ab996aEvan Millar        jobjectArray outStrings, jlongArray outLongs, jfloatArray outFloats)
8449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
84569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
8469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const jsize NF = env->GetArrayLength(format);
8479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const jsize NS = outStrings ? env->GetArrayLength(outStrings) : 0;
8489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const jsize NL = outLongs ? env->GetArrayLength(outLongs) : 0;
8499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const jsize NR = outFloats ? env->GetArrayLength(outFloats) : 0;
85069a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
8519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jint* formatData = env->GetIntArrayElements(format, 0);
8529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jlong* longsData = outLongs ?
8539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->GetLongArrayElements(outLongs, 0) : NULL;
8549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfloat* floatsData = outFloats ?
8559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->GetFloatArrayElements(outFloats, 0) : NULL;
8569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (formatData == NULL || (NL > 0 && longsData == NULL)
8579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            || (NR > 0 && floatsData == NULL)) {
8589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (formatData != NULL) {
8599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->ReleaseIntArrayElements(format, formatData, 0);
8609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (longsData != NULL) {
8629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->ReleaseLongArrayElements(outLongs, longsData, 0);
8639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (floatsData != NULL) {
8659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->ReleaseFloatArrayElements(outFloats, floatsData, 0);
8669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
8689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return JNI_FALSE;
8699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
871c64edde69d18498fb2954f71a546357b07ab996aEvan Millar    jsize i = startIndex;
8729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jsize di = 0;
87369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
8749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jboolean res = JNI_TRUE;
87569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
8769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (jsize fi=0; fi<NF; fi++) {
87713ac041b9f21043bc7c848a743be618bfd7a67e9Dianne Hackborn        jint mode = formatData[fi];
8789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ((mode&PROC_PARENS) != 0) {
8799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            i++;
8803b1e22e2898e27fe486ac4137692e0f20166e13eBernhard Rosenkränzer        } else if ((mode&PROC_QUOTES) != 0) {
88113ac041b9f21043bc7c848a743be618bfd7a67e9Dianne Hackborn            if (buffer[i] == '"') {
88213ac041b9f21043bc7c848a743be618bfd7a67e9Dianne Hackborn                i++;
88313ac041b9f21043bc7c848a743be618bfd7a67e9Dianne Hackborn            } else {
884