blob: e9ebe2d4e950eb506baee03ac5fc64f7959e832b [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 Sridharan1a61a0a2016-07-19 15:16:11 -070019import android.annotation.TestApi;
20import android.net.LocalSocket;
21import android.net.LocalSocketAddress;
Elliott Hughes34385d32014-04-28 11:11:32 -070022import android.system.Os;
Tim Murrayb89970e2016-07-01 10:42:20 -070023import android.system.OsConstants;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.util.Log;
Andreas Gampeee0cdce2015-06-08 20:47:50 -070025import dalvik.system.VMRuntime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
27/**
28 * Tools for managing OS processes.
29 */
30public class Process {
31 private static final String LOG_TAG = "Process";
32
Narayan Kamath64cd9072014-05-13 13:35:14 +010033 /**
34 * @hide for internal use only.
35 */
36 public static final String ZYGOTE_SOCKET = "zygote";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
Narayan Kamath64cd9072014-05-13 13:35:14 +010038 /**
39 * @hide for internal use only.
40 */
41 public static final String SECONDARY_ZYGOTE_SOCKET = "zygote_secondary";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43 /**
Jeff Sharkey1cb2d0d2014-07-30 16:45:01 -070044 * Defines the root UID.
45 * @hide
46 */
47 public static final int ROOT_UID = 0;
48
49 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 * Defines the UID/GID under which system code runs.
51 */
52 public static final int SYSTEM_UID = 1000;
53
54 /**
55 * Defines the UID/GID under which the telephony code runs.
56 */
57 public static final int PHONE_UID = 1001;
58
59 /**
Dianne Hackborn854060af2009-07-09 18:14:31 -070060 * Defines the UID/GID for the user shell.
61 * @hide
62 */
63 public static final int SHELL_UID = 2000;
64
65 /**
Mike Lockwoodd42685d2009-09-03 09:25:22 -040066 * Defines the UID/GID for the log group.
67 * @hide
68 */
69 public static final int LOG_UID = 1007;
70
71 /**
Amith Yamasanid1582142009-07-08 20:04:55 -070072 * Defines the UID/GID for the WIFI supplicant process.
73 * @hide
74 */
75 public static final int WIFI_UID = 1010;
76
77 /**
Glenn Kasten8b7d1b42011-07-13 16:23:22 -070078 * Defines the UID/GID for the mediaserver process.
79 * @hide
80 */
81 public static final int MEDIA_UID = 1013;
82
83 /**
Jeff Sharkey5294a2f2012-04-24 17:07:22 -070084 * Defines the UID/GID for the DRM process.
85 * @hide
86 */
87 public static final int DRM_UID = 1019;
88
89 /**
Kenny Root26993b32012-03-19 15:07:51 -070090 * Defines the UID/GID for the group that controls VPN services.
91 * @hide
92 */
93 public static final int VPN_UID = 1016;
94
95 /**
Nick Pellycd0e8392010-10-13 17:25:24 -070096 * Defines the UID/GID for the NFC service process.
97 * @hide
98 */
Nick Pellya5cb9f42011-11-21 14:54:46 -080099 public static final int NFC_UID = 1027;
Nick Pellycd0e8392010-10-13 17:25:24 -0700100
101 /**
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800102 * Defines the UID/GID for the Bluetooth service process.
103 * @hide
104 */
105 public static final int BLUETOOTH_UID = 1002;
106
107 /**
Mike Lockwooddcaa10c2010-12-16 12:50:44 -0800108 * Defines the GID for the group that allows write access to the internal media storage.
109 * @hide
110 */
111 public static final int MEDIA_RW_GID = 1023;
112
113 /**
Jeff Sharkey184a0102013-07-10 16:19:52 -0700114 * Access to installed package details
115 * @hide
116 */
117 public static final int PACKAGE_INFO_GID = 1032;
118
119 /**
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100120 * Defines the UID/GID for the shared RELRO file updater process.
121 * @hide
122 */
123 public static final int SHARED_RELRO_UID = 1037;
124
125 /**
Andy Hunged0ea402015-10-30 14:11:46 -0700126 * Defines the UID/GID for the audioserver process.
127 * @hide
128 */
129 public static final int AUDIOSERVER_UID = 1041;
130
131 /**
Chien-Yu Chen75cade02016-01-11 10:56:21 -0800132 * Defines the UID/GID for the cameraserver process
133 * @hide
134 */
Chien-Yu Chenb8b4e852016-01-29 12:02:28 -0800135 public static final int CAMERASERVER_UID = 1047;
Chien-Yu Chen75cade02016-01-11 10:56:21 -0800136
137 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * Defines the start of a range of UIDs (and GIDs), going from this
139 * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning
140 * to applications.
141 */
142 public static final int FIRST_APPLICATION_UID = 10000;
Jeff Sharkey184a0102013-07-10 16:19:52 -0700143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 /**
145 * Last of application-specific UIDs starting at
146 * {@link #FIRST_APPLICATION_UID}.
147 */
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800148 public static final int LAST_APPLICATION_UID = 19999;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800149
150 /**
151 * First uid used for fully isolated sandboxed processes (with no permissions of their own)
152 * @hide
153 */
154 public static final int FIRST_ISOLATED_UID = 99000;
155
156 /**
157 * Last uid used for fully isolated sandboxed processes (with no permissions of their own)
158 * @hide
159 */
160 public static final int LAST_ISOLATED_UID = 99999;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
162 /**
Robin Leee66b6892014-04-29 12:36:47 +0100163 * Defines the gid shared by all applications running under the same profile.
164 * @hide
165 */
166 public static final int SHARED_USER_GID = 9997;
167
168 /**
Kenny Roote091f222012-09-11 15:01:26 -0700169 * First gid for applications to share resources. Used when forward-locking
170 * is enabled but all UserHandles need to be able to read the resources.
171 * @hide
172 */
173 public static final int FIRST_SHARED_APPLICATION_GID = 50000;
174
175 /**
176 * Last gid for applications to share resources. Used when forward-locking
177 * is enabled but all UserHandles need to be able to read the resources.
178 * @hide
179 */
180 public static final int LAST_SHARED_APPLICATION_GID = 59999;
181
182 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 * Standard priority of application threads.
184 * Use with {@link #setThreadPriority(int)} and
185 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
186 * {@link java.lang.Thread} class.
187 */
188 public static final int THREAD_PRIORITY_DEFAULT = 0;
189
190 /*
191 * ***************************************
192 * ** Keep in sync with utils/threads.h **
193 * ***************************************
194 */
195
196 /**
197 * Lowest available thread priority. Only for those who really, really
198 * don't want to run if anything else is happening.
199 * Use with {@link #setThreadPriority(int)} and
200 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
201 * {@link java.lang.Thread} class.
202 */
203 public static final int THREAD_PRIORITY_LOWEST = 19;
204
205 /**
206 * Standard priority background threads. This gives your thread a slightly
207 * lower than normal priority, so that it will have less chance of impacting
208 * the responsiveness of the user interface.
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_BACKGROUND = 10;
214
215 /**
216 * Standard priority of threads that are currently running a user interface
217 * that the user is interacting with. Applications can not normally
218 * change to this priority; the system will automatically adjust your
219 * application threads as the user moves through the UI.
220 * Use with {@link #setThreadPriority(int)} and
221 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
222 * {@link java.lang.Thread} class.
223 */
224 public static final int THREAD_PRIORITY_FOREGROUND = -2;
225
226 /**
227 * Standard priority of system display threads, involved in updating
228 * the user interface. Applications can not
229 * normally change to this priority.
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_DISPLAY = -4;
235
236 /**
237 * Standard priority of the most important display threads, for compositing
238 * the screen and retrieving input events. Applications can not normally
239 * 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_URGENT_DISPLAY = -8;
245
246 /**
247 * Standard priority of audio threads. Applications can not normally
248 * change to this priority.
249 * Use with {@link #setThreadPriority(int)} and
250 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
251 * {@link java.lang.Thread} class.
252 */
253 public static final int THREAD_PRIORITY_AUDIO = -16;
254
255 /**
256 * Standard priority of the most important audio threads.
257 * Applications can not normally change to this priority.
258 * Use with {@link #setThreadPriority(int)} and
259 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
260 * {@link java.lang.Thread} class.
261 */
262 public static final int THREAD_PRIORITY_URGENT_AUDIO = -19;
263
264 /**
265 * Minimum increment to make a priority more favorable.
266 */
267 public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
268
269 /**
270 * Minimum increment to make a priority less favorable.
271 */
272 public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
273
San Mehate9d376b2009-04-21 14:06:36 -0700274 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700275 * Default scheduling policy
276 * @hide
277 */
278 public static final int SCHED_OTHER = 0;
279
280 /**
281 * First-In First-Out scheduling policy
282 * @hide
283 */
284 public static final int SCHED_FIFO = 1;
285
286 /**
287 * Round-Robin scheduling policy
288 * @hide
289 */
290 public static final int SCHED_RR = 2;
291
292 /**
293 * Batch scheduling policy
294 * @hide
295 */
296 public static final int SCHED_BATCH = 3;
297
298 /**
299 * Idle scheduling policy
300 * @hide
301 */
302 public static final int SCHED_IDLE = 5;
303
Srinath Sridharane535a582016-06-27 18:13:47 -0700304 /**
305 * Reset scheduler choice on fork.
306 * @hide
307 */
308 public static final int SCHED_RESET_ON_FORK = 0x40000000;
309
Glenn Kastenf1b56442012-03-15 16:33:43 -0700310 // Keep in sync with SP_* constants of enum type SchedPolicy
311 // declared in system/core/include/cutils/sched_policy.h,
312 // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
San Mehate9d376b2009-04-21 14:06:36 -0700313
314 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700315 * Default thread group -
316 * has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
317 * When used with setProcessGroup(), the group of each thread in the process
318 * is conditionally changed based on that thread's current priority, as follows:
319 * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
320 * are moved to foreground thread group. All other threads are left unchanged.
321 * @hide
322 */
323 public static final int THREAD_GROUP_DEFAULT = -1;
324
325 /**
326 * Background thread group - All threads in
San Mehate9d376b2009-04-21 14:06:36 -0700327 * this group are scheduled with a reduced share of the CPU.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700328 * Value is same as constant SP_BACKGROUND of enum SchedPolicy.
329 * FIXME rename to THREAD_GROUP_BACKGROUND.
San Mehate9d376b2009-04-21 14:06:36 -0700330 * @hide
331 */
Glenn Kastenf1b56442012-03-15 16:33:43 -0700332 public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0;
San Mehate9d376b2009-04-21 14:06:36 -0700333
334 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700335 * Foreground thread group - All threads in
336 * this group are scheduled with a normal share of the CPU.
337 * Value is same as constant SP_FOREGROUND of enum SchedPolicy.
338 * Not used at this level.
San Mehate9d376b2009-04-21 14:06:36 -0700339 * @hide
340 **/
Glenn Kastenf1b56442012-03-15 16:33:43 -0700341 private static final int THREAD_GROUP_FOREGROUND = 1;
San Mehate9d376b2009-04-21 14:06:36 -0700342
Glenn Kasten07b04652012-04-23 15:00:43 -0700343 /**
344 * System thread group.
345 * @hide
346 **/
347 public static final int THREAD_GROUP_SYSTEM = 2;
348
349 /**
350 * Application audio thread group.
351 * @hide
352 **/
353 public static final int THREAD_GROUP_AUDIO_APP = 3;
354
355 /**
356 * System audio thread group.
357 * @hide
358 **/
359 public static final int THREAD_GROUP_AUDIO_SYS = 4;
360
Tim Murrayece6a222016-01-14 11:22:57 -0800361 /**
362 * Thread group for top foreground app.
363 * @hide
364 **/
365 public static final int THREAD_GROUP_TOP_APP = 5;
366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 public static final int SIGNAL_QUIT = 3;
368 public static final int SIGNAL_KILL = 9;
369 public static final int SIGNAL_USR1 = 10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370
Dianne Hackbornd98885c2016-03-01 17:13:03 -0800371 private static long sStartElapsedRealtime;
372 private static long sStartUptimeMillis;
373
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100374 /**
Robert Sesek94e824b2016-03-18 16:52:57 -0400375 * State associated with the zygote process.
376 * @hide
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100377 */
Robert Sesek94e824b2016-03-18 16:52:57 -0400378 public static final ZygoteProcess zygoteProcess =
379 new ZygoteProcess(ZYGOTE_SOCKET, SECONDARY_ZYGOTE_SOCKET);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380
381 /**
382 * Start a new process.
383 *
384 * <p>If processes are enabled, a new process is created and the
385 * static main() function of a <var>processClass</var> is executed there.
386 * The process will continue running after this function returns.
387 *
388 * <p>If processes are not enabled, a new thread in the caller's
389 * process is created and main() of <var>processClass</var> called there.
390 *
391 * <p>The niceName parameter, if not an empty string, is a custom name to
392 * give to the process instead of using processClass. This allows you to
393 * make easily identifyable processes even if you are using the same base
394 * <var>processClass</var> to start them.
395 *
396 * @param processClass The class to use as the process's main entry
397 * point.
398 * @param niceName A more readable name to use for the process.
399 * @param uid The user-id under which the process will run.
400 * @param gid The group-id under which the process will run.
401 * @param gids Additional group-ids associated with the process.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700402 * @param debugFlags Additional flags.
403 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalleybd19b9e2013-04-12 14:49:38 -0400404 * @param seInfo null-ok SELinux information for the new process.
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100405 * @param abi non-null the ABI this app should be started with.
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700406 * @param instructionSet null-ok the instruction set to use.
jgu212eacd062014-09-10 06:55:07 -0400407 * @param appDataDir null-ok the data directory of the app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 * @param zygoteArgs Additional arguments to supply to the zygote process.
409 *
Jeff Brown3f9dd282011-07-08 20:02:19 -0700410 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 * @throws RuntimeException on fatal start failure
412 *
413 * {@hide}
414 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700415 public static final ProcessStartResult start(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 final String niceName,
417 int uid, int gid, int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700418 int debugFlags, int mountExternal,
419 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500420 String seInfo,
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100421 String abi,
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700422 String instructionSet,
jgu212eacd062014-09-10 06:55:07 -0400423 String appDataDir,
Jeff Brown10e89712011-07-08 18:52:57 -0700424 String[] zygoteArgs) {
Robert Sesek94e824b2016-03-18 16:52:57 -0400425 return zygoteProcess.start(processClass, niceName, uid, gid, gids,
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100426 debugFlags, mountExternal, targetSdkVersion, seInfo,
jgu212eacd062014-09-10 06:55:07 -0400427 abi, instructionSet, appDataDir, zygoteArgs);
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100428 }
429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 /**
431 * Returns elapsed milliseconds of the time this process has run.
432 * @return Returns the number of milliseconds this process has return.
433 */
434 public static final native long getElapsedCpuTime();
Andreas Gampeee0cdce2015-06-08 20:47:50 -0700435
436 /**
Dianne Hackbornd98885c2016-03-01 17:13:03 -0800437 * Return the {@link SystemClock#elapsedRealtime()} at which this process was started.
438 */
439 public static final long getStartElapsedRealtime() {
440 return sStartElapsedRealtime;
441 }
442
443 /**
444 * Return the {@link SystemClock#uptimeMillis()} at which this process was started.
445 */
446 public static final long getStartUptimeMillis() {
447 return sStartUptimeMillis;
448 }
449
450 /** @hide */
451 public static final void setStartTimes(long elapsedRealtime, long uptimeMillis) {
452 sStartElapsedRealtime = elapsedRealtime;
453 sStartUptimeMillis = uptimeMillis;
454 }
455
456 /**
Andreas Gampeee0cdce2015-06-08 20:47:50 -0700457 * Returns true if the current process is a 64-bit runtime.
458 */
459 public static final boolean is64Bit() {
460 return VMRuntime.getRuntime().is64Bit();
461 }
462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 /**
464 * Returns the identifier of this process, which can be used with
465 * {@link #killProcess} and {@link #sendSignal}.
466 */
Jeff Hao406ec152013-07-30 10:13:41 -0700467 public static final int myPid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700468 return Os.getpid();
Jeff Hao406ec152013-07-30 10:13:41 -0700469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470
471 /**
Romain Guy80b12fc2013-05-29 15:54:25 -0700472 * Returns the identifier of this process' parent.
473 * @hide
474 */
Elliott Hughes2a805f92013-07-31 18:25:47 -0700475 public static final int myPpid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700476 return Os.getppid();
Elliott Hughes2a805f92013-07-31 18:25:47 -0700477 }
Romain Guy80b12fc2013-05-29 15:54:25 -0700478
479 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 * Returns the identifier of the calling thread, which be used with
481 * {@link #setThreadPriority(int, int)}.
482 */
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700483 public static final int myTid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700484 return Os.gettid();
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486
487 /**
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700488 * Returns the identifier of this process's uid. This is the kernel uid
489 * that the process is running under, which is the identity of its
490 * app-specific sandbox. It is different from {@link #myUserHandle} in that
491 * a uid identifies a specific app sandbox in a specific user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 */
Jeff Hao406ec152013-07-30 10:13:41 -0700493 public static final int myUid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700494 return Os.getuid();
Jeff Hao406ec152013-07-30 10:13:41 -0700495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496
497 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700498 * Returns this process's user handle. This is the
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700499 * user the process is running under. It is distinct from
500 * {@link #myUid()} in that a particular user will have multiple
501 * distinct apps running under it each with their own uid.
502 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700503 public static UserHandle myUserHandle() {
504 return UserHandle.of(UserHandle.getUserId(myUid()));
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700505 }
506
507 /**
Jeff Davidson48aa86b2016-02-10 14:15:42 -0800508 * Returns whether the given uid belongs to an application.
509 * @param uid A kernel uid.
510 * @return Whether the uid corresponds to an application sandbox running in
511 * a specific user.
512 */
513 public static boolean isApplicationUid(int uid) {
514 return UserHandle.isApp(uid);
515 }
516
517 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800518 * Returns whether the current process is in an isolated sandbox.
519 * @hide
520 */
521 public static final boolean isIsolated() {
Jeff Sharkey9527b222015-06-24 15:24:48 -0700522 return isIsolated(myUid());
523 }
524
525 /** {@hide} */
526 public static final boolean isIsolated(int uid) {
527 uid = UserHandle.getAppId(uid);
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800528 return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID;
529 }
530
531 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 * Returns the UID assigned to a particular user name, or -1 if there is
533 * none. If the given string consists of only numbers, it is converted
534 * directly to a uid.
535 */
536 public static final native int getUidForName(String name);
537
538 /**
539 * Returns the GID assigned to a particular user name, or -1 if there is
540 * none. If the given string consists of only numbers, it is converted
541 * directly to a gid.
542 */
543 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700544
545 /**
546 * Returns a uid for a currently running process.
547 * @param pid the process id
548 * @return the uid of the process, or -1 if the process is not running.
549 * @hide pending API council review
550 */
551 public static final int getUidForPid(int pid) {
552 String[] procStatusLabels = { "Uid:" };
553 long[] procStatusValues = new long[1];
554 procStatusValues[0] = -1;
555 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
556 return (int) procStatusValues[0];
557 }
558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700560 * Returns the parent process id for a currently running process.
561 * @param pid the process id
562 * @return the parent process id of the process, or -1 if the process is not running.
563 * @hide
564 */
565 public static final int getParentPid(int pid) {
566 String[] procStatusLabels = { "PPid:" };
567 long[] procStatusValues = new long[1];
568 procStatusValues[0] = -1;
569 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
570 return (int) procStatusValues[0];
571 }
572
573 /**
Glenn Kasten07b04652012-04-23 15:00:43 -0700574 * Returns the thread group leader id for a currently running thread.
575 * @param tid the thread id
576 * @return the thread group leader id of the thread, or -1 if the thread is not running.
577 * This is same as what getpid(2) would return if called by tid.
578 * @hide
579 */
580 public static final int getThreadGroupLeader(int tid) {
581 String[] procStatusLabels = { "Tgid:" };
582 long[] procStatusValues = new long[1];
583 procStatusValues[0] = -1;
584 Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues);
585 return (int) procStatusValues[0];
586 }
587
588 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 * Set the priority of a thread, based on Linux priorities.
590 *
591 * @param tid The identifier of the thread/process to change.
592 * @param priority A Linux priority level, from -20 for highest scheduling
593 * priority to 19 for lowest scheduling priority.
594 *
595 * @throws IllegalArgumentException Throws IllegalArgumentException if
596 * <var>tid</var> does not exist.
597 * @throws SecurityException Throws SecurityException if your process does
598 * not have permission to modify the given thread, or to use the given
599 * priority.
600 */
601 public static final native void setThreadPriority(int tid, int priority)
602 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700603
604 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700605 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
606 * throw an exception if passed a background-level thread priority. This is only
607 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
608 *
609 * @hide
610 */
611 public static final native void setCanSelfBackground(boolean backgroundOk);
612
613 /**
San Mehate9d376b2009-04-21 14:06:36 -0700614 * Sets the scheduling group for a thread.
615 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700616 * @param tid The identifier of the thread to change.
617 * @param group The target group for this thread from THREAD_GROUP_*.
San Mehate9d376b2009-04-21 14:06:36 -0700618 *
619 * @throws IllegalArgumentException Throws IllegalArgumentException if
620 * <var>tid</var> does not exist.
621 * @throws SecurityException Throws SecurityException if your process does
622 * not have permission to modify the given thread, or to use the given
623 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700624 * If the thread is a thread group leader, that is it's gettid() == getpid(),
625 * then the other threads in the same thread group are _not_ affected.
Tim Murray4074ad02016-07-08 15:45:45 -0700626 *
627 * Does not set cpuset for some historical reason, just calls
628 * libcutils::set_sched_policy().
San Mehate9d376b2009-04-21 14:06:36 -0700629 */
630 public static final native void setThreadGroup(int tid, int group)
631 throws IllegalArgumentException, SecurityException;
Glenn Kastenf1b56442012-03-15 16:33:43 -0700632
San Mehat3e458242009-05-19 14:44:16 -0700633 /**
634 * Sets the scheduling group for a process and all child threads
635 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700636 * @param pid The identifier of the process to change.
637 * @param group The target group for this process from THREAD_GROUP_*.
San Mehat3e458242009-05-19 14:44:16 -0700638 *
639 * @throws IllegalArgumentException Throws IllegalArgumentException if
640 * <var>tid</var> does not exist.
641 * @throws SecurityException Throws SecurityException if your process does
642 * not have permission to modify the given thread, or to use the given
643 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700644 *
645 * group == THREAD_GROUP_DEFAULT means to move all non-background priority
646 * threads to the foreground scheduling group, but to leave background
647 * priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all
648 * threads, regardless of priority, to the background scheduling group.
649 * group == THREAD_GROUP_FOREGROUND is not allowed.
Tim Murray4074ad02016-07-08 15:45:45 -0700650 *
651 * Always sets cpusets.
San Mehat3e458242009-05-19 14:44:16 -0700652 */
653 public static final native void setProcessGroup(int pid, int group)
654 throws IllegalArgumentException, SecurityException;
Jeff Sharkey9e57c412013-01-17 14:12:41 -0800655
656 /**
657 * Return the scheduling group of requested process.
658 *
659 * @hide
660 */
661 public static final native int getProcessGroup(int pid)
662 throws IllegalArgumentException, SecurityException;
663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 /**
Martijn Coenencd4bdf32016-03-03 17:30:52 +0100665 * On some devices, the foreground process may have one or more CPU
666 * cores exclusively reserved for it. This method can be used to
667 * retrieve which cores that are (if any), so the calling process
668 * can then use sched_setaffinity() to lock a thread to these cores.
669 * Note that the calling process must currently be running in the
670 * foreground for this method to return any cores.
671 *
672 * The CPU core(s) exclusively reserved for the foreground process will
673 * stay reserved for as long as the process stays in the foreground.
674 *
675 * As soon as a process leaves the foreground, those CPU cores will
676 * no longer be reserved for it, and will most likely be reserved for
677 * the new foreground process. It's not necessary to change the affinity
678 * of your process when it leaves the foreground (if you had previously
679 * set it to use a reserved core); the OS will automatically take care
680 * of resetting the affinity at that point.
681 *
682 * @return an array of integers, indicating the CPU cores exclusively
683 * reserved for this process. The array will have length zero if no
684 * CPU cores are exclusively reserved for this process at this point
685 * in time.
686 */
687 public static final native int[] getExclusiveCores();
688
689 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 * Set the priority of the calling thread, based on Linux priorities. See
691 * {@link #setThreadPriority(int, int)} for more information.
692 *
693 * @param priority A Linux priority level, from -20 for highest scheduling
694 * priority to 19 for lowest scheduling priority.
695 *
696 * @throws IllegalArgumentException Throws IllegalArgumentException if
697 * <var>tid</var> does not exist.
698 * @throws SecurityException Throws SecurityException if your process does
699 * not have permission to modify the given thread, or to use the given
700 * priority.
701 *
702 * @see #setThreadPriority(int, int)
703 */
704 public static final native void setThreadPriority(int priority)
705 throws IllegalArgumentException, SecurityException;
706
707 /**
708 * Return the current priority of a thread, based on Linux priorities.
709 *
710 * @param tid The identifier of the thread/process to change.
711 *
712 * @return Returns the current priority, as a Linux priority level,
713 * from -20 for highest scheduling priority to 19 for lowest scheduling
714 * priority.
715 *
716 * @throws IllegalArgumentException Throws IllegalArgumentException if
717 * <var>tid</var> does not exist.
718 */
719 public static final native int getThreadPriority(int tid)
720 throws IllegalArgumentException;
721
722 /**
Srinath Sridharan1a61a0a2016-07-19 15:16:11 -0700723 * Return the current scheduling policy of a thread, based on Linux.
724 *
725 * @param tid The identifier of the thread/process to get the scheduling policy.
726 *
727 * @throws IllegalArgumentException Throws IllegalArgumentException if
728 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
729 * @throws SecurityException Throws SecurityException if your process does
730 * not have permission to modify the given thread, or to use the given
731 * scheduling policy or priority.
732 *
733 * {@hide}
734 */
735
736 @TestApi
737 public static final native int getThreadScheduler(int tid)
738 throws IllegalArgumentException;
739
740 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700741 * Set the scheduling policy and priority of a thread, based on Linux.
742 *
743 * @param tid The identifier of the thread/process to change.
744 * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
745 * @param priority A Linux priority level in a range appropriate for the given policy.
746 *
747 * @throws IllegalArgumentException Throws IllegalArgumentException if
748 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
749 * @throws SecurityException Throws SecurityException if your process does
750 * not have permission to modify the given thread, or to use the given
751 * scheduling policy or priority.
752 *
753 * {@hide}
754 */
Srinath Sridharan1a61a0a2016-07-19 15:16:11 -0700755
Glenn Kasten6793ac92011-07-13 12:44:12 -0700756 public static final native void setThreadScheduler(int tid, int policy, int priority)
757 throws IllegalArgumentException;
758
759 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 * Determine whether the current environment supports multiple processes.
761 *
762 * @return Returns true if the system can run in multiple processes, else
763 * false if everything is running in a single process.
Jeff Brown10e89712011-07-08 18:52:57 -0700764 *
765 * @deprecated This method always returns true. Do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 */
Jeff Brown10e89712011-07-08 18:52:57 -0700767 @Deprecated
768 public static final boolean supportsProcesses() {
769 return true;
770 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771
772 /**
Rom Lemarchand5534ba92013-07-12 16:15:36 -0700773 * Adjust the swappiness level for a process.
774 *
775 * @param pid The process identifier to set.
776 * @param is_increased Whether swappiness should be increased or default.
777 *
778 * @return Returns true if the underlying system supports this
779 * feature, else false.
780 *
781 * {@hide}
782 */
783 public static final native boolean setSwappiness(int pid, boolean is_increased);
784
785 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 * Change this process's argv[0] parameter. This can be useful to show
787 * more descriptive information in things like the 'ps' command.
788 *
789 * @param text The new name of this process.
790 *
791 * {@hide}
792 */
793 public static final native void setArgV0(String text);
794
795 /**
796 * Kill the process with the given PID.
797 * Note that, though this API allows us to request to
798 * kill any process based on its PID, the kernel will
799 * still impose standard restrictions on which PIDs you
800 * are actually able to kill. Typically this means only
801 * the process running the caller's packages/application
802 * and any additional processes created by that app; packages
803 * sharing a common UID will also be able to kill each
804 * other's processes.
805 */
806 public static final void killProcess(int pid) {
807 sendSignal(pid, SIGNAL_KILL);
808 }
809
810 /** @hide */
811 public static final native int setUid(int uid);
812
813 /** @hide */
814 public static final native int setGid(int uid);
815
816 /**
817 * Send a signal to the given process.
818 *
819 * @param pid The pid of the target process.
820 * @param signal The signal to send.
821 */
822 public static final native void sendSignal(int pid, int signal);
823
Dianne Hackborn906497c2010-05-10 15:57:38 -0700824 /**
825 * @hide
826 * Private impl for avoiding a log message... DO NOT USE without doing
827 * your own log, or the Android Illuminati will find you some night and
828 * beat you up.
829 */
830 public static final void killProcessQuiet(int pid) {
831 sendSignalQuiet(pid, SIGNAL_KILL);
832 }
833
834 /**
835 * @hide
836 * Private impl for avoiding a log message... DO NOT USE without doing
837 * your own log, or the Android Illuminati will find you some night and
838 * beat you up.
839 */
840 public static final native void sendSignalQuiet(int pid, int signal);
841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 /** @hide */
Marco Nelissen0bca96b2009-07-17 12:59:25 -0700843 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844
845 /** @hide */
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700846 public static final native long getTotalMemory();
847
848 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 public static final native void readProcLines(String path,
850 String[] reqFields, long[] outSizes);
851
852 /** @hide */
853 public static final native int[] getPids(String path, int[] lastArray);
854
855 /** @hide */
856 public static final int PROC_TERM_MASK = 0xff;
857 /** @hide */
858 public static final int PROC_ZERO_TERM = 0;
859 /** @hide */
860 public static final int PROC_SPACE_TERM = (int)' ';
861 /** @hide */
Evan Millarc64edde2009-04-18 12:26:32 -0700862 public static final int PROC_TAB_TERM = (int)'\t';
863 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 public static final int PROC_COMBINE = 0x100;
865 /** @hide */
866 public static final int PROC_PARENS = 0x200;
867 /** @hide */
Dianne Hackborn13ac0412013-06-25 19:34:49 -0700868 public static final int PROC_QUOTES = 0x400;
869 /** @hide */
Dianne Hackborn4de5a3a2016-06-20 15:20:15 -0700870 public static final int PROC_CHAR = 0x800;
871 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 public static final int PROC_OUT_STRING = 0x1000;
873 /** @hide */
874 public static final int PROC_OUT_LONG = 0x2000;
875 /** @hide */
876 public static final int PROC_OUT_FLOAT = 0x4000;
877
878 /** @hide */
879 public static final native boolean readProcFile(String file, int[] format,
880 String[] outStrings, long[] outLongs, float[] outFloats);
Evan Millarc64edde2009-04-18 12:26:32 -0700881
882 /** @hide */
883 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
884 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700886 /** @hide */
887 public static final native int[] getPidsForCommands(String[] cmds);
888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 /**
890 * Gets the total Pss value for a given process, in bytes.
891 *
892 * @param pid the process to the Pss for
893 * @return the total Pss value for the given process in bytes,
894 * or -1 if the value cannot be determined
895 * @hide
896 */
897 public static final native long getPss(int pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -0700898
899 /**
900 * Specifies the outcome of having started a process.
901 * @hide
902 */
903 public static final class ProcessStartResult {
904 /**
905 * The PID of the newly started process.
906 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
907 */
908 public int pid;
909
910 /**
911 * True if the process was started with a wrapper attached.
912 */
913 public boolean usingWrapper;
914 }
Colin Cross0769e552014-06-03 13:25:35 -0700915
916 /**
917 * Kill all processes in a process group started for the given
918 * pid.
919 * @hide
920 */
921 public static final native int killProcessGroup(int uid, int pid);
922
923 /**
924 * Remove all process groups. Expected to be called when ActivityManager
925 * is restarted.
926 * @hide
927 */
928 public static final native void removeAllProcessGroups();
Tim Murrayb89970e2016-07-01 10:42:20 -0700929
930 /**
931 * Check to see if a thread belongs to a given process. This may require
932 * more permissions than apps generally have.
933 * @return true if this thread belongs to a process
934 * @hide
935 */
936 public static final boolean isThreadInProcess(int tid, int pid) {
Tim Murray9bc12a82016-07-16 11:28:40 -0700937 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
Tim Murrayb89970e2016-07-01 10:42:20 -0700938 try {
939 if (Os.access("/proc/" + tid + "/task/" + pid, OsConstants.F_OK)) {
940 return true;
941 } else {
942 return false;
943 }
944 } catch (Exception e) {
945 return false;
Tim Murray9bc12a82016-07-16 11:28:40 -0700946 } finally {
947 StrictMode.setThreadPolicy(oldPolicy);
Tim Murrayb89970e2016-07-01 10:42:20 -0700948 }
Tim Murray9bc12a82016-07-16 11:28:40 -0700949
Tim Murrayb89970e2016-07-01 10:42:20 -0700950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951}