blob: 0290857c97617821b53717efdfca00af9bd41b3d [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* //device/libs/android_runtime/android_util_Process.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
Elliott Hughes69a017b2011-04-08 14:10:28 -07005** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008**
Elliott Hughes69a017b2011-04-08 14:10:28 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010**
Elliott Hughes69a017b2011-04-08 14:10:28 -070011** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015** limitations under the License.
16*/
17
18#define LOG_TAG "Process"
19
20#include <utils/Log.h>
Mathias Agopian07952722009-05-19 19:08:10 -070021#include <binder/IPCThreadState.h>
22#include <binder/ProcessState.h>
23#include <binder/IServiceManager.h>
Glenn Kastenf1b56442012-03-15 16:33:43 -070024#include <cutils/sched_policy.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <utils/String8.h>
26#include <utils/Vector.h>
27
28#include <android_runtime/AndroidRuntime.h>
29
30#include "android_util_Binder.h"
31#include "JNIHelp.h"
32
33#include <sys/errno.h>
34#include <sys/resource.h>
35#include <sys/types.h>
36#include <dirent.h>
37#include <fcntl.h>
38#include <grp.h>
39#include <pwd.h>
40#include <signal.h>
Glenn Kasten6af763b2011-05-04 17:58:57 -070041#include <unistd.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
San Mehat957e5862009-10-29 13:56:49 -070043#define POLICY_DEBUG 0
Christopher Tate160edb32010-06-30 17:46:30 -070044#define GUARD_THREAD_PRIORITY 0
San Mehata5109a82009-10-29 11:48:50 -070045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046using namespace android;
47
Christopher Tate160edb32010-06-30 17:46:30 -070048#if GUARD_THREAD_PRIORITY
49Mutex gKeyCreateMutex;
50static pthread_key_t gBgKey = -1;
51#endif
52
Glenn Kastenf1b56442012-03-15 16:33:43 -070053// For both of these, err should be in the errno range (positive), not a status_t (negative)
54
Glenn Kasten6793ac92011-07-13 12:44:12 -070055static void signalExceptionForPriorityError(JNIEnv* env, int err)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056{
57 switch (err) {
58 case EINVAL:
59 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
60 break;
61 case ESRCH:
62 jniThrowException(env, "java/lang/IllegalArgumentException", "Given thread does not exist");
63 break;
64 case EPERM:
65 jniThrowException(env, "java/lang/SecurityException", "No permission to modify given thread");
66 break;
67 case EACCES:
68 jniThrowException(env, "java/lang/SecurityException", "No permission to set to given priority");
69 break;
70 default:
71 jniThrowException(env, "java/lang/RuntimeException", "Unknown error");
72 break;
73 }
74}
75
Glenn Kasten6793ac92011-07-13 12:44:12 -070076static void signalExceptionForGroupError(JNIEnv* env, int err)
San Mehate9d376b2009-04-21 14:06:36 -070077{
78 switch (err) {
79 case EINVAL:
80 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
81 break;
82 case ESRCH:
83 jniThrowException(env, "java/lang/IllegalArgumentException", "Given thread does not exist");
84 break;
85 case EPERM:
86 jniThrowException(env, "java/lang/SecurityException", "No permission to modify given thread");
87 break;
88 case EACCES:
89 jniThrowException(env, "java/lang/SecurityException", "No permission to set to given group");
90 break;
91 default:
92 jniThrowException(env, "java/lang/RuntimeException", "Unknown error");
93 break;
94 }
95}
96
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097jint android_os_Process_myPid(JNIEnv* env, jobject clazz)
98{
99 return getpid();
100}
101
102jint android_os_Process_myUid(JNIEnv* env, jobject clazz)
103{
104 return getuid();
105}
106
107jint android_os_Process_myTid(JNIEnv* env, jobject clazz)
108{
Dianne Hackborn887f3552009-12-07 17:59:37 -0800109 return androidGetTid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110}
111
112jint android_os_Process_getUidForName(JNIEnv* env, jobject clazz, jstring name)
113{
114 if (name == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700115 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 return -1;
117 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 const jchar* str16 = env->GetStringCritical(name, 0);
120 String8 name8;
121 if (str16) {
122 name8 = String8(str16, env->GetStringLength(name));
123 env->ReleaseStringCritical(name, str16);
124 }
125
126 const size_t N = name8.size();
127 if (N > 0) {
128 const char* str = name8.string();
129 for (size_t i=0; i<N; i++) {
130 if (str[i] < '0' || str[i] > '9') {
131 struct passwd* pwd = getpwnam(str);
132 if (pwd == NULL) {
133 return -1;
134 }
135 return pwd->pw_uid;
136 }
137 }
138 return atoi(str);
139 }
140 return -1;
141}
142
143jint android_os_Process_getGidForName(JNIEnv* env, jobject clazz, jstring name)
144{
145 if (name == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700146 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 return -1;
148 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 const jchar* str16 = env->GetStringCritical(name, 0);
151 String8 name8;
152 if (str16) {
153 name8 = String8(str16, env->GetStringLength(name));
154 env->ReleaseStringCritical(name, str16);
155 }
156
157 const size_t N = name8.size();
158 if (N > 0) {
159 const char* str = name8.string();
160 for (size_t i=0; i<N; i++) {
161 if (str[i] < '0' || str[i] > '9') {
162 struct group* grp = getgrnam(str);
163 if (grp == NULL) {
164 return -1;
165 }
166 return grp->gr_gid;
167 }
168 }
169 return atoi(str);
170 }
171 return -1;
172}
173
Glenn Kastenf1b56442012-03-15 16:33:43 -0700174void android_os_Process_setThreadGroup(JNIEnv* env, jobject clazz, int tid, jint grp)
San Mehate9d376b2009-04-21 14:06:36 -0700175{
Glenn Kastenf1b56442012-03-15 16:33:43 -0700176 ALOGV("%s tid=%d grp=%d", __func__, tid, grp);
177 SchedPolicy sp = (SchedPolicy) grp;
178 int res = set_sched_policy(tid, sp);
Dianne Hackborn887f3552009-12-07 17:59:37 -0800179 if (res != NO_ERROR) {
Glenn Kastenf1b56442012-03-15 16:33:43 -0700180 signalExceptionForGroupError(env, -res);
San Mehate9d376b2009-04-21 14:06:36 -0700181 }
San Mehate9d376b2009-04-21 14:06:36 -0700182}
183
Elliott Hughes69a017b2011-04-08 14:10:28 -0700184void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jint grp)
San Mehat3e458242009-05-19 14:44:16 -0700185{
Glenn Kastenf1b56442012-03-15 16:33:43 -0700186 ALOGV("%s pid=%d grp=%d", __func__, pid, grp);
San Mehat3e458242009-05-19 14:44:16 -0700187 DIR *d;
188 FILE *fp;
189 char proc_path[255];
190 struct dirent *de;
191
Glenn Kastenf1b56442012-03-15 16:33:43 -0700192 if ((grp == SP_FOREGROUND) || (grp > SP_MAX)) {
Glenn Kasten6793ac92011-07-13 12:44:12 -0700193 signalExceptionForGroupError(env, EINVAL);
San Mehat3e458242009-05-19 14:44:16 -0700194 return;
195 }
196
Glenn Kastenf1b56442012-03-15 16:33:43 -0700197 bool isDefault = false;
198 if (grp < 0) {
199 grp = SP_FOREGROUND;
200 isDefault = true;
201 }
202 SchedPolicy sp = (SchedPolicy) grp;
203
San Mehata5109a82009-10-29 11:48:50 -0700204#if POLICY_DEBUG
205 char cmdline[32];
206 int fd;
207
208 strcpy(cmdline, "unknown");
209
210 sprintf(proc_path, "/proc/%d/cmdline", pid);
211 fd = open(proc_path, O_RDONLY);
212 if (fd >= 0) {
213 int rc = read(fd, cmdline, sizeof(cmdline)-1);
214 cmdline[rc] = 0;
215 close(fd);
216 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700217
Glenn Kastenf1b56442012-03-15 16:33:43 -0700218 if (sp == SP_BACKGROUND) {
Steve Block5baa3a62011-12-20 16:23:08 +0000219 ALOGD("setProcessGroup: vvv pid %d (%s)", pid, cmdline);
San Mehata5109a82009-10-29 11:48:50 -0700220 } else {
Steve Block5baa3a62011-12-20 16:23:08 +0000221 ALOGD("setProcessGroup: ^^^ pid %d (%s)", pid, cmdline);
San Mehata5109a82009-10-29 11:48:50 -0700222 }
223#endif
San Mehat3e458242009-05-19 14:44:16 -0700224 sprintf(proc_path, "/proc/%d/task", pid);
225 if (!(d = opendir(proc_path))) {
San Mehat1fd0ec72009-06-15 16:56:52 -0700226 // If the process exited on us, don't generate an exception
227 if (errno != ENOENT)
Glenn Kasten6793ac92011-07-13 12:44:12 -0700228 signalExceptionForGroupError(env, errno);
San Mehat3e458242009-05-19 14:44:16 -0700229 return;
230 }
231
232 while ((de = readdir(d))) {
San Mehat7e637892009-08-06 13:19:19 -0700233 int t_pid;
234 int t_pri;
235
San Mehat3e458242009-05-19 14:44:16 -0700236 if (de->d_name[0] == '.')
237 continue;
San Mehat7e637892009-08-06 13:19:19 -0700238 t_pid = atoi(de->d_name);
San Mehat1fd0ec72009-06-15 16:56:52 -0700239
San Mehat7e637892009-08-06 13:19:19 -0700240 if (!t_pid) {
Steve Block3762c312012-01-06 19:20:56 +0000241 ALOGE("Error getting pid for '%s'\n", de->d_name);
San Mehat7e637892009-08-06 13:19:19 -0700242 continue;
243 }
244
Glenn Kasten07b04652012-04-23 15:00:43 -0700245 t_pri = getpriority(PRIO_PROCESS, t_pid);
San Mehat7e637892009-08-06 13:19:19 -0700246
Glenn Kasten07b04652012-04-23 15:00:43 -0700247 if (t_pri <= ANDROID_PRIORITY_AUDIO) {
248 int scheduler = sched_getscheduler(t_pid);
249 if ((scheduler == SCHED_FIFO) || (scheduler == SCHED_RR)) {
250 // This task wants to stay in it's current audio group so it can keep it's budget
251 continue;
252 }
253 }
254
255 if (isDefault) {
Glenn Kastenf1b56442012-03-15 16:33:43 -0700256 if (t_pri >= ANDROID_PRIORITY_BACKGROUND) {
257 // This task wants to stay at background
258 continue;
259 }
San Mehat7e637892009-08-06 13:19:19 -0700260 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700261
Glenn Kastenf1b56442012-03-15 16:33:43 -0700262 int err = set_sched_policy(t_pid, sp);
263 if (err != NO_ERROR) {
264 signalExceptionForGroupError(env, -err);
San Mehat0a42b812009-12-03 12:21:28 -0800265 break;
San Mehat242d65b2009-09-12 10:10:37 -0700266 }
San Mehat3e458242009-05-19 14:44:16 -0700267 }
268 closedir(d);
269}
270
Christopher Tate160edb32010-06-30 17:46:30 -0700271static void android_os_Process_setCanSelfBackground(JNIEnv* env, jobject clazz, jboolean bgOk) {
272 // Establishes the calling thread as illegal to put into the background.
273 // Typically used only for the system process's main looper.
274#if GUARD_THREAD_PRIORITY
Steve Block71f2cf12011-10-20 11:56:00 +0100275 ALOGV("Process.setCanSelfBackground(%d) : tid=%d", bgOk, androidGetTid());
Christopher Tate160edb32010-06-30 17:46:30 -0700276 {
277 Mutex::Autolock _l(gKeyCreateMutex);
278 if (gBgKey == -1) {
279 pthread_key_create(&gBgKey, NULL);
280 }
281 }
282
283 // inverted: not-okay, we set a sentinel value
284 pthread_setspecific(gBgKey, (void*)(bgOk ? 0 : 0xbaad));
285#endif
286}
287
Glenn Kasten6793ac92011-07-13 12:44:12 -0700288void android_os_Process_setThreadScheduler(JNIEnv* env, jclass clazz,
289 jint tid, jint policy, jint pri)
290{
Glenn Kastencc767192012-01-17 08:38:51 -0800291#ifdef HAVE_SCHED_SETSCHEDULER
Glenn Kasten6793ac92011-07-13 12:44:12 -0700292 struct sched_param param;
293 param.sched_priority = pri;
294 int rc = sched_setscheduler(tid, policy, &param);
295 if (rc) {
296 signalExceptionForPriorityError(env, errno);
297 }
Glenn Kastencc767192012-01-17 08:38:51 -0800298#else
299 signalExceptionForPriorityError(env, ENOSYS);
300#endif
Glenn Kasten6793ac92011-07-13 12:44:12 -0700301}
302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303void android_os_Process_setThreadPriority(JNIEnv* env, jobject clazz,
304 jint pid, jint pri)
305{
Christopher Tate160edb32010-06-30 17:46:30 -0700306#if GUARD_THREAD_PRIORITY
307 // if we're putting the current thread into the background, check the TLS
308 // to make sure this thread isn't guarded. If it is, raise an exception.
309 if (pri >= ANDROID_PRIORITY_BACKGROUND) {
310 if (pid == androidGetTid()) {
311 void* bgOk = pthread_getspecific(gBgKey);
312 if (bgOk == ((void*)0xbaad)) {
Steve Block3762c312012-01-06 19:20:56 +0000313 ALOGE("Thread marked fg-only put self in background!");
Christopher Tate160edb32010-06-30 17:46:30 -0700314 jniThrowException(env, "java/lang/SecurityException", "May not put this thread into background");
315 return;
316 }
317 }
318 }
319#endif
320
Dianne Hackborn887f3552009-12-07 17:59:37 -0800321 int rc = androidSetThreadPriority(pid, pri);
322 if (rc != 0) {
323 if (rc == INVALID_OPERATION) {
Glenn Kasten6793ac92011-07-13 12:44:12 -0700324 signalExceptionForPriorityError(env, errno);
Dianne Hackborn887f3552009-12-07 17:59:37 -0800325 } else {
Glenn Kasten6793ac92011-07-13 12:44:12 -0700326 signalExceptionForGroupError(env, errno);
Dianne Hackborn887f3552009-12-07 17:59:37 -0800327 }
San Mehat242d65b2009-09-12 10:10:37 -0700328 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700329
Steve Block6215d3f2012-01-04 20:05:49 +0000330 //ALOGI("Setting priority of %d: %d, getpriority returns %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 // pid, pri, getpriority(PRIO_PROCESS, pid));
332}
333
334void android_os_Process_setCallingThreadPriority(JNIEnv* env, jobject clazz,
335 jint pri)
336{
337 jint tid = android_os_Process_myTid(env, clazz);
338 android_os_Process_setThreadPriority(env, clazz, tid, pri);
339}
340
341jint android_os_Process_getThreadPriority(JNIEnv* env, jobject clazz,
342 jint pid)
343{
344 errno = 0;
345 jint pri = getpriority(PRIO_PROCESS, pid);
346 if (errno != 0) {
Glenn Kasten6793ac92011-07-13 12:44:12 -0700347 signalExceptionForPriorityError(env, errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 }
Steve Block6215d3f2012-01-04 20:05:49 +0000349 //ALOGI("Returning priority of %d: %d\n", pid, pri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 return pri;
351}
352
353jboolean android_os_Process_setOomAdj(JNIEnv* env, jobject clazz,
354 jint pid, jint adj)
355{
356#ifdef HAVE_OOM_ADJ
Jeff Brown10e89712011-07-08 18:52:57 -0700357 char text[64];
358 sprintf(text, "/proc/%d/oom_adj", pid);
359 int fd = open(text, O_WRONLY);
360 if (fd >= 0) {
361 sprintf(text, "%d", adj);
362 write(fd, text, strlen(text));
363 close(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 }
Jeff Brown10e89712011-07-08 18:52:57 -0700365 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366#endif
367 return false;
368}
369
370void android_os_Process_setArgV0(JNIEnv* env, jobject clazz, jstring name)
371{
372 if (name == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700373 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 return;
375 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 const jchar* str = env->GetStringCritical(name, 0);
378 String8 name8;
379 if (str) {
380 name8 = String8(str, env->GetStringLength(name));
381 env->ReleaseStringCritical(name, str);
382 }
383
384 if (name8.size() > 0) {
385 ProcessState::self()->setArgV0(name8.string());
386 }
387}
388
389jint android_os_Process_setUid(JNIEnv* env, jobject clazz, jint uid)
390{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 return setuid(uid) == 0 ? 0 : errno;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392}
393
394jint android_os_Process_setGid(JNIEnv* env, jobject clazz, jint uid)
395{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 return setgid(uid) == 0 ? 0 : errno;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397}
398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399static int pid_compare(const void* v1, const void* v2)
400{
Steve Block6215d3f2012-01-04 20:05:49 +0000401 //ALOGI("Compare %d vs %d\n", *((const jint*)v1), *((const jint*)v2));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 return *((const jint*)v1) - *((const jint*)v2);
403}
404
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700405static jlong getFreeMemoryImpl(const char* const sums[], const int sumsLen[], int num)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406{
407 int fd = open("/proc/meminfo", O_RDONLY);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 if (fd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000410 ALOGW("Unable to open /proc/meminfo");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 return -1;
412 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 char buffer[256];
415 const int len = read(fd, buffer, sizeof(buffer)-1);
416 close(fd);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 if (len < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000419 ALOGW("Unable to read /proc/meminfo");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 return -1;
421 }
422 buffer[len] = 0;
423
424 int numFound = 0;
Marco Nelissen0bca96b2009-07-17 12:59:25 -0700425 jlong mem = 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 char* p = buffer;
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700428 while (*p && numFound < num) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 int i = 0;
430 while (sums[i]) {
431 if (strncmp(p, sums[i], sumsLen[i]) == 0) {
432 p += sumsLen[i];
433 while (*p == ' ') p++;
434 char* num = p;
435 while (*p >= '0' && *p <= '9') p++;
436 if (*p != 0) {
437 *p = 0;
438 p++;
439 if (*p == 0) p--;
440 }
Marco Nelissen0bca96b2009-07-17 12:59:25 -0700441 mem += atoll(num) * 1024;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 numFound++;
443 break;
444 }
445 i++;
446 }
447 p++;
448 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 return numFound > 0 ? mem : -1;
451}
452
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700453static jlong android_os_Process_getFreeMemory(JNIEnv* env, jobject clazz)
454{
455 static const char* const sums[] = { "MemFree:", "Cached:", NULL };
456 static const int sumsLen[] = { strlen("MemFree:"), strlen("Cached:"), 0 };
457 return getFreeMemoryImpl(sums, sumsLen, 2);
458}
459
460static jlong android_os_Process_getTotalMemory(JNIEnv* env, jobject clazz)
461{
462 static const char* const sums[] = { "MemTotal:", NULL };
463 static const int sumsLen[] = { strlen("MemTotal:"), 0 };
464 return getFreeMemoryImpl(sums, sumsLen, 1);
465}
466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467void android_os_Process_readProcLines(JNIEnv* env, jobject clazz, jstring fileStr,
468 jobjectArray reqFields, jlongArray outFields)
469{
Steve Block6215d3f2012-01-04 20:05:49 +0000470 //ALOGI("getMemInfo: %p %p", reqFields, outFields);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 if (fileStr == NULL || reqFields == NULL || outFields == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700473 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 return;
475 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 const char* file8 = env->GetStringUTFChars(fileStr, NULL);
478 if (file8 == NULL) {
479 return;
480 }
481 String8 file(file8);
482 env->ReleaseStringUTFChars(fileStr, file8);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 jsize count = env->GetArrayLength(reqFields);
485 if (count > env->GetArrayLength(outFields)) {
486 jniThrowException(env, "java/lang/IllegalArgumentException", "Array lengths differ");
487 return;
488 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 Vector<String8> fields;
491 int i;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 for (i=0; i<count; i++) {
494 jobject obj = env->GetObjectArrayElement(reqFields, i);
495 if (obj != NULL) {
496 const char* str8 = env->GetStringUTFChars((jstring)obj, NULL);
Steve Block6215d3f2012-01-04 20:05:49 +0000497 //ALOGI("String at %d: %p = %s", i, obj, str8);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 if (str8 == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700499 jniThrowNullPointerException(env, "Element in reqFields");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 return;
501 }
502 fields.add(String8(str8));
503 env->ReleaseStringUTFChars((jstring)obj, str8);
504 } else {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700505 jniThrowNullPointerException(env, "Element in reqFields");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 return;
507 }
508 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 jlong* sizesArray = env->GetLongArrayElements(outFields, 0);
511 if (sizesArray == NULL) {
512 return;
513 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700514
Steve Block6215d3f2012-01-04 20:05:49 +0000515 //ALOGI("Clearing %d sizes", count);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 for (i=0; i<count; i++) {
517 sizesArray[i] = 0;
518 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 int fd = open(file.string(), O_RDONLY);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 if (fd >= 0) {
523 const size_t BUFFER_SIZE = 2048;
524 char* buffer = (char*)malloc(BUFFER_SIZE);
525 int len = read(fd, buffer, BUFFER_SIZE-1);
526 close(fd);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 if (len < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000529 ALOGW("Unable to read %s", file.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 len = 0;
531 }
532 buffer[len] = 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 int foundCount = 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 char* p = buffer;
537 while (*p && foundCount < count) {
538 bool skipToEol = true;
Steve Block6215d3f2012-01-04 20:05:49 +0000539 //ALOGI("Parsing at: %s", p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 for (i=0; i<count; i++) {
541 const String8& field = fields[i];
542 if (strncmp(p, field.string(), field.length()) == 0) {
543 p += field.length();
Amith Yamasaniadd868c2009-06-25 11:57:40 -0700544 while (*p == ' ' || *p == '\t') p++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 char* num = p;
546 while (*p >= '0' && *p <= '9') p++;
547 skipToEol = *p != '\n';
548 if (*p != 0) {
549 *p = 0;
550 p++;
551 }
552 char* end;
553 sizesArray[i] = strtoll(num, &end, 10);
Steve Block6215d3f2012-01-04 20:05:49 +0000554 //ALOGI("Field %s = %d", field.string(), sizesArray[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 foundCount++;
556 break;
557 }
558 }
559 if (skipToEol) {
560 while (*p && *p != '\n') {
561 p++;
562 }
563 if (*p == '\n') {
564 p++;
565 }
566 }
567 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 free(buffer);
570 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000571 ALOGW("Unable to open %s", file.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700573
Steve Block6215d3f2012-01-04 20:05:49 +0000574 //ALOGI("Done!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 env->ReleaseLongArrayElements(outFields, sizesArray, 0);
576}
577
578jintArray android_os_Process_getPids(JNIEnv* env, jobject clazz,
579 jstring file, jintArray lastArray)
580{
581 if (file == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700582 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 return NULL;
584 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 const char* file8 = env->GetStringUTFChars(file, NULL);
587 if (file8 == NULL) {
588 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
589 return NULL;
590 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 DIR* dirp = opendir(file8);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 env->ReleaseStringUTFChars(file, file8);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 if(dirp == NULL) {
597 return NULL;
598 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 jsize curCount = 0;
601 jint* curData = NULL;
602 if (lastArray != NULL) {
603 curCount = env->GetArrayLength(lastArray);
604 curData = env->GetIntArrayElements(lastArray, 0);
605 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 jint curPos = 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 struct dirent* entry;
610 while ((entry=readdir(dirp)) != NULL) {
611 const char* p = entry->d_name;
612 while (*p) {
613 if (*p < '0' || *p > '9') break;
614 p++;
615 }
616 if (*p != 0) continue;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 char* end;
619 int pid = strtol(entry->d_name, &end, 10);
Steve Block6215d3f2012-01-04 20:05:49 +0000620 //ALOGI("File %s pid=%d\n", entry->d_name, pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 if (curPos >= curCount) {
622 jsize newCount = (curCount == 0) ? 10 : (curCount*2);
623 jintArray newArray = env->NewIntArray(newCount);
624 if (newArray == NULL) {
625 closedir(dirp);
626 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
627 return NULL;
628 }
629 jint* newData = env->GetIntArrayElements(newArray, 0);
630 if (curData != NULL) {
631 memcpy(newData, curData, sizeof(jint)*curCount);
632 env->ReleaseIntArrayElements(lastArray, curData, 0);
633 }
634 lastArray = newArray;
635 curCount = newCount;
636 curData = newData;
637 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 curData[curPos] = pid;
640 curPos++;
641 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 closedir(dirp);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 if (curData != NULL && curPos > 0) {
646 qsort(curData, curPos, sizeof(jint), pid_compare);
647 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 while (curPos < curCount) {
650 curData[curPos] = -1;
651 curPos++;
652 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 if (curData != NULL) {
655 env->ReleaseIntArrayElements(lastArray, curData, 0);
656 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 return lastArray;
659}
660
661enum {
662 PROC_TERM_MASK = 0xff,
663 PROC_ZERO_TERM = 0,
664 PROC_SPACE_TERM = ' ',
665 PROC_COMBINE = 0x100,
666 PROC_PARENS = 0x200,
667 PROC_OUT_STRING = 0x1000,
668 PROC_OUT_LONG = 0x2000,
669 PROC_OUT_FLOAT = 0x4000,
670};
671
Evan Millarc64edde2009-04-18 12:26:32 -0700672jboolean android_os_Process_parseProcLineArray(JNIEnv* env, jobject clazz,
Elliott Hughes69a017b2011-04-08 14:10:28 -0700673 char* buffer, jint startIndex, jint endIndex, jintArray format,
Evan Millarc64edde2009-04-18 12:26:32 -0700674 jobjectArray outStrings, jlongArray outLongs, jfloatArray outFloats)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675{
Elliott Hughes69a017b2011-04-08 14:10:28 -0700676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 const jsize NF = env->GetArrayLength(format);
678 const jsize NS = outStrings ? env->GetArrayLength(outStrings) : 0;
679 const jsize NL = outLongs ? env->GetArrayLength(outLongs) : 0;
680 const jsize NR = outFloats ? env->GetArrayLength(outFloats) : 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 jint* formatData = env->GetIntArrayElements(format, 0);
683 jlong* longsData = outLongs ?
684 env->GetLongArrayElements(outLongs, 0) : NULL;
685 jfloat* floatsData = outFloats ?
686 env->GetFloatArrayElements(outFloats, 0) : NULL;
687 if (formatData == NULL || (NL > 0 && longsData == NULL)
688 || (NR > 0 && floatsData == NULL)) {
689 if (formatData != NULL) {
690 env->ReleaseIntArrayElements(format, formatData, 0);
691 }
692 if (longsData != NULL) {
693 env->ReleaseLongArrayElements(outLongs, longsData, 0);
694 }
695 if (floatsData != NULL) {
696 env->ReleaseFloatArrayElements(outFloats, floatsData, 0);
697 }
698 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
699 return JNI_FALSE;
700 }
701
Evan Millarc64edde2009-04-18 12:26:32 -0700702 jsize i = startIndex;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 jsize di = 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 jboolean res = JNI_TRUE;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 for (jsize fi=0; fi<NF; fi++) {
708 const jint mode = formatData[fi];
709 if ((mode&PROC_PARENS) != 0) {
710 i++;
711 }
712 const char term = (char)(mode&PROC_TERM_MASK);
713 const jsize start = i;
Evan Millarc64edde2009-04-18 12:26:32 -0700714 if (i >= endIndex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 res = JNI_FALSE;
716 break;
717 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 jsize end = -1;
720 if ((mode&PROC_PARENS) != 0) {
Evan Millarc64edde2009-04-18 12:26:32 -0700721 while (buffer[i] != ')' && i < endIndex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 i++;
723 }
724 end = i;
725 i++;
726 }
Evan Millarc64edde2009-04-18 12:26:32 -0700727 while (buffer[i] != term && i < endIndex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 i++;
729 }
730 if (end < 0) {
731 end = i;
732 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700733
Evan Millarc64edde2009-04-18 12:26:32 -0700734 if (i < endIndex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 i++;
736 if ((mode&PROC_COMBINE) != 0) {
Evan Millarc64edde2009-04-18 12:26:32 -0700737 while (buffer[i] == term && i < endIndex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 i++;
739 }
740 }
741 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700742
Steve Block6215d3f2012-01-04 20:05:49 +0000743 //ALOGI("Field %d: %d-%d dest=%d mode=0x%x\n", i, start, end, di, mode);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 if ((mode&(PROC_OUT_FLOAT|PROC_OUT_LONG|PROC_OUT_STRING)) != 0) {
746 char c = buffer[end];
747 buffer[end] = 0;
748 if ((mode&PROC_OUT_FLOAT) != 0 && di < NR) {
749 char* end;
750 floatsData[di] = strtof(buffer+start, &end);
751 }
752 if ((mode&PROC_OUT_LONG) != 0 && di < NL) {
753 char* end;
754 longsData[di] = strtoll(buffer+start, &end, 10);
755 }
756 if ((mode&PROC_OUT_STRING) != 0 && di < NS) {
757 jstring str = env->NewStringUTF(buffer+start);
758 env->SetObjectArrayElement(outStrings, di, str);
759 }
760 buffer[end] = c;
761 di++;
762 }
763 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 env->ReleaseIntArrayElements(format, formatData, 0);
766 if (longsData != NULL) {
767 env->ReleaseLongArrayElements(outLongs, longsData, 0);
768 }
769 if (floatsData != NULL) {
770 env->ReleaseFloatArrayElements(outFloats, floatsData, 0);
771 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 return res;
774}
775
Evan Millarc64edde2009-04-18 12:26:32 -0700776jboolean android_os_Process_parseProcLine(JNIEnv* env, jobject clazz,
Elliott Hughes69a017b2011-04-08 14:10:28 -0700777 jbyteArray buffer, jint startIndex, jint endIndex, jintArray format,
Evan Millarc64edde2009-04-18 12:26:32 -0700778 jobjectArray outStrings, jlongArray outLongs, jfloatArray outFloats)
779{
780 jbyte* bufferArray = env->GetByteArrayElements(buffer, NULL);
781
Elliott Hughes69a017b2011-04-08 14:10:28 -0700782 jboolean result = android_os_Process_parseProcLineArray(env, clazz,
783 (char*) bufferArray, startIndex, endIndex, format, outStrings,
Evan Millarc64edde2009-04-18 12:26:32 -0700784 outLongs, outFloats);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700785
Evan Millarc64edde2009-04-18 12:26:32 -0700786 env->ReleaseByteArrayElements(buffer, bufferArray, 0);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700787
Evan Millarc64edde2009-04-18 12:26:32 -0700788 return result;
789}
790
791jboolean android_os_Process_readProcFile(JNIEnv* env, jobject clazz,
792 jstring file, jintArray format, jobjectArray outStrings,
793 jlongArray outLongs, jfloatArray outFloats)
794{
795 if (file == NULL || format == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700796 jniThrowNullPointerException(env, NULL);
Evan Millarc64edde2009-04-18 12:26:32 -0700797 return JNI_FALSE;
798 }
799
800 const char* file8 = env->GetStringUTFChars(file, NULL);
801 if (file8 == NULL) {
802 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
803 return JNI_FALSE;
804 }
805 int fd = open(file8, O_RDONLY);
806 env->ReleaseStringUTFChars(file, file8);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700807
Evan Millarc64edde2009-04-18 12:26:32 -0700808 if (fd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000809 //ALOGW("Unable to open process file: %s\n", file8);
Evan Millarc64edde2009-04-18 12:26:32 -0700810 return JNI_FALSE;
811 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700812
Evan Millarc64edde2009-04-18 12:26:32 -0700813 char buffer[256];
814 const int len = read(fd, buffer, sizeof(buffer)-1);
815 close(fd);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700816
Evan Millarc64edde2009-04-18 12:26:32 -0700817 if (len < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000818 //ALOGW("Unable to open process file: %s fd=%d\n", file8, fd);
Evan Millarc64edde2009-04-18 12:26:32 -0700819 return JNI_FALSE;
820 }
821 buffer[len] = 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700822
823 return android_os_Process_parseProcLineArray(env, clazz, buffer, 0, len,
Evan Millarc64edde2009-04-18 12:26:32 -0700824 format, outStrings, outLongs, outFloats);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700825
Evan Millarc64edde2009-04-18 12:26:32 -0700826}
827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828void android_os_Process_setApplicationObject(JNIEnv* env, jobject clazz,
829 jobject binderObject)
830{
831 if (binderObject == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700832 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 return;
834 }
835
836 sp<IBinder> binder = ibinderForJavaObject(env, binderObject);
837}
838
839void android_os_Process_sendSignal(JNIEnv* env, jobject clazz, jint pid, jint sig)
840{
841 if (pid > 0) {
Steve Block6215d3f2012-01-04 20:05:49 +0000842 ALOGI("Sending signal. PID: %d SIG: %d", pid, sig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 kill(pid, sig);
844 }
845}
846
Dianne Hackborn906497c2010-05-10 15:57:38 -0700847void android_os_Process_sendSignalQuiet(JNIEnv* env, jobject clazz, jint pid, jint sig)
848{
849 if (pid > 0) {
850 kill(pid, sig);
851 }
852}
853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854static jlong android_os_Process_getElapsedCpuTime(JNIEnv* env, jobject clazz)
855{
856 struct timespec ts;
857
858 int res = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 if (res != 0) {
861 return (jlong) 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700862 }
863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 nsecs_t when = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
865 return (jlong) nanoseconds_to_milliseconds(when);
866}
867
868static jlong android_os_Process_getPss(JNIEnv* env, jobject clazz, jint pid)
869{
870 char filename[64];
871
872 snprintf(filename, sizeof(filename), "/proc/%d/smaps", pid);
873
874 FILE * file = fopen(filename, "r");
875 if (!file) {
876 return (jlong) -1;
877 }
878
879 // Tally up all of the Pss from the various maps
880 char line[256];
881 jlong pss = 0;
882 while (fgets(line, sizeof(line), file)) {
883 jlong v;
884 if (sscanf(line, "Pss: %lld kB", &v) == 1) {
885 pss += v;
886 }
887 }
888
889 fclose(file);
890
891 // Return the Pss value in bytes, not kilobytes
892 return pss * 1024;
893}
894
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700895jintArray android_os_Process_getPidsForCommands(JNIEnv* env, jobject clazz,
896 jobjectArray commandNames)
897{
898 if (commandNames == NULL) {
899 jniThrowNullPointerException(env, NULL);
900 return NULL;
901 }
902
903 Vector<String8> commands;
904
905 jsize count = env->GetArrayLength(commandNames);
906
907 for (int i=0; i<count; i++) {
908 jobject obj = env->GetObjectArrayElement(commandNames, i);
909 if (obj != NULL) {
910 const char* str8 = env->GetStringUTFChars((jstring)obj, NULL);
911 if (str8 == NULL) {
912 jniThrowNullPointerException(env, "Element in commandNames");
913 return NULL;
914 }
915 commands.add(String8(str8));
916 env->ReleaseStringUTFChars((jstring)obj, str8);
917 } else {
918 jniThrowNullPointerException(env, "Element in commandNames");
919 return NULL;
920 }
921 }
922
923 Vector<jint> pids;
924
925 DIR *proc = opendir("/proc");
926 if (proc == NULL) {
927 fprintf(stderr, "/proc: %s\n", strerror(errno));
928 return NULL;
929 }
930
931 struct dirent *d;
932 while ((d = readdir(proc))) {
933 int pid = atoi(d->d_name);
934 if (pid <= 0) continue;
935
936 char path[PATH_MAX];
937 char data[PATH_MAX];
938 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
939
940 int fd = open(path, O_RDONLY);
941 if (fd < 0) {
942 continue;
943 }
944 const int len = read(fd, data, sizeof(data)-1);
945 close(fd);
946
947 if (len < 0) {
948 continue;
949 }
950 data[len] = 0;
951
952 for (int i=0; i<len; i++) {
953 if (data[i] == ' ') {
954 data[i] = 0;
955 break;
956 }
957 }
958
959 for (size_t i=0; i<commands.size(); i++) {
960 if (commands[i] == data) {
961 pids.add(pid);
962 break;
963 }
964 }
965 }
966
967 closedir(proc);
968
969 jintArray pidArray = env->NewIntArray(pids.size());
970 if (pidArray == NULL) {
971 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
972 return NULL;
973 }
974
975 if (pids.size() > 0) {
976 env->SetIntArrayRegion(pidArray, 0, pids.size(), pids.array());
977 }
978
979 return pidArray;
980}
981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982static const JNINativeMethod methods[] = {
983 {"myPid", "()I", (void*)android_os_Process_myPid},
984 {"myTid", "()I", (void*)android_os_Process_myTid},
985 {"myUid", "()I", (void*)android_os_Process_myUid},
986 {"getUidForName", "(Ljava/lang/String;)I", (void*)android_os_Process_getUidForName},
987 {"getGidForName", "(Ljava/lang/String;)I", (void*)android_os_Process_getGidForName},
988 {"setThreadPriority", "(II)V", (void*)android_os_Process_setThreadPriority},
Glenn Kasten6793ac92011-07-13 12:44:12 -0700989 {"setThreadScheduler", "(III)V", (void*)android_os_Process_setThreadScheduler},
Christopher Tate160edb32010-06-30 17:46:30 -0700990 {"setCanSelfBackground", "(Z)V", (void*)android_os_Process_setCanSelfBackground},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 {"setThreadPriority", "(I)V", (void*)android_os_Process_setCallingThreadPriority},
992 {"getThreadPriority", "(I)I", (void*)android_os_Process_getThreadPriority},
San Mehate9d376b2009-04-21 14:06:36 -0700993 {"setThreadGroup", "(II)V", (void*)android_os_Process_setThreadGroup},
San Mehat3e458242009-05-19 14:44:16 -0700994 {"setProcessGroup", "(II)V", (void*)android_os_Process_setProcessGroup},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 {"setOomAdj", "(II)Z", (void*)android_os_Process_setOomAdj},
996 {"setArgV0", "(Ljava/lang/String;)V", (void*)android_os_Process_setArgV0},
997 {"setUid", "(I)I", (void*)android_os_Process_setUid},
998 {"setGid", "(I)I", (void*)android_os_Process_setGid},
999 {"sendSignal", "(II)V", (void*)android_os_Process_sendSignal},
Dianne Hackborn906497c2010-05-10 15:57:38 -07001000 {"sendSignalQuiet", "(II)V", (void*)android_os_Process_sendSignalQuiet},
Marco Nelissen0bca96b2009-07-17 12:59:25 -07001001 {"getFreeMemory", "()J", (void*)android_os_Process_getFreeMemory},
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001002 {"getTotalMemory", "()J", (void*)android_os_Process_getTotalMemory},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 {"readProcLines", "(Ljava/lang/String;[Ljava/lang/String;[J)V", (void*)android_os_Process_readProcLines},
1004 {"getPids", "(Ljava/lang/String;[I)[I", (void*)android_os_Process_getPids},
1005 {"readProcFile", "(Ljava/lang/String;[I[Ljava/lang/String;[J[F)Z", (void*)android_os_Process_readProcFile},
Evan Millarc64edde2009-04-18 12:26:32 -07001006 {"parseProcLine", "([BII[I[Ljava/lang/String;[J[F)Z", (void*)android_os_Process_parseProcLine},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 {"getElapsedCpuTime", "()J", (void*)android_os_Process_getElapsedCpuTime},
1008 {"getPss", "(I)J", (void*)android_os_Process_getPss},
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001009 {"getPidsForCommands", "([Ljava/lang/String;)[I", (void*)android_os_Process_getPidsForCommands},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 //{"setApplicationObject", "(Landroid/os/IBinder;)V", (void*)android_os_Process_setApplicationObject},
1011};
1012
1013const char* const kProcessPathName = "android/os/Process";
1014
1015int register_android_os_Process(JNIEnv* env)
1016{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 return AndroidRuntime::registerNativeMethods(
1018 env, kProcessPathName,
1019 methods, NELEM(methods));
1020}