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