OSDN Git Service

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