blob: 770bf1c47ee9a4f266567ab2da30009321f48ad1 [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
269 /**
San Mehate9d376b2009-04-21 14:06:36 -0700270 * Default thread group - gets a 'normal' share of the CPU
271 * @hide
272 */
273 public static final int THREAD_GROUP_DEFAULT = 0;
274
275 /**
276 * Background non-interactive thread group - All threads in
277 * this group are scheduled with a reduced share of the CPU.
278 * @hide
279 */
280 public static final int THREAD_GROUP_BG_NONINTERACTIVE = 1;
281
282 /**
283 * Foreground 'boost' thread group - All threads in
284 * this group are scheduled with an increased share of the CPU
285 * @hide
286 **/
287 public static final int THREAD_GROUP_FG_BOOST = 2;
288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 public static final int SIGNAL_QUIT = 3;
290 public static final int SIGNAL_KILL = 9;
291 public static final int SIGNAL_USR1 = 10;
292
293 // State for communicating with zygote process
294
295 static LocalSocket sZygoteSocket;
296 static DataInputStream sZygoteInputStream;
297 static BufferedWriter sZygoteWriter;
298
299 /** true if previous zygote open failed */
300 static boolean sPreviousZygoteOpenFailed;
301
302 /**
303 * Start a new process.
304 *
305 * <p>If processes are enabled, a new process is created and the
306 * static main() function of a <var>processClass</var> is executed there.
307 * The process will continue running after this function returns.
308 *
309 * <p>If processes are not enabled, a new thread in the caller's
310 * process is created and main() of <var>processClass</var> called there.
311 *
312 * <p>The niceName parameter, if not an empty string, is a custom name to
313 * give to the process instead of using processClass. This allows you to
314 * make easily identifyable processes even if you are using the same base
315 * <var>processClass</var> to start them.
316 *
317 * @param processClass The class to use as the process's main entry
318 * point.
319 * @param niceName A more readable name to use for the process.
320 * @param uid The user-id under which the process will run.
321 * @param gid The group-id under which the process will run.
322 * @param gids Additional group-ids associated with the process.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700323 * @param debugFlags Additional flags.
324 * @param targetSdkVersion The target SDK version for the app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 * @param zygoteArgs Additional arguments to supply to the zygote process.
326 *
Jeff Brown3f9dd282011-07-08 20:02:19 -0700327 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 * @throws RuntimeException on fatal start failure
329 *
330 * {@hide}
331 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700332 public static final ProcessStartResult start(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 final String niceName,
334 int uid, int gid, int[] gids,
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700335 int debugFlags, int targetSdkVersion,
Jeff Brown10e89712011-07-08 18:52:57 -0700336 String[] zygoteArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 try {
Jeff Brown10e89712011-07-08 18:52:57 -0700338 return startViaZygote(processClass, niceName, uid, gid, gids,
339 debugFlags, targetSdkVersion, zygoteArgs);
340 } catch (ZygoteStartFailedEx ex) {
341 Log.e(LOG_TAG,
342 "Starting VM process through Zygote failed");
343 throw new RuntimeException(
344 "Starting VM process through Zygote failed", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 }
347
348 /** retry interval for opening a zygote socket */
349 static final int ZYGOTE_RETRY_MILLIS = 500;
350
351 /**
352 * Tries to open socket to Zygote process if not already open. If
353 * already open, does nothing. May block and retry.
354 */
355 private static void openZygoteSocketIfNeeded()
356 throws ZygoteStartFailedEx {
357
358 int retryCount;
359
360 if (sPreviousZygoteOpenFailed) {
361 /*
362 * If we've failed before, expect that we'll fail again and
363 * don't pause for retries.
364 */
365 retryCount = 0;
366 } else {
367 retryCount = 10;
368 }
369
370 /*
371 * See bug #811181: Sometimes runtime can make it up before zygote.
372 * Really, we'd like to do something better to avoid this condition,
373 * but for now just wait a bit...
374 */
375 for (int retry = 0
376 ; (sZygoteSocket == null) && (retry < (retryCount + 1))
377 ; retry++ ) {
378
379 if (retry > 0) {
380 try {
381 Log.i("Zygote", "Zygote not up yet, sleeping...");
382 Thread.sleep(ZYGOTE_RETRY_MILLIS);
383 } catch (InterruptedException ex) {
384 // should never happen
385 }
386 }
387
388 try {
389 sZygoteSocket = new LocalSocket();
390
391 sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET,
392 LocalSocketAddress.Namespace.RESERVED));
393
394 sZygoteInputStream
395 = new DataInputStream(sZygoteSocket.getInputStream());
396
397 sZygoteWriter =
398 new BufferedWriter(
399 new OutputStreamWriter(
400 sZygoteSocket.getOutputStream()),
401 256);
402
403 Log.i("Zygote", "Process: zygote socket opened");
404
405 sPreviousZygoteOpenFailed = false;
406 break;
407 } catch (IOException ex) {
408 if (sZygoteSocket != null) {
409 try {
410 sZygoteSocket.close();
411 } catch (IOException ex2) {
412 Log.e(LOG_TAG,"I/O exception on close after exception",
413 ex2);
414 }
415 }
416
417 sZygoteSocket = null;
418 }
419 }
420
421 if (sZygoteSocket == null) {
422 sPreviousZygoteOpenFailed = true;
423 throw new ZygoteStartFailedEx("connect failed");
424 }
425 }
426
427 /**
428 * Sends an argument list to the zygote process, which starts a new child
429 * and returns the child's pid. Please note: the present implementation
430 * replaces newlines in the argument list with spaces.
431 * @param args argument list
Jeff Brown3f9dd282011-07-08 20:02:19 -0700432 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 * @throws ZygoteStartFailedEx if process start failed for any reason
434 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700435 private static ProcessStartResult zygoteSendArgsAndGetResult(ArrayList<String> args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 openZygoteSocketIfNeeded();
438
439 try {
440 /**
441 * See com.android.internal.os.ZygoteInit.readArgumentList()
442 * Presently the wire format to the zygote process is:
443 * a) a count of arguments (argc, in essence)
444 * b) a number of newline-separated argument strings equal to count
445 *
446 * After the zygote process reads these it will write the pid of
Jeff Brown3f9dd282011-07-08 20:02:19 -0700447 * the child or -1 on failure, followed by boolean to
448 * indicate whether a wrapper process was used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 */
450
451 sZygoteWriter.write(Integer.toString(args.size()));
452 sZygoteWriter.newLine();
453
454 int sz = args.size();
455 for (int i = 0; i < sz; i++) {
456 String arg = args.get(i);
457 if (arg.indexOf('\n') >= 0) {
458 throw new ZygoteStartFailedEx(
459 "embedded newlines not allowed");
460 }
461 sZygoteWriter.write(arg);
462 sZygoteWriter.newLine();
463 }
464
465 sZygoteWriter.flush();
466
467 // Should there be a timeout on this?
Jeff Brown3f9dd282011-07-08 20:02:19 -0700468 ProcessStartResult result = new ProcessStartResult();
469 result.pid = sZygoteInputStream.readInt();
470 if (result.pid < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 throw new ZygoteStartFailedEx("fork() failed");
472 }
Jeff Brown3f9dd282011-07-08 20:02:19 -0700473 result.usingWrapper = sZygoteInputStream.readBoolean();
474 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 } catch (IOException ex) {
476 try {
477 if (sZygoteSocket != null) {
478 sZygoteSocket.close();
479 }
480 } catch (IOException ex2) {
481 // we're going to fail anyway
482 Log.e(LOG_TAG,"I/O exception on routine close", ex2);
483 }
484
485 sZygoteSocket = null;
486
487 throw new ZygoteStartFailedEx(ex);
488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 }
490
491 /**
492 * Starts a new process via the zygote mechanism.
493 *
494 * @param processClass Class name whose static main() to run
495 * @param niceName 'nice' process name to appear in ps
496 * @param uid a POSIX uid that the new process should setuid() to
497 * @param gid a POSIX gid that the new process shuold setgid() to
498 * @param gids null-ok; a list of supplementary group IDs that the
499 * new process should setgroup() to.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700500 * @param debugFlags Additional flags.
501 * @param targetSdkVersion The target SDK version for the app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 * @param extraArgs Additional arguments to supply to the zygote process.
Jeff Brown3f9dd282011-07-08 20:02:19 -0700503 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 * @throws ZygoteStartFailedEx if process start failed for any reason
505 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700506 private static ProcessStartResult startViaZygote(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 final String niceName,
508 final int uid, final int gid,
509 final int[] gids,
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700510 int debugFlags, int targetSdkVersion,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 String[] extraArgs)
512 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 synchronized(Process.class) {
514 ArrayList<String> argsForZygote = new ArrayList<String>();
515
516 // --runtime-init, --setuid=, --setgid=,
517 // and --setgroups= must go first
518 argsForZygote.add("--runtime-init");
519 argsForZygote.add("--setuid=" + uid);
520 argsForZygote.add("--setgid=" + gid);
Elliott Hughesae07ecf2011-07-06 17:33:27 -0700521 if ((debugFlags & Zygote.DEBUG_ENABLE_JNI_LOGGING) != 0) {
522 argsForZygote.add("--enable-jni-logging");
523 }
Ben Cheng23085b72010-02-08 16:06:32 -0800524 if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) {
525 argsForZygote.add("--enable-safemode");
526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) {
528 argsForZygote.add("--enable-debugger");
529 }
530 if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) {
531 argsForZygote.add("--enable-checkjni");
532 }
533 if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) {
534 argsForZygote.add("--enable-assert");
535 }
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700536 argsForZygote.add("--target-sdk-version=" + targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537
538 //TODO optionally enable debuger
539 //argsForZygote.add("--enable-debugger");
540
541 // --setgroups is a comma-separated list
542 if (gids != null && gids.length > 0) {
543 StringBuilder sb = new StringBuilder();
544 sb.append("--setgroups=");
545
546 int sz = gids.length;
547 for (int i = 0; i < sz; i++) {
548 if (i != 0) {
549 sb.append(',');
550 }
551 sb.append(gids[i]);
552 }
553
554 argsForZygote.add(sb.toString());
555 }
556
557 if (niceName != null) {
558 argsForZygote.add("--nice-name=" + niceName);
559 }
560
561 argsForZygote.add(processClass);
562
563 if (extraArgs != null) {
564 for (String arg : extraArgs) {
565 argsForZygote.add(arg);
566 }
567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568
Jeff Brown3f9dd282011-07-08 20:02:19 -0700569 return zygoteSendArgsAndGetResult(argsForZygote);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 }
572
573 /**
574 * Returns elapsed milliseconds of the time this process has run.
575 * @return Returns the number of milliseconds this process has return.
576 */
577 public static final native long getElapsedCpuTime();
578
579 /**
580 * Returns the identifier of this process, which can be used with
581 * {@link #killProcess} and {@link #sendSignal}.
582 */
583 public static final native int myPid();
584
585 /**
586 * Returns the identifier of the calling thread, which be used with
587 * {@link #setThreadPriority(int, int)}.
588 */
589 public static final native int myTid();
590
591 /**
592 * Returns the identifier of this process's user.
593 */
594 public static final native int myUid();
595
596 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800597 * Returns whether the current process is in an isolated sandbox.
598 * @hide
599 */
600 public static final boolean isIsolated() {
601 int uid = UserId.getAppId(myUid());
602 return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID;
603 }
604
605 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 * Returns the UID assigned to a particular user name, or -1 if there is
607 * none. If the given string consists of only numbers, it is converted
608 * directly to a uid.
609 */
610 public static final native int getUidForName(String name);
611
612 /**
613 * Returns the GID assigned to a particular user name, or -1 if there is
614 * none. If the given string consists of only numbers, it is converted
615 * directly to a gid.
616 */
617 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700618
619 /**
620 * Returns a uid for a currently running process.
621 * @param pid the process id
622 * @return the uid of the process, or -1 if the process is not running.
623 * @hide pending API council review
624 */
625 public static final int getUidForPid(int pid) {
626 String[] procStatusLabels = { "Uid:" };
627 long[] procStatusValues = new long[1];
628 procStatusValues[0] = -1;
629 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
630 return (int) procStatusValues[0];
631 }
632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700634 * Returns the parent process id for a currently running process.
635 * @param pid the process id
636 * @return the parent process id of the process, or -1 if the process is not running.
637 * @hide
638 */
639 public static final int getParentPid(int pid) {
640 String[] procStatusLabels = { "PPid:" };
641 long[] procStatusValues = new long[1];
642 procStatusValues[0] = -1;
643 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
644 return (int) procStatusValues[0];
645 }
646
647 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 * Set the priority of a thread, based on Linux priorities.
649 *
650 * @param tid The identifier of the thread/process to change.
651 * @param priority A Linux priority level, from -20 for highest scheduling
652 * priority to 19 for lowest scheduling priority.
653 *
654 * @throws IllegalArgumentException Throws IllegalArgumentException if
655 * <var>tid</var> does not exist.
656 * @throws SecurityException Throws SecurityException if your process does
657 * not have permission to modify the given thread, or to use the given
658 * priority.
659 */
660 public static final native void setThreadPriority(int tid, int priority)
661 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700662
663 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700664 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
665 * throw an exception if passed a background-level thread priority. This is only
666 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
667 *
668 * @hide
669 */
670 public static final native void setCanSelfBackground(boolean backgroundOk);
671
672 /**
San Mehate9d376b2009-04-21 14:06:36 -0700673 * Sets the scheduling group for a thread.
674 * @hide
675 * @param tid The indentifier of the thread/process to change.
676 * @param group The target group for this thread/process.
677 *
678 * @throws IllegalArgumentException Throws IllegalArgumentException if
679 * <var>tid</var> does not exist.
680 * @throws SecurityException Throws SecurityException if your process does
681 * not have permission to modify the given thread, or to use the given
682 * priority.
683 */
684 public static final native void setThreadGroup(int tid, int group)
685 throws IllegalArgumentException, SecurityException;
San Mehat3e458242009-05-19 14:44:16 -0700686 /**
687 * Sets the scheduling group for a process and all child threads
688 * @hide
689 * @param pid The indentifier of the process to change.
690 * @param group The target group for this process.
691 *
692 * @throws IllegalArgumentException Throws IllegalArgumentException if
693 * <var>tid</var> does not exist.
694 * @throws SecurityException Throws SecurityException if your process does
695 * not have permission to modify the given thread, or to use the given
696 * priority.
697 */
698 public static final native void setProcessGroup(int pid, int group)
699 throws IllegalArgumentException, SecurityException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700
701 /**
702 * Set the priority of the calling thread, based on Linux priorities. See
703 * {@link #setThreadPriority(int, int)} for more information.
704 *
705 * @param priority A Linux priority level, from -20 for highest scheduling
706 * priority to 19 for lowest scheduling priority.
707 *
708 * @throws IllegalArgumentException Throws IllegalArgumentException if
709 * <var>tid</var> does not exist.
710 * @throws SecurityException Throws SecurityException if your process does
711 * not have permission to modify the given thread, or to use the given
712 * priority.
713 *
714 * @see #setThreadPriority(int, int)
715 */
716 public static final native void setThreadPriority(int priority)
717 throws IllegalArgumentException, SecurityException;
718
719 /**
720 * Return the current priority of a thread, based on Linux priorities.
721 *
722 * @param tid The identifier of the thread/process to change.
723 *
724 * @return Returns the current priority, as a Linux priority level,
725 * from -20 for highest scheduling priority to 19 for lowest scheduling
726 * priority.
727 *
728 * @throws IllegalArgumentException Throws IllegalArgumentException if
729 * <var>tid</var> does not exist.
730 */
731 public static final native int getThreadPriority(int tid)
732 throws IllegalArgumentException;
733
734 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700735 * Set the scheduling policy and priority of a thread, based on Linux.
736 *
737 * @param tid The identifier of the thread/process to change.
738 * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
739 * @param priority A Linux priority level in a range appropriate for the given policy.
740 *
741 * @throws IllegalArgumentException Throws IllegalArgumentException if
742 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
743 * @throws SecurityException Throws SecurityException if your process does
744 * not have permission to modify the given thread, or to use the given
745 * scheduling policy or priority.
746 *
747 * {@hide}
748 */
749 public static final native void setThreadScheduler(int tid, int policy, int priority)
750 throws IllegalArgumentException;
751
752 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 * Determine whether the current environment supports multiple processes.
754 *
755 * @return Returns true if the system can run in multiple processes, else
756 * false if everything is running in a single process.
Jeff Brown10e89712011-07-08 18:52:57 -0700757 *
758 * @deprecated This method always returns true. Do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 */
Jeff Brown10e89712011-07-08 18:52:57 -0700760 @Deprecated
761 public static final boolean supportsProcesses() {
762 return true;
763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764
765 /**
766 * Set the out-of-memory badness adjustment for a process.
767 *
768 * @param pid The process identifier to set.
769 * @param amt Adjustment value -- linux allows -16 to +15.
770 *
771 * @return Returns true if the underlying system supports this
772 * feature, else false.
773 *
774 * {@hide}
775 */
776 public static final native boolean setOomAdj(int pid, int amt);
777
778 /**
779 * Change this process's argv[0] parameter. This can be useful to show
780 * more descriptive information in things like the 'ps' command.
781 *
782 * @param text The new name of this process.
783 *
784 * {@hide}
785 */
786 public static final native void setArgV0(String text);
787
788 /**
789 * Kill the process with the given PID.
790 * Note that, though this API allows us to request to
791 * kill any process based on its PID, the kernel will
792 * still impose standard restrictions on which PIDs you
793 * are actually able to kill. Typically this means only
794 * the process running the caller's packages/application
795 * and any additional processes created by that app; packages
796 * sharing a common UID will also be able to kill each
797 * other's processes.
798 */
799 public static final void killProcess(int pid) {
800 sendSignal(pid, SIGNAL_KILL);
801 }
802
803 /** @hide */
804 public static final native int setUid(int uid);
805
806 /** @hide */
807 public static final native int setGid(int uid);
808
809 /**
810 * Send a signal to the given process.
811 *
812 * @param pid The pid of the target process.
813 * @param signal The signal to send.
814 */
815 public static final native void sendSignal(int pid, int signal);
816
Dianne Hackborn906497c2010-05-10 15:57:38 -0700817 /**
818 * @hide
819 * Private impl for avoiding a log message... DO NOT USE without doing
820 * your own log, or the Android Illuminati will find you some night and
821 * beat you up.
822 */
823 public static final void killProcessQuiet(int pid) {
824 sendSignalQuiet(pid, SIGNAL_KILL);
825 }
826
827 /**
828 * @hide
829 * Private impl for avoiding a log message... DO NOT USE without doing
830 * your own log, or the Android Illuminati will find you some night and
831 * beat you up.
832 */
833 public static final native void sendSignalQuiet(int pid, int signal);
834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 /** @hide */
Marco Nelissen0bca96b2009-07-17 12:59:25 -0700836 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837
838 /** @hide */
839 public static final native void readProcLines(String path,
840 String[] reqFields, long[] outSizes);
841
842 /** @hide */
843 public static final native int[] getPids(String path, int[] lastArray);
844
845 /** @hide */
846 public static final int PROC_TERM_MASK = 0xff;
847 /** @hide */
848 public static final int PROC_ZERO_TERM = 0;
849 /** @hide */
850 public static final int PROC_SPACE_TERM = (int)' ';
851 /** @hide */
Evan Millarc64edde2009-04-18 12:26:32 -0700852 public static final int PROC_TAB_TERM = (int)'\t';
853 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 public static final int PROC_COMBINE = 0x100;
855 /** @hide */
856 public static final int PROC_PARENS = 0x200;
857 /** @hide */
858 public static final int PROC_OUT_STRING = 0x1000;
859 /** @hide */
860 public static final int PROC_OUT_LONG = 0x2000;
861 /** @hide */
862 public static final int PROC_OUT_FLOAT = 0x4000;
863
864 /** @hide */
865 public static final native boolean readProcFile(String file, int[] format,
866 String[] outStrings, long[] outLongs, float[] outFloats);
Evan Millarc64edde2009-04-18 12:26:32 -0700867
868 /** @hide */
869 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
870 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871
872 /**
873 * Gets the total Pss value for a given process, in bytes.
874 *
875 * @param pid the process to the Pss for
876 * @return the total Pss value for the given process in bytes,
877 * or -1 if the value cannot be determined
878 * @hide
879 */
880 public static final native long getPss(int pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -0700881
882 /**
883 * Specifies the outcome of having started a process.
884 * @hide
885 */
886 public static final class ProcessStartResult {
887 /**
888 * The PID of the newly started process.
889 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
890 */
891 public int pid;
892
893 /**
894 * True if the process was started with a wrapper attached.
895 */
896 public boolean usingWrapper;
897 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898}