blob: 4cf876787ac756569c71f4bf232909d62fe9c0a6 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import android.net.LocalSocket;
Narayan Kamath973b4662014-03-31 13:41:26 +010020import android.net.LocalSocketAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.util.Log;
Narayan Kamath973b4662014-03-31 13:41:26 +010022
23import com.android.internal.os.Zygote;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25import java.io.BufferedWriter;
26import java.io.DataInputStream;
27import java.io.IOException;
28import java.io.OutputStreamWriter;
29import java.util.ArrayList;
30
Jeff Hao406ec152013-07-30 10:13:41 -070031import libcore.io.Libcore;
32
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033/*package*/ class ZygoteStartFailedEx extends Exception {
34 /**
35 * Something prevented the zygote process startup from happening normally
36 */
37
38 ZygoteStartFailedEx() {};
39 ZygoteStartFailedEx(String s) {super(s);}
40 ZygoteStartFailedEx(Throwable cause) {super(cause);}
41}
42
43/**
44 * Tools for managing OS processes.
45 */
46public class Process {
47 private static final String LOG_TAG = "Process";
48
49 private static final String ZYGOTE_SOCKET = "zygote";
50
51 /**
52 * Name of a process for running the platform's media services.
53 * {@hide}
54 */
55 public static final String ANDROID_SHARED_MEDIA = "com.android.process.media";
56
57 /**
58 * Name of the process that Google content providers can share.
59 * {@hide}
60 */
61 public static final String GOOGLE_SHARED_APP_CONTENT = "com.google.process.content";
62
63 /**
64 * Defines the UID/GID under which system code runs.
65 */
66 public static final int SYSTEM_UID = 1000;
67
68 /**
69 * Defines the UID/GID under which the telephony code runs.
70 */
71 public static final int PHONE_UID = 1001;
72
73 /**
Dianne Hackborn854060af2009-07-09 18:14:31 -070074 * Defines the UID/GID for the user shell.
75 * @hide
76 */
77 public static final int SHELL_UID = 2000;
78
79 /**
Mike Lockwoodd42685d2009-09-03 09:25:22 -040080 * Defines the UID/GID for the log group.
81 * @hide
82 */
83 public static final int LOG_UID = 1007;
84
85 /**
Amith Yamasanid1582142009-07-08 20:04:55 -070086 * Defines the UID/GID for the WIFI supplicant process.
87 * @hide
88 */
89 public static final int WIFI_UID = 1010;
90
91 /**
Glenn Kasten8b7d1b42011-07-13 16:23:22 -070092 * Defines the UID/GID for the mediaserver process.
93 * @hide
94 */
95 public static final int MEDIA_UID = 1013;
96
97 /**
Jeff Sharkey5294a2f2012-04-24 17:07:22 -070098 * Defines the UID/GID for the DRM process.
99 * @hide
100 */
101 public static final int DRM_UID = 1019;
102
103 /**
Kenny Root26993b32012-03-19 15:07:51 -0700104 * Defines the UID/GID for the group that controls VPN services.
105 * @hide
106 */
107 public static final int VPN_UID = 1016;
108
109 /**
Nick Pellycd0e8392010-10-13 17:25:24 -0700110 * Defines the UID/GID for the NFC service process.
111 * @hide
112 */
Nick Pellya5cb9f42011-11-21 14:54:46 -0800113 public static final int NFC_UID = 1027;
Nick Pellycd0e8392010-10-13 17:25:24 -0700114
115 /**
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800116 * Defines the UID/GID for the Bluetooth service process.
117 * @hide
118 */
119 public static final int BLUETOOTH_UID = 1002;
120
121 /**
Mike Lockwooddcaa10c2010-12-16 12:50:44 -0800122 * Defines the GID for the group that allows write access to the internal media storage.
123 * @hide
124 */
125 public static final int MEDIA_RW_GID = 1023;
126
127 /**
Jeff Sharkey184a0102013-07-10 16:19:52 -0700128 * Access to installed package details
129 * @hide
130 */
131 public static final int PACKAGE_INFO_GID = 1032;
132
133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * Defines the start of a range of UIDs (and GIDs), going from this
135 * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning
136 * to applications.
137 */
138 public static final int FIRST_APPLICATION_UID = 10000;
Jeff Sharkey184a0102013-07-10 16:19:52 -0700139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 /**
141 * Last of application-specific UIDs starting at
142 * {@link #FIRST_APPLICATION_UID}.
143 */
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800144 public static final int LAST_APPLICATION_UID = 19999;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800145
146 /**
147 * First uid used for fully isolated sandboxed processes (with no permissions of their own)
148 * @hide
149 */
150 public static final int FIRST_ISOLATED_UID = 99000;
151
152 /**
153 * Last uid used for fully isolated sandboxed processes (with no permissions of their own)
154 * @hide
155 */
156 public static final int LAST_ISOLATED_UID = 99999;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157
158 /**
Kenny Roote091f222012-09-11 15:01:26 -0700159 * First gid for applications to share resources. Used when forward-locking
160 * is enabled but all UserHandles need to be able to read the resources.
161 * @hide
162 */
163 public static final int FIRST_SHARED_APPLICATION_GID = 50000;
164
165 /**
166 * Last gid for applications to share resources. Used when forward-locking
167 * is enabled but all UserHandles need to be able to read the resources.
168 * @hide
169 */
170 public static final int LAST_SHARED_APPLICATION_GID = 59999;
171
172 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 * Standard priority of application threads.
174 * Use with {@link #setThreadPriority(int)} and
175 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
176 * {@link java.lang.Thread} class.
177 */
178 public static final int THREAD_PRIORITY_DEFAULT = 0;
179
180 /*
181 * ***************************************
182 * ** Keep in sync with utils/threads.h **
183 * ***************************************
184 */
185
186 /**
187 * Lowest available thread priority. Only for those who really, really
188 * don't want to run if anything else is happening.
189 * Use with {@link #setThreadPriority(int)} and
190 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
191 * {@link java.lang.Thread} class.
192 */
193 public static final int THREAD_PRIORITY_LOWEST = 19;
194
195 /**
196 * Standard priority background threads. This gives your thread a slightly
197 * lower than normal priority, so that it will have less chance of impacting
198 * the responsiveness of the user interface.
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_BACKGROUND = 10;
204
205 /**
206 * Standard priority of threads that are currently running a user interface
207 * that the user is interacting with. Applications can not normally
208 * change to this priority; the system will automatically adjust your
209 * application threads as the user moves through the UI.
210 * Use with {@link #setThreadPriority(int)} and
211 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
212 * {@link java.lang.Thread} class.
213 */
214 public static final int THREAD_PRIORITY_FOREGROUND = -2;
215
216 /**
217 * Standard priority of system display threads, involved in updating
218 * the user interface. Applications can not
219 * normally change to this priority.
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_DISPLAY = -4;
225
226 /**
227 * Standard priority of the most important display threads, for compositing
228 * the screen and retrieving input events. Applications can not normally
229 * 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_URGENT_DISPLAY = -8;
235
236 /**
237 * Standard priority of audio threads. Applications can not normally
238 * change to this priority.
239 * Use with {@link #setThreadPriority(int)} and
240 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
241 * {@link java.lang.Thread} class.
242 */
243 public static final int THREAD_PRIORITY_AUDIO = -16;
244
245 /**
246 * Standard priority of the most important audio threads.
247 * Applications can not normally change to this priority.
248 * Use with {@link #setThreadPriority(int)} and
249 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
250 * {@link java.lang.Thread} class.
251 */
252 public static final int THREAD_PRIORITY_URGENT_AUDIO = -19;
253
254 /**
255 * Minimum increment to make a priority more favorable.
256 */
257 public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
258
259 /**
260 * Minimum increment to make a priority less favorable.
261 */
262 public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
263
San Mehate9d376b2009-04-21 14:06:36 -0700264 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700265 * Default scheduling policy
266 * @hide
267 */
268 public static final int SCHED_OTHER = 0;
269
270 /**
271 * First-In First-Out scheduling policy
272 * @hide
273 */
274 public static final int SCHED_FIFO = 1;
275
276 /**
277 * Round-Robin scheduling policy
278 * @hide
279 */
280 public static final int SCHED_RR = 2;
281
282 /**
283 * Batch scheduling policy
284 * @hide
285 */
286 public static final int SCHED_BATCH = 3;
287
288 /**
289 * Idle scheduling policy
290 * @hide
291 */
292 public static final int SCHED_IDLE = 5;
293
Glenn Kastenf1b56442012-03-15 16:33:43 -0700294 // Keep in sync with SP_* constants of enum type SchedPolicy
295 // declared in system/core/include/cutils/sched_policy.h,
296 // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
San Mehate9d376b2009-04-21 14:06:36 -0700297
298 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700299 * Default thread group -
300 * has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
301 * When used with setProcessGroup(), the group of each thread in the process
302 * is conditionally changed based on that thread's current priority, as follows:
303 * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
304 * are moved to foreground thread group. All other threads are left unchanged.
305 * @hide
306 */
307 public static final int THREAD_GROUP_DEFAULT = -1;
308
309 /**
310 * Background thread group - All threads in
San Mehate9d376b2009-04-21 14:06:36 -0700311 * this group are scheduled with a reduced share of the CPU.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700312 * Value is same as constant SP_BACKGROUND of enum SchedPolicy.
313 * FIXME rename to THREAD_GROUP_BACKGROUND.
San Mehate9d376b2009-04-21 14:06:36 -0700314 * @hide
315 */
Glenn Kastenf1b56442012-03-15 16:33:43 -0700316 public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0;
San Mehate9d376b2009-04-21 14:06:36 -0700317
318 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700319 * Foreground thread group - All threads in
320 * this group are scheduled with a normal share of the CPU.
321 * Value is same as constant SP_FOREGROUND of enum SchedPolicy.
322 * Not used at this level.
San Mehate9d376b2009-04-21 14:06:36 -0700323 * @hide
324 **/
Glenn Kastenf1b56442012-03-15 16:33:43 -0700325 private static final int THREAD_GROUP_FOREGROUND = 1;
San Mehate9d376b2009-04-21 14:06:36 -0700326
Glenn Kasten07b04652012-04-23 15:00:43 -0700327 /**
328 * System thread group.
329 * @hide
330 **/
331 public static final int THREAD_GROUP_SYSTEM = 2;
332
333 /**
334 * Application audio thread group.
335 * @hide
336 **/
337 public static final int THREAD_GROUP_AUDIO_APP = 3;
338
339 /**
340 * System audio thread group.
341 * @hide
342 **/
343 public static final int THREAD_GROUP_AUDIO_SYS = 4;
344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 public static final int SIGNAL_QUIT = 3;
346 public static final int SIGNAL_KILL = 9;
347 public static final int SIGNAL_USR1 = 10;
348
349 // State for communicating with zygote process
350
351 static LocalSocket sZygoteSocket;
352 static DataInputStream sZygoteInputStream;
353 static BufferedWriter sZygoteWriter;
354
355 /** true if previous zygote open failed */
356 static boolean sPreviousZygoteOpenFailed;
357
358 /**
359 * Start a new process.
360 *
361 * <p>If processes are enabled, a new process is created and the
362 * static main() function of a <var>processClass</var> is executed there.
363 * The process will continue running after this function returns.
364 *
365 * <p>If processes are not enabled, a new thread in the caller's
366 * process is created and main() of <var>processClass</var> called there.
367 *
368 * <p>The niceName parameter, if not an empty string, is a custom name to
369 * give to the process instead of using processClass. This allows you to
370 * make easily identifyable processes even if you are using the same base
371 * <var>processClass</var> to start them.
372 *
373 * @param processClass The class to use as the process's main entry
374 * point.
375 * @param niceName A more readable name to use for the process.
376 * @param uid The user-id under which the process will run.
377 * @param gid The group-id under which the process will run.
378 * @param gids Additional group-ids associated with the process.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700379 * @param debugFlags Additional flags.
380 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalleybd19b9e2013-04-12 14:49:38 -0400381 * @param seInfo null-ok SELinux information for the new process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 * @param zygoteArgs Additional arguments to supply to the zygote process.
383 *
Jeff Brown3f9dd282011-07-08 20:02:19 -0700384 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 * @throws RuntimeException on fatal start failure
386 *
387 * {@hide}
388 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700389 public static final ProcessStartResult start(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 final String niceName,
391 int uid, int gid, int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700392 int debugFlags, int mountExternal,
393 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500394 String seInfo,
Jeff Brown10e89712011-07-08 18:52:57 -0700395 String[] zygoteArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 try {
Jeff Brown10e89712011-07-08 18:52:57 -0700397 return startViaZygote(processClass, niceName, uid, gid, gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700398 debugFlags, mountExternal, targetSdkVersion, seInfo, zygoteArgs);
Jeff Brown10e89712011-07-08 18:52:57 -0700399 } catch (ZygoteStartFailedEx ex) {
400 Log.e(LOG_TAG,
401 "Starting VM process through Zygote failed");
402 throw new RuntimeException(
403 "Starting VM process through Zygote failed", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 }
406
407 /** retry interval for opening a zygote socket */
408 static final int ZYGOTE_RETRY_MILLIS = 500;
409
410 /**
411 * Tries to open socket to Zygote process if not already open. If
412 * already open, does nothing. May block and retry.
413 */
414 private static void openZygoteSocketIfNeeded()
415 throws ZygoteStartFailedEx {
416
417 int retryCount;
418
419 if (sPreviousZygoteOpenFailed) {
420 /*
421 * If we've failed before, expect that we'll fail again and
422 * don't pause for retries.
423 */
424 retryCount = 0;
425 } else {
426 retryCount = 10;
427 }
428
429 /*
430 * See bug #811181: Sometimes runtime can make it up before zygote.
431 * Really, we'd like to do something better to avoid this condition,
432 * but for now just wait a bit...
433 */
434 for (int retry = 0
435 ; (sZygoteSocket == null) && (retry < (retryCount + 1))
436 ; retry++ ) {
437
438 if (retry > 0) {
439 try {
440 Log.i("Zygote", "Zygote not up yet, sleeping...");
441 Thread.sleep(ZYGOTE_RETRY_MILLIS);
442 } catch (InterruptedException ex) {
443 // should never happen
444 }
445 }
446
447 try {
448 sZygoteSocket = new LocalSocket();
449
450 sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET,
451 LocalSocketAddress.Namespace.RESERVED));
452
453 sZygoteInputStream
454 = new DataInputStream(sZygoteSocket.getInputStream());
455
456 sZygoteWriter =
457 new BufferedWriter(
458 new OutputStreamWriter(
459 sZygoteSocket.getOutputStream()),
460 256);
461
462 Log.i("Zygote", "Process: zygote socket opened");
463
464 sPreviousZygoteOpenFailed = false;
465 break;
466 } catch (IOException ex) {
467 if (sZygoteSocket != null) {
468 try {
469 sZygoteSocket.close();
470 } catch (IOException ex2) {
471 Log.e(LOG_TAG,"I/O exception on close after exception",
472 ex2);
473 }
474 }
475
476 sZygoteSocket = null;
477 }
478 }
479
480 if (sZygoteSocket == null) {
481 sPreviousZygoteOpenFailed = true;
482 throw new ZygoteStartFailedEx("connect failed");
483 }
484 }
485
486 /**
487 * Sends an argument list to the zygote process, which starts a new child
488 * and returns the child's pid. Please note: the present implementation
489 * replaces newlines in the argument list with spaces.
490 * @param args argument list
Jeff Brown3f9dd282011-07-08 20:02:19 -0700491 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 * @throws ZygoteStartFailedEx if process start failed for any reason
493 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700494 private static ProcessStartResult zygoteSendArgsAndGetResult(ArrayList<String> args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 openZygoteSocketIfNeeded();
497
498 try {
499 /**
500 * See com.android.internal.os.ZygoteInit.readArgumentList()
501 * Presently the wire format to the zygote process is:
502 * a) a count of arguments (argc, in essence)
503 * b) a number of newline-separated argument strings equal to count
504 *
505 * After the zygote process reads these it will write the pid of
Jeff Brown3f9dd282011-07-08 20:02:19 -0700506 * the child or -1 on failure, followed by boolean to
507 * indicate whether a wrapper process was used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 */
509
510 sZygoteWriter.write(Integer.toString(args.size()));
511 sZygoteWriter.newLine();
512
513 int sz = args.size();
514 for (int i = 0; i < sz; i++) {
515 String arg = args.get(i);
516 if (arg.indexOf('\n') >= 0) {
517 throw new ZygoteStartFailedEx(
518 "embedded newlines not allowed");
519 }
520 sZygoteWriter.write(arg);
521 sZygoteWriter.newLine();
522 }
523
524 sZygoteWriter.flush();
525
526 // Should there be a timeout on this?
Jeff Brown3f9dd282011-07-08 20:02:19 -0700527 ProcessStartResult result = new ProcessStartResult();
528 result.pid = sZygoteInputStream.readInt();
529 if (result.pid < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 throw new ZygoteStartFailedEx("fork() failed");
531 }
Jeff Brown3f9dd282011-07-08 20:02:19 -0700532 result.usingWrapper = sZygoteInputStream.readBoolean();
533 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 } catch (IOException ex) {
535 try {
536 if (sZygoteSocket != null) {
537 sZygoteSocket.close();
538 }
539 } catch (IOException ex2) {
540 // we're going to fail anyway
541 Log.e(LOG_TAG,"I/O exception on routine close", ex2);
542 }
543
544 sZygoteSocket = null;
545
546 throw new ZygoteStartFailedEx(ex);
547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 }
549
550 /**
551 * Starts a new process via the zygote mechanism.
552 *
553 * @param processClass Class name whose static main() to run
554 * @param niceName 'nice' process name to appear in ps
555 * @param uid a POSIX uid that the new process should setuid() to
556 * @param gid a POSIX gid that the new process shuold setgid() to
557 * @param gids null-ok; a list of supplementary group IDs that the
558 * new process should setgroup() to.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700559 * @param debugFlags Additional flags.
560 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalleybd19b9e2013-04-12 14:49:38 -0400561 * @param seInfo null-ok SELinux information for the new process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 * @param extraArgs Additional arguments to supply to the zygote process.
Jeff Brown3f9dd282011-07-08 20:02:19 -0700563 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 * @throws ZygoteStartFailedEx if process start failed for any reason
565 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700566 private static ProcessStartResult startViaZygote(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 final String niceName,
568 final int uid, final int gid,
569 final int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700570 int debugFlags, int mountExternal,
571 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500572 String seInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 String[] extraArgs)
574 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 synchronized(Process.class) {
576 ArrayList<String> argsForZygote = new ArrayList<String>();
577
578 // --runtime-init, --setuid=, --setgid=,
579 // and --setgroups= must go first
580 argsForZygote.add("--runtime-init");
581 argsForZygote.add("--setuid=" + uid);
582 argsForZygote.add("--setgid=" + gid);
Elliott Hughesae07ecf2011-07-06 17:33:27 -0700583 if ((debugFlags & Zygote.DEBUG_ENABLE_JNI_LOGGING) != 0) {
584 argsForZygote.add("--enable-jni-logging");
585 }
Ben Cheng23085b72010-02-08 16:06:32 -0800586 if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) {
587 argsForZygote.add("--enable-safemode");
588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) {
590 argsForZygote.add("--enable-debugger");
591 }
592 if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) {
593 argsForZygote.add("--enable-checkjni");
594 }
595 if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) {
596 argsForZygote.add("--enable-assert");
597 }
Jeff Sharkey2bca8682012-08-22 13:59:58 -0700598 if (mountExternal == Zygote.MOUNT_EXTERNAL_MULTIUSER) {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700599 argsForZygote.add("--mount-external-multiuser");
Jeff Sharkeye217ee42012-08-28 16:23:01 -0700600 } else if (mountExternal == Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL) {
601 argsForZygote.add("--mount-external-multiuser-all");
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700602 }
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700603 argsForZygote.add("--target-sdk-version=" + targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604
605 //TODO optionally enable debuger
606 //argsForZygote.add("--enable-debugger");
607
608 // --setgroups is a comma-separated list
609 if (gids != null && gids.length > 0) {
610 StringBuilder sb = new StringBuilder();
611 sb.append("--setgroups=");
612
613 int sz = gids.length;
614 for (int i = 0; i < sz; i++) {
615 if (i != 0) {
616 sb.append(',');
617 }
618 sb.append(gids[i]);
619 }
620
621 argsForZygote.add(sb.toString());
622 }
623
624 if (niceName != null) {
625 argsForZygote.add("--nice-name=" + niceName);
626 }
627
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500628 if (seInfo != null) {
629 argsForZygote.add("--seinfo=" + seInfo);
630 }
631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 argsForZygote.add(processClass);
633
634 if (extraArgs != null) {
635 for (String arg : extraArgs) {
636 argsForZygote.add(arg);
637 }
638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639
Jeff Brown3f9dd282011-07-08 20:02:19 -0700640 return zygoteSendArgsAndGetResult(argsForZygote);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 }
643
644 /**
645 * Returns elapsed milliseconds of the time this process has run.
646 * @return Returns the number of milliseconds this process has return.
647 */
648 public static final native long getElapsedCpuTime();
649
650 /**
651 * Returns the identifier of this process, which can be used with
652 * {@link #killProcess} and {@link #sendSignal}.
653 */
Jeff Hao406ec152013-07-30 10:13:41 -0700654 public static final int myPid() {
655 return Libcore.os.getpid();
656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657
658 /**
Romain Guy80b12fc2013-05-29 15:54:25 -0700659 * Returns the identifier of this process' parent.
660 * @hide
661 */
Elliott Hughes2a805f92013-07-31 18:25:47 -0700662 public static final int myPpid() {
663 return Libcore.os.getppid();
664 }
Romain Guy80b12fc2013-05-29 15:54:25 -0700665
666 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 * Returns the identifier of the calling thread, which be used with
668 * {@link #setThreadPriority(int, int)}.
669 */
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700670 public static final int myTid() {
671 return Libcore.os.gettid();
672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673
674 /**
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700675 * Returns the identifier of this process's uid. This is the kernel uid
676 * that the process is running under, which is the identity of its
677 * app-specific sandbox. It is different from {@link #myUserHandle} in that
678 * a uid identifies a specific app sandbox in a specific user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 */
Jeff Hao406ec152013-07-30 10:13:41 -0700680 public static final int myUid() {
681 return Libcore.os.getuid();
682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683
684 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700685 * Returns this process's user handle. This is the
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700686 * user the process is running under. It is distinct from
687 * {@link #myUid()} in that a particular user will have multiple
688 * distinct apps running under it each with their own uid.
689 */
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700690 public static final UserHandle myUserHandle() {
691 return new UserHandle(UserHandle.getUserId(myUid()));
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700692 }
693
694 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800695 * Returns whether the current process is in an isolated sandbox.
696 * @hide
697 */
698 public static final boolean isIsolated() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700699 int uid = UserHandle.getAppId(myUid());
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800700 return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID;
701 }
702
703 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 * Returns the UID assigned to a particular user name, or -1 if there is
705 * none. If the given string consists of only numbers, it is converted
706 * directly to a uid.
707 */
708 public static final native int getUidForName(String name);
709
710 /**
711 * Returns the GID assigned to a particular user name, or -1 if there is
712 * none. If the given string consists of only numbers, it is converted
713 * directly to a gid.
714 */
715 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700716
717 /**
718 * Returns a uid for a currently running process.
719 * @param pid the process id
720 * @return the uid of the process, or -1 if the process is not running.
721 * @hide pending API council review
722 */
723 public static final int getUidForPid(int pid) {
724 String[] procStatusLabels = { "Uid:" };
725 long[] procStatusValues = new long[1];
726 procStatusValues[0] = -1;
727 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
728 return (int) procStatusValues[0];
729 }
730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700732 * Returns the parent process id for a currently running process.
733 * @param pid the process id
734 * @return the parent process id of the process, or -1 if the process is not running.
735 * @hide
736 */
737 public static final int getParentPid(int pid) {
738 String[] procStatusLabels = { "PPid:" };
739 long[] procStatusValues = new long[1];
740 procStatusValues[0] = -1;
741 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
742 return (int) procStatusValues[0];
743 }
744
745 /**
Glenn Kasten07b04652012-04-23 15:00:43 -0700746 * Returns the thread group leader id for a currently running thread.
747 * @param tid the thread id
748 * @return the thread group leader id of the thread, or -1 if the thread is not running.
749 * This is same as what getpid(2) would return if called by tid.
750 * @hide
751 */
752 public static final int getThreadGroupLeader(int tid) {
753 String[] procStatusLabels = { "Tgid:" };
754 long[] procStatusValues = new long[1];
755 procStatusValues[0] = -1;
756 Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues);
757 return (int) procStatusValues[0];
758 }
759
760 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 * Set the priority of a thread, based on Linux priorities.
762 *
763 * @param tid The identifier of the thread/process to change.
764 * @param priority A Linux priority level, from -20 for highest scheduling
765 * priority to 19 for lowest scheduling priority.
766 *
767 * @throws IllegalArgumentException Throws IllegalArgumentException if
768 * <var>tid</var> does not exist.
769 * @throws SecurityException Throws SecurityException if your process does
770 * not have permission to modify the given thread, or to use the given
771 * priority.
772 */
773 public static final native void setThreadPriority(int tid, int priority)
774 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700775
776 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700777 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
778 * throw an exception if passed a background-level thread priority. This is only
779 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
780 *
781 * @hide
782 */
783 public static final native void setCanSelfBackground(boolean backgroundOk);
784
785 /**
San Mehate9d376b2009-04-21 14:06:36 -0700786 * Sets the scheduling group for a thread.
787 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700788 * @param tid The identifier of the thread to change.
789 * @param group The target group for this thread from THREAD_GROUP_*.
San Mehate9d376b2009-04-21 14:06:36 -0700790 *
791 * @throws IllegalArgumentException Throws IllegalArgumentException if
792 * <var>tid</var> does not exist.
793 * @throws SecurityException Throws SecurityException if your process does
794 * not have permission to modify the given thread, or to use the given
795 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700796 * If the thread is a thread group leader, that is it's gettid() == getpid(),
797 * then the other threads in the same thread group are _not_ affected.
San Mehate9d376b2009-04-21 14:06:36 -0700798 */
799 public static final native void setThreadGroup(int tid, int group)
800 throws IllegalArgumentException, SecurityException;
Glenn Kastenf1b56442012-03-15 16:33:43 -0700801
San Mehat3e458242009-05-19 14:44:16 -0700802 /**
803 * Sets the scheduling group for a process and all child threads
804 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700805 * @param pid The identifier of the process to change.
806 * @param group The target group for this process from THREAD_GROUP_*.
San Mehat3e458242009-05-19 14:44:16 -0700807 *
808 * @throws IllegalArgumentException Throws IllegalArgumentException if
809 * <var>tid</var> does not exist.
810 * @throws SecurityException Throws SecurityException if your process does
811 * not have permission to modify the given thread, or to use the given
812 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700813 *
814 * group == THREAD_GROUP_DEFAULT means to move all non-background priority
815 * threads to the foreground scheduling group, but to leave background
816 * priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all
817 * threads, regardless of priority, to the background scheduling group.
818 * group == THREAD_GROUP_FOREGROUND is not allowed.
San Mehat3e458242009-05-19 14:44:16 -0700819 */
820 public static final native void setProcessGroup(int pid, int group)
821 throws IllegalArgumentException, SecurityException;
Jeff Sharkey9e57c412013-01-17 14:12:41 -0800822
823 /**
824 * Return the scheduling group of requested process.
825 *
826 * @hide
827 */
828 public static final native int getProcessGroup(int pid)
829 throws IllegalArgumentException, SecurityException;
830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 /**
832 * Set the priority of the calling thread, based on Linux priorities. See
833 * {@link #setThreadPriority(int, int)} for more information.
834 *
835 * @param priority A Linux priority level, from -20 for highest scheduling
836 * priority to 19 for lowest scheduling priority.
837 *
838 * @throws IllegalArgumentException Throws IllegalArgumentException if
839 * <var>tid</var> does not exist.
840 * @throws SecurityException Throws SecurityException if your process does
841 * not have permission to modify the given thread, or to use the given
842 * priority.
843 *
844 * @see #setThreadPriority(int, int)
845 */
846 public static final native void setThreadPriority(int priority)
847 throws IllegalArgumentException, SecurityException;
848
849 /**
850 * Return the current priority of a thread, based on Linux priorities.
851 *
852 * @param tid The identifier of the thread/process to change.
853 *
854 * @return Returns the current priority, as a Linux priority level,
855 * from -20 for highest scheduling priority to 19 for lowest scheduling
856 * priority.
857 *
858 * @throws IllegalArgumentException Throws IllegalArgumentException if
859 * <var>tid</var> does not exist.
860 */
861 public static final native int getThreadPriority(int tid)
862 throws IllegalArgumentException;
863
864 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700865 * Set the scheduling policy and priority of a thread, based on Linux.
866 *
867 * @param tid The identifier of the thread/process to change.
868 * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
869 * @param priority A Linux priority level in a range appropriate for the given policy.
870 *
871 * @throws IllegalArgumentException Throws IllegalArgumentException if
872 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
873 * @throws SecurityException Throws SecurityException if your process does
874 * not have permission to modify the given thread, or to use the given
875 * scheduling policy or priority.
876 *
877 * {@hide}
878 */
879 public static final native void setThreadScheduler(int tid, int policy, int priority)
880 throws IllegalArgumentException;
881
882 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 * Determine whether the current environment supports multiple processes.
884 *
885 * @return Returns true if the system can run in multiple processes, else
886 * false if everything is running in a single process.
Jeff Brown10e89712011-07-08 18:52:57 -0700887 *
888 * @deprecated This method always returns true. Do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 */
Jeff Brown10e89712011-07-08 18:52:57 -0700890 @Deprecated
891 public static final boolean supportsProcesses() {
892 return true;
893 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894
895 /**
896 * Set the out-of-memory badness adjustment for a process.
897 *
898 * @param pid The process identifier to set.
899 * @param amt Adjustment value -- linux allows -16 to +15.
900 *
901 * @return Returns true if the underlying system supports this
902 * feature, else false.
903 *
904 * {@hide}
905 */
906 public static final native boolean setOomAdj(int pid, int amt);
907
908 /**
Rom Lemarchand5534ba92013-07-12 16:15:36 -0700909 * Adjust the swappiness level for a process.
910 *
911 * @param pid The process identifier to set.
912 * @param is_increased Whether swappiness should be increased or default.
913 *
914 * @return Returns true if the underlying system supports this
915 * feature, else false.
916 *
917 * {@hide}
918 */
919 public static final native boolean setSwappiness(int pid, boolean is_increased);
920
921 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 * Change this process's argv[0] parameter. This can be useful to show
923 * more descriptive information in things like the 'ps' command.
924 *
925 * @param text The new name of this process.
926 *
927 * {@hide}
928 */
929 public static final native void setArgV0(String text);
930
931 /**
932 * Kill the process with the given PID.
933 * Note that, though this API allows us to request to
934 * kill any process based on its PID, the kernel will
935 * still impose standard restrictions on which PIDs you
936 * are actually able to kill. Typically this means only
937 * the process running the caller's packages/application
938 * and any additional processes created by that app; packages
939 * sharing a common UID will also be able to kill each
940 * other's processes.
941 */
942 public static final void killProcess(int pid) {
943 sendSignal(pid, SIGNAL_KILL);
944 }
945
946 /** @hide */
947 public static final native int setUid(int uid);
948
949 /** @hide */
950 public static final native int setGid(int uid);
951
952 /**
953 * Send a signal to the given process.
954 *
955 * @param pid The pid of the target process.
956 * @param signal The signal to send.
957 */
958 public static final native void sendSignal(int pid, int signal);
959
Dianne Hackborn906497c2010-05-10 15:57:38 -0700960 /**
961 * @hide
962 * Private impl for avoiding a log message... DO NOT USE without doing
963 * your own log, or the Android Illuminati will find you some night and
964 * beat you up.
965 */
966 public static final void killProcessQuiet(int pid) {
967 sendSignalQuiet(pid, SIGNAL_KILL);
968 }
969
970 /**
971 * @hide
972 * Private impl for avoiding a log message... DO NOT USE without doing
973 * your own log, or the Android Illuminati will find you some night and
974 * beat you up.
975 */
976 public static final native void sendSignalQuiet(int pid, int signal);
977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 /** @hide */
Marco Nelissen0bca96b2009-07-17 12:59:25 -0700979 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980
981 /** @hide */
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700982 public static final native long getTotalMemory();
983
984 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 public static final native void readProcLines(String path,
986 String[] reqFields, long[] outSizes);
987
988 /** @hide */
989 public static final native int[] getPids(String path, int[] lastArray);
990
991 /** @hide */
992 public static final int PROC_TERM_MASK = 0xff;
993 /** @hide */
994 public static final int PROC_ZERO_TERM = 0;
995 /** @hide */
996 public static final int PROC_SPACE_TERM = (int)' ';
997 /** @hide */
Evan Millarc64edde2009-04-18 12:26:32 -0700998 public static final int PROC_TAB_TERM = (int)'\t';
999 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 public static final int PROC_COMBINE = 0x100;
1001 /** @hide */
1002 public static final int PROC_PARENS = 0x200;
1003 /** @hide */
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001004 public static final int PROC_QUOTES = 0x400;
1005 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 public static final int PROC_OUT_STRING = 0x1000;
1007 /** @hide */
1008 public static final int PROC_OUT_LONG = 0x2000;
1009 /** @hide */
1010 public static final int PROC_OUT_FLOAT = 0x4000;
1011
1012 /** @hide */
1013 public static final native boolean readProcFile(String file, int[] format,
1014 String[] outStrings, long[] outLongs, float[] outFloats);
Evan Millarc64edde2009-04-18 12:26:32 -07001015
1016 /** @hide */
1017 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
1018 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001020 /** @hide */
1021 public static final native int[] getPidsForCommands(String[] cmds);
1022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 /**
1024 * Gets the total Pss value for a given process, in bytes.
1025 *
1026 * @param pid the process to the Pss for
1027 * @return the total Pss value for the given process in bytes,
1028 * or -1 if the value cannot be determined
1029 * @hide
1030 */
1031 public static final native long getPss(int pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -07001032
1033 /**
1034 * Specifies the outcome of having started a process.
1035 * @hide
1036 */
1037 public static final class ProcessStartResult {
1038 /**
1039 * The PID of the newly started process.
1040 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
1041 */
1042 public int pid;
1043
1044 /**
1045 * True if the process was started with a wrapper attached.
1046 */
1047 public boolean usingWrapper;
1048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049}