blob: facab4c8e2a7152498ceee5d1ef24d9642fed662 [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 * Defines a secondary group id for access to the bluetooth hardware.
170 */
171 public static final int BLUETOOTH_GID = 2000;
Kenny Roote091f222012-09-11 15:01:26 -0700172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 /**
174 * Standard priority of application threads.
175 * Use with {@link #setThreadPriority(int)} and
176 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
177 * {@link java.lang.Thread} class.
178 */
179 public static final int THREAD_PRIORITY_DEFAULT = 0;
180
181 /*
182 * ***************************************
183 * ** Keep in sync with utils/threads.h **
184 * ***************************************
185 */
186
187 /**
188 * Lowest available thread priority. Only for those who really, really
189 * don't want to run if anything else is happening.
190 * Use with {@link #setThreadPriority(int)} and
191 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
192 * {@link java.lang.Thread} class.
193 */
194 public static final int THREAD_PRIORITY_LOWEST = 19;
195
196 /**
197 * Standard priority background threads. This gives your thread a slightly
198 * lower than normal priority, so that it will have less chance of impacting
199 * the responsiveness of the user interface.
200 * Use with {@link #setThreadPriority(int)} and
201 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
202 * {@link java.lang.Thread} class.
203 */
204 public static final int THREAD_PRIORITY_BACKGROUND = 10;
205
206 /**
207 * Standard priority of threads that are currently running a user interface
208 * that the user is interacting with. Applications can not normally
209 * change to this priority; the system will automatically adjust your
210 * application threads as the user moves through the UI.
211 * Use with {@link #setThreadPriority(int)} and
212 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
213 * {@link java.lang.Thread} class.
214 */
215 public static final int THREAD_PRIORITY_FOREGROUND = -2;
216
217 /**
218 * Standard priority of system display threads, involved in updating
219 * the user interface. Applications can not
220 * normally change to this priority.
221 * Use with {@link #setThreadPriority(int)} and
222 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
223 * {@link java.lang.Thread} class.
224 */
225 public static final int THREAD_PRIORITY_DISPLAY = -4;
226
227 /**
228 * Standard priority of the most important display threads, for compositing
229 * the screen and retrieving input events. Applications can not normally
230 * change to this priority.
231 * Use with {@link #setThreadPriority(int)} and
232 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
233 * {@link java.lang.Thread} class.
234 */
235 public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8;
236
237 /**
238 * Standard priority of audio threads. 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_AUDIO = -16;
245
246 /**
247 * Standard priority of the most important audio threads.
248 * Applications can not normally 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_URGENT_AUDIO = -19;
254
255 /**
256 * Minimum increment to make a priority more favorable.
257 */
258 public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
259
260 /**
261 * Minimum increment to make a priority less favorable.
262 */
263 public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
264
San Mehate9d376b2009-04-21 14:06:36 -0700265 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700266 * Default scheduling policy
267 * @hide
268 */
269 public static final int SCHED_OTHER = 0;
270
271 /**
272 * First-In First-Out scheduling policy
273 * @hide
274 */
275 public static final int SCHED_FIFO = 1;
276
277 /**
278 * Round-Robin scheduling policy
279 * @hide
280 */
281 public static final int SCHED_RR = 2;
282
283 /**
284 * Batch scheduling policy
285 * @hide
286 */
287 public static final int SCHED_BATCH = 3;
288
289 /**
290 * Idle scheduling policy
291 * @hide
292 */
293 public static final int SCHED_IDLE = 5;
294
Glenn Kastenf1b56442012-03-15 16:33:43 -0700295 // Keep in sync with SP_* constants of enum type SchedPolicy
296 // declared in system/core/include/cutils/sched_policy.h,
297 // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
San Mehate9d376b2009-04-21 14:06:36 -0700298
299 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700300 * Default thread group -
301 * has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
302 * When used with setProcessGroup(), the group of each thread in the process
303 * is conditionally changed based on that thread's current priority, as follows:
304 * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
305 * are moved to foreground thread group. All other threads are left unchanged.
306 * @hide
307 */
308 public static final int THREAD_GROUP_DEFAULT = -1;
309
310 /**
311 * Background thread group - All threads in
San Mehate9d376b2009-04-21 14:06:36 -0700312 * this group are scheduled with a reduced share of the CPU.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700313 * Value is same as constant SP_BACKGROUND of enum SchedPolicy.
314 * FIXME rename to THREAD_GROUP_BACKGROUND.
San Mehate9d376b2009-04-21 14:06:36 -0700315 * @hide
316 */
Glenn Kastenf1b56442012-03-15 16:33:43 -0700317 public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0;
San Mehate9d376b2009-04-21 14:06:36 -0700318
319 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700320 * Foreground thread group - All threads in
321 * this group are scheduled with a normal share of the CPU.
322 * Value is same as constant SP_FOREGROUND of enum SchedPolicy.
323 * Not used at this level.
San Mehate9d376b2009-04-21 14:06:36 -0700324 * @hide
325 **/
Glenn Kastenf1b56442012-03-15 16:33:43 -0700326 private static final int THREAD_GROUP_FOREGROUND = 1;
San Mehate9d376b2009-04-21 14:06:36 -0700327
Glenn Kasten07b04652012-04-23 15:00:43 -0700328 /**
329 * System thread group.
330 * @hide
331 **/
332 public static final int THREAD_GROUP_SYSTEM = 2;
333
334 /**
335 * Application audio thread group.
336 * @hide
337 **/
338 public static final int THREAD_GROUP_AUDIO_APP = 3;
339
340 /**
341 * System audio thread group.
342 * @hide
343 **/
344 public static final int THREAD_GROUP_AUDIO_SYS = 4;
345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 public static final int SIGNAL_QUIT = 3;
347 public static final int SIGNAL_KILL = 9;
348 public static final int SIGNAL_USR1 = 10;
349
350 // State for communicating with zygote process
351
352 static LocalSocket sZygoteSocket;
353 static DataInputStream sZygoteInputStream;
354 static BufferedWriter sZygoteWriter;
355
356 /** true if previous zygote open failed */
357 static boolean sPreviousZygoteOpenFailed;
358
359 /**
360 * Start a new process.
361 *
362 * <p>If processes are enabled, a new process is created and the
363 * static main() function of a <var>processClass</var> is executed there.
364 * The process will continue running after this function returns.
365 *
366 * <p>If processes are not enabled, a new thread in the caller's
367 * process is created and main() of <var>processClass</var> called there.
368 *
369 * <p>The niceName parameter, if not an empty string, is a custom name to
370 * give to the process instead of using processClass. This allows you to
371 * make easily identifyable processes even if you are using the same base
372 * <var>processClass</var> to start them.
373 *
374 * @param processClass The class to use as the process's main entry
375 * point.
376 * @param niceName A more readable name to use for the process.
377 * @param uid The user-id under which the process will run.
378 * @param gid The group-id under which the process will run.
379 * @param gids Additional group-ids associated with the process.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700380 * @param debugFlags Additional flags.
381 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500382 * @param seInfo null-ok SE Android information for the new process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 * @param zygoteArgs Additional arguments to supply to the zygote process.
384 *
Jeff Brown3f9dd282011-07-08 20:02:19 -0700385 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 * @throws RuntimeException on fatal start failure
387 *
388 * {@hide}
389 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700390 public static final ProcessStartResult start(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 final String niceName,
392 int uid, int gid, int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700393 int debugFlags, int mountExternal,
394 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500395 String seInfo,
Jeff Brown10e89712011-07-08 18:52:57 -0700396 String[] zygoteArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 try {
Jeff Brown10e89712011-07-08 18:52:57 -0700398 return startViaZygote(processClass, niceName, uid, gid, gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700399 debugFlags, mountExternal, targetSdkVersion, seInfo, zygoteArgs);
Jeff Brown10e89712011-07-08 18:52:57 -0700400 } catch (ZygoteStartFailedEx ex) {
401 Log.e(LOG_TAG,
402 "Starting VM process through Zygote failed");
403 throw new RuntimeException(
404 "Starting VM process through Zygote failed", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 }
407
408 /** retry interval for opening a zygote socket */
409 static final int ZYGOTE_RETRY_MILLIS = 500;
410
411 /**
412 * Tries to open socket to Zygote process if not already open. If
413 * already open, does nothing. May block and retry.
414 */
415 private static void openZygoteSocketIfNeeded()
416 throws ZygoteStartFailedEx {
417
418 int retryCount;
419
420 if (sPreviousZygoteOpenFailed) {
421 /*
422 * If we've failed before, expect that we'll fail again and
423 * don't pause for retries.
424 */
425 retryCount = 0;
426 } else {
427 retryCount = 10;
428 }
429
430 /*
431 * See bug #811181: Sometimes runtime can make it up before zygote.
432 * Really, we'd like to do something better to avoid this condition,
433 * but for now just wait a bit...
434 */
435 for (int retry = 0
436 ; (sZygoteSocket == null) && (retry < (retryCount + 1))
437 ; retry++ ) {
438
439 if (retry > 0) {
440 try {
441 Log.i("Zygote", "Zygote not up yet, sleeping...");
442 Thread.sleep(ZYGOTE_RETRY_MILLIS);
443 } catch (InterruptedException ex) {
444 // should never happen
445 }
446 }
447
448 try {
449 sZygoteSocket = new LocalSocket();
450
451 sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET,
452 LocalSocketAddress.Namespace.RESERVED));
453
454 sZygoteInputStream
455 = new DataInputStream(sZygoteSocket.getInputStream());
456
457 sZygoteWriter =
458 new BufferedWriter(
459 new OutputStreamWriter(
460 sZygoteSocket.getOutputStream()),
461 256);
462
463 Log.i("Zygote", "Process: zygote socket opened");
464
465 sPreviousZygoteOpenFailed = false;
466 break;
467 } catch (IOException ex) {
468 if (sZygoteSocket != null) {
469 try {
470 sZygoteSocket.close();
471 } catch (IOException ex2) {
472 Log.e(LOG_TAG,"I/O exception on close after exception",
473 ex2);
474 }
475 }
476
477 sZygoteSocket = null;
478 }
479 }
480
481 if (sZygoteSocket == null) {
482 sPreviousZygoteOpenFailed = true;
483 throw new ZygoteStartFailedEx("connect failed");
484 }
485 }
486
487 /**
488 * Sends an argument list to the zygote process, which starts a new child
489 * and returns the child's pid. Please note: the present implementation
490 * replaces newlines in the argument list with spaces.
491 * @param args argument list
Jeff Brown3f9dd282011-07-08 20:02:19 -0700492 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 * @throws ZygoteStartFailedEx if process start failed for any reason
494 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700495 private static ProcessStartResult zygoteSendArgsAndGetResult(ArrayList<String> args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 openZygoteSocketIfNeeded();
498
499 try {
500 /**
501 * See com.android.internal.os.ZygoteInit.readArgumentList()
502 * Presently the wire format to the zygote process is:
503 * a) a count of arguments (argc, in essence)
504 * b) a number of newline-separated argument strings equal to count
505 *
506 * After the zygote process reads these it will write the pid of
Jeff Brown3f9dd282011-07-08 20:02:19 -0700507 * the child or -1 on failure, followed by boolean to
508 * indicate whether a wrapper process was used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 */
510
511 sZygoteWriter.write(Integer.toString(args.size()));
512 sZygoteWriter.newLine();
513
514 int sz = args.size();
515 for (int i = 0; i < sz; i++) {
516 String arg = args.get(i);
517 if (arg.indexOf('\n') >= 0) {
518 throw new ZygoteStartFailedEx(
519 "embedded newlines not allowed");
520 }
521 sZygoteWriter.write(arg);
522 sZygoteWriter.newLine();
523 }
524
525 sZygoteWriter.flush();
526
527 // Should there be a timeout on this?
Jeff Brown3f9dd282011-07-08 20:02:19 -0700528 ProcessStartResult result = new ProcessStartResult();
529 result.pid = sZygoteInputStream.readInt();
530 if (result.pid < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 throw new ZygoteStartFailedEx("fork() failed");
532 }
Jeff Brown3f9dd282011-07-08 20:02:19 -0700533 result.usingWrapper = sZygoteInputStream.readBoolean();
534 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 } catch (IOException ex) {
536 try {
537 if (sZygoteSocket != null) {
538 sZygoteSocket.close();
539 }
540 } catch (IOException ex2) {
541 // we're going to fail anyway
542 Log.e(LOG_TAG,"I/O exception on routine close", ex2);
543 }
544
545 sZygoteSocket = null;
546
547 throw new ZygoteStartFailedEx(ex);
548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 }
550
551 /**
552 * Starts a new process via the zygote mechanism.
553 *
554 * @param processClass Class name whose static main() to run
555 * @param niceName 'nice' process name to appear in ps
556 * @param uid a POSIX uid that the new process should setuid() to
557 * @param gid a POSIX gid that the new process shuold setgid() to
558 * @param gids null-ok; a list of supplementary group IDs that the
559 * new process should setgroup() to.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700560 * @param debugFlags Additional flags.
561 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500562 * @param seInfo null-ok SE Android information for the new process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 * @param extraArgs Additional arguments to supply to the zygote process.
Jeff Brown3f9dd282011-07-08 20:02:19 -0700564 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 * @throws ZygoteStartFailedEx if process start failed for any reason
566 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700567 private static ProcessStartResult startViaZygote(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 final String niceName,
569 final int uid, final int gid,
570 final int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700571 int debugFlags, int mountExternal,
572 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500573 String seInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 String[] extraArgs)
575 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 synchronized(Process.class) {
577 ArrayList<String> argsForZygote = new ArrayList<String>();
578
579 // --runtime-init, --setuid=, --setgid=,
580 // and --setgroups= must go first
581 argsForZygote.add("--runtime-init");
582 argsForZygote.add("--setuid=" + uid);
583 argsForZygote.add("--setgid=" + gid);
Elliott Hughesae07ecf2011-07-06 17:33:27 -0700584 if ((debugFlags & Zygote.DEBUG_ENABLE_JNI_LOGGING) != 0) {
585 argsForZygote.add("--enable-jni-logging");
586 }
Ben Cheng23085b72010-02-08 16:06:32 -0800587 if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) {
588 argsForZygote.add("--enable-safemode");
589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) {
591 argsForZygote.add("--enable-debugger");
592 }
593 if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) {
594 argsForZygote.add("--enable-checkjni");
595 }
596 if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) {
597 argsForZygote.add("--enable-assert");
598 }
Jeff Sharkey2bca8682012-08-22 13:59:58 -0700599 if (mountExternal == Zygote.MOUNT_EXTERNAL_MULTIUSER) {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700600 argsForZygote.add("--mount-external-multiuser");
Jeff Sharkeye217ee42012-08-28 16:23:01 -0700601 } else if (mountExternal == Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL) {
602 argsForZygote.add("--mount-external-multiuser-all");
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700603 }
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700604 argsForZygote.add("--target-sdk-version=" + targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605
606 //TODO optionally enable debuger
607 //argsForZygote.add("--enable-debugger");
608
609 // --setgroups is a comma-separated list
610 if (gids != null && gids.length > 0) {
611 StringBuilder sb = new StringBuilder();
612 sb.append("--setgroups=");
613
614 int sz = gids.length;
615 for (int i = 0; i < sz; i++) {
616 if (i != 0) {
617 sb.append(',');
618 }
619 sb.append(gids[i]);
620 }
621
622 argsForZygote.add(sb.toString());
623 }
624
625 if (niceName != null) {
626 argsForZygote.add("--nice-name=" + niceName);
627 }
628
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500629 if (seInfo != null) {
630 argsForZygote.add("--seinfo=" + seInfo);
631 }
632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 argsForZygote.add(processClass);
634
635 if (extraArgs != null) {
636 for (String arg : extraArgs) {
637 argsForZygote.add(arg);
638 }
639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640
Jeff Brown3f9dd282011-07-08 20:02:19 -0700641 return zygoteSendArgsAndGetResult(argsForZygote);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 }
644
645 /**
646 * Returns elapsed milliseconds of the time this process has run.
647 * @return Returns the number of milliseconds this process has return.
648 */
649 public static final native long getElapsedCpuTime();
650
651 /**
652 * Returns the identifier of this process, which can be used with
653 * {@link #killProcess} and {@link #sendSignal}.
654 */
655 public static final native int myPid();
656
657 /**
658 * Returns the identifier of the calling thread, which be used with
659 * {@link #setThreadPriority(int, int)}.
660 */
661 public static final native int myTid();
662
663 /**
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700664 * Returns the identifier of this process's uid. This is the kernel uid
665 * that the process is running under, which is the identity of its
666 * app-specific sandbox. It is different from {@link #myUserHandle} in that
667 * a uid identifies a specific app sandbox in a specific user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 */
669 public static final native int myUid();
670
671 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700672 * Returns this process's user handle. This is the
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700673 * user the process is running under. It is distinct from
674 * {@link #myUid()} in that a particular user will have multiple
675 * distinct apps running under it each with their own uid.
676 */
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700677 public static final UserHandle myUserHandle() {
678 return new UserHandle(UserHandle.getUserId(myUid()));
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700679 }
680
681 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800682 * Returns whether the current process is in an isolated sandbox.
683 * @hide
684 */
685 public static final boolean isIsolated() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700686 int uid = UserHandle.getAppId(myUid());
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800687 return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID;
688 }
689
690 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 * Returns the UID assigned to a particular user name, or -1 if there is
692 * none. If the given string consists of only numbers, it is converted
693 * directly to a uid.
694 */
695 public static final native int getUidForName(String name);
696
697 /**
698 * Returns the GID assigned to a particular user name, or -1 if there is
699 * none. If the given string consists of only numbers, it is converted
700 * directly to a gid.
701 */
702 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700703
704 /**
705 * Returns a uid for a currently running process.
706 * @param pid the process id
707 * @return the uid of the process, or -1 if the process is not running.
708 * @hide pending API council review
709 */
710 public static final int getUidForPid(int pid) {
711 String[] procStatusLabels = { "Uid:" };
712 long[] procStatusValues = new long[1];
713 procStatusValues[0] = -1;
714 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
715 return (int) procStatusValues[0];
716 }
717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700719 * Returns the parent process id for a currently running process.
720 * @param pid the process id
721 * @return the parent process id of the process, or -1 if the process is not running.
722 * @hide
723 */
724 public static final int getParentPid(int pid) {
725 String[] procStatusLabels = { "PPid:" };
726 long[] procStatusValues = new long[1];
727 procStatusValues[0] = -1;
728 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
729 return (int) procStatusValues[0];
730 }
731
732 /**
Glenn Kasten07b04652012-04-23 15:00:43 -0700733 * Returns the thread group leader id for a currently running thread.
734 * @param tid the thread id
735 * @return the thread group leader id of the thread, or -1 if the thread is not running.
736 * This is same as what getpid(2) would return if called by tid.
737 * @hide
738 */
739 public static final int getThreadGroupLeader(int tid) {
740 String[] procStatusLabels = { "Tgid:" };
741 long[] procStatusValues = new long[1];
742 procStatusValues[0] = -1;
743 Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues);
744 return (int) procStatusValues[0];
745 }
746
747 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 * Set the priority of a thread, based on Linux priorities.
749 *
750 * @param tid The identifier of the thread/process to change.
751 * @param priority A Linux priority level, from -20 for highest scheduling
752 * priority to 19 for lowest scheduling priority.
753 *
754 * @throws IllegalArgumentException Throws IllegalArgumentException if
755 * <var>tid</var> does not exist.
756 * @throws SecurityException Throws SecurityException if your process does
757 * not have permission to modify the given thread, or to use the given
758 * priority.
759 */
760 public static final native void setThreadPriority(int tid, int priority)
761 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700762
763 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700764 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
765 * throw an exception if passed a background-level thread priority. This is only
766 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
767 *
768 * @hide
769 */
770 public static final native void setCanSelfBackground(boolean backgroundOk);
771
772 /**
San Mehate9d376b2009-04-21 14:06:36 -0700773 * Sets the scheduling group for a thread.
774 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700775 * @param tid The identifier of the thread to change.
776 * @param group The target group for this thread from THREAD_GROUP_*.
San Mehate9d376b2009-04-21 14:06:36 -0700777 *
778 * @throws IllegalArgumentException Throws IllegalArgumentException if
779 * <var>tid</var> does not exist.
780 * @throws SecurityException Throws SecurityException if your process does
781 * not have permission to modify the given thread, or to use the given
782 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700783 * If the thread is a thread group leader, that is it's gettid() == getpid(),
784 * then the other threads in the same thread group are _not_ affected.
San Mehate9d376b2009-04-21 14:06:36 -0700785 */
786 public static final native void setThreadGroup(int tid, int group)
787 throws IllegalArgumentException, SecurityException;
Glenn Kastenf1b56442012-03-15 16:33:43 -0700788
San Mehat3e458242009-05-19 14:44:16 -0700789 /**
790 * Sets the scheduling group for a process and all child threads
791 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700792 * @param pid The identifier of the process to change.
793 * @param group The target group for this process from THREAD_GROUP_*.
San Mehat3e458242009-05-19 14:44:16 -0700794 *
795 * @throws IllegalArgumentException Throws IllegalArgumentException if
796 * <var>tid</var> does not exist.
797 * @throws SecurityException Throws SecurityException if your process does
798 * not have permission to modify the given thread, or to use the given
799 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700800 *
801 * group == THREAD_GROUP_DEFAULT means to move all non-background priority
802 * threads to the foreground scheduling group, but to leave background
803 * priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all
804 * threads, regardless of priority, to the background scheduling group.
805 * group == THREAD_GROUP_FOREGROUND is not allowed.
San Mehat3e458242009-05-19 14:44:16 -0700806 */
807 public static final native void setProcessGroup(int pid, int group)
808 throws IllegalArgumentException, SecurityException;
Jeff Sharkey9e57c412013-01-17 14:12:41 -0800809
810 /**
811 * Return the scheduling group of requested process.
812 *
813 * @hide
814 */
815 public static final native int getProcessGroup(int pid)
816 throws IllegalArgumentException, SecurityException;
817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 /**
819 * Set the priority of the calling thread, based on Linux priorities. See
820 * {@link #setThreadPriority(int, int)} for more information.
821 *
822 * @param priority A Linux priority level, from -20 for highest scheduling
823 * priority to 19 for lowest scheduling priority.
824 *
825 * @throws IllegalArgumentException Throws IllegalArgumentException if
826 * <var>tid</var> does not exist.
827 * @throws SecurityException Throws SecurityException if your process does
828 * not have permission to modify the given thread, or to use the given
829 * priority.
830 *
831 * @see #setThreadPriority(int, int)
832 */
833 public static final native void setThreadPriority(int priority)
834 throws IllegalArgumentException, SecurityException;
835
836 /**
837 * Return the current priority of a thread, based on Linux priorities.
838 *
839 * @param tid The identifier of the thread/process to change.
840 *
841 * @return Returns the current priority, as a Linux priority level,
842 * from -20 for highest scheduling priority to 19 for lowest scheduling
843 * priority.
844 *
845 * @throws IllegalArgumentException Throws IllegalArgumentException if
846 * <var>tid</var> does not exist.
847 */
848 public static final native int getThreadPriority(int tid)
849 throws IllegalArgumentException;
850
851 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700852 * Set the scheduling policy and priority of a thread, based on Linux.
853 *
854 * @param tid The identifier of the thread/process to change.
855 * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
856 * @param priority A Linux priority level in a range appropriate for the given policy.
857 *
858 * @throws IllegalArgumentException Throws IllegalArgumentException if
859 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
860 * @throws SecurityException Throws SecurityException if your process does
861 * not have permission to modify the given thread, or to use the given
862 * scheduling policy or priority.
863 *
864 * {@hide}
865 */
866 public static final native void setThreadScheduler(int tid, int policy, int priority)
867 throws IllegalArgumentException;
868
869 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 * Determine whether the current environment supports multiple processes.
871 *
872 * @return Returns true if the system can run in multiple processes, else
873 * false if everything is running in a single process.
Jeff Brown10e89712011-07-08 18:52:57 -0700874 *
875 * @deprecated This method always returns true. Do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 */
Jeff Brown10e89712011-07-08 18:52:57 -0700877 @Deprecated
878 public static final boolean supportsProcesses() {
879 return true;
880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881
882 /**
883 * Set the out-of-memory badness adjustment for a process.
884 *
885 * @param pid The process identifier to set.
886 * @param amt Adjustment value -- linux allows -16 to +15.
887 *
888 * @return Returns true if the underlying system supports this
889 * feature, else false.
890 *
891 * {@hide}
892 */
893 public static final native boolean setOomAdj(int pid, int amt);
894
895 /**
896 * Change this process's argv[0] parameter. This can be useful to show
897 * more descriptive information in things like the 'ps' command.
898 *
899 * @param text The new name of this process.
900 *
901 * {@hide}
902 */
903 public static final native void setArgV0(String text);
904
905 /**
906 * Kill the process with the given PID.
907 * Note that, though this API allows us to request to
908 * kill any process based on its PID, the kernel will
909 * still impose standard restrictions on which PIDs you
910 * are actually able to kill. Typically this means only
911 * the process running the caller's packages/application
912 * and any additional processes created by that app; packages
913 * sharing a common UID will also be able to kill each
914 * other's processes.
915 */
916 public static final void killProcess(int pid) {
917 sendSignal(pid, SIGNAL_KILL);
918 }
919
920 /** @hide */
921 public static final native int setUid(int uid);
922
923 /** @hide */
924 public static final native int setGid(int uid);
925
926 /**
927 * Send a signal to the given process.
928 *
929 * @param pid The pid of the target process.
930 * @param signal The signal to send.
931 */
932 public static final native void sendSignal(int pid, int signal);
933
Dianne Hackborn906497c2010-05-10 15:57:38 -0700934 /**
935 * @hide
936 * Private impl for avoiding a log message... DO NOT USE without doing
937 * your own log, or the Android Illuminati will find you some night and
938 * beat you up.
939 */
940 public static final void killProcessQuiet(int pid) {
941 sendSignalQuiet(pid, SIGNAL_KILL);
942 }
943
944 /**
945 * @hide
946 * Private impl for avoiding a log message... DO NOT USE without doing
947 * your own log, or the Android Illuminati will find you some night and
948 * beat you up.
949 */
950 public static final native void sendSignalQuiet(int pid, int signal);
951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 /** @hide */
Marco Nelissen0bca96b2009-07-17 12:59:25 -0700953 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954
955 /** @hide */
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700956 public static final native long getTotalMemory();
957
958 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 public static final native void readProcLines(String path,
960 String[] reqFields, long[] outSizes);
961
962 /** @hide */
963 public static final native int[] getPids(String path, int[] lastArray);
964
965 /** @hide */
966 public static final int PROC_TERM_MASK = 0xff;
967 /** @hide */
968 public static final int PROC_ZERO_TERM = 0;
969 /** @hide */
970 public static final int PROC_SPACE_TERM = (int)' ';
971 /** @hide */
Evan Millarc64edde2009-04-18 12:26:32 -0700972 public static final int PROC_TAB_TERM = (int)'\t';
973 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 public static final int PROC_COMBINE = 0x100;
975 /** @hide */
976 public static final int PROC_PARENS = 0x200;
977 /** @hide */
978 public static final int PROC_OUT_STRING = 0x1000;
979 /** @hide */
980 public static final int PROC_OUT_LONG = 0x2000;
981 /** @hide */
982 public static final int PROC_OUT_FLOAT = 0x4000;
983
984 /** @hide */
985 public static final native boolean readProcFile(String file, int[] format,
986 String[] outStrings, long[] outLongs, float[] outFloats);
Evan Millarc64edde2009-04-18 12:26:32 -0700987
988 /** @hide */
989 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
990 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700992 /** @hide */
993 public static final native int[] getPidsForCommands(String[] cmds);
994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 /**
996 * Gets the total Pss value for a given process, in bytes.
997 *
998 * @param pid the process to the Pss for
999 * @return the total Pss value for the given process in bytes,
1000 * or -1 if the value cannot be determined
1001 * @hide
1002 */
1003 public static final native long getPss(int pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -07001004
1005 /**
1006 * Specifies the outcome of having started a process.
1007 * @hide
1008 */
1009 public static final class ProcessStartResult {
1010 /**
1011 * The PID of the newly started process.
1012 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
1013 */
1014 public int pid;
1015
1016 /**
1017 * True if the process was started with a wrapper attached.
1018 */
1019 public boolean usingWrapper;
1020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021}