blob: 21c1263c47d1d8eb02b25c287c3c28e1eeae4fab [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 /**
Robert Sesekded20982016-08-15 13:59:13 -0400143 * Defines the UID/GID for the WebView zygote process.
144 * @hide
145 */
Robert Sesekbceb3822018-01-22 12:26:39 -0500146 public static final int WEBVIEW_ZYGOTE_UID = 1053;
Robert Sesekded20982016-08-15 13:59:13 -0400147
148 /**
Jeff Sharkey07c5c6f2017-03-21 10:09:50 -0600149 * Defines the UID used for resource tracking for OTA updates.
150 * @hide
151 */
152 public static final int OTA_UPDATE_UID = 1061;
153
Yi Jin4bab3a12018-01-10 16:50:59 -0800154 /**
155 * Defines the UID used for incidentd.
156 * @hide
157 */
158 public static final int INCIDENTD_UID = 1067;
159
Ruchi Kandoi6149b0f2018-01-03 16:14:57 -0800160 /**
161 * Defines the UID/GID for the Secure Element service process.
162 * @hide
163 */
164 public static final int SE_UID = 1068;
165
Jeff Sharkeya73e1652017-11-15 19:07:14 -0700166 /** {@hide} */
167 public static final int NOBODY_UID = 9999;
168
Jeff Sharkey07c5c6f2017-03-21 10:09:50 -0600169 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 * Defines the start of a range of UIDs (and GIDs), going from this
171 * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning
172 * to applications.
173 */
174 public static final int FIRST_APPLICATION_UID = 10000;
Jeff Sharkey184a0102013-07-10 16:19:52 -0700175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 /**
177 * Last of application-specific UIDs starting at
178 * {@link #FIRST_APPLICATION_UID}.
179 */
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800180 public static final int LAST_APPLICATION_UID = 19999;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800181
182 /**
183 * First uid used for fully isolated sandboxed processes (with no permissions of their own)
184 * @hide
185 */
186 public static final int FIRST_ISOLATED_UID = 99000;
187
188 /**
189 * Last uid used for fully isolated sandboxed processes (with no permissions of their own)
190 * @hide
191 */
192 public static final int LAST_ISOLATED_UID = 99999;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193
194 /**
Robin Leee66b6892014-04-29 12:36:47 +0100195 * Defines the gid shared by all applications running under the same profile.
196 * @hide
197 */
198 public static final int SHARED_USER_GID = 9997;
199
200 /**
Kenny Roote091f222012-09-11 15:01:26 -0700201 * First gid for applications to share resources. Used when forward-locking
202 * is enabled but all UserHandles need to be able to read the resources.
203 * @hide
204 */
205 public static final int FIRST_SHARED_APPLICATION_GID = 50000;
206
207 /**
208 * Last gid for applications to share resources. Used when forward-locking
209 * is enabled but all UserHandles need to be able to read the resources.
210 * @hide
211 */
212 public static final int LAST_SHARED_APPLICATION_GID = 59999;
213
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700214 /** {@hide} */
215 public static final int FIRST_APPLICATION_CACHE_GID = 20000;
216 /** {@hide} */
217 public static final int LAST_APPLICATION_CACHE_GID = 29999;
218
Kenny Roote091f222012-09-11 15:01:26 -0700219 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 * Standard priority of application threads.
221 * Use with {@link #setThreadPriority(int)} and
222 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
223 * {@link java.lang.Thread} class.
224 */
225 public static final int THREAD_PRIORITY_DEFAULT = 0;
226
227 /*
228 * ***************************************
229 * ** Keep in sync with utils/threads.h **
230 * ***************************************
231 */
232
233 /**
234 * Lowest available thread priority. Only for those who really, really
235 * don't want to run if anything else is happening.
236 * Use with {@link #setThreadPriority(int)} and
237 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
238 * {@link java.lang.Thread} class.
239 */
240 public static final int THREAD_PRIORITY_LOWEST = 19;
241
242 /**
243 * Standard priority background threads. This gives your thread a slightly
244 * lower than normal priority, so that it will have less chance of impacting
245 * the responsiveness of the user interface.
246 * Use with {@link #setThreadPriority(int)} and
247 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
248 * {@link java.lang.Thread} class.
249 */
250 public static final int THREAD_PRIORITY_BACKGROUND = 10;
251
252 /**
253 * Standard priority of threads that are currently running a user interface
254 * that the user is interacting with. Applications can not normally
255 * change to this priority; the system will automatically adjust your
256 * application threads as the user moves through the UI.
257 * Use with {@link #setThreadPriority(int)} and
258 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
259 * {@link java.lang.Thread} class.
260 */
261 public static final int THREAD_PRIORITY_FOREGROUND = -2;
262
263 /**
264 * Standard priority of system display threads, involved in updating
265 * the user interface. Applications can not
266 * normally change to this priority.
267 * Use with {@link #setThreadPriority(int)} and
268 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
269 * {@link java.lang.Thread} class.
270 */
271 public static final int THREAD_PRIORITY_DISPLAY = -4;
272
273 /**
274 * Standard priority of the most important display threads, for compositing
275 * the screen and retrieving input events. Applications can not normally
276 * change to this priority.
277 * Use with {@link #setThreadPriority(int)} and
278 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
279 * {@link java.lang.Thread} class.
280 */
281 public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8;
282
283 /**
Jeff Tinker9765e202018-01-08 15:05:48 -0800284 * Standard priority of video threads. Applications can not normally
285 * change to this priority.
286 * Use with {@link #setThreadPriority(int)} and
287 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
288 * {@link java.lang.Thread} class.
289 */
290 public static final int THREAD_PRIORITY_VIDEO = -10;
291
292 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 * Standard priority of audio threads. Applications can not normally
294 * change to this priority.
295 * Use with {@link #setThreadPriority(int)} and
296 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
297 * {@link java.lang.Thread} class.
298 */
299 public static final int THREAD_PRIORITY_AUDIO = -16;
300
301 /**
302 * Standard priority of the most important audio threads.
303 * Applications can not normally change to this priority.
304 * Use with {@link #setThreadPriority(int)} and
305 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
306 * {@link java.lang.Thread} class.
307 */
308 public static final int THREAD_PRIORITY_URGENT_AUDIO = -19;
309
310 /**
311 * Minimum increment to make a priority more favorable.
312 */
313 public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
314
315 /**
316 * Minimum increment to make a priority less favorable.
317 */
318 public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
319
San Mehate9d376b2009-04-21 14:06:36 -0700320 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700321 * Default scheduling policy
322 * @hide
323 */
324 public static final int SCHED_OTHER = 0;
325
326 /**
327 * First-In First-Out scheduling policy
328 * @hide
329 */
330 public static final int SCHED_FIFO = 1;
331
332 /**
333 * Round-Robin scheduling policy
334 * @hide
335 */
336 public static final int SCHED_RR = 2;
337
338 /**
339 * Batch scheduling policy
340 * @hide
341 */
342 public static final int SCHED_BATCH = 3;
343
344 /**
345 * Idle scheduling policy
346 * @hide
347 */
348 public static final int SCHED_IDLE = 5;
349
Srinath Sridharane535a582016-06-27 18:13:47 -0700350 /**
351 * Reset scheduler choice on fork.
352 * @hide
353 */
354 public static final int SCHED_RESET_ON_FORK = 0x40000000;
355
Glenn Kastenf1b56442012-03-15 16:33:43 -0700356 // Keep in sync with SP_* constants of enum type SchedPolicy
357 // declared in system/core/include/cutils/sched_policy.h,
358 // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
San Mehate9d376b2009-04-21 14:06:36 -0700359
360 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700361 * Default thread group -
362 * has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
363 * When used with setProcessGroup(), the group of each thread in the process
364 * is conditionally changed based on that thread's current priority, as follows:
365 * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
366 * are moved to foreground thread group. All other threads are left unchanged.
367 * @hide
368 */
369 public static final int THREAD_GROUP_DEFAULT = -1;
370
371 /**
372 * Background thread group - All threads in
San Mehate9d376b2009-04-21 14:06:36 -0700373 * this group are scheduled with a reduced share of the CPU.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700374 * Value is same as constant SP_BACKGROUND of enum SchedPolicy.
375 * FIXME rename to THREAD_GROUP_BACKGROUND.
San Mehate9d376b2009-04-21 14:06:36 -0700376 * @hide
377 */
Glenn Kastenf1b56442012-03-15 16:33:43 -0700378 public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0;
San Mehate9d376b2009-04-21 14:06:36 -0700379
380 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700381 * Foreground thread group - All threads in
382 * this group are scheduled with a normal share of the CPU.
383 * Value is same as constant SP_FOREGROUND of enum SchedPolicy.
384 * Not used at this level.
San Mehate9d376b2009-04-21 14:06:36 -0700385 * @hide
386 **/
Glenn Kastenf1b56442012-03-15 16:33:43 -0700387 private static final int THREAD_GROUP_FOREGROUND = 1;
San Mehate9d376b2009-04-21 14:06:36 -0700388
Glenn Kasten07b04652012-04-23 15:00:43 -0700389 /**
390 * System thread group.
391 * @hide
392 **/
393 public static final int THREAD_GROUP_SYSTEM = 2;
394
395 /**
396 * Application audio thread group.
397 * @hide
398 **/
399 public static final int THREAD_GROUP_AUDIO_APP = 3;
400
401 /**
402 * System audio thread group.
403 * @hide
404 **/
405 public static final int THREAD_GROUP_AUDIO_SYS = 4;
406
Tim Murrayece6a222016-01-14 11:22:57 -0800407 /**
408 * Thread group for top foreground app.
409 * @hide
410 **/
411 public static final int THREAD_GROUP_TOP_APP = 5;
412
Joel Fernandes59776002017-04-17 13:40:16 -0700413 /**
414 * Thread group for RT app.
415 * @hide
416 **/
417 public static final int THREAD_GROUP_RT_APP = 6;
418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 public static final int SIGNAL_QUIT = 3;
420 public static final int SIGNAL_KILL = 9;
421 public static final int SIGNAL_USR1 = 10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422
Dianne Hackbornd98885c2016-03-01 17:13:03 -0800423 private static long sStartElapsedRealtime;
424 private static long sStartUptimeMillis;
425
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100426 /**
Robert Sesek8f8d1872016-03-18 16:52:57 -0400427 * State associated with the zygote process.
428 * @hide
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100429 */
Robert Sesek8f8d1872016-03-18 16:52:57 -0400430 public static final ZygoteProcess zygoteProcess =
431 new ZygoteProcess(ZYGOTE_SOCKET, SECONDARY_ZYGOTE_SOCKET);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432
433 /**
434 * Start a new process.
435 *
436 * <p>If processes are enabled, a new process is created and the
437 * static main() function of a <var>processClass</var> is executed there.
438 * The process will continue running after this function returns.
439 *
440 * <p>If processes are not enabled, a new thread in the caller's
441 * process is created and main() of <var>processClass</var> called there.
442 *
443 * <p>The niceName parameter, if not an empty string, is a custom name to
444 * give to the process instead of using processClass. This allows you to
445 * make easily identifyable processes even if you are using the same base
446 * <var>processClass</var> to start them.
447 *
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000448 * When invokeWith is not null, the process will be started as a fresh app
Tamas Berghammer0ca16fa2016-11-11 16:08:26 +0000449 * and not a zygote fork. Note that this is only allowed for uid 0 or when
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100450 * runtimeFlags contains DEBUG_ENABLE_DEBUGGER.
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000451 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 * @param processClass The class to use as the process's main entry
453 * point.
454 * @param niceName A more readable name to use for the process.
455 * @param uid The user-id under which the process will run.
456 * @param gid The group-id under which the process will run.
457 * @param gids Additional group-ids associated with the process.
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100458 * @param runtimeFlags Additional flags for the runtime.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700459 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalleybd19b9e2013-04-12 14:49:38 -0400460 * @param seInfo null-ok SELinux information for the new process.
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100461 * @param abi non-null the ABI this app should be started with.
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700462 * @param instructionSet null-ok the instruction set to use.
jgu212eacd062014-09-10 06:55:07 -0400463 * @param appDataDir null-ok the data directory of the app.
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000464 * @param invokeWith null-ok the command to invoke with.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 * @param zygoteArgs Additional arguments to supply to the zygote process.
466 *
Jeff Brown3f9dd282011-07-08 20:02:19 -0700467 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 * @throws RuntimeException on fatal start failure
469 *
470 * {@hide}
471 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700472 public static final ProcessStartResult start(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 final String niceName,
474 int uid, int gid, int[] gids,
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100475 int runtimeFlags, int mountExternal,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700476 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500477 String seInfo,
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100478 String abi,
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700479 String instructionSet,
jgu212eacd062014-09-10 06:55:07 -0400480 String appDataDir,
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000481 String invokeWith,
Jeff Brown10e89712011-07-08 18:52:57 -0700482 String[] zygoteArgs) {
Robert Sesek8f8d1872016-03-18 16:52:57 -0400483 return zygoteProcess.start(processClass, niceName, uid, gid, gids,
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100484 runtimeFlags, mountExternal, targetSdkVersion, seInfo,
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000485 abi, instructionSet, appDataDir, invokeWith, zygoteArgs);
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100486 }
487
Robert Sesekded20982016-08-15 13:59:13 -0400488 /** @hide */
489 public static final ProcessStartResult startWebView(final String processClass,
490 final String niceName,
491 int uid, int gid, int[] gids,
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100492 int runtimeFlags, int mountExternal,
Robert Sesekded20982016-08-15 13:59:13 -0400493 int targetSdkVersion,
494 String seInfo,
495 String abi,
496 String instructionSet,
497 String appDataDir,
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000498 String invokeWith,
Robert Sesekded20982016-08-15 13:59:13 -0400499 String[] zygoteArgs) {
500 return WebViewZygote.getProcess().start(processClass, niceName, uid, gid, gids,
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100501 runtimeFlags, mountExternal, targetSdkVersion, seInfo,
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000502 abi, instructionSet, appDataDir, invokeWith, zygoteArgs);
Robert Sesekded20982016-08-15 13:59:13 -0400503 }
504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 /**
506 * Returns elapsed milliseconds of the time this process has run.
507 * @return Returns the number of milliseconds this process has return.
508 */
509 public static final native long getElapsedCpuTime();
Andreas Gampeee0cdce2015-06-08 20:47:50 -0700510
511 /**
Dianne Hackbornd98885c2016-03-01 17:13:03 -0800512 * Return the {@link SystemClock#elapsedRealtime()} at which this process was started.
513 */
514 public static final long getStartElapsedRealtime() {
515 return sStartElapsedRealtime;
516 }
517
518 /**
519 * Return the {@link SystemClock#uptimeMillis()} at which this process was started.
520 */
521 public static final long getStartUptimeMillis() {
522 return sStartUptimeMillis;
523 }
524
525 /** @hide */
526 public static final void setStartTimes(long elapsedRealtime, long uptimeMillis) {
527 sStartElapsedRealtime = elapsedRealtime;
528 sStartUptimeMillis = uptimeMillis;
529 }
530
531 /**
Andreas Gampeee0cdce2015-06-08 20:47:50 -0700532 * Returns true if the current process is a 64-bit runtime.
533 */
534 public static final boolean is64Bit() {
535 return VMRuntime.getRuntime().is64Bit();
536 }
537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 /**
539 * Returns the identifier of this process, which can be used with
540 * {@link #killProcess} and {@link #sendSignal}.
541 */
Jeff Hao406ec152013-07-30 10:13:41 -0700542 public static final int myPid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700543 return Os.getpid();
Jeff Hao406ec152013-07-30 10:13:41 -0700544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545
546 /**
Romain Guy80b12fc2013-05-29 15:54:25 -0700547 * Returns the identifier of this process' parent.
548 * @hide
549 */
Elliott Hughes2a805f92013-07-31 18:25:47 -0700550 public static final int myPpid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700551 return Os.getppid();
Elliott Hughes2a805f92013-07-31 18:25:47 -0700552 }
Romain Guy80b12fc2013-05-29 15:54:25 -0700553
554 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 * Returns the identifier of the calling thread, which be used with
556 * {@link #setThreadPriority(int, int)}.
557 */
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700558 public static final int myTid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700559 return Os.gettid();
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561
562 /**
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700563 * Returns the identifier of this process's uid. This is the kernel uid
564 * that the process is running under, which is the identity of its
565 * app-specific sandbox. It is different from {@link #myUserHandle} in that
566 * a uid identifies a specific app sandbox in a specific user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 */
Jeff Hao406ec152013-07-30 10:13:41 -0700568 public static final int myUid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700569 return Os.getuid();
Jeff Hao406ec152013-07-30 10:13:41 -0700570 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571
572 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700573 * Returns this process's user handle. This is the
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700574 * user the process is running under. It is distinct from
575 * {@link #myUid()} in that a particular user will have multiple
576 * distinct apps running under it each with their own uid.
577 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700578 public static UserHandle myUserHandle() {
579 return UserHandle.of(UserHandle.getUserId(myUid()));
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700580 }
581
582 /**
Makoto Onuki9cc471c2018-01-23 12:33:56 -0800583 * Returns whether the given uid belongs to a system core component or not.
584 * @hide
585 */
586 public static boolean isCoreUid(int uid) {
587 return UserHandle.isCore(uid);
588 }
589
590 /**
Jeff Davidson48aa86b2016-02-10 14:15:42 -0800591 * Returns whether the given uid belongs to an application.
592 * @param uid A kernel uid.
593 * @return Whether the uid corresponds to an application sandbox running in
594 * a specific user.
595 */
596 public static boolean isApplicationUid(int uid) {
597 return UserHandle.isApp(uid);
598 }
599
600 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800601 * Returns whether the current process is in an isolated sandbox.
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800602 */
603 public static final boolean isIsolated() {
Jeff Sharkey9527b222015-06-24 15:24:48 -0700604 return isIsolated(myUid());
605 }
606
607 /** {@hide} */
608 public static final boolean isIsolated(int uid) {
609 uid = UserHandle.getAppId(uid);
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800610 return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID;
611 }
612
613 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 * Returns the UID assigned to a particular user name, or -1 if there is
615 * none. If the given string consists of only numbers, it is converted
616 * directly to a uid.
617 */
618 public static final native int getUidForName(String name);
619
620 /**
621 * Returns the GID assigned to a particular user name, or -1 if there is
622 * none. If the given string consists of only numbers, it is converted
623 * directly to a gid.
624 */
625 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700626
627 /**
628 * Returns a uid for a currently running process.
629 * @param pid the process id
630 * @return the uid of the process, or -1 if the process is not running.
631 * @hide pending API council review
632 */
633 public static final int getUidForPid(int pid) {
634 String[] procStatusLabels = { "Uid:" };
635 long[] procStatusValues = new long[1];
636 procStatusValues[0] = -1;
637 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
638 return (int) procStatusValues[0];
639 }
640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700642 * Returns the parent process id for a currently running process.
643 * @param pid the process id
644 * @return the parent process id of the process, or -1 if the process is not running.
645 * @hide
646 */
647 public static final int getParentPid(int pid) {
648 String[] procStatusLabels = { "PPid:" };
649 long[] procStatusValues = new long[1];
650 procStatusValues[0] = -1;
651 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
652 return (int) procStatusValues[0];
653 }
654
655 /**
Glenn Kasten07b04652012-04-23 15:00:43 -0700656 * Returns the thread group leader id for a currently running thread.
657 * @param tid the thread id
658 * @return the thread group leader id of the thread, or -1 if the thread is not running.
659 * This is same as what getpid(2) would return if called by tid.
660 * @hide
661 */
662 public static final int getThreadGroupLeader(int tid) {
663 String[] procStatusLabels = { "Tgid:" };
664 long[] procStatusValues = new long[1];
665 procStatusValues[0] = -1;
666 Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues);
667 return (int) procStatusValues[0];
668 }
669
670 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 * Set the priority of a thread, based on Linux priorities.
672 *
673 * @param tid The identifier of the thread/process to change.
674 * @param priority A Linux priority level, from -20 for highest scheduling
675 * priority to 19 for lowest scheduling priority.
676 *
677 * @throws IllegalArgumentException Throws IllegalArgumentException if
678 * <var>tid</var> does not exist.
679 * @throws SecurityException Throws SecurityException if your process does
680 * not have permission to modify the given thread, or to use the given
681 * priority.
682 */
683 public static final native void setThreadPriority(int tid, int priority)
684 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700685
686 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700687 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
688 * throw an exception if passed a background-level thread priority. This is only
689 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
690 *
691 * @hide
692 */
693 public static final native void setCanSelfBackground(boolean backgroundOk);
694
695 /**
San Mehate9d376b2009-04-21 14:06:36 -0700696 * Sets the scheduling group for a thread.
697 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700698 * @param tid The identifier of the thread to change.
699 * @param group The target group for this thread from THREAD_GROUP_*.
Joel Fernandes474d3112017-04-04 16:32:15 -0700700 *
San Mehate9d376b2009-04-21 14:06:36 -0700701 * @throws IllegalArgumentException Throws IllegalArgumentException if
702 * <var>tid</var> does not exist.
703 * @throws SecurityException Throws SecurityException if your process does
704 * not have permission to modify the given thread, or to use the given
705 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700706 * If the thread is a thread group leader, that is it's gettid() == getpid(),
707 * then the other threads in the same thread group are _not_ affected.
Tim Murray4074ad02016-07-08 15:45:45 -0700708 *
709 * Does not set cpuset for some historical reason, just calls
710 * libcutils::set_sched_policy().
San Mehate9d376b2009-04-21 14:06:36 -0700711 */
712 public static final native void setThreadGroup(int tid, int group)
713 throws IllegalArgumentException, SecurityException;
Glenn Kastenf1b56442012-03-15 16:33:43 -0700714
San Mehat3e458242009-05-19 14:44:16 -0700715 /**
Joel Fernandes474d3112017-04-04 16:32:15 -0700716 * Sets the scheduling group and the corresponding cpuset group
717 * @hide
718 * @param tid The identifier of the thread to change.
719 * @param group The target group for this thread from THREAD_GROUP_*.
720 *
721 * @throws IllegalArgumentException Throws IllegalArgumentException if
722 * <var>tid</var> does not exist.
723 * @throws SecurityException Throws SecurityException if your process does
724 * not have permission to modify the given thread, or to use the given
725 * priority.
726 */
727 public static final native void setThreadGroupAndCpuset(int tid, int group)
728 throws IllegalArgumentException, SecurityException;
729
730 /**
San Mehat3e458242009-05-19 14:44:16 -0700731 * Sets the scheduling group for a process and all child threads
732 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700733 * @param pid The identifier of the process to change.
734 * @param group The target group for this process from THREAD_GROUP_*.
San Mehat3e458242009-05-19 14:44:16 -0700735 *
736 * @throws IllegalArgumentException Throws IllegalArgumentException if
737 * <var>tid</var> does not exist.
738 * @throws SecurityException Throws SecurityException if your process does
739 * not have permission to modify the given thread, or to use the given
740 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700741 *
742 * group == THREAD_GROUP_DEFAULT means to move all non-background priority
743 * threads to the foreground scheduling group, but to leave background
744 * priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all
745 * threads, regardless of priority, to the background scheduling group.
746 * group == THREAD_GROUP_FOREGROUND is not allowed.
Tim Murray4074ad02016-07-08 15:45:45 -0700747 *
748 * Always sets cpusets.
San Mehat3e458242009-05-19 14:44:16 -0700749 */
750 public static final native void setProcessGroup(int pid, int group)
751 throws IllegalArgumentException, SecurityException;
Jeff Sharkey9e57c412013-01-17 14:12:41 -0800752
753 /**
754 * Return the scheduling group of requested process.
755 *
756 * @hide
757 */
758 public static final native int getProcessGroup(int pid)
759 throws IllegalArgumentException, SecurityException;
760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 /**
Martijn Coenencd4bdf32016-03-03 17:30:52 +0100762 * On some devices, the foreground process may have one or more CPU
763 * cores exclusively reserved for it. This method can be used to
764 * retrieve which cores that are (if any), so the calling process
765 * can then use sched_setaffinity() to lock a thread to these cores.
766 * Note that the calling process must currently be running in the
767 * foreground for this method to return any cores.
768 *
769 * The CPU core(s) exclusively reserved for the foreground process will
770 * stay reserved for as long as the process stays in the foreground.
771 *
772 * As soon as a process leaves the foreground, those CPU cores will
773 * no longer be reserved for it, and will most likely be reserved for
774 * the new foreground process. It's not necessary to change the affinity
775 * of your process when it leaves the foreground (if you had previously
776 * set it to use a reserved core); the OS will automatically take care
777 * of resetting the affinity at that point.
778 *
779 * @return an array of integers, indicating the CPU cores exclusively
780 * reserved for this process. The array will have length zero if no
781 * CPU cores are exclusively reserved for this process at this point
782 * in time.
783 */
784 public static final native int[] getExclusiveCores();
785
786 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 * Set the priority of the calling thread, based on Linux priorities. See
788 * {@link #setThreadPriority(int, int)} for more information.
789 *
790 * @param priority A Linux priority level, from -20 for highest scheduling
791 * priority to 19 for lowest scheduling priority.
792 *
793 * @throws IllegalArgumentException Throws IllegalArgumentException if
794 * <var>tid</var> does not exist.
795 * @throws SecurityException Throws SecurityException if your process does
796 * not have permission to modify the given thread, or to use the given
797 * priority.
798 *
799 * @see #setThreadPriority(int, int)
800 */
801 public static final native void setThreadPriority(int priority)
802 throws IllegalArgumentException, SecurityException;
803
804 /**
805 * Return the current priority of a thread, based on Linux priorities.
806 *
Alessio Balsini68ae3012017-06-07 11:41:33 +0200807 * @param tid The identifier of the thread/process. If tid equals zero, the priority of the
808 * calling process/thread will be returned.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 *
810 * @return Returns the current priority, as a Linux priority level,
811 * from -20 for highest scheduling priority to 19 for lowest scheduling
812 * priority.
813 *
814 * @throws IllegalArgumentException Throws IllegalArgumentException if
815 * <var>tid</var> does not exist.
816 */
817 public static final native int getThreadPriority(int tid)
818 throws IllegalArgumentException;
819
820 /**
Srinath Sridharan1b15d132016-07-19 15:16:11 -0700821 * Return the current scheduling policy of a thread, based on Linux.
822 *
823 * @param tid The identifier of the thread/process to get the scheduling policy.
824 *
825 * @throws IllegalArgumentException Throws IllegalArgumentException if
826 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
827 * @throws SecurityException Throws SecurityException if your process does
828 * not have permission to modify the given thread, or to use the given
829 * scheduling policy or priority.
830 *
831 * {@hide}
832 */
833
834 @TestApi
835 public static final native int getThreadScheduler(int tid)
836 throws IllegalArgumentException;
837
838 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700839 * Set the scheduling policy and priority of a thread, based on Linux.
840 *
841 * @param tid The identifier of the thread/process to change.
842 * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
843 * @param priority A Linux priority level in a range appropriate for the given policy.
844 *
845 * @throws IllegalArgumentException Throws IllegalArgumentException if
846 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
847 * @throws SecurityException Throws SecurityException if your process does
848 * not have permission to modify the given thread, or to use the given
849 * scheduling policy or priority.
850 *
851 * {@hide}
852 */
Srinath Sridharan1b15d132016-07-19 15:16:11 -0700853
Glenn Kasten6793ac92011-07-13 12:44:12 -0700854 public static final native void setThreadScheduler(int tid, int policy, int priority)
855 throws IllegalArgumentException;
856
857 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 * Determine whether the current environment supports multiple processes.
859 *
860 * @return Returns true if the system can run in multiple processes, else
861 * false if everything is running in a single process.
Jeff Brown10e89712011-07-08 18:52:57 -0700862 *
863 * @deprecated This method always returns true. Do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 */
Jeff Brown10e89712011-07-08 18:52:57 -0700865 @Deprecated
866 public static final boolean supportsProcesses() {
867 return true;
868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869
870 /**
Rom Lemarchand5534ba92013-07-12 16:15:36 -0700871 * Adjust the swappiness level for a process.
872 *
873 * @param pid The process identifier to set.
874 * @param is_increased Whether swappiness should be increased or default.
875 *
876 * @return Returns true if the underlying system supports this
877 * feature, else false.
878 *
879 * {@hide}
880 */
881 public static final native boolean setSwappiness(int pid, boolean is_increased);
882
883 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 * Change this process's argv[0] parameter. This can be useful to show
885 * more descriptive information in things like the 'ps' command.
886 *
887 * @param text The new name of this process.
888 *
889 * {@hide}
890 */
891 public static final native void setArgV0(String text);
892
893 /**
894 * Kill the process with the given PID.
895 * Note that, though this API allows us to request to
896 * kill any process based on its PID, the kernel will
897 * still impose standard restrictions on which PIDs you
898 * are actually able to kill. Typically this means only
899 * the process running the caller's packages/application
900 * and any additional processes created by that app; packages
901 * sharing a common UID will also be able to kill each
902 * other's processes.
903 */
904 public static final void killProcess(int pid) {
905 sendSignal(pid, SIGNAL_KILL);
906 }
907
908 /** @hide */
909 public static final native int setUid(int uid);
910
911 /** @hide */
912 public static final native int setGid(int uid);
913
914 /**
915 * Send a signal to the given process.
916 *
917 * @param pid The pid of the target process.
918 * @param signal The signal to send.
919 */
920 public static final native void sendSignal(int pid, int signal);
921
Dianne Hackborn906497c2010-05-10 15:57:38 -0700922 /**
923 * @hide
924 * Private impl for avoiding a log message... DO NOT USE without doing
925 * your own log, or the Android Illuminati will find you some night and
926 * beat you up.
927 */
928 public static final void killProcessQuiet(int pid) {
929 sendSignalQuiet(pid, SIGNAL_KILL);
930 }
931
932 /**
933 * @hide
934 * Private impl for avoiding a log message... DO NOT USE without doing
935 * your own log, or the Android Illuminati will find you some night and
936 * beat you up.
937 */
938 public static final native void sendSignalQuiet(int pid, int signal);
939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 /** @hide */
Marco Nelissen0bca96b2009-07-17 12:59:25 -0700941 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942
943 /** @hide */
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700944 public static final native long getTotalMemory();
945
946 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 public static final native void readProcLines(String path,
948 String[] reqFields, long[] outSizes);
949
950 /** @hide */
951 public static final native int[] getPids(String path, int[] lastArray);
952
953 /** @hide */
954 public static final int PROC_TERM_MASK = 0xff;
955 /** @hide */
956 public static final int PROC_ZERO_TERM = 0;
957 /** @hide */
958 public static final int PROC_SPACE_TERM = (int)' ';
959 /** @hide */
Evan Millarc64edde2009-04-18 12:26:32 -0700960 public static final int PROC_TAB_TERM = (int)'\t';
961 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 public static final int PROC_COMBINE = 0x100;
963 /** @hide */
964 public static final int PROC_PARENS = 0x200;
965 /** @hide */
Dianne Hackborn13ac0412013-06-25 19:34:49 -0700966 public static final int PROC_QUOTES = 0x400;
967 /** @hide */
Dianne Hackborn4de5a3a2016-06-20 15:20:15 -0700968 public static final int PROC_CHAR = 0x800;
969 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 public static final int PROC_OUT_STRING = 0x1000;
971 /** @hide */
972 public static final int PROC_OUT_LONG = 0x2000;
973 /** @hide */
974 public static final int PROC_OUT_FLOAT = 0x4000;
975
976 /** @hide */
977 public static final native boolean readProcFile(String file, int[] format,
978 String[] outStrings, long[] outLongs, float[] outFloats);
Evan Millarc64edde2009-04-18 12:26:32 -0700979
980 /** @hide */
981 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
982 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700984 /** @hide */
985 public static final native int[] getPidsForCommands(String[] cmds);
986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 /**
988 * Gets the total Pss value for a given process, in bytes.
989 *
990 * @param pid the process to the Pss for
991 * @return the total Pss value for the given process in bytes,
992 * or -1 if the value cannot be determined
993 * @hide
994 */
995 public static final native long getPss(int pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -0700996
997 /**
998 * Specifies the outcome of having started a process.
999 * @hide
1000 */
1001 public static final class ProcessStartResult {
1002 /**
1003 * The PID of the newly started process.
1004 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
1005 */
1006 public int pid;
1007
1008 /**
1009 * True if the process was started with a wrapper attached.
1010 */
1011 public boolean usingWrapper;
1012 }
Colin Cross0769e552014-06-03 13:25:35 -07001013
1014 /**
1015 * Kill all processes in a process group started for the given
1016 * pid.
1017 * @hide
1018 */
1019 public static final native int killProcessGroup(int uid, int pid);
1020
1021 /**
1022 * Remove all process groups. Expected to be called when ActivityManager
1023 * is restarted.
1024 * @hide
1025 */
1026 public static final native void removeAllProcessGroups();
Tim Murrayb89970e2016-07-01 10:42:20 -07001027
1028 /**
1029 * Check to see if a thread belongs to a given process. This may require
1030 * more permissions than apps generally have.
1031 * @return true if this thread belongs to a process
1032 * @hide
1033 */
1034 public static final boolean isThreadInProcess(int tid, int pid) {
Tim Murray9bc12a82016-07-16 11:28:40 -07001035 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
Tim Murrayb89970e2016-07-01 10:42:20 -07001036 try {
1037 if (Os.access("/proc/" + tid + "/task/" + pid, OsConstants.F_OK)) {
1038 return true;
1039 } else {
1040 return false;
1041 }
1042 } catch (Exception e) {
1043 return false;
Tim Murray9bc12a82016-07-16 11:28:40 -07001044 } finally {
1045 StrictMode.setThreadPolicy(oldPolicy);
Tim Murrayb89970e2016-07-01 10:42:20 -07001046 }
Tim Murray9bc12a82016-07-16 11:28:40 -07001047
Tim Murrayb89970e2016-07-01 10:42:20 -07001048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049}