blob: d2ab053eb4e6e573aad9f304fef3184c6f701c33 [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;
Elliott Hughes34385d32014-04-28 11:11:32 -070022import android.system.Os;
Tim Murrayb89970e2016-07-01 10:42:20 -070023import android.system.OsConstants;
Robert Sesekded20982016-08-15 13:59:13 -040024import android.webkit.WebViewZygote;
Nicolas Geoffray81edac42017-09-07 14:13:29 +010025
Andreas Gampeee0cdce2015-06-08 20:47:50 -070026import dalvik.system.VMRuntime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
28/**
29 * Tools for managing OS processes.
30 */
31public class Process {
32 private static final String LOG_TAG = "Process";
33
Narayan Kamath64cd9072014-05-13 13:35:14 +010034 /**
Jeff Vander Stoep0ac2c092018-07-23 10:57:53 -070035 * An invalid UID value.
36 */
37 public static final int INVALID_UID = -1;
38
39 /**
Jeff Sharkey1cb2d0d2014-07-30 16:45:01 -070040 * Defines the root UID.
Jeff Sharkey1cb2d0d2014-07-30 16:45:01 -070041 */
42 public static final int ROOT_UID = 0;
43
44 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 * Defines the UID/GID under which system code runs.
46 */
47 public static final int SYSTEM_UID = 1000;
48
49 /**
50 * Defines the UID/GID under which the telephony code runs.
51 */
52 public static final int PHONE_UID = 1001;
53
54 /**
Dianne Hackborn854060af2009-07-09 18:14:31 -070055 * Defines the UID/GID for the user shell.
Dianne Hackborn854060af2009-07-09 18:14:31 -070056 */
57 public static final int SHELL_UID = 2000;
58
59 /**
Mike Lockwoodd42685d2009-09-03 09:25:22 -040060 * Defines the UID/GID for the log group.
61 * @hide
62 */
63 public static final int LOG_UID = 1007;
64
65 /**
Amith Yamasanid1582142009-07-08 20:04:55 -070066 * Defines the UID/GID for the WIFI supplicant process.
67 * @hide
68 */
69 public static final int WIFI_UID = 1010;
70
71 /**
Glenn Kasten8b7d1b42011-07-13 16:23:22 -070072 * Defines the UID/GID for the mediaserver process.
73 * @hide
74 */
75 public static final int MEDIA_UID = 1013;
76
77 /**
Jeff Sharkey5294a2f2012-04-24 17:07:22 -070078 * Defines the UID/GID for the DRM process.
79 * @hide
80 */
81 public static final int DRM_UID = 1019;
82
83 /**
Kenny Root26993b32012-03-19 15:07:51 -070084 * Defines the UID/GID for the group that controls VPN services.
85 * @hide
86 */
87 public static final int VPN_UID = 1016;
88
89 /**
Janis Danisevskis6ab9bb62017-03-08 10:35:00 -080090 * Defines the UID/GID for keystore.
91 * @hide
92 */
93 public static final int KEYSTORE_UID = 1017;
94
95 /**
Nick Pellycd0e8392010-10-13 17:25:24 -070096 * Defines the UID/GID for the NFC service process.
97 * @hide
98 */
Nick Pellya5cb9f42011-11-21 14:54:46 -080099 public static final int NFC_UID = 1027;
Nick Pellycd0e8392010-10-13 17:25:24 -0700100
101 /**
junyulaif6ebf4a2018-10-30 21:49:38 +0800102 * Defines the UID/GID for the clatd process.
103 * @hide
104 * */
105 public static final int CLAT_UID = 1029;
106
107 /**
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800108 * Defines the UID/GID for the Bluetooth service process.
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800109 */
110 public static final int BLUETOOTH_UID = 1002;
111
112 /**
Mike Lockwooddcaa10c2010-12-16 12:50:44 -0800113 * Defines the GID for the group that allows write access to the internal media storage.
114 * @hide
115 */
116 public static final int MEDIA_RW_GID = 1023;
117
118 /**
Jeff Sharkey184a0102013-07-10 16:19:52 -0700119 * Access to installed package details
120 * @hide
121 */
122 public static final int PACKAGE_INFO_GID = 1032;
123
124 /**
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100125 * Defines the UID/GID for the shared RELRO file updater process.
126 * @hide
127 */
128 public static final int SHARED_RELRO_UID = 1037;
129
130 /**
Andy Hunged0ea402015-10-30 14:11:46 -0700131 * Defines the UID/GID for the audioserver process.
132 * @hide
133 */
134 public static final int AUDIOSERVER_UID = 1041;
135
136 /**
Chien-Yu Chen75cade02016-01-11 10:56:21 -0800137 * Defines the UID/GID for the cameraserver process
138 * @hide
139 */
Chien-Yu Chenb8b4e852016-01-29 12:02:28 -0800140 public static final int CAMERASERVER_UID = 1047;
Chien-Yu Chen75cade02016-01-11 10:56:21 -0800141
142 /**
Lorenzo Colittif4b4f4d2018-03-01 21:06:11 +0900143 * Defines the UID/GID for the tethering DNS resolver (currently dnsmasq).
144 * @hide
145 */
146 public static final int DNS_TETHER_UID = 1052;
147
148 /**
Robert Sesekded20982016-08-15 13:59:13 -0400149 * Defines the UID/GID for the WebView zygote process.
150 * @hide
151 */
Robert Sesekbceb3822018-01-22 12:26:39 -0500152 public static final int WEBVIEW_ZYGOTE_UID = 1053;
Robert Sesekded20982016-08-15 13:59:13 -0400153
154 /**
Jeff Sharkey07c5c6f2017-03-21 10:09:50 -0600155 * Defines the UID used for resource tracking for OTA updates.
156 * @hide
157 */
158 public static final int OTA_UPDATE_UID = 1061;
159
Yi Jin4bab3a12018-01-10 16:50:59 -0800160 /**
161 * Defines the UID used for incidentd.
162 * @hide
163 */
164 public static final int INCIDENTD_UID = 1067;
165
Ruchi Kandoi6149b0f2018-01-03 16:14:57 -0800166 /**
167 * Defines the UID/GID for the Secure Element service process.
168 * @hide
169 */
170 public static final int SE_UID = 1068;
171
Remi NGUYEN VANc094a542018-12-07 16:52:24 +0900172 /**
173 * Defines the UID/GID for the NetworkStack app.
174 * @hide
175 */
176 public static final int NETWORK_STACK_UID = 1073;
177
Jeff Sharkeya73e1652017-11-15 19:07:14 -0700178 /** {@hide} */
179 public static final int NOBODY_UID = 9999;
180
Jeff Sharkey07c5c6f2017-03-21 10:09:50 -0600181 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 * Defines the start of a range of UIDs (and GIDs), going from this
183 * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning
184 * to applications.
185 */
186 public static final int FIRST_APPLICATION_UID = 10000;
Jeff Sharkey184a0102013-07-10 16:19:52 -0700187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 /**
189 * Last of application-specific UIDs starting at
190 * {@link #FIRST_APPLICATION_UID}.
191 */
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800192 public static final int LAST_APPLICATION_UID = 19999;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800193
194 /**
Martijn Coenen01e719b2018-12-05 16:01:38 +0100195 * First uid used for fully isolated sandboxed processes spawned from an app zygote
196 * @hide
197 */
Martijn Coenendbf6abc2019-01-10 08:37:45 +0100198 @TestApi
Martijn Coenen01e719b2018-12-05 16:01:38 +0100199 public static final int FIRST_APP_ZYGOTE_ISOLATED_UID = 90000;
200
201 /**
202 * Number of UIDs we allocate per application zygote
203 * @hide
204 */
Martijn Coenendbf6abc2019-01-10 08:37:45 +0100205 @TestApi
Martijn Coenen01e719b2018-12-05 16:01:38 +0100206 public static final int NUM_UIDS_PER_APP_ZYGOTE = 100;
207
208 /**
209 * Last uid used for fully isolated sandboxed processes spawned from an app zygote
210 * @hide
211 */
Martijn Coenendbf6abc2019-01-10 08:37:45 +0100212 @TestApi
Martijn Coenen01e719b2018-12-05 16:01:38 +0100213 public static final int LAST_APP_ZYGOTE_ISOLATED_UID = 98999;
214
215 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800216 * First uid used for fully isolated sandboxed processes (with no permissions of their own)
217 * @hide
218 */
Martijn Coenendbf6abc2019-01-10 08:37:45 +0100219 @TestApi
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800220 public static final int FIRST_ISOLATED_UID = 99000;
221
222 /**
223 * Last uid used for fully isolated sandboxed processes (with no permissions of their own)
224 * @hide
225 */
Martijn Coenendbf6abc2019-01-10 08:37:45 +0100226 @TestApi
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800227 public static final int LAST_ISOLATED_UID = 99999;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228
229 /**
Robin Leee66b6892014-04-29 12:36:47 +0100230 * Defines the gid shared by all applications running under the same profile.
231 * @hide
232 */
233 public static final int SHARED_USER_GID = 9997;
234
235 /**
Kenny Roote091f222012-09-11 15:01:26 -0700236 * First gid for applications to share resources. Used when forward-locking
237 * is enabled but all UserHandles need to be able to read the resources.
238 * @hide
239 */
240 public static final int FIRST_SHARED_APPLICATION_GID = 50000;
241
242 /**
243 * Last gid for applications to share resources. Used when forward-locking
244 * is enabled but all UserHandles need to be able to read the resources.
245 * @hide
246 */
247 public static final int LAST_SHARED_APPLICATION_GID = 59999;
248
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700249 /** {@hide} */
250 public static final int FIRST_APPLICATION_CACHE_GID = 20000;
251 /** {@hide} */
252 public static final int LAST_APPLICATION_CACHE_GID = 29999;
253
Kenny Roote091f222012-09-11 15:01:26 -0700254 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 * Standard priority of application threads.
256 * Use with {@link #setThreadPriority(int)} and
257 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
258 * {@link java.lang.Thread} class.
259 */
260 public static final int THREAD_PRIORITY_DEFAULT = 0;
261
262 /*
263 * ***************************************
264 * ** Keep in sync with utils/threads.h **
265 * ***************************************
266 */
267
268 /**
269 * Lowest available thread priority. Only for those who really, really
270 * don't want to run if anything else is happening.
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_LOWEST = 19;
276
277 /**
278 * Standard priority background threads. This gives your thread a slightly
279 * lower than normal priority, so that it will have less chance of impacting
280 * the responsiveness of the user interface.
281 * Use with {@link #setThreadPriority(int)} and
282 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
283 * {@link java.lang.Thread} class.
284 */
285 public static final int THREAD_PRIORITY_BACKGROUND = 10;
286
287 /**
288 * Standard priority of threads that are currently running a user interface
289 * that the user is interacting with. Applications can not normally
290 * change to this priority; the system will automatically adjust your
291 * application threads as the user moves through the UI.
292 * Use with {@link #setThreadPriority(int)} and
293 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
294 * {@link java.lang.Thread} class.
295 */
296 public static final int THREAD_PRIORITY_FOREGROUND = -2;
297
298 /**
299 * Standard priority of system display threads, involved in updating
300 * the user interface. Applications can not
301 * normally change to this priority.
302 * Use with {@link #setThreadPriority(int)} and
303 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
304 * {@link java.lang.Thread} class.
305 */
306 public static final int THREAD_PRIORITY_DISPLAY = -4;
307
308 /**
309 * Standard priority of the most important display threads, for compositing
310 * the screen and retrieving input events. Applications can not normally
311 * change to this priority.
312 * Use with {@link #setThreadPriority(int)} and
313 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
314 * {@link java.lang.Thread} class.
315 */
316 public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8;
317
318 /**
Jeff Tinker9765e202018-01-08 15:05:48 -0800319 * Standard priority of video threads. Applications can not normally
320 * change to this priority.
321 * Use with {@link #setThreadPriority(int)} and
322 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
323 * {@link java.lang.Thread} class.
324 */
325 public static final int THREAD_PRIORITY_VIDEO = -10;
326
327 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 * Standard priority of audio threads. Applications can not normally
329 * change to this priority.
330 * Use with {@link #setThreadPriority(int)} and
331 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
332 * {@link java.lang.Thread} class.
333 */
334 public static final int THREAD_PRIORITY_AUDIO = -16;
335
336 /**
337 * Standard priority of the most important audio threads.
338 * Applications can not normally change to this priority.
339 * Use with {@link #setThreadPriority(int)} and
340 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
341 * {@link java.lang.Thread} class.
342 */
343 public static final int THREAD_PRIORITY_URGENT_AUDIO = -19;
344
345 /**
346 * Minimum increment to make a priority more favorable.
347 */
348 public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
349
350 /**
351 * Minimum increment to make a priority less favorable.
352 */
353 public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
354
San Mehate9d376b2009-04-21 14:06:36 -0700355 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700356 * Default scheduling policy
357 * @hide
358 */
359 public static final int SCHED_OTHER = 0;
360
361 /**
362 * First-In First-Out scheduling policy
363 * @hide
364 */
365 public static final int SCHED_FIFO = 1;
366
367 /**
368 * Round-Robin scheduling policy
369 * @hide
370 */
371 public static final int SCHED_RR = 2;
372
373 /**
374 * Batch scheduling policy
375 * @hide
376 */
377 public static final int SCHED_BATCH = 3;
378
379 /**
380 * Idle scheduling policy
381 * @hide
382 */
383 public static final int SCHED_IDLE = 5;
384
Srinath Sridharane535a582016-06-27 18:13:47 -0700385 /**
386 * Reset scheduler choice on fork.
387 * @hide
388 */
389 public static final int SCHED_RESET_ON_FORK = 0x40000000;
390
Glenn Kastenf1b56442012-03-15 16:33:43 -0700391 // Keep in sync with SP_* constants of enum type SchedPolicy
392 // declared in system/core/include/cutils/sched_policy.h,
393 // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
San Mehate9d376b2009-04-21 14:06:36 -0700394
395 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700396 * Default thread group -
397 * has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
398 * When used with setProcessGroup(), the group of each thread in the process
399 * is conditionally changed based on that thread's current priority, as follows:
400 * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
401 * are moved to foreground thread group. All other threads are left unchanged.
402 * @hide
403 */
404 public static final int THREAD_GROUP_DEFAULT = -1;
405
406 /**
407 * Background thread group - All threads in
San Mehate9d376b2009-04-21 14:06:36 -0700408 * this group are scheduled with a reduced share of the CPU.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700409 * Value is same as constant SP_BACKGROUND of enum SchedPolicy.
410 * FIXME rename to THREAD_GROUP_BACKGROUND.
San Mehate9d376b2009-04-21 14:06:36 -0700411 * @hide
412 */
Glenn Kastenf1b56442012-03-15 16:33:43 -0700413 public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0;
San Mehate9d376b2009-04-21 14:06:36 -0700414
415 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700416 * Foreground thread group - All threads in
417 * this group are scheduled with a normal share of the CPU.
418 * Value is same as constant SP_FOREGROUND of enum SchedPolicy.
419 * Not used at this level.
San Mehate9d376b2009-04-21 14:06:36 -0700420 * @hide
421 **/
Glenn Kastenf1b56442012-03-15 16:33:43 -0700422 private static final int THREAD_GROUP_FOREGROUND = 1;
San Mehate9d376b2009-04-21 14:06:36 -0700423
Glenn Kasten07b04652012-04-23 15:00:43 -0700424 /**
425 * System thread group.
426 * @hide
427 **/
428 public static final int THREAD_GROUP_SYSTEM = 2;
429
430 /**
431 * Application audio thread group.
432 * @hide
433 **/
434 public static final int THREAD_GROUP_AUDIO_APP = 3;
435
436 /**
437 * System audio thread group.
438 * @hide
439 **/
440 public static final int THREAD_GROUP_AUDIO_SYS = 4;
441
Tim Murrayece6a222016-01-14 11:22:57 -0800442 /**
443 * Thread group for top foreground app.
444 * @hide
445 **/
446 public static final int THREAD_GROUP_TOP_APP = 5;
447
Joel Fernandes59776002017-04-17 13:40:16 -0700448 /**
449 * Thread group for RT app.
450 * @hide
451 **/
452 public static final int THREAD_GROUP_RT_APP = 6;
453
Tim Murrayfef10a42018-04-13 10:15:17 -0700454 /**
455 * Thread group for bound foreground services that should
456 * have additional CPU restrictions during screen off
457 * @hide
458 **/
459 public static final int THREAD_GROUP_RESTRICTED = 7;
460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 public static final int SIGNAL_QUIT = 3;
462 public static final int SIGNAL_KILL = 9;
463 public static final int SIGNAL_USR1 = 10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464
Dianne Hackbornd98885c2016-03-01 17:13:03 -0800465 private static long sStartElapsedRealtime;
466 private static long sStartUptimeMillis;
467
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100468 /**
Robert Sesek8f8d1872016-03-18 16:52:57 -0400469 * State associated with the zygote process.
470 * @hide
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100471 */
Chris Wailesefce9292019-01-11 13:19:20 -0800472 public static final ZygoteProcess ZYGOTE_PROCESS = new ZygoteProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473
474 /**
475 * Start a new process.
476 *
477 * <p>If processes are enabled, a new process is created and the
478 * static main() function of a <var>processClass</var> is executed there.
479 * The process will continue running after this function returns.
480 *
481 * <p>If processes are not enabled, a new thread in the caller's
482 * process is created and main() of <var>processClass</var> called there.
483 *
484 * <p>The niceName parameter, if not an empty string, is a custom name to
485 * give to the process instead of using processClass. This allows you to
486 * make easily identifyable processes even if you are using the same base
487 * <var>processClass</var> to start them.
488 *
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000489 * When invokeWith is not null, the process will be started as a fresh app
Tamas Berghammer0ca16fa2016-11-11 16:08:26 +0000490 * and not a zygote fork. Note that this is only allowed for uid 0 or when
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100491 * runtimeFlags contains DEBUG_ENABLE_DEBUGGER.
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000492 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 * @param processClass The class to use as the process's main entry
494 * point.
495 * @param niceName A more readable name to use for the process.
496 * @param uid The user-id under which the process will run.
497 * @param gid The group-id under which the process will run.
498 * @param gids Additional group-ids associated with the process.
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100499 * @param runtimeFlags Additional flags for the runtime.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700500 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalleybd19b9e2013-04-12 14:49:38 -0400501 * @param seInfo null-ok SELinux information for the new process.
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100502 * @param abi non-null the ABI this app should be started with.
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700503 * @param instructionSet null-ok the instruction set to use.
jgu212eacd062014-09-10 06:55:07 -0400504 * @param appDataDir null-ok the data directory of the app.
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000505 * @param invokeWith null-ok the command to invoke with.
Sudheer Shankad81b1d72018-09-05 16:37:30 -0700506 * @param packageName null-ok the name of the package this process belongs to.
Sudheer Shanka3f0645b2018-09-18 13:07:59 -0700507 * @param packagesForUid null-ok all the packages with the same uid as this process.
508 * @param visibleVols null-ok storage volumes that can be accessed by this process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 * @param zygoteArgs Additional arguments to supply to the zygote process.
510 *
Jeff Brown3f9dd282011-07-08 20:02:19 -0700511 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 * @throws RuntimeException on fatal start failure
513 *
514 * {@hide}
515 */
Sudheer Shankad81b1d72018-09-05 16:37:30 -0700516 public static final ProcessStartResult start(@NonNull final String processClass,
517 @Nullable final String niceName,
518 int uid, int gid, @Nullable int[] gids,
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100519 int runtimeFlags, int mountExternal,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700520 int targetSdkVersion,
Sudheer Shankad81b1d72018-09-05 16:37:30 -0700521 @Nullable String seInfo,
522 @NonNull String abi,
523 @Nullable String instructionSet,
524 @Nullable String appDataDir,
525 @Nullable String invokeWith,
526 @Nullable String packageName,
Sudheer Shanka3f0645b2018-09-18 13:07:59 -0700527 @Nullable String[] packagesForUid,
528 @Nullable String[] visibleVols,
Sudheer Shankad81b1d72018-09-05 16:37:30 -0700529 @Nullable String[] zygoteArgs) {
Chris Wailesefce9292019-01-11 13:19:20 -0800530 return ZYGOTE_PROCESS.start(processClass, niceName, uid, gid, gids,
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100531 runtimeFlags, mountExternal, targetSdkVersion, seInfo,
Sudheer Shanka3f0645b2018-09-18 13:07:59 -0700532 abi, instructionSet, appDataDir, invokeWith, packageName,
Chris Wailesba4c2eb2019-01-11 17:13:00 -0800533 packagesForUid, visibleVols, /*useBlastulaPool=*/ true, zygoteArgs);
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100534 }
535
Robert Sesekded20982016-08-15 13:59:13 -0400536 /** @hide */
Sudheer Shankad81b1d72018-09-05 16:37:30 -0700537 public static final ProcessStartResult startWebView(@NonNull final String processClass,
538 @Nullable final String niceName,
539 int uid, int gid, @Nullable int[] gids,
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100540 int runtimeFlags, int mountExternal,
Robert Sesekded20982016-08-15 13:59:13 -0400541 int targetSdkVersion,
Sudheer Shankad81b1d72018-09-05 16:37:30 -0700542 @Nullable String seInfo,
543 @NonNull String abi,
544 @Nullable String instructionSet,
545 @Nullable String appDataDir,
546 @Nullable String invokeWith,
547 @Nullable String packageName,
Sudheer Shanka3f0645b2018-09-18 13:07:59 -0700548 @Nullable String[] packagesForUid,
549 @Nullable String[] visibleVols,
Sudheer Shankad81b1d72018-09-05 16:37:30 -0700550 @Nullable String[] zygoteArgs) {
Robert Sesekded20982016-08-15 13:59:13 -0400551 return WebViewZygote.getProcess().start(processClass, niceName, uid, gid, gids,
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100552 runtimeFlags, mountExternal, targetSdkVersion, seInfo,
Sudheer Shanka3f0645b2018-09-18 13:07:59 -0700553 abi, instructionSet, appDataDir, invokeWith, packageName,
Chris Wailesba4c2eb2019-01-11 17:13:00 -0800554 packagesForUid, visibleVols, /*useBlastulaPool=*/ false, zygoteArgs);
Robert Sesekded20982016-08-15 13:59:13 -0400555 }
556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 /**
558 * Returns elapsed milliseconds of the time this process has run.
559 * @return Returns the number of milliseconds this process has return.
560 */
561 public static final native long getElapsedCpuTime();
Andreas Gampeee0cdce2015-06-08 20:47:50 -0700562
563 /**
Dianne Hackbornd98885c2016-03-01 17:13:03 -0800564 * Return the {@link SystemClock#elapsedRealtime()} at which this process was started.
565 */
566 public static final long getStartElapsedRealtime() {
567 return sStartElapsedRealtime;
568 }
569
570 /**
571 * Return the {@link SystemClock#uptimeMillis()} at which this process was started.
572 */
573 public static final long getStartUptimeMillis() {
574 return sStartUptimeMillis;
575 }
576
577 /** @hide */
578 public static final void setStartTimes(long elapsedRealtime, long uptimeMillis) {
579 sStartElapsedRealtime = elapsedRealtime;
580 sStartUptimeMillis = uptimeMillis;
581 }
582
583 /**
Andreas Gampeee0cdce2015-06-08 20:47:50 -0700584 * Returns true if the current process is a 64-bit runtime.
585 */
586 public static final boolean is64Bit() {
587 return VMRuntime.getRuntime().is64Bit();
588 }
589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 /**
591 * Returns the identifier of this process, which can be used with
592 * {@link #killProcess} and {@link #sendSignal}.
593 */
Jeff Hao406ec152013-07-30 10:13:41 -0700594 public static final int myPid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700595 return Os.getpid();
Jeff Hao406ec152013-07-30 10:13:41 -0700596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597
598 /**
Romain Guy80b12fc2013-05-29 15:54:25 -0700599 * Returns the identifier of this process' parent.
600 * @hide
601 */
Elliott Hughes2a805f92013-07-31 18:25:47 -0700602 public static final int myPpid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700603 return Os.getppid();
Elliott Hughes2a805f92013-07-31 18:25:47 -0700604 }
Romain Guy80b12fc2013-05-29 15:54:25 -0700605
606 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 * Returns the identifier of the calling thread, which be used with
608 * {@link #setThreadPriority(int, int)}.
609 */
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700610 public static final int myTid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700611 return Os.gettid();
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613
614 /**
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700615 * Returns the identifier of this process's uid. This is the kernel uid
616 * that the process is running under, which is the identity of its
617 * app-specific sandbox. It is different from {@link #myUserHandle} in that
618 * a uid identifies a specific app sandbox in a specific user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 */
Jeff Hao406ec152013-07-30 10:13:41 -0700620 public static final int myUid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700621 return Os.getuid();
Jeff Hao406ec152013-07-30 10:13:41 -0700622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623
624 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700625 * Returns this process's user handle. This is the
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700626 * user the process is running under. It is distinct from
627 * {@link #myUid()} in that a particular user will have multiple
628 * distinct apps running under it each with their own uid.
629 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700630 public static UserHandle myUserHandle() {
631 return UserHandle.of(UserHandle.getUserId(myUid()));
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700632 }
633
634 /**
Makoto Onuki9cc471c2018-01-23 12:33:56 -0800635 * Returns whether the given uid belongs to a system core component or not.
636 * @hide
637 */
638 public static boolean isCoreUid(int uid) {
639 return UserHandle.isCore(uid);
640 }
641
642 /**
Jeff Davidson48aa86b2016-02-10 14:15:42 -0800643 * Returns whether the given uid belongs to an application.
644 * @param uid A kernel uid.
645 * @return Whether the uid corresponds to an application sandbox running in
646 * a specific user.
647 */
648 public static boolean isApplicationUid(int uid) {
649 return UserHandle.isApp(uid);
650 }
651
652 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800653 * Returns whether the current process is in an isolated sandbox.
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800654 */
655 public static final boolean isIsolated() {
Jeff Sharkey9527b222015-06-24 15:24:48 -0700656 return isIsolated(myUid());
657 }
658
659 /** {@hide} */
660 public static final boolean isIsolated(int uid) {
661 uid = UserHandle.getAppId(uid);
Martijn Coenen01e719b2018-12-05 16:01:38 +0100662 return (uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID)
663 || (uid >= FIRST_APP_ZYGOTE_ISOLATED_UID && uid <= LAST_APP_ZYGOTE_ISOLATED_UID);
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800664 }
665
666 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 * Returns the UID assigned to a particular user name, or -1 if there is
668 * none. If the given string consists of only numbers, it is converted
669 * directly to a uid.
670 */
671 public static final native int getUidForName(String name);
672
673 /**
674 * Returns the GID assigned to a particular user name, or -1 if there is
675 * none. If the given string consists of only numbers, it is converted
676 * directly to a gid.
677 */
678 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700679
680 /**
681 * Returns a uid for a currently running process.
682 * @param pid the process id
683 * @return the uid of the process, or -1 if the process is not running.
684 * @hide pending API council review
685 */
686 public static final int getUidForPid(int pid) {
687 String[] procStatusLabels = { "Uid:" };
688 long[] procStatusValues = new long[1];
689 procStatusValues[0] = -1;
690 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
691 return (int) procStatusValues[0];
692 }
693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700695 * Returns the parent process id for a currently running process.
696 * @param pid the process id
697 * @return the parent process id of the process, or -1 if the process is not running.
698 * @hide
699 */
700 public static final int getParentPid(int pid) {
701 String[] procStatusLabels = { "PPid:" };
702 long[] procStatusValues = new long[1];
703 procStatusValues[0] = -1;
704 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
705 return (int) procStatusValues[0];
706 }
707
708 /**
Glenn Kasten07b04652012-04-23 15:00:43 -0700709 * Returns the thread group leader id for a currently running thread.
710 * @param tid the thread id
711 * @return the thread group leader id of the thread, or -1 if the thread is not running.
712 * This is same as what getpid(2) would return if called by tid.
713 * @hide
714 */
715 public static final int getThreadGroupLeader(int tid) {
716 String[] procStatusLabels = { "Tgid:" };
717 long[] procStatusValues = new long[1];
718 procStatusValues[0] = -1;
719 Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues);
720 return (int) procStatusValues[0];
721 }
722
723 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 * Set the priority of a thread, based on Linux priorities.
725 *
726 * @param tid The identifier of the thread/process to change.
727 * @param priority A Linux priority level, from -20 for highest scheduling
728 * priority to 19 for lowest scheduling priority.
729 *
730 * @throws IllegalArgumentException Throws IllegalArgumentException if
731 * <var>tid</var> does not exist.
732 * @throws SecurityException Throws SecurityException if your process does
733 * not have permission to modify the given thread, or to use the given
734 * priority.
735 */
736 public static final native void setThreadPriority(int tid, int priority)
737 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700738
739 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700740 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
741 * throw an exception if passed a background-level thread priority. This is only
742 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
743 *
744 * @hide
745 */
746 public static final native void setCanSelfBackground(boolean backgroundOk);
747
748 /**
San Mehate9d376b2009-04-21 14:06:36 -0700749 * Sets the scheduling group for a thread.
750 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700751 * @param tid The identifier of the thread to change.
752 * @param group The target group for this thread from THREAD_GROUP_*.
Joel Fernandes474d3112017-04-04 16:32:15 -0700753 *
San Mehate9d376b2009-04-21 14:06:36 -0700754 * @throws IllegalArgumentException Throws IllegalArgumentException if
755 * <var>tid</var> does not exist.
756 * @throws SecurityException Throws SecurityException if your process does
757 * not have permission to modify the given thread, or to use the given
758 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700759 * If the thread is a thread group leader, that is it's gettid() == getpid(),
760 * then the other threads in the same thread group are _not_ affected.
Tim Murray4074ad02016-07-08 15:45:45 -0700761 *
762 * Does not set cpuset for some historical reason, just calls
763 * libcutils::set_sched_policy().
San Mehate9d376b2009-04-21 14:06:36 -0700764 */
765 public static final native void setThreadGroup(int tid, int group)
766 throws IllegalArgumentException, SecurityException;
Glenn Kastenf1b56442012-03-15 16:33:43 -0700767
San Mehat3e458242009-05-19 14:44:16 -0700768 /**
Joel Fernandes474d3112017-04-04 16:32:15 -0700769 * Sets the scheduling group and the corresponding cpuset group
770 * @hide
771 * @param tid The identifier of the thread to change.
772 * @param group The target group for this thread from THREAD_GROUP_*.
773 *
774 * @throws IllegalArgumentException Throws IllegalArgumentException if
775 * <var>tid</var> does not exist.
776 * @throws SecurityException Throws SecurityException if your process does
777 * not have permission to modify the given thread, or to use the given
778 * priority.
779 */
780 public static final native void setThreadGroupAndCpuset(int tid, int group)
781 throws IllegalArgumentException, SecurityException;
782
783 /**
San Mehat3e458242009-05-19 14:44:16 -0700784 * Sets the scheduling group for a process and all child threads
785 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700786 * @param pid The identifier of the process to change.
787 * @param group The target group for this process from THREAD_GROUP_*.
San Mehat3e458242009-05-19 14:44:16 -0700788 *
789 * @throws IllegalArgumentException Throws IllegalArgumentException if
790 * <var>tid</var> does not exist.
791 * @throws SecurityException Throws SecurityException if your process does
792 * not have permission to modify the given thread, or to use the given
793 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700794 *
795 * group == THREAD_GROUP_DEFAULT means to move all non-background priority
796 * threads to the foreground scheduling group, but to leave background
797 * priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all
798 * threads, regardless of priority, to the background scheduling group.
799 * group == THREAD_GROUP_FOREGROUND is not allowed.
Tim Murray4074ad02016-07-08 15:45:45 -0700800 *
801 * Always sets cpusets.
San Mehat3e458242009-05-19 14:44:16 -0700802 */
803 public static final native void setProcessGroup(int pid, int group)
804 throws IllegalArgumentException, SecurityException;
Jeff Sharkey9e57c412013-01-17 14:12:41 -0800805
806 /**
807 * Return the scheduling group of requested process.
808 *
809 * @hide
810 */
811 public static final native int getProcessGroup(int pid)
812 throws IllegalArgumentException, SecurityException;
813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 /**
Martijn Coenencd4bdf32016-03-03 17:30:52 +0100815 * On some devices, the foreground process may have one or more CPU
816 * cores exclusively reserved for it. This method can be used to
817 * retrieve which cores that are (if any), so the calling process
818 * can then use sched_setaffinity() to lock a thread to these cores.
819 * Note that the calling process must currently be running in the
820 * foreground for this method to return any cores.
821 *
822 * The CPU core(s) exclusively reserved for the foreground process will
823 * stay reserved for as long as the process stays in the foreground.
824 *
825 * As soon as a process leaves the foreground, those CPU cores will
826 * no longer be reserved for it, and will most likely be reserved for
827 * the new foreground process. It's not necessary to change the affinity
828 * of your process when it leaves the foreground (if you had previously
829 * set it to use a reserved core); the OS will automatically take care
830 * of resetting the affinity at that point.
831 *
832 * @return an array of integers, indicating the CPU cores exclusively
833 * reserved for this process. The array will have length zero if no
834 * CPU cores are exclusively reserved for this process at this point
835 * in time.
836 */
837 public static final native int[] getExclusiveCores();
838
839 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 * Set the priority of the calling thread, based on Linux priorities. See
841 * {@link #setThreadPriority(int, int)} for more information.
842 *
843 * @param priority A Linux priority level, from -20 for highest scheduling
844 * priority to 19 for lowest scheduling priority.
845 *
846 * @throws IllegalArgumentException Throws IllegalArgumentException if
847 * <var>tid</var> does not exist.
848 * @throws SecurityException Throws SecurityException if your process does
849 * not have permission to modify the given thread, or to use the given
850 * priority.
851 *
852 * @see #setThreadPriority(int, int)
853 */
854 public static final native void setThreadPriority(int priority)
855 throws IllegalArgumentException, SecurityException;
856
857 /**
858 * Return the current priority of a thread, based on Linux priorities.
859 *
Alessio Balsini68ae3012017-06-07 11:41:33 +0200860 * @param tid The identifier of the thread/process. If tid equals zero, the priority of the
861 * calling process/thread will be returned.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 *
863 * @return Returns the current priority, as a Linux priority level,
864 * from -20 for highest scheduling priority to 19 for lowest scheduling
865 * priority.
866 *
867 * @throws IllegalArgumentException Throws IllegalArgumentException if
868 * <var>tid</var> does not exist.
869 */
870 public static final native int getThreadPriority(int tid)
871 throws IllegalArgumentException;
872
873 /**
Srinath Sridharan1b15d132016-07-19 15:16:11 -0700874 * Return the current scheduling policy of a thread, based on Linux.
875 *
876 * @param tid The identifier of the thread/process to get the scheduling policy.
877 *
878 * @throws IllegalArgumentException Throws IllegalArgumentException if
879 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
880 * @throws SecurityException Throws SecurityException if your process does
881 * not have permission to modify the given thread, or to use the given
882 * scheduling policy or priority.
883 *
884 * {@hide}
885 */
886
887 @TestApi
888 public static final native int getThreadScheduler(int tid)
889 throws IllegalArgumentException;
890
891 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700892 * Set the scheduling policy and priority of a thread, based on Linux.
893 *
894 * @param tid The identifier of the thread/process to change.
895 * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
896 * @param priority A Linux priority level in a range appropriate for the given policy.
897 *
898 * @throws IllegalArgumentException Throws IllegalArgumentException if
899 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
900 * @throws SecurityException Throws SecurityException if your process does
901 * not have permission to modify the given thread, or to use the given
902 * scheduling policy or priority.
903 *
904 * {@hide}
905 */
Srinath Sridharan1b15d132016-07-19 15:16:11 -0700906
Glenn Kasten6793ac92011-07-13 12:44:12 -0700907 public static final native void setThreadScheduler(int tid, int policy, int priority)
908 throws IllegalArgumentException;
909
910 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 * Determine whether the current environment supports multiple processes.
912 *
913 * @return Returns true if the system can run in multiple processes, else
914 * false if everything is running in a single process.
Jeff Brown10e89712011-07-08 18:52:57 -0700915 *
916 * @deprecated This method always returns true. Do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 */
Jeff Brown10e89712011-07-08 18:52:57 -0700918 @Deprecated
919 public static final boolean supportsProcesses() {
920 return true;
921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922
923 /**
Rom Lemarchand5534ba92013-07-12 16:15:36 -0700924 * Adjust the swappiness level for a process.
925 *
926 * @param pid The process identifier to set.
927 * @param is_increased Whether swappiness should be increased or default.
928 *
929 * @return Returns true if the underlying system supports this
930 * feature, else false.
931 *
932 * {@hide}
933 */
934 public static final native boolean setSwappiness(int pid, boolean is_increased);
935
936 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 * Change this process's argv[0] parameter. This can be useful to show
938 * more descriptive information in things like the 'ps' command.
939 *
940 * @param text The new name of this process.
941 *
942 * {@hide}
943 */
944 public static final native void setArgV0(String text);
945
946 /**
947 * Kill the process with the given PID.
948 * Note that, though this API allows us to request to
949 * kill any process based on its PID, the kernel will
950 * still impose standard restrictions on which PIDs you
951 * are actually able to kill. Typically this means only
952 * the process running the caller's packages/application
953 * and any additional processes created by that app; packages
954 * sharing a common UID will also be able to kill each
955 * other's processes.
956 */
957 public static final void killProcess(int pid) {
958 sendSignal(pid, SIGNAL_KILL);
959 }
960
961 /** @hide */
962 public static final native int setUid(int uid);
963
964 /** @hide */
965 public static final native int setGid(int uid);
966
967 /**
968 * Send a signal to the given process.
969 *
970 * @param pid The pid of the target process.
971 * @param signal The signal to send.
972 */
973 public static final native void sendSignal(int pid, int signal);
974
Dianne Hackborn906497c2010-05-10 15:57:38 -0700975 /**
976 * @hide
977 * Private impl for avoiding a log message... DO NOT USE without doing
978 * your own log, or the Android Illuminati will find you some night and
979 * beat you up.
980 */
981 public static final void killProcessQuiet(int pid) {
982 sendSignalQuiet(pid, SIGNAL_KILL);
983 }
984
985 /**
986 * @hide
987 * Private impl for avoiding a log message... DO NOT USE without doing
988 * your own log, or the Android Illuminati will find you some night and
989 * beat you up.
990 */
991 public static final native void sendSignalQuiet(int pid, int signal);
992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 /** @hide */
Marco Nelissen0bca96b2009-07-17 12:59:25 -0700994 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995
996 /** @hide */
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700997 public static final native long getTotalMemory();
998
999 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 public static final native void readProcLines(String path,
1001 String[] reqFields, long[] outSizes);
1002
1003 /** @hide */
1004 public static final native int[] getPids(String path, int[] lastArray);
1005
1006 /** @hide */
1007 public static final int PROC_TERM_MASK = 0xff;
1008 /** @hide */
1009 public static final int PROC_ZERO_TERM = 0;
1010 /** @hide */
1011 public static final int PROC_SPACE_TERM = (int)' ';
1012 /** @hide */
Evan Millarc64edde2009-04-18 12:26:32 -07001013 public static final int PROC_TAB_TERM = (int)'\t';
1014 /** @hide */
Misha Wagner566903a2018-10-02 10:50:12 +01001015 public static final int PROC_NEWLINE_TERM = (int) '\n';
1016 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 public static final int PROC_COMBINE = 0x100;
1018 /** @hide */
1019 public static final int PROC_PARENS = 0x200;
1020 /** @hide */
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001021 public static final int PROC_QUOTES = 0x400;
1022 /** @hide */
Dianne Hackborn4de5a3a2016-06-20 15:20:15 -07001023 public static final int PROC_CHAR = 0x800;
1024 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 public static final int PROC_OUT_STRING = 0x1000;
1026 /** @hide */
1027 public static final int PROC_OUT_LONG = 0x2000;
1028 /** @hide */
1029 public static final int PROC_OUT_FLOAT = 0x4000;
Misha Wagnercc065fb2018-10-15 10:45:44 +01001030
1031 /**
1032 * Read and parse a {@code proc} file in the given format.
1033 *
1034 * <p>The format is a list of integers, where every integer describes a variable in the file. It
1035 * specifies how the variable is syntactically terminated (e.g. {@link Process#PROC_SPACE_TERM},
Misha Wagner566903a2018-10-02 10:50:12 +01001036 * {@link Process#PROC_TAB_TERM}, {@link Process#PROC_ZERO_TERM}, {@link
1037 * Process#PROC_NEWLINE_TERM}).
Misha Wagnercc065fb2018-10-15 10:45:44 +01001038 *
1039 * <p>If the variable should be parsed and returned to the caller, the termination type should
1040 * be binary OR'd with the type of output (e.g. {@link Process#PROC_OUT_STRING}, {@link
1041 * Process#PROC_OUT_LONG}, {@link Process#PROC_OUT_FLOAT}.
1042 *
1043 * <p>If the variable is wrapped in quotation marks it should be binary OR'd with {@link
1044 * Process#PROC_QUOTES}. If the variable is wrapped in parentheses it should be binary OR'd with
1045 * {@link Process#PROC_PARENS}.
1046 *
1047 * <p>If the variable is not formatted as a string and should be cast directly from characters
1048 * to a long, the {@link Process#PROC_CHAR} integer should be binary OR'd.
1049 *
1050 * <p>If the terminating character can be repeated, the {@link Process#PROC_COMBINE} integer
1051 * should be binary OR'd.
1052 *
1053 * @param file the path of the {@code proc} file to read
1054 * @param format the format of the file
1055 * @param outStrings the parsed {@code String}s from the file
1056 * @param outLongs the parsed {@code long}s from the file
1057 * @param outFloats the parsed {@code float}s from the file
1058 * @hide
1059 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 public static final native boolean readProcFile(String file, int[] format,
1061 String[] outStrings, long[] outLongs, float[] outFloats);
Misha Wagnercc065fb2018-10-15 10:45:44 +01001062
Evan Millarc64edde2009-04-18 12:26:32 -07001063 /** @hide */
1064 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
1065 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001067 /** @hide */
1068 public static final native int[] getPidsForCommands(String[] cmds);
1069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 /**
1071 * Gets the total Pss value for a given process, in bytes.
1072 *
1073 * @param pid the process to the Pss for
1074 * @return the total Pss value for the given process in bytes,
1075 * or -1 if the value cannot be determined
1076 * @hide
1077 */
1078 public static final native long getPss(int pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -07001079
Tim Murrayfb076782018-11-27 12:22:22 -08001080 /** @hide */
1081 public static final native long[] getRss(int pid);
1082
Jeff Brown3f9dd282011-07-08 20:02:19 -07001083 /**
1084 * Specifies the outcome of having started a process.
1085 * @hide
1086 */
1087 public static final class ProcessStartResult {
1088 /**
1089 * The PID of the newly started process.
1090 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
1091 */
1092 public int pid;
1093
1094 /**
1095 * True if the process was started with a wrapper attached.
1096 */
1097 public boolean usingWrapper;
1098 }
Colin Cross0769e552014-06-03 13:25:35 -07001099
1100 /**
1101 * Kill all processes in a process group started for the given
1102 * pid.
1103 * @hide
1104 */
1105 public static final native int killProcessGroup(int uid, int pid);
1106
1107 /**
1108 * Remove all process groups. Expected to be called when ActivityManager
1109 * is restarted.
1110 * @hide
1111 */
1112 public static final native void removeAllProcessGroups();
Tim Murrayb89970e2016-07-01 10:42:20 -07001113
1114 /**
1115 * Check to see if a thread belongs to a given process. This may require
1116 * more permissions than apps generally have.
1117 * @return true if this thread belongs to a process
1118 * @hide
1119 */
1120 public static final boolean isThreadInProcess(int tid, int pid) {
Tim Murray9bc12a82016-07-16 11:28:40 -07001121 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
Tim Murrayb89970e2016-07-01 10:42:20 -07001122 try {
1123 if (Os.access("/proc/" + tid + "/task/" + pid, OsConstants.F_OK)) {
1124 return true;
1125 } else {
1126 return false;
1127 }
1128 } catch (Exception e) {
1129 return false;
Tim Murray9bc12a82016-07-16 11:28:40 -07001130 } finally {
1131 StrictMode.setThreadPolicy(oldPolicy);
Tim Murrayb89970e2016-07-01 10:42:20 -07001132 }
Tim Murray9bc12a82016-07-16 11:28:40 -07001133
Tim Murrayb89970e2016-07-01 10:42:20 -07001134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135}