blob: d6688e3c00f7853d37ce254c9abb0bf2295aa6d7 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.util.Log;
Robert Sesekded20982016-08-15 13:59:13 -040023import android.webkit.WebViewZygote;
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 /**
Nick Pellycd0e8392010-10-13 17:25:24 -070095 * Defines the UID/GID for the NFC service process.
96 * @hide
97 */
Nick Pellya5cb9f42011-11-21 14:54:46 -080098 public static final int NFC_UID = 1027;
Nick Pellycd0e8392010-10-13 17:25:24 -070099
100 /**
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800101 * Defines the UID/GID for the Bluetooth service process.
102 * @hide
103 */
104 public static final int BLUETOOTH_UID = 1002;
105
106 /**
Mike Lockwooddcaa10c2010-12-16 12:50:44 -0800107 * Defines the GID for the group that allows write access to the internal media storage.
108 * @hide
109 */
110 public static final int MEDIA_RW_GID = 1023;
111
112 /**
Jeff Sharkey184a0102013-07-10 16:19:52 -0700113 * Access to installed package details
114 * @hide
115 */
116 public static final int PACKAGE_INFO_GID = 1032;
117
118 /**
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100119 * Defines the UID/GID for the shared RELRO file updater process.
120 * @hide
121 */
122 public static final int SHARED_RELRO_UID = 1037;
123
124 /**
Andy Hunged0ea402015-10-30 14:11:46 -0700125 * Defines the UID/GID for the audioserver process.
126 * @hide
127 */
128 public static final int AUDIOSERVER_UID = 1041;
129
130 /**
Chien-Yu Chen75cade02016-01-11 10:56:21 -0800131 * Defines the UID/GID for the cameraserver process
132 * @hide
133 */
Chien-Yu Chenb8b4e852016-01-29 12:02:28 -0800134 public static final int CAMERASERVER_UID = 1047;
Chien-Yu Chen75cade02016-01-11 10:56:21 -0800135
136 /**
Robert Sesekded20982016-08-15 13:59:13 -0400137 * Defines the UID/GID for the WebView zygote process.
138 * @hide
139 */
140 public static final int WEBVIEW_ZYGOTE_UID = 1051;
141
142 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 * Defines the start of a range of UIDs (and GIDs), going from this
144 * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning
145 * to applications.
146 */
147 public static final int FIRST_APPLICATION_UID = 10000;
Jeff Sharkey184a0102013-07-10 16:19:52 -0700148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 /**
150 * Last of application-specific UIDs starting at
151 * {@link #FIRST_APPLICATION_UID}.
152 */
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800153 public static final int LAST_APPLICATION_UID = 19999;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800154
155 /**
156 * First uid used for fully isolated sandboxed processes (with no permissions of their own)
157 * @hide
158 */
159 public static final int FIRST_ISOLATED_UID = 99000;
160
161 /**
162 * Last uid used for fully isolated sandboxed processes (with no permissions of their own)
163 * @hide
164 */
165 public static final int LAST_ISOLATED_UID = 99999;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166
167 /**
Robin Leee66b6892014-04-29 12:36:47 +0100168 * Defines the gid shared by all applications running under the same profile.
169 * @hide
170 */
171 public static final int SHARED_USER_GID = 9997;
172
173 /**
Kenny Roote091f222012-09-11 15:01:26 -0700174 * First gid for applications to share resources. Used when forward-locking
175 * is enabled but all UserHandles need to be able to read the resources.
176 * @hide
177 */
178 public static final int FIRST_SHARED_APPLICATION_GID = 50000;
179
180 /**
181 * Last gid for applications to share resources. Used when forward-locking
182 * is enabled but all UserHandles need to be able to read the resources.
183 * @hide
184 */
185 public static final int LAST_SHARED_APPLICATION_GID = 59999;
186
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700187 /** {@hide} */
188 public static final int FIRST_APPLICATION_CACHE_GID = 20000;
189 /** {@hide} */
190 public static final int LAST_APPLICATION_CACHE_GID = 29999;
191
Kenny Roote091f222012-09-11 15:01:26 -0700192 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * Standard priority of application threads.
194 * Use with {@link #setThreadPriority(int)} and
195 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
196 * {@link java.lang.Thread} class.
197 */
198 public static final int THREAD_PRIORITY_DEFAULT = 0;
199
200 /*
201 * ***************************************
202 * ** Keep in sync with utils/threads.h **
203 * ***************************************
204 */
205
206 /**
207 * Lowest available thread priority. Only for those who really, really
208 * don't want to run if anything else is happening.
209 * Use with {@link #setThreadPriority(int)} and
210 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
211 * {@link java.lang.Thread} class.
212 */
213 public static final int THREAD_PRIORITY_LOWEST = 19;
214
215 /**
216 * Standard priority background threads. This gives your thread a slightly
217 * lower than normal priority, so that it will have less chance of impacting
218 * the responsiveness of the user interface.
219 * Use with {@link #setThreadPriority(int)} and
220 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
221 * {@link java.lang.Thread} class.
222 */
223 public static final int THREAD_PRIORITY_BACKGROUND = 10;
224
225 /**
226 * Standard priority of threads that are currently running a user interface
227 * that the user is interacting with. Applications can not normally
228 * change to this priority; the system will automatically adjust your
229 * application threads as the user moves through the UI.
230 * Use with {@link #setThreadPriority(int)} and
231 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
232 * {@link java.lang.Thread} class.
233 */
234 public static final int THREAD_PRIORITY_FOREGROUND = -2;
235
236 /**
237 * Standard priority of system display threads, involved in updating
238 * the user interface. Applications can not
239 * normally change to this priority.
240 * Use with {@link #setThreadPriority(int)} and
241 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
242 * {@link java.lang.Thread} class.
243 */
244 public static final int THREAD_PRIORITY_DISPLAY = -4;
245
246 /**
247 * Standard priority of the most important display threads, for compositing
248 * the screen and retrieving input events. Applications can not normally
249 * change to this priority.
250 * Use with {@link #setThreadPriority(int)} and
251 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
252 * {@link java.lang.Thread} class.
253 */
254 public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8;
255
256 /**
257 * Standard priority of audio threads. Applications can not normally
258 * change to this priority.
259 * Use with {@link #setThreadPriority(int)} and
260 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
261 * {@link java.lang.Thread} class.
262 */
263 public static final int THREAD_PRIORITY_AUDIO = -16;
264
265 /**
266 * Standard priority of the most important audio threads.
267 * Applications can not normally change to this priority.
268 * Use with {@link #setThreadPriority(int)} and
269 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
270 * {@link java.lang.Thread} class.
271 */
272 public static final int THREAD_PRIORITY_URGENT_AUDIO = -19;
273
274 /**
275 * Minimum increment to make a priority more favorable.
276 */
277 public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
278
279 /**
280 * Minimum increment to make a priority less favorable.
281 */
282 public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
283
San Mehate9d376b2009-04-21 14:06:36 -0700284 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700285 * Default scheduling policy
286 * @hide
287 */
288 public static final int SCHED_OTHER = 0;
289
290 /**
291 * First-In First-Out scheduling policy
292 * @hide
293 */
294 public static final int SCHED_FIFO = 1;
295
296 /**
297 * Round-Robin scheduling policy
298 * @hide
299 */
300 public static final int SCHED_RR = 2;
301
302 /**
303 * Batch scheduling policy
304 * @hide
305 */
306 public static final int SCHED_BATCH = 3;
307
308 /**
309 * Idle scheduling policy
310 * @hide
311 */
312 public static final int SCHED_IDLE = 5;
313
Srinath Sridharane535a582016-06-27 18:13:47 -0700314 /**
315 * Reset scheduler choice on fork.
316 * @hide
317 */
318 public static final int SCHED_RESET_ON_FORK = 0x40000000;
319
Glenn Kastenf1b56442012-03-15 16:33:43 -0700320 // Keep in sync with SP_* constants of enum type SchedPolicy
321 // declared in system/core/include/cutils/sched_policy.h,
322 // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
San Mehate9d376b2009-04-21 14:06:36 -0700323
324 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700325 * Default thread group -
326 * has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
327 * When used with setProcessGroup(), the group of each thread in the process
328 * is conditionally changed based on that thread's current priority, as follows:
329 * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
330 * are moved to foreground thread group. All other threads are left unchanged.
331 * @hide
332 */
333 public static final int THREAD_GROUP_DEFAULT = -1;
334
335 /**
336 * Background thread group - All threads in
San Mehate9d376b2009-04-21 14:06:36 -0700337 * this group are scheduled with a reduced share of the CPU.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700338 * Value is same as constant SP_BACKGROUND of enum SchedPolicy.
339 * FIXME rename to THREAD_GROUP_BACKGROUND.
San Mehate9d376b2009-04-21 14:06:36 -0700340 * @hide
341 */
Glenn Kastenf1b56442012-03-15 16:33:43 -0700342 public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0;
San Mehate9d376b2009-04-21 14:06:36 -0700343
344 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700345 * Foreground thread group - All threads in
346 * this group are scheduled with a normal share of the CPU.
347 * Value is same as constant SP_FOREGROUND of enum SchedPolicy.
348 * Not used at this level.
San Mehate9d376b2009-04-21 14:06:36 -0700349 * @hide
350 **/
Glenn Kastenf1b56442012-03-15 16:33:43 -0700351 private static final int THREAD_GROUP_FOREGROUND = 1;
San Mehate9d376b2009-04-21 14:06:36 -0700352
Glenn Kasten07b04652012-04-23 15:00:43 -0700353 /**
354 * System thread group.
355 * @hide
356 **/
357 public static final int THREAD_GROUP_SYSTEM = 2;
358
359 /**
360 * Application audio thread group.
361 * @hide
362 **/
363 public static final int THREAD_GROUP_AUDIO_APP = 3;
364
365 /**
366 * System audio thread group.
367 * @hide
368 **/
369 public static final int THREAD_GROUP_AUDIO_SYS = 4;
370
Tim Murrayece6a222016-01-14 11:22:57 -0800371 /**
372 * Thread group for top foreground app.
373 * @hide
374 **/
375 public static final int THREAD_GROUP_TOP_APP = 5;
376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 public static final int SIGNAL_QUIT = 3;
378 public static final int SIGNAL_KILL = 9;
379 public static final int SIGNAL_USR1 = 10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380
Dianne Hackbornd98885c2016-03-01 17:13:03 -0800381 private static long sStartElapsedRealtime;
382 private static long sStartUptimeMillis;
383
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100384 /**
Robert Sesek8f8d1872016-03-18 16:52:57 -0400385 * State associated with the zygote process.
386 * @hide
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100387 */
Robert Sesek8f8d1872016-03-18 16:52:57 -0400388 public static final ZygoteProcess zygoteProcess =
389 new ZygoteProcess(ZYGOTE_SOCKET, SECONDARY_ZYGOTE_SOCKET);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390
391 /**
392 * Start a new process.
393 *
394 * <p>If processes are enabled, a new process is created and the
395 * static main() function of a <var>processClass</var> is executed there.
396 * The process will continue running after this function returns.
397 *
398 * <p>If processes are not enabled, a new thread in the caller's
399 * process is created and main() of <var>processClass</var> called there.
400 *
401 * <p>The niceName parameter, if not an empty string, is a custom name to
402 * give to the process instead of using processClass. This allows you to
403 * make easily identifyable processes even if you are using the same base
404 * <var>processClass</var> to start them.
405 *
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000406 * When invokeWith is not null, the process will be started as a fresh app
Tamas Berghammer0ca16fa2016-11-11 16:08:26 +0000407 * and not a zygote fork. Note that this is only allowed for uid 0 or when
408 * debugFlags contains DEBUG_ENABLE_DEBUGGER.
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000409 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 * @param processClass The class to use as the process's main entry
411 * point.
412 * @param niceName A more readable name to use for the process.
413 * @param uid The user-id under which the process will run.
414 * @param gid The group-id under which the process will run.
415 * @param gids Additional group-ids associated with the process.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700416 * @param debugFlags Additional flags.
417 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalleybd19b9e2013-04-12 14:49:38 -0400418 * @param seInfo null-ok SELinux information for the new process.
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100419 * @param abi non-null the ABI this app should be started with.
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700420 * @param instructionSet null-ok the instruction set to use.
jgu212eacd062014-09-10 06:55:07 -0400421 * @param appDataDir null-ok the data directory of the app.
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000422 * @param invokeWith null-ok the command to invoke with.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 * @param zygoteArgs Additional arguments to supply to the zygote process.
424 *
Jeff Brown3f9dd282011-07-08 20:02:19 -0700425 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 * @throws RuntimeException on fatal start failure
427 *
428 * {@hide}
429 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700430 public static final ProcessStartResult start(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 final String niceName,
432 int uid, int gid, int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700433 int debugFlags, int mountExternal,
434 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500435 String seInfo,
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100436 String abi,
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700437 String instructionSet,
jgu212eacd062014-09-10 06:55:07 -0400438 String appDataDir,
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000439 String invokeWith,
Jeff Brown10e89712011-07-08 18:52:57 -0700440 String[] zygoteArgs) {
Robert Sesek8f8d1872016-03-18 16:52:57 -0400441 return zygoteProcess.start(processClass, niceName, uid, gid, gids,
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100442 debugFlags, mountExternal, targetSdkVersion, seInfo,
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000443 abi, instructionSet, appDataDir, invokeWith, zygoteArgs);
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100444 }
445
Robert Sesekded20982016-08-15 13:59:13 -0400446 /** @hide */
447 public static final ProcessStartResult startWebView(final String processClass,
448 final String niceName,
449 int uid, int gid, int[] gids,
450 int debugFlags, int mountExternal,
451 int targetSdkVersion,
452 String seInfo,
453 String abi,
454 String instructionSet,
455 String appDataDir,
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000456 String invokeWith,
Robert Sesekded20982016-08-15 13:59:13 -0400457 String[] zygoteArgs) {
458 return WebViewZygote.getProcess().start(processClass, niceName, uid, gid, gids,
459 debugFlags, mountExternal, targetSdkVersion, seInfo,
Tamas Berghammerb8f7c352016-11-11 16:08:26 +0000460 abi, instructionSet, appDataDir, invokeWith, zygoteArgs);
Robert Sesekded20982016-08-15 13:59:13 -0400461 }
462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 /**
464 * Returns elapsed milliseconds of the time this process has run.
465 * @return Returns the number of milliseconds this process has return.
466 */
467 public static final native long getElapsedCpuTime();
Andreas Gampeee0cdce2015-06-08 20:47:50 -0700468
469 /**
Dianne Hackbornd98885c2016-03-01 17:13:03 -0800470 * Return the {@link SystemClock#elapsedRealtime()} at which this process was started.
471 */
472 public static final long getStartElapsedRealtime() {
473 return sStartElapsedRealtime;
474 }
475
476 /**
477 * Return the {@link SystemClock#uptimeMillis()} at which this process was started.
478 */
479 public static final long getStartUptimeMillis() {
480 return sStartUptimeMillis;
481 }
482
483 /** @hide */
484 public static final void setStartTimes(long elapsedRealtime, long uptimeMillis) {
485 sStartElapsedRealtime = elapsedRealtime;
486 sStartUptimeMillis = uptimeMillis;
487 }
488
489 /**
Andreas Gampeee0cdce2015-06-08 20:47:50 -0700490 * Returns true if the current process is a 64-bit runtime.
491 */
492 public static final boolean is64Bit() {
493 return VMRuntime.getRuntime().is64Bit();
494 }
495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 /**
497 * Returns the identifier of this process, which can be used with
498 * {@link #killProcess} and {@link #sendSignal}.
499 */
Jeff Hao406ec152013-07-30 10:13:41 -0700500 public static final int myPid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700501 return Os.getpid();
Jeff Hao406ec152013-07-30 10:13:41 -0700502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503
504 /**
Romain Guy80b12fc2013-05-29 15:54:25 -0700505 * Returns the identifier of this process' parent.
506 * @hide
507 */
Elliott Hughes2a805f92013-07-31 18:25:47 -0700508 public static final int myPpid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700509 return Os.getppid();
Elliott Hughes2a805f92013-07-31 18:25:47 -0700510 }
Romain Guy80b12fc2013-05-29 15:54:25 -0700511
512 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 * Returns the identifier of the calling thread, which be used with
514 * {@link #setThreadPriority(int, int)}.
515 */
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700516 public static final int myTid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700517 return Os.gettid();
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519
520 /**
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700521 * Returns the identifier of this process's uid. This is the kernel uid
522 * that the process is running under, which is the identity of its
523 * app-specific sandbox. It is different from {@link #myUserHandle} in that
524 * a uid identifies a specific app sandbox in a specific user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 */
Jeff Hao406ec152013-07-30 10:13:41 -0700526 public static final int myUid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700527 return Os.getuid();
Jeff Hao406ec152013-07-30 10:13:41 -0700528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529
530 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700531 * Returns this process's user handle. This is the
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700532 * user the process is running under. It is distinct from
533 * {@link #myUid()} in that a particular user will have multiple
534 * distinct apps running under it each with their own uid.
535 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700536 public static UserHandle myUserHandle() {
537 return UserHandle.of(UserHandle.getUserId(myUid()));
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700538 }
539
540 /**
Jeff Davidson48aa86b2016-02-10 14:15:42 -0800541 * Returns whether the given uid belongs to an application.
542 * @param uid A kernel uid.
543 * @return Whether the uid corresponds to an application sandbox running in
544 * a specific user.
545 */
546 public static boolean isApplicationUid(int uid) {
547 return UserHandle.isApp(uid);
548 }
549
550 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800551 * Returns whether the current process is in an isolated sandbox.
552 * @hide
553 */
554 public static final boolean isIsolated() {
Jeff Sharkey9527b222015-06-24 15:24:48 -0700555 return isIsolated(myUid());
556 }
557
558 /** {@hide} */
559 public static final boolean isIsolated(int uid) {
560 uid = UserHandle.getAppId(uid);
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800561 return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID;
562 }
563
564 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 * Returns the UID assigned to a particular user name, or -1 if there is
566 * none. If the given string consists of only numbers, it is converted
567 * directly to a uid.
568 */
569 public static final native int getUidForName(String name);
570
571 /**
572 * Returns the GID assigned to a particular user name, or -1 if there is
573 * none. If the given string consists of only numbers, it is converted
574 * directly to a gid.
575 */
576 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700577
578 /**
579 * Returns a uid for a currently running process.
580 * @param pid the process id
581 * @return the uid of the process, or -1 if the process is not running.
582 * @hide pending API council review
583 */
584 public static final int getUidForPid(int pid) {
585 String[] procStatusLabels = { "Uid:" };
586 long[] procStatusValues = new long[1];
587 procStatusValues[0] = -1;
588 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
589 return (int) procStatusValues[0];
590 }
591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700593 * Returns the parent process id for a currently running process.
594 * @param pid the process id
595 * @return the parent process id of the process, or -1 if the process is not running.
596 * @hide
597 */
598 public static final int getParentPid(int pid) {
599 String[] procStatusLabels = { "PPid:" };
600 long[] procStatusValues = new long[1];
601 procStatusValues[0] = -1;
602 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
603 return (int) procStatusValues[0];
604 }
605
606 /**
Glenn Kasten07b04652012-04-23 15:00:43 -0700607 * Returns the thread group leader id for a currently running thread.
608 * @param tid the thread id
609 * @return the thread group leader id of the thread, or -1 if the thread is not running.
610 * This is same as what getpid(2) would return if called by tid.
611 * @hide
612 */
613 public static final int getThreadGroupLeader(int tid) {
614 String[] procStatusLabels = { "Tgid:" };
615 long[] procStatusValues = new long[1];
616 procStatusValues[0] = -1;
617 Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues);
618 return (int) procStatusValues[0];
619 }
620
621 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 * Set the priority of a thread, based on Linux priorities.
623 *
624 * @param tid The identifier of the thread/process to change.
625 * @param priority A Linux priority level, from -20 for highest scheduling
626 * priority to 19 for lowest scheduling priority.
627 *
628 * @throws IllegalArgumentException Throws IllegalArgumentException if
629 * <var>tid</var> does not exist.
630 * @throws SecurityException Throws SecurityException if your process does
631 * not have permission to modify the given thread, or to use the given
632 * priority.
633 */
634 public static final native void setThreadPriority(int tid, int priority)
635 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700636
637 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700638 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
639 * throw an exception if passed a background-level thread priority. This is only
640 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
641 *
642 * @hide
643 */
644 public static final native void setCanSelfBackground(boolean backgroundOk);
645
646 /**
San Mehate9d376b2009-04-21 14:06:36 -0700647 * Sets the scheduling group for a thread.
648 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700649 * @param tid The identifier of the thread to change.
650 * @param group The target group for this thread from THREAD_GROUP_*.
San Mehate9d376b2009-04-21 14:06:36 -0700651 *
652 * @throws IllegalArgumentException Throws IllegalArgumentException if
653 * <var>tid</var> does not exist.
654 * @throws SecurityException Throws SecurityException if your process does
655 * not have permission to modify the given thread, or to use the given
656 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700657 * If the thread is a thread group leader, that is it's gettid() == getpid(),
658 * then the other threads in the same thread group are _not_ affected.
Tim Murray4074ad02016-07-08 15:45:45 -0700659 *
660 * Does not set cpuset for some historical reason, just calls
661 * libcutils::set_sched_policy().
San Mehate9d376b2009-04-21 14:06:36 -0700662 */
663 public static final native void setThreadGroup(int tid, int group)
664 throws IllegalArgumentException, SecurityException;
Glenn Kastenf1b56442012-03-15 16:33:43 -0700665
San Mehat3e458242009-05-19 14:44:16 -0700666 /**
667 * Sets the scheduling group for a process and all child threads
668 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700669 * @param pid The identifier of the process to change.
670 * @param group The target group for this process from THREAD_GROUP_*.
San Mehat3e458242009-05-19 14:44:16 -0700671 *
672 * @throws IllegalArgumentException Throws IllegalArgumentException if
673 * <var>tid</var> does not exist.
674 * @throws SecurityException Throws SecurityException if your process does
675 * not have permission to modify the given thread, or to use the given
676 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700677 *
678 * group == THREAD_GROUP_DEFAULT means to move all non-background priority
679 * threads to the foreground scheduling group, but to leave background
680 * priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all
681 * threads, regardless of priority, to the background scheduling group.
682 * group == THREAD_GROUP_FOREGROUND is not allowed.
Tim Murray4074ad02016-07-08 15:45:45 -0700683 *
684 * Always sets cpusets.
San Mehat3e458242009-05-19 14:44:16 -0700685 */
686 public static final native void setProcessGroup(int pid, int group)
687 throws IllegalArgumentException, SecurityException;
Jeff Sharkey9e57c412013-01-17 14:12:41 -0800688
689 /**
690 * Return the scheduling group of requested process.
691 *
692 * @hide
693 */
694 public static final native int getProcessGroup(int pid)
695 throws IllegalArgumentException, SecurityException;
696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 /**
Martijn Coenencd4bdf32016-03-03 17:30:52 +0100698 * On some devices, the foreground process may have one or more CPU
699 * cores exclusively reserved for it. This method can be used to
700 * retrieve which cores that are (if any), so the calling process
701 * can then use sched_setaffinity() to lock a thread to these cores.
702 * Note that the calling process must currently be running in the
703 * foreground for this method to return any cores.
704 *
705 * The CPU core(s) exclusively reserved for the foreground process will
706 * stay reserved for as long as the process stays in the foreground.
707 *
708 * As soon as a process leaves the foreground, those CPU cores will
709 * no longer be reserved for it, and will most likely be reserved for
710 * the new foreground process. It's not necessary to change the affinity
711 * of your process when it leaves the foreground (if you had previously
712 * set it to use a reserved core); the OS will automatically take care
713 * of resetting the affinity at that point.
714 *
715 * @return an array of integers, indicating the CPU cores exclusively
716 * reserved for this process. The array will have length zero if no
717 * CPU cores are exclusively reserved for this process at this point
718 * in time.
719 */
720 public static final native int[] getExclusiveCores();
721
722 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 * Set the priority of the calling thread, based on Linux priorities. See
724 * {@link #setThreadPriority(int, int)} for more information.
725 *
726 * @param priority A Linux priority level, from -20 for highest scheduling
727 * priority to 19 for lowest scheduling priority.
728 *
729 * @throws IllegalArgumentException Throws IllegalArgumentException if
730 * <var>tid</var> does not exist.
731 * @throws SecurityException Throws SecurityException if your process does
732 * not have permission to modify the given thread, or to use the given
733 * priority.
734 *
735 * @see #setThreadPriority(int, int)
736 */
737 public static final native void setThreadPriority(int priority)
738 throws IllegalArgumentException, SecurityException;
739
740 /**
741 * Return the current priority of a thread, based on Linux priorities.
742 *
743 * @param tid The identifier of the thread/process to change.
744 *
745 * @return Returns the current priority, as a Linux priority level,
746 * from -20 for highest scheduling priority to 19 for lowest scheduling
747 * priority.
748 *
749 * @throws IllegalArgumentException Throws IllegalArgumentException if
750 * <var>tid</var> does not exist.
751 */
752 public static final native int getThreadPriority(int tid)
753 throws IllegalArgumentException;
754
755 /**
Srinath Sridharan1b15d132016-07-19 15:16:11 -0700756 * Return the current scheduling policy of a thread, based on Linux.
757 *
758 * @param tid The identifier of the thread/process to get the scheduling policy.
759 *
760 * @throws IllegalArgumentException Throws IllegalArgumentException if
761 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
762 * @throws SecurityException Throws SecurityException if your process does
763 * not have permission to modify the given thread, or to use the given
764 * scheduling policy or priority.
765 *
766 * {@hide}
767 */
768
769 @TestApi
770 public static final native int getThreadScheduler(int tid)
771 throws IllegalArgumentException;
772
773 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700774 * Set the scheduling policy and priority of a thread, based on Linux.
775 *
776 * @param tid The identifier of the thread/process to change.
777 * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
778 * @param priority A Linux priority level in a range appropriate for the given policy.
779 *
780 * @throws IllegalArgumentException Throws IllegalArgumentException if
781 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
782 * @throws SecurityException Throws SecurityException if your process does
783 * not have permission to modify the given thread, or to use the given
784 * scheduling policy or priority.
785 *
786 * {@hide}
787 */
Srinath Sridharan1b15d132016-07-19 15:16:11 -0700788
Glenn Kasten6793ac92011-07-13 12:44:12 -0700789 public static final native void setThreadScheduler(int tid, int policy, int priority)
790 throws IllegalArgumentException;
791
792 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 * Determine whether the current environment supports multiple processes.
794 *
795 * @return Returns true if the system can run in multiple processes, else
796 * false if everything is running in a single process.
Jeff Brown10e89712011-07-08 18:52:57 -0700797 *
798 * @deprecated This method always returns true. Do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 */
Jeff Brown10e89712011-07-08 18:52:57 -0700800 @Deprecated
801 public static final boolean supportsProcesses() {
802 return true;
803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804
805 /**
Rom Lemarchand5534ba92013-07-12 16:15:36 -0700806 * Adjust the swappiness level for a process.
807 *
808 * @param pid The process identifier to set.
809 * @param is_increased Whether swappiness should be increased or default.
810 *
811 * @return Returns true if the underlying system supports this
812 * feature, else false.
813 *
814 * {@hide}
815 */
816 public static final native boolean setSwappiness(int pid, boolean is_increased);
817
818 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 * Change this process's argv[0] parameter. This can be useful to show
820 * more descriptive information in things like the 'ps' command.
821 *
822 * @param text The new name of this process.
823 *
824 * {@hide}
825 */
826 public static final native void setArgV0(String text);
827
828 /**
829 * Kill the process with the given PID.
830 * Note that, though this API allows us to request to
831 * kill any process based on its PID, the kernel will
832 * still impose standard restrictions on which PIDs you
833 * are actually able to kill. Typically this means only
834 * the process running the caller's packages/application
835 * and any additional processes created by that app; packages
836 * sharing a common UID will also be able to kill each
837 * other's processes.
838 */
839 public static final void killProcess(int pid) {
840 sendSignal(pid, SIGNAL_KILL);
841 }
842
843 /** @hide */
844 public static final native int setUid(int uid);
845
846 /** @hide */
847 public static final native int setGid(int uid);
848
849 /**
850 * Send a signal to the given process.
851 *
852 * @param pid The pid of the target process.
853 * @param signal The signal to send.
854 */
855 public static final native void sendSignal(int pid, int signal);
856
Dianne Hackborn906497c2010-05-10 15:57:38 -0700857 /**
858 * @hide
859 * Private impl for avoiding a log message... DO NOT USE without doing
860 * your own log, or the Android Illuminati will find you some night and
861 * beat you up.
862 */
863 public static final void killProcessQuiet(int pid) {
864 sendSignalQuiet(pid, SIGNAL_KILL);
865 }
866
867 /**
868 * @hide
869 * Private impl for avoiding a log message... DO NOT USE without doing
870 * your own log, or the Android Illuminati will find you some night and
871 * beat you up.
872 */
873 public static final native void sendSignalQuiet(int pid, int signal);
874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 /** @hide */
Marco Nelissen0bca96b2009-07-17 12:59:25 -0700876 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877
878 /** @hide */
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700879 public static final native long getTotalMemory();
880
881 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 public static final native void readProcLines(String path,
883 String[] reqFields, long[] outSizes);
884
885 /** @hide */
886 public static final native int[] getPids(String path, int[] lastArray);
887
888 /** @hide */
889 public static final int PROC_TERM_MASK = 0xff;
890 /** @hide */
891 public static final int PROC_ZERO_TERM = 0;
892 /** @hide */
893 public static final int PROC_SPACE_TERM = (int)' ';
894 /** @hide */
Evan Millarc64edde2009-04-18 12:26:32 -0700895 public static final int PROC_TAB_TERM = (int)'\t';
896 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 public static final int PROC_COMBINE = 0x100;
898 /** @hide */
899 public static final int PROC_PARENS = 0x200;
900 /** @hide */
Dianne Hackborn13ac0412013-06-25 19:34:49 -0700901 public static final int PROC_QUOTES = 0x400;
902 /** @hide */
Dianne Hackborn4de5a3a2016-06-20 15:20:15 -0700903 public static final int PROC_CHAR = 0x800;
904 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 public static final int PROC_OUT_STRING = 0x1000;
906 /** @hide */
907 public static final int PROC_OUT_LONG = 0x2000;
908 /** @hide */
909 public static final int PROC_OUT_FLOAT = 0x4000;
910
911 /** @hide */
912 public static final native boolean readProcFile(String file, int[] format,
913 String[] outStrings, long[] outLongs, float[] outFloats);
Evan Millarc64edde2009-04-18 12:26:32 -0700914
915 /** @hide */
916 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
917 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700919 /** @hide */
920 public static final native int[] getPidsForCommands(String[] cmds);
921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 /**
923 * Gets the total Pss value for a given process, in bytes.
924 *
925 * @param pid the process to the Pss for
926 * @return the total Pss value for the given process in bytes,
927 * or -1 if the value cannot be determined
928 * @hide
929 */
930 public static final native long getPss(int pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -0700931
932 /**
933 * Specifies the outcome of having started a process.
934 * @hide
935 */
936 public static final class ProcessStartResult {
937 /**
938 * The PID of the newly started process.
939 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
940 */
941 public int pid;
942
943 /**
944 * True if the process was started with a wrapper attached.
945 */
946 public boolean usingWrapper;
947 }
Colin Cross0769e552014-06-03 13:25:35 -0700948
949 /**
950 * Kill all processes in a process group started for the given
951 * pid.
952 * @hide
953 */
954 public static final native int killProcessGroup(int uid, int pid);
955
956 /**
957 * Remove all process groups. Expected to be called when ActivityManager
958 * is restarted.
959 * @hide
960 */
961 public static final native void removeAllProcessGroups();
Tim Murrayb89970e2016-07-01 10:42:20 -0700962
963 /**
964 * Check to see if a thread belongs to a given process. This may require
965 * more permissions than apps generally have.
966 * @return true if this thread belongs to a process
967 * @hide
968 */
969 public static final boolean isThreadInProcess(int tid, int pid) {
Tim Murray9bc12a82016-07-16 11:28:40 -0700970 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
Tim Murrayb89970e2016-07-01 10:42:20 -0700971 try {
972 if (Os.access("/proc/" + tid + "/task/" + pid, OsConstants.F_OK)) {
973 return true;
974 } else {
975 return false;
976 }
977 } catch (Exception e) {
978 return false;
Tim Murray9bc12a82016-07-16 11:28:40 -0700979 } finally {
980 StrictMode.setThreadPolicy(oldPolicy);
Tim Murrayb89970e2016-07-01 10:42:20 -0700981 }
Tim Murray9bc12a82016-07-16 11:28:40 -0700982
Tim Murrayb89970e2016-07-01 10:42:20 -0700983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984}