blob: 93860aa859a780edb756331fafbb4fc42211884f [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 /**
155 * Defines a secondary group id for access to the bluetooth hardware.
156 */
157 public static final int BLUETOOTH_GID = 2000;
158
159 /**
160 * Standard priority of application threads.
161 * Use with {@link #setThreadPriority(int)} and
162 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
163 * {@link java.lang.Thread} class.
164 */
165 public static final int THREAD_PRIORITY_DEFAULT = 0;
166
167 /*
168 * ***************************************
169 * ** Keep in sync with utils/threads.h **
170 * ***************************************
171 */
172
173 /**
174 * Lowest available thread priority. Only for those who really, really
175 * don't want to run if anything else is happening.
176 * Use with {@link #setThreadPriority(int)} and
177 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
178 * {@link java.lang.Thread} class.
179 */
180 public static final int THREAD_PRIORITY_LOWEST = 19;
181
182 /**
183 * Standard priority background threads. This gives your thread a slightly
184 * lower than normal priority, so that it will have less chance of impacting
185 * the responsiveness of the user interface.
186 * Use with {@link #setThreadPriority(int)} and
187 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
188 * {@link java.lang.Thread} class.
189 */
190 public static final int THREAD_PRIORITY_BACKGROUND = 10;
191
192 /**
193 * Standard priority of threads that are currently running a user interface
194 * that the user is interacting with. Applications can not normally
195 * change to this priority; the system will automatically adjust your
196 * application threads as the user moves through the UI.
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_FOREGROUND = -2;
202
203 /**
204 * Standard priority of system display threads, involved in updating
205 * the user interface. Applications can not
206 * normally change to this priority.
207 * Use with {@link #setThreadPriority(int)} and
208 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
209 * {@link java.lang.Thread} class.
210 */
211 public static final int THREAD_PRIORITY_DISPLAY = -4;
212
213 /**
214 * Standard priority of the most important display threads, for compositing
215 * the screen and retrieving input events. Applications can not normally
216 * change to this priority.
217 * Use with {@link #setThreadPriority(int)} and
218 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
219 * {@link java.lang.Thread} class.
220 */
221 public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8;
222
223 /**
224 * Standard priority of audio threads. 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_AUDIO = -16;
231
232 /**
233 * Standard priority of the most important audio threads.
234 * Applications can not normally 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_URGENT_AUDIO = -19;
240
241 /**
242 * Minimum increment to make a priority more favorable.
243 */
244 public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
245
246 /**
247 * Minimum increment to make a priority less favorable.
248 */
249 public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
250
San Mehate9d376b2009-04-21 14:06:36 -0700251 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700252 * Default scheduling policy
253 * @hide
254 */
255 public static final int SCHED_OTHER = 0;
256
257 /**
258 * First-In First-Out scheduling policy
259 * @hide
260 */
261 public static final int SCHED_FIFO = 1;
262
263 /**
264 * Round-Robin scheduling policy
265 * @hide
266 */
267 public static final int SCHED_RR = 2;
268
269 /**
270 * Batch scheduling policy
271 * @hide
272 */
273 public static final int SCHED_BATCH = 3;
274
275 /**
276 * Idle scheduling policy
277 * @hide
278 */
279 public static final int SCHED_IDLE = 5;
280
Glenn Kastenf1b56442012-03-15 16:33:43 -0700281 // Keep in sync with SP_* constants of enum type SchedPolicy
282 // declared in system/core/include/cutils/sched_policy.h,
283 // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
San Mehate9d376b2009-04-21 14:06:36 -0700284
285 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700286 * Default thread group -
287 * has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
288 * When used with setProcessGroup(), the group of each thread in the process
289 * is conditionally changed based on that thread's current priority, as follows:
290 * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
291 * are moved to foreground thread group. All other threads are left unchanged.
292 * @hide
293 */
294 public static final int THREAD_GROUP_DEFAULT = -1;
295
296 /**
297 * Background thread group - All threads in
San Mehate9d376b2009-04-21 14:06:36 -0700298 * this group are scheduled with a reduced share of the CPU.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700299 * Value is same as constant SP_BACKGROUND of enum SchedPolicy.
300 * FIXME rename to THREAD_GROUP_BACKGROUND.
San Mehate9d376b2009-04-21 14:06:36 -0700301 * @hide
302 */
Glenn Kastenf1b56442012-03-15 16:33:43 -0700303 public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0;
San Mehate9d376b2009-04-21 14:06:36 -0700304
305 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700306 * Foreground thread group - All threads in
307 * this group are scheduled with a normal share of the CPU.
308 * Value is same as constant SP_FOREGROUND of enum SchedPolicy.
309 * Not used at this level.
San Mehate9d376b2009-04-21 14:06:36 -0700310 * @hide
311 **/
Glenn Kastenf1b56442012-03-15 16:33:43 -0700312 private static final int THREAD_GROUP_FOREGROUND = 1;
San Mehate9d376b2009-04-21 14:06:36 -0700313
Glenn Kasten07b04652012-04-23 15:00:43 -0700314 /**
315 * System thread group.
316 * @hide
317 **/
318 public static final int THREAD_GROUP_SYSTEM = 2;
319
320 /**
321 * Application audio thread group.
322 * @hide
323 **/
324 public static final int THREAD_GROUP_AUDIO_APP = 3;
325
326 /**
327 * System audio thread group.
328 * @hide
329 **/
330 public static final int THREAD_GROUP_AUDIO_SYS = 4;
331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 public static final int SIGNAL_QUIT = 3;
333 public static final int SIGNAL_KILL = 9;
334 public static final int SIGNAL_USR1 = 10;
335
336 // State for communicating with zygote process
337
338 static LocalSocket sZygoteSocket;
339 static DataInputStream sZygoteInputStream;
340 static BufferedWriter sZygoteWriter;
341
342 /** true if previous zygote open failed */
343 static boolean sPreviousZygoteOpenFailed;
344
345 /**
346 * Start a new process.
347 *
348 * <p>If processes are enabled, a new process is created and the
349 * static main() function of a <var>processClass</var> is executed there.
350 * The process will continue running after this function returns.
351 *
352 * <p>If processes are not enabled, a new thread in the caller's
353 * process is created and main() of <var>processClass</var> called there.
354 *
355 * <p>The niceName parameter, if not an empty string, is a custom name to
356 * give to the process instead of using processClass. This allows you to
357 * make easily identifyable processes even if you are using the same base
358 * <var>processClass</var> to start them.
359 *
360 * @param processClass The class to use as the process's main entry
361 * point.
362 * @param niceName A more readable name to use for the process.
363 * @param uid The user-id under which the process will run.
364 * @param gid The group-id under which the process will run.
365 * @param gids Additional group-ids associated with the process.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700366 * @param debugFlags Additional flags.
367 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500368 * @param seInfo null-ok SE Android information for the new process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 * @param zygoteArgs Additional arguments to supply to the zygote process.
370 *
Jeff Brown3f9dd282011-07-08 20:02:19 -0700371 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 * @throws RuntimeException on fatal start failure
373 *
374 * {@hide}
375 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700376 public static final ProcessStartResult start(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 final String niceName,
378 int uid, int gid, int[] gids,
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700379 int debugFlags, int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500380 String seInfo,
Jeff Brown10e89712011-07-08 18:52:57 -0700381 String[] zygoteArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 try {
Jeff Brown10e89712011-07-08 18:52:57 -0700383 return startViaZygote(processClass, niceName, uid, gid, gids,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500384 debugFlags, targetSdkVersion, seInfo, zygoteArgs);
Jeff Brown10e89712011-07-08 18:52:57 -0700385 } catch (ZygoteStartFailedEx ex) {
386 Log.e(LOG_TAG,
387 "Starting VM process through Zygote failed");
388 throw new RuntimeException(
389 "Starting VM process through Zygote failed", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 }
392
393 /** retry interval for opening a zygote socket */
394 static final int ZYGOTE_RETRY_MILLIS = 500;
395
396 /**
397 * Tries to open socket to Zygote process if not already open. If
398 * already open, does nothing. May block and retry.
399 */
400 private static void openZygoteSocketIfNeeded()
401 throws ZygoteStartFailedEx {
402
403 int retryCount;
404
405 if (sPreviousZygoteOpenFailed) {
406 /*
407 * If we've failed before, expect that we'll fail again and
408 * don't pause for retries.
409 */
410 retryCount = 0;
411 } else {
412 retryCount = 10;
413 }
414
415 /*
416 * See bug #811181: Sometimes runtime can make it up before zygote.
417 * Really, we'd like to do something better to avoid this condition,
418 * but for now just wait a bit...
419 */
420 for (int retry = 0
421 ; (sZygoteSocket == null) && (retry < (retryCount + 1))
422 ; retry++ ) {
423
424 if (retry > 0) {
425 try {
426 Log.i("Zygote", "Zygote not up yet, sleeping...");
427 Thread.sleep(ZYGOTE_RETRY_MILLIS);
428 } catch (InterruptedException ex) {
429 // should never happen
430 }
431 }
432
433 try {
434 sZygoteSocket = new LocalSocket();
435
436 sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET,
437 LocalSocketAddress.Namespace.RESERVED));
438
439 sZygoteInputStream
440 = new DataInputStream(sZygoteSocket.getInputStream());
441
442 sZygoteWriter =
443 new BufferedWriter(
444 new OutputStreamWriter(
445 sZygoteSocket.getOutputStream()),
446 256);
447
448 Log.i("Zygote", "Process: zygote socket opened");
449
450 sPreviousZygoteOpenFailed = false;
451 break;
452 } catch (IOException ex) {
453 if (sZygoteSocket != null) {
454 try {
455 sZygoteSocket.close();
456 } catch (IOException ex2) {
457 Log.e(LOG_TAG,"I/O exception on close after exception",
458 ex2);
459 }
460 }
461
462 sZygoteSocket = null;
463 }
464 }
465
466 if (sZygoteSocket == null) {
467 sPreviousZygoteOpenFailed = true;
468 throw new ZygoteStartFailedEx("connect failed");
469 }
470 }
471
472 /**
473 * Sends an argument list to the zygote process, which starts a new child
474 * and returns the child's pid. Please note: the present implementation
475 * replaces newlines in the argument list with spaces.
476 * @param args argument list
Jeff Brown3f9dd282011-07-08 20:02:19 -0700477 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 * @throws ZygoteStartFailedEx if process start failed for any reason
479 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700480 private static ProcessStartResult zygoteSendArgsAndGetResult(ArrayList<String> args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 openZygoteSocketIfNeeded();
483
484 try {
485 /**
486 * See com.android.internal.os.ZygoteInit.readArgumentList()
487 * Presently the wire format to the zygote process is:
488 * a) a count of arguments (argc, in essence)
489 * b) a number of newline-separated argument strings equal to count
490 *
491 * After the zygote process reads these it will write the pid of
Jeff Brown3f9dd282011-07-08 20:02:19 -0700492 * the child or -1 on failure, followed by boolean to
493 * indicate whether a wrapper process was used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 */
495
496 sZygoteWriter.write(Integer.toString(args.size()));
497 sZygoteWriter.newLine();
498
499 int sz = args.size();
500 for (int i = 0; i < sz; i++) {
501 String arg = args.get(i);
502 if (arg.indexOf('\n') >= 0) {
503 throw new ZygoteStartFailedEx(
504 "embedded newlines not allowed");
505 }
506 sZygoteWriter.write(arg);
507 sZygoteWriter.newLine();
508 }
509
510 sZygoteWriter.flush();
511
512 // Should there be a timeout on this?
Jeff Brown3f9dd282011-07-08 20:02:19 -0700513 ProcessStartResult result = new ProcessStartResult();
514 result.pid = sZygoteInputStream.readInt();
515 if (result.pid < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 throw new ZygoteStartFailedEx("fork() failed");
517 }
Jeff Brown3f9dd282011-07-08 20:02:19 -0700518 result.usingWrapper = sZygoteInputStream.readBoolean();
519 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 } catch (IOException ex) {
521 try {
522 if (sZygoteSocket != null) {
523 sZygoteSocket.close();
524 }
525 } catch (IOException ex2) {
526 // we're going to fail anyway
527 Log.e(LOG_TAG,"I/O exception on routine close", ex2);
528 }
529
530 sZygoteSocket = null;
531
532 throw new ZygoteStartFailedEx(ex);
533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 }
535
536 /**
537 * Starts a new process via the zygote mechanism.
538 *
539 * @param processClass Class name whose static main() to run
540 * @param niceName 'nice' process name to appear in ps
541 * @param uid a POSIX uid that the new process should setuid() to
542 * @param gid a POSIX gid that the new process shuold setgid() to
543 * @param gids null-ok; a list of supplementary group IDs that the
544 * new process should setgroup() to.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700545 * @param debugFlags Additional flags.
546 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500547 * @param seInfo null-ok SE Android information for the new process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 * @param extraArgs Additional arguments to supply to the zygote process.
Jeff Brown3f9dd282011-07-08 20:02:19 -0700549 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 * @throws ZygoteStartFailedEx if process start failed for any reason
551 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700552 private static ProcessStartResult startViaZygote(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 final String niceName,
554 final int uid, final int gid,
555 final int[] gids,
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700556 int debugFlags, int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500557 String seInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 String[] extraArgs)
559 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 synchronized(Process.class) {
561 ArrayList<String> argsForZygote = new ArrayList<String>();
562
563 // --runtime-init, --setuid=, --setgid=,
564 // and --setgroups= must go first
565 argsForZygote.add("--runtime-init");
566 argsForZygote.add("--setuid=" + uid);
567 argsForZygote.add("--setgid=" + gid);
Elliott Hughesae07ecf2011-07-06 17:33:27 -0700568 if ((debugFlags & Zygote.DEBUG_ENABLE_JNI_LOGGING) != 0) {
569 argsForZygote.add("--enable-jni-logging");
570 }
Ben Cheng23085b72010-02-08 16:06:32 -0800571 if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) {
572 argsForZygote.add("--enable-safemode");
573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) {
575 argsForZygote.add("--enable-debugger");
576 }
577 if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) {
578 argsForZygote.add("--enable-checkjni");
579 }
580 if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) {
581 argsForZygote.add("--enable-assert");
582 }
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700583 argsForZygote.add("--target-sdk-version=" + targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584
585 //TODO optionally enable debuger
586 //argsForZygote.add("--enable-debugger");
587
588 // --setgroups is a comma-separated list
589 if (gids != null && gids.length > 0) {
590 StringBuilder sb = new StringBuilder();
591 sb.append("--setgroups=");
592
593 int sz = gids.length;
594 for (int i = 0; i < sz; i++) {
595 if (i != 0) {
596 sb.append(',');
597 }
598 sb.append(gids[i]);
599 }
600
601 argsForZygote.add(sb.toString());
602 }
603
604 if (niceName != null) {
605 argsForZygote.add("--nice-name=" + niceName);
606 }
607
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500608 if (seInfo != null) {
609 argsForZygote.add("--seinfo=" + seInfo);
610 }
611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 argsForZygote.add(processClass);
613
614 if (extraArgs != null) {
615 for (String arg : extraArgs) {
616 argsForZygote.add(arg);
617 }
618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619
Jeff Brown3f9dd282011-07-08 20:02:19 -0700620 return zygoteSendArgsAndGetResult(argsForZygote);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 }
623
624 /**
625 * Returns elapsed milliseconds of the time this process has run.
626 * @return Returns the number of milliseconds this process has return.
627 */
628 public static final native long getElapsedCpuTime();
629
630 /**
631 * Returns the identifier of this process, which can be used with
632 * {@link #killProcess} and {@link #sendSignal}.
633 */
634 public static final native int myPid();
635
636 /**
637 * Returns the identifier of the calling thread, which be used with
638 * {@link #setThreadPriority(int, int)}.
639 */
640 public static final native int myTid();
641
642 /**
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700643 * Returns the identifier of this process's uid. This is the kernel uid
644 * that the process is running under, which is the identity of its
645 * app-specific sandbox. It is different from {@link #myUserHandle} in that
646 * a uid identifies a specific app sandbox in a specific user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 */
648 public static final native int myUid();
649
650 /**
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700651 * Returns the identifier of this process's user handle. This is the
652 * user the process is running under. It is distinct from
653 * {@link #myUid()} in that a particular user will have multiple
654 * distinct apps running under it each with their own uid.
655 */
656 public static final int myUserHandle() {
657 return UserId.getUserId(myUid());
658 }
659
660 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800661 * Returns whether the current process is in an isolated sandbox.
662 * @hide
663 */
664 public static final boolean isIsolated() {
665 int uid = UserId.getAppId(myUid());
666 return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID;
667 }
668
669 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 * Returns the UID assigned to a particular user name, or -1 if there is
671 * none. If the given string consists of only numbers, it is converted
672 * directly to a uid.
673 */
674 public static final native int getUidForName(String name);
675
676 /**
677 * Returns the GID assigned to a particular user name, or -1 if there is
678 * none. If the given string consists of only numbers, it is converted
679 * directly to a gid.
680 */
681 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700682
683 /**
684 * Returns a uid for a currently running process.
685 * @param pid the process id
686 * @return the uid of the process, or -1 if the process is not running.
687 * @hide pending API council review
688 */
689 public static final int getUidForPid(int pid) {
690 String[] procStatusLabels = { "Uid:" };
691 long[] procStatusValues = new long[1];
692 procStatusValues[0] = -1;
693 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
694 return (int) procStatusValues[0];
695 }
696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700698 * Returns the parent process id for a currently running process.
699 * @param pid the process id
700 * @return the parent process id of the process, or -1 if the process is not running.
701 * @hide
702 */
703 public static final int getParentPid(int pid) {
704 String[] procStatusLabels = { "PPid:" };
705 long[] procStatusValues = new long[1];
706 procStatusValues[0] = -1;
707 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
708 return (int) procStatusValues[0];
709 }
710
711 /**
Glenn Kasten07b04652012-04-23 15:00:43 -0700712 * Returns the thread group leader id for a currently running thread.
713 * @param tid the thread id
714 * @return the thread group leader id of the thread, or -1 if the thread is not running.
715 * This is same as what getpid(2) would return if called by tid.
716 * @hide
717 */
718 public static final int getThreadGroupLeader(int tid) {
719 String[] procStatusLabels = { "Tgid:" };
720 long[] procStatusValues = new long[1];
721 procStatusValues[0] = -1;
722 Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues);
723 return (int) procStatusValues[0];
724 }
725
726 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 * Set the priority of a thread, based on Linux priorities.
728 *
729 * @param tid The identifier of the thread/process to change.
730 * @param priority A Linux priority level, from -20 for highest scheduling
731 * priority to 19 for lowest scheduling priority.
732 *
733 * @throws IllegalArgumentException Throws IllegalArgumentException if
734 * <var>tid</var> does not exist.
735 * @throws SecurityException Throws SecurityException if your process does
736 * not have permission to modify the given thread, or to use the given
737 * priority.
738 */
739 public static final native void setThreadPriority(int tid, int priority)
740 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700741
742 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700743 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
744 * throw an exception if passed a background-level thread priority. This is only
745 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
746 *
747 * @hide
748 */
749 public static final native void setCanSelfBackground(boolean backgroundOk);
750
751 /**
San Mehate9d376b2009-04-21 14:06:36 -0700752 * Sets the scheduling group for a thread.
753 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700754 * @param tid The identifier of the thread to change.
755 * @param group The target group for this thread from THREAD_GROUP_*.
San Mehate9d376b2009-04-21 14:06:36 -0700756 *
757 * @throws IllegalArgumentException Throws IllegalArgumentException if
758 * <var>tid</var> does not exist.
759 * @throws SecurityException Throws SecurityException if your process does
760 * not have permission to modify the given thread, or to use the given
761 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700762 * If the thread is a thread group leader, that is it's gettid() == getpid(),
763 * then the other threads in the same thread group are _not_ affected.
San Mehate9d376b2009-04-21 14:06:36 -0700764 */
765 public static final native void setThreadGroup(int tid, int group)
766 throws IllegalArgumentException, SecurityException;
Glenn Kastenf1b56442012-03-15 16:33:43 -0700767
San Mehat3e458242009-05-19 14:44:16 -0700768 /**
769 * Sets the scheduling group for a process and all child threads
770 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700771 * @param pid The identifier of the process to change.
772 * @param group The target group for this process from THREAD_GROUP_*.
San Mehat3e458242009-05-19 14:44:16 -0700773 *
774 * @throws IllegalArgumentException Throws IllegalArgumentException if
775 * <var>tid</var> does not exist.
776 * @throws SecurityException Throws SecurityException if your process does
777 * not have permission to modify the given thread, or to use the given
778 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700779 *
780 * group == THREAD_GROUP_DEFAULT means to move all non-background priority
781 * threads to the foreground scheduling group, but to leave background
782 * priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all
783 * threads, regardless of priority, to the background scheduling group.
784 * group == THREAD_GROUP_FOREGROUND is not allowed.
San Mehat3e458242009-05-19 14:44:16 -0700785 */
786 public static final native void setProcessGroup(int pid, int group)
787 throws IllegalArgumentException, SecurityException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788
789 /**
790 * Set the priority of the calling thread, based on Linux priorities. See
791 * {@link #setThreadPriority(int, int)} for more information.
792 *
793 * @param priority A Linux priority level, from -20 for highest scheduling
794 * priority to 19 for lowest scheduling priority.
795 *
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.
801 *
802 * @see #setThreadPriority(int, int)
803 */
804 public static final native void setThreadPriority(int priority)
805 throws IllegalArgumentException, SecurityException;
806
807 /**
808 * Return the current priority of a thread, based on Linux priorities.
809 *
810 * @param tid The identifier of the thread/process to change.
811 *
812 * @return Returns the current priority, as a Linux priority level,
813 * from -20 for highest scheduling priority to 19 for lowest scheduling
814 * priority.
815 *
816 * @throws IllegalArgumentException Throws IllegalArgumentException if
817 * <var>tid</var> does not exist.
818 */
819 public static final native int getThreadPriority(int tid)
820 throws IllegalArgumentException;
821
822 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700823 * Set the scheduling policy and priority of a thread, based on Linux.
824 *
825 * @param tid The identifier of the thread/process to change.
826 * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
827 * @param priority A Linux priority level in a range appropriate for the given policy.
828 *
829 * @throws IllegalArgumentException Throws IllegalArgumentException if
830 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
831 * @throws SecurityException Throws SecurityException if your process does
832 * not have permission to modify the given thread, or to use the given
833 * scheduling policy or priority.
834 *
835 * {@hide}
836 */
837 public static final native void setThreadScheduler(int tid, int policy, int priority)
838 throws IllegalArgumentException;
839
840 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 * Determine whether the current environment supports multiple processes.
842 *
843 * @return Returns true if the system can run in multiple processes, else
844 * false if everything is running in a single process.
Jeff Brown10e89712011-07-08 18:52:57 -0700845 *
846 * @deprecated This method always returns true. Do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 */
Jeff Brown10e89712011-07-08 18:52:57 -0700848 @Deprecated
849 public static final boolean supportsProcesses() {
850 return true;
851 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852
853 /**
854 * Set the out-of-memory badness adjustment for a process.
855 *
856 * @param pid The process identifier to set.
857 * @param amt Adjustment value -- linux allows -16 to +15.
858 *
859 * @return Returns true if the underlying system supports this
860 * feature, else false.
861 *
862 * {@hide}
863 */
864 public static final native boolean setOomAdj(int pid, int amt);
865
866 /**
867 * Change this process's argv[0] parameter. This can be useful to show
868 * more descriptive information in things like the 'ps' command.
869 *
870 * @param text The new name of this process.
871 *
872 * {@hide}
873 */
874 public static final native void setArgV0(String text);
875
876 /**
877 * Kill the process with the given PID.
878 * Note that, though this API allows us to request to
879 * kill any process based on its PID, the kernel will
880 * still impose standard restrictions on which PIDs you
881 * are actually able to kill. Typically this means only
882 * the process running the caller's packages/application
883 * and any additional processes created by that app; packages
884 * sharing a common UID will also be able to kill each
885 * other's processes.
886 */
887 public static final void killProcess(int pid) {
888 sendSignal(pid, SIGNAL_KILL);
889 }
890
891 /** @hide */
892 public static final native int setUid(int uid);
893
894 /** @hide */
895 public static final native int setGid(int uid);
896
897 /**
898 * Send a signal to the given process.
899 *
900 * @param pid The pid of the target process.
901 * @param signal The signal to send.
902 */
903 public static final native void sendSignal(int pid, int signal);
904
Dianne Hackborn906497c2010-05-10 15:57:38 -0700905 /**
906 * @hide
907 * Private impl for avoiding a log message... DO NOT USE without doing
908 * your own log, or the Android Illuminati will find you some night and
909 * beat you up.
910 */
911 public static final void killProcessQuiet(int pid) {
912 sendSignalQuiet(pid, SIGNAL_KILL);
913 }
914
915 /**
916 * @hide
917 * Private impl for avoiding a log message... DO NOT USE without doing
918 * your own log, or the Android Illuminati will find you some night and
919 * beat you up.
920 */
921 public static final native void sendSignalQuiet(int pid, int signal);
922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 /** @hide */
Marco Nelissen0bca96b2009-07-17 12:59:25 -0700924 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925
926 /** @hide */
Dianne Hackborn59325eb2012-05-09 18:45:20 -0700927 public static final native long getTotalMemory();
928
929 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 public static final native void readProcLines(String path,
931 String[] reqFields, long[] outSizes);
932
933 /** @hide */
934 public static final native int[] getPids(String path, int[] lastArray);
935
936 /** @hide */
937 public static final int PROC_TERM_MASK = 0xff;
938 /** @hide */
939 public static final int PROC_ZERO_TERM = 0;
940 /** @hide */
941 public static final int PROC_SPACE_TERM = (int)' ';
942 /** @hide */
Evan Millarc64edde2009-04-18 12:26:32 -0700943 public static final int PROC_TAB_TERM = (int)'\t';
944 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 public static final int PROC_COMBINE = 0x100;
946 /** @hide */
947 public static final int PROC_PARENS = 0x200;
948 /** @hide */
949 public static final int PROC_OUT_STRING = 0x1000;
950 /** @hide */
951 public static final int PROC_OUT_LONG = 0x2000;
952 /** @hide */
953 public static final int PROC_OUT_FLOAT = 0x4000;
954
955 /** @hide */
956 public static final native boolean readProcFile(String file, int[] format,
957 String[] outStrings, long[] outLongs, float[] outFloats);
Evan Millarc64edde2009-04-18 12:26:32 -0700958
959 /** @hide */
960 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
961 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700963 /** @hide */
964 public static final native int[] getPidsForCommands(String[] cmds);
965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 /**
967 * Gets the total Pss value for a given process, in bytes.
968 *
969 * @param pid the process to the Pss for
970 * @return the total Pss value for the given process in bytes,
971 * or -1 if the value cannot be determined
972 * @hide
973 */
974 public static final native long getPss(int pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -0700975
976 /**
977 * Specifies the outcome of having started a process.
978 * @hide
979 */
980 public static final class ProcessStartResult {
981 /**
982 * The PID of the newly started process.
983 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
984 */
985 public int pid;
986
987 /**
988 * True if the process was started with a wrapper attached.
989 */
990 public boolean usingWrapper;
991 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992}