blob: 6408f6170fceb6daa0f9dab3a4d815d4ea91ce28 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os;
18
Sudheer Shankad81b1d72018-09-05 16:37:30 -070019import android.annotation.NonNull;
20import android.annotation.Nullable;
Srinath Sridharan1b15d132016-07-19 15:16:11 -070021import android.annotation.TestApi;
Andrei Onea24ec3212019-03-15 17:35:05 +000022import android.annotation.UnsupportedAppUsage;
Elliott Hughes34385d32014-04-28 11:11:32 -070023import android.system.Os;
Tim Murrayb89970e2016-07-01 10:42:20 -070024import android.system.OsConstants;
Robert Sesekded20982016-08-15 13:59:13 -040025import android.webkit.WebViewZygote;
Nicolas Geoffray81edac42017-09-07 14:13:29 +010026
Andreas Gampeee0cdce2015-06-08 20:47:50 -070027import dalvik.system.VMRuntime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
29/**
30 * Tools for managing OS processes.
31 */
32public class Process {
33 private static final String LOG_TAG = "Process";
34
Narayan Kamath64cd9072014-05-13 13:35:14 +010035 /**
Jeff Vander Stoep0ac2c092018-07-23 10:57:53 -070036 * An invalid UID value.
37 */
38 public static final int INVALID_UID = -1;
39
40 /**
Jeff Sharkey1cb2d0d2014-07-30 16:45:01 -070041 * Defines the root UID.
Jeff Sharkey1cb2d0d2014-07-30 16:45:01 -070042 */
43 public static final int ROOT_UID = 0;
44
45 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 * Defines the UID/GID under which system code runs.
47 */
48 public static final int SYSTEM_UID = 1000;
49
50 /**
51 * Defines the UID/GID under which the telephony code runs.
52 */
53 public static final int PHONE_UID = 1001;
54
55 /**
Dianne Hackborn854060af2009-07-09 18:14:31 -070056 * Defines the UID/GID for the user shell.
Dianne Hackborn854060af2009-07-09 18:14:31 -070057 */
58 public static final int SHELL_UID = 2000;
59
60 /**
Mike Lockwoodd42685d2009-09-03 09:25:22 -040061 * Defines the UID/GID for the log group.
62 * @hide
63 */
Andrei Onea24ec3212019-03-15 17:35:05 +000064 @UnsupportedAppUsage
Mike Lockwoodd42685d2009-09-03 09:25:22 -040065 public static final int LOG_UID = 1007;
66
67 /**
Amith Yamasanid1582142009-07-08 20:04:55 -070068 * Defines the UID/GID for the WIFI supplicant process.
69 * @hide
70 */
Andrei Onea24ec3212019-03-15 17:35:05 +000071 @UnsupportedAppUsage
Amith Yamasanid1582142009-07-08 20:04:55 -070072 public static final int WIFI_UID = 1010;
73
74 /**
Glenn Kasten8b7d1b42011-07-13 16:23:22 -070075 * Defines the UID/GID for the mediaserver process.
76 * @hide
77 */
Andrei Onea24ec3212019-03-15 17:35:05 +000078 @UnsupportedAppUsage
Glenn Kasten8b7d1b42011-07-13 16:23:22 -070079 public static final int MEDIA_UID = 1013;
80
81 /**
Jeff Sharkey5294a2f2012-04-24 17:07:22 -070082 * Defines the UID/GID for the DRM process.
83 * @hide
84 */
Andrei Onea24ec3212019-03-15 17:35:05 +000085 @UnsupportedAppUsage
Jeff Sharkey5294a2f2012-04-24 17:07:22 -070086 public static final int DRM_UID = 1019;
87
88 /**
Kenny Root26993b32012-03-19 15:07:51 -070089 * Defines the UID/GID for the group that controls VPN services.
90 * @hide
91 */
Andrei Onea24ec3212019-03-15 17:35:05 +000092 @UnsupportedAppUsage
Kenny Root26993b32012-03-19 15:07:51 -070093 public static final int VPN_UID = 1016;
94
95 /**
Janis Danisevskis6ab9bb62017-03-08 10:35:00 -080096 * Defines the UID/GID for keystore.
97 * @hide
98 */
99 public static final int KEYSTORE_UID = 1017;
100
101 /**
Nick Pellycd0e8392010-10-13 17:25:24 -0700102 * Defines the UID/GID for the NFC service process.
103 * @hide
104 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000105 @UnsupportedAppUsage
Nick Pellya5cb9f42011-11-21 14:54:46 -0800106 public static final int NFC_UID = 1027;
Nick Pellycd0e8392010-10-13 17:25:24 -0700107
108 /**
junyulaif6ebf4a2018-10-30 21:49:38 +0800109 * Defines the UID/GID for the clatd process.
110 * @hide
111 * */
112 public static final int CLAT_UID = 1029;
113
114 /**
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800115 * Defines the UID/GID for the Bluetooth service process.
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800116 */
117 public static final int BLUETOOTH_UID = 1002;
118
119 /**
Mike Lockwooddcaa10c2010-12-16 12:50:44 -0800120 * Defines the GID for the group that allows write access to the internal media storage.
121 * @hide
122 */
123 public static final int MEDIA_RW_GID = 1023;
124
125 /**
Jeff Sharkey184a0102013-07-10 16:19:52 -0700126 * Access to installed package details
127 * @hide
128 */
129 public static final int PACKAGE_INFO_GID = 1032;
130
131 /**
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100132 * Defines the UID/GID for the shared RELRO file updater process.
133 * @hide
134 */
135 public static final int SHARED_RELRO_UID = 1037;
136
137 /**
Andy Hunged0ea402015-10-30 14:11:46 -0700138 * Defines the UID/GID for the audioserver process.
139 * @hide
140 */
141 public static final int AUDIOSERVER_UID = 1041;
142
143 /**
Chien-Yu Chen75cade02016-01-11 10:56:21 -0800144 * Defines the UID/GID for the cameraserver process
145 * @hide
146 */
Chien-Yu Chenb8b4e852016-01-29 12:02:28 -0800147 public static final int CAMERASERVER_UID = 1047;
Chien-Yu Chen75cade02016-01-11 10:56:21 -0800148
149 /**
Lorenzo Colittif4b4f4d2018-03-01 21:06:11 +0900150 * Defines the UID/GID for the tethering DNS resolver (currently dnsmasq).
151 * @hide
152 */
153 public static final int DNS_TETHER_UID = 1052;
154
155 /**
Robert Sesekded20982016-08-15 13:59:13 -0400156 * Defines the UID/GID for the WebView zygote process.
157 * @hide
158 */
Robert Sesekbceb3822018-01-22 12:26:39 -0500159 public static final int WEBVIEW_ZYGOTE_UID = 1053;
Robert Sesekded20982016-08-15 13:59:13 -0400160
161 /**
Jeff Sharkey07c5c6f2017-03-21 10:09:50 -0600162 * Defines the UID used for resource tracking for OTA updates.
163 * @hide
164 */
165 public static final int OTA_UPDATE_UID = 1061;
166
Yi Jin4bab3a12018-01-10 16:50:59 -0800167 /**
168 * Defines the UID used for incidentd.
169 * @hide
170 */
171 public static final int INCIDENTD_UID = 1067;
172
Ruchi Kandoi6149b0f2018-01-03 16:14:57 -0800173 /**
174 * Defines the UID/GID for the Secure Element service process.
175 * @hide
176 */
177 public static final int SE_UID = 1068;
178
Remi NGUYEN VANc094a542018-12-07 16:52:24 +0900179 /**
180 * Defines the UID/GID for the NetworkStack app.
181 * @hide
182 */
183 public static final int NETWORK_STACK_UID = 1073;
184
Victor Hsieh3c68afe2019-09-18 14:33:54 -0700185 /**
186 * Defines the UID/GID for fs-verity certificate ownership in keystore.
187 * @hide
188 */
189 public static final int FSVERITY_CERT_UID = 1075;
190
Jeff Sharkeya73e1652017-11-15 19:07:14 -0700191 /** {@hide} */
192 public static final int NOBODY_UID = 9999;
193
Jeff Sharkey07c5c6f2017-03-21 10:09:50 -0600194 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 * Defines the start of a range of UIDs (and GIDs), going from this
196 * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning
197 * to applications.
198 */
199 public static final int FIRST_APPLICATION_UID = 10000;
Jeff Sharkey184a0102013-07-10 16:19:52 -0700200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 /**
202 * Last of application-specific UIDs starting at
203 * {@link #FIRST_APPLICATION_UID}.
204 */
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800205 public static final int LAST_APPLICATION_UID = 19999;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800206
207 /**
Martijn Coenen01e719b2018-12-05 16:01:38 +0100208 * First uid used for fully isolated sandboxed processes spawned from an app zygote
209 * @hide
210 */
Martijn Coenendbf6abc2019-01-10 08:37:45 +0100211 @TestApi
Martijn Coenen01e719b2018-12-05 16:01:38 +0100212 public static final int FIRST_APP_ZYGOTE_ISOLATED_UID = 90000;
213
214 /**
215 * Number of UIDs we allocate per application zygote
216 * @hide
217 */
Martijn Coenendbf6abc2019-01-10 08:37:45 +0100218 @TestApi
Martijn Coenen01e719b2018-12-05 16:01:38 +0100219 public static final int NUM_UIDS_PER_APP_ZYGOTE = 100;
220
221 /**
222 * Last uid used for fully isolated sandboxed processes spawned from an app zygote
223 * @hide
224 */
Martijn Coenendbf6abc2019-01-10 08:37:45 +0100225 @TestApi
Martijn Coenen01e719b2018-12-05 16:01:38 +0100226 public static final int LAST_APP_ZYGOTE_ISOLATED_UID = 98999;
227
228 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800229 * First uid used for fully isolated sandboxed processes (with no permissions of their own)
230 * @hide
231 */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +0000232 @UnsupportedAppUsage
Martijn Coenendbf6abc2019-01-10 08:37:45 +0100233 @TestApi
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800234 public static final int FIRST_ISOLATED_UID = 99000;
235
236 /**
237 * Last uid used for fully isolated sandboxed processes (with no permissions of their own)
238 * @hide
239 */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +0000240 @UnsupportedAppUsage
Martijn Coenendbf6abc2019-01-10 08:37:45 +0100241 @TestApi
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800242 public static final int LAST_ISOLATED_UID = 99999;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243
244 /**
Robin Leee66b6892014-04-29 12:36:47 +0100245 * Defines the gid shared by all applications running under the same profile.
246 * @hide
247 */
248 public static final int SHARED_USER_GID = 9997;
249
250 /**
Kenny Roote091f222012-09-11 15:01:26 -0700251 * First gid for applications to share resources. Used when forward-locking
252 * is enabled but all UserHandles need to be able to read the resources.
253 * @hide
254 */
255 public static final int FIRST_SHARED_APPLICATION_GID = 50000;
256
257 /**
258 * Last gid for applications to share resources. Used when forward-locking
259 * is enabled but all UserHandles need to be able to read the resources.
260 * @hide
261 */
262 public static final int LAST_SHARED_APPLICATION_GID = 59999;
263
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700264 /** {@hide} */
265 public static final int FIRST_APPLICATION_CACHE_GID = 20000;
266 /** {@hide} */
267 public static final int LAST_APPLICATION_CACHE_GID = 29999;
268
Kenny Roote091f222012-09-11 15:01:26 -0700269 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 * Standard priority of application threads.
271 * Use with {@link #setThreadPriority(int)} and
272 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
273 * {@link java.lang.Thread} class.
274 */
275 public static final int THREAD_PRIORITY_DEFAULT = 0;
276
277 /*
278 * ***************************************
279 * ** Keep in sync with utils/threads.h **
280 * ***************************************
281 */
282
283 /**
284 * Lowest available thread priority. Only for those who really, really
285 * don't want to run if anything else is happening.
286 * Use with {@link #setThreadPriority(int)} and
287 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
288 * {@link java.lang.Thread} class.
289 */
290 public static final int THREAD_PRIORITY_LOWEST = 19;
291
292 /**
293 * Standard priority background threads. This gives your thread a slightly
294 * lower than normal priority, so that it will have less chance of impacting
295 * the responsiveness of the user interface.
296 * Use with {@link #setThreadPriority(int)} and
297 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
298 * {@link java.lang.Thread} class.
299 */
300 public static final int THREAD_PRIORITY_BACKGROUND = 10;
301
302 /**
303 * Standard priority of threads that are currently running a user interface
304 * that the user is interacting with. Applications can not normally
305 * change to this priority; the system will automatically adjust your
306 * application threads as the user moves through the UI.
307 * Use with {@link #setThreadPriority(int)} and
308 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
309 * {@link java.lang.Thread} class.
310 */
311 public static final int THREAD_PRIORITY_FOREGROUND = -2;
312
313 /**
314 * Standard priority of system display threads, involved in updating
315 * the user interface. Applications can not
316 * normally change to this priority.
317 * Use with {@link #setThreadPriority(int)} and
318 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
319 * {@link java.lang.Thread} class.
320 */
321 public static final int THREAD_PRIORITY_DISPLAY = -4;
322
323 /**
324 * Standard priority of the most important display threads, for compositing
325 * the screen and retrieving input events. Applications can not normally
326 * change to this priority.
327 * Use with {@link #setThreadPriority(int)} and
328 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
329 * {@link java.lang.Thread} class.
330 */
331 public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8;
332
333 /**
Jeff Tinker9765e202018-01-08 15:05:48 -0800334 * Standard priority of video threads. Applications can not normally
335 * change to this priority.
336 * Use with {@link #setThreadPriority(int)} and
337 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
338 * {@link java.lang.Thread} class.
339 */
340 public static final int THREAD_PRIORITY_VIDEO = -10;
341
342 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 * Standard priority of audio threads. Applications can not normally
344 * change to this priority.
345 * Use with {@link #setThreadPriority(int)} and
346 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
347 * {@link java.lang.Thread} class.
348 */
349 public static final int THREAD_PRIORITY_AUDIO = -16;
350
351 /**
352 * Standard priority of the most important audio threads.
353 * Applications can not normally change to this priority.
354 * Use with {@link #setThreadPriority(int)} and
355 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
356 * {@link java.lang.Thread} class.
357 */
358 public static final int THREAD_PRIORITY_URGENT_AUDIO = -19;
359
360 /**
361 * Minimum increment to make a priority more favorable.
362 */
363 public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
364
365 /**
366 * Minimum increment to make a priority less favorable.
367 */
368 public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
369
San Mehate9d376b2009-04-21 14:06:36 -0700370 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700371 * Default scheduling policy
372 * @hide
373 */
374 public static final int SCHED_OTHER = 0;
375
376 /**
377 * First-In First-Out scheduling policy
378 * @hide
379 */
380 public static final int SCHED_FIFO = 1;
381
382 /**
383 * Round-Robin scheduling policy
384 * @hide
385 */
386 public static final int SCHED_RR = 2;
387
388 /**
389 * Batch scheduling policy
390 * @hide
391 */
392 public static final int SCHED_BATCH = 3;
393
394 /**
395 * Idle scheduling policy
396 * @hide
397 */
398 public static final int SCHED_IDLE = 5;
399
Srinath Sridharane535a582016-06-27 18:13:47 -0700400 /**
401 * Reset scheduler choice on fork.
402 * @hide
403 */
404 public static final int SCHED_RESET_ON_FORK = 0x40000000;
405
Glenn Kastenf1b56442012-03-15 16:33:43 -0700406 // Keep in sync with SP_* constants of enum type SchedPolicy
407 // declared in system/core/include/cutils/sched_policy.h,
408 // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
San Mehate9d376b2009-04-21 14:06:36 -0700409
410 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700411 * Default thread group -
412 * has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
413 * When used with setProcessGroup(), the group of each thread in the process
414 * is conditionally changed based on that thread's current priority, as follows:
415 * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
416 * are moved to foreground thread group. All other threads are left unchanged.
417 * @hide
418 */
419 public static final int THREAD_GROUP_DEFAULT = -1;
420
421 /**
422 * Background thread group - All threads in
San Mehate9d376b2009-04-21 14:06:36 -0700423 * this group are scheduled with a reduced share of the CPU.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700424 * Value is same as constant SP_BACKGROUND of enum SchedPolicy.
San Mehate9d376b2009-04-21 14:06:36 -0700425 * @hide
426 */
Rick Yiu021c0642019-10-03 17:29:20 +0800427 public static final int THREAD_GROUP_BACKGROUND = 0;
San Mehate9d376b2009-04-21 14:06:36 -0700428
429 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700430 * Foreground thread group - All threads in
431 * this group are scheduled with a normal share of the CPU.
432 * Value is same as constant SP_FOREGROUND of enum SchedPolicy.
433 * Not used at this level.
San Mehate9d376b2009-04-21 14:06:36 -0700434 * @hide
435 **/
Glenn Kastenf1b56442012-03-15 16:33:43 -0700436 private static final int THREAD_GROUP_FOREGROUND = 1;
San Mehate9d376b2009-04-21 14:06:36 -0700437
Glenn Kasten07b04652012-04-23 15:00:43 -0700438 /**
439 * System thread group.
440 * @hide
441 **/
442 public static final int THREAD_GROUP_SYSTEM = 2;
443
444 /**
445 * Application audio thread group.
446 * @hide
447 **/
448 public static final int THREAD_GROUP_AUDIO_APP = 3;
449
450 /**
451 * System audio thread group.
452 * @hide
453 **/
454 public static final int THREAD_GROUP_AUDIO_SYS = 4;
455
Tim Murrayece6a222016-01-14 11:22:57 -0800456 /**
457 * Thread group for top foreground app.
458 * @hide
459 **/
460 public static final int THREAD_GROUP_TOP_APP = 5;
461
Joel Fernandes59776002017-04-17 13:40:16 -0700462 /**
463 * Thread group for RT app.
464 * @hide
465 **/
466 public static final int THREAD_GROUP_RT_APP = 6;
467
Tim Murrayfef10a42018-04-13 10:15:17 -0700468 /**
469 * Thread group for bound foreground services that should
470 * have additional CPU restrictions during screen off
471 * @hide
472 **/
473 public static final int THREAD_GROUP_RESTRICTED = 7;
474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 public static final int SIGNAL_QUIT = 3;
476 public static final int SIGNAL_KILL = 9;
477 public static final int SIGNAL_USR1 = 10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478
Dianne Hackbornd98885c2016-03-01 17:13:03 -0800479 private static long sStartElapsedRealtime;
480 private static long sStartUptimeMillis;
481
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100482 /**
Robert Sesek8f8d1872016-03-18 16:52:57 -0400483 * State associated with the zygote process.
484 * @hide
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100485 */
Chris Wailesefce9292019-01-11 13:19:20 -0800486 public static final ZygoteProcess ZYGOTE_PROCESS = new ZygoteProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487
488 /**
489 * Start a new process.
490 *
491 * <p>If processes are enabled, a new process is created and the
492 * static main() function of a <var>processClass</var> is executed there.
493 * The process will continue running after this function returns.
494 *
495 * <p>If processes are not enabled, a new thread in the caller's
496 * process is created and main() of <var>processClass</var> called there.
497 *
498 * <p>The niceName parameter, if not an empty string, is a custom name to
499 * give to the process instead of using processClass. This allows you to
500 * make easily identifyable processes even if you are using the same base
501 * <var>processClass</var> to start them.
502 *
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000503 * When invokeWith is not null, the process will be started as a fresh app
Tamas Berghammer0ca16fa2016-11-11 16:08:26 +0000504 * and not a zygote fork. Note that this is only allowed for uid 0 or when
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100505 * runtimeFlags contains DEBUG_ENABLE_DEBUGGER.
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000506 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 * @param processClass The class to use as the process's main entry
508 * point.
509 * @param niceName A more readable name to use for the process.
510 * @param uid The user-id under which the process will run.
511 * @param gid The group-id under which the process will run.
512 * @param gids Additional group-ids associated with the process.
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100513 * @param runtimeFlags Additional flags for the runtime.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700514 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalleybd19b9e2013-04-12 14:49:38 -0400515 * @param seInfo null-ok SELinux information for the new process.
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100516 * @param abi non-null the ABI this app should be started with.
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700517 * @param instructionSet null-ok the instruction set to use.
jgu212eacd062014-09-10 06:55:07 -0400518 * @param appDataDir null-ok the data directory of the app.
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000519 * @param invokeWith null-ok the command to invoke with.
Sudheer Shankad81b1d72018-09-05 16:37:30 -0700520 * @param packageName null-ok the name of the package this process belongs to.
Riddle Hsu32dbdca2019-05-17 23:10:16 -0600521 * @param isTopApp whether the process starts for high priority application.
Chris Wailes301a1c42019-04-17 17:55:48 -0700522 *
523 * @param zygoteArgs Additional arguments to supply to the zygote process.
Jeff Brown3f9dd282011-07-08 20:02:19 -0700524 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 * @throws RuntimeException on fatal start failure
526 *
527 * {@hide}
528 */
Chris Wailes301a1c42019-04-17 17:55:48 -0700529 public static ProcessStartResult start(@NonNull final String processClass,
530 @Nullable final String niceName,
531 int uid, int gid, @Nullable int[] gids,
532 int runtimeFlags,
533 int mountExternal,
534 int targetSdkVersion,
535 @Nullable String seInfo,
536 @NonNull String abi,
537 @Nullable String instructionSet,
538 @Nullable String appDataDir,
539 @Nullable String invokeWith,
540 @Nullable String packageName,
Riddle Hsu32dbdca2019-05-17 23:10:16 -0600541 boolean isTopApp,
Chris Wailes301a1c42019-04-17 17:55:48 -0700542 @Nullable String[] zygoteArgs) {
Chris Wailesefce9292019-01-11 13:19:20 -0800543 return ZYGOTE_PROCESS.start(processClass, niceName, uid, gid, gids,
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100544 runtimeFlags, mountExternal, targetSdkVersion, seInfo,
Sudheer Shanka3f0645b2018-09-18 13:07:59 -0700545 abi, instructionSet, appDataDir, invokeWith, packageName,
Riddle Hsu32dbdca2019-05-17 23:10:16 -0600546 /*useUsapPool=*/ true, isTopApp, zygoteArgs);
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100547 }
548
Robert Sesekded20982016-08-15 13:59:13 -0400549 /** @hide */
Chris Wailes301a1c42019-04-17 17:55:48 -0700550 public static ProcessStartResult startWebView(@NonNull final String processClass,
551 @Nullable final String niceName,
552 int uid, int gid, @Nullable int[] gids,
553 int runtimeFlags,
554 int mountExternal,
555 int targetSdkVersion,
556 @Nullable String seInfo,
557 @NonNull String abi,
558 @Nullable String instructionSet,
559 @Nullable String appDataDir,
560 @Nullable String invokeWith,
561 @Nullable String packageName,
Chris Wailes301a1c42019-04-17 17:55:48 -0700562 @Nullable String[] zygoteArgs) {
Robert Sesekded20982016-08-15 13:59:13 -0400563 return WebViewZygote.getProcess().start(processClass, niceName, uid, gid, gids,
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100564 runtimeFlags, mountExternal, targetSdkVersion, seInfo,
Sudheer Shanka3f0645b2018-09-18 13:07:59 -0700565 abi, instructionSet, appDataDir, invokeWith, packageName,
Riddle Hsu32dbdca2019-05-17 23:10:16 -0600566 /*useUsapPool=*/ false, /*isTopApp=*/ false, zygoteArgs);
Robert Sesekded20982016-08-15 13:59:13 -0400567 }
568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 /**
570 * Returns elapsed milliseconds of the time this process has run.
571 * @return Returns the number of milliseconds this process has return.
572 */
573 public static final native long getElapsedCpuTime();
Andreas Gampeee0cdce2015-06-08 20:47:50 -0700574
575 /**
Dianne Hackbornd98885c2016-03-01 17:13:03 -0800576 * Return the {@link SystemClock#elapsedRealtime()} at which this process was started.
577 */
578 public static final long getStartElapsedRealtime() {
579 return sStartElapsedRealtime;
580 }
581
582 /**
583 * Return the {@link SystemClock#uptimeMillis()} at which this process was started.
584 */
585 public static final long getStartUptimeMillis() {
586 return sStartUptimeMillis;
587 }
588
589 /** @hide */
590 public static final void setStartTimes(long elapsedRealtime, long uptimeMillis) {
591 sStartElapsedRealtime = elapsedRealtime;
592 sStartUptimeMillis = uptimeMillis;
593 }
594
595 /**
Andreas Gampeee0cdce2015-06-08 20:47:50 -0700596 * Returns true if the current process is a 64-bit runtime.
597 */
598 public static final boolean is64Bit() {
599 return VMRuntime.getRuntime().is64Bit();
600 }
601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 /**
603 * Returns the identifier of this process, which can be used with
604 * {@link #killProcess} and {@link #sendSignal}.
605 */
Jeff Hao406ec152013-07-30 10:13:41 -0700606 public static final int myPid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700607 return Os.getpid();
Jeff Hao406ec152013-07-30 10:13:41 -0700608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609
610 /**
Romain Guy80b12fc2013-05-29 15:54:25 -0700611 * Returns the identifier of this process' parent.
612 * @hide
613 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000614 @UnsupportedAppUsage
Elliott Hughes2a805f92013-07-31 18:25:47 -0700615 public static final int myPpid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700616 return Os.getppid();
Elliott Hughes2a805f92013-07-31 18:25:47 -0700617 }
Romain Guy80b12fc2013-05-29 15:54:25 -0700618
619 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 * Returns the identifier of the calling thread, which be used with
621 * {@link #setThreadPriority(int, int)}.
622 */
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700623 public static final int myTid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700624 return Os.gettid();
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626
627 /**
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700628 * Returns the identifier of this process's uid. This is the kernel uid
629 * that the process is running under, which is the identity of its
630 * app-specific sandbox. It is different from {@link #myUserHandle} in that
631 * a uid identifies a specific app sandbox in a specific user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 */
Jeff Hao406ec152013-07-30 10:13:41 -0700633 public static final int myUid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700634 return Os.getuid();
Jeff Hao406ec152013-07-30 10:13:41 -0700635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636
637 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700638 * Returns this process's user handle. This is the
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700639 * user the process is running under. It is distinct from
640 * {@link #myUid()} in that a particular user will have multiple
641 * distinct apps running under it each with their own uid.
642 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700643 public static UserHandle myUserHandle() {
644 return UserHandle.of(UserHandle.getUserId(myUid()));
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700645 }
646
647 /**
Makoto Onuki9cc471c2018-01-23 12:33:56 -0800648 * Returns whether the given uid belongs to a system core component or not.
649 * @hide
650 */
651 public static boolean isCoreUid(int uid) {
652 return UserHandle.isCore(uid);
653 }
654
655 /**
Jeff Davidson48aa86b2016-02-10 14:15:42 -0800656 * Returns whether the given uid belongs to an application.
657 * @param uid A kernel uid.
658 * @return Whether the uid corresponds to an application sandbox running in
659 * a specific user.
660 */
661 public static boolean isApplicationUid(int uid) {
662 return UserHandle.isApp(uid);
663 }
664
665 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800666 * Returns whether the current process is in an isolated sandbox.
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800667 */
668 public static final boolean isIsolated() {
Jeff Sharkey9527b222015-06-24 15:24:48 -0700669 return isIsolated(myUid());
670 }
671
672 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +0000673 @UnsupportedAppUsage
Jeff Sharkey9527b222015-06-24 15:24:48 -0700674 public static final boolean isIsolated(int uid) {
675 uid = UserHandle.getAppId(uid);
Martijn Coenen01e719b2018-12-05 16:01:38 +0100676 return (uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID)
677 || (uid >= FIRST_APP_ZYGOTE_ISOLATED_UID && uid <= LAST_APP_ZYGOTE_ISOLATED_UID);
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800678 }
679
680 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 * Returns the UID assigned to a particular user name, or -1 if there is
682 * none. If the given string consists of only numbers, it is converted
683 * directly to a uid.
684 */
685 public static final native int getUidForName(String name);
686
687 /**
688 * Returns the GID assigned to a particular user name, or -1 if there is
689 * none. If the given string consists of only numbers, it is converted
690 * directly to a gid.
691 */
692 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700693
694 /**
695 * Returns a uid for a currently running process.
696 * @param pid the process id
697 * @return the uid of the process, or -1 if the process is not running.
698 * @hide pending API council review
699 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000700 @UnsupportedAppUsage
Amith Yamasani819f9282009-06-24 23:18:15 -0700701 public static final int getUidForPid(int pid) {
702 String[] procStatusLabels = { "Uid:" };
703 long[] procStatusValues = new long[1];
704 procStatusValues[0] = -1;
705 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
706 return (int) procStatusValues[0];
707 }
708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700710 * Returns the parent process id for a currently running process.
711 * @param pid the process id
712 * @return the parent process id of the process, or -1 if the process is not running.
713 * @hide
714 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000715 @UnsupportedAppUsage
Jeff Brownebed7d62011-05-16 17:08:42 -0700716 public static final int getParentPid(int pid) {
717 String[] procStatusLabels = { "PPid:" };
718 long[] procStatusValues = new long[1];
719 procStatusValues[0] = -1;
720 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
721 return (int) procStatusValues[0];
722 }
723
724 /**
Glenn Kasten07b04652012-04-23 15:00:43 -0700725 * Returns the thread group leader id for a currently running thread.
726 * @param tid the thread id
727 * @return the thread group leader id of the thread, or -1 if the thread is not running.
728 * This is same as what getpid(2) would return if called by tid.
729 * @hide
730 */
731 public static final int getThreadGroupLeader(int tid) {
732 String[] procStatusLabels = { "Tgid:" };
733 long[] procStatusValues = new long[1];
734 procStatusValues[0] = -1;
735 Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues);
736 return (int) procStatusValues[0];
737 }
738
739 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 * Set the priority of a thread, based on Linux priorities.
741 *
742 * @param tid The identifier of the thread/process to change.
743 * @param priority A Linux priority level, from -20 for highest scheduling
744 * priority to 19 for lowest scheduling priority.
745 *
746 * @throws IllegalArgumentException Throws IllegalArgumentException if
747 * <var>tid</var> does not exist.
748 * @throws SecurityException Throws SecurityException if your process does
749 * not have permission to modify the given thread, or to use the given
750 * priority.
751 */
752 public static final native void setThreadPriority(int tid, int priority)
753 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700754
755 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700756 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
757 * throw an exception if passed a background-level thread priority. This is only
758 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
759 *
760 * @hide
761 */
762 public static final native void setCanSelfBackground(boolean backgroundOk);
763
764 /**
San Mehate9d376b2009-04-21 14:06:36 -0700765 * Sets the scheduling group for a thread.
766 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700767 * @param tid The identifier of the thread to change.
768 * @param group The target group for this thread from THREAD_GROUP_*.
Joel Fernandes474d3112017-04-04 16:32:15 -0700769 *
San Mehate9d376b2009-04-21 14:06:36 -0700770 * @throws IllegalArgumentException Throws IllegalArgumentException if
771 * <var>tid</var> does not exist.
772 * @throws SecurityException Throws SecurityException if your process does
773 * not have permission to modify the given thread, or to use the given
774 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700775 * If the thread is a thread group leader, that is it's gettid() == getpid(),
776 * then the other threads in the same thread group are _not_ affected.
Tim Murray4074ad02016-07-08 15:45:45 -0700777 *
778 * Does not set cpuset for some historical reason, just calls
779 * libcutils::set_sched_policy().
San Mehate9d376b2009-04-21 14:06:36 -0700780 */
781 public static final native void setThreadGroup(int tid, int group)
782 throws IllegalArgumentException, SecurityException;
Glenn Kastenf1b56442012-03-15 16:33:43 -0700783
San Mehat3e458242009-05-19 14:44:16 -0700784 /**
Joel Fernandes474d3112017-04-04 16:32:15 -0700785 * Sets the scheduling group and the corresponding cpuset group
786 * @hide
787 * @param tid The identifier of the thread to change.
788 * @param group The target group for this thread from THREAD_GROUP_*.
789 *
790 * @throws IllegalArgumentException Throws IllegalArgumentException if
791 * <var>tid</var> does not exist.
792 * @throws SecurityException Throws SecurityException if your process does
793 * not have permission to modify the given thread, or to use the given
794 * priority.
795 */
796 public static final native void setThreadGroupAndCpuset(int tid, int group)
797 throws IllegalArgumentException, SecurityException;
798
799 /**
San Mehat3e458242009-05-19 14:44:16 -0700800 * Sets the scheduling group for a process and all child threads
801 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700802 * @param pid The identifier of the process to change.
803 * @param group The target group for this process from THREAD_GROUP_*.
San Mehat3e458242009-05-19 14:44:16 -0700804 *
805 * @throws IllegalArgumentException Throws IllegalArgumentException if
806 * <var>tid</var> does not exist.
807 * @throws SecurityException Throws SecurityException if your process does
808 * not have permission to modify the given thread, or to use the given
809 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700810 *
811 * group == THREAD_GROUP_DEFAULT means to move all non-background priority
812 * threads to the foreground scheduling group, but to leave background
Rick Yiu021c0642019-10-03 17:29:20 +0800813 * priority threads alone. group == THREAD_GROUP_BACKGROUND moves all
Glenn Kastenf1b56442012-03-15 16:33:43 -0700814 * threads, regardless of priority, to the background scheduling group.
815 * group == THREAD_GROUP_FOREGROUND is not allowed.
Tim Murray4074ad02016-07-08 15:45:45 -0700816 *
817 * Always sets cpusets.
San Mehat3e458242009-05-19 14:44:16 -0700818 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000819 @UnsupportedAppUsage
San Mehat3e458242009-05-19 14:44:16 -0700820 public static final native void setProcessGroup(int pid, int group)
821 throws IllegalArgumentException, SecurityException;
Jeff Sharkey9e57c412013-01-17 14:12:41 -0800822
823 /**
824 * Return the scheduling group of requested process.
825 *
826 * @hide
827 */
828 public static final native int getProcessGroup(int pid)
829 throws IllegalArgumentException, SecurityException;
830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 /**
Martijn Coenencd4bdf32016-03-03 17:30:52 +0100832 * On some devices, the foreground process may have one or more CPU
833 * cores exclusively reserved for it. This method can be used to
834 * retrieve which cores that are (if any), so the calling process
835 * can then use sched_setaffinity() to lock a thread to these cores.
836 * Note that the calling process must currently be running in the
837 * foreground for this method to return any cores.
838 *
839 * The CPU core(s) exclusively reserved for the foreground process will
840 * stay reserved for as long as the process stays in the foreground.
841 *
842 * As soon as a process leaves the foreground, those CPU cores will
843 * no longer be reserved for it, and will most likely be reserved for
844 * the new foreground process. It's not necessary to change the affinity
845 * of your process when it leaves the foreground (if you had previously
846 * set it to use a reserved core); the OS will automatically take care
847 * of resetting the affinity at that point.
848 *
849 * @return an array of integers, indicating the CPU cores exclusively
850 * reserved for this process. The array will have length zero if no
851 * CPU cores are exclusively reserved for this process at this point
852 * in time.
853 */
854 public static final native int[] getExclusiveCores();
855
856 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 * Set the priority of the calling thread, based on Linux priorities. See
858 * {@link #setThreadPriority(int, int)} for more information.
859 *
860 * @param priority A Linux priority level, from -20 for highest scheduling
861 * priority to 19 for lowest scheduling priority.
862 *
863 * @throws IllegalArgumentException Throws IllegalArgumentException if
864 * <var>tid</var> does not exist.
865 * @throws SecurityException Throws SecurityException if your process does
866 * not have permission to modify the given thread, or to use the given
867 * priority.
868 *
869 * @see #setThreadPriority(int, int)
870 */
871 public static final native void setThreadPriority(int priority)
872 throws IllegalArgumentException, SecurityException;
873
874 /**
875 * Return the current priority of a thread, based on Linux priorities.
876 *
Alessio Balsini68ae3012017-06-07 11:41:33 +0200877 * @param tid The identifier of the thread/process. If tid equals zero, the priority of the
878 * calling process/thread will be returned.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 *
880 * @return Returns the current priority, as a Linux priority level,
881 * from -20 for highest scheduling priority to 19 for lowest scheduling
882 * priority.
883 *
884 * @throws IllegalArgumentException Throws IllegalArgumentException if
885 * <var>tid</var> does not exist.
886 */
887 public static final native int getThreadPriority(int tid)
888 throws IllegalArgumentException;
889
890 /**
Srinath Sridharan1b15d132016-07-19 15:16:11 -0700891 * Return the current scheduling policy of a thread, based on Linux.
892 *
893 * @param tid The identifier of the thread/process to get the scheduling policy.
894 *
895 * @throws IllegalArgumentException Throws IllegalArgumentException if
896 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
897 * @throws SecurityException Throws SecurityException if your process does
898 * not have permission to modify the given thread, or to use the given
899 * scheduling policy or priority.
900 *
901 * {@hide}
902 */
903
904 @TestApi
905 public static final native int getThreadScheduler(int tid)
906 throws IllegalArgumentException;
907
908 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700909 * Set the scheduling policy and priority of a thread, based on Linux.
910 *
911 * @param tid The identifier of the thread/process to change.
912 * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
913 * @param priority A Linux priority level in a range appropriate for the given policy.
914 *
915 * @throws IllegalArgumentException Throws IllegalArgumentException if
916 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
917 * @throws SecurityException Throws SecurityException if your process does
918 * not have permission to modify the given thread, or to use the given
919 * scheduling policy or priority.
920 *
921 * {@hide}
922 */
Srinath Sridharan1b15d132016-07-19 15:16:11 -0700923
Glenn Kasten6793ac92011-07-13 12:44:12 -0700924 public static final native void setThreadScheduler(int tid, int policy, int priority)
925 throws IllegalArgumentException;
926
927 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 * Determine whether the current environment supports multiple processes.
929 *
930 * @return Returns true if the system can run in multiple processes, else
931 * false if everything is running in a single process.
Jeff Brown10e89712011-07-08 18:52:57 -0700932 *
933 * @deprecated This method always returns true. Do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 */
Jeff Brown10e89712011-07-08 18:52:57 -0700935 @Deprecated
936 public static final boolean supportsProcesses() {
937 return true;
938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939
940 /**
Rom Lemarchand5534ba92013-07-12 16:15:36 -0700941 * Adjust the swappiness level for a process.
942 *
943 * @param pid The process identifier to set.
944 * @param is_increased Whether swappiness should be increased or default.
945 *
946 * @return Returns true if the underlying system supports this
947 * feature, else false.
948 *
949 * {@hide}
950 */
951 public static final native boolean setSwappiness(int pid, boolean is_increased);
952
953 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 * Change this process's argv[0] parameter. This can be useful to show
955 * more descriptive information in things like the 'ps' command.
956 *
957 * @param text The new name of this process.
958 *
959 * {@hide}
960 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000961 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 public static final native void setArgV0(String text);
963
964 /**
965 * Kill the process with the given PID.
966 * Note that, though this API allows us to request to
967 * kill any process based on its PID, the kernel will
968 * still impose standard restrictions on which PIDs you
969 * are actually able to kill. Typically this means only
970 * the process running the caller's packages/application
971 * and any additional processes created by that app; packages
972 * sharing a common UID will also be able to kill each
973 * other's processes.
974 */
975 public static final void killProcess(int pid) {
976 sendSignal(pid, SIGNAL_KILL);
977 }
978
979 /** @hide */
980 public static final native int setUid(int uid);
981
982 /** @hide */
983 public static final native int setGid(int uid);
984
985 /**
986 * Send a signal to the given process.
987 *
988 * @param pid The pid of the target process.
989 * @param signal The signal to send.
990 */
991 public static final native void sendSignal(int pid, int signal);
992
Dianne Hackborn906497c2010-05-10 15:57:38 -0700993 /**
994 * @hide
995 * Private impl for avoiding a log message... DO NOT USE without doing
996 * your own log, or the Android Illuminati will find you some night and
997 * beat you up.
998 */
999 public static final void killProcessQuiet(int pid) {
1000 sendSignalQuiet(pid, SIGNAL_KILL);
1001 }
1002
1003 /**
1004 * @hide
1005 * Private impl for avoiding a log message... DO NOT USE without doing
1006 * your own log, or the Android Illuminati will find you some night and
1007 * beat you up.
1008 */
Artur Satayev70507ed2019-07-29 13:18:27 +01001009 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Dianne Hackborn906497c2010-05-10 15:57:38 -07001010 public static final native void sendSignalQuiet(int pid, int signal);
1011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001013 @UnsupportedAppUsage
Marco Nelissen0bca96b2009-07-17 12:59:25 -07001014 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015
1016 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001017 @UnsupportedAppUsage
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001018 public static final native long getTotalMemory();
1019
1020 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001021 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 public static final native void readProcLines(String path,
1023 String[] reqFields, long[] outSizes);
1024
1025 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001026 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 public static final native int[] getPids(String path, int[] lastArray);
1028
1029 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001030 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 public static final int PROC_TERM_MASK = 0xff;
1032 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001033 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 public static final int PROC_ZERO_TERM = 0;
1035 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001036 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 public static final int PROC_SPACE_TERM = (int)' ';
1038 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001039 @UnsupportedAppUsage
Evan Millarc64edde2009-04-18 12:26:32 -07001040 public static final int PROC_TAB_TERM = (int)'\t';
1041 /** @hide */
Misha Wagner566903a2018-10-02 10:50:12 +01001042 public static final int PROC_NEWLINE_TERM = (int) '\n';
1043 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001044 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 public static final int PROC_COMBINE = 0x100;
1046 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001047 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 public static final int PROC_PARENS = 0x200;
1049 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001050 @UnsupportedAppUsage
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001051 public static final int PROC_QUOTES = 0x400;
1052 /** @hide */
Dianne Hackborn4de5a3a2016-06-20 15:20:15 -07001053 public static final int PROC_CHAR = 0x800;
1054 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001055 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 public static final int PROC_OUT_STRING = 0x1000;
1057 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001058 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 public static final int PROC_OUT_LONG = 0x2000;
1060 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001061 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 public static final int PROC_OUT_FLOAT = 0x4000;
Misha Wagnercc065fb2018-10-15 10:45:44 +01001063
1064 /**
1065 * Read and parse a {@code proc} file in the given format.
1066 *
1067 * <p>The format is a list of integers, where every integer describes a variable in the file. It
1068 * specifies how the variable is syntactically terminated (e.g. {@link Process#PROC_SPACE_TERM},
Misha Wagner566903a2018-10-02 10:50:12 +01001069 * {@link Process#PROC_TAB_TERM}, {@link Process#PROC_ZERO_TERM}, {@link
1070 * Process#PROC_NEWLINE_TERM}).
Misha Wagnercc065fb2018-10-15 10:45:44 +01001071 *
1072 * <p>If the variable should be parsed and returned to the caller, the termination type should
1073 * be binary OR'd with the type of output (e.g. {@link Process#PROC_OUT_STRING}, {@link
1074 * Process#PROC_OUT_LONG}, {@link Process#PROC_OUT_FLOAT}.
1075 *
1076 * <p>If the variable is wrapped in quotation marks it should be binary OR'd with {@link
1077 * Process#PROC_QUOTES}. If the variable is wrapped in parentheses it should be binary OR'd with
1078 * {@link Process#PROC_PARENS}.
1079 *
1080 * <p>If the variable is not formatted as a string and should be cast directly from characters
1081 * to a long, the {@link Process#PROC_CHAR} integer should be binary OR'd.
1082 *
1083 * <p>If the terminating character can be repeated, the {@link Process#PROC_COMBINE} integer
1084 * should be binary OR'd.
1085 *
1086 * @param file the path of the {@code proc} file to read
1087 * @param format the format of the file
1088 * @param outStrings the parsed {@code String}s from the file
1089 * @param outLongs the parsed {@code long}s from the file
1090 * @param outFloats the parsed {@code float}s from the file
1091 * @hide
1092 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001093 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 public static final native boolean readProcFile(String file, int[] format,
1095 String[] outStrings, long[] outLongs, float[] outFloats);
Misha Wagnercc065fb2018-10-15 10:45:44 +01001096
Evan Millarc64edde2009-04-18 12:26:32 -07001097 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001098 @UnsupportedAppUsage
Evan Millarc64edde2009-04-18 12:26:32 -07001099 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
1100 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001102 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001103 @UnsupportedAppUsage
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001104 public static final native int[] getPidsForCommands(String[] cmds);
1105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 /**
1107 * Gets the total Pss value for a given process, in bytes.
1108 *
1109 * @param pid the process to the Pss for
1110 * @return the total Pss value for the given process in bytes,
1111 * or -1 if the value cannot be determined
1112 * @hide
1113 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001114 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 public static final native long getPss(int pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -07001116
Tim Murrayfb076782018-11-27 12:22:22 -08001117 /** @hide */
1118 public static final native long[] getRss(int pid);
1119
Jeff Brown3f9dd282011-07-08 20:02:19 -07001120 /**
1121 * Specifies the outcome of having started a process.
1122 * @hide
1123 */
1124 public static final class ProcessStartResult {
1125 /**
1126 * The PID of the newly started process.
1127 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
1128 */
1129 public int pid;
1130
1131 /**
1132 * True if the process was started with a wrapper attached.
1133 */
1134 public boolean usingWrapper;
1135 }
Colin Cross0769e552014-06-03 13:25:35 -07001136
1137 /**
1138 * Kill all processes in a process group started for the given
1139 * pid.
1140 * @hide
1141 */
1142 public static final native int killProcessGroup(int uid, int pid);
1143
1144 /**
1145 * Remove all process groups. Expected to be called when ActivityManager
1146 * is restarted.
1147 * @hide
1148 */
1149 public static final native void removeAllProcessGroups();
Tim Murrayb89970e2016-07-01 10:42:20 -07001150
1151 /**
1152 * Check to see if a thread belongs to a given process. This may require
1153 * more permissions than apps generally have.
1154 * @return true if this thread belongs to a process
1155 * @hide
1156 */
1157 public static final boolean isThreadInProcess(int tid, int pid) {
Tim Murray9bc12a82016-07-16 11:28:40 -07001158 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
Tim Murrayb89970e2016-07-01 10:42:20 -07001159 try {
1160 if (Os.access("/proc/" + tid + "/task/" + pid, OsConstants.F_OK)) {
1161 return true;
1162 } else {
1163 return false;
1164 }
1165 } catch (Exception e) {
1166 return false;
Tim Murray9bc12a82016-07-16 11:28:40 -07001167 } finally {
1168 StrictMode.setThreadPolicy(oldPolicy);
Tim Murrayb89970e2016-07-01 10:42:20 -07001169 }
Tim Murray9bc12a82016-07-16 11:28:40 -07001170
Tim Murrayb89970e2016-07-01 10:42:20 -07001171 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172}