blob: 5b1f563a29d9f0b210615e383e74d8db79b3ca26 [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 /**
Mike Lockwood58fd98a2010-09-24 11:02:47 -040089 * Defines the GID for the group that allows write access to the SD card.
90 * @hide
91 */
92 public static final int SDCARD_RW_GID = 1015;
93
94 /**
Nick Pellycd0e8392010-10-13 17:25:24 -070095 * Defines the UID/GID for the NFC service process.
96 * @hide
97 */
Jeff Hamilton84d34072011-04-01 13:43:28 -050098 public static final int NFC_UID = 1025;
Nick Pellycd0e8392010-10-13 17:25:24 -070099
100 /**
Mike Lockwooddcaa10c2010-12-16 12:50:44 -0800101 * Defines the GID for the group that allows write access to the internal media storage.
102 * @hide
103 */
104 public static final int MEDIA_RW_GID = 1023;
105
106 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * Defines the start of a range of UIDs (and GIDs), going from this
108 * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning
109 * to applications.
110 */
111 public static final int FIRST_APPLICATION_UID = 10000;
112 /**
113 * Last of application-specific UIDs starting at
114 * {@link #FIRST_APPLICATION_UID}.
115 */
116 public static final int LAST_APPLICATION_UID = 99999;
117
118 /**
119 * Defines a secondary group id for access to the bluetooth hardware.
120 */
121 public static final int BLUETOOTH_GID = 2000;
122
123 /**
124 * Standard priority of application threads.
125 * Use with {@link #setThreadPriority(int)} and
126 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
127 * {@link java.lang.Thread} class.
128 */
129 public static final int THREAD_PRIORITY_DEFAULT = 0;
130
131 /*
132 * ***************************************
133 * ** Keep in sync with utils/threads.h **
134 * ***************************************
135 */
136
137 /**
138 * Lowest available thread priority. Only for those who really, really
139 * don't want to run if anything else is happening.
140 * Use with {@link #setThreadPriority(int)} and
141 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
142 * {@link java.lang.Thread} class.
143 */
144 public static final int THREAD_PRIORITY_LOWEST = 19;
145
146 /**
147 * Standard priority background threads. This gives your thread a slightly
148 * lower than normal priority, so that it will have less chance of impacting
149 * the responsiveness of the user interface.
150 * Use with {@link #setThreadPriority(int)} and
151 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
152 * {@link java.lang.Thread} class.
153 */
154 public static final int THREAD_PRIORITY_BACKGROUND = 10;
155
156 /**
157 * Standard priority of threads that are currently running a user interface
158 * that the user is interacting with. Applications can not normally
159 * change to this priority; the system will automatically adjust your
160 * application threads as the user moves through the UI.
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_FOREGROUND = -2;
166
167 /**
168 * Standard priority of system display threads, involved in updating
169 * the user interface. Applications can not
170 * normally change to this priority.
171 * Use with {@link #setThreadPriority(int)} and
172 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
173 * {@link java.lang.Thread} class.
174 */
175 public static final int THREAD_PRIORITY_DISPLAY = -4;
176
177 /**
178 * Standard priority of the most important display threads, for compositing
179 * the screen and retrieving input events. Applications can not normally
180 * change to this priority.
181 * Use with {@link #setThreadPriority(int)} and
182 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
183 * {@link java.lang.Thread} class.
184 */
185 public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8;
186
187 /**
188 * Standard priority of audio threads. Applications can not normally
189 * change to this priority.
190 * Use with {@link #setThreadPriority(int)} and
191 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
192 * {@link java.lang.Thread} class.
193 */
194 public static final int THREAD_PRIORITY_AUDIO = -16;
195
196 /**
197 * Standard priority of the most important audio threads.
198 * Applications can not normally change to this priority.
199 * Use with {@link #setThreadPriority(int)} and
200 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
201 * {@link java.lang.Thread} class.
202 */
203 public static final int THREAD_PRIORITY_URGENT_AUDIO = -19;
204
205 /**
206 * Minimum increment to make a priority more favorable.
207 */
208 public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
209
210 /**
211 * Minimum increment to make a priority less favorable.
212 */
213 public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
214
San Mehate9d376b2009-04-21 14:06:36 -0700215 /**
216 * Default thread group - gets a 'normal' share of the CPU
217 * @hide
218 */
219 public static final int THREAD_GROUP_DEFAULT = 0;
220
221 /**
222 * Background non-interactive thread group - All threads in
223 * this group are scheduled with a reduced share of the CPU.
224 * @hide
225 */
226 public static final int THREAD_GROUP_BG_NONINTERACTIVE = 1;
227
228 /**
229 * Foreground 'boost' thread group - All threads in
230 * this group are scheduled with an increased share of the CPU
231 * @hide
232 **/
233 public static final int THREAD_GROUP_FG_BOOST = 2;
234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 public static final int SIGNAL_QUIT = 3;
236 public static final int SIGNAL_KILL = 9;
237 public static final int SIGNAL_USR1 = 10;
238
239 // State for communicating with zygote process
240
241 static LocalSocket sZygoteSocket;
242 static DataInputStream sZygoteInputStream;
243 static BufferedWriter sZygoteWriter;
244
245 /** true if previous zygote open failed */
246 static boolean sPreviousZygoteOpenFailed;
247
248 /**
249 * Start a new process.
250 *
251 * <p>If processes are enabled, a new process is created and the
252 * static main() function of a <var>processClass</var> is executed there.
253 * The process will continue running after this function returns.
254 *
255 * <p>If processes are not enabled, a new thread in the caller's
256 * process is created and main() of <var>processClass</var> called there.
257 *
258 * <p>The niceName parameter, if not an empty string, is a custom name to
259 * give to the process instead of using processClass. This allows you to
260 * make easily identifyable processes even if you are using the same base
261 * <var>processClass</var> to start them.
262 *
263 * @param processClass The class to use as the process's main entry
264 * point.
265 * @param niceName A more readable name to use for the process.
266 * @param uid The user-id under which the process will run.
267 * @param gid The group-id under which the process will run.
268 * @param gids Additional group-ids associated with the process.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700269 * @param debugFlags Additional flags.
270 * @param targetSdkVersion The target SDK version for the app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 * @param zygoteArgs Additional arguments to supply to the zygote process.
272 *
Jeff Brown3f9dd282011-07-08 20:02:19 -0700273 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 * @throws RuntimeException on fatal start failure
275 *
276 * {@hide}
277 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700278 public static final ProcessStartResult start(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 final String niceName,
280 int uid, int gid, int[] gids,
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700281 int debugFlags, int targetSdkVersion,
Jeff Brown10e89712011-07-08 18:52:57 -0700282 String[] zygoteArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 try {
Jeff Brown10e89712011-07-08 18:52:57 -0700284 return startViaZygote(processClass, niceName, uid, gid, gids,
285 debugFlags, targetSdkVersion, zygoteArgs);
286 } catch (ZygoteStartFailedEx ex) {
287 Log.e(LOG_TAG,
288 "Starting VM process through Zygote failed");
289 throw new RuntimeException(
290 "Starting VM process through Zygote failed", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 }
293
294 /** retry interval for opening a zygote socket */
295 static final int ZYGOTE_RETRY_MILLIS = 500;
296
297 /**
298 * Tries to open socket to Zygote process if not already open. If
299 * already open, does nothing. May block and retry.
300 */
301 private static void openZygoteSocketIfNeeded()
302 throws ZygoteStartFailedEx {
303
304 int retryCount;
305
306 if (sPreviousZygoteOpenFailed) {
307 /*
308 * If we've failed before, expect that we'll fail again and
309 * don't pause for retries.
310 */
311 retryCount = 0;
312 } else {
313 retryCount = 10;
314 }
315
316 /*
317 * See bug #811181: Sometimes runtime can make it up before zygote.
318 * Really, we'd like to do something better to avoid this condition,
319 * but for now just wait a bit...
320 */
321 for (int retry = 0
322 ; (sZygoteSocket == null) && (retry < (retryCount + 1))
323 ; retry++ ) {
324
325 if (retry > 0) {
326 try {
327 Log.i("Zygote", "Zygote not up yet, sleeping...");
328 Thread.sleep(ZYGOTE_RETRY_MILLIS);
329 } catch (InterruptedException ex) {
330 // should never happen
331 }
332 }
333
334 try {
335 sZygoteSocket = new LocalSocket();
336
337 sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET,
338 LocalSocketAddress.Namespace.RESERVED));
339
340 sZygoteInputStream
341 = new DataInputStream(sZygoteSocket.getInputStream());
342
343 sZygoteWriter =
344 new BufferedWriter(
345 new OutputStreamWriter(
346 sZygoteSocket.getOutputStream()),
347 256);
348
349 Log.i("Zygote", "Process: zygote socket opened");
350
351 sPreviousZygoteOpenFailed = false;
352 break;
353 } catch (IOException ex) {
354 if (sZygoteSocket != null) {
355 try {
356 sZygoteSocket.close();
357 } catch (IOException ex2) {
358 Log.e(LOG_TAG,"I/O exception on close after exception",
359 ex2);
360 }
361 }
362
363 sZygoteSocket = null;
364 }
365 }
366
367 if (sZygoteSocket == null) {
368 sPreviousZygoteOpenFailed = true;
369 throw new ZygoteStartFailedEx("connect failed");
370 }
371 }
372
373 /**
374 * Sends an argument list to the zygote process, which starts a new child
375 * and returns the child's pid. Please note: the present implementation
376 * replaces newlines in the argument list with spaces.
377 * @param args argument list
Jeff Brown3f9dd282011-07-08 20:02:19 -0700378 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 * @throws ZygoteStartFailedEx if process start failed for any reason
380 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700381 private static ProcessStartResult zygoteSendArgsAndGetResult(ArrayList<String> args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 openZygoteSocketIfNeeded();
384
385 try {
386 /**
387 * See com.android.internal.os.ZygoteInit.readArgumentList()
388 * Presently the wire format to the zygote process is:
389 * a) a count of arguments (argc, in essence)
390 * b) a number of newline-separated argument strings equal to count
391 *
392 * After the zygote process reads these it will write the pid of
Jeff Brown3f9dd282011-07-08 20:02:19 -0700393 * the child or -1 on failure, followed by boolean to
394 * indicate whether a wrapper process was used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 */
396
397 sZygoteWriter.write(Integer.toString(args.size()));
398 sZygoteWriter.newLine();
399
400 int sz = args.size();
401 for (int i = 0; i < sz; i++) {
402 String arg = args.get(i);
403 if (arg.indexOf('\n') >= 0) {
404 throw new ZygoteStartFailedEx(
405 "embedded newlines not allowed");
406 }
407 sZygoteWriter.write(arg);
408 sZygoteWriter.newLine();
409 }
410
411 sZygoteWriter.flush();
412
413 // Should there be a timeout on this?
Jeff Brown3f9dd282011-07-08 20:02:19 -0700414 ProcessStartResult result = new ProcessStartResult();
415 result.pid = sZygoteInputStream.readInt();
416 if (result.pid < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 throw new ZygoteStartFailedEx("fork() failed");
418 }
Jeff Brown3f9dd282011-07-08 20:02:19 -0700419 result.usingWrapper = sZygoteInputStream.readBoolean();
420 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 } catch (IOException ex) {
422 try {
423 if (sZygoteSocket != null) {
424 sZygoteSocket.close();
425 }
426 } catch (IOException ex2) {
427 // we're going to fail anyway
428 Log.e(LOG_TAG,"I/O exception on routine close", ex2);
429 }
430
431 sZygoteSocket = null;
432
433 throw new ZygoteStartFailedEx(ex);
434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 }
436
437 /**
438 * Starts a new process via the zygote mechanism.
439 *
440 * @param processClass Class name whose static main() to run
441 * @param niceName 'nice' process name to appear in ps
442 * @param uid a POSIX uid that the new process should setuid() to
443 * @param gid a POSIX gid that the new process shuold setgid() to
444 * @param gids null-ok; a list of supplementary group IDs that the
445 * new process should setgroup() to.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700446 * @param debugFlags Additional flags.
447 * @param targetSdkVersion The target SDK version for the app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 * @param extraArgs Additional arguments to supply to the zygote process.
Jeff Brown3f9dd282011-07-08 20:02:19 -0700449 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 * @throws ZygoteStartFailedEx if process start failed for any reason
451 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700452 private static ProcessStartResult startViaZygote(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 final String niceName,
454 final int uid, final int gid,
455 final int[] gids,
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700456 int debugFlags, int targetSdkVersion,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 String[] extraArgs)
458 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 synchronized(Process.class) {
460 ArrayList<String> argsForZygote = new ArrayList<String>();
461
462 // --runtime-init, --setuid=, --setgid=,
463 // and --setgroups= must go first
464 argsForZygote.add("--runtime-init");
465 argsForZygote.add("--setuid=" + uid);
466 argsForZygote.add("--setgid=" + gid);
Elliott Hughesae07ecf2011-07-06 17:33:27 -0700467 if ((debugFlags & Zygote.DEBUG_ENABLE_JNI_LOGGING) != 0) {
468 argsForZygote.add("--enable-jni-logging");
469 }
Ben Cheng23085b72010-02-08 16:06:32 -0800470 if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) {
471 argsForZygote.add("--enable-safemode");
472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) {
474 argsForZygote.add("--enable-debugger");
475 }
476 if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) {
477 argsForZygote.add("--enable-checkjni");
478 }
479 if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) {
480 argsForZygote.add("--enable-assert");
481 }
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700482 argsForZygote.add("--target-sdk-version=" + targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483
484 //TODO optionally enable debuger
485 //argsForZygote.add("--enable-debugger");
486
487 // --setgroups is a comma-separated list
488 if (gids != null && gids.length > 0) {
489 StringBuilder sb = new StringBuilder();
490 sb.append("--setgroups=");
491
492 int sz = gids.length;
493 for (int i = 0; i < sz; i++) {
494 if (i != 0) {
495 sb.append(',');
496 }
497 sb.append(gids[i]);
498 }
499
500 argsForZygote.add(sb.toString());
501 }
502
503 if (niceName != null) {
504 argsForZygote.add("--nice-name=" + niceName);
505 }
506
507 argsForZygote.add(processClass);
508
509 if (extraArgs != null) {
510 for (String arg : extraArgs) {
511 argsForZygote.add(arg);
512 }
513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514
Jeff Brown3f9dd282011-07-08 20:02:19 -0700515 return zygoteSendArgsAndGetResult(argsForZygote);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 }
518
519 /**
520 * Returns elapsed milliseconds of the time this process has run.
521 * @return Returns the number of milliseconds this process has return.
522 */
523 public static final native long getElapsedCpuTime();
524
525 /**
526 * Returns the identifier of this process, which can be used with
527 * {@link #killProcess} and {@link #sendSignal}.
528 */
529 public static final native int myPid();
530
531 /**
532 * Returns the identifier of the calling thread, which be used with
533 * {@link #setThreadPriority(int, int)}.
534 */
535 public static final native int myTid();
536
537 /**
538 * Returns the identifier of this process's user.
539 */
540 public static final native int myUid();
541
542 /**
543 * Returns the UID assigned to a particular user name, or -1 if there is
544 * none. If the given string consists of only numbers, it is converted
545 * directly to a uid.
546 */
547 public static final native int getUidForName(String name);
548
549 /**
550 * Returns the GID assigned to a particular user name, or -1 if there is
551 * none. If the given string consists of only numbers, it is converted
552 * directly to a gid.
553 */
554 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700555
556 /**
557 * Returns a uid for a currently running process.
558 * @param pid the process id
559 * @return the uid of the process, or -1 if the process is not running.
560 * @hide pending API council review
561 */
562 public static final int getUidForPid(int pid) {
563 String[] procStatusLabels = { "Uid:" };
564 long[] procStatusValues = new long[1];
565 procStatusValues[0] = -1;
566 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
567 return (int) procStatusValues[0];
568 }
569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700571 * Returns the parent process id for a currently running process.
572 * @param pid the process id
573 * @return the parent process id of the process, or -1 if the process is not running.
574 * @hide
575 */
576 public static final int getParentPid(int pid) {
577 String[] procStatusLabels = { "PPid:" };
578 long[] procStatusValues = new long[1];
579 procStatusValues[0] = -1;
580 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
581 return (int) procStatusValues[0];
582 }
583
584 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 * Set the priority of a thread, based on Linux priorities.
586 *
587 * @param tid The identifier of the thread/process to change.
588 * @param priority A Linux priority level, from -20 for highest scheduling
589 * priority to 19 for lowest scheduling priority.
590 *
591 * @throws IllegalArgumentException Throws IllegalArgumentException if
592 * <var>tid</var> does not exist.
593 * @throws SecurityException Throws SecurityException if your process does
594 * not have permission to modify the given thread, or to use the given
595 * priority.
596 */
597 public static final native void setThreadPriority(int tid, int priority)
598 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700599
600 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700601 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
602 * throw an exception if passed a background-level thread priority. This is only
603 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
604 *
605 * @hide
606 */
607 public static final native void setCanSelfBackground(boolean backgroundOk);
608
609 /**
San Mehate9d376b2009-04-21 14:06:36 -0700610 * Sets the scheduling group for a thread.
611 * @hide
612 * @param tid The indentifier of the thread/process to change.
613 * @param group The target group for this thread/process.
614 *
615 * @throws IllegalArgumentException Throws IllegalArgumentException if
616 * <var>tid</var> does not exist.
617 * @throws SecurityException Throws SecurityException if your process does
618 * not have permission to modify the given thread, or to use the given
619 * priority.
620 */
621 public static final native void setThreadGroup(int tid, int group)
622 throws IllegalArgumentException, SecurityException;
San Mehat3e458242009-05-19 14:44:16 -0700623 /**
624 * Sets the scheduling group for a process and all child threads
625 * @hide
626 * @param pid The indentifier of the process to change.
627 * @param group The target group for this process.
628 *
629 * @throws IllegalArgumentException Throws IllegalArgumentException if
630 * <var>tid</var> does not exist.
631 * @throws SecurityException Throws SecurityException if your process does
632 * not have permission to modify the given thread, or to use the given
633 * priority.
634 */
635 public static final native void setProcessGroup(int pid, int group)
636 throws IllegalArgumentException, SecurityException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637
638 /**
639 * Set the priority of the calling thread, based on Linux priorities. See
640 * {@link #setThreadPriority(int, int)} for more information.
641 *
642 * @param priority A Linux priority level, from -20 for highest scheduling
643 * priority to 19 for lowest scheduling priority.
644 *
645 * @throws IllegalArgumentException Throws IllegalArgumentException if
646 * <var>tid</var> does not exist.
647 * @throws SecurityException Throws SecurityException if your process does
648 * not have permission to modify the given thread, or to use the given
649 * priority.
650 *
651 * @see #setThreadPriority(int, int)
652 */
653 public static final native void setThreadPriority(int priority)
654 throws IllegalArgumentException, SecurityException;
655
656 /**
657 * Return the current priority of a thread, based on Linux priorities.
658 *
659 * @param tid The identifier of the thread/process to change.
660 *
661 * @return Returns the current priority, as a Linux priority level,
662 * from -20 for highest scheduling priority to 19 for lowest scheduling
663 * priority.
664 *
665 * @throws IllegalArgumentException Throws IllegalArgumentException if
666 * <var>tid</var> does not exist.
667 */
668 public static final native int getThreadPriority(int tid)
669 throws IllegalArgumentException;
670
671 /**
672 * Determine whether the current environment supports multiple processes.
673 *
674 * @return Returns true if the system can run in multiple processes, else
675 * false if everything is running in a single process.
Jeff Brown10e89712011-07-08 18:52:57 -0700676 *
677 * @deprecated This method always returns true. Do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 */
Jeff Brown10e89712011-07-08 18:52:57 -0700679 @Deprecated
680 public static final boolean supportsProcesses() {
681 return true;
682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683
684 /**
685 * Set the out-of-memory badness adjustment for a process.
686 *
687 * @param pid The process identifier to set.
688 * @param amt Adjustment value -- linux allows -16 to +15.
689 *
690 * @return Returns true if the underlying system supports this
691 * feature, else false.
692 *
693 * {@hide}
694 */
695 public static final native boolean setOomAdj(int pid, int amt);
696
697 /**
698 * Change this process's argv[0] parameter. This can be useful to show
699 * more descriptive information in things like the 'ps' command.
700 *
701 * @param text The new name of this process.
702 *
703 * {@hide}
704 */
705 public static final native void setArgV0(String text);
706
707 /**
708 * Kill the process with the given PID.
709 * Note that, though this API allows us to request to
710 * kill any process based on its PID, the kernel will
711 * still impose standard restrictions on which PIDs you
712 * are actually able to kill. Typically this means only
713 * the process running the caller's packages/application
714 * and any additional processes created by that app; packages
715 * sharing a common UID will also be able to kill each
716 * other's processes.
717 */
718 public static final void killProcess(int pid) {
719 sendSignal(pid, SIGNAL_KILL);
720 }
721
722 /** @hide */
723 public static final native int setUid(int uid);
724
725 /** @hide */
726 public static final native int setGid(int uid);
727
728 /**
729 * Send a signal to the given process.
730 *
731 * @param pid The pid of the target process.
732 * @param signal The signal to send.
733 */
734 public static final native void sendSignal(int pid, int signal);
735
Dianne Hackborn906497c2010-05-10 15:57:38 -0700736 /**
737 * @hide
738 * Private impl for avoiding a log message... DO NOT USE without doing
739 * your own log, or the Android Illuminati will find you some night and
740 * beat you up.
741 */
742 public static final void killProcessQuiet(int pid) {
743 sendSignalQuiet(pid, SIGNAL_KILL);
744 }
745
746 /**
747 * @hide
748 * Private impl for avoiding a log message... DO NOT USE without doing
749 * your own log, or the Android Illuminati will find you some night and
750 * beat you up.
751 */
752 public static final native void sendSignalQuiet(int pid, int signal);
753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 /** @hide */
Marco Nelissen0bca96b2009-07-17 12:59:25 -0700755 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756
757 /** @hide */
758 public static final native void readProcLines(String path,
759 String[] reqFields, long[] outSizes);
760
761 /** @hide */
762 public static final native int[] getPids(String path, int[] lastArray);
763
764 /** @hide */
765 public static final int PROC_TERM_MASK = 0xff;
766 /** @hide */
767 public static final int PROC_ZERO_TERM = 0;
768 /** @hide */
769 public static final int PROC_SPACE_TERM = (int)' ';
770 /** @hide */
Evan Millarc64edde2009-04-18 12:26:32 -0700771 public static final int PROC_TAB_TERM = (int)'\t';
772 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 public static final int PROC_COMBINE = 0x100;
774 /** @hide */
775 public static final int PROC_PARENS = 0x200;
776 /** @hide */
777 public static final int PROC_OUT_STRING = 0x1000;
778 /** @hide */
779 public static final int PROC_OUT_LONG = 0x2000;
780 /** @hide */
781 public static final int PROC_OUT_FLOAT = 0x4000;
782
783 /** @hide */
784 public static final native boolean readProcFile(String file, int[] format,
785 String[] outStrings, long[] outLongs, float[] outFloats);
Evan Millarc64edde2009-04-18 12:26:32 -0700786
787 /** @hide */
788 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
789 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790
791 /**
792 * Gets the total Pss value for a given process, in bytes.
793 *
794 * @param pid the process to the Pss for
795 * @return the total Pss value for the given process in bytes,
796 * or -1 if the value cannot be determined
797 * @hide
798 */
799 public static final native long getPss(int pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -0700800
801 /**
802 * Specifies the outcome of having started a process.
803 * @hide
804 */
805 public static final class ProcessStartResult {
806 /**
807 * The PID of the newly started process.
808 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
809 */
810 public int pid;
811
812 /**
813 * True if the process was started with a wrapper attached.
814 */
815 public boolean usingWrapper;
816 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817}