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.annotation.TestApi;
20import android.system.Os;
21import android.system.OsConstants;
22import android.webkit.WebViewZygote;
23
24import dalvik.system.VMRuntime;
25
26/**
27 * Tools for managing OS processes.
28 */
29public class Process {
30    private static final String LOG_TAG = "Process";
31
32    /**
33     * @hide for internal use only.
34     */
35    public static final String ZYGOTE_SOCKET = "zygote";
36
37    /**
38     * @hide for internal use only.
39     */
40    public static final String SECONDARY_ZYGOTE_SOCKET = "zygote_secondary";
41
42    /**
43     * Defines the root UID.
44     * @hide
45     */
46    public static final int ROOT_UID = 0;
47
48    /**
49     * Defines the UID/GID under which system code runs.
50     */
51    public static final int SYSTEM_UID = 1000;
52
53    /**
54     * Defines the UID/GID under which the telephony code runs.
55     */
56    public static final int PHONE_UID = 1001;
57
58    /**
59     * Defines the UID/GID for the user shell.
60     * @hide
61     */
62    public static final int SHELL_UID = 2000;
63
64    /**
65     * Defines the UID/GID for the log group.
66     * @hide
67     */
68    public static final int LOG_UID = 1007;
69
70    /**
71     * Defines the UID/GID for the WIFI supplicant process.
72     * @hide
73     */
74    public static final int WIFI_UID = 1010;
75
76    /**
77     * Defines the UID/GID for the mediaserver process.
78     * @hide
79     */
80    public static final int MEDIA_UID = 1013;
81
82    /**
83     * Defines the UID/GID for the DRM process.
84     * @hide
85     */
86    public static final int DRM_UID = 1019;
87
88    /**
89     * Defines the UID/GID for the group that controls VPN services.
90     * @hide
91     */
92    public static final int VPN_UID = 1016;
93
94    /**
95     * Defines the UID/GID for keystore.
96     * @hide
97     */
98    public static final int KEYSTORE_UID = 1017;
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 UID/GID for the Bluetooth service process.
108     * @hide
109     */
110    public static final int BLUETOOTH_UID = 1002;
111
112    /**
113     * Defines the GID for the group that allows write access to the internal media storage.
114     * @hide
115     */
116    public static final int MEDIA_RW_GID = 1023;
117
118    /**
119     * Access to installed package details
120     * @hide
121     */
122    public static final int PACKAGE_INFO_GID = 1032;
123
124    /**
125     * Defines the UID/GID for the shared RELRO file updater process.
126     * @hide
127     */
128    public static final int SHARED_RELRO_UID = 1037;
129
130    /**
131     * Defines the UID/GID for the audioserver process.
132     * @hide
133     */
134    public static final int AUDIOSERVER_UID = 1041;
135
136    /**
137     * Defines the UID/GID for the cameraserver process
138     * @hide
139     */
140    public static final int CAMERASERVER_UID = 1047;
141
142    /**
143     * Defines the UID/GID for the WebView zygote process.
144     * @hide
145     */
146    public static final int WEBVIEW_ZYGOTE_UID = 1053;
147
148    /**
149     * Defines the UID used for resource tracking for OTA updates.
150     * @hide
151     */
152    public static final int OTA_UPDATE_UID = 1061;
153
154    /**
155     * Defines the UID used for incidentd.
156     * @hide
157     */
158    public static final int INCIDENTD_UID = 1067;
159
160    /**
161     * Defines the UID/GID for the Secure Element service process.
162     * @hide
163     */
164    public static final int SE_UID = 1068;
165
166    /** {@hide} */
167    public static final int NOBODY_UID = 9999;
168
169    /**
170     * Defines the start of a range of UIDs (and GIDs), going from this
171     * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning
172     * to applications.
173     */
174    public static final int FIRST_APPLICATION_UID = 10000;
175
176    /**
177     * Last of application-specific UIDs starting at
178     * {@link #FIRST_APPLICATION_UID}.
179     */
180    public static final int LAST_APPLICATION_UID = 19999;
181
182    /**
183     * First uid used for fully isolated sandboxed processes (with no permissions of their own)
184     * @hide
185     */
186    public static final int FIRST_ISOLATED_UID = 99000;
187
188    /**
189     * Last uid used for fully isolated sandboxed processes (with no permissions of their own)
190     * @hide
191     */
192    public static final int LAST_ISOLATED_UID = 99999;
193
194    /**
195     * Defines the gid shared by all applications running under the same profile.
196     * @hide
197     */
198    public static final int SHARED_USER_GID = 9997;
199
200    /**
201     * First gid for applications to share resources. Used when forward-locking
202     * is enabled but all UserHandles need to be able to read the resources.
203     * @hide
204     */
205    public static final int FIRST_SHARED_APPLICATION_GID = 50000;
206
207    /**
208     * Last gid for applications to share resources. Used when forward-locking
209     * is enabled but all UserHandles need to be able to read the resources.
210     * @hide
211     */
212    public static final int LAST_SHARED_APPLICATION_GID = 59999;
213
214    /** {@hide} */
215    public static final int FIRST_APPLICATION_CACHE_GID = 20000;
216    /** {@hide} */
217    public static final int LAST_APPLICATION_CACHE_GID = 29999;
218
219    /**
220     * Standard priority of application threads.
221     * Use with {@link #setThreadPriority(int)} and
222     * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
223     * {@link java.lang.Thread} class.
224     */
225    public static final int THREAD_PRIORITY_DEFAULT = 0;
226
227    /*
228     * ***************************************
229     * ** Keep in sync with utils/threads.h **
230     * ***************************************
231     */
232
233    /**
234     * Lowest available thread priority.  Only for those who really, really
235     * don't want to run if anything else is happening.
236     * Use with {@link #setThreadPriority(int)} and
237     * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
238     * {@link java.lang.Thread} class.
239     */
240    public static final int THREAD_PRIORITY_LOWEST = 19;
241
242    /**
243     * Standard priority background threads.  This gives your thread a slightly
244     * lower than normal priority, so that it will have less chance of impacting
245     * the responsiveness of the user interface.
246     * Use with {@link #setThreadPriority(int)} and
247     * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
248     * {@link java.lang.Thread} class.
249     */
250    public static final int THREAD_PRIORITY_BACKGROUND = 10;
251
252    /**
253     * Standard priority of threads that are currently running a user interface
254     * that the user is interacting with.  Applications can not normally
255     * change to this priority; the system will automatically adjust your
256     * application threads as the user moves through the UI.
257     * Use with {@link #setThreadPriority(int)} and
258     * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
259     * {@link java.lang.Thread} class.
260     */
261    public static final int THREAD_PRIORITY_FOREGROUND = -2;
262
263    /**
264     * Standard priority of system display threads, involved in updating
265     * the user interface.  Applications can not
266     * normally change to this priority.
267     * Use with {@link #setThreadPriority(int)} and
268     * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
269     * {@link java.lang.Thread} class.
270     */
271    public static final int THREAD_PRIORITY_DISPLAY = -4;
272
273    /**
274     * Standard priority of the most important display threads, for compositing
275     * the screen and retrieving input events.  Applications can not normally
276     * change to this priority.
277     * Use with {@link #setThreadPriority(int)} and
278     * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
279     * {@link java.lang.Thread} class.
280     */
281    public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8;
282
283    /**
284     * Standard priority of video threads.  Applications can not normally
285     * change to this priority.
286     * Use with {@link #setThreadPriority(int)} and
287     * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
288     * {@link java.lang.Thread} class.
289     */
290    public static final int THREAD_PRIORITY_VIDEO = -10;
291
292    /**
293     * Standard priority of audio threads.  Applications can not normally
294     * change to this priority.
295     * Use with {@link #setThreadPriority(int)} and
296     * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
297     * {@link java.lang.Thread} class.
298     */
299    public static final int THREAD_PRIORITY_AUDIO = -16;
300
301    /**
302     * Standard priority of the most important audio threads.
303     * Applications can not normally change to this priority.
304     * Use with {@link #setThreadPriority(int)} and
305     * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
306     * {@link java.lang.Thread} class.
307     */
308    public static final int THREAD_PRIORITY_URGENT_AUDIO = -19;
309
310    /**
311     * Minimum increment to make a priority more favorable.
312     */
313    public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
314
315    /**
316     * Minimum increment to make a priority less favorable.
317     */
318    public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
319
320    /**
321     * Default scheduling policy
322     * @hide
323     */
324    public static final int SCHED_OTHER = 0;
325
326    /**
327     * First-In First-Out scheduling policy
328     * @hide
329     */
330    public static final int SCHED_FIFO = 1;
331
332    /**
333     * Round-Robin scheduling policy
334     * @hide
335     */
336    public static final int SCHED_RR = 2;
337
338    /**
339     * Batch scheduling policy
340     * @hide
341     */
342    public static final int SCHED_BATCH = 3;
343
344    /**
345     * Idle scheduling policy
346     * @hide
347     */
348    public static final int SCHED_IDLE = 5;
349
350    /**
351     * Reset scheduler choice on fork.
352     * @hide
353     */
354    public static final int SCHED_RESET_ON_FORK = 0x40000000;
355
356    // Keep in sync with SP_* constants of enum type SchedPolicy
357    // declared in system/core/include/cutils/sched_policy.h,
358    // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
359
360    /**
361     * Default thread group -
362     * has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
363     * When used with setProcessGroup(), the group of each thread in the process
364     * is conditionally changed based on that thread's current priority, as follows:
365     * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
366     * are moved to foreground thread group.  All other threads are left unchanged.
367     * @hide
368     */
369    public static final int THREAD_GROUP_DEFAULT = -1;
370
371    /**
372     * Background thread group - All threads in
373     * this group are scheduled with a reduced share of the CPU.
374     * Value is same as constant SP_BACKGROUND of enum SchedPolicy.
375     * FIXME rename to THREAD_GROUP_BACKGROUND.
376     * @hide
377     */
378    public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0;
379
380    /**
381     * Foreground thread group - All threads in
382     * this group are scheduled with a normal share of the CPU.
383     * Value is same as constant SP_FOREGROUND of enum SchedPolicy.
384     * Not used at this level.
385     * @hide
386     **/
387    private static final int THREAD_GROUP_FOREGROUND = 1;
388
389    /**
390     * System thread group.
391     * @hide
392     **/
393    public static final int THREAD_GROUP_SYSTEM = 2;
394
395    /**
396     * Application audio thread group.
397     * @hide
398     **/
399    public static final int THREAD_GROUP_AUDIO_APP = 3;
400
401    /**
402     * System audio thread group.
403     * @hide
404     **/
405    public static final int THREAD_GROUP_AUDIO_SYS = 4;
406
407    /**
408     * Thread group for top foreground app.
409     * @hide
410     **/
411    public static final int THREAD_GROUP_TOP_APP = 5;
412
413    /**
414     * Thread group for RT app.
415     * @hide
416     **/
417    public static final int THREAD_GROUP_RT_APP = 6;
418
419    /**
420     * Thread group for bound foreground services that should
421     * have additional CPU restrictions during screen off
422     * @hide
423     **/
424    public static final int THREAD_GROUP_RESTRICTED = 7;
425
426    public static final int SIGNAL_QUIT = 3;
427    public static final int SIGNAL_KILL = 9;
428    public static final int SIGNAL_USR1 = 10;
429
430    private static long sStartElapsedRealtime;
431    private static long sStartUptimeMillis;
432
433    /**
434     * State associated with the zygote process.
435     * @hide
436     */
437    public static final ZygoteProcess zygoteProcess =
438            new ZygoteProcess(ZYGOTE_SOCKET, SECONDARY_ZYGOTE_SOCKET);
439
440    /**
441     * Start a new process.
442     *
443     * <p>If processes are enabled, a new process is created and the
444     * static main() function of a <var>processClass</var> is executed there.
445     * The process will continue running after this function returns.
446     *
447     * <p>If processes are not enabled, a new thread in the caller's
448     * process is created and main() of <var>processClass</var> called there.
449     *
450     * <p>The niceName parameter, if not an empty string, is a custom name to
451     * give to the process instead of using processClass.  This allows you to
452     * make easily identifyable processes even if you are using the same base
453     * <var>processClass</var> to start them.
454     *
455     * When invokeWith is not null, the process will be started as a fresh app
456     * and not a zygote fork. Note that this is only allowed for uid 0 or when
457     * runtimeFlags contains DEBUG_ENABLE_DEBUGGER.
458     *
459     * @param processClass The class to use as the process's main entry
460     *                     point.
461     * @param niceName A more readable name to use for the process.
462     * @param uid The user-id under which the process will run.
463     * @param gid The group-id under which the process will run.
464     * @param gids Additional group-ids associated with the process.
465     * @param runtimeFlags Additional flags for the runtime.
466     * @param targetSdkVersion The target SDK version for the app.
467     * @param seInfo null-ok SELinux information for the new process.
468     * @param abi non-null the ABI this app should be started with.
469     * @param instructionSet null-ok the instruction set to use.
470     * @param appDataDir null-ok the data directory of the app.
471     * @param invokeWith null-ok the command to invoke with.
472     * @param zygoteArgs Additional arguments to supply to the zygote process.
473     *
474     * @return An object that describes the result of the attempt to start the process.
475     * @throws RuntimeException on fatal start failure
476     *
477     * {@hide}
478     */
479    public static final ProcessStartResult start(final String processClass,
480                                  final String niceName,
481                                  int uid, int gid, int[] gids,
482                                  int runtimeFlags, int mountExternal,
483                                  int targetSdkVersion,
484                                  String seInfo,
485                                  String abi,
486                                  String instructionSet,
487                                  String appDataDir,
488                                  String invokeWith,
489                                  String[] zygoteArgs) {
490        return zygoteProcess.start(processClass, niceName, uid, gid, gids,
491                    runtimeFlags, mountExternal, targetSdkVersion, seInfo,
492                    abi, instructionSet, appDataDir, invokeWith, zygoteArgs);
493    }
494
495    /** @hide */
496    public static final ProcessStartResult startWebView(final String processClass,
497                                  final String niceName,
498                                  int uid, int gid, int[] gids,
499                                  int runtimeFlags, int mountExternal,
500                                  int targetSdkVersion,
501                                  String seInfo,
502                                  String abi,
503                                  String instructionSet,
504                                  String appDataDir,
505                                  String invokeWith,
506                                  String[] zygoteArgs) {
507        return WebViewZygote.getProcess().start(processClass, niceName, uid, gid, gids,
508                    runtimeFlags, mountExternal, targetSdkVersion, seInfo,
509                    abi, instructionSet, appDataDir, invokeWith, zygoteArgs);
510    }
511
512    /**
513     * Returns elapsed milliseconds of the time this process has run.
514     * @return  Returns the number of milliseconds this process has return.
515     */
516    public static final native long getElapsedCpuTime();
517
518    /**
519     * Return the {@link SystemClock#elapsedRealtime()} at which this process was started.
520     */
521    public static final long getStartElapsedRealtime() {
522        return sStartElapsedRealtime;
523    }
524
525    /**
526     * Return the {@link SystemClock#uptimeMillis()} at which this process was started.
527     */
528    public static final long getStartUptimeMillis() {
529        return sStartUptimeMillis;
530    }
531
532    /** @hide */
533    public static final void setStartTimes(long elapsedRealtime, long uptimeMillis) {
534        sStartElapsedRealtime = elapsedRealtime;
535        sStartUptimeMillis = uptimeMillis;
536    }
537
538    /**
539     * Returns true if the current process is a 64-bit runtime.
540     */
541    public static final boolean is64Bit() {
542        return VMRuntime.getRuntime().is64Bit();
543    }
544
545    /**
546     * Returns the identifier of this process, which can be used with
547     * {@link #killProcess} and {@link #sendSignal}.
548     */
549    public static final int myPid() {
550        return Os.getpid();
551    }
552
553    /**
554     * Returns the identifier of this process' parent.
555     * @hide
556     */
557    public static final int myPpid() {
558        return Os.getppid();
559    }
560
561    /**
562     * Returns the identifier of the calling thread, which be used with
563     * {@link #setThreadPriority(int, int)}.
564     */
565    public static final int myTid() {
566        return Os.gettid();
567    }
568
569    /**
570     * Returns the identifier of this process's uid.  This is the kernel uid
571     * that the process is running under, which is the identity of its
572     * app-specific sandbox.  It is different from {@link #myUserHandle} in that
573     * a uid identifies a specific app sandbox in a specific user.
574     */
575    public static final int myUid() {
576        return Os.getuid();
577    }
578
579    /**
580     * Returns this process's user handle.  This is the
581     * user the process is running under.  It is distinct from
582     * {@link #myUid()} in that a particular user will have multiple
583     * distinct apps running under it each with their own uid.
584     */
585    public static UserHandle myUserHandle() {
586        return UserHandle.of(UserHandle.getUserId(myUid()));
587    }
588
589    /**
590     * Returns whether the given uid belongs to a system core component or not.
591     * @hide
592     */
593    public static boolean isCoreUid(int uid) {
594        return UserHandle.isCore(uid);
595    }
596
597    /**
598     * Returns whether the given uid belongs to an application.
599     * @param uid A kernel uid.
600     * @return Whether the uid corresponds to an application sandbox running in
601     *     a specific user.
602     */
603    public static boolean isApplicationUid(int uid) {
604        return UserHandle.isApp(uid);
605    }
606
607    /**
608     * Returns whether the current process is in an isolated sandbox.
609     */
610    public static final boolean isIsolated() {
611        return isIsolated(myUid());
612    }
613
614    /** {@hide} */
615    public static final boolean isIsolated(int uid) {
616        uid = UserHandle.getAppId(uid);
617        return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID;
618    }
619
620    /**
621     * Returns the UID assigned to a particular user name, or -1 if there is
622     * none.  If the given string consists of only numbers, it is converted
623     * directly to a uid.
624     */
625    public static final native int getUidForName(String name);
626
627    /**
628     * Returns the GID assigned to a particular user name, or -1 if there is
629     * none.  If the given string consists of only numbers, it is converted
630     * directly to a gid.
631     */
632    public static final native int getGidForName(String name);
633
634    /**
635     * Returns a uid for a currently running process.
636     * @param pid the process id
637     * @return the uid of the process, or -1 if the process is not running.
638     * @hide pending API council review
639     */
640    public static final int getUidForPid(int pid) {
641        String[] procStatusLabels = { "Uid:" };
642        long[] procStatusValues = new long[1];
643        procStatusValues[0] = -1;
644        Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
645        return (int) procStatusValues[0];
646    }
647
648    /**
649     * Returns the parent process id for a currently running process.
650     * @param pid the process id
651     * @return the parent process id of the process, or -1 if the process is not running.
652     * @hide
653     */
654    public static final int getParentPid(int pid) {
655        String[] procStatusLabels = { "PPid:" };
656        long[] procStatusValues = new long[1];
657        procStatusValues[0] = -1;
658        Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
659        return (int) procStatusValues[0];
660    }
661
662    /**
663     * Returns the thread group leader id for a currently running thread.
664     * @param tid the thread id
665     * @return the thread group leader id of the thread, or -1 if the thread is not running.
666     *         This is same as what getpid(2) would return if called by tid.
667     * @hide
668     */
669    public static final int getThreadGroupLeader(int tid) {
670        String[] procStatusLabels = { "Tgid:" };
671        long[] procStatusValues = new long[1];
672        procStatusValues[0] = -1;
673        Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues);
674        return (int) procStatusValues[0];
675    }
676
677    /**
678     * Set the priority of a thread, based on Linux priorities.
679     *
680     * @param tid The identifier of the thread/process to change.
681     * @param priority A Linux priority level, from -20 for highest scheduling
682     * priority to 19 for lowest scheduling priority.
683     *
684     * @throws IllegalArgumentException Throws IllegalArgumentException if
685     * <var>tid</var> does not exist.
686     * @throws SecurityException Throws SecurityException if your process does
687     * not have permission to modify the given thread, or to use the given
688     * priority.
689     */
690    public static final native void setThreadPriority(int tid, int priority)
691            throws IllegalArgumentException, SecurityException;
692
693    /**
694     * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
695     * throw an exception if passed a background-level thread priority.  This is only
696     * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
697     *
698     * @hide
699     */
700    public static final native void setCanSelfBackground(boolean backgroundOk);
701
702    /**
703     * Sets the scheduling group for a thread.
704     * @hide
705     * @param tid The identifier of the thread to change.
706     * @param group The target group for this thread from THREAD_GROUP_*.
707     *
708     * @throws IllegalArgumentException Throws IllegalArgumentException if
709     * <var>tid</var> does not exist.
710     * @throws SecurityException Throws SecurityException if your process does
711     * not have permission to modify the given thread, or to use the given
712     * priority.
713     * If the thread is a thread group leader, that is it's gettid() == getpid(),
714     * then the other threads in the same thread group are _not_ affected.
715     *
716     * Does not set cpuset for some historical reason, just calls
717     * libcutils::set_sched_policy().
718     */
719    public static final native void setThreadGroup(int tid, int group)
720            throws IllegalArgumentException, SecurityException;
721
722    /**
723     * Sets the scheduling group and the corresponding cpuset group
724     * @hide
725     * @param tid The identifier of the thread to change.
726     * @param group The target group for this thread from THREAD_GROUP_*.
727     *
728     * @throws IllegalArgumentException Throws IllegalArgumentException if
729     * <var>tid</var> does not exist.
730     * @throws SecurityException Throws SecurityException if your process does
731     * not have permission to modify the given thread, or to use the given
732     * priority.
733     */
734    public static final native void setThreadGroupAndCpuset(int tid, int group)
735            throws IllegalArgumentException, SecurityException;
736
737    /**
738     * Sets the scheduling group for a process and all child threads
739     * @hide
740     * @param pid The identifier of the process to change.
741     * @param group The target group for this process from THREAD_GROUP_*.
742     *
743     * @throws IllegalArgumentException Throws IllegalArgumentException if
744     * <var>tid</var> does not exist.
745     * @throws SecurityException Throws SecurityException if your process does
746     * not have permission to modify the given thread, or to use the given
747     * priority.
748     *
749     * group == THREAD_GROUP_DEFAULT means to move all non-background priority
750     * threads to the foreground scheduling group, but to leave background
751     * priority threads alone.  group == THREAD_GROUP_BG_NONINTERACTIVE moves all
752     * threads, regardless of priority, to the background scheduling group.
753     * group == THREAD_GROUP_FOREGROUND is not allowed.
754     *
755     * Always sets cpusets.
756     */
757    public static final native void setProcessGroup(int pid, int group)
758            throws IllegalArgumentException, SecurityException;
759
760    /**
761     * Return the scheduling group of requested process.
762     *
763     * @hide
764     */
765    public static final native int getProcessGroup(int pid)
766            throws IllegalArgumentException, SecurityException;
767
768    /**
769     * On some devices, the foreground process may have one or more CPU
770     * cores exclusively reserved for it. This method can be used to
771     * retrieve which cores that are (if any), so the calling process
772     * can then use sched_setaffinity() to lock a thread to these cores.
773     * Note that the calling process must currently be running in the
774     * foreground for this method to return any cores.
775     *
776     * The CPU core(s) exclusively reserved for the foreground process will
777     * stay reserved for as long as the process stays in the foreground.
778     *
779     * As soon as a process leaves the foreground, those CPU cores will
780     * no longer be reserved for it, and will most likely be reserved for
781     * the new foreground process. It's not necessary to change the affinity
782     * of your process when it leaves the foreground (if you had previously
783     * set it to use a reserved core); the OS will automatically take care
784     * of resetting the affinity at that point.
785     *
786     * @return an array of integers, indicating the CPU cores exclusively
787     * reserved for this process. The array will have length zero if no
788     * CPU cores are exclusively reserved for this process at this point
789     * in time.
790     */
791    public static final native int[] getExclusiveCores();
792
793    /**
794     * Set the priority of the calling thread, based on Linux priorities.  See
795     * {@link #setThreadPriority(int, int)} for more information.
796     *
797     * @param priority A Linux priority level, from -20 for highest scheduling
798     * priority to 19 for lowest scheduling priority.
799     *
800     * @throws IllegalArgumentException Throws IllegalArgumentException if
801     * <var>tid</var> does not exist.
802     * @throws SecurityException Throws SecurityException if your process does
803     * not have permission to modify the given thread, or to use the given
804     * priority.
805     *
806     * @see #setThreadPriority(int, int)
807     */
808    public static final native void setThreadPriority(int priority)
809            throws IllegalArgumentException, SecurityException;
810
811    /**
812     * Return the current priority of a thread, based on Linux priorities.
813     *
814     * @param tid The identifier of the thread/process. If tid equals zero, the priority of the
815     * calling process/thread will be returned.
816     *
817     * @return Returns the current priority, as a Linux priority level,
818     * from -20 for highest scheduling priority to 19 for lowest scheduling
819     * priority.
820     *
821     * @throws IllegalArgumentException Throws IllegalArgumentException if
822     * <var>tid</var> does not exist.
823     */
824    public static final native int getThreadPriority(int tid)
825            throws IllegalArgumentException;
826
827    /**
828     * Return the current scheduling policy of a thread, based on Linux.
829     *
830     * @param tid The identifier of the thread/process to get the scheduling policy.
831     *
832     * @throws IllegalArgumentException Throws IllegalArgumentException if
833     * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
834     * @throws SecurityException Throws SecurityException if your process does
835     * not have permission to modify the given thread, or to use the given
836     * scheduling policy or priority.
837     *
838     * {@hide}
839     */
840
841    @TestApi
842    public static final native int getThreadScheduler(int tid)
843            throws IllegalArgumentException;
844
845    /**
846     * Set the scheduling policy and priority of a thread, based on Linux.
847     *
848     * @param tid The identifier of the thread/process to change.
849     * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
850     * @param priority A Linux priority level in a range appropriate for the given policy.
851     *
852     * @throws IllegalArgumentException Throws IllegalArgumentException if
853     * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
854     * @throws SecurityException Throws SecurityException if your process does
855     * not have permission to modify the given thread, or to use the given
856     * scheduling policy or priority.
857     *
858     * {@hide}
859     */
860
861    public static final native void setThreadScheduler(int tid, int policy, int priority)
862            throws IllegalArgumentException;
863
864    /**
865     * Determine whether the current environment supports multiple processes.
866     *
867     * @return Returns true if the system can run in multiple processes, else
868     * false if everything is running in a single process.
869     *
870     * @deprecated This method always returns true.  Do not use.
871     */
872    @Deprecated
873    public static final boolean supportsProcesses() {
874        return true;
875    }
876
877    /**
878     * Adjust the swappiness level for a process.
879     *
880     * @param pid The process identifier to set.
881     * @param is_increased Whether swappiness should be increased or default.
882     *
883     * @return Returns true if the underlying system supports this
884     *         feature, else false.
885     *
886     * {@hide}
887     */
888    public static final native boolean setSwappiness(int pid, boolean is_increased);
889
890    /**
891     * Change this process's argv[0] parameter.  This can be useful to show
892     * more descriptive information in things like the 'ps' command.
893     *
894     * @param text The new name of this process.
895     *
896     * {@hide}
897     */
898    public static final native void setArgV0(String text);
899
900    /**
901     * Kill the process with the given PID.
902     * Note that, though this API allows us to request to
903     * kill any process based on its PID, the kernel will
904     * still impose standard restrictions on which PIDs you
905     * are actually able to kill.  Typically this means only
906     * the process running the caller's packages/application
907     * and any additional processes created by that app; packages
908     * sharing a common UID will also be able to kill each
909     * other's processes.
910     */
911    public static final void killProcess(int pid) {
912        sendSignal(pid, SIGNAL_KILL);
913    }
914
915    /** @hide */
916    public static final native int setUid(int uid);
917
918    /** @hide */
919    public static final native int setGid(int uid);
920
921    /**
922     * Send a signal to the given process.
923     *
924     * @param pid The pid of the target process.
925     * @param signal The signal to send.
926     */
927    public static final native void sendSignal(int pid, int signal);
928
929    /**
930     * @hide
931     * Private impl for avoiding a log message...  DO NOT USE without doing
932     * your own log, or the Android Illuminati will find you some night and
933     * beat you up.
934     */
935    public static final void killProcessQuiet(int pid) {
936        sendSignalQuiet(pid, SIGNAL_KILL);
937    }
938
939    /**
940     * @hide
941     * Private impl for avoiding a log message...  DO NOT USE without doing
942     * your own log, or the Android Illuminati will find you some night and
943     * beat you up.
944     */
945    public static final native void sendSignalQuiet(int pid, int signal);
946
947    /** @hide */
948    public static final native long getFreeMemory();
949
950    /** @hide */
951    public static final native long getTotalMemory();
952
953    /** @hide */
954    public static final native void readProcLines(String path,
955            String[] reqFields, long[] outSizes);
956
957    /** @hide */
958    public static final native int[] getPids(String path, int[] lastArray);
959
960    /** @hide */
961    public static final int PROC_TERM_MASK = 0xff;
962    /** @hide */
963    public static final int PROC_ZERO_TERM = 0;
964    /** @hide */
965    public static final int PROC_SPACE_TERM = (int)' ';
966    /** @hide */
967    public static final int PROC_TAB_TERM = (int)'\t';
968    /** @hide */
969    public static final int PROC_COMBINE = 0x100;
970    /** @hide */
971    public static final int PROC_PARENS = 0x200;
972    /** @hide */
973    public static final int PROC_QUOTES = 0x400;
974    /** @hide */
975    public static final int PROC_CHAR = 0x800;
976    /** @hide */
977    public static final int PROC_OUT_STRING = 0x1000;
978    /** @hide */
979    public static final int PROC_OUT_LONG = 0x2000;
980    /** @hide */
981    public static final int PROC_OUT_FLOAT = 0x4000;
982
983    /** @hide */
984    public static final native boolean readProcFile(String file, int[] format,
985            String[] outStrings, long[] outLongs, float[] outFloats);
986
987    /** @hide */
988    public static final native boolean parseProcLine(byte[] buffer, int startIndex,
989            int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
990
991    /** @hide */
992    public static final native int[] getPidsForCommands(String[] cmds);
993
994    /**
995     * Gets the total Pss value for a given process, in bytes.
996     *
997     * @param pid the process to the Pss for
998     * @return the total Pss value for the given process in bytes,
999     *  or -1 if the value cannot be determined
1000     * @hide
1001     */
1002    public static final native long getPss(int pid);
1003
1004    /**
1005     * Specifies the outcome of having started a process.
1006     * @hide
1007     */
1008    public static final class ProcessStartResult {
1009        /**
1010         * The PID of the newly started process.
1011         * Always >= 0.  (If the start failed, an exception will have been thrown instead.)
1012         */
1013        public int pid;
1014
1015        /**
1016         * True if the process was started with a wrapper attached.
1017         */
1018        public boolean usingWrapper;
1019    }
1020
1021    /**
1022     * Kill all processes in a process group started for the given
1023     * pid.
1024     * @hide
1025     */
1026    public static final native int killProcessGroup(int uid, int pid);
1027
1028    /**
1029     * Remove all process groups.  Expected to be called when ActivityManager
1030     * is restarted.
1031     * @hide
1032     */
1033    public static final native void removeAllProcessGroups();
1034
1035    /**
1036     * Check to see if a thread belongs to a given process. This may require
1037     * more permissions than apps generally have.
1038     * @return true if this thread belongs to a process
1039     * @hide
1040     */
1041    public static final boolean isThreadInProcess(int tid, int pid) {
1042        StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
1043        try {
1044            if (Os.access("/proc/" + tid + "/task/" + pid, OsConstants.F_OK)) {
1045                return true;
1046            } else {
1047                return false;
1048            }
1049        } catch (Exception e) {
1050            return false;
1051        } finally {
1052            StrictMode.setThreadPolicy(oldPolicy);
1053        }
1054
1055    }
1056}
1057