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