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