blob: 476b4ea8d8cfe0f6dc09f5063778f7898a170243 [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
19import android.net.LocalSocketAddress;
20import android.net.LocalSocket;
21import android.util.Log;
22import dalvik.system.Zygote;
23
24import java.io.BufferedWriter;
25import java.io.DataInputStream;
26import java.io.IOException;
27import java.io.OutputStreamWriter;
28import java.util.ArrayList;
29
30/*package*/ class ZygoteStartFailedEx extends Exception {
31 /**
32 * Something prevented the zygote process startup from happening normally
33 */
34
35 ZygoteStartFailedEx() {};
36 ZygoteStartFailedEx(String s) {super(s);}
37 ZygoteStartFailedEx(Throwable cause) {super(cause);}
38}
39
40/**
41 * Tools for managing OS processes.
42 */
43public class Process {
44 private static final String LOG_TAG = "Process";
45
46 private static final String ZYGOTE_SOCKET = "zygote";
47
48 /**
49 * Name of a process for running the platform's media services.
50 * {@hide}
51 */
52 public static final String ANDROID_SHARED_MEDIA = "com.android.process.media";
53
54 /**
55 * Name of the process that Google content providers can share.
56 * {@hide}
57 */
58 public static final String GOOGLE_SHARED_APP_CONTENT = "com.google.process.content";
59
60 /**
61 * Defines the UID/GID under which system code runs.
62 */
63 public static final int SYSTEM_UID = 1000;
64
65 /**
66 * Defines the UID/GID under which the telephony code runs.
67 */
68 public static final int PHONE_UID = 1001;
69
70 /**
Dianne Hackborn854060af2009-07-09 18:14:31 -070071 * Defines the UID/GID for the user shell.
72 * @hide
73 */
74 public static final int SHELL_UID = 2000;
75
76 /**
Mike Lockwoodd42685d2009-09-03 09:25:22 -040077 * Defines the UID/GID for the log group.
78 * @hide
79 */
80 public static final int LOG_UID = 1007;
81
82 /**
Amith Yamasanid1582142009-07-08 20:04:55 -070083 * Defines the UID/GID for the WIFI supplicant process.
84 * @hide
85 */
86 public static final int WIFI_UID = 1010;
87
88 /**
Glenn Kasten8b7d1b42011-07-13 16:23:22 -070089 * Defines the UID/GID for the mediaserver process.
90 * @hide
91 */
92 public static final int MEDIA_UID = 1013;
93
94 /**
Jeff Sharkey5294a2f2012-04-24 17:07:22 -070095 * Defines the UID/GID for the DRM process.
96 * @hide
97 */
98 public static final int DRM_UID = 1019;
99
100 /**
Mike Lockwood58fd98a2010-09-24 11:02:47 -0400101 * Defines the GID for the group that allows write access to the SD card.
102 * @hide
103 */
104 public static final int SDCARD_RW_GID = 1015;
105
106 /**
Kenny Root26993b32012-03-19 15:07:51 -0700107 * Defines the UID/GID for the group that controls VPN services.
108 * @hide
109 */
110 public static final int VPN_UID = 1016;
111
112 /**
Nick Pellycd0e8392010-10-13 17:25:24 -0700113 * Defines the UID/GID for the NFC service process.
114 * @hide
115 */
Nick Pellya5cb9f42011-11-21 14:54:46 -0800116 public static final int NFC_UID = 1027;
Nick Pellycd0e8392010-10-13 17:25:24 -0700117
118 /**
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800119 * Defines the UID/GID for the Bluetooth service process.
120 * @hide
121 */
122 public static final int BLUETOOTH_UID = 1002;
123
124 /**
Mike Lockwooddcaa10c2010-12-16 12:50:44 -0800125 * Defines the GID for the group that allows write access to the internal media storage.
126 * @hide
127 */
128 public static final int MEDIA_RW_GID = 1023;
129
130 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 * Defines the start of a range of UIDs (and GIDs), going from this
132 * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning
133 * to applications.
134 */
135 public static final int FIRST_APPLICATION_UID = 10000;
136 /**
137 * Last of application-specific UIDs starting at
138 * {@link #FIRST_APPLICATION_UID}.
139 */
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800140 public static final int LAST_APPLICATION_UID = 19999;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800141
142 /**
143 * First uid used for fully isolated sandboxed processes (with no permissions of their own)
144 * @hide
145 */
146 public static final int FIRST_ISOLATED_UID = 99000;
147
148 /**
149 * Last uid used for fully isolated sandboxed processes (with no permissions of their own)
150 * @hide
151 */
152 public static final int LAST_ISOLATED_UID = 99999;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153
154 /**
Kenny Roote091f222012-09-11 15:01:26 -0700155 * First gid for applications to share resources. Used when forward-locking
156 * is enabled but all UserHandles need to be able to read the resources.
157 * @hide
158 */
159 public static final int FIRST_SHARED_APPLICATION_GID = 50000;
160
161 /**
162 * Last gid for applications to share resources. Used when forward-locking
163 * is enabled but all UserHandles need to be able to read the resources.
164 * @hide
165 */
166 public static final int LAST_SHARED_APPLICATION_GID = 59999;
167
168 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 * Standard priority of application threads.
170 * Use with {@link #setThreadPriority(int)} and
171 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
172 * {@link java.lang.Thread} class.
173 */
174 public static final int THREAD_PRIORITY_DEFAULT = 0;
175
176 /*
177 * ***************************************
178 * ** Keep in sync with utils/threads.h **
179 * ***************************************
180 */
181
182 /**
183 * Lowest available thread priority. Only for those who really, really
184 * don't want to run if anything else is happening.
185 * Use with {@link #setThreadPriority(int)} and
186 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
187 * {@link java.lang.Thread} class.
188 */
189 public static final int THREAD_PRIORITY_LOWEST = 19;
190
191 /**
192 * Standard priority background threads. This gives your thread a slightly
193 * lower than normal priority, so that it will have less chance of impacting
194 * the responsiveness of the user interface.
195 * Use with {@link #setThreadPriority(int)} and
196 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
197 * {@link java.lang.Thread} class.
198 */
199 public static final int THREAD_PRIORITY_BACKGROUND = 10;
200
201 /**
202 * Standard priority of threads that are currently running a user interface
203 * that the user is interacting with. Applications can not normally
204 * change to this priority; the system will automatically adjust your
205 * application threads as the user moves through the UI.
206 * Use with {@link #setThreadPriority(int)} and
207 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
208 * {@link java.lang.Thread} class.
209 */
210 public static final int THREAD_PRIORITY_FOREGROUND = -2;
211
212 /**
213 * Standard priority of system display threads, involved in updating
214 * the user interface. Applications can not
215 * normally change to this priority.
216 * Use with {@link #setThreadPriority(int)} and
217 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
218 * {@link java.lang.Thread} class.
219 */
220 public static final int THREAD_PRIORITY_DISPLAY = -4;
221
222 /**
223 * Standard priority of the most important display threads, for compositing
224 * the screen and retrieving input events. Applications can not normally
225 * change to this priority.
226 * Use with {@link #setThreadPriority(int)} and
227 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
228 * {@link java.lang.Thread} class.
229 */
230 public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8;
231
232 /**
233 * Standard priority of audio threads. Applications can not normally
234 * change to this priority.
235 * Use with {@link #setThreadPriority(int)} and
236 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
237 * {@link java.lang.Thread} class.
238 */
239 public static final int THREAD_PRIORITY_AUDIO = -16;
240
241 /**
242 * Standard priority of the most important audio threads.
243 * Applications can not normally change to this priority.
244 * Use with {@link #setThreadPriority(int)} and
245 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
246 * {@link java.lang.Thread} class.
247 */
248 public static final int THREAD_PRIORITY_URGENT_AUDIO = -19;
249
250 /**
251 * Minimum increment to make a priority more favorable.
252 */
253 public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
254
255 /**
256 * Minimum increment to make a priority less favorable.
257 */
258 public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
259
San Mehate9d376b2009-04-21 14:06:36 -0700260 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700261 * Default scheduling policy
262 * @hide
263 */
264 public static final int SCHED_OTHER = 0;
265
266 /**
267 * First-In First-Out scheduling policy
268 * @hide
269 */
270 public static final int SCHED_FIFO = 1;
271
272 /**
273 * Round-Robin scheduling policy
274 * @hide
275 */
276 public static final int SCHED_RR = 2;
277
278 /**
279 * Batch scheduling policy
280 * @hide
281 */
282 public static final int SCHED_BATCH = 3;
283
284 /**
285 * Idle scheduling policy
286 * @hide
287 */
288 public static final int SCHED_IDLE = 5;
289
Glenn Kastenf1b56442012-03-15 16:33:43 -0700290 // Keep in sync with SP_* constants of enum type SchedPolicy
291 // declared in system/core/include/cutils/sched_policy.h,
292 // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
San Mehate9d376b2009-04-21 14:06:36 -0700293
294 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700295 * Default thread group -
296 * has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
297 * When used with setProcessGroup(), the group of each thread in the process
298 * is conditionally changed based on that thread's current priority, as follows:
299 * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
300 * are moved to foreground thread group. All other threads are left unchanged.
301 * @hide
302 */
303 public static final int THREAD_GROUP_DEFAULT = -1;
304
305 /**
306 * Background thread group - All threads in
San Mehate9d376b2009-04-21 14:06:36 -0700307 * this group are scheduled with a reduced share of the CPU.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700308 * Value is same as constant SP_BACKGROUND of enum SchedPolicy.
309 * FIXME rename to THREAD_GROUP_BACKGROUND.
San Mehate9d376b2009-04-21 14:06:36 -0700310 * @hide
311 */
Glenn Kastenf1b56442012-03-15 16:33:43 -0700312 public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0;
San Mehate9d376b2009-04-21 14:06:36 -0700313
314 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700315 * Foreground thread group - All threads in
316 * this group are scheduled with a normal share of the CPU.
317 * Value is same as constant SP_FOREGROUND of enum SchedPolicy.
318 * Not used at this level.
San Mehate9d376b2009-04-21 14:06:36 -0700319 * @hide
320 **/
Glenn Kastenf1b56442012-03-15 16:33:43 -0700321 private static final int THREAD_GROUP_FOREGROUND = 1;
San Mehate9d376b2009-04-21 14:06:36 -0700322
Glenn Kasten07b04652012-04-23 15:00:43 -0700323 /**
324 * System thread group.
325 * @hide
326 **/
327 public static final int THREAD_GROUP_SYSTEM = 2;
328
329 /**
330 * Application audio thread group.
331 * @hide
332 **/
333 public static final int THREAD_GROUP_AUDIO_APP = 3;
334
335 /**
336 * System audio thread group.
337 * @hide
338 **/
339 public static final int THREAD_GROUP_AUDIO_SYS = 4;
340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 public static final int SIGNAL_QUIT = 3;
342 public static final int SIGNAL_KILL = 9;
343 public static final int SIGNAL_USR1 = 10;
344
345 // State for communicating with zygote process
346
347 static LocalSocket sZygoteSocket;
348 static DataInputStream sZygoteInputStream;
349 static BufferedWriter sZygoteWriter;
350
351 /** true if previous zygote open failed */
352 static boolean sPreviousZygoteOpenFailed;
353
354 /**
355 * Start a new process.
356 *
357 * <p>If processes are enabled, a new process is created and the
358 * static main() function of a <var>processClass</var> is executed there.
359 * The process will continue running after this function returns.
360 *
361 * <p>If processes are not enabled, a new thread in the caller's
362 * process is created and main() of <var>processClass</var> called there.
363 *
364 * <p>The niceName parameter, if not an empty string, is a custom name to
365 * give to the process instead of using processClass. This allows you to
366 * make easily identifyable processes even if you are using the same base
367 * <var>processClass</var> to start them.
368 *
369 * @param processClass The class to use as the process's main entry
370 * point.
371 * @param niceName A more readable name to use for the process.
372 * @param uid The user-id under which the process will run.
373 * @param gid The group-id under which the process will run.
374 * @param gids Additional group-ids associated with the process.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700375 * @param debugFlags Additional flags.
376 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500377 * @param seInfo null-ok SE Android information for the new process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 * @param zygoteArgs Additional arguments to supply to the zygote process.
379 *
Jeff Brown3f9dd282011-07-08 20:02:19 -0700380 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 * @throws RuntimeException on fatal start failure
382 *
383 * {@hide}
384 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700385 public static final ProcessStartResult start(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 final String niceName,
387 int uid, int gid, int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700388 int debugFlags, int mountExternal,
389 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500390 String seInfo,
Jeff Brown10e89712011-07-08 18:52:57 -0700391 String[] zygoteArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 try {
Jeff Brown10e89712011-07-08 18:52:57 -0700393 return startViaZygote(processClass, niceName, uid, gid, gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700394 debugFlags, mountExternal, targetSdkVersion, seInfo, zygoteArgs);
Jeff Brown10e89712011-07-08 18:52:57 -0700395 } catch (ZygoteStartFailedEx ex) {
396 Log.e(LOG_TAG,
397 "Starting VM process through Zygote failed");
398 throw new RuntimeException(
399 "Starting VM process through Zygote failed", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 }
402
403 /** retry interval for opening a zygote socket */
404 static final int ZYGOTE_RETRY_MILLIS = 500;
405
406 /**
407 * Tries to open socket to Zygote process if not already open. If
408 * already open, does nothing. May block and retry.
409 */
410 private static void openZygoteSocketIfNeeded()
411 throws ZygoteStartFailedEx {
412
413 int retryCount;
414
415 if (sPreviousZygoteOpenFailed) {
416 /*
417 * If we've failed before, expect that we'll fail again and
418 * don't pause for retries.
419 */
420 retryCount = 0;
421 } else {
422 retryCount = 10;
423 }
424
425 /*
426 * See bug #811181: Sometimes runtime can make it up before zygote.
427 * Really, we'd like to do something better to avoid this condition,
428 * but for now just wait a bit...
429 */
430 for (int retry = 0
431 ; (sZygoteSocket == null) && (retry < (retryCount + 1))
432 ; retry++ ) {
433
434 if (retry > 0) {
435 try {
436 Log.i("Zygote", "Zygote not up yet, sleeping...");
437 Thread.sleep(ZYGOTE_RETRY_MILLIS);
438 } catch (InterruptedException ex) {
439 // should never happen
440 }
441 }
442
443 try {
444 sZygoteSocket = new LocalSocket();
445
446 sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET,
447 LocalSocketAddress.Namespace.RESERVED));
448
449 sZygoteInputStream
450 = new DataInputStream(sZygoteSocket.getInputStream());
451
452 sZygoteWriter =
453 new BufferedWriter(
454 new OutputStreamWriter(
455 sZygoteSocket.getOutputStream()),
456 256);
457
458 Log.i("Zygote", "Process: zygote socket opened");
459
460 sPreviousZygoteOpenFailed = false;
461 break;
462 } catch (IOException ex) {
463 if (sZygoteSocket != null) {
464 try {
465 sZygoteSocket.close();
466 } catch (IOException ex2) {
467 Log.e(LOG_TAG,"I/O exception on close after exception",
468 ex2);
469 }
470 }
471
472 sZygoteSocket = null;
473 }
474 }
475
476 if (sZygoteSocket == null) {
477 sPreviousZygoteOpenFailed = true;
478 throw new ZygoteStartFailedEx("connect failed");
479 }
480 }
481
482 /**
483 * Sends an argument list to the zygote process, which starts a new child
484 * and returns the child's pid. Please note: the present implementation
485 * replaces newlines in the argument list with spaces.
486 * @param args argument list
Jeff Brown3f9dd282011-07-08 20:02:19 -0700487 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 * @throws ZygoteStartFailedEx if process start failed for any reason
489 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700490 private static ProcessStartResult zygoteSendArgsAndGetResult(ArrayList<String> args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 openZygoteSocketIfNeeded();
493
494 try {
495 /**
496 * See com.android.internal.os.ZygoteInit.readArgumentList()
497 * Presently the wire format to the zygote process is:
498 * a) a count of arguments (argc, in essence)
499 * b) a number of newline-separated argument strings equal to count
500 *
501 * After the zygote process reads these it will write the pid of
Jeff Brown3f9dd282011-07-08 20:02:19 -0700502 * the child or -1 on failure, followed by boolean to
503 * indicate whether a wrapper process was used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 */
505
506 sZygoteWriter.write(Integer.toString(args.size()));
507 sZygoteWriter.newLine();
508
509 int sz = args.size();
510 for (int i = 0; i < sz; i++) {
511 String arg = args.get(i);
512 if (arg.indexOf('\n') >= 0) {
513 throw new ZygoteStartFailedEx(
514 "embedded newlines not allowed");
515 }
516 sZygoteWriter.write(arg);
517 sZygoteWriter.newLine();
518 }
519
520 sZygoteWriter.flush();
521
522 // Should there be a timeout on this?
Jeff Brown3f9dd282011-07-08 20:02:19 -0700523 ProcessStartResult result = new ProcessStartResult();
524 result.pid = sZygoteInputStream.readInt();
525 if (result.pid < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 throw new ZygoteStartFailedEx("fork() failed");
527 }
Jeff Brown3f9dd282011-07-08 20:02:19 -0700528 result.usingWrapper = sZygoteInputStream.readBoolean();
529 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 } catch (IOException ex) {
531 try {
532 if (sZygoteSocket != null) {
533 sZygoteSocket.close();
534 }
535 } catch (IOException ex2) {
536 // we're going to fail anyway
537 Log.e(LOG_TAG,"I/O exception on routine close", ex2);
538 }
539
540 sZygoteSocket = null;
541
542 throw new ZygoteStartFailedEx(ex);
543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 }
545
546 /**
547 * Starts a new process via the zygote mechanism.
548 *
549 * @param processClass Class name whose static main() to run
550 * @param niceName 'nice' process name to appear in ps
551 * @param uid a POSIX uid that the new process should setuid() to
552 * @param gid a POSIX gid that the new process shuold setgid() to
553 * @param gids null-ok; a list of supplementary group IDs that the
554 * new process should setgroup() to.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700555 * @param debugFlags Additional flags.
556 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500557 * @param seInfo null-ok SE Android information for the new process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 * @param extraArgs Additional arguments to supply to the zygote process.
Jeff Brown3f9dd282011-07-08 20:02:19 -0700559 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 * @throws ZygoteStartFailedEx if process start failed for any reason
561 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700562 private static ProcessStartResult startViaZygote(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 final String niceName,
564 final int uid, final int gid,
565 final int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700566 int debugFlags, int mountExternal,
567 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500568 String seInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 String[] extraArgs)
570 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 synchronized(Process.class) {
572 ArrayList<String> argsForZygote = new ArrayList<String>();
573
574 // --runtime-init, --setuid=, --setgid=,
575 // and --setgroups= must go first
576 argsForZygote.add("--runtime-init");
577 argsForZygote.add("--setuid=" + uid);
578 argsForZygote.add("--setgid=" + gid);
Elliott Hughesae07ecf2011-07-06 17:33:27 -0700579 if ((debugFlags & Zygote.DEBUG_ENABLE_JNI_LOGGING) != 0) {
580 argsForZygote.add("--enable-jni-logging");
581 }
Ben Cheng23085b72010-02-08 16:06:32 -0800582 if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) {
583 argsForZygote.add("--enable-safemode");
584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) {
586 argsForZygote.add("--enable-debugger");
587 }
588 if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) {
589 argsForZygote.add("--enable-checkjni");
590 }
591 if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) {
592 argsForZygote.add("--enable-assert");
593 }
Jeff Sharkey2bca8682012-08-22 13:59:58 -0700594 if (mountExternal == Zygote.MOUNT_EXTERNAL_MULTIUSER) {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700595 argsForZygote.add("--mount-external-multiuser");
Jeff Sharkeye217ee42012-08-28 16:23:01 -0700596 } else if (mountExternal == Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL) {
597 argsForZygote.add("--mount-external-multiuser-all");
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700598 }
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700599 argsForZygote.add("--target-sdk-version=" + targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600
601 //TODO optionally enable debuger
602 //argsForZygote.add("--enable-debugger");
603
604 // --setgroups is a comma-separated list
605 if (gids != null && gids.length > 0) {
606 StringBuilder sb = new StringBuilder();
607 sb.append("--setgroups=");
608
609 int sz = gids.length;
610 for (int i = 0; i < sz; i++) {
611 if (i != 0) {
612 sb.append(',');
613 }
614 sb.append(gids[i]);
615 }
616
617 argsForZygote.add(sb.toString());
618 }
619
620 if (niceName != null) {
621 argsForZygote.add("--nice-name=" + niceName);
622 }
623
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500624 if (seInfo != null) {
625 argsForZygote.add("--seinfo=" + seInfo);
626 }
627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 argsForZygote.add(processClass);
629
630 if (extraArgs != null) {
631 for (String arg : extraArgs) {
632 argsForZygote.add(arg);
633 }
634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635
Jeff Brown3f9dd282011-07-08 20:02:19 -0700636 return zygoteSendArgsAndGetResult(argsForZygote);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 }
639
640 /**
641 * Returns elapsed milliseconds of the time this process has run.
642 * @return Returns the number of milliseconds this process has return.
643 */
644 public static final native long getElapsedCpuTime();
645
646 /**
647 * Returns the identifier of this process, which can be used with
648 * {@link #killProcess} and {@link #sendSignal}.
649 */
650 public static final native int myPid();
651
652 /**
653 * Returns the identifier of the calling thread, which be used with
654 * {@link #setThreadPriority(int, int)}.
655 */
656 public static final native int myTid();
657
658 /**
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700659 * Returns the identifier of this process's uid. This is the kernel uid
660 * that the process is running under, which is the identity of its
661 * app-specific sandbox. It is different from {@link #myUserHandle} in that
662 * a uid identifies a specific app sandbox in a specific user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 */
664 public static final native int myUid();
665
666 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700667 * Returns this process's user handle. This is the
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700668 * user the process is running under. It is distinct from
669 * {@link #myUid()} in that a particular user will have multiple
670 * distinct apps running under it each with their own uid.
671 */
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700672 public static final UserHandle myUserHandle() {
673 return new UserHandle(UserHandle.getUserId(myUid()));
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700674 }
675
676 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800677 * Returns whether the current process is in an isolated sandbox.
678 * @hide
679 */
680 public static final boolean isIsolated() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700681 int uid = UserHandle.getAppId(myUid());
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800682 return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID;
683 }
684
685 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 * Returns the UID assigned to a particular user name, or -1 if there is
687 * none. If the given string consists of only numbers, it is converted
688 * directly to a uid.
689 */
690 public static final native int getUidForName(String name);
691
692 /**
693 * Returns the GID assigned to a particular user name, or -1 if there is
694 * none. If the given string consists of only numbers, it is converted
695 * directly to a gid.
696 */
697 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700698
699 /**
700 * Returns a uid for a currently running process.
701 * @param pid the process id
702 * @return the uid of the process, or -1 if the process is not running.
703 * @hide pending API council review
704 */
705 public static final int getUidForPid(int pid) {
706 String[] procStatusLabels = { "Uid:" };
707 long[] procStatusValues = new long[1];
708 procStatusValues[0] = -1;
709 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
710 return (int) procStatusValues[0];
711 }
712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700714 * Returns the parent process id for a currently running process.
715 * @param pid the process id
716 * @return the parent process id of the process, or -1 if the process is not running.
717 * @hide
718 */
719 public static final int getParentPid(int pid) {
720 String[] procStatusLabels = { "PPid:" };
721 long[] procStatusValues = new long[1];
722 procStatusValues[0] = -1;
723 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
724 return (int) procStatusValues[0];
725 }
726
727 /**
Glenn Kasten07b04652012-04-23 15:00:43 -0700728 * Returns the thread group leader id for a currently running thread.
729 * @param tid the thread id
730 * @return the thread group leader id of the thread, or -1 if the thread is not running.
731 * This is same as what getpid(2) would return if called by tid.
732 * @hide
733 */
734 public static final int getThreadGroupLeader(int tid) {
735 String[] procStatusLabels = { "Tgid:" };
736 long[] procStatusValues = new long[1];
737 procStatusValues[0] = -1;
738 Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues);
739 return (int) procStatusValues[0];
740 }
741
742 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 * Set the priority of a thread, based on Linux priorities.
744 *
745 * @param tid The identifier of the thread/process to change.
746 * @param priority A Linux priority level, from -20 for highest scheduling
747 * priority to 19 for lowest scheduling priority.
748 *
749 * @throws IllegalArgumentException Throws IllegalArgumentException if
750 * <var>tid</var> does not exist.
751 * @throws SecurityException Throws SecurityException if your process does
752 * not have permission to modify the given thread, or to use the given
753 * priority.
754 */
755 public static final native void setThreadPriority(int tid, int priority)
756 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700757
758 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700759 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
760 * throw an exception if passed a background-level thread priority. This is only
761 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
762 *
763 * @hide
764 */
765 public static final native void setCanSelfBackground(boolean backgroundOk);
766
767 /**
San Mehate9d376b2009-04-21 14:06:36 -0700768 * Sets the scheduling group for a thread.
769 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700770 * @param tid The identifier of the thread to change.
771 * @param group The target group for this thread from THREAD_GROUP_*.
San Mehate9d376b2009-04-21 14:06:36 -0700772 *
773 * @throws IllegalArgumentException Throws IllegalArgumentException if
774 * <var>tid</var> does not exist.
775 * @throws SecurityException Throws SecurityException if your process does
776 * not have permission to modify the given thread, or to use the given
777 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700778 * If the thread is a thread group leader, that is it's gettid() == getpid(),
779 * then the other threads in the same thread group are _not_ affected.
San Mehate9d376b2009-04-21 14:06:36 -0700780 */
781 public static final native void setThreadGroup(int tid, int group)
782 throws IllegalArgumentException, SecurityException;
Glenn Kastenf1b56442012-03-15 16:33:43 -0700783
San Mehat3e458242009-05-19 14:44:16 -0700784 /**
785 * Sets the scheduling group for a process and all child threads
786 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700787 * @param pid The identifier of the process to change.
788 * @param group The target group for this process from THREAD_GROUP_*.
San Mehat3e458242009-05-19 14:44:16 -0700789 *
790 * @throws IllegalArgumentException Throws IllegalArgumentException if
791 * <var>tid</var> does not exist.
792 * @throws SecurityException Throws SecurityException if your process does
793 * not have permission to modify the given thread, or to use the given
794 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700795 *
796 * group == THREAD_GROUP_DEFAULT means to move all non-background priority
797 * threads to the foreground scheduling group, but to leave background
798 * priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all
799 * threads, regardless of priority, to the background scheduling group.
800 * group == THREAD_GROUP_FOREGROUND is not allowed.
San Mehat3e458242009-05-19 14:44:16 -0700801 */
802 public static final native void setProcessGroup(int pid, int group)
803 throws IllegalArgumentException, SecurityException;
Jeff Sharkey9e57c412013-01-17 14:12:41 -0800804
805 /**
806 * Return the scheduling group of requested process.
807 *
808 * @hide
809 */
810 public static final native int getProcessGroup(int pid)
811 throws IllegalArgumentException, SecurityException;
812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 /**
814 * Set the priority of the calling thread, based on Linux priorities. See
815 * {@link #setThreadPriority(int, int)} for more information.
816 *
817 * @param priority A Linux priority level, from -20 for highest scheduling
818 * priority to 19 for lowest scheduling priority.
819 *
820 * @throws IllegalArgumentException Throws IllegalArgumentException if
821 * <var>tid</var> does not exist.
822 * @throws SecurityException Throws SecurityException if your process does
823 * not have permission to modify the given thread, or to use the given
824 * priority.
825 *
826 * @see #setThreadPriority(int, int)
827 */
828 public static final native void setThreadPriority(int priority)
829 throws IllegalArgumentException, SecurityException;
830
831 /**
832 * Return the current priority of a thread, based on Linux priorities.
833 *
834 * @param tid The identifier of the thread/process to change.
835 *
836 * @return Returns the current priority, as a Linux priority level,
837 * from -20 for highest scheduling priority to 19 for lowest scheduling
838 * priority.
839 *
840 * @throws IllegalArgumentException Throws IllegalArgumentException if
841 * <var>tid</var> does not exist.
842 */
843 public static final native int getThreadPriority(int tid)
844 throws IllegalArgumentException;
845
846 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700847 * Set the scheduling policy and priority of a thread, based on Linux.
848 *
849 * @param tid The identifier of the thread/process to change.
850 * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
851 * @param priority A Linux priority level in a range appropriate for the given policy.
852 *
853 * @throws IllegalArgumentException Throws IllegalArgumentException if
854 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
855 * @throws SecurityException Throws SecurityException if your process does
856 * not have permission to modify the given thread, or to use the given
857 * scheduling policy or priority.
858 *
859 * {@hide}
860 */
861 public static final native void setThreadScheduler(int tid, int policy, int priority)
862 throws IllegalArgumentException;
863
864 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 * Determine whether the current environment supports multiple processes.
866 *
867 * @return Returns true if the system can run in multiple processes, else
868 * false if everything is running in a single process.
Jeff Brown10e89712011-07-08 18:52:57 -0700869 *
870 * @deprecated This method always returns true. Do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 */
Jeff Brown10e89712011-07-08 18:52:57 -0700872 @Deprecated
873 public static final boolean supportsProcesses() {
874 return true;
875 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876
877 /**
878 * Set the out-of-memory badness adjustment for a process.
879 *
880 * @param pid The process identifier to set.
881 * @param amt Adjustment value -- linux allows -16 to +15.
882 *
883 * @return Returns true if the underlying system supports this
884 * feature, else false.
885 *
886 * {@hide}
887 */
888 public static final native boolean setOomAdj(int pid, int amt);
889
890 /**
891 * Change this process's argv[0] parameter. This can be useful to show
892 * more descriptive information in things like the 'ps' command.
893 *
894 * @param text The new name of this process.
895 *
896 * {@hide}
897 */
898 public static final native void setArgV0(String text);
899
900 /**
901 * Kill the process with the given PID.
902 * Note that, though this API allows us to request to
903 * kill any process based on its PID, the kernel will
904 * still impose standard restrictions on which PIDs you
905 * are actually able to kill. Typically this means only
906 * the process running the caller's packages/application
907 * and any additional processes created by that app; packages
908 * sharing a common UID will also be able to kill each
909 * other's processes.
910 */
911 public static final void killProcess(int pid) {
912 sendSignal(pid, SIGNAL_KILL);
913 }
914
915 /** @hide */
916 public static final native int setUid(int uid);
917
918 /** @hide */
919 public static final native int setGid(int uid);
920
921 /**
922 * Send a signal to the given process.
923 *
924 * @param pid The pid of the target process.
925 * @param signal The signal to send.
926 */
927 public static final native void sendSignal(int pid, int signal);
928
Dianne Hackborn906497c2010-05-10 15:57:38 -0700929 /**
930 * @hide
931 * Private impl for avoiding a log message... DO NOT USE without doing
932 * your own log, or the Android Illuminati will find you some night and
933 * beat you up.
934 */
935 public static final void killProcessQuiet(int pid) {
936 sendSignalQuiet(pid, SIGNAL_KILL);
937 }
938
939 /**
940 * @hide
941 * Private impl for avoiding a log message... DO NOT USE without doing
942 * your own log, or the Android Illuminati will find you some night and
943 * beat you up.
944 */
945 public static final native void sendSignalQuiet(int pid, int signal);
946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 /** @hide */
Marco Nelissen0bca96b2009-07-17 12:59:25 -0700948 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949
950 /** @hide */
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700951 public static final native long getTotalMemory();
952
953 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 public static final native void readProcLines(String path,
955 String[] reqFields, long[] outSizes);
956
957 /** @hide */
958 public static final native int[] getPids(String path, int[] lastArray);
959
960 /** @hide */
961 public static final int PROC_TERM_MASK = 0xff;
962 /** @hide */
963 public static final int PROC_ZERO_TERM = 0;
964 /** @hide */
965 public static final int PROC_SPACE_TERM = (int)' ';
966 /** @hide */
Evan Millarc64edde2009-04-18 12:26:32 -0700967 public static final int PROC_TAB_TERM = (int)'\t';
968 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 public static final int PROC_COMBINE = 0x100;
970 /** @hide */
971 public static final int PROC_PARENS = 0x200;
972 /** @hide */
973 public static final int PROC_OUT_STRING = 0x1000;
974 /** @hide */
975 public static final int PROC_OUT_LONG = 0x2000;
976 /** @hide */
977 public static final int PROC_OUT_FLOAT = 0x4000;
978
979 /** @hide */
980 public static final native boolean readProcFile(String file, int[] format,
981 String[] outStrings, long[] outLongs, float[] outFloats);
Evan Millarc64edde2009-04-18 12:26:32 -0700982
983 /** @hide */
984 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
985 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700987 /** @hide */
988 public static final native int[] getPidsForCommands(String[] cmds);
989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 /**
991 * Gets the total Pss value for a given process, in bytes.
992 *
993 * @param pid the process to the Pss for
994 * @return the total Pss value for the given process in bytes,
995 * or -1 if the value cannot be determined
996 * @hide
997 */
998 public static final native long getPss(int pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -0700999
1000 /**
1001 * Specifies the outcome of having started a process.
1002 * @hide
1003 */
1004 public static final class ProcessStartResult {
1005 /**
1006 * The PID of the newly started process.
1007 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
1008 */
1009 public int pid;
1010
1011 /**
1012 * True if the process was started with a wrapper attached.
1013 */
1014 public boolean usingWrapper;
1015 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016}