blob: ee6e8c487badf363da5194b76fabf8860a599b5e [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
Martijn Coenencd4bdf32016-03-03 17:30:52 +010020// To make sure cpu_set_t is included from sched.h
21#define _GNU_SOURCE 1
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022#include <utils/Log.h>
Mathias Agopian07952722009-05-19 19:08:10 -070023#include <binder/IPCThreadState.h>
Mathias Agopian07952722009-05-19 19:08:10 -070024#include <binder/IServiceManager.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <utils/String8.h>
26#include <utils/Vector.h>
Sandeep Patilfcd4a4a2019-01-12 22:42:05 -080027#include <meminfo/procmeminfo.h>
Sandeep Patilbe825412018-12-11 10:09:46 -080028#include <meminfo/sysmeminfo.h>
Colin Cross0769e552014-06-03 13:25:35 -070029#include <processgroup/processgroup.h>
Suren Baghdasaryane4433262019-01-04 12:16:57 -080030#include <processgroup/sched_policy.h>
Daniel Colascione82e97442019-09-03 13:42:23 -070031#include <android-base/unique_fd.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
Daniel Colascione82e97442019-09-03 13:42:23 -070033#include <algorithm>
Daniel Colascione6f0ca432019-09-03 18:20:15 -070034#include <array>
Daniel Colascione82e97442019-09-03 13:42:23 -070035#include <limits>
36#include <memory>
Sandeep Patilbe825412018-12-11 10:09:46 -080037#include <string>
38#include <vector>
39
Andreas Gampeed6b9df2014-11-20 22:02:20 -080040#include "core_jni_helpers.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
42#include "android_util_Binder.h"
Steven Moreland2279b252017-07-19 09:50:45 -070043#include <nativehelper/JNIHelp.h>
Daniel Colascione6c518102017-07-27 03:33:34 -070044#include "android_os_Debug.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046#include <dirent.h>
47#include <fcntl.h>
48#include <grp.h>
Mark Salyzync6a41012014-04-24 13:05:18 -070049#include <inttypes.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050#include <pwd.h>
51#include <signal.h>
Sandeep Patilbe825412018-12-11 10:09:46 -080052#include <string.h>
Mark Salyzync6a41012014-04-24 13:05:18 -070053#include <sys/errno.h>
54#include <sys/resource.h>
55#include <sys/stat.h>
Sandeep Patilbe825412018-12-11 10:09:46 -080056#include <sys/sysinfo.h>
Mark Salyzync6a41012014-04-24 13:05:18 -070057#include <sys/types.h>
Glenn Kasten6af763b2011-05-04 17:58:57 -070058#include <unistd.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
Christopher Tate160edb32010-06-30 17:46:30 -070060#define GUARD_THREAD_PRIORITY 0
San Mehata5109a82009-10-29 11:48:50 -070061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062using namespace android;
63
Wei Wang517fa2b2019-10-15 20:23:13 -070064static constexpr bool kDebugPolicy = false;
65static constexpr bool kDebugProc = false;
Daniel Colascione82e97442019-09-03 13:42:23 -070066
67// Stack reservation for reading small proc files. Most callers of
68// readProcFile() are reading files under this threshold, e.g.,
69// /proc/pid/stat. /proc/pid/time_in_state ends up being about 520
70// bytes, so use 1024 for the stack to provide a bit of slack.
Wei Wang517fa2b2019-10-15 20:23:13 -070071static constexpr ssize_t kProcReadStackBufferSize = 1024;
Daniel Colascione82e97442019-09-03 13:42:23 -070072
73// The other files we read from proc tend to be a bit larger (e.g.,
74// /proc/stat is about 3kB), so once we exhaust the stack buffer,
75// retry with a relatively large heap-allocated buffer. We double
76// this size and retry until the whole file fits.
Wei Wang517fa2b2019-10-15 20:23:13 -070077static constexpr ssize_t kProcReadMinHeapBufferSize = 4096;
Andreas Gampe0f0b4912014-11-12 08:03:48 -080078
Christopher Tate160edb32010-06-30 17:46:30 -070079#if GUARD_THREAD_PRIORITY
80Mutex gKeyCreateMutex;
81static pthread_key_t gBgKey = -1;
82#endif
83
Glenn Kastenf1b56442012-03-15 16:33:43 -070084// For both of these, err should be in the errno range (positive), not a status_t (negative)
Ruben Brunk4c0c4df2016-08-09 12:46:13 -070085static void signalExceptionForError(JNIEnv* env, int err, int tid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 switch (err) {
87 case EINVAL:
Ruben Brunk4c0c4df2016-08-09 12:46:13 -070088 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
89 "Invalid argument: %d", tid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 break;
91 case ESRCH:
Ruben Brunk4c0c4df2016-08-09 12:46:13 -070092 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
93 "Given thread %d does not exist", tid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 break;
95 case EPERM:
Ruben Brunk4c0c4df2016-08-09 12:46:13 -070096 jniThrowExceptionFmt(env, "java/lang/SecurityException",
97 "No permission to modify given thread %d", tid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 break;
99 default:
100 jniThrowException(env, "java/lang/RuntimeException", "Unknown error");
101 break;
102 }
103}
104
Ruben Brunk4c0c4df2016-08-09 12:46:13 -0700105static void signalExceptionForPriorityError(JNIEnv* env, int err, int tid) {
San Mehate9d376b2009-04-21 14:06:36 -0700106 switch (err) {
San Mehate9d376b2009-04-21 14:06:36 -0700107 case EACCES:
Ruben Brunk4c0c4df2016-08-09 12:46:13 -0700108 jniThrowExceptionFmt(env, "java/lang/SecurityException",
109 "No permission to set the priority of %d", tid);
San Mehate9d376b2009-04-21 14:06:36 -0700110 break;
111 default:
Ruben Brunk4c0c4df2016-08-09 12:46:13 -0700112 signalExceptionForError(env, err, tid);
113 break;
114 }
115
116}
117
118static void signalExceptionForGroupError(JNIEnv* env, int err, int tid) {
119 switch (err) {
120 case EACCES:
121 jniThrowExceptionFmt(env, "java/lang/SecurityException",
122 "No permission to set the group of %d", tid);
123 break;
124 default:
125 signalExceptionForError(env, err, tid);
San Mehate9d376b2009-04-21 14:06:36 -0700126 break;
127 }
128}
129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130jint android_os_Process_getUidForName(JNIEnv* env, jobject clazz, jstring name)
131{
132 if (name == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700133 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 return -1;
135 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 const jchar* str16 = env->GetStringCritical(name, 0);
138 String8 name8;
139 if (str16) {
Dan Albert66987492014-11-20 11:41:21 -0800140 name8 = String8(reinterpret_cast<const char16_t*>(str16),
141 env->GetStringLength(name));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 env->ReleaseStringCritical(name, str16);
143 }
144
145 const size_t N = name8.size();
146 if (N > 0) {
147 const char* str = name8.string();
148 for (size_t i=0; i<N; i++) {
149 if (str[i] < '0' || str[i] > '9') {
150 struct passwd* pwd = getpwnam(str);
151 if (pwd == NULL) {
152 return -1;
153 }
154 return pwd->pw_uid;
155 }
156 }
157 return atoi(str);
158 }
159 return -1;
160}
161
162jint android_os_Process_getGidForName(JNIEnv* env, jobject clazz, jstring name)
163{
164 if (name == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700165 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 return -1;
167 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 const jchar* str16 = env->GetStringCritical(name, 0);
170 String8 name8;
171 if (str16) {
Dan Albert66987492014-11-20 11:41:21 -0800172 name8 = String8(reinterpret_cast<const char16_t*>(str16),
173 env->GetStringLength(name));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 env->ReleaseStringCritical(name, str16);
175 }
176
177 const size_t N = name8.size();
178 if (N > 0) {
179 const char* str = name8.string();
180 for (size_t i=0; i<N; i++) {
181 if (str[i] < '0' || str[i] > '9') {
182 struct group* grp = getgrnam(str);
183 if (grp == NULL) {
184 return -1;
185 }
186 return grp->gr_gid;
187 }
188 }
189 return atoi(str);
190 }
191 return -1;
192}
193
Wei Wang6eb53142019-10-03 21:41:36 -0700194static bool verifyGroup(JNIEnv* env, int grp)
195{
196 if (grp < SP_DEFAULT || grp >= SP_CNT) {
197 signalExceptionForError(env, EINVAL, grp);
198 return false;
199 }
200 return true;
201}
202
Glenn Kastenf1b56442012-03-15 16:33:43 -0700203void android_os_Process_setThreadGroup(JNIEnv* env, jobject clazz, int tid, jint grp)
San Mehate9d376b2009-04-21 14:06:36 -0700204{
Mark Salyzync6a41012014-04-24 13:05:18 -0700205 ALOGV("%s tid=%d grp=%" PRId32, __func__, tid, grp);
Wei Wang6eb53142019-10-03 21:41:36 -0700206 if (!verifyGroup(env, grp)) {
207 return;
208 }
Rick Yiuc9faeea2019-10-02 14:41:55 +0800209
Wei Wang2999d0d2019-11-07 07:52:21 -0800210 int res = SetTaskProfiles(tid, {get_sched_policy_profile_name((SchedPolicy)grp)}, true) ? 0 : -1;
Rick Yiuc9faeea2019-10-02 14:41:55 +0800211
Dianne Hackborn887f3552009-12-07 17:59:37 -0800212 if (res != NO_ERROR) {
Ruben Brunk4c0c4df2016-08-09 12:46:13 -0700213 signalExceptionForGroupError(env, -res, tid);
San Mehate9d376b2009-04-21 14:06:36 -0700214 }
San Mehate9d376b2009-04-21 14:06:36 -0700215}
216
Joel Fernandes474d3112017-04-04 16:32:15 -0700217void android_os_Process_setThreadGroupAndCpuset(JNIEnv* env, jobject clazz, int tid, jint grp)
218{
219 ALOGV("%s tid=%d grp=%" PRId32, __func__, tid, grp);
Wei Wang6eb53142019-10-03 21:41:36 -0700220 if (!verifyGroup(env, grp)) {
221 return;
222 }
Joel Fernandes474d3112017-04-04 16:32:15 -0700223
Wei Wang517fa2b2019-10-15 20:23:13 -0700224 int res = SetTaskProfiles(tid, {get_cpuset_policy_profile_name((SchedPolicy)grp)}, true) ? 0 : -1;
Joel Fernandes474d3112017-04-04 16:32:15 -0700225
Joel Fernandes474d3112017-04-04 16:32:15 -0700226 if (res != NO_ERROR) {
227 signalExceptionForGroupError(env, -res, tid);
228 }
229}
230
Elliott Hughes69a017b2011-04-08 14:10:28 -0700231void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jint grp)
San Mehat3e458242009-05-19 14:44:16 -0700232{
Mark Salyzync6a41012014-04-24 13:05:18 -0700233 ALOGV("%s pid=%d grp=%" PRId32, __func__, pid, grp);
San Mehat3e458242009-05-19 14:44:16 -0700234 DIR *d;
San Mehat3e458242009-05-19 14:44:16 -0700235 char proc_path[255];
236 struct dirent *de;
237
Rick Yiuc9faeea2019-10-02 14:41:55 +0800238 if (!verifyGroup(env, grp)) {
239 return;
240 }
241
242 if (grp == SP_FOREGROUND) {
Ruben Brunk4c0c4df2016-08-09 12:46:13 -0700243 signalExceptionForGroupError(env, EINVAL, pid);
San Mehat3e458242009-05-19 14:44:16 -0700244 return;
245 }
246
Glenn Kastenf1b56442012-03-15 16:33:43 -0700247 bool isDefault = false;
248 if (grp < 0) {
249 grp = SP_FOREGROUND;
250 isDefault = true;
251 }
Glenn Kastenf1b56442012-03-15 16:33:43 -0700252
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800253 if (kDebugPolicy) {
254 char cmdline[32];
255 int fd;
San Mehata5109a82009-10-29 11:48:50 -0700256
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800257 strcpy(cmdline, "unknown");
San Mehata5109a82009-10-29 11:48:50 -0700258
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800259 sprintf(proc_path, "/proc/%d/cmdline", pid);
Nick Kralevich422dd00f2017-10-19 17:51:50 -0700260 fd = open(proc_path, O_RDONLY | O_CLOEXEC);
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800261 if (fd >= 0) {
262 int rc = read(fd, cmdline, sizeof(cmdline)-1);
263 cmdline[rc] = 0;
264 close(fd);
265 }
266
Rick Yiuc9faeea2019-10-02 14:41:55 +0800267 if (grp == SP_BACKGROUND) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800268 ALOGD("setProcessGroup: vvv pid %d (%s)", pid, cmdline);
269 } else {
270 ALOGD("setProcessGroup: ^^^ pid %d (%s)", pid, cmdline);
271 }
San Mehata5109a82009-10-29 11:48:50 -0700272 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700273
San Mehat3e458242009-05-19 14:44:16 -0700274 sprintf(proc_path, "/proc/%d/task", pid);
275 if (!(d = opendir(proc_path))) {
San Mehat1fd0ec72009-06-15 16:56:52 -0700276 // If the process exited on us, don't generate an exception
277 if (errno != ENOENT)
Ruben Brunk4c0c4df2016-08-09 12:46:13 -0700278 signalExceptionForGroupError(env, errno, pid);
San Mehat3e458242009-05-19 14:44:16 -0700279 return;
280 }
281
282 while ((de = readdir(d))) {
San Mehat7e637892009-08-06 13:19:19 -0700283 int t_pid;
284 int t_pri;
Rick Yiuc9faeea2019-10-02 14:41:55 +0800285 int err;
San Mehat7e637892009-08-06 13:19:19 -0700286
San Mehat3e458242009-05-19 14:44:16 -0700287 if (de->d_name[0] == '.')
288 continue;
San Mehat7e637892009-08-06 13:19:19 -0700289 t_pid = atoi(de->d_name);
San Mehat1fd0ec72009-06-15 16:56:52 -0700290
San Mehat7e637892009-08-06 13:19:19 -0700291 if (!t_pid) {
Steve Block3762c312012-01-06 19:20:56 +0000292 ALOGE("Error getting pid for '%s'\n", de->d_name);
San Mehat7e637892009-08-06 13:19:19 -0700293 continue;
294 }
295
Glenn Kasten07b04652012-04-23 15:00:43 -0700296 t_pri = getpriority(PRIO_PROCESS, t_pid);
San Mehat7e637892009-08-06 13:19:19 -0700297
Glenn Kasten07b04652012-04-23 15:00:43 -0700298 if (t_pri <= ANDROID_PRIORITY_AUDIO) {
Tim Murray20375fe2016-12-20 11:47:24 -0800299 int scheduler = sched_getscheduler(t_pid) & ~SCHED_RESET_ON_FORK;
Glenn Kasten07b04652012-04-23 15:00:43 -0700300 if ((scheduler == SCHED_FIFO) || (scheduler == SCHED_RR)) {
Tim Murray9e41c742015-06-08 14:56:53 -0700301 // This task wants to stay in its current audio group so it can keep its budget
302 // don't update its cpuset or cgroup
Glenn Kasten07b04652012-04-23 15:00:43 -0700303 continue;
304 }
305 }
306
307 if (isDefault) {
Glenn Kastenf1b56442012-03-15 16:33:43 -0700308 if (t_pri >= ANDROID_PRIORITY_BACKGROUND) {
309 // This task wants to stay at background
Tim Murray9e41c742015-06-08 14:56:53 -0700310 // update its cpuset so it doesn't only run on bg core(s)
Wei Wang517fa2b2019-10-15 20:23:13 -0700311 err = SetTaskProfiles(t_pid, {get_cpuset_policy_profile_name((SchedPolicy)grp)}, true) ? 0 : -1;
Rick Yiuc9faeea2019-10-02 14:41:55 +0800312 if (err != NO_ERROR) {
313 signalExceptionForGroupError(env, -err, t_pid);
314 break;
Tim Murray9e41c742015-06-08 14:56:53 -0700315 }
Glenn Kastenf1b56442012-03-15 16:33:43 -0700316 continue;
317 }
San Mehat7e637892009-08-06 13:19:19 -0700318 }
Isaac Chend34fac52017-02-16 11:51:08 +0800319
Wei Wang517fa2b2019-10-15 20:23:13 -0700320 err = SetTaskProfiles(t_pid, {get_cpuset_policy_profile_name((SchedPolicy)grp)}, true) ? 0 : -1;
Tim Murray9e41c742015-06-08 14:56:53 -0700321 if (err != NO_ERROR) {
Ruben Brunk4c0c4df2016-08-09 12:46:13 -0700322 signalExceptionForGroupError(env, -err, t_pid);
Tim Murray9e41c742015-06-08 14:56:53 -0700323 break;
324 }
325
San Mehat3e458242009-05-19 14:44:16 -0700326 }
327 closedir(d);
328}
329
Jeff Sharkey9e57c412013-01-17 14:12:41 -0800330jint android_os_Process_getProcessGroup(JNIEnv* env, jobject clazz, jint pid)
331{
332 SchedPolicy sp;
333 if (get_sched_policy(pid, &sp) != 0) {
Ruben Brunk4c0c4df2016-08-09 12:46:13 -0700334 signalExceptionForGroupError(env, errno, pid);
Jeff Sharkey9e57c412013-01-17 14:12:41 -0800335 }
336 return (int) sp;
337}
338
Martijn Coenencd4bdf32016-03-03 17:30:52 +0100339/** Sample CPUset list format:
340 * 0-3,4,6-8
341 */
342static void parse_cpuset_cpus(char *cpus, cpu_set_t *cpu_set) {
343 unsigned int start, end, matched, i;
344 char *cpu_range = strtok(cpus, ",");
345 while (cpu_range != NULL) {
346 start = end = 0;
347 matched = sscanf(cpu_range, "%u-%u", &start, &end);
348 cpu_range = strtok(NULL, ",");
349 if (start >= CPU_SETSIZE) {
350 ALOGE("parse_cpuset_cpus: ignoring CPU number larger than %d.", CPU_SETSIZE);
351 continue;
352 } else if (end >= CPU_SETSIZE) {
353 ALOGE("parse_cpuset_cpus: ignoring CPU numbers larger than %d.", CPU_SETSIZE);
354 end = CPU_SETSIZE - 1;
355 }
356 if (matched == 1) {
357 CPU_SET(start, cpu_set);
358 } else if (matched == 2) {
359 for (i = start; i <= end; i++) {
360 CPU_SET(i, cpu_set);
361 }
362 } else {
363 ALOGE("Failed to match cpus");
364 }
365 }
366 return;
367}
368
369/**
370 * Stores the CPUs assigned to the cpuset corresponding to the
371 * SchedPolicy in the passed in cpu_set.
372 */
373static void get_cpuset_cores_for_policy(SchedPolicy policy, cpu_set_t *cpu_set)
374{
375 FILE *file;
Suren Baghdasaryan3fc4af62018-12-14 10:32:22 -0800376 std::string filename;
Martijn Coenencd4bdf32016-03-03 17:30:52 +0100377
378 CPU_ZERO(cpu_set);
379
380 switch (policy) {
381 case SP_BACKGROUND:
Suren Baghdasaryan3fc4af62018-12-14 10:32:22 -0800382 if (!CgroupGetAttributePath("LowCapacityCPUs", &filename)) {
383 return;
384 }
Martijn Coenencd4bdf32016-03-03 17:30:52 +0100385 break;
386 case SP_FOREGROUND:
387 case SP_AUDIO_APP:
388 case SP_AUDIO_SYS:
Glenn Kasten73a78002017-04-27 15:29:23 -0700389 case SP_RT_APP:
Suren Baghdasaryan3fc4af62018-12-14 10:32:22 -0800390 if (!CgroupGetAttributePath("HighCapacityCPUs", &filename)) {
391 return;
392 }
Martijn Coenencd4bdf32016-03-03 17:30:52 +0100393 break;
394 case SP_TOP_APP:
Suren Baghdasaryan3fc4af62018-12-14 10:32:22 -0800395 if (!CgroupGetAttributePath("MaxCapacityCPUs", &filename)) {
396 return;
397 }
Martijn Coenencd4bdf32016-03-03 17:30:52 +0100398 break;
399 default:
Suren Baghdasaryan3fc4af62018-12-14 10:32:22 -0800400 return;
Martijn Coenencd4bdf32016-03-03 17:30:52 +0100401 }
402
Suren Baghdasaryan3fc4af62018-12-14 10:32:22 -0800403 file = fopen(filename.c_str(), "re");
Martijn Coenencd4bdf32016-03-03 17:30:52 +0100404 if (file != NULL) {
405 // Parse cpus string
406 char *line = NULL;
407 size_t len = 0;
408 ssize_t num_read = getline(&line, &len, file);
409 fclose (file);
410 if (num_read > 0) {
411 parse_cpuset_cpus(line, cpu_set);
412 } else {
Suren Baghdasaryan3fc4af62018-12-14 10:32:22 -0800413 ALOGE("Failed to read %s", filename.c_str());
Martijn Coenencd4bdf32016-03-03 17:30:52 +0100414 }
415 free(line);
416 }
417 return;
418}
Martijn Coenencd4bdf32016-03-03 17:30:52 +0100419
420
421/**
422 * Determine CPU cores exclusively assigned to the
423 * cpuset corresponding to the SchedPolicy and store
424 * them in the passed in cpu_set_t
425 */
426void get_exclusive_cpuset_cores(SchedPolicy policy, cpu_set_t *cpu_set) {
Isaac Chend34fac52017-02-16 11:51:08 +0800427 if (cpusets_enabled()) {
428 int i;
429 cpu_set_t tmp_set;
430 get_cpuset_cores_for_policy(policy, cpu_set);
431 for (i = 0; i < SP_CNT; i++) {
432 if ((SchedPolicy) i == policy) continue;
433 get_cpuset_cores_for_policy((SchedPolicy)i, &tmp_set);
434 // First get cores exclusive to one set or the other
435 CPU_XOR(&tmp_set, cpu_set, &tmp_set);
436 // Then get the ones only in cpu_set
437 CPU_AND(cpu_set, cpu_set, &tmp_set);
438 }
439 } else {
440 CPU_ZERO(cpu_set);
Martijn Coenencd4bdf32016-03-03 17:30:52 +0100441 }
Martijn Coenencd4bdf32016-03-03 17:30:52 +0100442 return;
443}
444
445jintArray android_os_Process_getExclusiveCores(JNIEnv* env, jobject clazz) {
446 SchedPolicy sp;
447 cpu_set_t cpu_set;
448 jintArray cpus;
449 int pid = getpid();
450 if (get_sched_policy(pid, &sp) != 0) {
Ruben Brunk4c0c4df2016-08-09 12:46:13 -0700451 signalExceptionForGroupError(env, errno, pid);
Martijn Coenencd4bdf32016-03-03 17:30:52 +0100452 return NULL;
453 }
454 get_exclusive_cpuset_cores(sp, &cpu_set);
455 int num_cpus = CPU_COUNT(&cpu_set);
456 cpus = env->NewIntArray(num_cpus);
457 if (cpus == NULL) {
458 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
459 return NULL;
460 }
461
462 jint* cpu_elements = env->GetIntArrayElements(cpus, 0);
463 int count = 0;
464 for (int i = 0; i < CPU_SETSIZE && count < num_cpus; i++) {
465 if (CPU_ISSET(i, &cpu_set)) {
466 cpu_elements[count++] = i;
467 }
468 }
469
470 env->ReleaseIntArrayElements(cpus, cpu_elements, 0);
471 return cpus;
472}
473
Christopher Tate160edb32010-06-30 17:46:30 -0700474static void android_os_Process_setCanSelfBackground(JNIEnv* env, jobject clazz, jboolean bgOk) {
475 // Establishes the calling thread as illegal to put into the background.
476 // Typically used only for the system process's main looper.
477#if GUARD_THREAD_PRIORITY
Elliott Hughes06451fe2014-08-18 10:26:52 -0700478 ALOGV("Process.setCanSelfBackground(%d) : tid=%d", bgOk, gettid());
Christopher Tate160edb32010-06-30 17:46:30 -0700479 {
480 Mutex::Autolock _l(gKeyCreateMutex);
481 if (gBgKey == -1) {
482 pthread_key_create(&gBgKey, NULL);
483 }
484 }
485
486 // inverted: not-okay, we set a sentinel value
487 pthread_setspecific(gBgKey, (void*)(bgOk ? 0 : 0xbaad));
488#endif
489}
490
Srinath Sridharan1b15d132016-07-19 15:16:11 -0700491jint android_os_Process_getThreadScheduler(JNIEnv* env, jclass clazz,
492 jint tid)
493{
494 int policy = 0;
495// linux has sched_getscheduler(), others don't.
496#if defined(__linux__)
497 errno = 0;
498 policy = sched_getscheduler(tid);
499 if (errno != 0) {
Ruben Brunk4c0c4df2016-08-09 12:46:13 -0700500 signalExceptionForPriorityError(env, errno, tid);
Srinath Sridharan1b15d132016-07-19 15:16:11 -0700501 }
502#else
Ruben Brunk4c0c4df2016-08-09 12:46:13 -0700503 signalExceptionForPriorityError(env, ENOSYS, tid);
Srinath Sridharan1b15d132016-07-19 15:16:11 -0700504#endif
505 return policy;
506}
507
Glenn Kasten6793ac92011-07-13 12:44:12 -0700508void android_os_Process_setThreadScheduler(JNIEnv* env, jclass clazz,
509 jint tid, jint policy, jint pri)
510{
Yabin Cui65b4a682014-11-10 12:15:46 -0800511// linux has sched_setscheduler(), others don't.
512#if defined(__linux__)
Glenn Kasten6793ac92011-07-13 12:44:12 -0700513 struct sched_param param;
514 param.sched_priority = pri;
515 int rc = sched_setscheduler(tid, policy, &param);
516 if (rc) {
Ruben Brunk4c0c4df2016-08-09 12:46:13 -0700517 signalExceptionForPriorityError(env, errno, tid);
Glenn Kasten6793ac92011-07-13 12:44:12 -0700518 }
Glenn Kastencc767192012-01-17 08:38:51 -0800519#else
Ruben Brunk4c0c4df2016-08-09 12:46:13 -0700520 signalExceptionForPriorityError(env, ENOSYS, tid);
Glenn Kastencc767192012-01-17 08:38:51 -0800521#endif
Glenn Kasten6793ac92011-07-13 12:44:12 -0700522}
523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524void android_os_Process_setThreadPriority(JNIEnv* env, jobject clazz,
525 jint pid, jint pri)
526{
Christopher Tate160edb32010-06-30 17:46:30 -0700527#if GUARD_THREAD_PRIORITY
528 // if we're putting the current thread into the background, check the TLS
529 // to make sure this thread isn't guarded. If it is, raise an exception.
530 if (pri >= ANDROID_PRIORITY_BACKGROUND) {
Elliott Hughes06451fe2014-08-18 10:26:52 -0700531 if (pid == gettid()) {
Christopher Tate160edb32010-06-30 17:46:30 -0700532 void* bgOk = pthread_getspecific(gBgKey);
533 if (bgOk == ((void*)0xbaad)) {
Steve Block3762c312012-01-06 19:20:56 +0000534 ALOGE("Thread marked fg-only put self in background!");
Christopher Tate160edb32010-06-30 17:46:30 -0700535 jniThrowException(env, "java/lang/SecurityException", "May not put this thread into background");
536 return;
537 }
538 }
539 }
540#endif
541
Dianne Hackborn887f3552009-12-07 17:59:37 -0800542 int rc = androidSetThreadPriority(pid, pri);
543 if (rc != 0) {
544 if (rc == INVALID_OPERATION) {
Ruben Brunk4c0c4df2016-08-09 12:46:13 -0700545 signalExceptionForPriorityError(env, errno, pid);
Dianne Hackborn887f3552009-12-07 17:59:37 -0800546 } else {
Ruben Brunk4c0c4df2016-08-09 12:46:13 -0700547 signalExceptionForGroupError(env, errno, pid);
Dianne Hackborn887f3552009-12-07 17:59:37 -0800548 }
San Mehat242d65b2009-09-12 10:10:37 -0700549 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700550
Mark Salyzync6a41012014-04-24 13:05:18 -0700551 //ALOGI("Setting priority of %" PRId32 ": %" PRId32 ", getpriority returns %d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 // pid, pri, getpriority(PRIO_PROCESS, pid));
553}
554
555void android_os_Process_setCallingThreadPriority(JNIEnv* env, jobject clazz,
556 jint pri)
557{
Elliott Hughes06451fe2014-08-18 10:26:52 -0700558 android_os_Process_setThreadPriority(env, clazz, gettid(), pri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559}
560
561jint android_os_Process_getThreadPriority(JNIEnv* env, jobject clazz,
562 jint pid)
563{
564 errno = 0;
565 jint pri = getpriority(PRIO_PROCESS, pid);
566 if (errno != 0) {
Ruben Brunk4c0c4df2016-08-09 12:46:13 -0700567 signalExceptionForPriorityError(env, errno, pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 }
Mark Salyzync6a41012014-04-24 13:05:18 -0700569 //ALOGI("Returning priority of %" PRId32 ": %" PRId32 "\n", pid, pri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 return pri;
571}
572
Rom Lemarchand5534ba92013-07-12 16:15:36 -0700573jboolean android_os_Process_setSwappiness(JNIEnv *env, jobject clazz,
574 jint pid, jboolean is_increased)
575{
576 char text[64];
577
578 if (is_increased) {
579 strcpy(text, "/sys/fs/cgroup/memory/sw/tasks");
580 } else {
581 strcpy(text, "/sys/fs/cgroup/memory/tasks");
582 }
583
584 struct stat st;
585 if (stat(text, &st) || !S_ISREG(st.st_mode)) {
586 return false;
587 }
588
Nick Kralevich422dd00f2017-10-19 17:51:50 -0700589 int fd = open(text, O_WRONLY | O_CLOEXEC);
Rom Lemarchand5534ba92013-07-12 16:15:36 -0700590 if (fd >= 0) {
Mark Salyzync6a41012014-04-24 13:05:18 -0700591 sprintf(text, "%" PRId32, pid);
Rom Lemarchand5534ba92013-07-12 16:15:36 -0700592 write(fd, text, strlen(text));
593 close(fd);
594 }
595
596 return true;
597}
598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599void android_os_Process_setArgV0(JNIEnv* env, jobject clazz, jstring name)
600{
601 if (name == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700602 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 return;
604 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 const jchar* str = env->GetStringCritical(name, 0);
607 String8 name8;
608 if (str) {
Dan Albert66987492014-11-20 11:41:21 -0800609 name8 = String8(reinterpret_cast<const char16_t*>(str),
610 env->GetStringLength(name));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 env->ReleaseStringCritical(name, str);
612 }
613
Dmitriy Filchenko342c7dc2016-07-12 15:40:54 -0700614 if (!name8.isEmpty()) {
Dmitriy Filchenkof5b6e552016-07-18 16:00:35 -0700615 AndroidRuntime::getRuntime()->setArgv0(name8.string(), true /* setProcName */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 }
617}
618
619jint android_os_Process_setUid(JNIEnv* env, jobject clazz, jint uid)
620{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 return setuid(uid) == 0 ? 0 : errno;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622}
623
624jint android_os_Process_setGid(JNIEnv* env, jobject clazz, jint uid)
625{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 return setgid(uid) == 0 ? 0 : errno;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627}
628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629static int pid_compare(const void* v1, const void* v2)
630{
Mark Salyzync6a41012014-04-24 13:05:18 -0700631 //ALOGI("Compare %" PRId32 " vs %" PRId32 "\n", *((const jint*)v1), *((const jint*)v2));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 return *((const jint*)v1) - *((const jint*)v2);
633}
634
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700635static jlong android_os_Process_getFreeMemory(JNIEnv* env, jobject clazz)
636{
Daniel Colascione6f0ca432019-09-03 18:20:15 -0700637 std::array<std::string_view, 2> memFreeTags = {
Sandeep Patilbe825412018-12-11 10:09:46 -0800638 ::android::meminfo::SysMemInfo::kMemFree,
639 ::android::meminfo::SysMemInfo::kMemCached,
640 };
641 std::vector<uint64_t> mem(memFreeTags.size());
642 ::android::meminfo::SysMemInfo smi;
643
Daniel Colascione6f0ca432019-09-03 18:20:15 -0700644 if (!smi.ReadMemInfo(memFreeTags.size(),
645 memFreeTags.data(),
646 mem.data())) {
Sandeep Patilbe825412018-12-11 10:09:46 -0800647 jniThrowRuntimeException(env, "SysMemInfo read failed to get Free Memory");
648 return -1L;
649 }
650
651 jlong sum = 0;
652 std::for_each(mem.begin(), mem.end(), [&](uint64_t val) { sum += val; });
653 return sum * 1024;
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700654}
655
656static jlong android_os_Process_getTotalMemory(JNIEnv* env, jobject clazz)
657{
Sandeep Patilbe825412018-12-11 10:09:46 -0800658 struct sysinfo si;
659 if (sysinfo(&si) == -1) {
660 ALOGE("sysinfo failed: %s", strerror(errno));
661 return -1;
662 }
663
664 return si.totalram;
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700665}
666
qiwang1e7467f2019-08-22 11:25:30 +0800667/*
668 * The outFields array is initialized to -1 to allow the caller to identify
669 * when the status file (and therefore the process) they specified is invalid.
670 * This array should not be overwritten or cleared before we know that the
671 * status file can be read.
672 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673void android_os_Process_readProcLines(JNIEnv* env, jobject clazz, jstring fileStr,
674 jobjectArray reqFields, jlongArray outFields)
675{
Steve Block6215d3f2012-01-04 20:05:49 +0000676 //ALOGI("getMemInfo: %p %p", reqFields, outFields);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 if (fileStr == NULL || reqFields == NULL || outFields == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700679 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 return;
681 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 const char* file8 = env->GetStringUTFChars(fileStr, NULL);
684 if (file8 == NULL) {
685 return;
686 }
687 String8 file(file8);
688 env->ReleaseStringUTFChars(fileStr, file8);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 jsize count = env->GetArrayLength(reqFields);
691 if (count > env->GetArrayLength(outFields)) {
692 jniThrowException(env, "java/lang/IllegalArgumentException", "Array lengths differ");
693 return;
694 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 Vector<String8> fields;
697 int i;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 for (i=0; i<count; i++) {
700 jobject obj = env->GetObjectArrayElement(reqFields, i);
701 if (obj != NULL) {
702 const char* str8 = env->GetStringUTFChars((jstring)obj, NULL);
Steve Block6215d3f2012-01-04 20:05:49 +0000703 //ALOGI("String at %d: %p = %s", i, obj, str8);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 if (str8 == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700705 jniThrowNullPointerException(env, "Element in reqFields");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 return;
707 }
708 fields.add(String8(str8));
709 env->ReleaseStringUTFChars((jstring)obj, str8);
710 } else {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700711 jniThrowNullPointerException(env, "Element in reqFields");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 return;
713 }
714 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 jlong* sizesArray = env->GetLongArrayElements(outFields, 0);
717 if (sizesArray == NULL) {
718 return;
719 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700720
Nick Kralevich422dd00f2017-10-19 17:51:50 -0700721 int fd = open(file.string(), O_RDONLY | O_CLOEXEC);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 if (fd >= 0) {
qiwang1e7467f2019-08-22 11:25:30 +0800724 //ALOGI("Clearing %" PRId32 " sizes", count);
725 for (i=0; i<count; i++) {
726 sizesArray[i] = 0;
727 }
728
Dianne Hackborne17b4452018-01-10 13:15:40 -0800729 const size_t BUFFER_SIZE = 4096;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 char* buffer = (char*)malloc(BUFFER_SIZE);
731 int len = read(fd, buffer, BUFFER_SIZE-1);
732 close(fd);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 if (len < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000735 ALOGW("Unable to read %s", file.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 len = 0;
737 }
738 buffer[len] = 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 int foundCount = 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 char* p = buffer;
743 while (*p && foundCount < count) {
744 bool skipToEol = true;
Steve Block6215d3f2012-01-04 20:05:49 +0000745 //ALOGI("Parsing at: %s", p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 for (i=0; i<count; i++) {
747 const String8& field = fields[i];
748 if (strncmp(p, field.string(), field.length()) == 0) {
749 p += field.length();
Amith Yamasaniadd868c2009-06-25 11:57:40 -0700750 while (*p == ' ' || *p == '\t') p++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 char* num = p;
752 while (*p >= '0' && *p <= '9') p++;
753 skipToEol = *p != '\n';
754 if (*p != 0) {
755 *p = 0;
756 p++;
757 }
758 char* end;
759 sizesArray[i] = strtoll(num, &end, 10);
Mark Salyzync6a41012014-04-24 13:05:18 -0700760 //ALOGI("Field %s = %" PRId64, field.string(), sizesArray[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 foundCount++;
762 break;
763 }
764 }
765 if (skipToEol) {
766 while (*p && *p != '\n') {
767 p++;
768 }
769 if (*p == '\n') {
770 p++;
771 }
772 }
773 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 free(buffer);
776 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000777 ALOGW("Unable to open %s", file.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700779
Steve Block6215d3f2012-01-04 20:05:49 +0000780 //ALOGI("Done!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 env->ReleaseLongArrayElements(outFields, sizesArray, 0);
782}
783
784jintArray android_os_Process_getPids(JNIEnv* env, jobject clazz,
785 jstring file, jintArray lastArray)
786{
787 if (file == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -0700788 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 return NULL;
790 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 const char* file8 = env->GetStringUTFChars(file, NULL);
793 if (file8 == NULL) {
794 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
795 return NULL;
796 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 DIR* dirp = opendir(file8);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 env->ReleaseStringUTFChars(file, file8);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 if(dirp == NULL) {
803 return NULL;
804 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 jsize curCount = 0;
807 jint* curData = NULL;
808 if (lastArray != NULL) {
809 curCount = env->GetArrayLength(lastArray);
810 curData = env->GetIntArrayElements(lastArray, 0);
811 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 jint curPos = 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 struct dirent* entry;
816 while ((entry=readdir(dirp)) != NULL) {
817 const char* p = entry->d_name;
818 while (*p) {
819 if (*p < '0' || *p > '9') break;
820 p++;
821 }
822 if (*p != 0) continue;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 char* end;
825 int pid = strtol(entry->d_name, &end, 10);
Steve Block6215d3f2012-01-04 20:05:49 +0000826 //ALOGI("File %s pid=%d\n", entry->d_name, pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 if (curPos >= curCount) {
828 jsize newCount = (curCount == 0) ? 10 : (curCount*2);
829 jintArray newArray = env->NewIntArray(newCount);
830 if (newArray == NULL) {
831 closedir(dirp);
832 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
833 return NULL;
834 }
835 jint* newData = env->GetIntArrayElements(newArray, 0);
836 if (curData != NULL) {
837 memcpy(newData, curData, sizeof(jint)*curCount);
838 env->ReleaseIntArrayElements(lastArray, curData, 0);
839 }
840 lastArray = newArray;
841 curCount = newCount;
842 curData = newData;
843 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 curData[curPos] = pid;
846 curPos++;
847 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 closedir(dirp);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 if (curData != NULL && curPos > 0) {
852 qsort(curData, curPos, sizeof(jint), pid_compare);
853 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 while (curPos < curCount) {
856 curData[curPos] = -1;
857 curPos++;
858 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 if (curData != NULL) {
861 env->ReleaseIntArrayElements(lastArray, curData, 0);
862 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 return lastArray;
865}
866
867enum {
868 PROC_TERM_MASK = 0xff,
869 PROC_ZERO_TERM = 0,
870 PROC_SPACE_TERM = ' ',
871 PROC_COMBINE = 0x100,
872 PROC_PARENS = 0x200,
Dianne Hackborn13ac0412013-06-25 19:34:49 -0700873 PROC_QUOTES = 0x400,
Dianne Hackborn4de5a3a2016-06-20 15:20:15 -0700874 PROC_CHAR = 0x800,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 PROC_OUT_STRING = 0x1000,
876 PROC_OUT_LONG = 0x2000,
877 PROC_OUT_FLOAT = 0x4000,
878};
879
Evan Millarc64edde2009-04-18 12:26:32 -0700880jboolean android_os_Process_parseProcLineArray(JNIEnv* env, jobject clazz,
Elliott Hughes69a017b2011-04-08 14:10:28 -0700881 char* buffer, jint startIndex, jint endIndex, jintArray format,
Evan Millarc64edde2009-04-18 12:26:32 -0700882 jobjectArray outStrings, jlongArray outLongs, jfloatArray outFloats)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883{
Elliott Hughes69a017b2011-04-08 14:10:28 -0700884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 const jsize NF = env->GetArrayLength(format);
886 const jsize NS = outStrings ? env->GetArrayLength(outStrings) : 0;
887 const jsize NL = outLongs ? env->GetArrayLength(outLongs) : 0;
888 const jsize NR = outFloats ? env->GetArrayLength(outFloats) : 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 jint* formatData = env->GetIntArrayElements(format, 0);
891 jlong* longsData = outLongs ?
892 env->GetLongArrayElements(outLongs, 0) : NULL;
893 jfloat* floatsData = outFloats ?
894 env->GetFloatArrayElements(outFloats, 0) : NULL;
895 if (formatData == NULL || (NL > 0 && longsData == NULL)
896 || (NR > 0 && floatsData == NULL)) {
897 if (formatData != NULL) {
898 env->ReleaseIntArrayElements(format, formatData, 0);
899 }
900 if (longsData != NULL) {
901 env->ReleaseLongArrayElements(outLongs, longsData, 0);
902 }
903 if (floatsData != NULL) {
904 env->ReleaseFloatArrayElements(outFloats, floatsData, 0);
905 }
906 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
907 return JNI_FALSE;
908 }
909
Evan Millarc64edde2009-04-18 12:26:32 -0700910 jsize i = startIndex;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 jsize di = 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 jboolean res = JNI_TRUE;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 for (jsize fi=0; fi<NF; fi++) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -0700916 jint mode = formatData[fi];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 if ((mode&PROC_PARENS) != 0) {
918 i++;
Bernhard Rosenkränzer3b1e22e2014-11-17 22:30:56 +0100919 } else if ((mode&PROC_QUOTES) != 0) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -0700920 if (buffer[i] == '"') {
921 i++;
922 } else {
923 mode &= ~PROC_QUOTES;
924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 }
926 const char term = (char)(mode&PROC_TERM_MASK);
927 const jsize start = i;
Evan Millarc64edde2009-04-18 12:26:32 -0700928 if (i >= endIndex) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800929 if (kDebugProc) {
930 ALOGW("Ran off end of data @%d", i);
931 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 res = JNI_FALSE;
933 break;
934 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 jsize end = -1;
937 if ((mode&PROC_PARENS) != 0) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700938 while (i < endIndex && buffer[i] != ')') {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 i++;
940 }
941 end = i;
942 i++;
Dianne Hackborn13ac0412013-06-25 19:34:49 -0700943 } else if ((mode&PROC_QUOTES) != 0) {
944 while (buffer[i] != '"' && i < endIndex) {
945 i++;
946 }
947 end = i;
948 i++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 }
Elliott Hughesc367d482013-10-29 13:12:55 -0700950 while (i < endIndex && buffer[i] != term) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 i++;
952 }
953 if (end < 0) {
954 end = i;
955 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700956
Evan Millarc64edde2009-04-18 12:26:32 -0700957 if (i < endIndex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 i++;
959 if ((mode&PROC_COMBINE) != 0) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700960 while (i < endIndex && buffer[i] == term) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 i++;
962 }
963 }
964 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700965
Mark Salyzync6a41012014-04-24 13:05:18 -0700966 //ALOGI("Field %" PRId32 ": %" PRId32 "-%" PRId32 " dest=%" PRId32 " mode=0x%" PRIx32 "\n", i, start, end, di, mode);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 if ((mode&(PROC_OUT_FLOAT|PROC_OUT_LONG|PROC_OUT_STRING)) != 0) {
969 char c = buffer[end];
970 buffer[end] = 0;
971 if ((mode&PROC_OUT_FLOAT) != 0 && di < NR) {
972 char* end;
973 floatsData[di] = strtof(buffer+start, &end);
974 }
975 if ((mode&PROC_OUT_LONG) != 0 && di < NL) {
Dianne Hackborn4de5a3a2016-06-20 15:20:15 -0700976 if ((mode&PROC_CHAR) != 0) {
977 // Caller wants single first character returned as one long.
978 longsData[di] = buffer[start];
979 } else {
980 char* end;
981 longsData[di] = strtoll(buffer+start, &end, 10);
982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 }
984 if ((mode&PROC_OUT_STRING) != 0 && di < NS) {
985 jstring str = env->NewStringUTF(buffer+start);
986 env->SetObjectArrayElement(outStrings, di, str);
987 }
988 buffer[end] = c;
989 di++;
990 }
991 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 env->ReleaseIntArrayElements(format, formatData, 0);
994 if (longsData != NULL) {
995 env->ReleaseLongArrayElements(outLongs, longsData, 0);
996 }
997 if (floatsData != NULL) {
998 env->ReleaseFloatArrayElements(outFloats, floatsData, 0);
999 }
Elliott Hughes69a017b2011-04-08 14:10:28 -07001000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 return res;
1002}
1003
Evan Millarc64edde2009-04-18 12:26:32 -07001004jboolean android_os_Process_parseProcLine(JNIEnv* env, jobject clazz,
Elliott Hughes69a017b2011-04-08 14:10:28 -07001005 jbyteArray buffer, jint startIndex, jint endIndex, jintArray format,
Evan Millarc64edde2009-04-18 12:26:32 -07001006 jobjectArray outStrings, jlongArray outLongs, jfloatArray outFloats)
1007{
1008 jbyte* bufferArray = env->GetByteArrayElements(buffer, NULL);
1009
Elliott Hughes69a017b2011-04-08 14:10:28 -07001010 jboolean result = android_os_Process_parseProcLineArray(env, clazz,
1011 (char*) bufferArray, startIndex, endIndex, format, outStrings,
Evan Millarc64edde2009-04-18 12:26:32 -07001012 outLongs, outFloats);
Elliott Hughes69a017b2011-04-08 14:10:28 -07001013
Evan Millarc64edde2009-04-18 12:26:32 -07001014 env->ReleaseByteArrayElements(buffer, bufferArray, 0);
Elliott Hughes69a017b2011-04-08 14:10:28 -07001015
Evan Millarc64edde2009-04-18 12:26:32 -07001016 return result;
1017}
1018
1019jboolean android_os_Process_readProcFile(JNIEnv* env, jobject clazz,
1020 jstring file, jintArray format, jobjectArray outStrings,
1021 jlongArray outLongs, jfloatArray outFloats)
1022{
1023 if (file == NULL || format == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -07001024 jniThrowNullPointerException(env, NULL);
Evan Millarc64edde2009-04-18 12:26:32 -07001025 return JNI_FALSE;
1026 }
1027
1028 const char* file8 = env->GetStringUTFChars(file, NULL);
1029 if (file8 == NULL) {
1030 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
1031 return JNI_FALSE;
1032 }
Elliott Hughes69a017b2011-04-08 14:10:28 -07001033
Daniel Colascione82e97442019-09-03 13:42:23 -07001034 ::android::base::unique_fd fd(open(file8, O_RDONLY | O_CLOEXEC));
1035 if (!fd.ok()) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -08001036 if (kDebugProc) {
1037 ALOGW("Unable to open process file: %s\n", file8);
1038 }
Dianne Hackborn306af672014-06-24 15:41:03 -07001039 env->ReleaseStringUTFChars(file, file8);
Evan Millarc64edde2009-04-18 12:26:32 -07001040 return JNI_FALSE;
1041 }
Dianne Hackborn306af672014-06-24 15:41:03 -07001042 env->ReleaseStringUTFChars(file, file8);
Elliott Hughes69a017b2011-04-08 14:10:28 -07001043
Daniel Colascione82e97442019-09-03 13:42:23 -07001044 // Most proc files we read are small, so we only go through the
1045 // loop once and use the stack buffer. We allocate a buffer big
1046 // enough for the whole file.
1047
1048 char readBufferStack[kProcReadStackBufferSize];
1049 std::unique_ptr<char[]> readBufferHeap;
1050 char* readBuffer = &readBufferStack[0];
1051 ssize_t readBufferSize = kProcReadStackBufferSize;
1052 ssize_t numberBytesRead;
1053 for (;;) {
1054 // By using pread, we can avoid an lseek to rewind the FD
1055 // before retry, saving a system call.
1056 numberBytesRead = pread(fd, readBuffer, readBufferSize, 0);
1057 if (numberBytesRead < 0 && errno == EINTR) {
1058 continue;
1059 }
1060 if (numberBytesRead < 0) {
Misha Wagnercc065fb2018-10-15 10:45:44 +01001061 if (kDebugProc) {
Daniel Colascione82e97442019-09-03 13:42:23 -07001062 ALOGW("Unable to open process file: %s fd=%d\n", file8, fd.get());
Misha Wagnercc065fb2018-10-15 10:45:44 +01001063 }
Misha Wagnercc065fb2018-10-15 10:45:44 +01001064 return JNI_FALSE;
Daniel Colascione82e97442019-09-03 13:42:23 -07001065 }
1066 if (numberBytesRead < readBufferSize) {
Misha Wagnercc065fb2018-10-15 10:45:44 +01001067 break;
1068 }
Daniel Colascione82e97442019-09-03 13:42:23 -07001069 if (readBufferSize > std::numeric_limits<ssize_t>::max() / 2) {
1070 if (kDebugProc) {
1071 ALOGW("Proc file too big: %s fd=%d\n", file8, fd.get());
1072 }
1073 return JNI_FALSE;
1074 }
1075 readBufferSize = std::max(readBufferSize * 2,
1076 kProcReadMinHeapBufferSize);
1077 readBufferHeap.reset(); // Free address space before getting more.
1078 readBufferHeap = std::make_unique<char[]>(readBufferSize);
1079 if (!readBufferHeap) {
1080 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
1081 return JNI_FALSE;
1082 }
1083 readBuffer = readBufferHeap.get();
Misha Wagnercc065fb2018-10-15 10:45:44 +01001084 }
Elliott Hughes69a017b2011-04-08 14:10:28 -07001085
Daniel Colascione82e97442019-09-03 13:42:23 -07001086 // parseProcLineArray below modifies the buffer while parsing!
1087 return android_os_Process_parseProcLineArray(
1088 env, clazz, readBuffer, 0, numberBytesRead,
1089 format, outStrings, outLongs, outFloats);
Evan Millarc64edde2009-04-18 12:26:32 -07001090}
1091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092void android_os_Process_setApplicationObject(JNIEnv* env, jobject clazz,
1093 jobject binderObject)
1094{
1095 if (binderObject == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -07001096 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 return;
1098 }
1099
1100 sp<IBinder> binder = ibinderForJavaObject(env, binderObject);
1101}
1102
1103void android_os_Process_sendSignal(JNIEnv* env, jobject clazz, jint pid, jint sig)
1104{
1105 if (pid > 0) {
Mark Salyzync6a41012014-04-24 13:05:18 -07001106 ALOGI("Sending signal. PID: %" PRId32 " SIG: %" PRId32, pid, sig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 kill(pid, sig);
1108 }
1109}
1110
Dianne Hackborn906497c2010-05-10 15:57:38 -07001111void android_os_Process_sendSignalQuiet(JNIEnv* env, jobject clazz, jint pid, jint sig)
1112{
1113 if (pid > 0) {
1114 kill(pid, sig);
1115 }
1116}
1117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118static jlong android_os_Process_getElapsedCpuTime(JNIEnv* env, jobject clazz)
1119{
1120 struct timespec ts;
1121
1122 int res = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
Elliott Hughes69a017b2011-04-08 14:10:28 -07001123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 if (res != 0) {
1125 return (jlong) 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -07001126 }
1127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 nsecs_t when = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
1129 return (jlong) nanoseconds_to_milliseconds(when);
1130}
1131
1132static jlong android_os_Process_getPss(JNIEnv* env, jobject clazz, jint pid)
1133{
Sandeep Patilfcd4a4a2019-01-12 22:42:05 -08001134 ::android::meminfo::ProcMemInfo proc_mem(pid);
1135 uint64_t pss;
1136 if (!proc_mem.SmapsOrRollupPss(&pss)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 return (jlong) -1;
1138 }
1139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 // Return the Pss value in bytes, not kilobytes
1141 return pss * 1024;
1142}
1143
Tim Murrayfb076782018-11-27 12:22:22 -08001144static jlongArray android_os_Process_getRss(JNIEnv* env, jobject clazz, jint pid)
1145{
1146 // total, file, anon, swap
1147 jlong rss[4] = {0, 0, 0, 0};
1148 std::string status_path =
1149 android::base::StringPrintf("/proc/%d/status", pid);
1150 UniqueFile file = MakeUniqueFile(status_path.c_str(), "re");
1151
1152 char line[256];
Tim Murray8d652172018-12-12 16:26:47 -08001153 while (file != nullptr && fgets(line, sizeof(line), file.get())) {
Tim Murrayfb076782018-11-27 12:22:22 -08001154 jlong v;
1155 if ( sscanf(line, "VmRSS: %" SCNd64 " kB", &v) == 1) {
1156 rss[0] = v;
1157 } else if ( sscanf(line, "RssFile: %" SCNd64 " kB", &v) == 1) {
1158 rss[1] = v;
1159 } else if ( sscanf(line, "RssAnon: %" SCNd64 " kB", &v) == 1) {
1160 rss[2] = v;
1161 } else if ( sscanf(line, "VmSwap: %" SCNd64 " kB", &v) == 1) {
1162 rss[3] = v;
1163 }
1164 }
1165
1166 jlongArray rssArray = env->NewLongArray(4);
1167 if (rssArray == NULL) {
1168 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
1169 return NULL;
1170 }
1171
1172 env->SetLongArrayRegion(rssArray, 0, 4, rss);
1173
1174 return rssArray;
1175}
1176
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001177jintArray android_os_Process_getPidsForCommands(JNIEnv* env, jobject clazz,
1178 jobjectArray commandNames)
1179{
1180 if (commandNames == NULL) {
1181 jniThrowNullPointerException(env, NULL);
1182 return NULL;
1183 }
1184
1185 Vector<String8> commands;
1186
1187 jsize count = env->GetArrayLength(commandNames);
1188
1189 for (int i=0; i<count; i++) {
1190 jobject obj = env->GetObjectArrayElement(commandNames, i);
1191 if (obj != NULL) {
1192 const char* str8 = env->GetStringUTFChars((jstring)obj, NULL);
1193 if (str8 == NULL) {
1194 jniThrowNullPointerException(env, "Element in commandNames");
1195 return NULL;
1196 }
1197 commands.add(String8(str8));
1198 env->ReleaseStringUTFChars((jstring)obj, str8);
1199 } else {
1200 jniThrowNullPointerException(env, "Element in commandNames");
1201 return NULL;
1202 }
1203 }
1204
1205 Vector<jint> pids;
1206
1207 DIR *proc = opendir("/proc");
1208 if (proc == NULL) {
1209 fprintf(stderr, "/proc: %s\n", strerror(errno));
1210 return NULL;
1211 }
1212
1213 struct dirent *d;
1214 while ((d = readdir(proc))) {
1215 int pid = atoi(d->d_name);
1216 if (pid <= 0) continue;
1217
1218 char path[PATH_MAX];
1219 char data[PATH_MAX];
1220 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
1221
Nick Kralevich422dd00f2017-10-19 17:51:50 -07001222 int fd = open(path, O_RDONLY | O_CLOEXEC);
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001223 if (fd < 0) {
1224 continue;
1225 }
1226 const int len = read(fd, data, sizeof(data)-1);
1227 close(fd);
1228
1229 if (len < 0) {
1230 continue;
1231 }
1232 data[len] = 0;
1233
1234 for (int i=0; i<len; i++) {
1235 if (data[i] == ' ') {
1236 data[i] = 0;
1237 break;
1238 }
1239 }
1240
1241 for (size_t i=0; i<commands.size(); i++) {
1242 if (commands[i] == data) {
1243 pids.add(pid);
1244 break;
1245 }
1246 }
1247 }
1248
1249 closedir(proc);
1250
1251 jintArray pidArray = env->NewIntArray(pids.size());
1252 if (pidArray == NULL) {
1253 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
1254 return NULL;
1255 }
1256
1257 if (pids.size() > 0) {
1258 env->SetIntArrayRegion(pidArray, 0, pids.size(), pids.array());
1259 }
1260
1261 return pidArray;
1262}
1263
Colin Cross0769e552014-06-03 13:25:35 -07001264jint android_os_Process_killProcessGroup(JNIEnv* env, jobject clazz, jint uid, jint pid)
1265{
1266 return killProcessGroup(uid, pid, SIGKILL);
1267}
1268
1269void android_os_Process_removeAllProcessGroups(JNIEnv* env, jobject clazz)
1270{
1271 return removeAllProcessGroups();
1272}
1273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274static const JNINativeMethod methods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 {"getUidForName", "(Ljava/lang/String;)I", (void*)android_os_Process_getUidForName},
1276 {"getGidForName", "(Ljava/lang/String;)I", (void*)android_os_Process_getGidForName},
1277 {"setThreadPriority", "(II)V", (void*)android_os_Process_setThreadPriority},
Glenn Kasten6793ac92011-07-13 12:44:12 -07001278 {"setThreadScheduler", "(III)V", (void*)android_os_Process_setThreadScheduler},
Christopher Tate160edb32010-06-30 17:46:30 -07001279 {"setCanSelfBackground", "(Z)V", (void*)android_os_Process_setCanSelfBackground},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 {"setThreadPriority", "(I)V", (void*)android_os_Process_setCallingThreadPriority},
1281 {"getThreadPriority", "(I)I", (void*)android_os_Process_getThreadPriority},
Srinath Sridharan1b15d132016-07-19 15:16:11 -07001282 {"getThreadScheduler", "(I)I", (void*)android_os_Process_getThreadScheduler},
San Mehate9d376b2009-04-21 14:06:36 -07001283 {"setThreadGroup", "(II)V", (void*)android_os_Process_setThreadGroup},
Joel Fernandes474d3112017-04-04 16:32:15 -07001284 {"setThreadGroupAndCpuset", "(II)V", (void*)android_os_Process_setThreadGroupAndCpuset},
Jeff Sharkey9e57c412013-01-17 14:12:41 -08001285 {"setProcessGroup", "(II)V", (void*)android_os_Process_setProcessGroup},
1286 {"getProcessGroup", "(I)I", (void*)android_os_Process_getProcessGroup},
Martijn Coenencd4bdf32016-03-03 17:30:52 +01001287 {"getExclusiveCores", "()[I", (void*)android_os_Process_getExclusiveCores},
Rom Lemarchand5534ba92013-07-12 16:15:36 -07001288 {"setSwappiness", "(IZ)Z", (void*)android_os_Process_setSwappiness},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 {"setArgV0", "(Ljava/lang/String;)V", (void*)android_os_Process_setArgV0},
1290 {"setUid", "(I)I", (void*)android_os_Process_setUid},
1291 {"setGid", "(I)I", (void*)android_os_Process_setGid},
1292 {"sendSignal", "(II)V", (void*)android_os_Process_sendSignal},
Dianne Hackborn906497c2010-05-10 15:57:38 -07001293 {"sendSignalQuiet", "(II)V", (void*)android_os_Process_sendSignalQuiet},
Marco Nelissen0bca96b2009-07-17 12:59:25 -07001294 {"getFreeMemory", "()J", (void*)android_os_Process_getFreeMemory},
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001295 {"getTotalMemory", "()J", (void*)android_os_Process_getTotalMemory},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 {"readProcLines", "(Ljava/lang/String;[Ljava/lang/String;[J)V", (void*)android_os_Process_readProcLines},
1297 {"getPids", "(Ljava/lang/String;[I)[I", (void*)android_os_Process_getPids},
1298 {"readProcFile", "(Ljava/lang/String;[I[Ljava/lang/String;[J[F)Z", (void*)android_os_Process_readProcFile},
Evan Millarc64edde2009-04-18 12:26:32 -07001299 {"parseProcLine", "([BII[I[Ljava/lang/String;[J[F)Z", (void*)android_os_Process_parseProcLine},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 {"getElapsedCpuTime", "()J", (void*)android_os_Process_getElapsedCpuTime},
1301 {"getPss", "(I)J", (void*)android_os_Process_getPss},
Tim Murrayfb076782018-11-27 12:22:22 -08001302 {"getRss", "(I)[J", (void*)android_os_Process_getRss},
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001303 {"getPidsForCommands", "([Ljava/lang/String;)[I", (void*)android_os_Process_getPidsForCommands},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 //{"setApplicationObject", "(Landroid/os/IBinder;)V", (void*)android_os_Process_setApplicationObject},
Colin Cross0769e552014-06-03 13:25:35 -07001305 {"killProcessGroup", "(II)I", (void*)android_os_Process_killProcessGroup},
1306 {"removeAllProcessGroups", "()V", (void*)android_os_Process_removeAllProcessGroups},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307};
1308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309int register_android_os_Process(JNIEnv* env)
1310{
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001311 return RegisterMethodsOrDie(env, "android/os/Process", methods, NELEM(methods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312}