blob: 0e38ebc1e390b053daa0a13fb32cae268bfb1668 [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
Jeff Hao406ec152013-07-30 10:13:41 -070030import libcore.io.Libcore;
31
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032/*package*/ class ZygoteStartFailedEx extends Exception {
33 /**
34 * Something prevented the zygote process startup from happening normally
35 */
36
37 ZygoteStartFailedEx() {};
38 ZygoteStartFailedEx(String s) {super(s);}
39 ZygoteStartFailedEx(Throwable cause) {super(cause);}
40}
41
42/**
43 * Tools for managing OS processes.
44 */
45public class Process {
46 private static final String LOG_TAG = "Process";
47
48 private static final String ZYGOTE_SOCKET = "zygote";
49
50 /**
51 * Name of a process for running the platform's media services.
52 * {@hide}
53 */
54 public static final String ANDROID_SHARED_MEDIA = "com.android.process.media";
55
56 /**
57 * Name of the process that Google content providers can share.
58 * {@hide}
59 */
60 public static final String GOOGLE_SHARED_APP_CONTENT = "com.google.process.content";
61
62 /**
63 * Defines the UID/GID under which system code runs.
64 */
65 public static final int SYSTEM_UID = 1000;
66
67 /**
68 * Defines the UID/GID under which the telephony code runs.
69 */
70 public static final int PHONE_UID = 1001;
71
72 /**
Dianne Hackborn854060af2009-07-09 18:14:31 -070073 * Defines the UID/GID for the user shell.
74 * @hide
75 */
76 public static final int SHELL_UID = 2000;
77
78 /**
Mike Lockwoodd42685d2009-09-03 09:25:22 -040079 * Defines the UID/GID for the log group.
80 * @hide
81 */
82 public static final int LOG_UID = 1007;
83
84 /**
Amith Yamasanid1582142009-07-08 20:04:55 -070085 * Defines the UID/GID for the WIFI supplicant process.
86 * @hide
87 */
88 public static final int WIFI_UID = 1010;
89
90 /**
Glenn Kasten8b7d1b42011-07-13 16:23:22 -070091 * Defines the UID/GID for the mediaserver process.
92 * @hide
93 */
94 public static final int MEDIA_UID = 1013;
95
96 /**
Jeff Sharkey5294a2f2012-04-24 17:07:22 -070097 * Defines the UID/GID for the DRM process.
98 * @hide
99 */
100 public static final int DRM_UID = 1019;
101
102 /**
Mike Lockwood58fd98a2010-09-24 11:02:47 -0400103 * Defines the GID for the group that allows write access to the SD card.
104 * @hide
105 */
106 public static final int SDCARD_RW_GID = 1015;
107
108 /**
Kenny Root26993b32012-03-19 15:07:51 -0700109 * Defines the UID/GID for the group that controls VPN services.
110 * @hide
111 */
112 public static final int VPN_UID = 1016;
113
114 /**
Nick Pellycd0e8392010-10-13 17:25:24 -0700115 * Defines the UID/GID for the NFC service process.
116 * @hide
117 */
Nick Pellya5cb9f42011-11-21 14:54:46 -0800118 public static final int NFC_UID = 1027;
Nick Pellycd0e8392010-10-13 17:25:24 -0700119
120 /**
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800121 * Defines the UID/GID for the Bluetooth service process.
122 * @hide
123 */
124 public static final int BLUETOOTH_UID = 1002;
125
126 /**
Mike Lockwooddcaa10c2010-12-16 12:50:44 -0800127 * Defines the GID for the group that allows write access to the internal media storage.
128 * @hide
129 */
130 public static final int MEDIA_RW_GID = 1023;
131
132 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 * Defines the start of a range of UIDs (and GIDs), going from this
134 * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning
135 * to applications.
136 */
137 public static final int FIRST_APPLICATION_UID = 10000;
138 /**
139 * Last of application-specific UIDs starting at
140 * {@link #FIRST_APPLICATION_UID}.
141 */
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800142 public static final int LAST_APPLICATION_UID = 19999;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800143
144 /**
145 * First uid used for fully isolated sandboxed processes (with no permissions of their own)
146 * @hide
147 */
148 public static final int FIRST_ISOLATED_UID = 99000;
149
150 /**
151 * Last uid used for fully isolated sandboxed processes (with no permissions of their own)
152 * @hide
153 */
154 public static final int LAST_ISOLATED_UID = 99999;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155
156 /**
Kenny Roote091f222012-09-11 15:01:26 -0700157 * First gid for applications to share resources. Used when forward-locking
158 * is enabled but all UserHandles need to be able to read the resources.
159 * @hide
160 */
161 public static final int FIRST_SHARED_APPLICATION_GID = 50000;
162
163 /**
164 * Last gid for applications to share resources. Used when forward-locking
165 * is enabled but all UserHandles need to be able to read the resources.
166 * @hide
167 */
168 public static final int LAST_SHARED_APPLICATION_GID = 59999;
169
170 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 * Standard priority of application threads.
172 * Use with {@link #setThreadPriority(int)} and
173 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
174 * {@link java.lang.Thread} class.
175 */
176 public static final int THREAD_PRIORITY_DEFAULT = 0;
177
178 /*
179 * ***************************************
180 * ** Keep in sync with utils/threads.h **
181 * ***************************************
182 */
183
184 /**
185 * Lowest available thread priority. Only for those who really, really
186 * don't want to run if anything else is happening.
187 * Use with {@link #setThreadPriority(int)} and
188 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
189 * {@link java.lang.Thread} class.
190 */
191 public static final int THREAD_PRIORITY_LOWEST = 19;
192
193 /**
194 * Standard priority background threads. This gives your thread a slightly
195 * lower than normal priority, so that it will have less chance of impacting
196 * the responsiveness of the user interface.
197 * Use with {@link #setThreadPriority(int)} and
198 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
199 * {@link java.lang.Thread} class.
200 */
201 public static final int THREAD_PRIORITY_BACKGROUND = 10;
202
203 /**
204 * Standard priority of threads that are currently running a user interface
205 * that the user is interacting with. Applications can not normally
206 * change to this priority; the system will automatically adjust your
207 * application threads as the user moves through the UI.
208 * Use with {@link #setThreadPriority(int)} and
209 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
210 * {@link java.lang.Thread} class.
211 */
212 public static final int THREAD_PRIORITY_FOREGROUND = -2;
213
214 /**
215 * Standard priority of system display threads, involved in updating
216 * the user interface. Applications can not
217 * normally change to this priority.
218 * Use with {@link #setThreadPriority(int)} and
219 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
220 * {@link java.lang.Thread} class.
221 */
222 public static final int THREAD_PRIORITY_DISPLAY = -4;
223
224 /**
225 * Standard priority of the most important display threads, for compositing
226 * the screen and retrieving input events. Applications can not normally
227 * change to this priority.
228 * Use with {@link #setThreadPriority(int)} and
229 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
230 * {@link java.lang.Thread} class.
231 */
232 public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8;
233
234 /**
235 * Standard priority of audio threads. Applications can not normally
236 * change to this priority.
237 * Use with {@link #setThreadPriority(int)} and
238 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
239 * {@link java.lang.Thread} class.
240 */
241 public static final int THREAD_PRIORITY_AUDIO = -16;
242
243 /**
244 * Standard priority of the most important audio threads.
245 * Applications can not normally change to this priority.
246 * Use with {@link #setThreadPriority(int)} and
247 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
248 * {@link java.lang.Thread} class.
249 */
250 public static final int THREAD_PRIORITY_URGENT_AUDIO = -19;
251
252 /**
253 * Minimum increment to make a priority more favorable.
254 */
255 public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
256
257 /**
258 * Minimum increment to make a priority less favorable.
259 */
260 public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
261
San Mehate9d376b2009-04-21 14:06:36 -0700262 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700263 * Default scheduling policy
264 * @hide
265 */
266 public static final int SCHED_OTHER = 0;
267
268 /**
269 * First-In First-Out scheduling policy
270 * @hide
271 */
272 public static final int SCHED_FIFO = 1;
273
274 /**
275 * Round-Robin scheduling policy
276 * @hide
277 */
278 public static final int SCHED_RR = 2;
279
280 /**
281 * Batch scheduling policy
282 * @hide
283 */
284 public static final int SCHED_BATCH = 3;
285
286 /**
287 * Idle scheduling policy
288 * @hide
289 */
290 public static final int SCHED_IDLE = 5;
291
Glenn Kastenf1b56442012-03-15 16:33:43 -0700292 // Keep in sync with SP_* constants of enum type SchedPolicy
293 // declared in system/core/include/cutils/sched_policy.h,
294 // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
San Mehate9d376b2009-04-21 14:06:36 -0700295
296 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700297 * Default thread group -
298 * has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
299 * When used with setProcessGroup(), the group of each thread in the process
300 * is conditionally changed based on that thread's current priority, as follows:
301 * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
302 * are moved to foreground thread group. All other threads are left unchanged.
303 * @hide
304 */
305 public static final int THREAD_GROUP_DEFAULT = -1;
306
307 /**
308 * Background thread group - All threads in
San Mehate9d376b2009-04-21 14:06:36 -0700309 * this group are scheduled with a reduced share of the CPU.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700310 * Value is same as constant SP_BACKGROUND of enum SchedPolicy.
311 * FIXME rename to THREAD_GROUP_BACKGROUND.
San Mehate9d376b2009-04-21 14:06:36 -0700312 * @hide
313 */
Glenn Kastenf1b56442012-03-15 16:33:43 -0700314 public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0;
San Mehate9d376b2009-04-21 14:06:36 -0700315
316 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700317 * Foreground thread group - All threads in
318 * this group are scheduled with a normal share of the CPU.
319 * Value is same as constant SP_FOREGROUND of enum SchedPolicy.
320 * Not used at this level.
San Mehate9d376b2009-04-21 14:06:36 -0700321 * @hide
322 **/
Glenn Kastenf1b56442012-03-15 16:33:43 -0700323 private static final int THREAD_GROUP_FOREGROUND = 1;
San Mehate9d376b2009-04-21 14:06:36 -0700324
Glenn Kasten07b04652012-04-23 15:00:43 -0700325 /**
326 * System thread group.
327 * @hide
328 **/
329 public static final int THREAD_GROUP_SYSTEM = 2;
330
331 /**
332 * Application audio thread group.
333 * @hide
334 **/
335 public static final int THREAD_GROUP_AUDIO_APP = 3;
336
337 /**
338 * System audio thread group.
339 * @hide
340 **/
341 public static final int THREAD_GROUP_AUDIO_SYS = 4;
342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 public static final int SIGNAL_QUIT = 3;
344 public static final int SIGNAL_KILL = 9;
345 public static final int SIGNAL_USR1 = 10;
346
347 // State for communicating with zygote process
348
349 static LocalSocket sZygoteSocket;
350 static DataInputStream sZygoteInputStream;
351 static BufferedWriter sZygoteWriter;
352
353 /** true if previous zygote open failed */
354 static boolean sPreviousZygoteOpenFailed;
355
356 /**
357 * Start a new process.
358 *
359 * <p>If processes are enabled, a new process is created and the
360 * static main() function of a <var>processClass</var> is executed there.
361 * The process will continue running after this function returns.
362 *
363 * <p>If processes are not enabled, a new thread in the caller's
364 * process is created and main() of <var>processClass</var> called there.
365 *
366 * <p>The niceName parameter, if not an empty string, is a custom name to
367 * give to the process instead of using processClass. This allows you to
368 * make easily identifyable processes even if you are using the same base
369 * <var>processClass</var> to start them.
370 *
371 * @param processClass The class to use as the process's main entry
372 * point.
373 * @param niceName A more readable name to use for the process.
374 * @param uid The user-id under which the process will run.
375 * @param gid The group-id under which the process will run.
376 * @param gids Additional group-ids associated with the process.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700377 * @param debugFlags Additional flags.
378 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500379 * @param seInfo null-ok SE Android information for the new process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 * @param zygoteArgs Additional arguments to supply to the zygote process.
381 *
Jeff Brown3f9dd282011-07-08 20:02:19 -0700382 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 * @throws RuntimeException on fatal start failure
384 *
385 * {@hide}
386 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700387 public static final ProcessStartResult start(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 final String niceName,
389 int uid, int gid, int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700390 int debugFlags, int mountExternal,
391 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500392 String seInfo,
Jeff Brown10e89712011-07-08 18:52:57 -0700393 String[] zygoteArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 try {
Jeff Brown10e89712011-07-08 18:52:57 -0700395 return startViaZygote(processClass, niceName, uid, gid, gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700396 debugFlags, mountExternal, targetSdkVersion, seInfo, zygoteArgs);
Jeff Brown10e89712011-07-08 18:52:57 -0700397 } catch (ZygoteStartFailedEx ex) {
398 Log.e(LOG_TAG,
399 "Starting VM process through Zygote failed");
400 throw new RuntimeException(
401 "Starting VM process through Zygote failed", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 }
404
405 /** retry interval for opening a zygote socket */
406 static final int ZYGOTE_RETRY_MILLIS = 500;
407
408 /**
409 * Tries to open socket to Zygote process if not already open. If
410 * already open, does nothing. May block and retry.
411 */
412 private static void openZygoteSocketIfNeeded()
413 throws ZygoteStartFailedEx {
414
415 int retryCount;
416
417 if (sPreviousZygoteOpenFailed) {
418 /*
419 * If we've failed before, expect that we'll fail again and
420 * don't pause for retries.
421 */
422 retryCount = 0;
423 } else {
424 retryCount = 10;
425 }
426
427 /*
428 * See bug #811181: Sometimes runtime can make it up before zygote.
429 * Really, we'd like to do something better to avoid this condition,
430 * but for now just wait a bit...
431 */
432 for (int retry = 0
433 ; (sZygoteSocket == null) && (retry < (retryCount + 1))
434 ; retry++ ) {
435
436 if (retry > 0) {
437 try {
438 Log.i("Zygote", "Zygote not up yet, sleeping...");
439 Thread.sleep(ZYGOTE_RETRY_MILLIS);
440 } catch (InterruptedException ex) {
441 // should never happen
442 }
443 }
444
445 try {
446 sZygoteSocket = new LocalSocket();
447
448 sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET,
449 LocalSocketAddress.Namespace.RESERVED));
450
451 sZygoteInputStream
452 = new DataInputStream(sZygoteSocket.getInputStream());
453
454 sZygoteWriter =
455 new BufferedWriter(
456 new OutputStreamWriter(
457 sZygoteSocket.getOutputStream()),
458 256);
459
460 Log.i("Zygote", "Process: zygote socket opened");
461
462 sPreviousZygoteOpenFailed = false;
463 break;
464 } catch (IOException ex) {
465 if (sZygoteSocket != null) {
466 try {
467 sZygoteSocket.close();
468 } catch (IOException ex2) {
469 Log.e(LOG_TAG,"I/O exception on close after exception",
470 ex2);
471 }
472 }
473
474 sZygoteSocket = null;
475 }
476 }
477
478 if (sZygoteSocket == null) {
479 sPreviousZygoteOpenFailed = true;
480 throw new ZygoteStartFailedEx("connect failed");
481 }
482 }
483
484 /**
485 * Sends an argument list to the zygote process, which starts a new child
486 * and returns the child's pid. Please note: the present implementation
487 * replaces newlines in the argument list with spaces.
488 * @param args argument list
Jeff Brown3f9dd282011-07-08 20:02:19 -0700489 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 * @throws ZygoteStartFailedEx if process start failed for any reason
491 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700492 private static ProcessStartResult zygoteSendArgsAndGetResult(ArrayList<String> args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 openZygoteSocketIfNeeded();
495
496 try {
497 /**
498 * See com.android.internal.os.ZygoteInit.readArgumentList()
499 * Presently the wire format to the zygote process is:
500 * a) a count of arguments (argc, in essence)
501 * b) a number of newline-separated argument strings equal to count
502 *
503 * After the zygote process reads these it will write the pid of
Jeff Brown3f9dd282011-07-08 20:02:19 -0700504 * the child or -1 on failure, followed by boolean to
505 * indicate whether a wrapper process was used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 */
507
508 sZygoteWriter.write(Integer.toString(args.size()));
509 sZygoteWriter.newLine();
510
511 int sz = args.size();
512 for (int i = 0; i < sz; i++) {
513 String arg = args.get(i);
514 if (arg.indexOf('\n') >= 0) {
515 throw new ZygoteStartFailedEx(
516 "embedded newlines not allowed");
517 }
518 sZygoteWriter.write(arg);
519 sZygoteWriter.newLine();
520 }
521
522 sZygoteWriter.flush();
523
524 // Should there be a timeout on this?
Jeff Brown3f9dd282011-07-08 20:02:19 -0700525 ProcessStartResult result = new ProcessStartResult();
526 result.pid = sZygoteInputStream.readInt();
527 if (result.pid < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 throw new ZygoteStartFailedEx("fork() failed");
529 }
Jeff Brown3f9dd282011-07-08 20:02:19 -0700530 result.usingWrapper = sZygoteInputStream.readBoolean();
531 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 } catch (IOException ex) {
533 try {
534 if (sZygoteSocket != null) {
535 sZygoteSocket.close();
536 }
537 } catch (IOException ex2) {
538 // we're going to fail anyway
539 Log.e(LOG_TAG,"I/O exception on routine close", ex2);
540 }
541
542 sZygoteSocket = null;
543
544 throw new ZygoteStartFailedEx(ex);
545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 }
547
548 /**
549 * Starts a new process via the zygote mechanism.
550 *
551 * @param processClass Class name whose static main() to run
552 * @param niceName 'nice' process name to appear in ps
553 * @param uid a POSIX uid that the new process should setuid() to
554 * @param gid a POSIX gid that the new process shuold setgid() to
555 * @param gids null-ok; a list of supplementary group IDs that the
556 * new process should setgroup() to.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700557 * @param debugFlags Additional flags.
558 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500559 * @param seInfo null-ok SE Android information for the new process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 * @param extraArgs Additional arguments to supply to the zygote process.
Jeff Brown3f9dd282011-07-08 20:02:19 -0700561 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 * @throws ZygoteStartFailedEx if process start failed for any reason
563 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700564 private static ProcessStartResult startViaZygote(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 final String niceName,
566 final int uid, final int gid,
567 final int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700568 int debugFlags, int mountExternal,
569 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500570 String seInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 String[] extraArgs)
572 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 synchronized(Process.class) {
574 ArrayList<String> argsForZygote = new ArrayList<String>();
575
576 // --runtime-init, --setuid=, --setgid=,
577 // and --setgroups= must go first
578 argsForZygote.add("--runtime-init");
579 argsForZygote.add("--setuid=" + uid);
580 argsForZygote.add("--setgid=" + gid);
Elliott Hughesae07ecf2011-07-06 17:33:27 -0700581 if ((debugFlags & Zygote.DEBUG_ENABLE_JNI_LOGGING) != 0) {
582 argsForZygote.add("--enable-jni-logging");
583 }
Ben Cheng23085b72010-02-08 16:06:32 -0800584 if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) {
585 argsForZygote.add("--enable-safemode");
586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) {
588 argsForZygote.add("--enable-debugger");
589 }
590 if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) {
591 argsForZygote.add("--enable-checkjni");
592 }
593 if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) {
594 argsForZygote.add("--enable-assert");
595 }
Jeff Sharkey2bca8682012-08-22 13:59:58 -0700596 if (mountExternal == Zygote.MOUNT_EXTERNAL_MULTIUSER) {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700597 argsForZygote.add("--mount-external-multiuser");
Jeff Sharkeye217ee42012-08-28 16:23:01 -0700598 } else if (mountExternal == Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL) {
599 argsForZygote.add("--mount-external-multiuser-all");
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700600 }
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700601 argsForZygote.add("--target-sdk-version=" + targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602
603 //TODO optionally enable debuger
604 //argsForZygote.add("--enable-debugger");
605
606 // --setgroups is a comma-separated list
607 if (gids != null && gids.length > 0) {
608 StringBuilder sb = new StringBuilder();
609 sb.append("--setgroups=");
610
611 int sz = gids.length;
612 for (int i = 0; i < sz; i++) {
613 if (i != 0) {
614 sb.append(',');
615 }
616 sb.append(gids[i]);
617 }
618
619 argsForZygote.add(sb.toString());
620 }
621
622 if (niceName != null) {
623 argsForZygote.add("--nice-name=" + niceName);
624 }
625
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500626 if (seInfo != null) {
627 argsForZygote.add("--seinfo=" + seInfo);
628 }
629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 argsForZygote.add(processClass);
631
632 if (extraArgs != null) {
633 for (String arg : extraArgs) {
634 argsForZygote.add(arg);
635 }
636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637
Jeff Brown3f9dd282011-07-08 20:02:19 -0700638 return zygoteSendArgsAndGetResult(argsForZygote);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 }
641
642 /**
643 * Returns elapsed milliseconds of the time this process has run.
644 * @return Returns the number of milliseconds this process has return.
645 */
646 public static final native long getElapsedCpuTime();
647
648 /**
649 * Returns the identifier of this process, which can be used with
650 * {@link #killProcess} and {@link #sendSignal}.
651 */
Jeff Hao406ec152013-07-30 10:13:41 -0700652 public static final int myPid() {
653 return Libcore.os.getpid();
654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655
656 /**
657 * Returns the identifier of the calling thread, which be used with
658 * {@link #setThreadPriority(int, int)}.
659 */
660 public static final native int myTid();
661
662 /**
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700663 * Returns the identifier of this process's uid. This is the kernel uid
664 * that the process is running under, which is the identity of its
665 * app-specific sandbox. It is different from {@link #myUserHandle} in that
666 * a uid identifies a specific app sandbox in a specific user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 */
Jeff Hao406ec152013-07-30 10:13:41 -0700668 public static final int myUid() {
669 return Libcore.os.getuid();
670 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671
672 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700673 * Returns this process's user handle. This is the
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700674 * user the process is running under. It is distinct from
675 * {@link #myUid()} in that a particular user will have multiple
676 * distinct apps running under it each with their own uid.
677 */
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700678 public static final UserHandle myUserHandle() {
679 return new UserHandle(UserHandle.getUserId(myUid()));
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700680 }
681
682 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800683 * Returns whether the current process is in an isolated sandbox.
684 * @hide
685 */
686 public static final boolean isIsolated() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700687 int uid = UserHandle.getAppId(myUid());
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800688 return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID;
689 }
690
691 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 * Returns the UID assigned to a particular user name, or -1 if there is
693 * none. If the given string consists of only numbers, it is converted
694 * directly to a uid.
695 */
696 public static final native int getUidForName(String name);
697
698 /**
699 * Returns the GID assigned to a particular user name, or -1 if there is
700 * none. If the given string consists of only numbers, it is converted
701 * directly to a gid.
702 */
703 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700704
705 /**
706 * Returns a uid for a currently running process.
707 * @param pid the process id
708 * @return the uid of the process, or -1 if the process is not running.
709 * @hide pending API council review
710 */
711 public static final int getUidForPid(int pid) {
712 String[] procStatusLabels = { "Uid:" };
713 long[] procStatusValues = new long[1];
714 procStatusValues[0] = -1;
715 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
716 return (int) procStatusValues[0];
717 }
718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700720 * Returns the parent process id for a currently running process.
721 * @param pid the process id
722 * @return the parent process id of the process, or -1 if the process is not running.
723 * @hide
724 */
725 public static final int getParentPid(int pid) {
726 String[] procStatusLabels = { "PPid:" };
727 long[] procStatusValues = new long[1];
728 procStatusValues[0] = -1;
729 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
730 return (int) procStatusValues[0];
731 }
732
733 /**
Glenn Kasten07b04652012-04-23 15:00:43 -0700734 * Returns the thread group leader id for a currently running thread.
735 * @param tid the thread id
736 * @return the thread group leader id of the thread, or -1 if the thread is not running.
737 * This is same as what getpid(2) would return if called by tid.
738 * @hide
739 */
740 public static final int getThreadGroupLeader(int tid) {
741 String[] procStatusLabels = { "Tgid:" };
742 long[] procStatusValues = new long[1];
743 procStatusValues[0] = -1;
744 Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues);
745 return (int) procStatusValues[0];
746 }
747
748 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 * Set the priority of a thread, based on Linux priorities.
750 *
751 * @param tid The identifier of the thread/process to change.
752 * @param priority A Linux priority level, from -20 for highest scheduling
753 * priority to 19 for lowest scheduling priority.
754 *
755 * @throws IllegalArgumentException Throws IllegalArgumentException if
756 * <var>tid</var> does not exist.
757 * @throws SecurityException Throws SecurityException if your process does
758 * not have permission to modify the given thread, or to use the given
759 * priority.
760 */
761 public static final native void setThreadPriority(int tid, int priority)
762 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700763
764 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700765 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
766 * throw an exception if passed a background-level thread priority. This is only
767 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
768 *
769 * @hide
770 */
771 public static final native void setCanSelfBackground(boolean backgroundOk);
772
773 /**
San Mehate9d376b2009-04-21 14:06:36 -0700774 * Sets the scheduling group for a thread.
775 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700776 * @param tid The identifier of the thread to change.
777 * @param group The target group for this thread from THREAD_GROUP_*.
San Mehate9d376b2009-04-21 14:06:36 -0700778 *
779 * @throws IllegalArgumentException Throws IllegalArgumentException if
780 * <var>tid</var> does not exist.
781 * @throws SecurityException Throws SecurityException if your process does
782 * not have permission to modify the given thread, or to use the given
783 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700784 * If the thread is a thread group leader, that is it's gettid() == getpid(),
785 * then the other threads in the same thread group are _not_ affected.
San Mehate9d376b2009-04-21 14:06:36 -0700786 */
787 public static final native void setThreadGroup(int tid, int group)
788 throws IllegalArgumentException, SecurityException;
Glenn Kastenf1b56442012-03-15 16:33:43 -0700789
San Mehat3e458242009-05-19 14:44:16 -0700790 /**
791 * Sets the scheduling group for a process and all child threads
792 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700793 * @param pid The identifier of the process to change.
794 * @param group The target group for this process from THREAD_GROUP_*.
San Mehat3e458242009-05-19 14:44:16 -0700795 *
796 * @throws IllegalArgumentException Throws IllegalArgumentException if
797 * <var>tid</var> does not exist.
798 * @throws SecurityException Throws SecurityException if your process does
799 * not have permission to modify the given thread, or to use the given
800 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700801 *
802 * group == THREAD_GROUP_DEFAULT means to move all non-background priority
803 * threads to the foreground scheduling group, but to leave background
804 * priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all
805 * threads, regardless of priority, to the background scheduling group.
806 * group == THREAD_GROUP_FOREGROUND is not allowed.
San Mehat3e458242009-05-19 14:44:16 -0700807 */
808 public static final native void setProcessGroup(int pid, int group)
809 throws IllegalArgumentException, SecurityException;
Jeff Sharkey9e57c412013-01-17 14:12:41 -0800810
811 /**
812 * Return the scheduling group of requested process.
813 *
814 * @hide
815 */
816 public static final native int getProcessGroup(int pid)
817 throws IllegalArgumentException, SecurityException;
818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 /**
820 * Set the priority of the calling thread, based on Linux priorities. See
821 * {@link #setThreadPriority(int, int)} for more information.
822 *
823 * @param priority A Linux priority level, from -20 for highest scheduling
824 * priority to 19 for lowest scheduling priority.
825 *
826 * @throws IllegalArgumentException Throws IllegalArgumentException if
827 * <var>tid</var> does not exist.
828 * @throws SecurityException Throws SecurityException if your process does
829 * not have permission to modify the given thread, or to use the given
830 * priority.
831 *
832 * @see #setThreadPriority(int, int)
833 */
834 public static final native void setThreadPriority(int priority)
835 throws IllegalArgumentException, SecurityException;
836
837 /**
838 * Return the current priority of a thread, based on Linux priorities.
839 *
840 * @param tid The identifier of the thread/process to change.
841 *
842 * @return Returns the current priority, as a Linux priority level,
843 * from -20 for highest scheduling priority to 19 for lowest scheduling
844 * priority.
845 *
846 * @throws IllegalArgumentException Throws IllegalArgumentException if
847 * <var>tid</var> does not exist.
848 */
849 public static final native int getThreadPriority(int tid)
850 throws IllegalArgumentException;
851
852 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700853 * Set the scheduling policy and priority of a thread, based on Linux.
854 *
855 * @param tid The identifier of the thread/process to change.
856 * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
857 * @param priority A Linux priority level in a range appropriate for the given policy.
858 *
859 * @throws IllegalArgumentException Throws IllegalArgumentException if
860 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
861 * @throws SecurityException Throws SecurityException if your process does
862 * not have permission to modify the given thread, or to use the given
863 * scheduling policy or priority.
864 *
865 * {@hide}
866 */
867 public static final native void setThreadScheduler(int tid, int policy, int priority)
868 throws IllegalArgumentException;
869
870 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 * Determine whether the current environment supports multiple processes.
872 *
873 * @return Returns true if the system can run in multiple processes, else
874 * false if everything is running in a single process.
Jeff Brown10e89712011-07-08 18:52:57 -0700875 *
876 * @deprecated This method always returns true. Do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 */
Jeff Brown10e89712011-07-08 18:52:57 -0700878 @Deprecated
879 public static final boolean supportsProcesses() {
880 return true;
881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882
883 /**
884 * Set the out-of-memory badness adjustment for a process.
885 *
886 * @param pid The process identifier to set.
887 * @param amt Adjustment value -- linux allows -16 to +15.
888 *
889 * @return Returns true if the underlying system supports this
890 * feature, else false.
891 *
892 * {@hide}
893 */
894 public static final native boolean setOomAdj(int pid, int amt);
895
896 /**
897 * Change this process's argv[0] parameter. This can be useful to show
898 * more descriptive information in things like the 'ps' command.
899 *
900 * @param text The new name of this process.
901 *
902 * {@hide}
903 */
904 public static final native void setArgV0(String text);
905
906 /**
907 * Kill the process with the given PID.
908 * Note that, though this API allows us to request to
909 * kill any process based on its PID, the kernel will
910 * still impose standard restrictions on which PIDs you
911 * are actually able to kill. Typically this means only
912 * the process running the caller's packages/application
913 * and any additional processes created by that app; packages
914 * sharing a common UID will also be able to kill each
915 * other's processes.
916 */
917 public static final void killProcess(int pid) {
918 sendSignal(pid, SIGNAL_KILL);
919 }
920
921 /** @hide */
922 public static final native int setUid(int uid);
923
924 /** @hide */
925 public static final native int setGid(int uid);
926
927 /**
928 * Send a signal to the given process.
929 *
930 * @param pid The pid of the target process.
931 * @param signal The signal to send.
932 */
933 public static final native void sendSignal(int pid, int signal);
934
Dianne Hackborn906497c2010-05-10 15:57:38 -0700935 /**
936 * @hide
937 * Private impl for avoiding a log message... DO NOT USE without doing
938 * your own log, or the Android Illuminati will find you some night and
939 * beat you up.
940 */
941 public static final void killProcessQuiet(int pid) {
942 sendSignalQuiet(pid, SIGNAL_KILL);
943 }
944
945 /**
946 * @hide
947 * Private impl for avoiding a log message... DO NOT USE without doing
948 * your own log, or the Android Illuminati will find you some night and
949 * beat you up.
950 */
951 public static final native void sendSignalQuiet(int pid, int signal);
952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 /** @hide */
Marco Nelissen0bca96b2009-07-17 12:59:25 -0700954 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955
956 /** @hide */
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700957 public static final native long getTotalMemory();
958
959 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 public static final native void readProcLines(String path,
961 String[] reqFields, long[] outSizes);
962
963 /** @hide */
964 public static final native int[] getPids(String path, int[] lastArray);
965
966 /** @hide */
967 public static final int PROC_TERM_MASK = 0xff;
968 /** @hide */
969 public static final int PROC_ZERO_TERM = 0;
970 /** @hide */
971 public static final int PROC_SPACE_TERM = (int)' ';
972 /** @hide */
Evan Millarc64edde2009-04-18 12:26:32 -0700973 public static final int PROC_TAB_TERM = (int)'\t';
974 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 public static final int PROC_COMBINE = 0x100;
976 /** @hide */
977 public static final int PROC_PARENS = 0x200;
978 /** @hide */
979 public static final int PROC_OUT_STRING = 0x1000;
980 /** @hide */
981 public static final int PROC_OUT_LONG = 0x2000;
982 /** @hide */
983 public static final int PROC_OUT_FLOAT = 0x4000;
984
985 /** @hide */
986 public static final native boolean readProcFile(String file, int[] format,
987 String[] outStrings, long[] outLongs, float[] outFloats);
Evan Millarc64edde2009-04-18 12:26:32 -0700988
989 /** @hide */
990 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
991 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700993 /** @hide */
994 public static final native int[] getPidsForCommands(String[] cmds);
995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 /**
997 * Gets the total Pss value for a given process, in bytes.
998 *
999 * @param pid the process to the Pss for
1000 * @return the total Pss value for the given process in bytes,
1001 * or -1 if the value cannot be determined
1002 * @hide
1003 */
1004 public static final native long getPss(int pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -07001005
1006 /**
1007 * Specifies the outcome of having started a process.
1008 * @hide
1009 */
1010 public static final class ProcessStartResult {
1011 /**
1012 * The PID of the newly started process.
1013 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
1014 */
1015 public int pid;
1016
1017 /**
1018 * True if the process was started with a wrapper attached.
1019 */
1020 public boolean usingWrapper;
1021 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022}