blob: a9508dad154f56979a037210f749061f6e8cf848 [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
Srinath Sridharan1b15d132016-07-19 15:16:11 -070019import android.annotation.TestApi;
Elliott Hughes34385d32014-04-28 11:11:32 -070020import android.system.Os;
Tim Murrayb89970e2016-07-01 10:42:20 -070021import android.system.OsConstants;
Robert Sesekded20982016-08-15 13:59:13 -040022import android.webkit.WebViewZygote;
Nicolas Geoffray81edac42017-09-07 14:13:29 +010023
Andreas Gampeee0cdce2015-06-08 20:47:50 -070024import dalvik.system.VMRuntime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025
26/**
27 * Tools for managing OS processes.
28 */
29public class Process {
30 private static final String LOG_TAG = "Process";
31
Narayan Kamath64cd9072014-05-13 13:35:14 +010032 /**
33 * @hide for internal use only.
34 */
35 public static final String ZYGOTE_SOCKET = "zygote";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
Narayan Kamath64cd9072014-05-13 13:35:14 +010037 /**
38 * @hide for internal use only.
39 */
40 public static final String SECONDARY_ZYGOTE_SOCKET = "zygote_secondary";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
42 /**
Jeff Sharkey1cb2d0d2014-07-30 16:45:01 -070043 * Defines the root UID.
44 * @hide
45 */
46 public static final int ROOT_UID = 0;
47
48 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 * Defines the UID/GID under which system code runs.
50 */
51 public static final int SYSTEM_UID = 1000;
52
53 /**
54 * Defines the UID/GID under which the telephony code runs.
55 */
56 public static final int PHONE_UID = 1001;
57
58 /**
Dianne Hackborn854060af2009-07-09 18:14:31 -070059 * Defines the UID/GID for the user shell.
60 * @hide
61 */
62 public static final int SHELL_UID = 2000;
63
64 /**
Mike Lockwoodd42685d2009-09-03 09:25:22 -040065 * Defines the UID/GID for the log group.
66 * @hide
67 */
68 public static final int LOG_UID = 1007;
69
70 /**
Amith Yamasanid1582142009-07-08 20:04:55 -070071 * Defines the UID/GID for the WIFI supplicant process.
72 * @hide
73 */
74 public static final int WIFI_UID = 1010;
75
76 /**
Glenn Kasten8b7d1b42011-07-13 16:23:22 -070077 * Defines the UID/GID for the mediaserver process.
78 * @hide
79 */
80 public static final int MEDIA_UID = 1013;
81
82 /**
Jeff Sharkey5294a2f2012-04-24 17:07:22 -070083 * Defines the UID/GID for the DRM process.
84 * @hide
85 */
86 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 */
92 public static final int VPN_UID = 1016;
93
94 /**
Janis Danisevskis6ab9bb62017-03-08 10:35:00 -080095 * Defines the UID/GID for keystore.
96 * @hide
97 */
98 public static final int KEYSTORE_UID = 1017;
99
100 /**
Nick Pellycd0e8392010-10-13 17:25:24 -0700101 * Defines the UID/GID for the NFC service process.
102 * @hide
103 */
Nick Pellya5cb9f42011-11-21 14:54:46 -0800104 public static final int NFC_UID = 1027;
Nick Pellycd0e8392010-10-13 17:25:24 -0700105
106 /**
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800107 * Defines the UID/GID for the Bluetooth service process.
108 * @hide
109 */
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
Jeff Sharkeya73e1652017-11-15 19:07:14 -0700172 /** {@hide} */
173 public static final int NOBODY_UID = 9999;
174
Jeff Sharkey07c5c6f2017-03-21 10:09:50 -0600175 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 * Defines the start of a range of UIDs (and GIDs), going from this
177 * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning
178 * to applications.
179 */
180 public static final int FIRST_APPLICATION_UID = 10000;
Jeff Sharkey184a0102013-07-10 16:19:52 -0700181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 /**
183 * Last of application-specific UIDs starting at
184 * {@link #FIRST_APPLICATION_UID}.
185 */
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800186 public static final int LAST_APPLICATION_UID = 19999;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800187
188 /**
189 * First uid used for fully isolated sandboxed processes (with no permissions of their own)
190 * @hide
191 */
192 public static final int FIRST_ISOLATED_UID = 99000;
193
194 /**
195 * Last uid used for fully isolated sandboxed processes (with no permissions of their own)
196 * @hide
197 */
198 public static final int LAST_ISOLATED_UID = 99999;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199
200 /**
Robin Leee66b6892014-04-29 12:36:47 +0100201 * Defines the gid shared by all applications running under the same profile.
202 * @hide
203 */
204 public static final int SHARED_USER_GID = 9997;
205
206 /**
Kenny Roote091f222012-09-11 15:01:26 -0700207 * First gid for applications to share resources. Used when forward-locking
208 * is enabled but all UserHandles need to be able to read the resources.
209 * @hide
210 */
211 public static final int FIRST_SHARED_APPLICATION_GID = 50000;
212
213 /**
214 * Last gid for applications to share resources. Used when forward-locking
215 * is enabled but all UserHandles need to be able to read the resources.
216 * @hide
217 */
218 public static final int LAST_SHARED_APPLICATION_GID = 59999;
219
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700220 /** {@hide} */
221 public static final int FIRST_APPLICATION_CACHE_GID = 20000;
222 /** {@hide} */
223 public static final int LAST_APPLICATION_CACHE_GID = 29999;
224
Kenny Roote091f222012-09-11 15:01:26 -0700225 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 * Standard priority of application threads.
227 * Use with {@link #setThreadPriority(int)} and
228 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
229 * {@link java.lang.Thread} class.
230 */
231 public static final int THREAD_PRIORITY_DEFAULT = 0;
232
233 /*
234 * ***************************************
235 * ** Keep in sync with utils/threads.h **
236 * ***************************************
237 */
238
239 /**
240 * Lowest available thread priority. Only for those who really, really
241 * don't want to run if anything else is happening.
242 * Use with {@link #setThreadPriority(int)} and
243 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
244 * {@link java.lang.Thread} class.
245 */
246 public static final int THREAD_PRIORITY_LOWEST = 19;
247
248 /**
249 * Standard priority background threads. This gives your thread a slightly
250 * lower than normal priority, so that it will have less chance of impacting
251 * the responsiveness of the user interface.
252 * Use with {@link #setThreadPriority(int)} and
253 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
254 * {@link java.lang.Thread} class.
255 */
256 public static final int THREAD_PRIORITY_BACKGROUND = 10;
257
258 /**
259 * Standard priority of threads that are currently running a user interface
260 * that the user is interacting with. Applications can not normally
261 * change to this priority; the system will automatically adjust your
262 * application threads as the user moves through the UI.
263 * Use with {@link #setThreadPriority(int)} and
264 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
265 * {@link java.lang.Thread} class.
266 */
267 public static final int THREAD_PRIORITY_FOREGROUND = -2;
268
269 /**
270 * Standard priority of system display threads, involved in updating
271 * the user interface. Applications can not
272 * normally change to this priority.
273 * Use with {@link #setThreadPriority(int)} and
274 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
275 * {@link java.lang.Thread} class.
276 */
277 public static final int THREAD_PRIORITY_DISPLAY = -4;
278
279 /**
280 * Standard priority of the most important display threads, for compositing
281 * the screen and retrieving input events. Applications can not normally
282 * change to this priority.
283 * Use with {@link #setThreadPriority(int)} and
284 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
285 * {@link java.lang.Thread} class.
286 */
287 public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8;
288
289 /**
Jeff Tinker9765e202018-01-08 15:05:48 -0800290 * Standard priority of video threads. Applications can not normally
291 * change to this priority.
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_VIDEO = -10;
297
298 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 * Standard priority of audio threads. Applications can not normally
300 * change to this priority.
301 * Use with {@link #setThreadPriority(int)} and
302 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
303 * {@link java.lang.Thread} class.
304 */
305 public static final int THREAD_PRIORITY_AUDIO = -16;
306
307 /**
308 * Standard priority of the most important audio threads.
309 * Applications can not normally change to this priority.
310 * Use with {@link #setThreadPriority(int)} and
311 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
312 * {@link java.lang.Thread} class.
313 */
314 public static final int THREAD_PRIORITY_URGENT_AUDIO = -19;
315
316 /**
317 * Minimum increment to make a priority more favorable.
318 */
319 public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
320
321 /**
322 * Minimum increment to make a priority less favorable.
323 */
324 public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
325
San Mehate9d376b2009-04-21 14:06:36 -0700326 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700327 * Default scheduling policy
328 * @hide
329 */
330 public static final int SCHED_OTHER = 0;
331
332 /**
333 * First-In First-Out scheduling policy
334 * @hide
335 */
336 public static final int SCHED_FIFO = 1;
337
338 /**
339 * Round-Robin scheduling policy
340 * @hide
341 */
342 public static final int SCHED_RR = 2;
343
344 /**
345 * Batch scheduling policy
346 * @hide
347 */
348 public static final int SCHED_BATCH = 3;
349
350 /**
351 * Idle scheduling policy
352 * @hide
353 */
354 public static final int SCHED_IDLE = 5;
355
Srinath Sridharane535a582016-06-27 18:13:47 -0700356 /**
357 * Reset scheduler choice on fork.
358 * @hide
359 */
360 public static final int SCHED_RESET_ON_FORK = 0x40000000;
361
Glenn Kastenf1b56442012-03-15 16:33:43 -0700362 // Keep in sync with SP_* constants of enum type SchedPolicy
363 // declared in system/core/include/cutils/sched_policy.h,
364 // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
San Mehate9d376b2009-04-21 14:06:36 -0700365
366 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700367 * Default thread group -
368 * has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
369 * When used with setProcessGroup(), the group of each thread in the process
370 * is conditionally changed based on that thread's current priority, as follows:
371 * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
372 * are moved to foreground thread group. All other threads are left unchanged.
373 * @hide
374 */
375 public static final int THREAD_GROUP_DEFAULT = -1;
376
377 /**
378 * Background thread group - All threads in
San Mehate9d376b2009-04-21 14:06:36 -0700379 * this group are scheduled with a reduced share of the CPU.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700380 * Value is same as constant SP_BACKGROUND of enum SchedPolicy.
381 * FIXME rename to THREAD_GROUP_BACKGROUND.
San Mehate9d376b2009-04-21 14:06:36 -0700382 * @hide
383 */
Glenn Kastenf1b56442012-03-15 16:33:43 -0700384 public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0;
San Mehate9d376b2009-04-21 14:06:36 -0700385
386 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700387 * Foreground thread group - All threads in
388 * this group are scheduled with a normal share of the CPU.
389 * Value is same as constant SP_FOREGROUND of enum SchedPolicy.
390 * Not used at this level.
San Mehate9d376b2009-04-21 14:06:36 -0700391 * @hide
392 **/
Glenn Kastenf1b56442012-03-15 16:33:43 -0700393 private static final int THREAD_GROUP_FOREGROUND = 1;
San Mehate9d376b2009-04-21 14:06:36 -0700394
Glenn Kasten07b04652012-04-23 15:00:43 -0700395 /**
396 * System thread group.
397 * @hide
398 **/
399 public static final int THREAD_GROUP_SYSTEM = 2;
400
401 /**
402 * Application audio thread group.
403 * @hide
404 **/
405 public static final int THREAD_GROUP_AUDIO_APP = 3;
406
407 /**
408 * System audio thread group.
409 * @hide
410 **/
411 public static final int THREAD_GROUP_AUDIO_SYS = 4;
412
Tim Murrayece6a222016-01-14 11:22:57 -0800413 /**
414 * Thread group for top foreground app.
415 * @hide
416 **/
417 public static final int THREAD_GROUP_TOP_APP = 5;
418
Joel Fernandes59776002017-04-17 13:40:16 -0700419 /**
420 * Thread group for RT app.
421 * @hide
422 **/
423 public static final int THREAD_GROUP_RT_APP = 6;
424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 public static final int SIGNAL_QUIT = 3;
426 public static final int SIGNAL_KILL = 9;
427 public static final int SIGNAL_USR1 = 10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428
Dianne Hackbornd98885c2016-03-01 17:13:03 -0800429 private static long sStartElapsedRealtime;
430 private static long sStartUptimeMillis;
431
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100432 /**
Robert Sesek8f8d1872016-03-18 16:52:57 -0400433 * State associated with the zygote process.
434 * @hide
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100435 */
Robert Sesek8f8d1872016-03-18 16:52:57 -0400436 public static final ZygoteProcess zygoteProcess =
437 new ZygoteProcess(ZYGOTE_SOCKET, SECONDARY_ZYGOTE_SOCKET);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438
439 /**
440 * Start a new process.
441 *
442 * <p>If processes are enabled, a new process is created and the
443 * static main() function of a <var>processClass</var> is executed there.
444 * The process will continue running after this function returns.
445 *
446 * <p>If processes are not enabled, a new thread in the caller's
447 * process is created and main() of <var>processClass</var> called there.
448 *
449 * <p>The niceName parameter, if not an empty string, is a custom name to
450 * give to the process instead of using processClass. This allows you to
451 * make easily identifyable processes even if you are using the same base
452 * <var>processClass</var> to start them.
453 *
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000454 * When invokeWith is not null, the process will be started as a fresh app
Tamas Berghammer0ca16fa2016-11-11 16:08:26 +0000455 * and not a zygote fork. Note that this is only allowed for uid 0 or when
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100456 * runtimeFlags contains DEBUG_ENABLE_DEBUGGER.
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000457 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 * @param processClass The class to use as the process's main entry
459 * point.
460 * @param niceName A more readable name to use for the process.
461 * @param uid The user-id under which the process will run.
462 * @param gid The group-id under which the process will run.
463 * @param gids Additional group-ids associated with the process.
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100464 * @param runtimeFlags Additional flags for the runtime.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700465 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalleybd19b9e2013-04-12 14:49:38 -0400466 * @param seInfo null-ok SELinux information for the new process.
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100467 * @param abi non-null the ABI this app should be started with.
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700468 * @param instructionSet null-ok the instruction set to use.
jgu212eacd062014-09-10 06:55:07 -0400469 * @param appDataDir null-ok the data directory of the app.
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000470 * @param invokeWith null-ok the command to invoke with.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 * @param zygoteArgs Additional arguments to supply to the zygote process.
472 *
Jeff Brown3f9dd282011-07-08 20:02:19 -0700473 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 * @throws RuntimeException on fatal start failure
475 *
476 * {@hide}
477 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700478 public static final ProcessStartResult start(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 final String niceName,
480 int uid, int gid, int[] gids,
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100481 int runtimeFlags, int mountExternal,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700482 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500483 String seInfo,
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100484 String abi,
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700485 String instructionSet,
jgu212eacd062014-09-10 06:55:07 -0400486 String appDataDir,
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000487 String invokeWith,
Jeff Brown10e89712011-07-08 18:52:57 -0700488 String[] zygoteArgs) {
Robert Sesek8f8d1872016-03-18 16:52:57 -0400489 return zygoteProcess.start(processClass, niceName, uid, gid, gids,
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100490 runtimeFlags, mountExternal, targetSdkVersion, seInfo,
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000491 abi, instructionSet, appDataDir, invokeWith, zygoteArgs);
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100492 }
493
Robert Sesekded20982016-08-15 13:59:13 -0400494 /** @hide */
495 public static final ProcessStartResult startWebView(final String processClass,
496 final String niceName,
497 int uid, int gid, int[] gids,
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100498 int runtimeFlags, int mountExternal,
Robert Sesekded20982016-08-15 13:59:13 -0400499 int targetSdkVersion,
500 String seInfo,
501 String abi,
502 String instructionSet,
503 String appDataDir,
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000504 String invokeWith,
Robert Sesekded20982016-08-15 13:59:13 -0400505 String[] zygoteArgs) {
506 return WebViewZygote.getProcess().start(processClass, niceName, uid, gid, gids,
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100507 runtimeFlags, mountExternal, targetSdkVersion, seInfo,
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000508 abi, instructionSet, appDataDir, invokeWith, zygoteArgs);
Robert Sesekded20982016-08-15 13:59:13 -0400509 }
510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 /**
512 * Returns elapsed milliseconds of the time this process has run.
513 * @return Returns the number of milliseconds this process has return.
514 */
515 public static final native long getElapsedCpuTime();
Andreas Gampeee0cdce2015-06-08 20:47:50 -0700516
517 /**
Dianne Hackbornd98885c2016-03-01 17:13:03 -0800518 * Return the {@link SystemClock#elapsedRealtime()} at which this process was started.
519 */
520 public static final long getStartElapsedRealtime() {
521 return sStartElapsedRealtime;
522 }
523
524 /**
525 * Return the {@link SystemClock#uptimeMillis()} at which this process was started.
526 */
527 public static final long getStartUptimeMillis() {
528 return sStartUptimeMillis;
529 }
530
531 /** @hide */
532 public static final void setStartTimes(long elapsedRealtime, long uptimeMillis) {
533 sStartElapsedRealtime = elapsedRealtime;
534 sStartUptimeMillis = uptimeMillis;
535 }
536
537 /**
Andreas Gampeee0cdce2015-06-08 20:47:50 -0700538 * Returns true if the current process is a 64-bit runtime.
539 */
540 public static final boolean is64Bit() {
541 return VMRuntime.getRuntime().is64Bit();
542 }
543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 /**
545 * Returns the identifier of this process, which can be used with
546 * {@link #killProcess} and {@link #sendSignal}.
547 */
Jeff Hao406ec152013-07-30 10:13:41 -0700548 public static final int myPid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700549 return Os.getpid();
Jeff Hao406ec152013-07-30 10:13:41 -0700550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551
552 /**
Romain Guy80b12fc2013-05-29 15:54:25 -0700553 * Returns the identifier of this process' parent.
554 * @hide
555 */
Elliott Hughes2a805f92013-07-31 18:25:47 -0700556 public static final int myPpid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700557 return Os.getppid();
Elliott Hughes2a805f92013-07-31 18:25:47 -0700558 }
Romain Guy80b12fc2013-05-29 15:54:25 -0700559
560 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 * Returns the identifier of the calling thread, which be used with
562 * {@link #setThreadPriority(int, int)}.
563 */
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700564 public static final int myTid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700565 return Os.gettid();
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567
568 /**
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700569 * Returns the identifier of this process's uid. This is the kernel uid
570 * that the process is running under, which is the identity of its
571 * app-specific sandbox. It is different from {@link #myUserHandle} in that
572 * a uid identifies a specific app sandbox in a specific user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 */
Jeff Hao406ec152013-07-30 10:13:41 -0700574 public static final int myUid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700575 return Os.getuid();
Jeff Hao406ec152013-07-30 10:13:41 -0700576 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577
578 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700579 * Returns this process's user handle. This is the
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700580 * user the process is running under. It is distinct from
581 * {@link #myUid()} in that a particular user will have multiple
582 * distinct apps running under it each with their own uid.
583 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700584 public static UserHandle myUserHandle() {
585 return UserHandle.of(UserHandle.getUserId(myUid()));
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700586 }
587
588 /**
Makoto Onuki9cc471c2018-01-23 12:33:56 -0800589 * Returns whether the given uid belongs to a system core component or not.
590 * @hide
591 */
592 public static boolean isCoreUid(int uid) {
593 return UserHandle.isCore(uid);
594 }
595
596 /**
Jeff Davidson48aa86b2016-02-10 14:15:42 -0800597 * Returns whether the given uid belongs to an application.
598 * @param uid A kernel uid.
599 * @return Whether the uid corresponds to an application sandbox running in
600 * a specific user.
601 */
602 public static boolean isApplicationUid(int uid) {
603 return UserHandle.isApp(uid);
604 }
605
606 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800607 * Returns whether the current process is in an isolated sandbox.
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800608 */
609 public static final boolean isIsolated() {
Jeff Sharkey9527b222015-06-24 15:24:48 -0700610 return isIsolated(myUid());
611 }
612
613 /** {@hide} */
614 public static final boolean isIsolated(int uid) {
615 uid = UserHandle.getAppId(uid);
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800616 return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID;
617 }
618
619 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 * Returns the UID assigned to a particular user name, or -1 if there is
621 * none. If the given string consists of only numbers, it is converted
622 * directly to a uid.
623 */
624 public static final native int getUidForName(String name);
625
626 /**
627 * Returns the GID assigned to a particular user name, or -1 if there is
628 * none. If the given string consists of only numbers, it is converted
629 * directly to a gid.
630 */
631 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700632
633 /**
634 * Returns a uid for a currently running process.
635 * @param pid the process id
636 * @return the uid of the process, or -1 if the process is not running.
637 * @hide pending API council review
638 */
639 public static final int getUidForPid(int pid) {
640 String[] procStatusLabels = { "Uid:" };
641 long[] procStatusValues = new long[1];
642 procStatusValues[0] = -1;
643 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
644 return (int) procStatusValues[0];
645 }
646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700648 * Returns the parent process id for a currently running process.
649 * @param pid the process id
650 * @return the parent process id of the process, or -1 if the process is not running.
651 * @hide
652 */
653 public static final int getParentPid(int pid) {
654 String[] procStatusLabels = { "PPid:" };
655 long[] procStatusValues = new long[1];
656 procStatusValues[0] = -1;
657 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
658 return (int) procStatusValues[0];
659 }
660
661 /**
Glenn Kasten07b04652012-04-23 15:00:43 -0700662 * Returns the thread group leader id for a currently running thread.
663 * @param tid the thread id
664 * @return the thread group leader id of the thread, or -1 if the thread is not running.
665 * This is same as what getpid(2) would return if called by tid.
666 * @hide
667 */
668 public static final int getThreadGroupLeader(int tid) {
669 String[] procStatusLabels = { "Tgid:" };
670 long[] procStatusValues = new long[1];
671 procStatusValues[0] = -1;
672 Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues);
673 return (int) procStatusValues[0];
674 }
675
676 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 * Set the priority of a thread, based on Linux priorities.
678 *
679 * @param tid The identifier of the thread/process to change.
680 * @param priority A Linux priority level, from -20 for highest scheduling
681 * priority to 19 for lowest scheduling priority.
682 *
683 * @throws IllegalArgumentException Throws IllegalArgumentException if
684 * <var>tid</var> does not exist.
685 * @throws SecurityException Throws SecurityException if your process does
686 * not have permission to modify the given thread, or to use the given
687 * priority.
688 */
689 public static final native void setThreadPriority(int tid, int priority)
690 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700691
692 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700693 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
694 * throw an exception if passed a background-level thread priority. This is only
695 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
696 *
697 * @hide
698 */
699 public static final native void setCanSelfBackground(boolean backgroundOk);
700
701 /**
San Mehate9d376b2009-04-21 14:06:36 -0700702 * Sets the scheduling group for a thread.
703 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700704 * @param tid The identifier of the thread to change.
705 * @param group The target group for this thread from THREAD_GROUP_*.
Joel Fernandes474d3112017-04-04 16:32:15 -0700706 *
San Mehate9d376b2009-04-21 14:06:36 -0700707 * @throws IllegalArgumentException Throws IllegalArgumentException if
708 * <var>tid</var> does not exist.
709 * @throws SecurityException Throws SecurityException if your process does
710 * not have permission to modify the given thread, or to use the given
711 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700712 * If the thread is a thread group leader, that is it's gettid() == getpid(),
713 * then the other threads in the same thread group are _not_ affected.
Tim Murray4074ad02016-07-08 15:45:45 -0700714 *
715 * Does not set cpuset for some historical reason, just calls
716 * libcutils::set_sched_policy().
San Mehate9d376b2009-04-21 14:06:36 -0700717 */
718 public static final native void setThreadGroup(int tid, int group)
719 throws IllegalArgumentException, SecurityException;
Glenn Kastenf1b56442012-03-15 16:33:43 -0700720
San Mehat3e458242009-05-19 14:44:16 -0700721 /**
Joel Fernandes474d3112017-04-04 16:32:15 -0700722 * Sets the scheduling group and the corresponding cpuset group
723 * @hide
724 * @param tid The identifier of the thread to change.
725 * @param group The target group for this thread from THREAD_GROUP_*.
726 *
727 * @throws IllegalArgumentException Throws IllegalArgumentException if
728 * <var>tid</var> does not exist.
729 * @throws SecurityException Throws SecurityException if your process does
730 * not have permission to modify the given thread, or to use the given
731 * priority.
732 */
733 public static final native void setThreadGroupAndCpuset(int tid, int group)
734 throws IllegalArgumentException, SecurityException;
735
736 /**
San Mehat3e458242009-05-19 14:44:16 -0700737 * Sets the scheduling group for a process and all child threads
738 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700739 * @param pid The identifier of the process to change.
740 * @param group The target group for this process from THREAD_GROUP_*.
San Mehat3e458242009-05-19 14:44:16 -0700741 *
742 * @throws IllegalArgumentException Throws IllegalArgumentException if
743 * <var>tid</var> does not exist.
744 * @throws SecurityException Throws SecurityException if your process does
745 * not have permission to modify the given thread, or to use the given
746 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700747 *
748 * group == THREAD_GROUP_DEFAULT means to move all non-background priority
749 * threads to the foreground scheduling group, but to leave background
750 * priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all
751 * threads, regardless of priority, to the background scheduling group.
752 * group == THREAD_GROUP_FOREGROUND is not allowed.
Tim Murray4074ad02016-07-08 15:45:45 -0700753 *
754 * Always sets cpusets.
San Mehat3e458242009-05-19 14:44:16 -0700755 */
756 public static final native void setProcessGroup(int pid, int group)
757 throws IllegalArgumentException, SecurityException;
Jeff Sharkey9e57c412013-01-17 14:12:41 -0800758
759 /**
760 * Return the scheduling group of requested process.
761 *
762 * @hide
763 */
764 public static final native int getProcessGroup(int pid)
765 throws IllegalArgumentException, SecurityException;
766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 /**
Martijn Coenencd4bdf32016-03-03 17:30:52 +0100768 * On some devices, the foreground process may have one or more CPU
769 * cores exclusively reserved for it. This method can be used to
770 * retrieve which cores that are (if any), so the calling process
771 * can then use sched_setaffinity() to lock a thread to these cores.
772 * Note that the calling process must currently be running in the
773 * foreground for this method to return any cores.
774 *
775 * The CPU core(s) exclusively reserved for the foreground process will
776 * stay reserved for as long as the process stays in the foreground.
777 *
778 * As soon as a process leaves the foreground, those CPU cores will
779 * no longer be reserved for it, and will most likely be reserved for
780 * the new foreground process. It's not necessary to change the affinity
781 * of your process when it leaves the foreground (if you had previously
782 * set it to use a reserved core); the OS will automatically take care
783 * of resetting the affinity at that point.
784 *
785 * @return an array of integers, indicating the CPU cores exclusively
786 * reserved for this process. The array will have length zero if no
787 * CPU cores are exclusively reserved for this process at this point
788 * in time.
789 */
790 public static final native int[] getExclusiveCores();
791
792 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 * Set the priority of the calling thread, based on Linux priorities. See
794 * {@link #setThreadPriority(int, int)} for more information.
795 *
796 * @param priority A Linux priority level, from -20 for highest scheduling
797 * priority to 19 for lowest scheduling priority.
798 *
799 * @throws IllegalArgumentException Throws IllegalArgumentException if
800 * <var>tid</var> does not exist.
801 * @throws SecurityException Throws SecurityException if your process does
802 * not have permission to modify the given thread, or to use the given
803 * priority.
804 *
805 * @see #setThreadPriority(int, int)
806 */
807 public static final native void setThreadPriority(int priority)
808 throws IllegalArgumentException, SecurityException;
809
810 /**
811 * Return the current priority of a thread, based on Linux priorities.
812 *
Alessio Balsini68ae3012017-06-07 11:41:33 +0200813 * @param tid The identifier of the thread/process. If tid equals zero, the priority of the
814 * calling process/thread will be returned.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 *
816 * @return Returns the current priority, as a Linux priority level,
817 * from -20 for highest scheduling priority to 19 for lowest scheduling
818 * priority.
819 *
820 * @throws IllegalArgumentException Throws IllegalArgumentException if
821 * <var>tid</var> does not exist.
822 */
823 public static final native int getThreadPriority(int tid)
824 throws IllegalArgumentException;
825
826 /**
Srinath Sridharan1b15d132016-07-19 15:16:11 -0700827 * Return the current scheduling policy of a thread, based on Linux.
828 *
829 * @param tid The identifier of the thread/process to get the scheduling policy.
830 *
831 * @throws IllegalArgumentException Throws IllegalArgumentException if
832 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
833 * @throws SecurityException Throws SecurityException if your process does
834 * not have permission to modify the given thread, or to use the given
835 * scheduling policy or priority.
836 *
837 * {@hide}
838 */
839
840 @TestApi
841 public static final native int getThreadScheduler(int tid)
842 throws IllegalArgumentException;
843
844 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700845 * Set the scheduling policy and priority of a thread, based on Linux.
846 *
847 * @param tid The identifier of the thread/process to change.
848 * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
849 * @param priority A Linux priority level in a range appropriate for the given policy.
850 *
851 * @throws IllegalArgumentException Throws IllegalArgumentException if
852 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
853 * @throws SecurityException Throws SecurityException if your process does
854 * not have permission to modify the given thread, or to use the given
855 * scheduling policy or priority.
856 *
857 * {@hide}
858 */
Srinath Sridharan1b15d132016-07-19 15:16:11 -0700859
Glenn Kasten6793ac92011-07-13 12:44:12 -0700860 public static final native void setThreadScheduler(int tid, int policy, int priority)
861 throws IllegalArgumentException;
862
863 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 * Determine whether the current environment supports multiple processes.
865 *
866 * @return Returns true if the system can run in multiple processes, else
867 * false if everything is running in a single process.
Jeff Brown10e89712011-07-08 18:52:57 -0700868 *
869 * @deprecated This method always returns true. Do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 */
Jeff Brown10e89712011-07-08 18:52:57 -0700871 @Deprecated
872 public static final boolean supportsProcesses() {
873 return true;
874 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875
876 /**
Rom Lemarchand5534ba92013-07-12 16:15:36 -0700877 * Adjust the swappiness level for a process.
878 *
879 * @param pid The process identifier to set.
880 * @param is_increased Whether swappiness should be increased or default.
881 *
882 * @return Returns true if the underlying system supports this
883 * feature, else false.
884 *
885 * {@hide}
886 */
887 public static final native boolean setSwappiness(int pid, boolean is_increased);
888
889 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 * Change this process's argv[0] parameter. This can be useful to show
891 * more descriptive information in things like the 'ps' command.
892 *
893 * @param text The new name of this process.
894 *
895 * {@hide}
896 */
897 public static final native void setArgV0(String text);
898
899 /**
900 * Kill the process with the given PID.
901 * Note that, though this API allows us to request to
902 * kill any process based on its PID, the kernel will
903 * still impose standard restrictions on which PIDs you
904 * are actually able to kill. Typically this means only
905 * the process running the caller's packages/application
906 * and any additional processes created by that app; packages
907 * sharing a common UID will also be able to kill each
908 * other's processes.
909 */
910 public static final void killProcess(int pid) {
911 sendSignal(pid, SIGNAL_KILL);
912 }
913
914 /** @hide */
915 public static final native int setUid(int uid);
916
917 /** @hide */
918 public static final native int setGid(int uid);
919
920 /**
921 * Send a signal to the given process.
922 *
923 * @param pid The pid of the target process.
924 * @param signal The signal to send.
925 */
926 public static final native void sendSignal(int pid, int signal);
927
Dianne Hackborn906497c2010-05-10 15:57:38 -0700928 /**
929 * @hide
930 * Private impl for avoiding a log message... DO NOT USE without doing
931 * your own log, or the Android Illuminati will find you some night and
932 * beat you up.
933 */
934 public static final void killProcessQuiet(int pid) {
935 sendSignalQuiet(pid, SIGNAL_KILL);
936 }
937
938 /**
939 * @hide
940 * Private impl for avoiding a log message... DO NOT USE without doing
941 * your own log, or the Android Illuminati will find you some night and
942 * beat you up.
943 */
944 public static final native void sendSignalQuiet(int pid, int signal);
945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 /** @hide */
Marco Nelissen0bca96b2009-07-17 12:59:25 -0700947 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948
949 /** @hide */
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700950 public static final native long getTotalMemory();
951
952 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 public static final native void readProcLines(String path,
954 String[] reqFields, long[] outSizes);
955
956 /** @hide */
957 public static final native int[] getPids(String path, int[] lastArray);
958
959 /** @hide */
960 public static final int PROC_TERM_MASK = 0xff;
961 /** @hide */
962 public static final int PROC_ZERO_TERM = 0;
963 /** @hide */
964 public static final int PROC_SPACE_TERM = (int)' ';
965 /** @hide */
Evan Millarc64edde2009-04-18 12:26:32 -0700966 public static final int PROC_TAB_TERM = (int)'\t';
967 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 public static final int PROC_COMBINE = 0x100;
969 /** @hide */
970 public static final int PROC_PARENS = 0x200;
971 /** @hide */
Dianne Hackborn13ac0412013-06-25 19:34:49 -0700972 public static final int PROC_QUOTES = 0x400;
973 /** @hide */
Dianne Hackborn4de5a3a2016-06-20 15:20:15 -0700974 public static final int PROC_CHAR = 0x800;
975 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 public static final int PROC_OUT_STRING = 0x1000;
977 /** @hide */
978 public static final int PROC_OUT_LONG = 0x2000;
979 /** @hide */
980 public static final int PROC_OUT_FLOAT = 0x4000;
981
982 /** @hide */
983 public static final native boolean readProcFile(String file, int[] format,
984 String[] outStrings, long[] outLongs, float[] outFloats);
Evan Millarc64edde2009-04-18 12:26:32 -0700985
986 /** @hide */
987 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
988 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700990 /** @hide */
991 public static final native int[] getPidsForCommands(String[] cmds);
992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 /**
994 * Gets the total Pss value for a given process, in bytes.
995 *
996 * @param pid the process to the Pss for
997 * @return the total Pss value for the given process in bytes,
998 * or -1 if the value cannot be determined
999 * @hide
1000 */
1001 public static final native long getPss(int pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -07001002
1003 /**
1004 * Specifies the outcome of having started a process.
1005 * @hide
1006 */
1007 public static final class ProcessStartResult {
1008 /**
1009 * The PID of the newly started process.
1010 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
1011 */
1012 public int pid;
1013
1014 /**
1015 * True if the process was started with a wrapper attached.
1016 */
1017 public boolean usingWrapper;
1018 }
Colin Cross0769e552014-06-03 13:25:35 -07001019
1020 /**
1021 * Kill all processes in a process group started for the given
1022 * pid.
1023 * @hide
1024 */
1025 public static final native int killProcessGroup(int uid, int pid);
1026
1027 /**
1028 * Remove all process groups. Expected to be called when ActivityManager
1029 * is restarted.
1030 * @hide
1031 */
1032 public static final native void removeAllProcessGroups();
Tim Murrayb89970e2016-07-01 10:42:20 -07001033
1034 /**
1035 * Check to see if a thread belongs to a given process. This may require
1036 * more permissions than apps generally have.
1037 * @return true if this thread belongs to a process
1038 * @hide
1039 */
1040 public static final boolean isThreadInProcess(int tid, int pid) {
Tim Murray9bc12a82016-07-16 11:28:40 -07001041 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
Tim Murrayb89970e2016-07-01 10:42:20 -07001042 try {
1043 if (Os.access("/proc/" + tid + "/task/" + pid, OsConstants.F_OK)) {
1044 return true;
1045 } else {
1046 return false;
1047 }
1048 } catch (Exception e) {
1049 return false;
Tim Murray9bc12a82016-07-16 11:28:40 -07001050 } finally {
1051 StrictMode.setThreadPolicy(oldPolicy);
Tim Murrayb89970e2016-07-01 10:42:20 -07001052 }
Tim Murray9bc12a82016-07-16 11:28:40 -07001053
Tim Murrayb89970e2016-07-01 10:42:20 -07001054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055}