blob: dbefb1fd1000d5e57511225473904e91398f6d7d [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 *
273 * @return int If > 0 the pid of the new process; if 0 the process is
274 * being emulated by a thread
275 * @throws RuntimeException on fatal start failure
276 *
277 * {@hide}
278 */
279 public static final int start(final String processClass,
280 final String niceName,
281 int uid, int gid, int[] gids,
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700282 int debugFlags, int targetSdkVersion,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 String[] zygoteArgs)
284 {
285 if (supportsProcesses()) {
286 try {
287 return startViaZygote(processClass, niceName, uid, gid, gids,
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700288 debugFlags, targetSdkVersion, zygoteArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 } catch (ZygoteStartFailedEx ex) {
290 Log.e(LOG_TAG,
291 "Starting VM process through Zygote failed");
292 throw new RuntimeException(
293 "Starting VM process through Zygote failed", ex);
294 }
295 } else {
296 // Running in single-process mode
297
298 Runnable runnable = new Runnable() {
299 public void run() {
300 Process.invokeStaticMain(processClass);
301 }
302 };
303
304 // Thread constructors must not be called with null names (see spec).
305 if (niceName != null) {
306 new Thread(runnable, niceName).start();
307 } else {
308 new Thread(runnable).start();
309 }
310
311 return 0;
312 }
313 }
314
315 /**
316 * Start a new process. Don't supply a custom nice name.
317 * {@hide}
318 */
319 public static final int start(String processClass, int uid, int gid,
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700320 int[] gids, int debugFlags, int targetSdkVersion,
321 String[] zygoteArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 return start(processClass, "", uid, gid, gids,
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700323 debugFlags, targetSdkVersion, zygoteArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 }
325
326 private static void invokeStaticMain(String className) {
327 Class cl;
328 Object args[] = new Object[1];
329
330 args[0] = new String[0]; //this is argv
331
332 try {
333 cl = Class.forName(className);
334 cl.getMethod("main", new Class[] { String[].class })
335 .invoke(null, args);
336 } catch (Exception ex) {
337 // can be: ClassNotFoundException,
338 // NoSuchMethodException, SecurityException,
339 // IllegalAccessException, IllegalArgumentException
340 // InvocationTargetException
341 // or uncaught exception from main()
342
343 Log.e(LOG_TAG, "Exception invoking static main on "
344 + className, ex);
345
346 throw new RuntimeException(ex);
347 }
348
349 }
350
351 /** retry interval for opening a zygote socket */
352 static final int ZYGOTE_RETRY_MILLIS = 500;
353
354 /**
355 * Tries to open socket to Zygote process if not already open. If
356 * already open, does nothing. May block and retry.
357 */
358 private static void openZygoteSocketIfNeeded()
359 throws ZygoteStartFailedEx {
360
361 int retryCount;
362
363 if (sPreviousZygoteOpenFailed) {
364 /*
365 * If we've failed before, expect that we'll fail again and
366 * don't pause for retries.
367 */
368 retryCount = 0;
369 } else {
370 retryCount = 10;
371 }
372
373 /*
374 * See bug #811181: Sometimes runtime can make it up before zygote.
375 * Really, we'd like to do something better to avoid this condition,
376 * but for now just wait a bit...
377 */
378 for (int retry = 0
379 ; (sZygoteSocket == null) && (retry < (retryCount + 1))
380 ; retry++ ) {
381
382 if (retry > 0) {
383 try {
384 Log.i("Zygote", "Zygote not up yet, sleeping...");
385 Thread.sleep(ZYGOTE_RETRY_MILLIS);
386 } catch (InterruptedException ex) {
387 // should never happen
388 }
389 }
390
391 try {
392 sZygoteSocket = new LocalSocket();
393
394 sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET,
395 LocalSocketAddress.Namespace.RESERVED));
396
397 sZygoteInputStream
398 = new DataInputStream(sZygoteSocket.getInputStream());
399
400 sZygoteWriter =
401 new BufferedWriter(
402 new OutputStreamWriter(
403 sZygoteSocket.getOutputStream()),
404 256);
405
406 Log.i("Zygote", "Process: zygote socket opened");
407
408 sPreviousZygoteOpenFailed = false;
409 break;
410 } catch (IOException ex) {
411 if (sZygoteSocket != null) {
412 try {
413 sZygoteSocket.close();
414 } catch (IOException ex2) {
415 Log.e(LOG_TAG,"I/O exception on close after exception",
416 ex2);
417 }
418 }
419
420 sZygoteSocket = null;
421 }
422 }
423
424 if (sZygoteSocket == null) {
425 sPreviousZygoteOpenFailed = true;
426 throw new ZygoteStartFailedEx("connect failed");
427 }
428 }
429
430 /**
431 * Sends an argument list to the zygote process, which starts a new child
432 * and returns the child's pid. Please note: the present implementation
433 * replaces newlines in the argument list with spaces.
434 * @param args argument list
435 * @return PID of new child process
436 * @throws ZygoteStartFailedEx if process start failed for any reason
437 */
438 private static int zygoteSendArgsAndGetPid(ArrayList<String> args)
439 throws ZygoteStartFailedEx {
440
441 int pid;
442
443 openZygoteSocketIfNeeded();
444
445 try {
446 /**
447 * See com.android.internal.os.ZygoteInit.readArgumentList()
448 * Presently the wire format to the zygote process is:
449 * a) a count of arguments (argc, in essence)
450 * b) a number of newline-separated argument strings equal to count
451 *
452 * After the zygote process reads these it will write the pid of
453 * the child or -1 on failure.
454 */
455
456 sZygoteWriter.write(Integer.toString(args.size()));
457 sZygoteWriter.newLine();
458
459 int sz = args.size();
460 for (int i = 0; i < sz; i++) {
461 String arg = args.get(i);
462 if (arg.indexOf('\n') >= 0) {
463 throw new ZygoteStartFailedEx(
464 "embedded newlines not allowed");
465 }
466 sZygoteWriter.write(arg);
467 sZygoteWriter.newLine();
468 }
469
470 sZygoteWriter.flush();
471
472 // Should there be a timeout on this?
473 pid = sZygoteInputStream.readInt();
474
475 if (pid < 0) {
476 throw new ZygoteStartFailedEx("fork() failed");
477 }
478 } catch (IOException ex) {
479 try {
480 if (sZygoteSocket != null) {
481 sZygoteSocket.close();
482 }
483 } catch (IOException ex2) {
484 // we're going to fail anyway
485 Log.e(LOG_TAG,"I/O exception on routine close", ex2);
486 }
487
488 sZygoteSocket = null;
489
490 throw new ZygoteStartFailedEx(ex);
491 }
492
493 return pid;
494 }
495
496 /**
497 * Starts a new process via the zygote mechanism.
498 *
499 * @param processClass Class name whose static main() to run
500 * @param niceName 'nice' process name to appear in ps
501 * @param uid a POSIX uid that the new process should setuid() to
502 * @param gid a POSIX gid that the new process shuold setgid() to
503 * @param gids null-ok; a list of supplementary group IDs that the
504 * new process should setgroup() to.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700505 * @param debugFlags Additional flags.
506 * @param targetSdkVersion The target SDK version for the app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 * @param extraArgs Additional arguments to supply to the zygote process.
508 * @return PID
509 * @throws ZygoteStartFailedEx if process start failed for any reason
510 */
511 private static int startViaZygote(final String processClass,
512 final String niceName,
513 final int uid, final int gid,
514 final int[] gids,
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700515 int debugFlags, int targetSdkVersion,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 String[] extraArgs)
517 throws ZygoteStartFailedEx {
518 int pid;
519
520 synchronized(Process.class) {
521 ArrayList<String> argsForZygote = new ArrayList<String>();
522
523 // --runtime-init, --setuid=, --setgid=,
524 // and --setgroups= must go first
525 argsForZygote.add("--runtime-init");
526 argsForZygote.add("--setuid=" + uid);
527 argsForZygote.add("--setgid=" + gid);
Elliott Hughesae07ecf2011-07-06 17:33:27 -0700528 if ((debugFlags & Zygote.DEBUG_ENABLE_JNI_LOGGING) != 0) {
529 argsForZygote.add("--enable-jni-logging");
530 }
Ben Cheng23085b72010-02-08 16:06:32 -0800531 if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) {
532 argsForZygote.add("--enable-safemode");
533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) {
535 argsForZygote.add("--enable-debugger");
536 }
537 if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) {
538 argsForZygote.add("--enable-checkjni");
539 }
540 if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) {
541 argsForZygote.add("--enable-assert");
542 }
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700543 argsForZygote.add("--target-sdk-version=" + targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544
545 //TODO optionally enable debuger
546 //argsForZygote.add("--enable-debugger");
547
548 // --setgroups is a comma-separated list
549 if (gids != null && gids.length > 0) {
550 StringBuilder sb = new StringBuilder();
551 sb.append("--setgroups=");
552
553 int sz = gids.length;
554 for (int i = 0; i < sz; i++) {
555 if (i != 0) {
556 sb.append(',');
557 }
558 sb.append(gids[i]);
559 }
560
561 argsForZygote.add(sb.toString());
562 }
563
564 if (niceName != null) {
565 argsForZygote.add("--nice-name=" + niceName);
566 }
567
568 argsForZygote.add(processClass);
569
570 if (extraArgs != null) {
571 for (String arg : extraArgs) {
572 argsForZygote.add(arg);
573 }
574 }
575
576 pid = zygoteSendArgsAndGetPid(argsForZygote);
577 }
578
579 if (pid <= 0) {
580 throw new ZygoteStartFailedEx("zygote start failed:" + pid);
581 }
582
583 return pid;
584 }
585
586 /**
587 * Returns elapsed milliseconds of the time this process has run.
588 * @return Returns the number of milliseconds this process has return.
589 */
590 public static final native long getElapsedCpuTime();
591
592 /**
593 * Returns the identifier of this process, which can be used with
594 * {@link #killProcess} and {@link #sendSignal}.
595 */
596 public static final native int myPid();
597
598 /**
599 * Returns the identifier of the calling thread, which be used with
600 * {@link #setThreadPriority(int, int)}.
601 */
602 public static final native int myTid();
603
604 /**
605 * Returns the identifier of this process's user.
606 */
607 public static final native int myUid();
608
609 /**
610 * Returns the UID assigned to a particular user name, or -1 if there is
611 * none. If the given string consists of only numbers, it is converted
612 * directly to a uid.
613 */
614 public static final native int getUidForName(String name);
615
616 /**
617 * Returns the GID assigned to a particular user name, or -1 if there is
618 * none. If the given string consists of only numbers, it is converted
619 * directly to a gid.
620 */
621 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700622
623 /**
624 * Returns a uid for a currently running process.
625 * @param pid the process id
626 * @return the uid of the process, or -1 if the process is not running.
627 * @hide pending API council review
628 */
629 public static final int getUidForPid(int pid) {
630 String[] procStatusLabels = { "Uid:" };
631 long[] procStatusValues = new long[1];
632 procStatusValues[0] = -1;
633 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
634 return (int) procStatusValues[0];
635 }
636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700638 * Returns the parent process id for a currently running process.
639 * @param pid the process id
640 * @return the parent process id of the process, or -1 if the process is not running.
641 * @hide
642 */
643 public static final int getParentPid(int pid) {
644 String[] procStatusLabels = { "PPid:" };
645 long[] procStatusValues = new long[1];
646 procStatusValues[0] = -1;
647 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
648 return (int) procStatusValues[0];
649 }
650
651 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 * Set the priority of a thread, based on Linux priorities.
653 *
654 * @param tid The identifier of the thread/process to change.
655 * @param priority A Linux priority level, from -20 for highest scheduling
656 * priority to 19 for lowest scheduling priority.
657 *
658 * @throws IllegalArgumentException Throws IllegalArgumentException if
659 * <var>tid</var> does not exist.
660 * @throws SecurityException Throws SecurityException if your process does
661 * not have permission to modify the given thread, or to use the given
662 * priority.
663 */
664 public static final native void setThreadPriority(int tid, int priority)
665 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700666
667 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700668 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
669 * throw an exception if passed a background-level thread priority. This is only
670 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
671 *
672 * @hide
673 */
674 public static final native void setCanSelfBackground(boolean backgroundOk);
675
676 /**
San Mehate9d376b2009-04-21 14:06:36 -0700677 * Sets the scheduling group for a thread.
678 * @hide
679 * @param tid The indentifier of the thread/process to change.
680 * @param group The target group for this thread/process.
681 *
682 * @throws IllegalArgumentException Throws IllegalArgumentException if
683 * <var>tid</var> does not exist.
684 * @throws SecurityException Throws SecurityException if your process does
685 * not have permission to modify the given thread, or to use the given
686 * priority.
687 */
688 public static final native void setThreadGroup(int tid, int group)
689 throws IllegalArgumentException, SecurityException;
San Mehat3e458242009-05-19 14:44:16 -0700690 /**
691 * Sets the scheduling group for a process and all child threads
692 * @hide
693 * @param pid The indentifier of the process to change.
694 * @param group The target group for this process.
695 *
696 * @throws IllegalArgumentException Throws IllegalArgumentException if
697 * <var>tid</var> does not exist.
698 * @throws SecurityException Throws SecurityException if your process does
699 * not have permission to modify the given thread, or to use the given
700 * priority.
701 */
702 public static final native void setProcessGroup(int pid, int group)
703 throws IllegalArgumentException, SecurityException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704
705 /**
706 * Set the priority of the calling thread, based on Linux priorities. See
707 * {@link #setThreadPriority(int, int)} for more information.
708 *
709 * @param priority A Linux priority level, from -20 for highest scheduling
710 * priority to 19 for lowest scheduling priority.
711 *
712 * @throws IllegalArgumentException Throws IllegalArgumentException if
713 * <var>tid</var> does not exist.
714 * @throws SecurityException Throws SecurityException if your process does
715 * not have permission to modify the given thread, or to use the given
716 * priority.
717 *
718 * @see #setThreadPriority(int, int)
719 */
720 public static final native void setThreadPriority(int priority)
721 throws IllegalArgumentException, SecurityException;
722
723 /**
724 * Return the current priority of a thread, based on Linux priorities.
725 *
726 * @param tid The identifier of the thread/process to change.
727 *
728 * @return Returns the current priority, as a Linux priority level,
729 * from -20 for highest scheduling priority to 19 for lowest scheduling
730 * priority.
731 *
732 * @throws IllegalArgumentException Throws IllegalArgumentException if
733 * <var>tid</var> does not exist.
734 */
735 public static final native int getThreadPriority(int tid)
736 throws IllegalArgumentException;
737
738 /**
739 * Determine whether the current environment supports multiple processes.
740 *
741 * @return Returns true if the system can run in multiple processes, else
742 * false if everything is running in a single process.
743 */
744 public static final native boolean supportsProcesses();
745
746 /**
747 * Set the out-of-memory badness adjustment for a process.
748 *
749 * @param pid The process identifier to set.
750 * @param amt Adjustment value -- linux allows -16 to +15.
751 *
752 * @return Returns true if the underlying system supports this
753 * feature, else false.
754 *
755 * {@hide}
756 */
757 public static final native boolean setOomAdj(int pid, int amt);
758
759 /**
760 * Change this process's argv[0] parameter. This can be useful to show
761 * more descriptive information in things like the 'ps' command.
762 *
763 * @param text The new name of this process.
764 *
765 * {@hide}
766 */
767 public static final native void setArgV0(String text);
768
769 /**
770 * Kill the process with the given PID.
771 * Note that, though this API allows us to request to
772 * kill any process based on its PID, the kernel will
773 * still impose standard restrictions on which PIDs you
774 * are actually able to kill. Typically this means only
775 * the process running the caller's packages/application
776 * and any additional processes created by that app; packages
777 * sharing a common UID will also be able to kill each
778 * other's processes.
779 */
780 public static final void killProcess(int pid) {
781 sendSignal(pid, SIGNAL_KILL);
782 }
783
784 /** @hide */
785 public static final native int setUid(int uid);
786
787 /** @hide */
788 public static final native int setGid(int uid);
789
790 /**
791 * Send a signal to the given process.
792 *
793 * @param pid The pid of the target process.
794 * @param signal The signal to send.
795 */
796 public static final native void sendSignal(int pid, int signal);
797
Dianne Hackborn906497c2010-05-10 15:57:38 -0700798 /**
799 * @hide
800 * Private impl for avoiding a log message... DO NOT USE without doing
801 * your own log, or the Android Illuminati will find you some night and
802 * beat you up.
803 */
804 public static final void killProcessQuiet(int pid) {
805 sendSignalQuiet(pid, SIGNAL_KILL);
806 }
807
808 /**
809 * @hide
810 * Private impl for avoiding a log message... DO NOT USE without doing
811 * your own log, or the Android Illuminati will find you some night and
812 * beat you up.
813 */
814 public static final native void sendSignalQuiet(int pid, int signal);
815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 /** @hide */
Marco Nelissen0bca96b2009-07-17 12:59:25 -0700817 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818
819 /** @hide */
820 public static final native void readProcLines(String path,
821 String[] reqFields, long[] outSizes);
822
823 /** @hide */
824 public static final native int[] getPids(String path, int[] lastArray);
825
826 /** @hide */
827 public static final int PROC_TERM_MASK = 0xff;
828 /** @hide */
829 public static final int PROC_ZERO_TERM = 0;
830 /** @hide */
831 public static final int PROC_SPACE_TERM = (int)' ';
832 /** @hide */
Evan Millarc64edde2009-04-18 12:26:32 -0700833 public static final int PROC_TAB_TERM = (int)'\t';
834 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 public static final int PROC_COMBINE = 0x100;
836 /** @hide */
837 public static final int PROC_PARENS = 0x200;
838 /** @hide */
839 public static final int PROC_OUT_STRING = 0x1000;
840 /** @hide */
841 public static final int PROC_OUT_LONG = 0x2000;
842 /** @hide */
843 public static final int PROC_OUT_FLOAT = 0x4000;
844
845 /** @hide */
846 public static final native boolean readProcFile(String file, int[] format,
847 String[] outStrings, long[] outLongs, float[] outFloats);
Evan Millarc64edde2009-04-18 12:26:32 -0700848
849 /** @hide */
850 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
851 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852
853 /**
854 * Gets the total Pss value for a given process, in bytes.
855 *
856 * @param pid the process to the Pss for
857 * @return the total Pss value for the given process in bytes,
858 * or -1 if the value cannot be determined
859 * @hide
860 */
861 public static final native long getPss(int pid);
862}