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