Process.java revision a0c283eac33dd2da72235751bbfa4f2d9898d5ea
1/*
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    /**
71     * Defines the UID/GID for the user shell.
72     * @hide
73     */
74    public static final int SHELL_UID = 2000;
75
76    /**
77     * Defines the UID/GID for the log group.
78     * @hide
79     */
80    public static final int LOG_UID = 1007;
81
82    /**
83     * Defines the UID/GID for the WIFI supplicant process.
84     * @hide
85     */
86    public static final int WIFI_UID = 1010;
87
88    /**
89     * Defines the UID/GID for the mediaserver process.
90     * @hide
91     */
92    public static final int MEDIA_UID = 1013;
93
94    /**
95     * Defines the GID for the group that allows write access to the SD card.
96     * @hide
97     */
98    public static final int SDCARD_RW_GID = 1015;
99
100    /**
101     * Defines the UID/GID for the NFC service process.
102     * @hide
103     */
104    public static final int NFC_UID = 1027;
105
106    /**
107     * Defines the GID for the group that allows write access to the internal media storage.
108     * @hide
109     */
110    public static final int MEDIA_RW_GID = 1023;
111
112    /**
113     * Defines the start of a range of UIDs (and GIDs), going from this
114     * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning
115     * to applications.
116     */
117    public static final int FIRST_APPLICATION_UID = 10000;
118    /**
119     * Last of application-specific UIDs starting at
120     * {@link #FIRST_APPLICATION_UID}.
121     */
122    public static final int LAST_APPLICATION_UID = 89999;
123
124    /**
125     * First uid used for fully isolated sandboxed processes (with no permissions of their own)
126     * @hide
127     */
128    public static final int FIRST_ISOLATED_UID = 99000;
129
130    /**
131     * Last uid used for fully isolated sandboxed processes (with no permissions of their own)
132     * @hide
133     */
134    public static final int LAST_ISOLATED_UID = 99999;
135
136    /**
137     * Defines a secondary group id for access to the bluetooth hardware.
138     */
139    public static final int BLUETOOTH_GID = 2000;
140
141    /**
142     * Standard priority of application threads.
143     * Use with {@link #setThreadPriority(int)} and
144     * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
145     * {@link java.lang.Thread} class.
146     */
147    public static final int THREAD_PRIORITY_DEFAULT = 0;
148
149    /*
150     * ***************************************
151     * ** Keep in sync with utils/threads.h **
152     * ***************************************
153     */
154
155    /**
156     * Lowest available thread priority.  Only for those who really, really
157     * don't want to run if anything else is happening.
158     * Use with {@link #setThreadPriority(int)} and
159     * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
160     * {@link java.lang.Thread} class.
161     */
162    public static final int THREAD_PRIORITY_LOWEST = 19;
163
164    /**
165     * Standard priority background threads.  This gives your thread a slightly
166     * lower than normal priority, so that it will have less chance of impacting
167     * the responsiveness of the user interface.
168     * Use with {@link #setThreadPriority(int)} and
169     * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
170     * {@link java.lang.Thread} class.
171     */
172    public static final int THREAD_PRIORITY_BACKGROUND = 10;
173
174    /**
175     * Standard priority of threads that are currently running a user interface
176     * that the user is interacting with.  Applications can not normally
177     * change to this priority; the system will automatically adjust your
178     * application threads as the user moves through the UI.
179     * Use with {@link #setThreadPriority(int)} and
180     * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
181     * {@link java.lang.Thread} class.
182     */
183    public static final int THREAD_PRIORITY_FOREGROUND = -2;
184
185    /**
186     * Standard priority of system display threads, involved in updating
187     * the user interface.  Applications can not
188     * normally change to this priority.
189     * Use with {@link #setThreadPriority(int)} and
190     * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
191     * {@link java.lang.Thread} class.
192     */
193    public static final int THREAD_PRIORITY_DISPLAY = -4;
194
195    /**
196     * Standard priority of the most important display threads, for compositing
197     * the screen and retrieving input events.  Applications can not normally
198     * 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_DISPLAY = -8;
204
205    /**
206     * Standard priority of audio threads.  Applications can not normally
207     * change to this priority.
208     * Use with {@link #setThreadPriority(int)} and
209     * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
210     * {@link java.lang.Thread} class.
211     */
212    public static final int THREAD_PRIORITY_AUDIO = -16;
213
214    /**
215     * Standard priority of the most important audio threads.
216     * Applications can not normally change to this priority.
217     * Use with {@link #setThreadPriority(int)} and
218     * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
219     * {@link java.lang.Thread} class.
220     */
221    public static final int THREAD_PRIORITY_URGENT_AUDIO = -19;
222
223    /**
224     * Minimum increment to make a priority more favorable.
225     */
226    public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
227
228    /**
229     * Minimum increment to make a priority less favorable.
230     */
231    public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
232
233    /**
234     * Default scheduling policy
235     * @hide
236     */
237    public static final int SCHED_OTHER = 0;
238
239    /**
240     * First-In First-Out scheduling policy
241     * @hide
242     */
243    public static final int SCHED_FIFO = 1;
244
245    /**
246     * Round-Robin scheduling policy
247     * @hide
248     */
249    public static final int SCHED_RR = 2;
250
251    /**
252     * Batch scheduling policy
253     * @hide
254     */
255    public static final int SCHED_BATCH = 3;
256
257    /**
258     * Idle scheduling policy
259     * @hide
260     */
261    public static final int SCHED_IDLE = 5;
262
263    /**
264     * Default thread group - gets a 'normal' share of the CPU
265     * @hide
266     */
267    public static final int THREAD_GROUP_DEFAULT = 0;
268
269    /**
270     * Background non-interactive thread group - All threads in
271     * this group are scheduled with a reduced share of the CPU.
272     * @hide
273     */
274    public static final int THREAD_GROUP_BG_NONINTERACTIVE = 1;
275
276    /**
277     * Foreground 'boost' thread group - All threads in
278     * this group are scheduled with an increased share of the CPU
279     * @hide
280     **/
281    public static final int THREAD_GROUP_FG_BOOST = 2;
282
283    public static final int SIGNAL_QUIT = 3;
284    public static final int SIGNAL_KILL = 9;
285    public static final int SIGNAL_USR1 = 10;
286
287    // State for communicating with zygote process
288
289    static LocalSocket sZygoteSocket;
290    static DataInputStream sZygoteInputStream;
291    static BufferedWriter sZygoteWriter;
292
293    /** true if previous zygote open failed */
294    static boolean sPreviousZygoteOpenFailed;
295
296    /**
297     * Start a new process.
298     *
299     * <p>If processes are enabled, a new process is created and the
300     * static main() function of a <var>processClass</var> is executed there.
301     * The process will continue running after this function returns.
302     *
303     * <p>If processes are not enabled, a new thread in the caller's
304     * process is created and main() of <var>processClass</var> called there.
305     *
306     * <p>The niceName parameter, if not an empty string, is a custom name to
307     * give to the process instead of using processClass.  This allows you to
308     * make easily identifyable processes even if you are using the same base
309     * <var>processClass</var> to start them.
310     *
311     * @param processClass The class to use as the process's main entry
312     *                     point.
313     * @param niceName A more readable name to use for the process.
314     * @param uid The user-id under which the process will run.
315     * @param gid The group-id under which the process will run.
316     * @param gids Additional group-ids associated with the process.
317     * @param debugFlags Additional flags.
318     * @param targetSdkVersion The target SDK version for the app.
319     * @param zygoteArgs Additional arguments to supply to the zygote process.
320     *
321     * @return An object that describes the result of the attempt to start the process.
322     * @throws RuntimeException on fatal start failure
323     *
324     * {@hide}
325     */
326    public static final ProcessStartResult start(final String processClass,
327                                  final String niceName,
328                                  int uid, int gid, int[] gids,
329                                  int debugFlags, int targetSdkVersion,
330                                  String[] zygoteArgs) {
331        try {
332            return startViaZygote(processClass, niceName, uid, gid, gids,
333                    debugFlags, targetSdkVersion, zygoteArgs);
334        } catch (ZygoteStartFailedEx ex) {
335            Log.e(LOG_TAG,
336                    "Starting VM process through Zygote failed");
337            throw new RuntimeException(
338                    "Starting VM process through Zygote failed", ex);
339        }
340    }
341
342    /** retry interval for opening a zygote socket */
343    static final int ZYGOTE_RETRY_MILLIS = 500;
344
345    /**
346     * Tries to open socket to Zygote process if not already open. If
347     * already open, does nothing.  May block and retry.
348     */
349    private static void openZygoteSocketIfNeeded()
350            throws ZygoteStartFailedEx {
351
352        int retryCount;
353
354        if (sPreviousZygoteOpenFailed) {
355            /*
356             * If we've failed before, expect that we'll fail again and
357             * don't pause for retries.
358             */
359            retryCount = 0;
360        } else {
361            retryCount = 10;
362        }
363
364        /*
365         * See bug #811181: Sometimes runtime can make it up before zygote.
366         * Really, we'd like to do something better to avoid this condition,
367         * but for now just wait a bit...
368         */
369        for (int retry = 0
370                ; (sZygoteSocket == null) && (retry < (retryCount + 1))
371                ; retry++ ) {
372
373            if (retry > 0) {
374                try {
375                    Log.i("Zygote", "Zygote not up yet, sleeping...");
376                    Thread.sleep(ZYGOTE_RETRY_MILLIS);
377                } catch (InterruptedException ex) {
378                    // should never happen
379                }
380            }
381
382            try {
383                sZygoteSocket = new LocalSocket();
384
385                sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET,
386                        LocalSocketAddress.Namespace.RESERVED));
387
388                sZygoteInputStream
389                        = new DataInputStream(sZygoteSocket.getInputStream());
390
391                sZygoteWriter =
392                    new BufferedWriter(
393                            new OutputStreamWriter(
394                                    sZygoteSocket.getOutputStream()),
395                            256);
396
397                Log.i("Zygote", "Process: zygote socket opened");
398
399                sPreviousZygoteOpenFailed = false;
400                break;
401            } catch (IOException ex) {
402                if (sZygoteSocket != null) {
403                    try {
404                        sZygoteSocket.close();
405                    } catch (IOException ex2) {
406                        Log.e(LOG_TAG,"I/O exception on close after exception",
407                                ex2);
408                    }
409                }
410
411                sZygoteSocket = null;
412            }
413        }
414
415        if (sZygoteSocket == null) {
416            sPreviousZygoteOpenFailed = true;
417            throw new ZygoteStartFailedEx("connect failed");
418        }
419    }
420
421    /**
422     * Sends an argument list to the zygote process, which starts a new child
423     * and returns the child's pid. Please note: the present implementation
424     * replaces newlines in the argument list with spaces.
425     * @param args argument list
426     * @return An object that describes the result of the attempt to start the process.
427     * @throws ZygoteStartFailedEx if process start failed for any reason
428     */
429    private static ProcessStartResult zygoteSendArgsAndGetResult(ArrayList<String> args)
430            throws ZygoteStartFailedEx {
431        openZygoteSocketIfNeeded();
432
433        try {
434            /**
435             * See com.android.internal.os.ZygoteInit.readArgumentList()
436             * Presently the wire format to the zygote process is:
437             * a) a count of arguments (argc, in essence)
438             * b) a number of newline-separated argument strings equal to count
439             *
440             * After the zygote process reads these it will write the pid of
441             * the child or -1 on failure, followed by boolean to
442             * indicate whether a wrapper process was used.
443             */
444
445            sZygoteWriter.write(Integer.toString(args.size()));
446            sZygoteWriter.newLine();
447
448            int sz = args.size();
449            for (int i = 0; i < sz; i++) {
450                String arg = args.get(i);
451                if (arg.indexOf('\n') >= 0) {
452                    throw new ZygoteStartFailedEx(
453                            "embedded newlines not allowed");
454                }
455                sZygoteWriter.write(arg);
456                sZygoteWriter.newLine();
457            }
458
459            sZygoteWriter.flush();
460
461            // Should there be a timeout on this?
462            ProcessStartResult result = new ProcessStartResult();
463            result.pid = sZygoteInputStream.readInt();
464            if (result.pid < 0) {
465                throw new ZygoteStartFailedEx("fork() failed");
466            }
467            result.usingWrapper = sZygoteInputStream.readBoolean();
468            return result;
469        } catch (IOException ex) {
470            try {
471                if (sZygoteSocket != null) {
472                    sZygoteSocket.close();
473                }
474            } catch (IOException ex2) {
475                // we're going to fail anyway
476                Log.e(LOG_TAG,"I/O exception on routine close", ex2);
477            }
478
479            sZygoteSocket = null;
480
481            throw new ZygoteStartFailedEx(ex);
482        }
483    }
484
485    /**
486     * Starts a new process via the zygote mechanism.
487     *
488     * @param processClass Class name whose static main() to run
489     * @param niceName 'nice' process name to appear in ps
490     * @param uid a POSIX uid that the new process should setuid() to
491     * @param gid a POSIX gid that the new process shuold setgid() to
492     * @param gids null-ok; a list of supplementary group IDs that the
493     * new process should setgroup() to.
494     * @param debugFlags Additional flags.
495     * @param targetSdkVersion The target SDK version for the app.
496     * @param extraArgs Additional arguments to supply to the zygote process.
497     * @return An object that describes the result of the attempt to start the process.
498     * @throws ZygoteStartFailedEx if process start failed for any reason
499     */
500    private static ProcessStartResult startViaZygote(final String processClass,
501                                  final String niceName,
502                                  final int uid, final int gid,
503                                  final int[] gids,
504                                  int debugFlags, int targetSdkVersion,
505                                  String[] extraArgs)
506                                  throws ZygoteStartFailedEx {
507        synchronized(Process.class) {
508            ArrayList<String> argsForZygote = new ArrayList<String>();
509
510            // --runtime-init, --setuid=, --setgid=,
511            // and --setgroups= must go first
512            argsForZygote.add("--runtime-init");
513            argsForZygote.add("--setuid=" + uid);
514            argsForZygote.add("--setgid=" + gid);
515            if ((debugFlags & Zygote.DEBUG_ENABLE_JNI_LOGGING) != 0) {
516                argsForZygote.add("--enable-jni-logging");
517            }
518            if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) {
519                argsForZygote.add("--enable-safemode");
520            }
521            if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) {
522                argsForZygote.add("--enable-debugger");
523            }
524            if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) {
525                argsForZygote.add("--enable-checkjni");
526            }
527            if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) {
528                argsForZygote.add("--enable-assert");
529            }
530            argsForZygote.add("--target-sdk-version=" + targetSdkVersion);
531
532            //TODO optionally enable debuger
533            //argsForZygote.add("--enable-debugger");
534
535            // --setgroups is a comma-separated list
536            if (gids != null && gids.length > 0) {
537                StringBuilder sb = new StringBuilder();
538                sb.append("--setgroups=");
539
540                int sz = gids.length;
541                for (int i = 0; i < sz; i++) {
542                    if (i != 0) {
543                        sb.append(',');
544                    }
545                    sb.append(gids[i]);
546                }
547
548                argsForZygote.add(sb.toString());
549            }
550
551            if (niceName != null) {
552                argsForZygote.add("--nice-name=" + niceName);
553            }
554
555            argsForZygote.add(processClass);
556
557            if (extraArgs != null) {
558                for (String arg : extraArgs) {
559                    argsForZygote.add(arg);
560                }
561            }
562
563            return zygoteSendArgsAndGetResult(argsForZygote);
564        }
565    }
566
567    /**
568     * Returns elapsed milliseconds of the time this process has run.
569     * @return  Returns the number of milliseconds this process has return.
570     */
571    public static final native long getElapsedCpuTime();
572
573    /**
574     * Returns the identifier of this process, which can be used with
575     * {@link #killProcess} and {@link #sendSignal}.
576     */
577    public static final native int myPid();
578
579    /**
580     * Returns the identifier of the calling thread, which be used with
581     * {@link #setThreadPriority(int, int)}.
582     */
583    public static final native int myTid();
584
585    /**
586     * Returns the identifier of this process's user.
587     */
588    public static final native int myUid();
589
590    /**
591     * Returns whether the current process is in an isolated sandbox.
592     * @hide
593     */
594    public static final boolean isIsolated() {
595        int uid = UserId.getAppId(myUid());
596        return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID;
597    }
598
599    /**
600     * Returns the UID assigned to a particular user name, or -1 if there is
601     * none.  If the given string consists of only numbers, it is converted
602     * directly to a uid.
603     */
604    public static final native int getUidForName(String name);
605
606    /**
607     * Returns the GID assigned to a particular user name, or -1 if there is
608     * none.  If the given string consists of only numbers, it is converted
609     * directly to a gid.
610     */
611    public static final native int getGidForName(String name);
612
613    /**
614     * Returns a uid for a currently running process.
615     * @param pid the process id
616     * @return the uid of the process, or -1 if the process is not running.
617     * @hide pending API council review
618     */
619    public static final int getUidForPid(int pid) {
620        String[] procStatusLabels = { "Uid:" };
621        long[] procStatusValues = new long[1];
622        procStatusValues[0] = -1;
623        Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
624        return (int) procStatusValues[0];
625    }
626
627    /**
628     * Returns the parent process id for a currently running process.
629     * @param pid the process id
630     * @return the parent process id of the process, or -1 if the process is not running.
631     * @hide
632     */
633    public static final int getParentPid(int pid) {
634        String[] procStatusLabels = { "PPid:" };
635        long[] procStatusValues = new long[1];
636        procStatusValues[0] = -1;
637        Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
638        return (int) procStatusValues[0];
639    }
640
641    /**
642     * Set the priority of a thread, based on Linux priorities.
643     *
644     * @param tid The identifier of the thread/process to change.
645     * @param priority A Linux priority level, from -20 for highest scheduling
646     * priority to 19 for lowest scheduling priority.
647     *
648     * @throws IllegalArgumentException Throws IllegalArgumentException if
649     * <var>tid</var> does not exist.
650     * @throws SecurityException Throws SecurityException if your process does
651     * not have permission to modify the given thread, or to use the given
652     * priority.
653     */
654    public static final native void setThreadPriority(int tid, int priority)
655            throws IllegalArgumentException, SecurityException;
656
657    /**
658     * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
659     * throw an exception if passed a background-level thread priority.  This is only
660     * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
661     *
662     * @hide
663     */
664    public static final native void setCanSelfBackground(boolean backgroundOk);
665
666    /**
667     * Sets the scheduling group for a thread.
668     * @hide
669     * @param tid The indentifier of the thread/process to change.
670     * @param group The target group for this thread/process.
671     *
672     * @throws IllegalArgumentException Throws IllegalArgumentException if
673     * <var>tid</var> does not exist.
674     * @throws SecurityException Throws SecurityException if your process does
675     * not have permission to modify the given thread, or to use the given
676     * priority.
677     */
678    public static final native void setThreadGroup(int tid, int group)
679            throws IllegalArgumentException, SecurityException;
680    /**
681     * Sets the scheduling group for a process and all child threads
682     * @hide
683     * @param pid The indentifier of the process to change.
684     * @param group The target group for this process.
685     *
686     * @throws IllegalArgumentException Throws IllegalArgumentException if
687     * <var>tid</var> does not exist.
688     * @throws SecurityException Throws SecurityException if your process does
689     * not have permission to modify the given thread, or to use the given
690     * priority.
691     */
692    public static final native void setProcessGroup(int pid, int group)
693            throws IllegalArgumentException, SecurityException;
694
695    /**
696     * Set the priority of the calling thread, based on Linux priorities.  See
697     * {@link #setThreadPriority(int, int)} for more information.
698     *
699     * @param priority A Linux priority level, from -20 for highest scheduling
700     * priority to 19 for lowest scheduling priority.
701     *
702     * @throws IllegalArgumentException Throws IllegalArgumentException if
703     * <var>tid</var> does not exist.
704     * @throws SecurityException Throws SecurityException if your process does
705     * not have permission to modify the given thread, or to use the given
706     * priority.
707     *
708     * @see #setThreadPriority(int, int)
709     */
710    public static final native void setThreadPriority(int priority)
711            throws IllegalArgumentException, SecurityException;
712
713    /**
714     * Return the current priority of a thread, based on Linux priorities.
715     *
716     * @param tid The identifier of the thread/process to change.
717     *
718     * @return Returns the current priority, as a Linux priority level,
719     * from -20 for highest scheduling priority to 19 for lowest scheduling
720     * priority.
721     *
722     * @throws IllegalArgumentException Throws IllegalArgumentException if
723     * <var>tid</var> does not exist.
724     */
725    public static final native int getThreadPriority(int tid)
726            throws IllegalArgumentException;
727
728    /**
729     * Set the scheduling policy and priority of a thread, based on Linux.
730     *
731     * @param tid The identifier of the thread/process to change.
732     * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
733     * @param priority A Linux priority level in a range appropriate for the given policy.
734     *
735     * @throws IllegalArgumentException Throws IllegalArgumentException if
736     * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
737     * @throws SecurityException Throws SecurityException if your process does
738     * not have permission to modify the given thread, or to use the given
739     * scheduling policy or priority.
740     *
741     * {@hide}
742     */
743    public static final native void setThreadScheduler(int tid, int policy, int priority)
744            throws IllegalArgumentException;
745
746    /**
747     * Determine whether the current environment supports multiple processes.
748     *
749     * @return Returns true if the system can run in multiple processes, else
750     * false if everything is running in a single process.
751     *
752     * @deprecated This method always returns true.  Do not use.
753     */
754    @Deprecated
755    public static final boolean supportsProcesses() {
756        return true;
757    }
758
759    /**
760     * Set the out-of-memory badness adjustment for a process.
761     *
762     * @param pid The process identifier to set.
763     * @param amt Adjustment value -- linux allows -16 to +15.
764     *
765     * @return Returns true if the underlying system supports this
766     *         feature, else false.
767     *
768     * {@hide}
769     */
770    public static final native boolean setOomAdj(int pid, int amt);
771
772    /**
773     * Change this process's argv[0] parameter.  This can be useful to show
774     * more descriptive information in things like the 'ps' command.
775     *
776     * @param text The new name of this process.
777     *
778     * {@hide}
779     */
780    public static final native void setArgV0(String text);
781
782    /**
783     * Kill the process with the given PID.
784     * Note that, though this API allows us to request to
785     * kill any process based on its PID, the kernel will
786     * still impose standard restrictions on which PIDs you
787     * are actually able to kill.  Typically this means only
788     * the process running the caller's packages/application
789     * and any additional processes created by that app; packages
790     * sharing a common UID will also be able to kill each
791     * other's processes.
792     */
793    public static final void killProcess(int pid) {
794        sendSignal(pid, SIGNAL_KILL);
795    }
796
797    /** @hide */
798    public static final native int setUid(int uid);
799
800    /** @hide */
801    public static final native int setGid(int uid);
802
803    /**
804     * Send a signal to the given process.
805     *
806     * @param pid The pid of the target process.
807     * @param signal The signal to send.
808     */
809    public static final native void sendSignal(int pid, int signal);
810
811    /**
812     * @hide
813     * Private impl for avoiding a log message...  DO NOT USE without doing
814     * your own log, or the Android Illuminati will find you some night and
815     * beat you up.
816     */
817    public static final void killProcessQuiet(int pid) {
818        sendSignalQuiet(pid, SIGNAL_KILL);
819    }
820
821    /**
822     * @hide
823     * Private impl for avoiding a log message...  DO NOT USE without doing
824     * your own log, or the Android Illuminati will find you some night and
825     * beat you up.
826     */
827    public static final native void sendSignalQuiet(int pid, int signal);
828
829    /** @hide */
830    public static final native long getFreeMemory();
831
832    /** @hide */
833    public static final native void readProcLines(String path,
834            String[] reqFields, long[] outSizes);
835
836    /** @hide */
837    public static final native int[] getPids(String path, int[] lastArray);
838
839    /** @hide */
840    public static final int PROC_TERM_MASK = 0xff;
841    /** @hide */
842    public static final int PROC_ZERO_TERM = 0;
843    /** @hide */
844    public static final int PROC_SPACE_TERM = (int)' ';
845    /** @hide */
846    public static final int PROC_TAB_TERM = (int)'\t';
847    /** @hide */
848    public static final int PROC_COMBINE = 0x100;
849    /** @hide */
850    public static final int PROC_PARENS = 0x200;
851    /** @hide */
852    public static final int PROC_OUT_STRING = 0x1000;
853    /** @hide */
854    public static final int PROC_OUT_LONG = 0x2000;
855    /** @hide */
856    public static final int PROC_OUT_FLOAT = 0x4000;
857
858    /** @hide */
859    public static final native boolean readProcFile(String file, int[] format,
860            String[] outStrings, long[] outLongs, float[] outFloats);
861
862    /** @hide */
863    public static final native boolean parseProcLine(byte[] buffer, int startIndex,
864            int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
865
866    /**
867     * Gets the total Pss value for a given process, in bytes.
868     *
869     * @param pid the process to the Pss for
870     * @return the total Pss value for the given process in bytes,
871     *  or -1 if the value cannot be determined
872     * @hide
873     */
874    public static final native long getPss(int pid);
875
876    /**
877     * Specifies the outcome of having started a process.
878     * @hide
879     */
880    public static final class ProcessStartResult {
881        /**
882         * The PID of the newly started process.
883         * Always >= 0.  (If the start failed, an exception will have been thrown instead.)
884         */
885        public int pid;
886
887        /**
888         * True if the process was started with a wrapper attached.
889         */
890        public boolean usingWrapper;
891    }
892}
893