Process.java revision 5294a2fc2f401cd248a4bc3f0f9ee1e2854f4e9a
1/* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.os; 18 19import android.net.LocalSocketAddress; 20import android.net.LocalSocket; 21import android.util.Log; 22import dalvik.system.Zygote; 23 24import java.io.BufferedWriter; 25import java.io.DataInputStream; 26import java.io.IOException; 27import java.io.OutputStreamWriter; 28import java.util.ArrayList; 29 30/*package*/ class ZygoteStartFailedEx extends Exception { 31 /** 32 * Something prevented the zygote process startup from happening normally 33 */ 34 35 ZygoteStartFailedEx() {}; 36 ZygoteStartFailedEx(String s) {super(s);} 37 ZygoteStartFailedEx(Throwable cause) {super(cause);} 38} 39 40/** 41 * Tools for managing OS processes. 42 */ 43public class Process { 44 private static final String LOG_TAG = "Process"; 45 46 private static final String ZYGOTE_SOCKET = "zygote"; 47 48 /** 49 * Name of a process for running the platform's media services. 50 * {@hide} 51 */ 52 public static final String ANDROID_SHARED_MEDIA = "com.android.process.media"; 53 54 /** 55 * Name of the process that Google content providers can share. 56 * {@hide} 57 */ 58 public static final String GOOGLE_SHARED_APP_CONTENT = "com.google.process.content"; 59 60 /** 61 * Defines the UID/GID under which system code runs. 62 */ 63 public static final int SYSTEM_UID = 1000; 64 65 /** 66 * Defines the UID/GID under which the telephony code runs. 67 */ 68 public static final int PHONE_UID = 1001; 69 70 /** 71 * Defines the UID/GID for the user shell. 72 * @hide 73 */ 74 public static final int SHELL_UID = 2000; 75 76 /** 77 * Defines the UID/GID for the log group. 78 * @hide 79 */ 80 public static final int LOG_UID = 1007; 81 82 /** 83 * Defines the UID/GID for the WIFI supplicant process. 84 * @hide 85 */ 86 public static final int WIFI_UID = 1010; 87 88 /** 89 * Defines the UID/GID for the mediaserver process. 90 * @hide 91 */ 92 public static final int MEDIA_UID = 1013; 93 94 /** 95 * Defines the UID/GID for the DRM process. 96 * @hide 97 */ 98 public static final int DRM_UID = 1019; 99 100 /** 101 * Defines the GID for the group that allows write access to the SD card. 102 * @hide 103 */ 104 public static final int SDCARD_RW_GID = 1015; 105 106 /** 107 * Defines the UID/GID for the group that controls VPN services. 108 * @hide 109 */ 110 public static final int VPN_UID = 1016; 111 112 /** 113 * Defines the UID/GID for the NFC service process. 114 * @hide 115 */ 116 public static final int NFC_UID = 1027; 117 118 /** 119 * Defines the GID for the group that allows write access to the internal media storage. 120 * @hide 121 */ 122 public static final int MEDIA_RW_GID = 1023; 123 124 /** 125 * Defines the start of a range of UIDs (and GIDs), going from this 126 * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning 127 * to applications. 128 */ 129 public static final int FIRST_APPLICATION_UID = 10000; 130 /** 131 * Last of application-specific UIDs starting at 132 * {@link #FIRST_APPLICATION_UID}. 133 */ 134 public static final int LAST_APPLICATION_UID = 19999; 135 136 /** 137 * First uid used for fully isolated sandboxed processes (with no permissions of their own) 138 * @hide 139 */ 140 public static final int FIRST_ISOLATED_UID = 99000; 141 142 /** 143 * Last uid used for fully isolated sandboxed processes (with no permissions of their own) 144 * @hide 145 */ 146 public static final int LAST_ISOLATED_UID = 99999; 147 148 /** 149 * Defines a secondary group id for access to the bluetooth hardware. 150 */ 151 public static final int BLUETOOTH_GID = 2000; 152 153 /** 154 * Standard priority of application threads. 155 * Use with {@link #setThreadPriority(int)} and 156 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal 157 * {@link java.lang.Thread} class. 158 */ 159 public static final int THREAD_PRIORITY_DEFAULT = 0; 160 161 /* 162 * *************************************** 163 * ** Keep in sync with utils/threads.h ** 164 * *************************************** 165 */ 166 167 /** 168 * Lowest available thread priority. Only for those who really, really 169 * don't want to run if anything else is happening. 170 * Use with {@link #setThreadPriority(int)} and 171 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal 172 * {@link java.lang.Thread} class. 173 */ 174 public static final int THREAD_PRIORITY_LOWEST = 19; 175 176 /** 177 * Standard priority background threads. This gives your thread a slightly 178 * lower than normal priority, so that it will have less chance of impacting 179 * the responsiveness of the user interface. 180 * Use with {@link #setThreadPriority(int)} and 181 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal 182 * {@link java.lang.Thread} class. 183 */ 184 public static final int THREAD_PRIORITY_BACKGROUND = 10; 185 186 /** 187 * Standard priority of threads that are currently running a user interface 188 * that the user is interacting with. Applications can not normally 189 * change to this priority; the system will automatically adjust your 190 * application threads as the user moves through the UI. 191 * Use with {@link #setThreadPriority(int)} and 192 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal 193 * {@link java.lang.Thread} class. 194 */ 195 public static final int THREAD_PRIORITY_FOREGROUND = -2; 196 197 /** 198 * Standard priority of system display threads, involved in updating 199 * the user interface. Applications can not 200 * normally change to this priority. 201 * Use with {@link #setThreadPriority(int)} and 202 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal 203 * {@link java.lang.Thread} class. 204 */ 205 public static final int THREAD_PRIORITY_DISPLAY = -4; 206 207 /** 208 * Standard priority of the most important display threads, for compositing 209 * the screen and retrieving input events. Applications can not normally 210 * change to this priority. 211 * Use with {@link #setThreadPriority(int)} and 212 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal 213 * {@link java.lang.Thread} class. 214 */ 215 public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8; 216 217 /** 218 * Standard priority of audio threads. Applications can not normally 219 * change to this priority. 220 * Use with {@link #setThreadPriority(int)} and 221 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal 222 * {@link java.lang.Thread} class. 223 */ 224 public static final int THREAD_PRIORITY_AUDIO = -16; 225 226 /** 227 * Standard priority of the most important audio threads. 228 * Applications can not normally change to this priority. 229 * Use with {@link #setThreadPriority(int)} and 230 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal 231 * {@link java.lang.Thread} class. 232 */ 233 public static final int THREAD_PRIORITY_URGENT_AUDIO = -19; 234 235 /** 236 * Minimum increment to make a priority more favorable. 237 */ 238 public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1; 239 240 /** 241 * Minimum increment to make a priority less favorable. 242 */ 243 public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1; 244 245 /** 246 * Default scheduling policy 247 * @hide 248 */ 249 public static final int SCHED_OTHER = 0; 250 251 /** 252 * First-In First-Out scheduling policy 253 * @hide 254 */ 255 public static final int SCHED_FIFO = 1; 256 257 /** 258 * Round-Robin scheduling policy 259 * @hide 260 */ 261 public static final int SCHED_RR = 2; 262 263 /** 264 * Batch scheduling policy 265 * @hide 266 */ 267 public static final int SCHED_BATCH = 3; 268 269 /** 270 * Idle scheduling policy 271 * @hide 272 */ 273 public static final int SCHED_IDLE = 5; 274 275 /** 276 * Default thread group - gets a 'normal' share of the CPU 277 * @hide 278 */ 279 public static final int THREAD_GROUP_DEFAULT = 0; 280 281 /** 282 * Background non-interactive thread group - All threads in 283 * this group are scheduled with a reduced share of the CPU. 284 * @hide 285 */ 286 public static final int THREAD_GROUP_BG_NONINTERACTIVE = 1; 287 288 /** 289 * Foreground 'boost' thread group - All threads in 290 * this group are scheduled with an increased share of the CPU 291 * @hide 292 **/ 293 public static final int THREAD_GROUP_FG_BOOST = 2; 294 295 public static final int SIGNAL_QUIT = 3; 296 public static final int SIGNAL_KILL = 9; 297 public static final int SIGNAL_USR1 = 10; 298 299 // State for communicating with zygote process 300 301 static LocalSocket sZygoteSocket; 302 static DataInputStream sZygoteInputStream; 303 static BufferedWriter sZygoteWriter; 304 305 /** true if previous zygote open failed */ 306 static boolean sPreviousZygoteOpenFailed; 307 308 /** 309 * Start a new process. 310 * 311 * <p>If processes are enabled, a new process is created and the 312 * static main() function of a <var>processClass</var> is executed there. 313 * The process will continue running after this function returns. 314 * 315 * <p>If processes are not enabled, a new thread in the caller's 316 * process is created and main() of <var>processClass</var> called there. 317 * 318 * <p>The niceName parameter, if not an empty string, is a custom name to 319 * give to the process instead of using processClass. This allows you to 320 * make easily identifyable processes even if you are using the same base 321 * <var>processClass</var> to start them. 322 * 323 * @param processClass The class to use as the process's main entry 324 * point. 325 * @param niceName A more readable name to use for the process. 326 * @param uid The user-id under which the process will run. 327 * @param gid The group-id under which the process will run. 328 * @param gids Additional group-ids associated with the process. 329 * @param debugFlags Additional flags. 330 * @param targetSdkVersion The target SDK version for the app. 331 * @param zygoteArgs Additional arguments to supply to the zygote process. 332 * 333 * @return An object that describes the result of the attempt to start the process. 334 * @throws RuntimeException on fatal start failure 335 * 336 * {@hide} 337 */ 338 public static final ProcessStartResult start(final String processClass, 339 final String niceName, 340 int uid, int gid, int[] gids, 341 int debugFlags, int targetSdkVersion, 342 String[] zygoteArgs) { 343 try { 344 return startViaZygote(processClass, niceName, uid, gid, gids, 345 debugFlags, targetSdkVersion, zygoteArgs); 346 } catch (ZygoteStartFailedEx ex) { 347 Log.e(LOG_TAG, 348 "Starting VM process through Zygote failed"); 349 throw new RuntimeException( 350 "Starting VM process through Zygote failed", ex); 351 } 352 } 353 354 /** retry interval for opening a zygote socket */ 355 static final int ZYGOTE_RETRY_MILLIS = 500; 356 357 /** 358 * Tries to open socket to Zygote process if not already open. If 359 * already open, does nothing. May block and retry. 360 */ 361 private static void openZygoteSocketIfNeeded() 362 throws ZygoteStartFailedEx { 363 364 int retryCount; 365 366 if (sPreviousZygoteOpenFailed) { 367 /* 368 * If we've failed before, expect that we'll fail again and 369 * don't pause for retries. 370 */ 371 retryCount = 0; 372 } else { 373 retryCount = 10; 374 } 375 376 /* 377 * See bug #811181: Sometimes runtime can make it up before zygote. 378 * Really, we'd like to do something better to avoid this condition, 379 * but for now just wait a bit... 380 */ 381 for (int retry = 0 382 ; (sZygoteSocket == null) && (retry < (retryCount + 1)) 383 ; retry++ ) { 384 385 if (retry > 0) { 386 try { 387 Log.i("Zygote", "Zygote not up yet, sleeping..."); 388 Thread.sleep(ZYGOTE_RETRY_MILLIS); 389 } catch (InterruptedException ex) { 390 // should never happen 391 } 392 } 393 394 try { 395 sZygoteSocket = new LocalSocket(); 396 397 sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET, 398 LocalSocketAddress.Namespace.RESERVED)); 399 400 sZygoteInputStream 401 = new DataInputStream(sZygoteSocket.getInputStream()); 402 403 sZygoteWriter = 404 new BufferedWriter( 405 new OutputStreamWriter( 406 sZygoteSocket.getOutputStream()), 407 256); 408 409 Log.i("Zygote", "Process: zygote socket opened"); 410 411 sPreviousZygoteOpenFailed = false; 412 break; 413 } catch (IOException ex) { 414 if (sZygoteSocket != null) { 415 try { 416 sZygoteSocket.close(); 417 } catch (IOException ex2) { 418 Log.e(LOG_TAG,"I/O exception on close after exception", 419 ex2); 420 } 421 } 422 423 sZygoteSocket = null; 424 } 425 } 426 427 if (sZygoteSocket == null) { 428 sPreviousZygoteOpenFailed = true; 429 throw new ZygoteStartFailedEx("connect failed"); 430 } 431 } 432 433 /** 434 * Sends an argument list to the zygote process, which starts a new child 435 * and returns the child's pid. Please note: the present implementation 436 * replaces newlines in the argument list with spaces. 437 * @param args argument list 438 * @return An object that describes the result of the attempt to start the process. 439 * @throws ZygoteStartFailedEx if process start failed for any reason 440 */ 441 private static ProcessStartResult zygoteSendArgsAndGetResult(ArrayList<String> args) 442 throws ZygoteStartFailedEx { 443 openZygoteSocketIfNeeded(); 444 445 try { 446 /** 447 * See com.android.internal.os.ZygoteInit.readArgumentList() 448 * Presently the wire format to the zygote process is: 449 * a) a count of arguments (argc, in essence) 450 * b) a number of newline-separated argument strings equal to count 451 * 452 * After the zygote process reads these it will write the pid of 453 * the child or -1 on failure, followed by boolean to 454 * indicate whether a wrapper process was used. 455 */ 456 457 sZygoteWriter.write(Integer.toString(args.size())); 458 sZygoteWriter.newLine(); 459 460 int sz = args.size(); 461 for (int i = 0; i < sz; i++) { 462 String arg = args.get(i); 463 if (arg.indexOf('\n') >= 0) { 464 throw new ZygoteStartFailedEx( 465 "embedded newlines not allowed"); 466 } 467 sZygoteWriter.write(arg); 468 sZygoteWriter.newLine(); 469 } 470 471 sZygoteWriter.flush(); 472 473 // Should there be a timeout on this? 474 ProcessStartResult result = new ProcessStartResult(); 475 result.pid = sZygoteInputStream.readInt(); 476 if (result.pid < 0) { 477 throw new ZygoteStartFailedEx("fork() failed"); 478 } 479 result.usingWrapper = sZygoteInputStream.readBoolean(); 480 return result; 481 } catch (IOException ex) { 482 try { 483 if (sZygoteSocket != null) { 484 sZygoteSocket.close(); 485 } 486 } catch (IOException ex2) { 487 // we're going to fail anyway 488 Log.e(LOG_TAG,"I/O exception on routine close", ex2); 489 } 490 491 sZygoteSocket = null; 492 493 throw new ZygoteStartFailedEx(ex); 494 } 495 } 496 497 /** 498 * Starts a new process via the zygote mechanism. 499 * 500 * @param processClass Class name whose static main() to run 501 * @param niceName 'nice' process name to appear in ps 502 * @param uid a POSIX uid that the new process should setuid() to 503 * @param gid a POSIX gid that the new process shuold setgid() to 504 * @param gids null-ok; a list of supplementary group IDs that the 505 * new process should setgroup() to. 506 * @param debugFlags Additional flags. 507 * @param targetSdkVersion The target SDK version for the app. 508 * @param extraArgs Additional arguments to supply to the zygote process. 509 * @return An object that describes the result of the attempt to start the process. 510 * @throws ZygoteStartFailedEx if process start failed for any reason 511 */ 512 private static ProcessStartResult startViaZygote(final String processClass, 513 final String niceName, 514 final int uid, final int gid, 515 final int[] gids, 516 int debugFlags, int targetSdkVersion, 517 String[] extraArgs) 518 throws ZygoteStartFailedEx { 519 synchronized(Process.class) { 520 ArrayList<String> argsForZygote = new ArrayList<String>(); 521 522 // --runtime-init, --setuid=, --setgid=, 523 // and --setgroups= must go first 524 argsForZygote.add("--runtime-init"); 525 argsForZygote.add("--setuid=" + uid); 526 argsForZygote.add("--setgid=" + gid); 527 if ((debugFlags & Zygote.DEBUG_ENABLE_JNI_LOGGING) != 0) { 528 argsForZygote.add("--enable-jni-logging"); 529 } 530 if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) { 531 argsForZygote.add("--enable-safemode"); 532 } 533 if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) { 534 argsForZygote.add("--enable-debugger"); 535 } 536 if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) { 537 argsForZygote.add("--enable-checkjni"); 538 } 539 if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) { 540 argsForZygote.add("--enable-assert"); 541 } 542 argsForZygote.add("--target-sdk-version=" + targetSdkVersion); 543 544 //TODO optionally enable debuger 545 //argsForZygote.add("--enable-debugger"); 546 547 // --setgroups is a comma-separated list 548 if (gids != null && gids.length > 0) { 549 StringBuilder sb = new StringBuilder(); 550 sb.append("--setgroups="); 551 552 int sz = gids.length; 553 for (int i = 0; i < sz; i++) { 554 if (i != 0) { 555 sb.append(','); 556 } 557 sb.append(gids[i]); 558 } 559 560 argsForZygote.add(sb.toString()); 561 } 562 563 if (niceName != null) { 564 argsForZygote.add("--nice-name=" + niceName); 565 } 566 567 argsForZygote.add(processClass); 568 569 if (extraArgs != null) { 570 for (String arg : extraArgs) { 571 argsForZygote.add(arg); 572 } 573 } 574 575 return zygoteSendArgsAndGetResult(argsForZygote); 576 } 577 } 578 579 /** 580 * Returns elapsed milliseconds of the time this process has run. 581 * @return Returns the number of milliseconds this process has return. 582 */ 583 public static final native long getElapsedCpuTime(); 584 585 /** 586 * Returns the identifier of this process, which can be used with 587 * {@link #killProcess} and {@link #sendSignal}. 588 */ 589 public static final native int myPid(); 590 591 /** 592 * Returns the identifier of the calling thread, which be used with 593 * {@link #setThreadPriority(int, int)}. 594 */ 595 public static final native int myTid(); 596 597 /** 598 * Returns the identifier of this process's user. 599 */ 600 public static final native int myUid(); 601 602 /** 603 * Returns whether the current process is in an isolated sandbox. 604 * @hide 605 */ 606 public static final boolean isIsolated() { 607 int uid = UserId.getAppId(myUid()); 608 return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID; 609 } 610 611 /** 612 * Returns the UID assigned to a particular user name, or -1 if there is 613 * none. If the given string consists of only numbers, it is converted 614 * directly to a uid. 615 */ 616 public static final native int getUidForName(String name); 617 618 /** 619 * Returns the GID assigned to a particular user name, or -1 if there is 620 * none. If the given string consists of only numbers, it is converted 621 * directly to a gid. 622 */ 623 public static final native int getGidForName(String name); 624 625 /** 626 * Returns a uid for a currently running process. 627 * @param pid the process id 628 * @return the uid of the process, or -1 if the process is not running. 629 * @hide pending API council review 630 */ 631 public static final int getUidForPid(int pid) { 632 String[] procStatusLabels = { "Uid:" }; 633 long[] procStatusValues = new long[1]; 634 procStatusValues[0] = -1; 635 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues); 636 return (int) procStatusValues[0]; 637 } 638 639 /** 640 * Returns the parent process id for a currently running process. 641 * @param pid the process id 642 * @return the parent process id of the process, or -1 if the process is not running. 643 * @hide 644 */ 645 public static final int getParentPid(int pid) { 646 String[] procStatusLabels = { "PPid:" }; 647 long[] procStatusValues = new long[1]; 648 procStatusValues[0] = -1; 649 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues); 650 return (int) procStatusValues[0]; 651 } 652 653 /** 654 * Set the priority of a thread, based on Linux priorities. 655 * 656 * @param tid The identifier of the thread/process to change. 657 * @param priority A Linux priority level, from -20 for highest scheduling 658 * priority to 19 for lowest scheduling priority. 659 * 660 * @throws IllegalArgumentException Throws IllegalArgumentException if 661 * <var>tid</var> does not exist. 662 * @throws SecurityException Throws SecurityException if your process does 663 * not have permission to modify the given thread, or to use the given 664 * priority. 665 */ 666 public static final native void setThreadPriority(int tid, int priority) 667 throws IllegalArgumentException, SecurityException; 668 669 /** 670 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to 671 * throw an exception if passed a background-level thread priority. This is only 672 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1. 673 * 674 * @hide 675 */ 676 public static final native void setCanSelfBackground(boolean backgroundOk); 677 678 /** 679 * Sets the scheduling group for a thread. 680 * @hide 681 * @param tid The indentifier of the thread/process to change. 682 * @param group The target group for this thread/process. 683 * 684 * @throws IllegalArgumentException Throws IllegalArgumentException if 685 * <var>tid</var> does not exist. 686 * @throws SecurityException Throws SecurityException if your process does 687 * not have permission to modify the given thread, or to use the given 688 * priority. 689 */ 690 public static final native void setThreadGroup(int tid, int group) 691 throws IllegalArgumentException, SecurityException; 692 /** 693 * Sets the scheduling group for a process and all child threads 694 * @hide 695 * @param pid The indentifier of the process to change. 696 * @param group The target group for this process. 697 * 698 * @throws IllegalArgumentException Throws IllegalArgumentException if 699 * <var>tid</var> does not exist. 700 * @throws SecurityException Throws SecurityException if your process does 701 * not have permission to modify the given thread, or to use the given 702 * priority. 703 */ 704 public static final native void setProcessGroup(int pid, int group) 705 throws IllegalArgumentException, SecurityException; 706 707 /** 708 * Set the priority of the calling thread, based on Linux priorities. See 709 * {@link #setThreadPriority(int, int)} for more information. 710 * 711 * @param priority A Linux priority level, from -20 for highest scheduling 712 * priority to 19 for lowest scheduling priority. 713 * 714 * @throws IllegalArgumentException Throws IllegalArgumentException if 715 * <var>tid</var> does not exist. 716 * @throws SecurityException Throws SecurityException if your process does 717 * not have permission to modify the given thread, or to use the given 718 * priority. 719 * 720 * @see #setThreadPriority(int, int) 721 */ 722 public static final native void setThreadPriority(int priority) 723 throws IllegalArgumentException, SecurityException; 724 725 /** 726 * Return the current priority of a thread, based on Linux priorities. 727 * 728 * @param tid The identifier of the thread/process to change. 729 * 730 * @return Returns the current priority, as a Linux priority level, 731 * from -20 for highest scheduling priority to 19 for lowest scheduling 732 * priority. 733 * 734 * @throws IllegalArgumentException Throws IllegalArgumentException if 735 * <var>tid</var> does not exist. 736 */ 737 public static final native int getThreadPriority(int tid) 738 throws IllegalArgumentException; 739 740 /** 741 * Set the scheduling policy and priority of a thread, based on Linux. 742 * 743 * @param tid The identifier of the thread/process to change. 744 * @param policy A Linux scheduling policy such as SCHED_OTHER etc. 745 * @param priority A Linux priority level in a range appropriate for the given policy. 746 * 747 * @throws IllegalArgumentException Throws IllegalArgumentException if 748 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy. 749 * @throws SecurityException Throws SecurityException if your process does 750 * not have permission to modify the given thread, or to use the given 751 * scheduling policy or priority. 752 * 753 * {@hide} 754 */ 755 public static final native void setThreadScheduler(int tid, int policy, int priority) 756 throws IllegalArgumentException; 757 758 /** 759 * Determine whether the current environment supports multiple processes. 760 * 761 * @return Returns true if the system can run in multiple processes, else 762 * false if everything is running in a single process. 763 * 764 * @deprecated This method always returns true. Do not use. 765 */ 766 @Deprecated 767 public static final boolean supportsProcesses() { 768 return true; 769 } 770 771 /** 772 * Set the out-of-memory badness adjustment for a process. 773 * 774 * @param pid The process identifier to set. 775 * @param amt Adjustment value -- linux allows -16 to +15. 776 * 777 * @return Returns true if the underlying system supports this 778 * feature, else false. 779 * 780 * {@hide} 781 */ 782 public static final native boolean setOomAdj(int pid, int amt); 783 784 /** 785 * Change this process's argv[0] parameter. This can be useful to show 786 * more descriptive information in things like the 'ps' command. 787 * 788 * @param text The new name of this process. 789 * 790 * {@hide} 791 */ 792 public static final native void setArgV0(String text); 793 794 /** 795 * Kill the process with the given PID. 796 * Note that, though this API allows us to request to 797 * kill any process based on its PID, the kernel will 798 * still impose standard restrictions on which PIDs you 799 * are actually able to kill. Typically this means only 800 * the process running the caller's packages/application 801 * and any additional processes created by that app; packages 802 * sharing a common UID will also be able to kill each 803 * other's processes. 804 */ 805 public static final void killProcess(int pid) { 806 sendSignal(pid, SIGNAL_KILL); 807 } 808 809 /** @hide */ 810 public static final native int setUid(int uid); 811 812 /** @hide */ 813 public static final native int setGid(int uid); 814 815 /** 816 * Send a signal to the given process. 817 * 818 * @param pid The pid of the target process. 819 * @param signal The signal to send. 820 */ 821 public static final native void sendSignal(int pid, int signal); 822 823 /** 824 * @hide 825 * Private impl for avoiding a log message... DO NOT USE without doing 826 * your own log, or the Android Illuminati will find you some night and 827 * beat you up. 828 */ 829 public static final void killProcessQuiet(int pid) { 830 sendSignalQuiet(pid, SIGNAL_KILL); 831 } 832 833 /** 834 * @hide 835 * Private impl for avoiding a log message... DO NOT USE without doing 836 * your own log, or the Android Illuminati will find you some night and 837 * beat you up. 838 */ 839 public static final native void sendSignalQuiet(int pid, int signal); 840 841 /** @hide */ 842 public static final native long getFreeMemory(); 843 844 /** @hide */ 845 public static final native void readProcLines(String path, 846 String[] reqFields, long[] outSizes); 847 848 /** @hide */ 849 public static final native int[] getPids(String path, int[] lastArray); 850 851 /** @hide */ 852 public static final int PROC_TERM_MASK = 0xff; 853 /** @hide */ 854 public static final int PROC_ZERO_TERM = 0; 855 /** @hide */ 856 public static final int PROC_SPACE_TERM = (int)' '; 857 /** @hide */ 858 public static final int PROC_TAB_TERM = (int)'\t'; 859 /** @hide */ 860 public static final int PROC_COMBINE = 0x100; 861 /** @hide */ 862 public static final int PROC_PARENS = 0x200; 863 /** @hide */ 864 public static final int PROC_OUT_STRING = 0x1000; 865 /** @hide */ 866 public static final int PROC_OUT_LONG = 0x2000; 867 /** @hide */ 868 public static final int PROC_OUT_FLOAT = 0x4000; 869 870 /** @hide */ 871 public static final native boolean readProcFile(String file, int[] format, 872 String[] outStrings, long[] outLongs, float[] outFloats); 873 874 /** @hide */ 875 public static final native boolean parseProcLine(byte[] buffer, int startIndex, 876 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats); 877 878 /** 879 * Gets the total Pss value for a given process, in bytes. 880 * 881 * @param pid the process to the Pss for 882 * @return the total Pss value for the given process in bytes, 883 * or -1 if the value cannot be determined 884 * @hide 885 */ 886 public static final native long getPss(int pid); 887 888 /** 889 * Specifies the outcome of having started a process. 890 * @hide 891 */ 892 public static final class ProcessStartResult { 893 /** 894 * The PID of the newly started process. 895 * Always >= 0. (If the start failed, an exception will have been thrown instead.) 896 */ 897 public int pid; 898 899 /** 900 * True if the process was started with a wrapper attached. 901 */ 902 public boolean usingWrapper; 903 } 904} 905