blob: 0ba7b88535c963eacf02d5b9c745cd2f6d1b2254 [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 /**
Mike Lockwood58fd98a2010-09-24 11:02:47 -040095 * Defines the GID for the group that allows write access to the SD card.
96 * @hide
97 */
98 public static final int SDCARD_RW_GID = 1015;
99
100 /**
Kenny Root26993b32012-03-19 15:07:51 -0700101 * Defines the UID/GID for the group that controls VPN services.
102 * @hide
103 */
104 public static final int VPN_UID = 1016;
105
106 /**
Nick Pellycd0e8392010-10-13 17:25:24 -0700107 * Defines the UID/GID for the NFC service process.
108 * @hide
109 */
Nick Pellya5cb9f42011-11-21 14:54:46 -0800110 public static final int NFC_UID = 1027;
Nick Pellycd0e8392010-10-13 17:25:24 -0700111
112 /**
Mike Lockwooddcaa10c2010-12-16 12:50:44 -0800113 * Defines the GID for the group that allows write access to the internal media storage.
114 * @hide
115 */
116 public static final int MEDIA_RW_GID = 1023;
117
118 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 * Defines the start of a range of UIDs (and GIDs), going from this
120 * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning
121 * to applications.
122 */
123 public static final int FIRST_APPLICATION_UID = 10000;
124 /**
125 * Last of application-specific UIDs starting at
126 * {@link #FIRST_APPLICATION_UID}.
127 */
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800128 public static final int LAST_APPLICATION_UID = 19999;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800129
130 /**
131 * First uid used for fully isolated sandboxed processes (with no permissions of their own)
132 * @hide
133 */
134 public static final int FIRST_ISOLATED_UID = 99000;
135
136 /**
137 * Last uid used for fully isolated sandboxed processes (with no permissions of their own)
138 * @hide
139 */
140 public static final int LAST_ISOLATED_UID = 99999;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141
142 /**
143 * Defines a secondary group id for access to the bluetooth hardware.
144 */
145 public static final int BLUETOOTH_GID = 2000;
146
147 /**
148 * Standard priority of application threads.
149 * Use with {@link #setThreadPriority(int)} and
150 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
151 * {@link java.lang.Thread} class.
152 */
153 public static final int THREAD_PRIORITY_DEFAULT = 0;
154
155 /*
156 * ***************************************
157 * ** Keep in sync with utils/threads.h **
158 * ***************************************
159 */
160
161 /**
162 * Lowest available thread priority. Only for those who really, really
163 * don't want to run if anything else is happening.
164 * Use with {@link #setThreadPriority(int)} and
165 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
166 * {@link java.lang.Thread} class.
167 */
168 public static final int THREAD_PRIORITY_LOWEST = 19;
169
170 /**
171 * Standard priority background threads. This gives your thread a slightly
172 * lower than normal priority, so that it will have less chance of impacting
173 * the responsiveness of the user interface.
174 * Use with {@link #setThreadPriority(int)} and
175 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
176 * {@link java.lang.Thread} class.
177 */
178 public static final int THREAD_PRIORITY_BACKGROUND = 10;
179
180 /**
181 * Standard priority of threads that are currently running a user interface
182 * that the user is interacting with. Applications can not normally
183 * change to this priority; the system will automatically adjust your
184 * application threads as the user moves through the UI.
185 * Use with {@link #setThreadPriority(int)} and
186 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
187 * {@link java.lang.Thread} class.
188 */
189 public static final int THREAD_PRIORITY_FOREGROUND = -2;
190
191 /**
192 * Standard priority of system display threads, involved in updating
193 * the user interface. Applications can not
194 * normally change to this priority.
195 * Use with {@link #setThreadPriority(int)} and
196 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
197 * {@link java.lang.Thread} class.
198 */
199 public static final int THREAD_PRIORITY_DISPLAY = -4;
200
201 /**
202 * Standard priority of the most important display threads, for compositing
203 * the screen and retrieving input events. Applications can not normally
204 * change to this priority.
205 * Use with {@link #setThreadPriority(int)} and
206 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
207 * {@link java.lang.Thread} class.
208 */
209 public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8;
210
211 /**
212 * Standard priority of audio threads. Applications can not normally
213 * change to this priority.
214 * Use with {@link #setThreadPriority(int)} and
215 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
216 * {@link java.lang.Thread} class.
217 */
218 public static final int THREAD_PRIORITY_AUDIO = -16;
219
220 /**
221 * Standard priority of the most important audio threads.
222 * Applications can not normally change to this priority.
223 * Use with {@link #setThreadPriority(int)} and
224 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
225 * {@link java.lang.Thread} class.
226 */
227 public static final int THREAD_PRIORITY_URGENT_AUDIO = -19;
228
229 /**
230 * Minimum increment to make a priority more favorable.
231 */
232 public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
233
234 /**
235 * Minimum increment to make a priority less favorable.
236 */
237 public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
238
San Mehate9d376b2009-04-21 14:06:36 -0700239 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700240 * Default scheduling policy
241 * @hide
242 */
243 public static final int SCHED_OTHER = 0;
244
245 /**
246 * First-In First-Out scheduling policy
247 * @hide
248 */
249 public static final int SCHED_FIFO = 1;
250
251 /**
252 * Round-Robin scheduling policy
253 * @hide
254 */
255 public static final int SCHED_RR = 2;
256
257 /**
258 * Batch scheduling policy
259 * @hide
260 */
261 public static final int SCHED_BATCH = 3;
262
263 /**
264 * Idle scheduling policy
265 * @hide
266 */
267 public static final int SCHED_IDLE = 5;
268
Glenn Kastenf1b56442012-03-15 16:33:43 -0700269 // Keep in sync with SP_* constants of enum type SchedPolicy
270 // declared in system/core/include/cutils/sched_policy.h,
271 // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
San Mehate9d376b2009-04-21 14:06:36 -0700272
273 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700274 * Default thread group -
275 * has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
276 * When used with setProcessGroup(), the group of each thread in the process
277 * is conditionally changed based on that thread's current priority, as follows:
278 * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
279 * are moved to foreground thread group. All other threads are left unchanged.
280 * @hide
281 */
282 public static final int THREAD_GROUP_DEFAULT = -1;
283
284 /**
285 * Background thread group - All threads in
San Mehate9d376b2009-04-21 14:06:36 -0700286 * this group are scheduled with a reduced share of the CPU.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700287 * Value is same as constant SP_BACKGROUND of enum SchedPolicy.
288 * FIXME rename to THREAD_GROUP_BACKGROUND.
San Mehate9d376b2009-04-21 14:06:36 -0700289 * @hide
290 */
Glenn Kastenf1b56442012-03-15 16:33:43 -0700291 public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0;
San Mehate9d376b2009-04-21 14:06:36 -0700292
293 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700294 * Foreground thread group - All threads in
295 * this group are scheduled with a normal share of the CPU.
296 * Value is same as constant SP_FOREGROUND of enum SchedPolicy.
297 * Not used at this level.
San Mehate9d376b2009-04-21 14:06:36 -0700298 * @hide
299 **/
Glenn Kastenf1b56442012-03-15 16:33:43 -0700300 private static final int THREAD_GROUP_FOREGROUND = 1;
San Mehate9d376b2009-04-21 14:06:36 -0700301
Glenn Kasten07b04652012-04-23 15:00:43 -0700302 /**
303 * System thread group.
304 * @hide
305 **/
306 public static final int THREAD_GROUP_SYSTEM = 2;
307
308 /**
309 * Application audio thread group.
310 * @hide
311 **/
312 public static final int THREAD_GROUP_AUDIO_APP = 3;
313
314 /**
315 * System audio thread group.
316 * @hide
317 **/
318 public static final int THREAD_GROUP_AUDIO_SYS = 4;
319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 public static final int SIGNAL_QUIT = 3;
321 public static final int SIGNAL_KILL = 9;
322 public static final int SIGNAL_USR1 = 10;
323
324 // State for communicating with zygote process
325
326 static LocalSocket sZygoteSocket;
327 static DataInputStream sZygoteInputStream;
328 static BufferedWriter sZygoteWriter;
329
330 /** true if previous zygote open failed */
331 static boolean sPreviousZygoteOpenFailed;
332
333 /**
334 * Start a new process.
335 *
336 * <p>If processes are enabled, a new process is created and the
337 * static main() function of a <var>processClass</var> is executed there.
338 * The process will continue running after this function returns.
339 *
340 * <p>If processes are not enabled, a new thread in the caller's
341 * process is created and main() of <var>processClass</var> called there.
342 *
343 * <p>The niceName parameter, if not an empty string, is a custom name to
344 * give to the process instead of using processClass. This allows you to
345 * make easily identifyable processes even if you are using the same base
346 * <var>processClass</var> to start them.
347 *
348 * @param processClass The class to use as the process's main entry
349 * point.
350 * @param niceName A more readable name to use for the process.
351 * @param uid The user-id under which the process will run.
352 * @param gid The group-id under which the process will run.
353 * @param gids Additional group-ids associated with the process.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700354 * @param debugFlags Additional flags.
355 * @param targetSdkVersion The target SDK version for the app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 * @param zygoteArgs Additional arguments to supply to the zygote process.
357 *
Jeff Brown3f9dd282011-07-08 20:02:19 -0700358 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 * @throws RuntimeException on fatal start failure
360 *
361 * {@hide}
362 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700363 public static final ProcessStartResult start(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 final String niceName,
365 int uid, int gid, int[] gids,
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700366 int debugFlags, int targetSdkVersion,
Jeff Brown10e89712011-07-08 18:52:57 -0700367 String[] zygoteArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 try {
Jeff Brown10e89712011-07-08 18:52:57 -0700369 return startViaZygote(processClass, niceName, uid, gid, gids,
370 debugFlags, targetSdkVersion, zygoteArgs);
371 } catch (ZygoteStartFailedEx ex) {
372 Log.e(LOG_TAG,
373 "Starting VM process through Zygote failed");
374 throw new RuntimeException(
375 "Starting VM process through Zygote failed", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 }
378
379 /** retry interval for opening a zygote socket */
380 static final int ZYGOTE_RETRY_MILLIS = 500;
381
382 /**
383 * Tries to open socket to Zygote process if not already open. If
384 * already open, does nothing. May block and retry.
385 */
386 private static void openZygoteSocketIfNeeded()
387 throws ZygoteStartFailedEx {
388
389 int retryCount;
390
391 if (sPreviousZygoteOpenFailed) {
392 /*
393 * If we've failed before, expect that we'll fail again and
394 * don't pause for retries.
395 */
396 retryCount = 0;
397 } else {
398 retryCount = 10;
399 }
400
401 /*
402 * See bug #811181: Sometimes runtime can make it up before zygote.
403 * Really, we'd like to do something better to avoid this condition,
404 * but for now just wait a bit...
405 */
406 for (int retry = 0
407 ; (sZygoteSocket == null) && (retry < (retryCount + 1))
408 ; retry++ ) {
409
410 if (retry > 0) {
411 try {
412 Log.i("Zygote", "Zygote not up yet, sleeping...");
413 Thread.sleep(ZYGOTE_RETRY_MILLIS);
414 } catch (InterruptedException ex) {
415 // should never happen
416 }
417 }
418
419 try {
420 sZygoteSocket = new LocalSocket();
421
422 sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET,
423 LocalSocketAddress.Namespace.RESERVED));
424
425 sZygoteInputStream
426 = new DataInputStream(sZygoteSocket.getInputStream());
427
428 sZygoteWriter =
429 new BufferedWriter(
430 new OutputStreamWriter(
431 sZygoteSocket.getOutputStream()),
432 256);
433
434 Log.i("Zygote", "Process: zygote socket opened");
435
436 sPreviousZygoteOpenFailed = false;
437 break;
438 } catch (IOException ex) {
439 if (sZygoteSocket != null) {
440 try {
441 sZygoteSocket.close();
442 } catch (IOException ex2) {
443 Log.e(LOG_TAG,"I/O exception on close after exception",
444 ex2);
445 }
446 }
447
448 sZygoteSocket = null;
449 }
450 }
451
452 if (sZygoteSocket == null) {
453 sPreviousZygoteOpenFailed = true;
454 throw new ZygoteStartFailedEx("connect failed");
455 }
456 }
457
458 /**
459 * Sends an argument list to the zygote process, which starts a new child
460 * and returns the child's pid. Please note: the present implementation
461 * replaces newlines in the argument list with spaces.
462 * @param args argument list
Jeff Brown3f9dd282011-07-08 20:02:19 -0700463 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 * @throws ZygoteStartFailedEx if process start failed for any reason
465 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700466 private static ProcessStartResult zygoteSendArgsAndGetResult(ArrayList<String> args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 openZygoteSocketIfNeeded();
469
470 try {
471 /**
472 * See com.android.internal.os.ZygoteInit.readArgumentList()
473 * Presently the wire format to the zygote process is:
474 * a) a count of arguments (argc, in essence)
475 * b) a number of newline-separated argument strings equal to count
476 *
477 * After the zygote process reads these it will write the pid of
Jeff Brown3f9dd282011-07-08 20:02:19 -0700478 * the child or -1 on failure, followed by boolean to
479 * indicate whether a wrapper process was used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 */
481
482 sZygoteWriter.write(Integer.toString(args.size()));
483 sZygoteWriter.newLine();
484
485 int sz = args.size();
486 for (int i = 0; i < sz; i++) {
487 String arg = args.get(i);
488 if (arg.indexOf('\n') >= 0) {
489 throw new ZygoteStartFailedEx(
490 "embedded newlines not allowed");
491 }
492 sZygoteWriter.write(arg);
493 sZygoteWriter.newLine();
494 }
495
496 sZygoteWriter.flush();
497
498 // Should there be a timeout on this?
Jeff Brown3f9dd282011-07-08 20:02:19 -0700499 ProcessStartResult result = new ProcessStartResult();
500 result.pid = sZygoteInputStream.readInt();
501 if (result.pid < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 throw new ZygoteStartFailedEx("fork() failed");
503 }
Jeff Brown3f9dd282011-07-08 20:02:19 -0700504 result.usingWrapper = sZygoteInputStream.readBoolean();
505 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 } catch (IOException ex) {
507 try {
508 if (sZygoteSocket != null) {
509 sZygoteSocket.close();
510 }
511 } catch (IOException ex2) {
512 // we're going to fail anyway
513 Log.e(LOG_TAG,"I/O exception on routine close", ex2);
514 }
515
516 sZygoteSocket = null;
517
518 throw new ZygoteStartFailedEx(ex);
519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 }
521
522 /**
523 * Starts a new process via the zygote mechanism.
524 *
525 * @param processClass Class name whose static main() to run
526 * @param niceName 'nice' process name to appear in ps
527 * @param uid a POSIX uid that the new process should setuid() to
528 * @param gid a POSIX gid that the new process shuold setgid() to
529 * @param gids null-ok; a list of supplementary group IDs that the
530 * new process should setgroup() to.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700531 * @param debugFlags Additional flags.
532 * @param targetSdkVersion The target SDK version for the app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 * @param extraArgs Additional arguments to supply to the zygote process.
Jeff Brown3f9dd282011-07-08 20:02:19 -0700534 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 * @throws ZygoteStartFailedEx if process start failed for any reason
536 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700537 private static ProcessStartResult startViaZygote(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 final String niceName,
539 final int uid, final int gid,
540 final int[] gids,
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700541 int debugFlags, int targetSdkVersion,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 String[] extraArgs)
543 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 synchronized(Process.class) {
545 ArrayList<String> argsForZygote = new ArrayList<String>();
546
547 // --runtime-init, --setuid=, --setgid=,
548 // and --setgroups= must go first
549 argsForZygote.add("--runtime-init");
550 argsForZygote.add("--setuid=" + uid);
551 argsForZygote.add("--setgid=" + gid);
Elliott Hughesae07ecf2011-07-06 17:33:27 -0700552 if ((debugFlags & Zygote.DEBUG_ENABLE_JNI_LOGGING) != 0) {
553 argsForZygote.add("--enable-jni-logging");
554 }
Ben Cheng23085b72010-02-08 16:06:32 -0800555 if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) {
556 argsForZygote.add("--enable-safemode");
557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) {
559 argsForZygote.add("--enable-debugger");
560 }
561 if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) {
562 argsForZygote.add("--enable-checkjni");
563 }
564 if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) {
565 argsForZygote.add("--enable-assert");
566 }
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700567 argsForZygote.add("--target-sdk-version=" + targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568
569 //TODO optionally enable debuger
570 //argsForZygote.add("--enable-debugger");
571
572 // --setgroups is a comma-separated list
573 if (gids != null && gids.length > 0) {
574 StringBuilder sb = new StringBuilder();
575 sb.append("--setgroups=");
576
577 int sz = gids.length;
578 for (int i = 0; i < sz; i++) {
579 if (i != 0) {
580 sb.append(',');
581 }
582 sb.append(gids[i]);
583 }
584
585 argsForZygote.add(sb.toString());
586 }
587
588 if (niceName != null) {
589 argsForZygote.add("--nice-name=" + niceName);
590 }
591
592 argsForZygote.add(processClass);
593
594 if (extraArgs != null) {
595 for (String arg : extraArgs) {
596 argsForZygote.add(arg);
597 }
598 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599
Jeff Brown3f9dd282011-07-08 20:02:19 -0700600 return zygoteSendArgsAndGetResult(argsForZygote);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 }
603
604 /**
605 * Returns elapsed milliseconds of the time this process has run.
606 * @return Returns the number of milliseconds this process has return.
607 */
608 public static final native long getElapsedCpuTime();
609
610 /**
611 * Returns the identifier of this process, which can be used with
612 * {@link #killProcess} and {@link #sendSignal}.
613 */
614 public static final native int myPid();
615
616 /**
617 * Returns the identifier of the calling thread, which be used with
618 * {@link #setThreadPriority(int, int)}.
619 */
620 public static final native int myTid();
621
622 /**
623 * Returns the identifier of this process's user.
624 */
625 public static final native int myUid();
626
627 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800628 * Returns whether the current process is in an isolated sandbox.
629 * @hide
630 */
631 public static final boolean isIsolated() {
632 int uid = UserId.getAppId(myUid());
633 return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID;
634 }
635
636 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 * Returns the UID assigned to a particular user name, or -1 if there is
638 * none. If the given string consists of only numbers, it is converted
639 * directly to a uid.
640 */
641 public static final native int getUidForName(String name);
642
643 /**
644 * Returns the GID assigned to a particular user name, or -1 if there is
645 * none. If the given string consists of only numbers, it is converted
646 * directly to a gid.
647 */
648 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700649
650 /**
651 * Returns a uid for a currently running process.
652 * @param pid the process id
653 * @return the uid of the process, or -1 if the process is not running.
654 * @hide pending API council review
655 */
656 public static final int getUidForPid(int pid) {
657 String[] procStatusLabels = { "Uid:" };
658 long[] procStatusValues = new long[1];
659 procStatusValues[0] = -1;
660 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
661 return (int) procStatusValues[0];
662 }
663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700665 * Returns the parent process id for a currently running process.
666 * @param pid the process id
667 * @return the parent process id of the process, or -1 if the process is not running.
668 * @hide
669 */
670 public static final int getParentPid(int pid) {
671 String[] procStatusLabels = { "PPid:" };
672 long[] procStatusValues = new long[1];
673 procStatusValues[0] = -1;
674 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
675 return (int) procStatusValues[0];
676 }
677
678 /**
Glenn Kasten07b04652012-04-23 15:00:43 -0700679 * Returns the thread group leader id for a currently running thread.
680 * @param tid the thread id
681 * @return the thread group leader id of the thread, or -1 if the thread is not running.
682 * This is same as what getpid(2) would return if called by tid.
683 * @hide
684 */
685 public static final int getThreadGroupLeader(int tid) {
686 String[] procStatusLabels = { "Tgid:" };
687 long[] procStatusValues = new long[1];
688 procStatusValues[0] = -1;
689 Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues);
690 return (int) procStatusValues[0];
691 }
692
693 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 * Set the priority of a thread, based on Linux priorities.
695 *
696 * @param tid The identifier of the thread/process to change.
697 * @param priority A Linux priority level, from -20 for highest scheduling
698 * priority to 19 for lowest scheduling priority.
699 *
700 * @throws IllegalArgumentException Throws IllegalArgumentException if
701 * <var>tid</var> does not exist.
702 * @throws SecurityException Throws SecurityException if your process does
703 * not have permission to modify the given thread, or to use the given
704 * priority.
705 */
706 public static final native void setThreadPriority(int tid, int priority)
707 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700708
709 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700710 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
711 * throw an exception if passed a background-level thread priority. This is only
712 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
713 *
714 * @hide
715 */
716 public static final native void setCanSelfBackground(boolean backgroundOk);
717
718 /**
San Mehate9d376b2009-04-21 14:06:36 -0700719 * Sets the scheduling group for a thread.
720 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700721 * @param tid The identifier of the thread to change.
722 * @param group The target group for this thread from THREAD_GROUP_*.
San Mehate9d376b2009-04-21 14:06:36 -0700723 *
724 * @throws IllegalArgumentException Throws IllegalArgumentException if
725 * <var>tid</var> does not exist.
726 * @throws SecurityException Throws SecurityException if your process does
727 * not have permission to modify the given thread, or to use the given
728 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700729 * If the thread is a thread group leader, that is it's gettid() == getpid(),
730 * then the other threads in the same thread group are _not_ affected.
San Mehate9d376b2009-04-21 14:06:36 -0700731 */
732 public static final native void setThreadGroup(int tid, int group)
733 throws IllegalArgumentException, SecurityException;
Glenn Kastenf1b56442012-03-15 16:33:43 -0700734
San Mehat3e458242009-05-19 14:44:16 -0700735 /**
736 * Sets the scheduling group for a process and all child threads
737 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700738 * @param pid The identifier of the process to change.
739 * @param group The target group for this process from THREAD_GROUP_*.
San Mehat3e458242009-05-19 14:44:16 -0700740 *
741 * @throws IllegalArgumentException Throws IllegalArgumentException if
742 * <var>tid</var> does not exist.
743 * @throws SecurityException Throws SecurityException if your process does
744 * not have permission to modify the given thread, or to use the given
745 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700746 *
747 * group == THREAD_GROUP_DEFAULT means to move all non-background priority
748 * threads to the foreground scheduling group, but to leave background
749 * priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all
750 * threads, regardless of priority, to the background scheduling group.
751 * group == THREAD_GROUP_FOREGROUND is not allowed.
San Mehat3e458242009-05-19 14:44:16 -0700752 */
753 public static final native void setProcessGroup(int pid, int group)
754 throws IllegalArgumentException, SecurityException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755
756 /**
757 * Set the priority of the calling thread, based on Linux priorities. See
758 * {@link #setThreadPriority(int, int)} for more information.
759 *
760 * @param priority A Linux priority level, from -20 for highest scheduling
761 * priority to 19 for lowest scheduling priority.
762 *
763 * @throws IllegalArgumentException Throws IllegalArgumentException if
764 * <var>tid</var> does not exist.
765 * @throws SecurityException Throws SecurityException if your process does
766 * not have permission to modify the given thread, or to use the given
767 * priority.
768 *
769 * @see #setThreadPriority(int, int)
770 */
771 public static final native void setThreadPriority(int priority)
772 throws IllegalArgumentException, SecurityException;
773
774 /**
775 * Return the current priority of a thread, based on Linux priorities.
776 *
777 * @param tid The identifier of the thread/process to change.
778 *
779 * @return Returns the current priority, as a Linux priority level,
780 * from -20 for highest scheduling priority to 19 for lowest scheduling
781 * priority.
782 *
783 * @throws IllegalArgumentException Throws IllegalArgumentException if
784 * <var>tid</var> does not exist.
785 */
786 public static final native int getThreadPriority(int tid)
787 throws IllegalArgumentException;
788
789 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700790 * Set the scheduling policy and priority of a thread, based on Linux.
791 *
792 * @param tid The identifier of the thread/process to change.
793 * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
794 * @param priority A Linux priority level in a range appropriate for the given policy.
795 *
796 * @throws IllegalArgumentException Throws IllegalArgumentException if
797 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
798 * @throws SecurityException Throws SecurityException if your process does
799 * not have permission to modify the given thread, or to use the given
800 * scheduling policy or priority.
801 *
802 * {@hide}
803 */
804 public static final native void setThreadScheduler(int tid, int policy, int priority)
805 throws IllegalArgumentException;
806
807 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 * Determine whether the current environment supports multiple processes.
809 *
810 * @return Returns true if the system can run in multiple processes, else
811 * false if everything is running in a single process.
Jeff Brown10e89712011-07-08 18:52:57 -0700812 *
813 * @deprecated This method always returns true. Do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 */
Jeff Brown10e89712011-07-08 18:52:57 -0700815 @Deprecated
816 public static final boolean supportsProcesses() {
817 return true;
818 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819
820 /**
821 * Set the out-of-memory badness adjustment for a process.
822 *
823 * @param pid The process identifier to set.
824 * @param amt Adjustment value -- linux allows -16 to +15.
825 *
826 * @return Returns true if the underlying system supports this
827 * feature, else false.
828 *
829 * {@hide}
830 */
831 public static final native boolean setOomAdj(int pid, int amt);
832
833 /**
834 * Change this process's argv[0] parameter. This can be useful to show
835 * more descriptive information in things like the 'ps' command.
836 *
837 * @param text The new name of this process.
838 *
839 * {@hide}
840 */
841 public static final native void setArgV0(String text);
842
843 /**
844 * Kill the process with the given PID.
845 * Note that, though this API allows us to request to
846 * kill any process based on its PID, the kernel will
847 * still impose standard restrictions on which PIDs you
848 * are actually able to kill. Typically this means only
849 * the process running the caller's packages/application
850 * and any additional processes created by that app; packages
851 * sharing a common UID will also be able to kill each
852 * other's processes.
853 */
854 public static final void killProcess(int pid) {
855 sendSignal(pid, SIGNAL_KILL);
856 }
857
858 /** @hide */
859 public static final native int setUid(int uid);
860
861 /** @hide */
862 public static final native int setGid(int uid);
863
864 /**
865 * Send a signal to the given process.
866 *
867 * @param pid The pid of the target process.
868 * @param signal The signal to send.
869 */
870 public static final native void sendSignal(int pid, int signal);
871
Dianne Hackborn906497c2010-05-10 15:57:38 -0700872 /**
873 * @hide
874 * Private impl for avoiding a log message... DO NOT USE without doing
875 * your own log, or the Android Illuminati will find you some night and
876 * beat you up.
877 */
878 public static final void killProcessQuiet(int pid) {
879 sendSignalQuiet(pid, SIGNAL_KILL);
880 }
881
882 /**
883 * @hide
884 * Private impl for avoiding a log message... DO NOT USE without doing
885 * your own log, or the Android Illuminati will find you some night and
886 * beat you up.
887 */
888 public static final native void sendSignalQuiet(int pid, int signal);
889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 /** @hide */
Marco Nelissen0bca96b2009-07-17 12:59:25 -0700891 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892
893 /** @hide */
894 public static final native void readProcLines(String path,
895 String[] reqFields, long[] outSizes);
896
897 /** @hide */
898 public static final native int[] getPids(String path, int[] lastArray);
899
900 /** @hide */
901 public static final int PROC_TERM_MASK = 0xff;
902 /** @hide */
903 public static final int PROC_ZERO_TERM = 0;
904 /** @hide */
905 public static final int PROC_SPACE_TERM = (int)' ';
906 /** @hide */
Evan Millarc64edde2009-04-18 12:26:32 -0700907 public static final int PROC_TAB_TERM = (int)'\t';
908 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 public static final int PROC_COMBINE = 0x100;
910 /** @hide */
911 public static final int PROC_PARENS = 0x200;
912 /** @hide */
913 public static final int PROC_OUT_STRING = 0x1000;
914 /** @hide */
915 public static final int PROC_OUT_LONG = 0x2000;
916 /** @hide */
917 public static final int PROC_OUT_FLOAT = 0x4000;
918
919 /** @hide */
920 public static final native boolean readProcFile(String file, int[] format,
921 String[] outStrings, long[] outLongs, float[] outFloats);
Evan Millarc64edde2009-04-18 12:26:32 -0700922
923 /** @hide */
924 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
925 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926
927 /**
928 * Gets the total Pss value for a given process, in bytes.
929 *
930 * @param pid the process to the Pss for
931 * @return the total Pss value for the given process in bytes,
932 * or -1 if the value cannot be determined
933 * @hide
934 */
935 public static final native long getPss(int pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -0700936
937 /**
938 * Specifies the outcome of having started a process.
939 * @hide
940 */
941 public static final class ProcessStartResult {
942 /**
943 * The PID of the newly started process.
944 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
945 */
946 public int pid;
947
948 /**
949 * True if the process was started with a wrapper attached.
950 */
951 public boolean usingWrapper;
952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953}