blob: 28307249249fc2522900c7f10a2718d20663ce07 [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>
Mathias Agopian07952722009-05-19 19:08:10 -070022#include <binder/IServiceManager.h>
Narayan Kamatha23fcd72014-03-28 13:39:21 +000023#include <cutils/process_name.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>
Colin Cross0769e552014-06-03 13:25:35 -070027#include <processgroup/processgroup.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Andreas Gampeed6b9df2014-11-20 22:02:20 -080029#include "core_jni_helpers.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
31#include "android_util_Binder.h"
32#include "JNIHelp.h"
33
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034#include <dirent.h>
35#include <fcntl.h>
36#include <grp.h>
Mark Salyzync6a41012014-04-24 13:05:18 -070037#include <inttypes.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038#include <pwd.h>
39#include <signal.h>
Mark Salyzync6a41012014-04-24 13:05:18 -070040#include <sys/errno.h>
41#include <sys/resource.h>
42#include <sys/stat.h>
43#include <sys/types.h>
Glenn Kasten6af763b2011-05-04 17:58:57 -070044#include <unistd.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
Christopher Tate160edb32010-06-30 17:46:30 -070046#define GUARD_THREAD_PRIORITY 0
San Mehata5109a82009-10-29 11:48:50 -070047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048using namespace android;
49
Andreas Gampe0f0b4912014-11-12 08:03:48 -080050static const bool kDebugPolicy = false;
51static const bool kDebugProc = false;
52
Christopher Tate160edb32010-06-30 17:46:30 -070053#if GUARD_THREAD_PRIORITY
54Mutex gKeyCreateMutex;
55static pthread_key_t gBgKey = -1;
56#endif
57
Glenn Kastenf1b56442012-03-15 16:33:43 -070058// For both of these, err should be in the errno range (positive), not a status_t (negative)
59
Glenn Kasten6793ac92011-07-13 12:44:12 -070060static void signalExceptionForPriorityError(JNIEnv* env, int err)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061{
62 switch (err) {
63 case EINVAL:
64 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
65 break;
66 case ESRCH:
67 jniThrowException(env, "java/lang/IllegalArgumentException", "Given thread does not exist");
68 break;
69 case EPERM:
70 jniThrowException(env, "java/lang/SecurityException", "No permission to modify given thread");
71 break;
72 case EACCES:
73 jniThrowException(env, "java/lang/SecurityException", "No permission to set to given priority");
74 break;
75 default:
76 jniThrowException(env, "java/lang/RuntimeException", "Unknown error");
77 break;
78 }
79}
80
Glenn Kasten6793ac92011-07-13 12:44:12 -070081static void signalExceptionForGroupError(JNIEnv* env, int err)
San Mehate9d376b2009-04-21 14:06:36 -070082{
83 switch (err) {
84 case EINVAL:
85 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
86 break;
87 case ESRCH:
88 jniThrowException(env, "java/lang/IllegalArgumentException", "Given thread does not exist");
89 break;
90 case EPERM:
91 jniThrowException(env, "java/lang/SecurityException", "No permission to modify given thread");
92 break;
93 case EACCES:
94 jniThrowException(env, "java/lang/SecurityException", "No permission to set to given group");
95 break;
96 default:
97 jniThrowException(env, "java/lang/RuntimeException", "Unknown error");
98 break;
99 }
100}
101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102jint android_os_Process_getUidForName(JNIEnv* env, jobject clazz, jstring name)
103{
104 if (name == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700105 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 return -1;
107 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 const jchar* str16 = env->GetStringCritical(name, 0);
110 String8 name8;
111 if (str16) {
Dan Albert66987492014-11-20 11:41:21 -0800112 name8 = String8(reinterpret_cast<const char16_t*>(str16),
113 env->GetStringLength(name));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 env->ReleaseStringCritical(name, str16);
115 }
116
117 const size_t N = name8.size();
118 if (N > 0) {
119 const char* str = name8.string();
120 for (size_t i=0; i<N; i++) {
121 if (str[i] < '0' || str[i] > '9') {
122 struct passwd* pwd = getpwnam(str);
123 if (pwd == NULL) {
124 return -1;
125 }
126 return pwd->pw_uid;
127 }
128 }
129 return atoi(str);
130 }
131 return -1;
132}
133
134jint android_os_Process_getGidForName(JNIEnv* env, jobject clazz, jstring name)
135{
136 if (name == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700137 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 return -1;
139 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 const jchar* str16 = env->GetStringCritical(name, 0);
142 String8 name8;
143 if (str16) {
Dan Albert66987492014-11-20 11:41:21 -0800144 name8 = String8(reinterpret_cast<const char16_t*>(str16),
145 env->GetStringLength(name));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 env->ReleaseStringCritical(name, str16);
147 }
148
149 const size_t N = name8.size();
150 if (N > 0) {
151 const char* str = name8.string();
152 for (size_t i=0; i<N; i++) {
153 if (str[i] < '0' || str[i] > '9') {
154 struct group* grp = getgrnam(str);
155 if (grp == NULL) {
156 return -1;
157 }
158 return grp->gr_gid;
159 }
160 }
161 return atoi(str);
162 }
163 return -1;
164}
165
Glenn Kastenf1b56442012-03-15 16:33:43 -0700166void android_os_Process_setThreadGroup(JNIEnv* env, jobject clazz, int tid, jint grp)
San Mehate9d376b2009-04-21 14:06:36 -0700167{
Mark Salyzync6a41012014-04-24 13:05:18 -0700168 ALOGV("%s tid=%d grp=%" PRId32, __func__, tid, grp);
Glenn Kastenf1b56442012-03-15 16:33:43 -0700169 SchedPolicy sp = (SchedPolicy) grp;
170 int res = set_sched_policy(tid, sp);
Dianne Hackborn887f3552009-12-07 17:59:37 -0800171 if (res != NO_ERROR) {
Glenn Kastenf1b56442012-03-15 16:33:43 -0700172 signalExceptionForGroupError(env, -res);
San Mehate9d376b2009-04-21 14:06:36 -0700173 }
San Mehate9d376b2009-04-21 14:06:36 -0700174}
175
Elliott Hughes69a017b2011-04-08 14:10:28 -0700176void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jint grp)
San Mehat3e458242009-05-19 14:44:16 -0700177{
Mark Salyzync6a41012014-04-24 13:05:18 -0700178 ALOGV("%s pid=%d grp=%" PRId32, __func__, pid, grp);
San Mehat3e458242009-05-19 14:44:16 -0700179 DIR *d;
San Mehat3e458242009-05-19 14:44:16 -0700180 char proc_path[255];
181 struct dirent *de;
182
Glenn Kastenf1b56442012-03-15 16:33:43 -0700183 if ((grp == SP_FOREGROUND) || (grp > SP_MAX)) {
Glenn Kasten6793ac92011-07-13 12:44:12 -0700184 signalExceptionForGroupError(env, EINVAL);
San Mehat3e458242009-05-19 14:44:16 -0700185 return;
186 }
187
Glenn Kastenf1b56442012-03-15 16:33:43 -0700188 bool isDefault = false;
189 if (grp < 0) {
190 grp = SP_FOREGROUND;
191 isDefault = true;
192 }
193 SchedPolicy sp = (SchedPolicy) grp;
194
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800195 if (kDebugPolicy) {
196 char cmdline[32];
197 int fd;
San Mehata5109a82009-10-29 11:48:50 -0700198
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800199 strcpy(cmdline, "unknown");
San Mehata5109a82009-10-29 11:48:50 -0700200
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800201 sprintf(proc_path, "/proc/%d/cmdline", pid);
202 fd = open(proc_path, O_RDONLY);
203 if (fd >= 0) {
204 int rc = read(fd, cmdline, sizeof(cmdline)-1);
205 cmdline[rc] = 0;
206 close(fd);
207 }
208
209 if (sp == SP_BACKGROUND) {
210 ALOGD("setProcessGroup: vvv pid %d (%s)", pid, cmdline);
211 } else {
212 ALOGD("setProcessGroup: ^^^ pid %d (%s)", pid, cmdline);
213 }
San Mehata5109a82009-10-29 11:48:50 -0700214 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700215
San Mehat3e458242009-05-19 14:44:16 -0700216 sprintf(proc_path, "/proc/%d/task", pid);
217 if (!(d = opendir(proc_path))) {
San Mehat1fd0ec72009-06-15 16:56:52 -0700218 // If the process exited on us, don't generate an exception
219 if (errno != ENOENT)
Glenn Kasten6793ac92011-07-13 12:44:12 -0700220 signalExceptionForGroupError(env, errno);
San Mehat3e458242009-05-19 14:44:16 -0700221 return;
222 }
223
224 while ((de = readdir(d))) {
San Mehat7e637892009-08-06 13:19:19 -0700225 int t_pid;
226 int t_pri;
227
San Mehat3e458242009-05-19 14:44:16 -0700228 if (de->d_name[0] == '.')
229 continue;
San Mehat7e637892009-08-06 13:19:19 -0700230 t_pid = atoi(de->d_name);
San Mehat1fd0ec72009-06-15 16:56:52 -0700231
San Mehat7e637892009-08-06 13:19:19 -0700232 if (!t_pid) {
Steve Block3762c312012-01-06 19:20:56 +0000233 ALOGE("Error getting pid for '%s'\n", de->d_name);
San Mehat7e637892009-08-06 13:19:19 -0700234 continue;
235 }
236
Glenn Kasten07b04652012-04-23 15:00:43 -0700237 t_pri = getpriority(PRIO_PROCESS, t_pid);
San Mehat7e637892009-08-06 13:19:19 -0700238
Glenn Kasten07b04652012-04-23 15:00:43 -0700239 if (t_pri <= ANDROID_PRIORITY_AUDIO) {
240 int scheduler = sched_getscheduler(t_pid);
241 if ((scheduler == SCHED_FIFO) || (scheduler == SCHED_RR)) {
242 // This task wants to stay in it's current audio group so it can keep it's budget
243 continue;
244 }
245 }
246
247 if (isDefault) {
Glenn Kastenf1b56442012-03-15 16:33:43 -0700248 if (t_pri >= ANDROID_PRIORITY_BACKGROUND) {
249 // This task wants to stay at background
250 continue;
251 }
San Mehat7e637892009-08-06 13:19:19 -0700252 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700253
Glenn Kastenf1b56442012-03-15 16:33:43 -0700254 int err = set_sched_policy(t_pid, sp);
255 if (err != NO_ERROR) {
256 signalExceptionForGroupError(env, -err);
San Mehat0a42b812009-12-03 12:21:28 -0800257 break;
San Mehat242d65b2009-09-12 10:10:37 -0700258 }
San Mehat3e458242009-05-19 14:44:16 -0700259 }
260 closedir(d);
261}
262
Jeff Sharkey9e57c412013-01-17 14:12:41 -0800263jint android_os_Process_getProcessGroup(JNIEnv* env, jobject clazz, jint pid)
264{
265 SchedPolicy sp;
266 if (get_sched_policy(pid, &sp) != 0) {
267 signalExceptionForGroupError(env, errno);
268 }
269 return (int) sp;
270}
271
Christopher Tate160edb32010-06-30 17:46:30 -0700272static void android_os_Process_setCanSelfBackground(JNIEnv* env, jobject clazz, jboolean bgOk) {
273 // Establishes the calling thread as illegal to put into the background.
274 // Typically used only for the system process's main looper.
275#if GUARD_THREAD_PRIORITY
Elliott Hughes06451fe2014-08-18 10:26:52 -0700276 ALOGV("Process.setCanSelfBackground(%d) : tid=%d", bgOk, gettid());
Christopher Tate160edb32010-06-30 17:46:30 -0700277 {
278 Mutex::Autolock _l(gKeyCreateMutex);
279 if (gBgKey == -1) {
280 pthread_key_create(&gBgKey, NULL);
281 }
282 }
283
284 // inverted: not-okay, we set a sentinel value
285 pthread_setspecific(gBgKey, (void*)(bgOk ? 0 : 0xbaad));
286#endif
287}
288
Glenn Kasten6793ac92011-07-13 12:44:12 -0700289void android_os_Process_setThreadScheduler(JNIEnv* env, jclass clazz,
290 jint tid, jint policy, jint pri)
291{
Yabin Cui65b4a682014-11-10 12:15:46 -0800292// linux has sched_setscheduler(), others don't.
293#if defined(__linux__)
Glenn Kasten6793ac92011-07-13 12:44:12 -0700294 struct sched_param param;
295 param.sched_priority = pri;
296 int rc = sched_setscheduler(tid, policy, &param);
297 if (rc) {
298 signalExceptionForPriorityError(env, errno);
299 }
Glenn Kastencc767192012-01-17 08:38:51 -0800300#else
301 signalExceptionForPriorityError(env, ENOSYS);
302#endif
Glenn Kasten6793ac92011-07-13 12:44:12 -0700303}
304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305void android_os_Process_setThreadPriority(JNIEnv* env, jobject clazz,
306 jint pid, jint pri)
307{
Christopher Tate160edb32010-06-30 17:46:30 -0700308#if GUARD_THREAD_PRIORITY
309 // if we're putting the current thread into the background, check the TLS
310 // to make sure this thread isn't guarded. If it is, raise an exception.
311 if (pri >= ANDROID_PRIORITY_BACKGROUND) {
Elliott Hughes06451fe2014-08-18 10:26:52 -0700312 if (pid == gettid()) {
Christopher Tate160edb32010-06-30 17:46:30 -0700313 void* bgOk = pthread_getspecific(gBgKey);
314 if (bgOk == ((void*)0xbaad)) {
Steve Block3762c312012-01-06 19:20:56 +0000315 ALOGE("Thread marked fg-only put self in background!");
Christopher Tate160edb32010-06-30 17:46:30 -0700316 jniThrowException(env, "java/lang/SecurityException", "May not put this thread into background");
317 return;
318 }
319 }
320 }
321#endif
322
Dianne Hackborn887f3552009-12-07 17:59:37 -0800323 int rc = androidSetThreadPriority(pid, pri);
324 if (rc != 0) {
325 if (rc == INVALID_OPERATION) {
Glenn Kasten6793ac92011-07-13 12:44:12 -0700326 signalExceptionForPriorityError(env, errno);
Dianne Hackborn887f3552009-12-07 17:59:37 -0800327 } else {
Glenn Kasten6793ac92011-07-13 12:44:12 -0700328 signalExceptionForGroupError(env, errno);
Dianne Hackborn887f3552009-12-07 17:59:37 -0800329 }
San Mehat242d65b2009-09-12 10:10:37 -0700330 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700331
Mark Salyzync6a41012014-04-24 13:05:18 -0700332 //ALOGI("Setting priority of %" PRId32 ": %" PRId32 ", getpriority returns %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 // pid, pri, getpriority(PRIO_PROCESS, pid));
334}
335
336void android_os_Process_setCallingThreadPriority(JNIEnv* env, jobject clazz,
337 jint pri)
338{
Elliott Hughes06451fe2014-08-18 10:26:52 -0700339 android_os_Process_setThreadPriority(env, clazz, gettid(), pri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340}
341
342jint android_os_Process_getThreadPriority(JNIEnv* env, jobject clazz,
343 jint pid)
344{
345 errno = 0;
346 jint pri = getpriority(PRIO_PROCESS, pid);
347 if (errno != 0) {
Glenn Kasten6793ac92011-07-13 12:44:12 -0700348 signalExceptionForPriorityError(env, errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 }
Mark Salyzync6a41012014-04-24 13:05:18 -0700350 //ALOGI("Returning priority of %" PRId32 ": %" PRId32 "\n", pid, pri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 return pri;
352}
353
Rom Lemarchand5534ba92013-07-12 16:15:36 -0700354jboolean android_os_Process_setSwappiness(JNIEnv *env, jobject clazz,
355 jint pid, jboolean is_increased)
356{
357 char text[64];
358
359 if (is_increased) {
360 strcpy(text, "/sys/fs/cgroup/memory/sw/tasks");
361 } else {
362 strcpy(text, "/sys/fs/cgroup/memory/tasks");
363 }
364
365 struct stat st;
366 if (stat(text, &st) || !S_ISREG(st.st_mode)) {
367 return false;
368 }
369
370 int fd = open(text, O_WRONLY);
371 if (fd >= 0) {
Mark Salyzync6a41012014-04-24 13:05:18 -0700372 sprintf(text, "%" PRId32, pid);
Rom Lemarchand5534ba92013-07-12 16:15:36 -0700373 write(fd, text, strlen(text));
374 close(fd);
375 }
376
377 return true;
378}
379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380void android_os_Process_setArgV0(JNIEnv* env, jobject clazz, jstring name)
381{
382 if (name == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700383 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 return;
385 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 const jchar* str = env->GetStringCritical(name, 0);
388 String8 name8;
389 if (str) {
Dan Albert66987492014-11-20 11:41:21 -0800390 name8 = String8(reinterpret_cast<const char16_t*>(str),
391 env->GetStringLength(name));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 env->ReleaseStringCritical(name, str);
393 }
394
395 if (name8.size() > 0) {
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000396 const char* procName = name8.string();
397 set_process_name(procName);
398 AndroidRuntime::getRuntime()->setArgv0(procName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 }
400}
401
402jint android_os_Process_setUid(JNIEnv* env, jobject clazz, jint uid)
403{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 return setuid(uid) == 0 ? 0 : errno;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405}
406
407jint android_os_Process_setGid(JNIEnv* env, jobject clazz, jint uid)
408{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 return setgid(uid) == 0 ? 0 : errno;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410}
411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412static int pid_compare(const void* v1, const void* v2)
413{
Mark Salyzync6a41012014-04-24 13:05:18 -0700414 //ALOGI("Compare %" PRId32 " vs %" PRId32 "\n", *((const jint*)v1), *((const jint*)v2));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 return *((const jint*)v1) - *((const jint*)v2);
416}
417
Elliott Hughesc367d482013-10-29 13:12:55 -0700418static jlong getFreeMemoryImpl(const char* const sums[], const size_t sumsLen[], size_t num)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419{
420 int fd = open("/proc/meminfo", O_RDONLY);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 if (fd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000423 ALOGW("Unable to open /proc/meminfo");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 return -1;
425 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 char buffer[256];
428 const int len = read(fd, buffer, sizeof(buffer)-1);
429 close(fd);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 if (len < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000432 ALOGW("Unable to read /proc/meminfo");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 return -1;
434 }
435 buffer[len] = 0;
436
Elliott Hughesc367d482013-10-29 13:12:55 -0700437 size_t numFound = 0;
Marco Nelissen0bca96b2009-07-17 12:59:25 -0700438 jlong mem = 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 char* p = buffer;
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700441 while (*p && numFound < num) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 int i = 0;
443 while (sums[i]) {
444 if (strncmp(p, sums[i], sumsLen[i]) == 0) {
445 p += sumsLen[i];
446 while (*p == ' ') p++;
447 char* num = p;
448 while (*p >= '0' && *p <= '9') p++;
449 if (*p != 0) {
450 *p = 0;
451 p++;
452 if (*p == 0) p--;
453 }
Marco Nelissen0bca96b2009-07-17 12:59:25 -0700454 mem += atoll(num) * 1024;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 numFound++;
456 break;
457 }
458 i++;
459 }
460 p++;
461 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 return numFound > 0 ? mem : -1;
464}
465
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700466static jlong android_os_Process_getFreeMemory(JNIEnv* env, jobject clazz)
467{
468 static const char* const sums[] = { "MemFree:", "Cached:", NULL };
Elliott Hughesc367d482013-10-29 13:12:55 -0700469 static const size_t sumsLen[] = { strlen("MemFree:"), strlen("Cached:"), 0 };
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700470 return getFreeMemoryImpl(sums, sumsLen, 2);
471}
472
473static jlong android_os_Process_getTotalMemory(JNIEnv* env, jobject clazz)
474{
475 static const char* const sums[] = { "MemTotal:", NULL };
Elliott Hughesc367d482013-10-29 13:12:55 -0700476 static const size_t sumsLen[] = { strlen("MemTotal:"), 0 };
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700477 return getFreeMemoryImpl(sums, sumsLen, 1);
478}
479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480void android_os_Process_readProcLines(JNIEnv* env, jobject clazz, jstring fileStr,
481 jobjectArray reqFields, jlongArray outFields)
482{
Steve Block6215d3f2012-01-04 20:05:49 +0000483 //ALOGI("getMemInfo: %p %p", reqFields, outFields);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 if (fileStr == NULL || reqFields == NULL || outFields == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700486 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 return;
488 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 const char* file8 = env->GetStringUTFChars(fileStr, NULL);
491 if (file8 == NULL) {
492 return;
493 }
494 String8 file(file8);
495 env->ReleaseStringUTFChars(fileStr, file8);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 jsize count = env->GetArrayLength(reqFields);
498 if (count > env->GetArrayLength(outFields)) {
499 jniThrowException(env, "java/lang/IllegalArgumentException", "Array lengths differ");
500 return;
501 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 Vector<String8> fields;
504 int i;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 for (i=0; i<count; i++) {
507 jobject obj = env->GetObjectArrayElement(reqFields, i);
508 if (obj != NULL) {
509 const char* str8 = env->GetStringUTFChars((jstring)obj, NULL);
Steve Block6215d3f2012-01-04 20:05:49 +0000510 //ALOGI("String at %d: %p = %s", i, obj, str8);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 if (str8 == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700512 jniThrowNullPointerException(env, "Element in reqFields");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 return;
514 }
515 fields.add(String8(str8));
516 env->ReleaseStringUTFChars((jstring)obj, str8);
517 } else {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700518 jniThrowNullPointerException(env, "Element in reqFields");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 return;
520 }
521 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 jlong* sizesArray = env->GetLongArrayElements(outFields, 0);
524 if (sizesArray == NULL) {
525 return;
526 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700527
Mark Salyzync6a41012014-04-24 13:05:18 -0700528 //ALOGI("Clearing %" PRId32 " sizes", count);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 for (i=0; i<count; i++) {
530 sizesArray[i] = 0;
531 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 int fd = open(file.string(), O_RDONLY);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 if (fd >= 0) {
536 const size_t BUFFER_SIZE = 2048;
537 char* buffer = (char*)malloc(BUFFER_SIZE);
538 int len = read(fd, buffer, BUFFER_SIZE-1);
539 close(fd);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 if (len < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000542 ALOGW("Unable to read %s", file.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 len = 0;
544 }
545 buffer[len] = 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 int foundCount = 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 char* p = buffer;
550 while (*p && foundCount < count) {
551 bool skipToEol = true;
Steve Block6215d3f2012-01-04 20:05:49 +0000552 //ALOGI("Parsing at: %s", p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 for (i=0; i<count; i++) {
554 const String8& field = fields[i];
555 if (strncmp(p, field.string(), field.length()) == 0) {
556 p += field.length();
Amith Yamasaniadd868c2009-06-25 11:57:40 -0700557 while (*p == ' ' || *p == '\t') p++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 char* num = p;
559 while (*p >= '0' && *p <= '9') p++;
560 skipToEol = *p != '\n';
561 if (*p != 0) {
562 *p = 0;
563 p++;
564 }
565 char* end;
566 sizesArray[i] = strtoll(num, &end, 10);
Mark Salyzync6a41012014-04-24 13:05:18 -0700567 //ALOGI("Field %s = %" PRId64, field.string(), sizesArray[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 foundCount++;
569 break;
570 }
571 }
572 if (skipToEol) {
573 while (*p && *p != '\n') {
574 p++;
575 }
576 if (*p == '\n') {
577 p++;
578 }
579 }
580 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 free(buffer);
583 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000584 ALOGW("Unable to open %s", file.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700586
Steve Block6215d3f2012-01-04 20:05:49 +0000587 //ALOGI("Done!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 env->ReleaseLongArrayElements(outFields, sizesArray, 0);
589}
590
591jintArray android_os_Process_getPids(JNIEnv* env, jobject clazz,
592 jstring file, jintArray lastArray)
593{
594 if (file == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700595 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 return NULL;
597 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 const char* file8 = env->GetStringUTFChars(file, NULL);
600 if (file8 == NULL) {
601 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
602 return NULL;
603 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 DIR* dirp = opendir(file8);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 env->ReleaseStringUTFChars(file, file8);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 if(dirp == NULL) {
610 return NULL;
611 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 jsize curCount = 0;
614 jint* curData = NULL;
615 if (lastArray != NULL) {
616 curCount = env->GetArrayLength(lastArray);
617 curData = env->GetIntArrayElements(lastArray, 0);
618 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 jint curPos = 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 struct dirent* entry;
623 while ((entry=readdir(dirp)) != NULL) {
624 const char* p = entry->d_name;
625 while (*p) {
626 if (*p < '0' || *p > '9') break;
627 p++;
628 }
629 if (*p != 0) continue;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 char* end;
632 int pid = strtol(entry->d_name, &end, 10);
Steve Block6215d3f2012-01-04 20:05:49 +0000633 //ALOGI("File %s pid=%d\n", entry->d_name, pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 if (curPos >= curCount) {
635 jsize newCount = (curCount == 0) ? 10 : (curCount*2);
636 jintArray newArray = env->NewIntArray(newCount);
637 if (newArray == NULL) {
638 closedir(dirp);
639 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
640 return NULL;
641 }
642 jint* newData = env->GetIntArrayElements(newArray, 0);
643 if (curData != NULL) {
644 memcpy(newData, curData, sizeof(jint)*curCount);
645 env->ReleaseIntArrayElements(lastArray, curData, 0);
646 }
647 lastArray = newArray;
648 curCount = newCount;
649 curData = newData;
650 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 curData[curPos] = pid;
653 curPos++;
654 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 closedir(dirp);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 if (curData != NULL && curPos > 0) {
659 qsort(curData, curPos, sizeof(jint), pid_compare);
660 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 while (curPos < curCount) {
663 curData[curPos] = -1;
664 curPos++;
665 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 if (curData != NULL) {
668 env->ReleaseIntArrayElements(lastArray, curData, 0);
669 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 return lastArray;
672}
673
674enum {
675 PROC_TERM_MASK = 0xff,
676 PROC_ZERO_TERM = 0,
677 PROC_SPACE_TERM = ' ',
678 PROC_COMBINE = 0x100,
679 PROC_PARENS = 0x200,
Dianne Hackborn13ac0412013-06-25 19:34:49 -0700680 PROC_QUOTES = 0x400,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 PROC_OUT_STRING = 0x1000,
682 PROC_OUT_LONG = 0x2000,
683 PROC_OUT_FLOAT = 0x4000,
684};
685
Evan Millarc64edde2009-04-18 12:26:32 -0700686jboolean android_os_Process_parseProcLineArray(JNIEnv* env, jobject clazz,
Elliott Hughes69a017b2011-04-08 14:10:28 -0700687 char* buffer, jint startIndex, jint endIndex, jintArray format,
Evan Millarc64edde2009-04-18 12:26:32 -0700688 jobjectArray outStrings, jlongArray outLongs, jfloatArray outFloats)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689{
Elliott Hughes69a017b2011-04-08 14:10:28 -0700690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 const jsize NF = env->GetArrayLength(format);
692 const jsize NS = outStrings ? env->GetArrayLength(outStrings) : 0;
693 const jsize NL = outLongs ? env->GetArrayLength(outLongs) : 0;
694 const jsize NR = outFloats ? env->GetArrayLength(outFloats) : 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 jint* formatData = env->GetIntArrayElements(format, 0);
697 jlong* longsData = outLongs ?
698 env->GetLongArrayElements(outLongs, 0) : NULL;
699 jfloat* floatsData = outFloats ?
700 env->GetFloatArrayElements(outFloats, 0) : NULL;
701 if (formatData == NULL || (NL > 0 && longsData == NULL)
702 || (NR > 0 && floatsData == NULL)) {
703 if (formatData != NULL) {
704 env->ReleaseIntArrayElements(format, formatData, 0);
705 }
706 if (longsData != NULL) {
707 env->ReleaseLongArrayElements(outLongs, longsData, 0);
708 }
709 if (floatsData != NULL) {
710 env->ReleaseFloatArrayElements(outFloats, floatsData, 0);
711 }
712 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
713 return JNI_FALSE;
714 }
715
Evan Millarc64edde2009-04-18 12:26:32 -0700716 jsize i = startIndex;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 jsize di = 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 jboolean res = JNI_TRUE;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 for (jsize fi=0; fi<NF; fi++) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -0700722 jint mode = formatData[fi];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 if ((mode&PROC_PARENS) != 0) {
724 i++;
Bernhard Rosenkränzer3b1e22e2014-11-17 22:30:56 +0100725 } else if ((mode&PROC_QUOTES) != 0) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -0700726 if (buffer[i] == '"') {
727 i++;
728 } else {
729 mode &= ~PROC_QUOTES;
730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 }
732 const char term = (char)(mode&PROC_TERM_MASK);
733 const jsize start = i;
Evan Millarc64edde2009-04-18 12:26:32 -0700734 if (i >= endIndex) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800735 if (kDebugProc) {
736 ALOGW("Ran off end of data @%d", i);
737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 res = JNI_FALSE;
739 break;
740 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 jsize end = -1;
743 if ((mode&PROC_PARENS) != 0) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700744 while (i < endIndex && buffer[i] != ')') {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 i++;
746 }
747 end = i;
748 i++;
Dianne Hackborn13ac0412013-06-25 19:34:49 -0700749 } else if ((mode&PROC_QUOTES) != 0) {
750 while (buffer[i] != '"' && i < endIndex) {
751 i++;
752 }
753 end = i;
754 i++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
Elliott Hughesc367d482013-10-29 13:12:55 -0700756 while (i < endIndex && buffer[i] != term) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 i++;
758 }
759 if (end < 0) {
760 end = i;
761 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700762
Evan Millarc64edde2009-04-18 12:26:32 -0700763 if (i < endIndex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 i++;
765 if ((mode&PROC_COMBINE) != 0) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700766 while (i < endIndex && buffer[i] == term) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 i++;
768 }
769 }
770 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700771
Mark Salyzync6a41012014-04-24 13:05:18 -0700772 //ALOGI("Field %" PRId32 ": %" PRId32 "-%" PRId32 " dest=%" PRId32 " mode=0x%" PRIx32 "\n", i, start, end, di, mode);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 if ((mode&(PROC_OUT_FLOAT|PROC_OUT_LONG|PROC_OUT_STRING)) != 0) {
775 char c = buffer[end];
776 buffer[end] = 0;
777 if ((mode&PROC_OUT_FLOAT) != 0 && di < NR) {
778 char* end;
779 floatsData[di] = strtof(buffer+start, &end);
780 }
781 if ((mode&PROC_OUT_LONG) != 0 && di < NL) {
782 char* end;
783 longsData[di] = strtoll(buffer+start, &end, 10);
784 }
785 if ((mode&PROC_OUT_STRING) != 0 && di < NS) {
786 jstring str = env->NewStringUTF(buffer+start);
787 env->SetObjectArrayElement(outStrings, di, str);
788 }
789 buffer[end] = c;
790 di++;
791 }
792 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 env->ReleaseIntArrayElements(format, formatData, 0);
795 if (longsData != NULL) {
796 env->ReleaseLongArrayElements(outLongs, longsData, 0);
797 }
798 if (floatsData != NULL) {
799 env->ReleaseFloatArrayElements(outFloats, floatsData, 0);
800 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 return res;
803}
804
Evan Millarc64edde2009-04-18 12:26:32 -0700805jboolean android_os_Process_parseProcLine(JNIEnv* env, jobject clazz,
Elliott Hughes69a017b2011-04-08 14:10:28 -0700806 jbyteArray buffer, jint startIndex, jint endIndex, jintArray format,
Evan Millarc64edde2009-04-18 12:26:32 -0700807 jobjectArray outStrings, jlongArray outLongs, jfloatArray outFloats)
808{
809 jbyte* bufferArray = env->GetByteArrayElements(buffer, NULL);
810
Elliott Hughes69a017b2011-04-08 14:10:28 -0700811 jboolean result = android_os_Process_parseProcLineArray(env, clazz,
812 (char*) bufferArray, startIndex, endIndex, format, outStrings,
Evan Millarc64edde2009-04-18 12:26:32 -0700813 outLongs, outFloats);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700814
Evan Millarc64edde2009-04-18 12:26:32 -0700815 env->ReleaseByteArrayElements(buffer, bufferArray, 0);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700816
Evan Millarc64edde2009-04-18 12:26:32 -0700817 return result;
818}
819
820jboolean android_os_Process_readProcFile(JNIEnv* env, jobject clazz,
821 jstring file, jintArray format, jobjectArray outStrings,
822 jlongArray outLongs, jfloatArray outFloats)
823{
824 if (file == NULL || format == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700825 jniThrowNullPointerException(env, NULL);
Evan Millarc64edde2009-04-18 12:26:32 -0700826 return JNI_FALSE;
827 }
828
829 const char* file8 = env->GetStringUTFChars(file, NULL);
830 if (file8 == NULL) {
831 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
832 return JNI_FALSE;
833 }
834 int fd = open(file8, O_RDONLY);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700835
Evan Millarc64edde2009-04-18 12:26:32 -0700836 if (fd < 0) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800837 if (kDebugProc) {
838 ALOGW("Unable to open process file: %s\n", file8);
839 }
Dianne Hackborn306af672014-06-24 15:41:03 -0700840 env->ReleaseStringUTFChars(file, file8);
Evan Millarc64edde2009-04-18 12:26:32 -0700841 return JNI_FALSE;
842 }
Dianne Hackborn306af672014-06-24 15:41:03 -0700843 env->ReleaseStringUTFChars(file, file8);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700844
Evan Millarc64edde2009-04-18 12:26:32 -0700845 char buffer[256];
846 const int len = read(fd, buffer, sizeof(buffer)-1);
847 close(fd);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700848
Evan Millarc64edde2009-04-18 12:26:32 -0700849 if (len < 0) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800850 if (kDebugProc) {
851 ALOGW("Unable to open process file: %s fd=%d\n", file8, fd);
852 }
Evan Millarc64edde2009-04-18 12:26:32 -0700853 return JNI_FALSE;
854 }
855 buffer[len] = 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700856
857 return android_os_Process_parseProcLineArray(env, clazz, buffer, 0, len,
Evan Millarc64edde2009-04-18 12:26:32 -0700858 format, outStrings, outLongs, outFloats);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700859
Evan Millarc64edde2009-04-18 12:26:32 -0700860}
861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862void android_os_Process_setApplicationObject(JNIEnv* env, jobject clazz,
863 jobject binderObject)
864{
865 if (binderObject == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700866 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 return;
868 }
869
870 sp<IBinder> binder = ibinderForJavaObject(env, binderObject);
871}
872
873void android_os_Process_sendSignal(JNIEnv* env, jobject clazz, jint pid, jint sig)
874{
875 if (pid > 0) {
Mark Salyzync6a41012014-04-24 13:05:18 -0700876 ALOGI("Sending signal. PID: %" PRId32 " SIG: %" PRId32, pid, sig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 kill(pid, sig);
878 }
879}
880
Dianne Hackborn906497c2010-05-10 15:57:38 -0700881void android_os_Process_sendSignalQuiet(JNIEnv* env, jobject clazz, jint pid, jint sig)
882{
883 if (pid > 0) {
884 kill(pid, sig);
885 }
886}
887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888static jlong android_os_Process_getElapsedCpuTime(JNIEnv* env, jobject clazz)
889{
890 struct timespec ts;
891
892 int res = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 if (res != 0) {
895 return (jlong) 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700896 }
897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 nsecs_t when = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
899 return (jlong) nanoseconds_to_milliseconds(when);
900}
901
902static jlong android_os_Process_getPss(JNIEnv* env, jobject clazz, jint pid)
903{
904 char filename[64];
905
Mark Salyzync6a41012014-04-24 13:05:18 -0700906 snprintf(filename, sizeof(filename), "/proc/%" PRId32 "/smaps", pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907
908 FILE * file = fopen(filename, "r");
909 if (!file) {
910 return (jlong) -1;
911 }
912
913 // Tally up all of the Pss from the various maps
914 char line[256];
915 jlong pss = 0;
916 while (fgets(line, sizeof(line), file)) {
917 jlong v;
Mark Salyzync6a41012014-04-24 13:05:18 -0700918 if (sscanf(line, "Pss: %" SCNd64 " kB", &v) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 pss += v;
920 }
921 }
922
923 fclose(file);
924
925 // Return the Pss value in bytes, not kilobytes
926 return pss * 1024;
927}
928
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700929jintArray android_os_Process_getPidsForCommands(JNIEnv* env, jobject clazz,
930 jobjectArray commandNames)
931{
932 if (commandNames == NULL) {
933 jniThrowNullPointerException(env, NULL);
934 return NULL;
935 }
936
937 Vector<String8> commands;
938
939 jsize count = env->GetArrayLength(commandNames);
940
941 for (int i=0; i<count; i++) {
942 jobject obj = env->GetObjectArrayElement(commandNames, i);
943 if (obj != NULL) {
944 const char* str8 = env->GetStringUTFChars((jstring)obj, NULL);
945 if (str8 == NULL) {
946 jniThrowNullPointerException(env, "Element in commandNames");
947 return NULL;
948 }
949 commands.add(String8(str8));
950 env->ReleaseStringUTFChars((jstring)obj, str8);
951 } else {
952 jniThrowNullPointerException(env, "Element in commandNames");
953 return NULL;
954 }
955 }
956
957 Vector<jint> pids;
958
959 DIR *proc = opendir("/proc");
960 if (proc == NULL) {
961 fprintf(stderr, "/proc: %s\n", strerror(errno));
962 return NULL;
963 }
964
965 struct dirent *d;
966 while ((d = readdir(proc))) {
967 int pid = atoi(d->d_name);
968 if (pid <= 0) continue;
969
970 char path[PATH_MAX];
971 char data[PATH_MAX];
972 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
973
974 int fd = open(path, O_RDONLY);
975 if (fd < 0) {
976 continue;
977 }
978 const int len = read(fd, data, sizeof(data)-1);
979 close(fd);
980
981 if (len < 0) {
982 continue;
983 }
984 data[len] = 0;
985
986 for (int i=0; i<len; i++) {
987 if (data[i] == ' ') {
988 data[i] = 0;
989 break;
990 }
991 }
992
993 for (size_t i=0; i<commands.size(); i++) {
994 if (commands[i] == data) {
995 pids.add(pid);
996 break;
997 }
998 }
999 }
1000
1001 closedir(proc);
1002
1003 jintArray pidArray = env->NewIntArray(pids.size());
1004 if (pidArray == NULL) {
1005 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
1006 return NULL;
1007 }
1008
1009 if (pids.size() > 0) {
1010 env->SetIntArrayRegion(pidArray, 0, pids.size(), pids.array());
1011 }
1012
1013 return pidArray;
1014}
1015
Colin Cross0769e552014-06-03 13:25:35 -07001016jint android_os_Process_killProcessGroup(JNIEnv* env, jobject clazz, jint uid, jint pid)
1017{
1018 return killProcessGroup(uid, pid, SIGKILL);
1019}
1020
1021void android_os_Process_removeAllProcessGroups(JNIEnv* env, jobject clazz)
1022{
1023 return removeAllProcessGroups();
1024}
1025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026static const JNINativeMethod methods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 {"getUidForName", "(Ljava/lang/String;)I", (void*)android_os_Process_getUidForName},
1028 {"getGidForName", "(Ljava/lang/String;)I", (void*)android_os_Process_getGidForName},
1029 {"setThreadPriority", "(II)V", (void*)android_os_Process_setThreadPriority},
Glenn Kasten6793ac92011-07-13 12:44:12 -07001030 {"setThreadScheduler", "(III)V", (void*)android_os_Process_setThreadScheduler},
Christopher Tate160edb32010-06-30 17:46:30 -07001031 {"setCanSelfBackground", "(Z)V", (void*)android_os_Process_setCanSelfBackground},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 {"setThreadPriority", "(I)V", (void*)android_os_Process_setCallingThreadPriority},
1033 {"getThreadPriority", "(I)I", (void*)android_os_Process_getThreadPriority},
San Mehate9d376b2009-04-21 14:06:36 -07001034 {"setThreadGroup", "(II)V", (void*)android_os_Process_setThreadGroup},
Jeff Sharkey9e57c412013-01-17 14:12:41 -08001035 {"setProcessGroup", "(II)V", (void*)android_os_Process_setProcessGroup},
1036 {"getProcessGroup", "(I)I", (void*)android_os_Process_getProcessGroup},
Rom Lemarchand5534ba92013-07-12 16:15:36 -07001037 {"setSwappiness", "(IZ)Z", (void*)android_os_Process_setSwappiness},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 {"setArgV0", "(Ljava/lang/String;)V", (void*)android_os_Process_setArgV0},
1039 {"setUid", "(I)I", (void*)android_os_Process_setUid},
1040 {"setGid", "(I)I", (void*)android_os_Process_setGid},
1041 {"sendSignal", "(II)V", (void*)android_os_Process_sendSignal},
Dianne Hackborn906497c2010-05-10 15:57:38 -07001042 {"sendSignalQuiet", "(II)V", (void*)android_os_Process_sendSignalQuiet},
Marco Nelissen0bca96b2009-07-17 12:59:25 -07001043 {"getFreeMemory", "()J", (void*)android_os_Process_getFreeMemory},
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001044 {"getTotalMemory", "()J", (void*)android_os_Process_getTotalMemory},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 {"readProcLines", "(Ljava/lang/String;[Ljava/lang/String;[J)V", (void*)android_os_Process_readProcLines},
1046 {"getPids", "(Ljava/lang/String;[I)[I", (void*)android_os_Process_getPids},
1047 {"readProcFile", "(Ljava/lang/String;[I[Ljava/lang/String;[J[F)Z", (void*)android_os_Process_readProcFile},
Evan Millarc64edde2009-04-18 12:26:32 -07001048 {"parseProcLine", "([BII[I[Ljava/lang/String;[J[F)Z", (void*)android_os_Process_parseProcLine},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 {"getElapsedCpuTime", "()J", (void*)android_os_Process_getElapsedCpuTime},
1050 {"getPss", "(I)J", (void*)android_os_Process_getPss},
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001051 {"getPidsForCommands", "([Ljava/lang/String;)[I", (void*)android_os_Process_getPidsForCommands},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 //{"setApplicationObject", "(Landroid/os/IBinder;)V", (void*)android_os_Process_setApplicationObject},
Colin Cross0769e552014-06-03 13:25:35 -07001053 {"killProcessGroup", "(II)I", (void*)android_os_Process_killProcessGroup},
1054 {"removeAllProcessGroups", "()V", (void*)android_os_Process_removeAllProcessGroups},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055};
1056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057int register_android_os_Process(JNIEnv* env)
1058{
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001059 return RegisterMethodsOrDie(env, "android/os/Process", methods, NELEM(methods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060}