2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import android.net.LocalSocketAddress;
20 import android.net.LocalSocket;
21 import android.util.Log;
22 import dalvik.system.Zygote;
24 import java.io.BufferedWriter;
25 import java.io.DataInputStream;
26 import java.io.IOException;
27 import java.io.OutputStreamWriter;
28 import java.util.ArrayList;
30 import libcore.io.Libcore;
32 /*package*/ class ZygoteStartFailedEx extends Exception {
34 * Something prevented the zygote process startup from happening normally
37 ZygoteStartFailedEx() {};
38 ZygoteStartFailedEx(String s) {super(s);}
39 ZygoteStartFailedEx(Throwable cause) {super(cause);}
43 * Tools for managing OS processes.
45 public class Process {
46 private static final String LOG_TAG = "Process";
48 private static final String ZYGOTE_SOCKET = "zygote";
51 * Name of a process for running the platform's media services.
54 public static final String ANDROID_SHARED_MEDIA = "com.android.process.media";
57 * Name of the process that Google content providers can share.
60 public static final String GOOGLE_SHARED_APP_CONTENT = "com.google.process.content";
63 * Defines the UID/GID under which system code runs.
65 public static final int SYSTEM_UID = 1000;
68 * Defines the UID/GID under which the telephony code runs.
70 public static final int PHONE_UID = 1001;
73 * Defines the UID/GID for the user shell.
76 public static final int SHELL_UID = 2000;
79 * Defines the UID/GID for the log group.
82 public static final int LOG_UID = 1007;
85 * Defines the UID/GID for the WIFI supplicant process.
88 public static final int WIFI_UID = 1010;
91 * Defines the UID/GID for the mediaserver process.
94 public static final int MEDIA_UID = 1013;
97 * Defines the UID/GID for the DRM process.
100 public static final int DRM_UID = 1019;
103 * Defines the UID/GID for the group that controls VPN services.
106 public static final int VPN_UID = 1016;
109 * Defines the UID/GID for the NFC service process.
112 public static final int NFC_UID = 1027;
115 * Defines the UID/GID for the Bluetooth service process.
118 public static final int BLUETOOTH_UID = 1002;
121 * Defines the GID for the group that allows write access to the internal media storage.
124 public static final int MEDIA_RW_GID = 1023;
127 * Access to installed package details
130 public static final int PACKAGE_INFO_GID = 1032;
133 * Defines the start of a range of UIDs (and GIDs), going from this
134 * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning
137 public static final int FIRST_APPLICATION_UID = 10000;
140 * Last of application-specific UIDs starting at
141 * {@link #FIRST_APPLICATION_UID}.
143 public static final int LAST_APPLICATION_UID = 19999;
146 * First uid used for fully isolated sandboxed processes (with no permissions of their own)
149 public static final int FIRST_ISOLATED_UID = 99000;
152 * Last uid used for fully isolated sandboxed processes (with no permissions of their own)
155 public static final int LAST_ISOLATED_UID = 99999;
158 * First gid for applications to share resources. Used when forward-locking
159 * is enabled but all UserHandles need to be able to read the resources.
162 public static final int FIRST_SHARED_APPLICATION_GID = 50000;
165 * Last gid for applications to share resources. Used when forward-locking
166 * is enabled but all UserHandles need to be able to read the resources.
169 public static final int LAST_SHARED_APPLICATION_GID = 59999;
172 * Standard priority of application threads.
173 * Use with {@link #setThreadPriority(int)} and
174 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
175 * {@link java.lang.Thread} class.
177 public static final int THREAD_PRIORITY_DEFAULT = 0;
180 * ***************************************
181 * ** Keep in sync with utils/threads.h **
182 * ***************************************
186 * Lowest available thread priority. Only for those who really, really
187 * don't want to run if anything else is happening.
188 * Use with {@link #setThreadPriority(int)} and
189 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
190 * {@link java.lang.Thread} class.
192 public static final int THREAD_PRIORITY_LOWEST = 19;
195 * Standard priority background threads. This gives your thread a slightly
196 * lower than normal priority, so that it will have less chance of impacting
197 * the responsiveness of the user interface.
198 * Use with {@link #setThreadPriority(int)} and
199 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
200 * {@link java.lang.Thread} class.
202 public static final int THREAD_PRIORITY_BACKGROUND = 10;
205 * Standard priority of threads that are currently running a user interface
206 * that the user is interacting with. Applications can not normally
207 * change to this priority; the system will automatically adjust your
208 * application threads as the user moves through the UI.
209 * Use with {@link #setThreadPriority(int)} and
210 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
211 * {@link java.lang.Thread} class.
213 public static final int THREAD_PRIORITY_FOREGROUND = -2;
216 * Standard priority of system display threads, involved in updating
217 * the user interface. Applications can not
218 * normally change to this priority.
219 * Use with {@link #setThreadPriority(int)} and
220 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
221 * {@link java.lang.Thread} class.
223 public static final int THREAD_PRIORITY_DISPLAY = -4;
226 * Standard priority of the most important display threads, for compositing
227 * the screen and retrieving input events. Applications can not normally
228 * change to this priority.
229 * Use with {@link #setThreadPriority(int)} and
230 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
231 * {@link java.lang.Thread} class.
233 public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8;
236 * Standard priority of audio threads. Applications can not normally
237 * change to this priority.
238 * Use with {@link #setThreadPriority(int)} and
239 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
240 * {@link java.lang.Thread} class.
242 public static final int THREAD_PRIORITY_AUDIO = -16;
245 * Standard priority of the most important audio threads.
246 * Applications can not normally change to this priority.
247 * Use with {@link #setThreadPriority(int)} and
248 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
249 * {@link java.lang.Thread} class.
251 public static final int THREAD_PRIORITY_URGENT_AUDIO = -19;
254 * Minimum increment to make a priority more favorable.
256 public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
259 * Minimum increment to make a priority less favorable.
261 public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
264 * Default scheduling policy
267 public static final int SCHED_OTHER = 0;
270 * First-In First-Out scheduling policy
273 public static final int SCHED_FIFO = 1;
276 * Round-Robin scheduling policy
279 public static final int SCHED_RR = 2;
282 * Batch scheduling policy
285 public static final int SCHED_BATCH = 3;
288 * Idle scheduling policy
291 public static final int SCHED_IDLE = 5;
293 // Keep in sync with SP_* constants of enum type SchedPolicy
294 // declared in system/core/include/cutils/sched_policy.h,
295 // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
298 * Default thread group -
299 * has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
300 * When used with setProcessGroup(), the group of each thread in the process
301 * is conditionally changed based on that thread's current priority, as follows:
302 * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
303 * are moved to foreground thread group. All other threads are left unchanged.
306 public static final int THREAD_GROUP_DEFAULT = -1;
309 * Background thread group - All threads in
310 * this group are scheduled with a reduced share of the CPU.
311 * Value is same as constant SP_BACKGROUND of enum SchedPolicy.
312 * FIXME rename to THREAD_GROUP_BACKGROUND.
315 public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0;
318 * Foreground thread group - All threads in
319 * this group are scheduled with a normal share of the CPU.
320 * Value is same as constant SP_FOREGROUND of enum SchedPolicy.
321 * Not used at this level.
324 private static final int THREAD_GROUP_FOREGROUND = 1;
327 * System thread group.
330 public static final int THREAD_GROUP_SYSTEM = 2;
333 * Application audio thread group.
336 public static final int THREAD_GROUP_AUDIO_APP = 3;
339 * System audio thread group.
342 public static final int THREAD_GROUP_AUDIO_SYS = 4;
344 public static final int SIGNAL_QUIT = 3;
345 public static final int SIGNAL_KILL = 9;
346 public static final int SIGNAL_USR1 = 10;
348 // State for communicating with zygote process
350 static LocalSocket sZygoteSocket;
351 static DataInputStream sZygoteInputStream;
352 static BufferedWriter sZygoteWriter;
354 /** true if previous zygote open failed */
355 static boolean sPreviousZygoteOpenFailed;
358 * Start a new process.
360 * <p>If processes are enabled, a new process is created and the
361 * static main() function of a <var>processClass</var> is executed there.
362 * The process will continue running after this function returns.
364 * <p>If processes are not enabled, a new thread in the caller's
365 * process is created and main() of <var>processClass</var> called there.
367 * <p>The niceName parameter, if not an empty string, is a custom name to
368 * give to the process instead of using processClass. This allows you to
369 * make easily identifyable processes even if you are using the same base
370 * <var>processClass</var> to start them.
372 * @param processClass The class to use as the process's main entry
374 * @param niceName A more readable name to use for the process.
375 * @param uid The user-id under which the process will run.
376 * @param gid The group-id under which the process will run.
377 * @param gids Additional group-ids associated with the process.
378 * @param debugFlags Additional flags.
379 * @param targetSdkVersion The target SDK version for the app.
380 * @param seInfo null-ok SE Android information for the new process.
381 * @param zygoteArgs Additional arguments to supply to the zygote process.
383 * @return An object that describes the result of the attempt to start the process.
384 * @throws RuntimeException on fatal start failure
388 public static final ProcessStartResult start(final String processClass,
389 final String niceName,
390 int uid, int gid, int[] gids,
391 int debugFlags, int mountExternal,
392 int targetSdkVersion,
394 String[] zygoteArgs) {
396 return startViaZygote(processClass, niceName, uid, gid, gids,
397 debugFlags, mountExternal, targetSdkVersion, seInfo, zygoteArgs);
398 } catch (ZygoteStartFailedEx ex) {
400 "Starting VM process through Zygote failed");
401 throw new RuntimeException(
402 "Starting VM process through Zygote failed", ex);
406 /** retry interval for opening a zygote socket */
407 static final int ZYGOTE_RETRY_MILLIS = 500;
410 * Tries to open socket to Zygote process if not already open. If
411 * already open, does nothing. May block and retry.
413 private static void openZygoteSocketIfNeeded()
414 throws ZygoteStartFailedEx {
418 if (sPreviousZygoteOpenFailed) {
420 * If we've failed before, expect that we'll fail again and
421 * don't pause for retries.
429 * See bug #811181: Sometimes runtime can make it up before zygote.
430 * Really, we'd like to do something better to avoid this condition,
431 * but for now just wait a bit...
434 ; (sZygoteSocket == null) && (retry < (retryCount + 1))
439 Log.i("Zygote", "Zygote not up yet, sleeping...");
440 Thread.sleep(ZYGOTE_RETRY_MILLIS);
441 } catch (InterruptedException ex) {
442 // should never happen
447 sZygoteSocket = new LocalSocket();
449 sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET,
450 LocalSocketAddress.Namespace.RESERVED));
453 = new DataInputStream(sZygoteSocket.getInputStream());
457 new OutputStreamWriter(
458 sZygoteSocket.getOutputStream()),
461 Log.i("Zygote", "Process: zygote socket opened");
463 sPreviousZygoteOpenFailed = false;
465 } catch (IOException ex) {
466 if (sZygoteSocket != null) {
468 sZygoteSocket.close();
469 } catch (IOException ex2) {
470 Log.e(LOG_TAG,"I/O exception on close after exception",
475 sZygoteSocket = null;
479 if (sZygoteSocket == null) {
480 sPreviousZygoteOpenFailed = true;
481 throw new ZygoteStartFailedEx("connect failed");
486 * Sends an argument list to the zygote process, which starts a new child
487 * and returns the child's pid. Please note: the present implementation
488 * replaces newlines in the argument list with spaces.
489 * @param args argument list
490 * @return An object that describes the result of the attempt to start the process.
491 * @throws ZygoteStartFailedEx if process start failed for any reason
493 private static ProcessStartResult zygoteSendArgsAndGetResult(ArrayList<String> args)
494 throws ZygoteStartFailedEx {
495 openZygoteSocketIfNeeded();
498 // Throw early if any of the arguments are malformed. This means we can
499 // avoid writing a partial response to the zygote.
500 int sz = args.size();
501 for (int i = 0; i < sz; i++) {
502 if (args.get(i).indexOf('\n') >= 0) {
503 throw new ZygoteStartFailedEx("embedded newlines not allowed");
508 * See com.android.internal.os.ZygoteInit.readArgumentList()
509 * Presently the wire format to the zygote process is:
510 * a) a count of arguments (argc, in essence)
511 * b) a number of newline-separated argument strings equal to count
513 * After the zygote process reads these it will write the pid of
514 * the child or -1 on failure, followed by boolean to
515 * indicate whether a wrapper process was used.
518 sZygoteWriter.write(Integer.toString(args.size()));
519 sZygoteWriter.newLine();
521 for (int i = 0; i < sz; i++) {
522 String arg = args.get(i);
523 sZygoteWriter.write(arg);
524 sZygoteWriter.newLine();
527 sZygoteWriter.flush();
529 // Should there be a timeout on this?
530 ProcessStartResult result = new ProcessStartResult();
531 // Always read the entire result from the input stream to avoid leaving
532 // bytes in the stream for future process starts to accidentally stumble
534 result.pid = sZygoteInputStream.readInt();
535 result.usingWrapper = sZygoteInputStream.readBoolean();
537 if (result.pid < 0) {
538 throw new ZygoteStartFailedEx("fork() failed");
541 } catch (IOException ex) {
543 if (sZygoteSocket != null) {
544 sZygoteSocket.close();
546 } catch (IOException ex2) {
547 // we're going to fail anyway
548 Log.e(LOG_TAG,"I/O exception on routine close", ex2);
551 sZygoteSocket = null;
553 throw new ZygoteStartFailedEx(ex);
558 * Starts a new process via the zygote mechanism.
560 * @param processClass Class name whose static main() to run
561 * @param niceName 'nice' process name to appear in ps
562 * @param uid a POSIX uid that the new process should setuid() to
563 * @param gid a POSIX gid that the new process shuold setgid() to
564 * @param gids null-ok; a list of supplementary group IDs that the
565 * new process should setgroup() to.
566 * @param debugFlags Additional flags.
567 * @param targetSdkVersion The target SDK version for the app.
568 * @param seInfo null-ok SE Android information for the new process.
569 * @param extraArgs Additional arguments to supply to the zygote process.
570 * @return An object that describes the result of the attempt to start the process.
571 * @throws ZygoteStartFailedEx if process start failed for any reason
573 private static ProcessStartResult startViaZygote(final String processClass,
574 final String niceName,
575 final int uid, final int gid,
577 int debugFlags, int mountExternal,
578 int targetSdkVersion,
581 throws ZygoteStartFailedEx {
582 synchronized(Process.class) {
583 ArrayList<String> argsForZygote = new ArrayList<String>();
585 // --runtime-init, --setuid=, --setgid=,
586 // and --setgroups= must go first
587 argsForZygote.add("--runtime-init");
588 argsForZygote.add("--setuid=" + uid);
589 argsForZygote.add("--setgid=" + gid);
590 if ((debugFlags & Zygote.DEBUG_ENABLE_JNI_LOGGING) != 0) {
591 argsForZygote.add("--enable-jni-logging");
593 if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) {
594 argsForZygote.add("--enable-safemode");
596 if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) {
597 argsForZygote.add("--enable-debugger");
599 if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) {
600 argsForZygote.add("--enable-checkjni");
602 if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) {
603 argsForZygote.add("--enable-assert");
605 if (mountExternal == Zygote.MOUNT_EXTERNAL_MULTIUSER) {
606 argsForZygote.add("--mount-external-multiuser");
607 } else if (mountExternal == Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL) {
608 argsForZygote.add("--mount-external-multiuser-all");
610 argsForZygote.add("--target-sdk-version=" + targetSdkVersion);
612 //TODO optionally enable debuger
613 //argsForZygote.add("--enable-debugger");
615 // --setgroups is a comma-separated list
616 if (gids != null && gids.length > 0) {
617 StringBuilder sb = new StringBuilder();
618 sb.append("--setgroups=");
620 int sz = gids.length;
621 for (int i = 0; i < sz; i++) {
628 argsForZygote.add(sb.toString());
631 if (niceName != null) {
632 argsForZygote.add("--nice-name=" + niceName);
635 if (seInfo != null) {
636 argsForZygote.add("--seinfo=" + seInfo);
639 argsForZygote.add(processClass);
641 if (extraArgs != null) {
642 for (String arg : extraArgs) {
643 argsForZygote.add(arg);
647 return zygoteSendArgsAndGetResult(argsForZygote);
652 * Returns elapsed milliseconds of the time this process has run.
653 * @return Returns the number of milliseconds this process has return.
655 public static final native long getElapsedCpuTime();
658 * Returns the identifier of this process, which can be used with
659 * {@link #killProcess} and {@link #sendSignal}.
661 public static final int myPid() {
662 return Libcore.os.getpid();
666 * Returns the identifier of this process' parent.
669 public static final int myPpid() {
670 return Libcore.os.getppid();
674 * Returns the identifier of the calling thread, which be used with
675 * {@link #setThreadPriority(int, int)}.
677 public static final int myTid() {
678 return Libcore.os.gettid();
682 * Returns the identifier of this process's uid. This is the kernel uid
683 * that the process is running under, which is the identity of its
684 * app-specific sandbox. It is different from {@link #myUserHandle} in that
685 * a uid identifies a specific app sandbox in a specific user.
687 public static final int myUid() {
688 return Libcore.os.getuid();
692 * Returns this process's user handle. This is the
693 * user the process is running under. It is distinct from
694 * {@link #myUid()} in that a particular user will have multiple
695 * distinct apps running under it each with their own uid.
697 public static final UserHandle myUserHandle() {
698 return new UserHandle(UserHandle.getUserId(myUid()));
702 * Returns whether the current process is in an isolated sandbox.
705 public static final boolean isIsolated() {
706 int uid = UserHandle.getAppId(myUid());
707 return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID;
711 * Returns the UID assigned to a particular user name, or -1 if there is
712 * none. If the given string consists of only numbers, it is converted
715 public static final native int getUidForName(String name);
718 * Returns the GID assigned to a particular user name, or -1 if there is
719 * none. If the given string consists of only numbers, it is converted
722 public static final native int getGidForName(String name);
725 * Returns a uid for a currently running process.
726 * @param pid the process id
727 * @return the uid of the process, or -1 if the process is not running.
728 * @hide pending API council review
730 public static final int getUidForPid(int pid) {
731 String[] procStatusLabels = { "Uid:" };
732 long[] procStatusValues = new long[1];
733 procStatusValues[0] = -1;
734 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
735 return (int) procStatusValues[0];
739 * Returns the parent process id for a currently running process.
740 * @param pid the process id
741 * @return the parent process id of the process, or -1 if the process is not running.
744 public static final int getParentPid(int pid) {
745 String[] procStatusLabels = { "PPid:" };
746 long[] procStatusValues = new long[1];
747 procStatusValues[0] = -1;
748 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
749 return (int) procStatusValues[0];
753 * Returns the thread group leader id for a currently running thread.
754 * @param tid the thread id
755 * @return the thread group leader id of the thread, or -1 if the thread is not running.
756 * This is same as what getpid(2) would return if called by tid.
759 public static final int getThreadGroupLeader(int tid) {
760 String[] procStatusLabels = { "Tgid:" };
761 long[] procStatusValues = new long[1];
762 procStatusValues[0] = -1;
763 Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues);
764 return (int) procStatusValues[0];
768 * Set the priority of a thread, based on Linux priorities.
770 * @param tid The identifier of the thread/process to change.
771 * @param priority A Linux priority level, from -20 for highest scheduling
772 * priority to 19 for lowest scheduling priority.
774 * @throws IllegalArgumentException Throws IllegalArgumentException if
775 * <var>tid</var> does not exist.
776 * @throws SecurityException Throws SecurityException if your process does
777 * not have permission to modify the given thread, or to use the given
780 public static final native void setThreadPriority(int tid, int priority)
781 throws IllegalArgumentException, SecurityException;
784 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
785 * throw an exception if passed a background-level thread priority. This is only
786 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
790 public static final native void setCanSelfBackground(boolean backgroundOk);
793 * Sets the scheduling group for a thread.
795 * @param tid The identifier of the thread to change.
796 * @param group The target group for this thread from THREAD_GROUP_*.
798 * @throws IllegalArgumentException Throws IllegalArgumentException if
799 * <var>tid</var> does not exist.
800 * @throws SecurityException Throws SecurityException if your process does
801 * not have permission to modify the given thread, or to use the given
803 * If the thread is a thread group leader, that is it's gettid() == getpid(),
804 * then the other threads in the same thread group are _not_ affected.
806 public static final native void setThreadGroup(int tid, int group)
807 throws IllegalArgumentException, SecurityException;
810 * Sets the scheduling group for a process and all child threads
812 * @param pid The identifier of the process to change.
813 * @param group The target group for this process from THREAD_GROUP_*.
815 * @throws IllegalArgumentException Throws IllegalArgumentException if
816 * <var>tid</var> does not exist.
817 * @throws SecurityException Throws SecurityException if your process does
818 * not have permission to modify the given thread, or to use the given
821 * group == THREAD_GROUP_DEFAULT means to move all non-background priority
822 * threads to the foreground scheduling group, but to leave background
823 * priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all
824 * threads, regardless of priority, to the background scheduling group.
825 * group == THREAD_GROUP_FOREGROUND is not allowed.
827 public static final native void setProcessGroup(int pid, int group)
828 throws IllegalArgumentException, SecurityException;
831 * Return the scheduling group of requested process.
835 public static final native int getProcessGroup(int pid)
836 throws IllegalArgumentException, SecurityException;
839 * Set the priority of the calling thread, based on Linux priorities. See
840 * {@link #setThreadPriority(int, int)} for more information.
842 * @param priority A Linux priority level, from -20 for highest scheduling
843 * priority to 19 for lowest scheduling priority.
845 * @throws IllegalArgumentException Throws IllegalArgumentException if
846 * <var>tid</var> does not exist.
847 * @throws SecurityException Throws SecurityException if your process does
848 * not have permission to modify the given thread, or to use the given
851 * @see #setThreadPriority(int, int)
853 public static final native void setThreadPriority(int priority)
854 throws IllegalArgumentException, SecurityException;
857 * Return the current priority of a thread, based on Linux priorities.
859 * @param tid The identifier of the thread/process to change.
861 * @return Returns the current priority, as a Linux priority level,
862 * from -20 for highest scheduling priority to 19 for lowest scheduling
865 * @throws IllegalArgumentException Throws IllegalArgumentException if
866 * <var>tid</var> does not exist.
868 public static final native int getThreadPriority(int tid)
869 throws IllegalArgumentException;
872 * Set the scheduling policy and priority of a thread, based on Linux.
874 * @param tid The identifier of the thread/process to change.
875 * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
876 * @param priority A Linux priority level in a range appropriate for the given policy.
878 * @throws IllegalArgumentException Throws IllegalArgumentException if
879 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
880 * @throws SecurityException Throws SecurityException if your process does
881 * not have permission to modify the given thread, or to use the given
882 * scheduling policy or priority.
886 public static final native void setThreadScheduler(int tid, int policy, int priority)
887 throws IllegalArgumentException;
890 * Determine whether the current environment supports multiple processes.
892 * @return Returns true if the system can run in multiple processes, else
893 * false if everything is running in a single process.
895 * @deprecated This method always returns true. Do not use.
898 public static final boolean supportsProcesses() {
903 * Set the out-of-memory badness adjustment for a process.
905 * @param pid The process identifier to set.
906 * @param amt Adjustment value -- linux allows -16 to +15.
908 * @return Returns true if the underlying system supports this
909 * feature, else false.
913 public static final native boolean setOomAdj(int pid, int amt);
916 * Adjust the swappiness level for a process.
918 * @param pid The process identifier to set.
919 * @param is_increased Whether swappiness should be increased or default.
921 * @return Returns true if the underlying system supports this
922 * feature, else false.
926 public static final native boolean setSwappiness(int pid, boolean is_increased);
929 * Change this process's argv[0] parameter. This can be useful to show
930 * more descriptive information in things like the 'ps' command.
932 * @param text The new name of this process.
936 public static final native void setArgV0(String text);
939 * Kill the process with the given PID.
940 * Note that, though this API allows us to request to
941 * kill any process based on its PID, the kernel will
942 * still impose standard restrictions on which PIDs you
943 * are actually able to kill. Typically this means only
944 * the process running the caller's packages/application
945 * and any additional processes created by that app; packages
946 * sharing a common UID will also be able to kill each
949 public static final void killProcess(int pid) {
950 sendSignal(pid, SIGNAL_KILL);
954 public static final native int setUid(int uid);
957 public static final native int setGid(int uid);
960 * Send a signal to the given process.
962 * @param pid The pid of the target process.
963 * @param signal The signal to send.
965 public static final native void sendSignal(int pid, int signal);
969 * Private impl for avoiding a log message... DO NOT USE without doing
970 * your own log, or the Android Illuminati will find you some night and
973 public static final void killProcessQuiet(int pid) {
974 sendSignalQuiet(pid, SIGNAL_KILL);
979 * Private impl for avoiding a log message... DO NOT USE without doing
980 * your own log, or the Android Illuminati will find you some night and
983 public static final native void sendSignalQuiet(int pid, int signal);
986 public static final native long getFreeMemory();
989 public static final native long getTotalMemory();
992 public static final native void readProcLines(String path,
993 String[] reqFields, long[] outSizes);
996 public static final native int[] getPids(String path, int[] lastArray);
999 public static final int PROC_TERM_MASK = 0xff;
1001 public static final int PROC_ZERO_TERM = 0;
1003 public static final int PROC_SPACE_TERM = (int)' ';
1005 public static final int PROC_TAB_TERM = (int)'\t';
1007 public static final int PROC_COMBINE = 0x100;
1009 public static final int PROC_PARENS = 0x200;
1011 public static final int PROC_QUOTES = 0x400;
1013 public static final int PROC_OUT_STRING = 0x1000;
1015 public static final int PROC_OUT_LONG = 0x2000;
1017 public static final int PROC_OUT_FLOAT = 0x4000;
1020 public static final native boolean readProcFile(String file, int[] format,
1021 String[] outStrings, long[] outLongs, float[] outFloats);
1024 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
1025 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
1028 public static final native int[] getPidsForCommands(String[] cmds);
1031 * Gets the total Pss value for a given process, in bytes.
1033 * @param pid the process to the Pss for
1034 * @return the total Pss value for the given process in bytes,
1035 * or -1 if the value cannot be determined
1038 public static final native long getPss(int pid);
1041 * Specifies the outcome of having started a process.
1044 public static final class ProcessStartResult {
1046 * The PID of the newly started process.
1047 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
1052 * True if the process was started with a wrapper attached.
1054 public boolean usingWrapper;