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
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/Log.h>
210795272aa226f4e965968a03daddc53ce30b7cdaMathias Agopian#include <binder/IPCThreadState.h>
220795272aa226f4e965968a03daddc53ce30b7cdaMathias Agopian#include <binder/ProcessState.h>
230795272aa226f4e965968a03daddc53ce30b7cdaMathias Agopian#include <binder/IServiceManager.h>
24f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten#include <cutils/sched_policy.h>
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/String8.h>
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/Vector.h>
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <android_runtime/AndroidRuntime.h>
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "android_util_Binder.h"
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "JNIHelp.h"
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <sys/errno.h>
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <sys/resource.h>
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <sys/types.h>
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <dirent.h>
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <fcntl.h>
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <grp.h>
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <pwd.h>
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <signal.h>
416af763bec7c3f4d50fee8dd0046409bb8a7fe8f6Glenn Kasten#include <unistd.h>
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
43957e58670baad8c5995f1368e3b5280f0dbd891fSan Mehat#define POLICY_DEBUG 0
44160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate#define GUARD_THREAD_PRIORITY 0
45a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectusing namespace android;
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
48160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate#if GUARD_THREAD_PRIORITY
49160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher TateMutex gKeyCreateMutex;
50160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tatestatic pthread_key_t gBgKey = -1;
51160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate#endif
52160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate
53f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten// For both of these, err should be in the errno range (positive), not a status_t (negative)
54f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten
556793ac943afeb16642f477c43ddfd27e498db37bGlenn Kastenstatic void signalExceptionForPriorityError(JNIEnv* env, int err)
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    switch (err) {
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case EINVAL:
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case ESRCH:
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/IllegalArgumentException", "Given thread does not exist");
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case EPERM:
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/SecurityException", "No permission to modify given thread");
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case EACCES:
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/SecurityException", "No permission to set to given priority");
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        default:
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/RuntimeException", "Unknown error");
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
766793ac943afeb16642f477c43ddfd27e498db37bGlenn Kastenstatic void signalExceptionForGroupError(JNIEnv* env, int err)
77e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat{
78e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat    switch (err) {
79e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat        case EINVAL:
80e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
81e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            break;
82e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat        case ESRCH:
83e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            jniThrowException(env, "java/lang/IllegalArgumentException", "Given thread does not exist");
84e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            break;
85e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat        case EPERM:
86e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            jniThrowException(env, "java/lang/SecurityException", "No permission to modify given thread");
87e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            break;
88e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat        case EACCES:
89e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            jniThrowException(env, "java/lang/SecurityException", "No permission to set to given group");
90e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            break;
91e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat        default:
92e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            jniThrowException(env, "java/lang/RuntimeException", "Unknown error");
93e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat            break;
94e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat    }
95e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat}
96e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjint android_os_Process_myPid(JNIEnv* env, jobject clazz)
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return getpid();
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjint android_os_Process_myUid(JNIEnv* env, jobject clazz)
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return getuid();
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjint android_os_Process_myTid(JNIEnv* env, jobject clazz)
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
109887f355f99ff83d568ef2885a4fdcaae475583dfDianne Hackborn    return androidGetTid();
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjint android_os_Process_getUidForName(JNIEnv* env, jobject clazz, jstring name)
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (name == NULL) {
11569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowNullPointerException(env, NULL);
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11869a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const jchar* str16 = env->GetStringCritical(name, 0);
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    String8 name8;
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (str16) {
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        name8 = String8(str16, env->GetStringLength(name));
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseStringCritical(name, str16);
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const size_t N = name8.size();
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (N > 0) {
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const char* str = name8.string();
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        for (size_t i=0; i<N; i++) {
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (str[i] < '0' || str[i] > '9') {
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                struct passwd* pwd = getpwnam(str);
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (pwd == NULL) {
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    return -1;
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return pwd->pw_uid;
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return atoi(str);
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return -1;
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjint android_os_Process_getGidForName(JNIEnv* env, jobject clazz, jstring name)
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (name == NULL) {
14669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowNullPointerException(env, NULL);
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14969a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const jchar* str16 = env->GetStringCritical(name, 0);
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    String8 name8;
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (str16) {
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        name8 = String8(str16, env->GetStringLength(name));
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseStringCritical(name, str16);
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const size_t N = name8.size();
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (N > 0) {
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const char* str = name8.string();
1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        for (size_t i=0; i<N; i++) {
1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (str[i] < '0' || str[i] > '9') {
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                struct group* grp = getgrnam(str);
1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (grp == NULL) {
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    return -1;
1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return grp->gr_gid;
1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return atoi(str);
1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return -1;
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
174f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kastenvoid android_os_Process_setThreadGroup(JNIEnv* env, jobject clazz, int tid, jint grp)
175e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat{
176f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten    ALOGV("%s tid=%d grp=%d", __func__, tid, grp);
177f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten    SchedPolicy sp = (SchedPolicy) grp;
178f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten    int res = set_sched_policy(tid, sp);
179887f355f99ff83d568ef2885a4fdcaae475583dfDianne Hackborn    if (res != NO_ERROR) {
180f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten        signalExceptionForGroupError(env, -res);
181e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat    }
182e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat}
183e9d376b801b7890b1ef5006ed55de4208e64bb63San Mehat
18469a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughesvoid android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jint grp)
1853e458241d9930465a20a861ecb42744355d48e48San Mehat{
186f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten    ALOGV("%s pid=%d grp=%d", __func__, pid, grp);
1873e458241d9930465a20a861ecb42744355d48e48San Mehat    DIR *d;
1883e458241d9930465a20a861ecb42744355d48e48San Mehat    FILE *fp;
1893e458241d9930465a20a861ecb42744355d48e48San Mehat    char proc_path[255];
1903e458241d9930465a20a861ecb42744355d48e48San Mehat    struct dirent *de;
1913e458241d9930465a20a861ecb42744355d48e48San Mehat
192f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten    if ((grp == SP_FOREGROUND) || (grp > SP_MAX)) {
1936793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten        signalExceptionForGroupError(env, EINVAL);
1943e458241d9930465a20a861ecb42744355d48e48San Mehat        return;
1953e458241d9930465a20a861ecb42744355d48e48San Mehat    }
1963e458241d9930465a20a861ecb42744355d48e48San Mehat
197f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten    bool isDefault = false;
198f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten    if (grp < 0) {
199f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten        grp = SP_FOREGROUND;
200f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten        isDefault = true;
201f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten    }
202f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten    SchedPolicy sp = (SchedPolicy) grp;
203f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten
204a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat#if POLICY_DEBUG
205a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat    char cmdline[32];
206a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat    int fd;
207a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat
208a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat    strcpy(cmdline, "unknown");
209a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat
210a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat    sprintf(proc_path, "/proc/%d/cmdline", pid);
211a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat    fd = open(proc_path, O_RDONLY);
212a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat    if (fd >= 0) {
213a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat        int rc = read(fd, cmdline, sizeof(cmdline)-1);
214a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat        cmdline[rc] = 0;
215a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat        close(fd);
216a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat    }
21769a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
218f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten    if (sp == SP_BACKGROUND) {
2195baa3a62a97544669fba6d65a11c07f252e654ddSteve Block        ALOGD("setProcessGroup: vvv pid %d (%s)", pid, cmdline);
220a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat    } else {
2215baa3a62a97544669fba6d65a11c07f252e654ddSteve Block        ALOGD("setProcessGroup: ^^^ pid %d (%s)", pid, cmdline);
222a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat    }
223a5109a878eeff22e32ee5ce1b1cd15e8daad5234San Mehat#endif
2243e458241d9930465a20a861ecb42744355d48e48San Mehat    sprintf(proc_path, "/proc/%d/task", pid);
2253e458241d9930465a20a861ecb42744355d48e48San Mehat    if (!(d = opendir(proc_path))) {
2261fd0ec738b0a2b97cc28701aa37b1a9869afc684San Mehat        // If the process exited on us, don't generate an exception
2271fd0ec738b0a2b97cc28701aa37b1a9869afc684San Mehat        if (errno != ENOENT)
2286793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten            signalExceptionForGroupError(env, errno);
2293e458241d9930465a20a861ecb42744355d48e48San Mehat        return;
2303e458241d9930465a20a861ecb42744355d48e48San Mehat    }
2313e458241d9930465a20a861ecb42744355d48e48San Mehat
2323e458241d9930465a20a861ecb42744355d48e48San Mehat    while ((de = readdir(d))) {
2337e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat        int t_pid;
2347e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat        int t_pri;
2357e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat
2363e458241d9930465a20a861ecb42744355d48e48San Mehat        if (de->d_name[0] == '.')
2373e458241d9930465a20a861ecb42744355d48e48San Mehat            continue;
2387e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat        t_pid = atoi(de->d_name);
2397e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat
2407e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat        if (!t_pid) {
2413762c311729fe9f3af085c14c5c1fb471d994c03Steve Block            ALOGE("Error getting pid for '%s'\n", de->d_name);
2427e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat            continue;
2437e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat        }
2441fd0ec738b0a2b97cc28701aa37b1a9869afc684San Mehat
24507b0465095bd9ab3412caefa4fcacbdc3825c64bGlenn Kasten        t_pri = getpriority(PRIO_PROCESS, t_pid);
24607b0465095bd9ab3412caefa4fcacbdc3825c64bGlenn Kasten
24707b0465095bd9ab3412caefa4fcacbdc3825c64bGlenn Kasten        if (t_pri <= ANDROID_PRIORITY_AUDIO) {
24807b0465095bd9ab3412caefa4fcacbdc3825c64bGlenn Kasten            int scheduler = sched_getscheduler(t_pid);
24907b0465095bd9ab3412caefa4fcacbdc3825c64bGlenn Kasten            if ((scheduler == SCHED_FIFO) || (scheduler == SCHED_RR)) {
25007b0465095bd9ab3412caefa4fcacbdc3825c64bGlenn Kasten                // This task wants to stay in it's current audio group so it can keep it's budget
25107b0465095bd9ab3412caefa4fcacbdc3825c64bGlenn Kasten                continue;
25207b0465095bd9ab3412caefa4fcacbdc3825c64bGlenn Kasten            }
25307b0465095bd9ab3412caefa4fcacbdc3825c64bGlenn Kasten        }
2547e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat
25507b0465095bd9ab3412caefa4fcacbdc3825c64bGlenn Kasten        if (isDefault) {
256f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten            if (t_pri >= ANDROID_PRIORITY_BACKGROUND) {
257f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten                // This task wants to stay at background
258f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten                continue;
259f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten            }
2607e63789a0e0689d940609b1daceebc1bc08dcbefSan Mehat        }
26169a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
262f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten        int err = set_sched_policy(t_pid, sp);
263f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten        if (err != NO_ERROR) {
264f1b56449f58963e4f0473d5e26961f68c31759f4Glenn Kasten            signalExceptionForGroupError(env, -err);
2650a42b811aea490a9a605b75f0320101f6eafd283San Mehat            break;
266242d65bf9faf1d2bc3468490e510551140e23462San Mehat        }
2673e458241d9930465a20a861ecb42744355d48e48San Mehat    }
2683e458241d9930465a20a861ecb42744355d48e48San Mehat    closedir(d);
2693e458241d9930465a20a861ecb42744355d48e48San Mehat}
2703e458241d9930465a20a861ecb42744355d48e48San Mehat
271160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tatestatic void android_os_Process_setCanSelfBackground(JNIEnv* env, jobject clazz, jboolean bgOk) {
272160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    // Establishes the calling thread as illegal to put into the background.
273160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    // Typically used only for the system process's main looper.
274160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate#if GUARD_THREAD_PRIORITY
27571f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("Process.setCanSelfBackground(%d) : tid=%d", bgOk, androidGetTid());
276160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    {
277160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate        Mutex::Autolock _l(gKeyCreateMutex);
278160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate        if (gBgKey == -1) {
279160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate            pthread_key_create(&gBgKey, NULL);
280160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate        }
281160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    }
282160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate
283160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    // inverted:  not-okay, we set a sentinel value
284160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    pthread_setspecific(gBgKey, (void*)(bgOk ? 0 : 0xbaad));
285160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate#endif
286160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate}
287160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate
2886793ac943afeb16642f477c43ddfd27e498db37bGlenn Kastenvoid android_os_Process_setThreadScheduler(JNIEnv* env, jclass clazz,
2896793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten                                              jint tid, jint policy, jint pri)
2906793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten{
291cc767191cfb675f744e0165608b0a4196aba2b37Glenn Kasten#ifdef HAVE_SCHED_SETSCHEDULER
2926793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten    struct sched_param param;
2936793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten    param.sched_priority = pri;
2946793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten    int rc = sched_setscheduler(tid, policy, &param);
2956793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten    if (rc) {
2966793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten        signalExceptionForPriorityError(env, errno);
2976793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten    }
298cc767191cfb675f744e0165608b0a4196aba2b37Glenn Kasten#else
299cc767191cfb675f744e0165608b0a4196aba2b37Glenn Kasten    signalExceptionForPriorityError(env, ENOSYS);
300cc767191cfb675f744e0165608b0a4196aba2b37Glenn Kasten#endif
3016793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten}
3026793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten
3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid android_os_Process_setThreadPriority(JNIEnv* env, jobject clazz,
3049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                              jint pid, jint pri)
3059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
306160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate#if GUARD_THREAD_PRIORITY
307160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    // if we're putting the current thread into the background, check the TLS
308160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    // to make sure this thread isn't guarded.  If it is, raise an exception.
309160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    if (pri >= ANDROID_PRIORITY_BACKGROUND) {
310160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate        if (pid == androidGetTid()) {
311160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate            void* bgOk = pthread_getspecific(gBgKey);
312160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate            if (bgOk == ((void*)0xbaad)) {
3133762c311729fe9f3af085c14c5c1fb471d994c03Steve Block                ALOGE("Thread marked fg-only put self in background!");
314160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate                jniThrowException(env, "java/lang/SecurityException", "May not put this thread into background");
315160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate                return;
316160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate            }
317160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate        }
318160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate    }
319160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate#endif
320160edb3645f8b7012bab70ae6e6e8c4a5733082bChristopher Tate
321887f355f99ff83d568ef2885a4fdcaae475583dfDianne Hackborn    int rc = androidSetThreadPriority(pid, pri);
322887f355f99ff83d568ef2885a4fdcaae475583dfDianne Hackborn    if (rc != 0) {
323887f355f99ff83d568ef2885a4fdcaae475583dfDianne Hackborn        if (rc == INVALID_OPERATION) {
3246793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten            signalExceptionForPriorityError(env, errno);
325887f355f99ff83d568ef2885a4fdcaae475583dfDianne Hackborn        } else {
3266793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten            signalExceptionForGroupError(env, errno);
327887f355f99ff83d568ef2885a4fdcaae475583dfDianne Hackborn        }
3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
32969a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
3306215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block    //ALOGI("Setting priority of %d: %d, getpriority returns %d\n",
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //     pid, pri, getpriority(PRIO_PROCESS, pid));
3329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid android_os_Process_setCallingThreadPriority(JNIEnv* env, jobject clazz,
3359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                        jint pri)
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jint tid = android_os_Process_myTid(env, clazz);
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    android_os_Process_setThreadPriority(env, clazz, tid, pri);
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjint android_os_Process_getThreadPriority(JNIEnv* env, jobject clazz,
3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                              jint pid)
3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    errno = 0;
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jint pri = getpriority(PRIO_PROCESS, pid);
3469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (errno != 0) {
3476793ac943afeb16642f477c43ddfd27e498db37bGlenn Kasten        signalExceptionForPriorityError(env, errno);
3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3496215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block    //ALOGI("Returning priority of %d: %d\n", pid, pri);
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return pri;
3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjboolean android_os_Process_setOomAdj(JNIEnv* env, jobject clazz,
3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                      jint pid, jint adj)
3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#ifdef HAVE_OOM_ADJ
35710e89712863f5b91a2982dc1783fbdfe39c1485dJeff Brown    char text[64];
35810e89712863f5b91a2982dc1783fbdfe39c1485dJeff Brown    sprintf(text, "/proc/%d/oom_adj", pid);
35910e89712863f5b91a2982dc1783fbdfe39c1485dJeff Brown    int fd = open(text, O_WRONLY);
36010e89712863f5b91a2982dc1783fbdfe39c1485dJeff Brown    if (fd >= 0) {
36110e89712863f5b91a2982dc1783fbdfe39c1485dJeff Brown        sprintf(text, "%d", adj);
36210e89712863f5b91a2982dc1783fbdfe39c1485dJeff Brown        write(fd, text, strlen(text));
36310e89712863f5b91a2982dc1783fbdfe39c1485dJeff Brown        close(fd);
3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
36510e89712863f5b91a2982dc1783fbdfe39c1485dJeff Brown    return true;
3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return false;
3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid android_os_Process_setArgV0(JNIEnv* env, jobject clazz, jstring name)
3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (name == NULL) {
37369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowNullPointerException(env, NULL);
3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
37669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const jchar* str = env->GetStringCritical(name, 0);
3789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    String8 name8;
3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (str) {
3809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        name8 = String8(str, env->GetStringLength(name));
3819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseStringCritical(name, str);
3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (name8.size() > 0) {
3859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        ProcessState::self()->setArgV0(name8.string());
3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjint android_os_Process_setUid(JNIEnv* env, jobject clazz, jint uid)
3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return setuid(uid) == 0 ? 0 : errno;
3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjint android_os_Process_setGid(JNIEnv* env, jobject clazz, jint uid)
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return setgid(uid) == 0 ? 0 : errno;
3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic int pid_compare(const void* v1, const void* v2)
4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
4016215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block    //ALOGI("Compare %d vs %d\n", *((const jint*)v1), *((const jint*)v2));
4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return *((const jint*)v1) - *((const jint*)v2);
4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
40559325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackbornstatic jlong getFreeMemoryImpl(const char* const sums[], const int sumsLen[], int num)
4069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int fd = open("/proc/meminfo", O_RDONLY);
40869a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (fd < 0) {
4108564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("Unable to open /proc/meminfo");
4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
41369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    char buffer[256];
4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const int len = read(fd, buffer, sizeof(buffer)-1);
4169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    close(fd);
41769a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
4189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (len < 0) {
4198564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("Unable to read /proc/meminfo");
4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
4219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    buffer[len] = 0;
4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int numFound = 0;
4250bca96bcbfe559f9330a01f723c5c9cba51ec05aMarco Nelissen    jlong mem = 0;
42669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
4279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    char* p = buffer;
42859325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn    while (*p && numFound < num) {
4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int i = 0;
4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        while (sums[i]) {
4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (strncmp(p, sums[i], sumsLen[i]) == 0) {
4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                p += sumsLen[i];
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                while (*p == ' ') p++;
4349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                char* num = p;
4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                while (*p >= '0' && *p <= '9') p++;
4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (*p != 0) {
4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    *p = 0;
4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    p++;
4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (*p == 0) p--;
4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
4410bca96bcbfe559f9330a01f723c5c9cba51ec05aMarco Nelissen                mem += atoll(num) * 1024;
4429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                numFound++;
4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                break;
4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            i++;
4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        p++;
4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
44969a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
4509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return numFound > 0 ? mem : -1;
4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
45359325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackbornstatic jlong android_os_Process_getFreeMemory(JNIEnv* env, jobject clazz)
45459325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn{
45559325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn    static const char* const sums[] = { "MemFree:", "Cached:", NULL };
45659325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn    static const int sumsLen[] = { strlen("MemFree:"), strlen("Cached:"), 0 };
45759325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn    return getFreeMemoryImpl(sums, sumsLen, 2);
45859325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn}
45959325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn
46059325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackbornstatic jlong android_os_Process_getTotalMemory(JNIEnv* env, jobject clazz)
46159325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn{
46259325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn    static const char* const sums[] = { "MemTotal:", NULL };
46359325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn    static const int sumsLen[] = { strlen("MemTotal:"), 0 };
46459325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn    return getFreeMemoryImpl(sums, sumsLen, 1);
46559325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn}
46659325eb31f25704bb88c348160bb69e7c1aa3b48Dianne Hackborn
4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid android_os_Process_readProcLines(JNIEnv* env, jobject clazz, jstring fileStr,
4689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                      jobjectArray reqFields, jlongArray outFields)
4699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
4706215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block    //ALOGI("getMemInfo: %p %p", reqFields, outFields);
47169a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
4729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (fileStr == NULL || reqFields == NULL || outFields == NULL) {
47369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowNullPointerException(env, NULL);
4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
4759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
47669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const char* file8 = env->GetStringUTFChars(fileStr, NULL);
4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (file8 == NULL) {
4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
4809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    String8 file(file8);
4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->ReleaseStringUTFChars(fileStr, file8);
48369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jsize count = env->GetArrayLength(reqFields);
4859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (count > env->GetArrayLength(outFields)) {
4869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalArgumentException", "Array lengths differ");
4879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
4889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
48969a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
4909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Vector<String8> fields;
4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int i;
49269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
4939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (i=0; i<count; i++) {
4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jobject obj = env->GetObjectArrayElement(reqFields, i);
4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (obj != NULL) {
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const char* str8 = env->GetStringUTFChars((jstring)obj, NULL);
4976215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block            //ALOGI("String at %d: %p = %s", i, obj, str8);
4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (str8 == NULL) {
49969a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes                jniThrowNullPointerException(env, "Element in reqFields");
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return;
5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            fields.add(String8(str8));
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->ReleaseStringUTFChars((jstring)obj, str8);
5049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
50569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes            jniThrowNullPointerException(env, "Element in reqFields");
5069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
5079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
50969a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
5109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jlong* sizesArray = env->GetLongArrayElements(outFields, 0);
5119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (sizesArray == NULL) {
5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
51469a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
5156215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block    //ALOGI("Clearing %d sizes", count);
5169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (i=0; i<count; i++) {
5179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        sizesArray[i] = 0;
5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
51969a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
5209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int fd = open(file.string(), O_RDONLY);
52169a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
5229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (fd >= 0) {
5239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const size_t BUFFER_SIZE = 2048;
5249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        char* buffer = (char*)malloc(BUFFER_SIZE);
5259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int len = read(fd, buffer, BUFFER_SIZE-1);
5269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        close(fd);
52769a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
5289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (len < 0) {
5298564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("Unable to read %s", file.string());
5309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            len = 0;
5319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        buffer[len] = 0;
53369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
5349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int foundCount = 0;
53569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
5369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        char* p = buffer;
5379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        while (*p && foundCount < count) {
5389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            bool skipToEol = true;
5396215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block            //ALOGI("Parsing at: %s", p);
5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (i=0; i<count; i++) {
5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                const String8& field = fields[i];
5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (strncmp(p, field.string(), field.length()) == 0) {
5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    p += field.length();
544add868cebaf62cffe96e79764ea0b7f2320a03ebAmith Yamasani                    while (*p == ' ' || *p == '\t') p++;
5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    char* num = p;
5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    while (*p >= '0' && *p <= '9') p++;
5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    skipToEol = *p != '\n';
5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (*p != 0) {
5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        *p = 0;
5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        p++;
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    char* end;
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    sizesArray[i] = strtoll(num, &end, 10);
5546215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block                    //ALOGI("Field %s = %d", field.string(), sizesArray[i]);
5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    foundCount++;
5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    break;
5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
5599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (skipToEol) {
5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                while (*p && *p != '\n') {
5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    p++;
5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (*p == '\n') {
5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    p++;
5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
56869a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        free(buffer);
5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else {
5718564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("Unable to open %s", file.string());
5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
57369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
5746215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block    //ALOGI("Done!");
5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->ReleaseLongArrayElements(outFields, sizesArray, 0);
5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectjintArray android_os_Process_getPids(JNIEnv* env, jobject clazz,
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                     jstring file, jintArray lastArray)
5809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (file == NULL) {
58269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowNullPointerException(env, NULL);
5839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
5849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
58569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const char* file8 = env->GetStringUTFChars(file, NULL);
5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (file8 == NULL) {
5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
59169a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
5929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    DIR* dirp = opendir(file8);
59369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
5949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->ReleaseStringUTFChars(file, file8);
59569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if(dirp == NULL) {
5979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
5989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
59969a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jsize curCount = 0;
6019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jint* curData = NULL;
6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (lastArray != NULL) {
6039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        curCount = env->GetArrayLength(lastArray);
6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        curData = env->GetIntArrayElements(lastArray, 0);
6059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
60669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jint curPos = 0;
60869a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    struct dirent* entry;
6109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    while ((entry=readdir(dirp)) != NULL) {
6119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const char* p = entry->d_name;
6129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        while (*p) {
6139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (*p < '0' || *p > '9') break;
6149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            p++;
6159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (*p != 0) continue;
61769a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        char* end;
6199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int pid = strtol(entry->d_name, &end, 10);
6206215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block        //ALOGI("File %s pid=%d\n", entry->d_name, pid);
6219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (curPos >= curCount) {
6229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jsize newCount = (curCount == 0) ? 10 : (curCount*2);
6239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jintArray newArray = env->NewIntArray(newCount);
6249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (newArray == NULL) {
6259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                closedir(dirp);
6269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
6279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return NULL;
6289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
6299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jint* newData = env->GetIntArrayElements(newArray, 0);
6309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (curData != NULL) {
6319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                memcpy(newData, curData, sizeof(jint)*curCount);
6329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                env->ReleaseIntArrayElements(lastArray, curData, 0);
6339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
6349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            lastArray = newArray;
6359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            curCount = newCount;
6369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            curData = newData;
6379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
63869a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        curData[curPos] = pid;
6409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        curPos++;
6419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
64269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    closedir(dirp);
64469a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (curData != NULL && curPos > 0) {
6469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        qsort(curData, curPos, sizeof(jint), pid_compare);
6479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
64869a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    while (curPos < curCount) {
6509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        curData[curPos] = -1;
6519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        curPos++;
6529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
65369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (curData != NULL) {
6559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseIntArrayElements(lastArray, curData, 0);
6569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
65769a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return lastArray;
6599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectenum {
6629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    PROC_TERM_MASK = 0xff,
6639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    PROC_ZERO_TERM = 0,
6649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    PROC_SPACE_TERM = ' ',
6659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    PROC_COMBINE = 0x100,
6669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    PROC_PARENS = 0x200,
6679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    PROC_OUT_STRING = 0x1000,
6689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    PROC_OUT_LONG = 0x2000,
6699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    PROC_OUT_FLOAT = 0x4000,
6709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
6719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
672c64edde69d18498fb2954f71a546357b07ab996aEvan Millarjboolean android_os_Process_parseProcLineArray(JNIEnv* env, jobject clazz,
67369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        char* buffer, jint startIndex, jint endIndex, jintArray format,
674c64edde69d18498fb2954f71a546357b07ab996aEvan Millar        jobjectArray outStrings, jlongArray outLongs, jfloatArray outFloats)
6759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
67669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const jsize NF = env->GetArrayLength(format);
6789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const jsize NS = outStrings ? env->GetArrayLength(outStrings) : 0;
6799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const jsize NL = outLongs ? env->GetArrayLength(outLongs) : 0;
6809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const jsize NR = outFloats ? env->GetArrayLength(outFloats) : 0;
68169a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
6829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jint* formatData = env->GetIntArrayElements(format, 0);
6839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jlong* longsData = outLongs ?
6849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->GetLongArrayElements(outLongs, 0) : NULL;
6859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfloat* floatsData = outFloats ?
6869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->GetFloatArrayElements(outFloats, 0) : NULL;
6879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (formatData == NULL || (NL > 0 && longsData == NULL)
6889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            || (NR > 0 && floatsData == NULL)) {
6899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (formatData != NULL) {
6909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->ReleaseIntArrayElements(format, formatData, 0);
6919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (longsData != NULL) {
6939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->ReleaseLongArrayElements(outLongs, longsData, 0);
6949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (floatsData != NULL) {
6969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->ReleaseFloatArrayElements(outFloats, floatsData, 0);
6979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
6999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return JNI_FALSE;
7009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
702c64edde69d18498fb2954f71a546357b07ab996aEvan Millar    jsize i = startIndex;
7039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jsize di = 0;
70469a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
7059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jboolean res = JNI_TRUE;
70669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
7079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (jsize fi=0; fi<NF; fi++) {
7089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jint mode = formatData[fi];
7099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ((mode&PROC_PARENS) != 0) {
7109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            i++;
7119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const char term = (char)(mode&PROC_TERM_MASK);
7139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jsize start = i;
714c64edde69d18498fb2954f71a546357b07ab996aEvan Millar        if (i >= endIndex) {
7159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            res = JNI_FALSE;
7169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
7179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
71869a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
7199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jsize end = -1;
7209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ((mode&PROC_PARENS) != 0) {
721c64edde69d18498fb2954f71a546357b07ab996aEvan Millar            while (buffer[i] != ')' && i < endIndex) {
7229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                i++;
7239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
7249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            end = i;
7259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            i++;
7269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
727c64edde69d18498fb2954f71a546357b07ab996aEvan Millar        while (buffer[i] != term && i < endIndex) {
7289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            i++;
7299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (end < 0) {
7319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            end = i;
7329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
73369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
734c64edde69d18498fb2954f71a546357b07ab996aEvan Millar        if (i < endIndex) {
7359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            i++;
7369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if ((mode&PROC_COMBINE) != 0) {
737c64edde69d18498fb2954f71a546357b07ab996aEvan Millar                while (buffer[i] == term && i < endIndex) {
7389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    i++;
7399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
7409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
7419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
74269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
7436215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block        //ALOGI("Field %d: %d-%d dest=%d mode=0x%x\n", i, start, end, di, mode);
74469a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
7459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ((mode&(PROC_OUT_FLOAT|PROC_OUT_LONG|PROC_OUT_STRING)) != 0) {
7469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            char c = buffer[end];
7479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            buffer[end] = 0;
7489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if ((mode&PROC_OUT_FLOAT) != 0 && di < NR) {
7499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                char* end;
7509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                floatsData[di] = strtof(buffer+start, &end);
7519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
7529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if ((mode&PROC_OUT_LONG) != 0 && di < NL) {
7539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                char* end;
7549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                longsData[di] = strtoll(buffer+start, &end, 10);
7559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
7569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if ((mode&PROC_OUT_STRING) != 0 && di < NS) {
7579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                jstring str = env->NewStringUTF(buffer+start);
7589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                env->SetObjectArrayElement(outStrings, di, str);
7599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
7609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            buffer[end] = c;
7619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            di++;
7629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
76469a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
7659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->ReleaseIntArrayElements(format, formatData, 0);
7669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (longsData != NULL) {
7679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseLongArrayElements(outLongs, longsData, 0);
7689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (floatsData != NULL) {
7709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseFloatArrayElements(outFloats, floatsData, 0);
7719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
77269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
7739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return res;
7749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
7759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
776c64edde69d18498fb2954f71a546357b07ab996aEvan Millarjboolean android_os_Process_parseProcLine(JNIEnv* env, jobject clazz,
77769a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jbyteArray buffer, jint startIndex, jint endIndex, jintArray format,
778c64edde69d18498fb2954f71a546357b07ab996aEvan Millar        jobjectArray outStrings, jlongArray outLongs, jfloatArray outFloats)
779c64edde69d18498fb2954f71a546357b07ab996aEvan Millar{
780c64edde69d18498fb2954f71a546357b07ab996aEvan Millar        jbyte* bufferArray = env->GetByteArrayElements(buffer, NULL);
781c64edde69d18498fb2954f71a546357b07ab996aEvan Millar
78269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jboolean result = android_os_Process_parseProcLineArray(env, clazz,
78369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes                (char*) bufferArray, startIndex, endIndex, format, outStrings,
784c64edde69d18498fb2954f71a546357b07ab996aEvan Millar                outLongs, outFloats);
78569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
786c64edde69d18498fb2954f71a546357b07ab996aEvan Millar        env->ReleaseByteArrayElements(buffer, bufferArray, 0);
78769a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
788c64edde69d18498fb2954f71a546357b07ab996aEvan Millar        return result;
789c64edde69d18498fb2954f71a546357b07ab996aEvan Millar}
790c64edde69d18498fb2954f71a546357b07ab996aEvan Millar
791c64edde69d18498fb2954f71a546357b07ab996aEvan Millarjboolean android_os_Process_readProcFile(JNIEnv* env, jobject clazz,
792c64edde69d18498fb2954f71a546357b07ab996aEvan Millar        jstring file, jintArray format, jobjectArray outStrings,
793c64edde69d18498fb2954f71a546357b07ab996aEvan Millar        jlongArray outLongs, jfloatArray outFloats)
794c64edde69d18498fb2954f71a546357b07ab996aEvan Millar{
795c64edde69d18498fb2954f71a546357b07ab996aEvan Millar    if (file == NULL || format == NULL) {
79669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowNullPointerException(env, NULL);
797c64edde69d18498fb2954f71a546357b07ab996aEvan Millar        return JNI_FALSE;
798c64edde69d18498fb2954f71a546357b07ab996aEvan Millar    }
799c64edde69d18498fb2954f71a546357b07ab996aEvan Millar
800c64edde69d18498fb2954f71a546357b07ab996aEvan Millar    const char* file8 = env->GetStringUTFChars(file, NULL);
801c64edde69d18498fb2954f71a546357b07ab996aEvan Millar    if (file8 == NULL) {
802c64edde69d18498fb2954f71a546357b07ab996aEvan Millar        jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
803c64edde69d18498fb2954f71a546357b07ab996aEvan Millar        return JNI_FALSE;
804c64edde69d18498fb2954f71a546357b07ab996aEvan Millar    }
805c64edde69d18498fb2954f71a546357b07ab996aEvan Millar    int fd = open(file8, O_RDONLY);
806c64edde69d18498fb2954f71a546357b07ab996aEvan Millar    env->ReleaseStringUTFChars(file, file8);
80769a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
808c64edde69d18498fb2954f71a546357b07ab996aEvan Millar    if (fd < 0) {
8098564c8da817a845353d213acd8636b76f567b234Steve Block        //ALOGW("Unable to open process file: %s\n", file8);
810c64edde69d18498fb2954f71a546357b07ab996aEvan Millar        return JNI_FALSE;
811c64edde69d18498fb2954f71a546357b07ab996aEvan Millar    }
81269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
813c64edde69d18498fb2954f71a546357b07ab996aEvan Millar    char buffer[256];
814c64edde69d18498fb2954f71a546357b07ab996aEvan Millar    const int len = read(fd, buffer, sizeof(buffer)-1);
815c64edde69d18498fb2954f71a546357b07ab996aEvan Millar    close(fd);
81669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
817c64edde69d18498fb2954f71a546357b07ab996aEvan Millar    if (len < 0) {
8188564c8da817a845353d213acd8636b76f567b234Steve Block        //ALOGW("Unable to open process file: %s fd=%d\n", file8, fd);
819c64edde69d18498fb2954f71a546357b07ab996aEvan Millar        return JNI_FALSE;
820c64edde69d18498fb2954f71a546357b07ab996aEvan Millar    }
821c64edde69d18498fb2954f71a546357b07ab996aEvan Millar    buffer[len] = 0;
82269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
82369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes    return android_os_Process_parseProcLineArray(env, clazz, buffer, 0, len,
824c64edde69d18498fb2954f71a546357b07ab996aEvan Millar            format, outStrings, outLongs, outFloats);
82569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
826c64edde69d18498fb2954f71a546357b07ab996aEvan Millar}
827c64edde69d18498fb2954f71a546357b07ab996aEvan Millar
8289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid android_os_Process_setApplicationObject(JNIEnv* env, jobject clazz,
8299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                             jobject binderObject)
8309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
8319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (binderObject == NULL) {
83269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowNullPointerException(env, NULL);
8339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
8349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<IBinder> binder = ibinderForJavaObject(env, binderObject);
8379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid android_os_Process_sendSignal(JNIEnv* env, jobject clazz, jint pid, jint sig)
8409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
8419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (pid > 0) {
8426215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block        ALOGI("Sending signal. PID: %d SIG: %d", pid, sig);
8439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        kill(pid, sig);
8449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
847906497c574d45d8dfd295b16dece0d0bc32c0895Dianne Hackbornvoid android_os_Process_sendSignalQuiet(JNIEnv* env, jobject clazz, jint pid, jint sig)
848906497c574d45d8dfd295b16dece0d0bc32c0895Dianne Hackborn{
849906497c574d45d8dfd295b16dece0d0bc32c0895Dianne Hackborn    if (pid > 0) {
850906497c574d45d8dfd295b16dece0d0bc32c0895Dianne Hackborn        kill(pid, sig);
851906497c574d45d8dfd295b16dece0d0bc32c0895Dianne Hackborn    }
852906497c574d45d8dfd295b16dece0d0bc32c0895Dianne Hackborn}
853906497c574d45d8dfd295b16dece0d0bc32c0895Dianne Hackborn
8549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jlong android_os_Process_getElapsedCpuTime(JNIEnv* env, jobject clazz)
8559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
8569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    struct timespec ts;
8579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int res = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
85969a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
8609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (res != 0) {
8619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return (jlong) 0;
86269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes    }
86369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
8649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    nsecs_t when = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
8659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return (jlong) nanoseconds_to_milliseconds(when);
8669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jlong android_os_Process_getPss(JNIEnv* env, jobject clazz, jint pid)
8699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
8709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    char filename[64];
8719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    snprintf(filename, sizeof(filename), "/proc/%d/smaps", pid);
8739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    FILE * file = fopen(filename, "r");
8759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (!file) {
8769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return (jlong) -1;
8779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Tally up all of the Pss from the various maps
8809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    char line[256];
8819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jlong pss = 0;
8829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    while (fgets(line, sizeof(line), file)) {
8839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jlong v;
8849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (sscanf(line, "Pss: %lld kB", &v) == 1) {
8859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            pss += v;
8869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    fclose(file);
8909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Return the Pss value in bytes, not kilobytes
8929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return pss * 1024;
8939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
895f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne HackbornjintArray android_os_Process_getPidsForCommands(JNIEnv* env, jobject clazz,
896f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        jobjectArray commandNames)
897f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn{
898f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn    if (commandNames == NULL) {
899f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        jniThrowNullPointerException(env, NULL);
900f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        return NULL;
901f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn    }
902f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn
903f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn    Vector<String8> commands;
904f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn
905f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn    jsize count = env->GetArrayLength(commandNames);
906f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn
907f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn    for (int i=0; i<count; i++) {
908f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        jobject obj = env->GetObjectArrayElement(commandNames, i);
909f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        if (obj != NULL) {
910f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn            const char* str8 = env->GetStringUTFChars((jstring)obj, NULL);
911f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn            if (str8 == NULL) {
912f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn                jniThrowNullPointerException(env, "Element in commandNames");
913f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn                return NULL;
914f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn            }
915f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn            commands.add(String8(str8));
916f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn            env->ReleaseStringUTFChars((jstring)obj, str8);
917f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        } else {
918f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn            jniThrowNullPointerException(env, "Element in commandNames");
919f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn            return NULL;
920f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        }
921f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn    }
922f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn
923f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn    Vector<jint> pids;
924f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn
925f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn    DIR *proc = opendir("/proc");
926f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn    if (proc == NULL) {
927f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        fprintf(stderr, "/proc: %s\n", strerror(errno));
928f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        return NULL;
929f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn    }
930f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn
931f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn    struct dirent *d;
932f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn    while ((d = readdir(proc))) {
933f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        int pid = atoi(d->d_name);
934f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        if (pid <= 0) continue;
935f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn
936f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        char path[PATH_MAX];
937f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        char data[PATH_MAX];
938f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
939f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn
940f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        int fd = open(path, O_RDONLY);
941f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        if (fd < 0) {
942f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn            continue;
943f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        }
944f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        const int len = read(fd, data, sizeof(data)-1);
945f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        close(fd);
946f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn
947f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        if (len < 0) {
948f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn            continue;
949f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        }
950f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        data[len] = 0;
951f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn
952f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        for (int i=0; i<len; i++) {
953f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn            if (data[i] == ' ') {
954f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn                data[i] = 0;
955f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn                break;
956f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn            }
957f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        }
958f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn
959f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        for (size_t i=0; i<commands.size(); i++) {
960f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn            if (commands[i] == data) {
961f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn                pids.add(pid);
962f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn                break;
963f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn            }
964f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        }
965f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn    }
966f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn
967f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn    closedir(proc);
968f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn
969f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn    jintArray pidArray = env->NewIntArray(pids.size());
970f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn    if (pidArray == NULL) {
971f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
972f72467ad9843bf5d4b75fb308386e77ebb5c3447Dianne Hackborn        return