1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 package java.lang; 28 29 import dalvik.annotation.optimization.FastNative; 30 import java.io.*; 31 import java.util.StringTokenizer; 32 33 import dalvik.system.BlockGuard; 34 import sun.reflect.CallerSensitive; 35 import java.lang.ref.FinalizerReference; 36 import java.util.ArrayList; 37 import java.util.List; 38 import dalvik.system.DelegateLastClassLoader; 39 import dalvik.system.PathClassLoader; 40 import dalvik.system.VMDebug; 41 import dalvik.system.VMRuntime; 42 import sun.reflect.Reflection; 43 44 import libcore.io.IoUtils; 45 import libcore.io.Libcore; 46 import libcore.util.EmptyArray; 47 import static android.system.OsConstants._SC_NPROCESSORS_CONF; 48 49 /** 50 * Every Java application has a single instance of class 51 * <code>Runtime</code> that allows the application to interface with 52 * the environment in which the application is running. The current 53 * runtime can be obtained from the <code>getRuntime</code> method. 54 * <p> 55 * An application cannot create its own instance of this class. 56 * 57 * @author unascribed 58 * @see java.lang.Runtime#getRuntime() 59 * @since JDK1.0 60 */ 61 62 public class Runtime { 63 private static Runtime currentRuntime = new Runtime(); 64 65 /** 66 * Holds the list of threads to run when the VM terminates 67 */ 68 private List<Thread> shutdownHooks = new ArrayList<Thread>(); 69 70 /** 71 * Reflects whether finalization should be run for all objects 72 * when the VM terminates. 73 */ 74 private static boolean finalizeOnExit; 75 76 /** 77 * Reflects whether we are already shutting down the VM. 78 */ 79 private boolean shuttingDown; 80 nativeExit(int code)81 private static native void nativeExit(int code); 82 83 /** 84 * Returns the runtime object associated with the current Java application. 85 * Most of the methods of class <code>Runtime</code> are instance 86 * methods and must be invoked with respect to the current runtime object. 87 * 88 * @return the <code>Runtime</code> object associated with the current 89 * Java application. 90 */ getRuntime()91 public static Runtime getRuntime() { 92 return currentRuntime; 93 } 94 95 /** Don't let anyone else instantiate this class */ Runtime()96 private Runtime() {} 97 98 /** 99 * Terminates the currently running Java virtual machine by initiating its 100 * shutdown sequence. This method never returns normally. The argument 101 * serves as a status code; by convention, a nonzero status code indicates 102 * abnormal termination. 103 * 104 * <p> The virtual machine's shutdown sequence consists of two phases. In 105 * the first phase all registered {@link #addShutdownHook shutdown hooks}, 106 * if any, are started in some unspecified order and allowed to run 107 * concurrently until they finish. In the second phase all uninvoked 108 * finalizers are run if {@link #runFinalizersOnExit finalization-on-exit} 109 * has been enabled. Once this is done the virtual machine {@link #halt 110 * halts}. 111 * 112 * <p> If this method is invoked after the virtual machine has begun its 113 * shutdown sequence then if shutdown hooks are being run this method will 114 * block indefinitely. If shutdown hooks have already been run and on-exit 115 * finalization has been enabled then this method halts the virtual machine 116 * with the given status code if the status is nonzero; otherwise, it 117 * blocks indefinitely. 118 * 119 * <p> The <tt>{@link System#exit(int) System.exit}</tt> method is the 120 * conventional and convenient means of invoking this method. <p> 121 * 122 * @param status 123 * Termination status. By convention, a nonzero status code 124 * indicates abnormal termination. 125 * 126 * @throws SecurityException 127 * If a security manager is present and its <tt>{@link 128 * SecurityManager#checkExit checkExit}</tt> method does not permit 129 * exiting with the specified status 130 * 131 * @see java.lang.SecurityException 132 * @see java.lang.SecurityManager#checkExit(int) 133 * @see #addShutdownHook 134 * @see #removeShutdownHook 135 * @see #runFinalizersOnExit 136 * @see #halt(int) 137 */ exit(int status)138 public void exit(int status) { 139 // Make sure we don't try this several times 140 synchronized(this) { 141 if (!shuttingDown) { 142 shuttingDown = true; 143 144 Thread[] hooks; 145 synchronized (shutdownHooks) { 146 // create a copy of the hooks 147 hooks = new Thread[shutdownHooks.size()]; 148 shutdownHooks.toArray(hooks); 149 } 150 151 // Start all shutdown hooks concurrently 152 for (Thread hook : hooks) { 153 hook.start(); 154 } 155 156 // Wait for all shutdown hooks to finish 157 for (Thread hook : hooks) { 158 try { 159 hook.join(); 160 } catch (InterruptedException ex) { 161 // Ignore, since we are at VM shutdown. 162 } 163 } 164 165 // Ensure finalization on exit, if requested 166 if (finalizeOnExit) { 167 runFinalization(); 168 } 169 170 // Get out of here finally... 171 nativeExit(status); 172 } 173 } 174 } 175 176 /** 177 * Registers a new virtual-machine shutdown hook. 178 * 179 * <p> The Java virtual machine <i>shuts down</i> in response to two kinds 180 * of events: 181 * 182 * <ul> 183 * 184 * <li> The program <i>exits</i> normally, when the last non-daemon 185 * thread exits or when the <tt>{@link #exit exit}</tt> (equivalently, 186 * {@link System#exit(int) System.exit}) method is invoked, or 187 * 188 * <li> The virtual machine is <i>terminated</i> in response to a 189 * user interrupt, such as typing <tt>^C</tt>, or a system-wide event, 190 * such as user logoff or system shutdown. 191 * 192 * </ul> 193 * 194 * <p> A <i>shutdown hook</i> is simply an initialized but unstarted 195 * thread. When the virtual machine begins its shutdown sequence it will 196 * start all registered shutdown hooks in some unspecified order and let 197 * them run concurrently. When all the hooks have finished it will then 198 * run all uninvoked finalizers if finalization-on-exit has been enabled. 199 * Finally, the virtual machine will halt. Note that daemon threads will 200 * continue to run during the shutdown sequence, as will non-daemon threads 201 * if shutdown was initiated by invoking the <tt>{@link #exit exit}</tt> 202 * method. 203 * 204 * <p> Once the shutdown sequence has begun it can be stopped only by 205 * invoking the <tt>{@link #halt halt}</tt> method, which forcibly 206 * terminates the virtual machine. 207 * 208 * <p> Once the shutdown sequence has begun it is impossible to register a 209 * new shutdown hook or de-register a previously-registered hook. 210 * Attempting either of these operations will cause an 211 * <tt>{@link IllegalStateException}</tt> to be thrown. 212 * 213 * <p> Shutdown hooks run at a delicate time in the life cycle of a virtual 214 * machine and should therefore be coded defensively. They should, in 215 * particular, be written to be thread-safe and to avoid deadlocks insofar 216 * as possible. They should also not rely blindly upon services that may 217 * have registered their own shutdown hooks and therefore may themselves in 218 * the process of shutting down. Attempts to use other thread-based 219 * services such as the AWT event-dispatch thread, for example, may lead to 220 * deadlocks. 221 * 222 * <p> Shutdown hooks should also finish their work quickly. When a 223 * program invokes <tt>{@link #exit exit}</tt> the expectation is 224 * that the virtual machine will promptly shut down and exit. When the 225 * virtual machine is terminated due to user logoff or system shutdown the 226 * underlying operating system may only allow a fixed amount of time in 227 * which to shut down and exit. It is therefore inadvisable to attempt any 228 * user interaction or to perform a long-running computation in a shutdown 229 * hook. 230 * 231 * <p> Uncaught exceptions are handled in shutdown hooks just as in any 232 * other thread, by invoking the <tt>{@link ThreadGroup#uncaughtException 233 * uncaughtException}</tt> method of the thread's <tt>{@link 234 * ThreadGroup}</tt> object. The default implementation of this method 235 * prints the exception's stack trace to <tt>{@link System#err}</tt> and 236 * terminates the thread; it does not cause the virtual machine to exit or 237 * halt. 238 * 239 * <p> In rare circumstances the virtual machine may <i>abort</i>, that is, 240 * stop running without shutting down cleanly. This occurs when the 241 * virtual machine is terminated externally, for example with the 242 * <tt>SIGKILL</tt> signal on Unix or the <tt>TerminateProcess</tt> call on 243 * Microsoft Windows. The virtual machine may also abort if a native 244 * method goes awry by, for example, corrupting internal data structures or 245 * attempting to access nonexistent memory. If the virtual machine aborts 246 * then no guarantee can be made about whether or not any shutdown hooks 247 * will be run. <p> 248 * 249 * @param hook 250 * An initialized but unstarted <tt>{@link Thread}</tt> object 251 * 252 * @throws IllegalArgumentException 253 * If the specified hook has already been registered, 254 * or if it can be determined that the hook is already running or 255 * has already been run 256 * 257 * @throws IllegalStateException 258 * If the virtual machine is already in the process 259 * of shutting down 260 * 261 * @throws SecurityException 262 * If a security manager is present and it denies 263 * <tt>{@link RuntimePermission}("shutdownHooks")</tt> 264 * 265 * @see #removeShutdownHook 266 * @see #halt(int) 267 * @see #exit(int) 268 * @since 1.3 269 */ addShutdownHook(Thread hook)270 public void addShutdownHook(Thread hook) { 271 // Sanity checks 272 if (hook == null) { 273 throw new NullPointerException("hook == null"); 274 } 275 276 if (shuttingDown) { 277 throw new IllegalStateException("VM already shutting down"); 278 } 279 280 if (hook.started) { 281 throw new IllegalArgumentException("Hook has already been started"); 282 } 283 284 synchronized (shutdownHooks) { 285 if (shutdownHooks.contains(hook)) { 286 throw new IllegalArgumentException("Hook already registered."); 287 } 288 289 shutdownHooks.add(hook); 290 } 291 } 292 293 /** 294 * De-registers a previously-registered virtual-machine shutdown hook. <p> 295 * 296 * @param hook the hook to remove 297 * @return <tt>true</tt> if the specified hook had previously been 298 * registered and was successfully de-registered, <tt>false</tt> 299 * otherwise. 300 * 301 * @throws IllegalStateException 302 * If the virtual machine is already in the process of shutting 303 * down 304 * 305 * @throws SecurityException 306 * If a security manager is present and it denies 307 * <tt>{@link RuntimePermission}("shutdownHooks")</tt> 308 * 309 * @see #addShutdownHook 310 * @see #exit(int) 311 * @since 1.3 312 */ removeShutdownHook(Thread hook)313 public boolean removeShutdownHook(Thread hook) { 314 // Sanity checks 315 if (hook == null) { 316 throw new NullPointerException("hook == null"); 317 } 318 319 if (shuttingDown) { 320 throw new IllegalStateException("VM already shutting down"); 321 } 322 323 synchronized (shutdownHooks) { 324 return shutdownHooks.remove(hook); 325 } 326 } 327 328 /** 329 * Forcibly terminates the currently running Java virtual machine. This 330 * method never returns normally. 331 * 332 * <p> This method should be used with extreme caution. Unlike the 333 * <tt>{@link #exit exit}</tt> method, this method does not cause shutdown 334 * hooks to be started and does not run uninvoked finalizers if 335 * finalization-on-exit has been enabled. If the shutdown sequence has 336 * already been initiated then this method does not wait for any running 337 * shutdown hooks or finalizers to finish their work. <p> 338 * 339 * @param status 340 * Termination status. By convention, a nonzero status code 341 * indicates abnormal termination. If the <tt>{@link Runtime#exit 342 * exit}</tt> (equivalently, <tt>{@link System#exit(int) 343 * System.exit}</tt>) method has already been invoked then this 344 * status code will override the status code passed to that method. 345 * 346 * @throws SecurityException 347 * If a security manager is present and its <tt>{@link 348 * SecurityManager#checkExit checkExit}</tt> method does not permit 349 * an exit with the specified status 350 * 351 * @see #exit 352 * @see #addShutdownHook 353 * @see #removeShutdownHook 354 * @since 1.3 355 */ halt(int status)356 public void halt(int status) { 357 nativeExit(status); 358 } 359 360 /** 361 * Enable or disable finalization on exit; doing so specifies that the 362 * finalizers of all objects that have finalizers that have not yet been 363 * automatically invoked are to be run before the Java runtime exits. 364 * By default, finalization on exit is disabled. 365 * 366 * <p>If there is a security manager, 367 * its <code>checkExit</code> method is first called 368 * with 0 as its argument to ensure the exit is allowed. 369 * This could result in a SecurityException. 370 * 371 * @param value true to enable finalization on exit, false to disable 372 * @deprecated This method is inherently unsafe. It may result in 373 * finalizers being called on live objects while other threads are 374 * concurrently manipulating those objects, resulting in erratic 375 * behavior or deadlock. 376 * 377 * @throws SecurityException 378 * if a security manager exists and its <code>checkExit</code> 379 * method doesn't allow the exit. 380 * 381 * @see java.lang.Runtime#exit(int) 382 * @see java.lang.Runtime#gc() 383 * @see java.lang.SecurityManager#checkExit(int) 384 * @since JDK1.1 385 */ 386 @Deprecated runFinalizersOnExit(boolean value)387 public static void runFinalizersOnExit(boolean value) { 388 finalizeOnExit = value; 389 } 390 391 /** 392 * Executes the specified string command in a separate process. 393 * 394 * <p>This is a convenience method. An invocation of the form 395 * <tt>exec(command)</tt> 396 * behaves in exactly the same way as the invocation 397 * <tt>{@link #exec(String, String[], File) exec}(command, null, null)</tt>. 398 * 399 * @param command a specified system command. 400 * 401 * @return A new {@link Process} object for managing the subprocess 402 * 403 * @throws SecurityException 404 * If a security manager exists and its 405 * {@link SecurityManager#checkExec checkExec} 406 * method doesn't allow creation of the subprocess 407 * 408 * @throws IOException 409 * If an I/O error occurs 410 * 411 * @throws NullPointerException 412 * If <code>command</code> is <code>null</code> 413 * 414 * @throws IllegalArgumentException 415 * If <code>command</code> is empty 416 * 417 * @see #exec(String[], String[], File) 418 * @see ProcessBuilder 419 */ exec(String command)420 public Process exec(String command) throws IOException { 421 return exec(command, null, null); 422 } 423 424 /** 425 * Executes the specified string command in a separate process with the 426 * specified environment. 427 * 428 * <p>This is a convenience method. An invocation of the form 429 * <tt>exec(command, envp)</tt> 430 * behaves in exactly the same way as the invocation 431 * <tt>{@link #exec(String, String[], File) exec}(command, envp, null)</tt>. 432 * 433 * @param command a specified system command. 434 * 435 * @param envp array of strings, each element of which 436 * has environment variable settings in the format 437 * <i>name</i>=<i>value</i>, or 438 * <tt>null</tt> if the subprocess should inherit 439 * the environment of the current process. 440 * 441 * @return A new {@link Process} object for managing the subprocess 442 * 443 * @throws SecurityException 444 * If a security manager exists and its 445 * {@link SecurityManager#checkExec checkExec} 446 * method doesn't allow creation of the subprocess 447 * 448 * @throws IOException 449 * If an I/O error occurs 450 * 451 * @throws NullPointerException 452 * If <code>command</code> is <code>null</code>, 453 * or one of the elements of <code>envp</code> is <code>null</code> 454 * 455 * @throws IllegalArgumentException 456 * If <code>command</code> is empty 457 * 458 * @see #exec(String[], String[], File) 459 * @see ProcessBuilder 460 */ exec(String command, String[] envp)461 public Process exec(String command, String[] envp) throws IOException { 462 return exec(command, envp, null); 463 } 464 465 /** 466 * Executes the specified string command in a separate process with the 467 * specified environment and working directory. 468 * 469 * <p>This is a convenience method. An invocation of the form 470 * <tt>exec(command, envp, dir)</tt> 471 * behaves in exactly the same way as the invocation 472 * <tt>{@link #exec(String[], String[], File) exec}(cmdarray, envp, dir)</tt>, 473 * where <code>cmdarray</code> is an array of all the tokens in 474 * <code>command</code>. 475 * 476 * <p>More precisely, the <code>command</code> string is broken 477 * into tokens using a {@link StringTokenizer} created by the call 478 * <code>new {@link StringTokenizer}(command)</code> with no 479 * further modification of the character categories. The tokens 480 * produced by the tokenizer are then placed in the new string 481 * array <code>cmdarray</code>, in the same order. 482 * 483 * @param command a specified system command. 484 * 485 * @param envp array of strings, each element of which 486 * has environment variable settings in the format 487 * <i>name</i>=<i>value</i>, or 488 * <tt>null</tt> if the subprocess should inherit 489 * the environment of the current process. 490 * 491 * @param dir the working directory of the subprocess, or 492 * <tt>null</tt> if the subprocess should inherit 493 * the working directory of the current process. 494 * 495 * @return A new {@link Process} object for managing the subprocess 496 * 497 * @throws SecurityException 498 * If a security manager exists and its 499 * {@link SecurityManager#checkExec checkExec} 500 * method doesn't allow creation of the subprocess 501 * 502 * @throws IOException 503 * If an I/O error occurs 504 * 505 * @throws NullPointerException 506 * If <code>command</code> is <code>null</code>, 507 * or one of the elements of <code>envp</code> is <code>null</code> 508 * 509 * @throws IllegalArgumentException 510 * If <code>command</code> is empty 511 * 512 * @see ProcessBuilder 513 * @since 1.3 514 */ exec(String command, String[] envp, File dir)515 public Process exec(String command, String[] envp, File dir) 516 throws IOException { 517 if (command.length() == 0) 518 throw new IllegalArgumentException("Empty command"); 519 520 StringTokenizer st = new StringTokenizer(command); 521 String[] cmdarray = new String[st.countTokens()]; 522 for (int i = 0; st.hasMoreTokens(); i++) 523 cmdarray[i] = st.nextToken(); 524 return exec(cmdarray, envp, dir); 525 } 526 527 /** 528 * Executes the specified command and arguments in a separate process. 529 * 530 * <p>This is a convenience method. An invocation of the form 531 * <tt>exec(cmdarray)</tt> 532 * behaves in exactly the same way as the invocation 533 * <tt>{@link #exec(String[], String[], File) exec}(cmdarray, null, null)</tt>. 534 * 535 * @param cmdarray array containing the command to call and 536 * its arguments. 537 * 538 * @return A new {@link Process} object for managing the subprocess 539 * 540 * @throws SecurityException 541 * If a security manager exists and its 542 * {@link SecurityManager#checkExec checkExec} 543 * method doesn't allow creation of the subprocess 544 * 545 * @throws IOException 546 * If an I/O error occurs 547 * 548 * @throws NullPointerException 549 * If <code>cmdarray</code> is <code>null</code>, 550 * or one of the elements of <code>cmdarray</code> is <code>null</code> 551 * 552 * @throws IndexOutOfBoundsException 553 * If <code>cmdarray</code> is an empty array 554 * (has length <code>0</code>) 555 * 556 * @see ProcessBuilder 557 */ exec(String cmdarray[])558 public Process exec(String cmdarray[]) throws IOException { 559 return exec(cmdarray, null, null); 560 } 561 562 /** 563 * Executes the specified command and arguments in a separate process 564 * with the specified environment. 565 * 566 * <p>This is a convenience method. An invocation of the form 567 * <tt>exec(cmdarray, envp)</tt> 568 * behaves in exactly the same way as the invocation 569 * <tt>{@link #exec(String[], String[], File) exec}(cmdarray, envp, null)</tt>. 570 * 571 * @param cmdarray array containing the command to call and 572 * its arguments. 573 * 574 * @param envp array of strings, each element of which 575 * has environment variable settings in the format 576 * <i>name</i>=<i>value</i>, or 577 * <tt>null</tt> if the subprocess should inherit 578 * the environment of the current process. 579 * 580 * @return A new {@link Process} object for managing the subprocess 581 * 582 * @throws SecurityException 583 * If a security manager exists and its 584 * {@link SecurityManager#checkExec checkExec} 585 * method doesn't allow creation of the subprocess 586 * 587 * @throws IOException 588 * If an I/O error occurs 589 * 590 * @throws NullPointerException 591 * If <code>cmdarray</code> is <code>null</code>, 592 * or one of the elements of <code>cmdarray</code> is <code>null</code>, 593 * or one of the elements of <code>envp</code> is <code>null</code> 594 * 595 * @throws IndexOutOfBoundsException 596 * If <code>cmdarray</code> is an empty array 597 * (has length <code>0</code>) 598 * 599 * @see ProcessBuilder 600 */ exec(String[] cmdarray, String[] envp)601 public Process exec(String[] cmdarray, String[] envp) throws IOException { 602 return exec(cmdarray, envp, null); 603 } 604 605 606 /** 607 * Executes the specified command and arguments in a separate process with 608 * the specified environment and working directory. 609 * 610 * <p>Given an array of strings <code>cmdarray</code>, representing the 611 * tokens of a command line, and an array of strings <code>envp</code>, 612 * representing "environment" variable settings, this method creates 613 * a new process in which to execute the specified command. 614 * 615 * <p>This method checks that <code>cmdarray</code> is a valid operating 616 * system command. Which commands are valid is system-dependent, 617 * but at the very least the command must be a non-empty list of 618 * non-null strings. 619 * 620 * <p>If <tt>envp</tt> is <tt>null</tt>, the subprocess inherits the 621 * environment settings of the current process. 622 * 623 * <p>A minimal set of system dependent environment variables may 624 * be required to start a process on some operating systems. 625 * As a result, the subprocess may inherit additional environment variable 626 * settings beyond those in the specified environment. 627 * 628 * <p>{@link ProcessBuilder#start()} is now the preferred way to 629 * start a process with a modified environment. 630 * 631 * <p>The working directory of the new subprocess is specified by <tt>dir</tt>. 632 * If <tt>dir</tt> is <tt>null</tt>, the subprocess inherits the 633 * current working directory of the current process. 634 * 635 * <p>If a security manager exists, its 636 * {@link SecurityManager#checkExec checkExec} 637 * method is invoked with the first component of the array 638 * <code>cmdarray</code> as its argument. This may result in a 639 * {@link SecurityException} being thrown. 640 * 641 * <p>Starting an operating system process is highly system-dependent. 642 * Among the many things that can go wrong are: 643 * <ul> 644 * <li>The operating system program file was not found. 645 * <li>Access to the program file was denied. 646 * <li>The working directory does not exist. 647 * </ul> 648 * 649 * <p>In such cases an exception will be thrown. The exact nature 650 * of the exception is system-dependent, but it will always be a 651 * subclass of {@link IOException}. 652 * 653 * 654 * @param cmdarray array containing the command to call and 655 * its arguments. 656 * 657 * @param envp array of strings, each element of which 658 * has environment variable settings in the format 659 * <i>name</i>=<i>value</i>, or 660 * <tt>null</tt> if the subprocess should inherit 661 * the environment of the current process. 662 * 663 * @param dir the working directory of the subprocess, or 664 * <tt>null</tt> if the subprocess should inherit 665 * the working directory of the current process. 666 * 667 * @return A new {@link Process} object for managing the subprocess 668 * 669 * @throws SecurityException 670 * If a security manager exists and its 671 * {@link SecurityManager#checkExec checkExec} 672 * method doesn't allow creation of the subprocess 673 * 674 * @throws IOException 675 * If an I/O error occurs 676 * 677 * @throws NullPointerException 678 * If <code>cmdarray</code> is <code>null</code>, 679 * or one of the elements of <code>cmdarray</code> is <code>null</code>, 680 * or one of the elements of <code>envp</code> is <code>null</code> 681 * 682 * @throws IndexOutOfBoundsException 683 * If <code>cmdarray</code> is an empty array 684 * (has length <code>0</code>) 685 * 686 * @see ProcessBuilder 687 * @since 1.3 688 */ exec(String[] cmdarray, String[] envp, File dir)689 public Process exec(String[] cmdarray, String[] envp, File dir) 690 throws IOException { 691 return new ProcessBuilder(cmdarray) 692 .environment(envp) 693 .directory(dir) 694 .start(); 695 } 696 697 /** 698 * Returns the number of processors available to the Java virtual machine. 699 * 700 * <p> This value may change during a particular invocation of the virtual 701 * machine. Applications that are sensitive to the number of available 702 * processors should therefore occasionally poll this property and adjust 703 * their resource usage appropriately. </p> 704 * 705 * @return the maximum number of processors available to the virtual 706 * machine; never smaller than one 707 * @since 1.4 708 */ availableProcessors()709 public int availableProcessors() { 710 return (int) Libcore.os.sysconf(_SC_NPROCESSORS_CONF); 711 } 712 713 /** 714 * Returns the amount of free memory in the Java Virtual Machine. 715 * Calling the 716 * <code>gc</code> method may result in increasing the value returned 717 * by <code>freeMemory.</code> 718 * 719 * @return an approximation to the total amount of memory currently 720 * available for future allocated objects, measured in bytes. 721 */ 722 @FastNative freeMemory()723 public native long freeMemory(); 724 725 /** 726 * Returns the total amount of memory in the Java virtual machine. 727 * The value returned by this method may vary over time, depending on 728 * the host environment. 729 * <p> 730 * Note that the amount of memory required to hold an object of any 731 * given type may be implementation-dependent. 732 * 733 * @return the total amount of memory currently available for current 734 * and future objects, measured in bytes. 735 */ 736 @FastNative totalMemory()737 public native long totalMemory(); 738 739 /** 740 * Returns the maximum amount of memory that the Java virtual machine will 741 * attempt to use. If there is no inherent limit then the value {@link 742 * java.lang.Long#MAX_VALUE} will be returned. 743 * 744 * @return the maximum amount of memory that the virtual machine will 745 * attempt to use, measured in bytes 746 * @since 1.4 747 */ 748 @FastNative maxMemory()749 public native long maxMemory(); 750 751 /** 752 * Runs the garbage collector. 753 * Calling this method suggests that the Java virtual machine expend 754 * effort toward recycling unused objects in order to make the memory 755 * they currently occupy available for quick reuse. When control 756 * returns from the method call, the virtual machine has made 757 * its best effort to recycle all discarded objects. 758 * <p> 759 * The name <code>gc</code> stands for "garbage 760 * collector". The virtual machine performs this recycling 761 * process automatically as needed, in a separate thread, even if the 762 * <code>gc</code> method is not invoked explicitly. 763 * <p> 764 * The method {@link System#gc()} is the conventional and convenient 765 * means of invoking this method. 766 */ 767 // Android-changed: Added BlockGuard check to gc() 768 // public native void gc(); gc()769 public void gc() { 770 BlockGuard.getThreadPolicy().onExplicitGc(); 771 nativeGc(); 772 } 773 nativeGc()774 private native void nativeGc(); 775 776 /* Wormhole for calling java.lang.ref.Finalizer.runFinalization */ runFinalization0()777 private static native void runFinalization0(); 778 779 /** 780 * Runs the finalization methods of any objects pending finalization. 781 * Calling this method suggests that the Java virtual machine expend 782 * effort toward running the <code>finalize</code> methods of objects 783 * that have been found to be discarded but whose <code>finalize</code> 784 * methods have not yet been run. When control returns from the 785 * method call, the virtual machine has made a best effort to 786 * complete all outstanding finalizations. 787 * <p> 788 * The virtual machine performs the finalization process 789 * automatically as needed, in a separate thread, if the 790 * <code>runFinalization</code> method is not invoked explicitly. 791 * <p> 792 * The method {@link System#runFinalization()} is the conventional 793 * and convenient means of invoking this method. 794 * 795 * @see java.lang.Object#finalize() 796 */ runFinalization()797 public void runFinalization() { 798 VMRuntime.runFinalization(0); 799 } 800 801 /** 802 * Enables/Disables tracing of instructions. 803 * If the <code>boolean</code> argument is <code>true</code>, this 804 * method suggests that the Java virtual machine emit debugging 805 * information for each instruction in the virtual machine as it 806 * is executed. The format of this information, and the file or other 807 * output stream to which it is emitted, depends on the host environment. 808 * The virtual machine may ignore this request if it does not support 809 * this feature. The destination of the trace output is system 810 * dependent. 811 * <p> 812 * If the <code>boolean</code> argument is <code>false</code>, this 813 * method causes the virtual machine to stop performing the 814 * detailed instruction trace it is performing. 815 * 816 * @param on <code>true</code> to enable instruction tracing; 817 * <code>false</code> to disable this feature. 818 */ traceInstructions(boolean on)819 public void traceInstructions(boolean on) { 820 } 821 822 /** 823 * Enables/Disables tracing of method calls. 824 * If the <code>boolean</code> argument is <code>true</code>, this 825 * method suggests that the Java virtual machine emit debugging 826 * information for each method in the virtual machine as it is 827 * called. The format of this information, and the file or other output 828 * stream to which it is emitted, depends on the host environment. The 829 * virtual machine may ignore this request if it does not support 830 * this feature. 831 * <p> 832 * Calling this method with argument false suggests that the 833 * virtual machine cease emitting per-call debugging information. 834 * <p> 835 * Calling this method on Android Lollipop or later (API level >= 21) 836 * with {@code true} argument will cause it to throw an 837 * {@code UnsupportedOperationException}. 838 * 839 * @param on <code>true</code> to enable instruction tracing; 840 * <code>false</code> to disable this feature. 841 */ traceMethodCalls(boolean on)842 public void traceMethodCalls(boolean on) { 843 if (on) { 844 throw new UnsupportedOperationException(); 845 } 846 } 847 848 /** 849 * Loads the native library specified by the filename argument. The filename 850 * argument must be an absolute path name. 851 * (for example 852 * <code>Runtime.getRuntime().load("/home/avh/lib/libX11.so");</code>). 853 * 854 * If the filename argument, when stripped of any platform-specific library 855 * prefix, path, and file extension, indicates a library whose name is, 856 * for example, L, and a native library called L is statically linked 857 * with the VM, then the JNI_OnLoad_L function exported by the library 858 * is invoked rather than attempting to load a dynamic library. 859 * A filename matching the argument does not have to exist in the file 860 * system. See the JNI Specification for more details. 861 * 862 * Otherwise, the filename argument is mapped to a native library image in 863 * an implementation-dependent manner. 864 * <p> 865 * First, if there is a security manager, its <code>checkLink</code> 866 * method is called with the <code>filename</code> as its argument. 867 * This may result in a security exception. 868 * <p> 869 * This is similar to the method {@link #loadLibrary(String)}, but it 870 * accepts a general file name as an argument rather than just a library 871 * name, allowing any file of native code to be loaded. 872 * <p> 873 * The method {@link System#load(String)} is the conventional and 874 * convenient means of invoking this method. 875 * 876 * @param filename the file to load. 877 * @exception SecurityException if a security manager exists and its 878 * <code>checkLink</code> method doesn't allow 879 * loading of the specified dynamic library 880 * @exception UnsatisfiedLinkError if either the filename is not an 881 * absolute path name, the native library is not statically 882 * linked with the VM, or the library cannot be mapped to 883 * a native library image by the host system. 884 * @exception NullPointerException if <code>filename</code> is 885 * <code>null</code> 886 * @see java.lang.Runtime#getRuntime() 887 * @see java.lang.SecurityException 888 * @see java.lang.SecurityManager#checkLink(java.lang.String) 889 */ 890 @CallerSensitive load(String filename)891 public void load(String filename) { 892 load0(Reflection.getCallerClass(), filename); 893 } 894 895 /** Check target sdk, if it's higher than N, we throw an UnsupportedOperationException */ checkTargetSdkVersionForLoad(String methodName)896 private void checkTargetSdkVersionForLoad(String methodName) { 897 final int targetSdkVersion = VMRuntime.getRuntime().getTargetSdkVersion(); 898 if (targetSdkVersion > 24) { 899 throw new UnsupportedOperationException(methodName + " is not supported on SDK " + 900 targetSdkVersion); 901 } 902 } 903 904 // Fixes b/25859957 regression. Depending on private methods is bad, mkay. load(String absolutePath, ClassLoader loader)905 void load(String absolutePath, ClassLoader loader) { 906 checkTargetSdkVersionForLoad("java.lang.Runtime#load(String, ClassLoader)"); 907 908 java.lang.System.logE("java.lang.Runtime#load(String, ClassLoader)" + 909 " is private and will be removed in a future Android release"); 910 if (absolutePath == null) { 911 throw new NullPointerException("absolutePath == null"); 912 } 913 String error = nativeLoad(absolutePath, loader); 914 if (error != null) { 915 throw new UnsatisfiedLinkError(error); 916 } 917 } 918 load0(Class<?> fromClass, String filename)919 synchronized void load0(Class<?> fromClass, String filename) { 920 if (!(new File(filename).isAbsolute())) { 921 throw new UnsatisfiedLinkError( 922 "Expecting an absolute path of the library: " + filename); 923 } 924 if (filename == null) { 925 throw new NullPointerException("filename == null"); 926 } 927 String error = nativeLoad(filename, fromClass.getClassLoader()); 928 if (error != null) { 929 throw new UnsatisfiedLinkError(error); 930 } 931 } 932 933 /** 934 * Loads the native library specified by the <code>libname</code> 935 * argument. The <code>libname</code> argument must not contain any platform 936 * specific prefix, file extension or path. If a native library 937 * called <code>libname</code> is statically linked with the VM, then the 938 * JNI_OnLoad_<code>libname</code> function exported by the library is invoked. 939 * See the JNI Specification for more details. 940 * 941 * Otherwise, the libname argument is loaded from a system library 942 * location and mapped to a native library image in an implementation- 943 * dependent manner. 944 * <p> 945 * First, if there is a security manager, its <code>checkLink</code> 946 * method is called with the <code>libname</code> as its argument. 947 * This may result in a security exception. 948 * <p> 949 * The method {@link System#loadLibrary(String)} is the conventional 950 * and convenient means of invoking this method. If native 951 * methods are to be used in the implementation of a class, a standard 952 * strategy is to put the native code in a library file (call it 953 * <code>LibFile</code>) and then to put a static initializer: 954 * <blockquote><pre> 955 * static { System.loadLibrary("LibFile"); } 956 * </pre></blockquote> 957 * within the class declaration. When the class is loaded and 958 * initialized, the necessary native code implementation for the native 959 * methods will then be loaded as well. 960 * <p> 961 * If this method is called more than once with the same library 962 * name, the second and subsequent calls are ignored. 963 * 964 * @param libname the name of the library. 965 * @exception SecurityException if a security manager exists and its 966 * <code>checkLink</code> method doesn't allow 967 * loading of the specified dynamic library 968 * @exception UnsatisfiedLinkError if either the libname argument 969 * contains a file path, the native library is not statically 970 * linked with the VM, or the library cannot be mapped to a 971 * native library image by the host system. 972 * @exception NullPointerException if <code>libname</code> is 973 * <code>null</code> 974 * @see java.lang.SecurityException 975 * @see java.lang.SecurityManager#checkLink(java.lang.String) 976 */ 977 @CallerSensitive loadLibrary(String libname)978 public void loadLibrary(String libname) { 979 loadLibrary0(Reflection.getCallerClass(), libname); 980 } 981 982 // BEGIN Android-changed: Different implementation of loadLibrary0(Class, String). 983 /* 984 synchronized void loadLibrary0(Class<?> fromClass, String libname) { 985 SecurityManager security = System.getSecurityManager(); 986 if (security != null) { 987 security.checkLink(libname); 988 } 989 if (libname.indexOf((int)File.separatorChar) != -1) { 990 throw new UnsatisfiedLinkError( 991 "Directory separator should not appear in library name: " + libname); 992 } 993 ClassLoader.loadLibrary(fromClass, libname, false); 994 } 995 */ loadLibrary0(Class<?> fromClass, String libname)996 void loadLibrary0(Class<?> fromClass, String libname) { 997 ClassLoader classLoader = ClassLoader.getClassLoader(fromClass); 998 loadLibrary0(classLoader, fromClass, libname); 999 } 1000 1001 /** 1002 * Temporarily preserved for backward compatibility. Applications call this 1003 * method using reflection. 1004 * 1005 * **** THIS METHOD WILL BE REMOVED IN A FUTURE ANDROID VERSION **** 1006 * 1007 * http://b/26217329 1008 * 1009 * @hide 1010 */ loadLibrary(String libname, ClassLoader classLoader)1011 public void loadLibrary(String libname, ClassLoader classLoader) { 1012 checkTargetSdkVersionForLoad("java.lang.Runtime#loadLibrary(String, ClassLoader)"); 1013 java.lang.System.logE("java.lang.Runtime#loadLibrary(String, ClassLoader)" + 1014 " is private and will be removed in a future Android release"); 1015 // Pass null for callerClass, we don't know it at this point. Passing null preserved 1016 // the behavior when we used to not pass the class. 1017 loadLibrary0(classLoader, null, libname); 1018 } 1019 1020 // This overload exists for @UnsupportedAppUsage loadLibrary0(ClassLoader loader, String libname)1021 void loadLibrary0(ClassLoader loader, String libname) { 1022 // Pass null for callerClass, we don't know it at this point. Passing null preserved 1023 // the behavior when we used to not pass the class. 1024 loadLibrary0(loader, null, libname); 1025 } 1026 1027 /** 1028 * Loads the shared library {@code libname} in the context of {@code loader} and 1029 * {@code callerClass}. 1030 * 1031 * @param loader the class loader that initiated the loading. Used by the 1032 * underlying linker to determine linker namespace. A {@code null} 1033 * value represents the boot class loader. 1034 * @param fromClass the class that initiated the loading. Used when loader is 1035 * {@code null} and ignored in all other cases. When used, it 1036 * determines the linker namespace from the class's .dex location. 1037 * {@code null} indicates the default namespace for the boot 1038 * class loader. 1039 * @param libname the name of the library. 1040 */ loadLibrary0(ClassLoader loader, Class<?> callerClass, String libname)1041 private synchronized void loadLibrary0(ClassLoader loader, Class<?> callerClass, String libname) { 1042 if (libname.indexOf((int)File.separatorChar) != -1) { 1043 throw new UnsatisfiedLinkError( 1044 "Directory separator should not appear in library name: " + libname); 1045 } 1046 String libraryName = libname; 1047 // Android-note: BootClassLoader doesn't implement findLibrary(). http://b/111850480 1048 // Android's class.getClassLoader() can return BootClassLoader where the RI would 1049 // have returned null; therefore we treat BootClassLoader the same as null here. 1050 if (loader != null && !(loader instanceof BootClassLoader)) { 1051 String filename = loader.findLibrary(libraryName); 1052 if (filename == null && 1053 (loader.getClass() == PathClassLoader.class || 1054 loader.getClass() == DelegateLastClassLoader.class)) { 1055 // Don't give up even if we failed to find the library in the native lib paths. 1056 // The underlying dynamic linker might be able to find the lib in one of the linker 1057 // namespaces associated with the current linker namespace. In order to give the 1058 // dynamic linker a chance, proceed to load the library with its soname, which 1059 // is the fileName. 1060 // Note that we do this only for PathClassLoader and DelegateLastClassLoader to 1061 // minimize the scope of this behavioral change as much as possible, which might 1062 // cause problem like b/143649498. These two class loaders are the only 1063 // platform-provided class loaders that can load apps. See the classLoader attribute 1064 // of the application tag in app manifest. 1065 filename = System.mapLibraryName(libraryName); 1066 } 1067 if (filename == null) { 1068 // It's not necessarily true that the ClassLoader used 1069 // System.mapLibraryName, but the default setup does, and it's 1070 // misleading to say we didn't find "libMyLibrary.so" when we 1071 // actually searched for "liblibMyLibrary.so.so". 1072 throw new UnsatisfiedLinkError(loader + " couldn't find \"" + 1073 System.mapLibraryName(libraryName) + "\""); 1074 } 1075 String error = nativeLoad(filename, loader); 1076 if (error != null) { 1077 throw new UnsatisfiedLinkError(error); 1078 } 1079 return; 1080 } 1081 1082 // We know some apps use mLibPaths directly, potentially assuming it's not null. 1083 // Initialize it here to make sure apps see a non-null value. 1084 getLibPaths(); 1085 String filename = System.mapLibraryName(libraryName); 1086 String error = nativeLoad(filename, loader, callerClass); 1087 if (error != null) { 1088 throw new UnsatisfiedLinkError(error); 1089 } 1090 } 1091 1092 private volatile String[] mLibPaths = null; 1093 getLibPaths()1094 private String[] getLibPaths() { 1095 if (mLibPaths == null) { 1096 synchronized(this) { 1097 if (mLibPaths == null) { 1098 mLibPaths = initLibPaths(); 1099 } 1100 } 1101 } 1102 return mLibPaths; 1103 } 1104 initLibPaths()1105 private static String[] initLibPaths() { 1106 String javaLibraryPath = System.getProperty("java.library.path"); 1107 if (javaLibraryPath == null) { 1108 return EmptyArray.STRING; 1109 } 1110 String[] paths = javaLibraryPath.split(":"); 1111 // Add a '/' to the end of each directory so we don't have to do it every time. 1112 for (int i = 0; i < paths.length; ++i) { 1113 if (!paths[i].endsWith("/")) { 1114 paths[i] += "/"; 1115 } 1116 } 1117 return paths; 1118 } 1119 nativeLoad(String filename, ClassLoader loader)1120 private static String nativeLoad(String filename, ClassLoader loader) { 1121 return nativeLoad(filename, loader, null); 1122 } 1123 nativeLoad(String filename, ClassLoader loader, Class<?> caller)1124 private static native String nativeLoad(String filename, ClassLoader loader, Class<?> caller); 1125 // END Android-changed: Different implementation of loadLibrary0(Class, String). 1126 1127 /** 1128 * Creates a localized version of an input stream. This method takes 1129 * an <code>InputStream</code> and returns an <code>InputStream</code> 1130 * equivalent to the argument in all respects except that it is 1131 * localized: as characters in the local character set are read from 1132 * the stream, they are automatically converted from the local 1133 * character set to Unicode. 1134 * <p> 1135 * If the argument is already a localized stream, it may be returned 1136 * as the result. 1137 * 1138 * @param in InputStream to localize 1139 * @return a localized input stream 1140 * @see java.io.InputStream 1141 * @see java.io.BufferedReader#BufferedReader(java.io.Reader) 1142 * @see java.io.InputStreamReader#InputStreamReader(java.io.InputStream) 1143 * @deprecated As of JDK 1.1, the preferred way to translate a byte 1144 * stream in the local encoding into a character stream in Unicode is via 1145 * the <code>InputStreamReader</code> and <code>BufferedReader</code> 1146 * classes. 1147 */ 1148 @Deprecated getLocalizedInputStream(InputStream in)1149 public InputStream getLocalizedInputStream(InputStream in) { 1150 return in; 1151 } 1152 1153 /** 1154 * Creates a localized version of an output stream. This method 1155 * takes an <code>OutputStream</code> and returns an 1156 * <code>OutputStream</code> equivalent to the argument in all respects 1157 * except that it is localized: as Unicode characters are written to 1158 * the stream, they are automatically converted to the local 1159 * character set. 1160 * <p> 1161 * If the argument is already a localized stream, it may be returned 1162 * as the result. 1163 * 1164 * @deprecated As of JDK 1.1, the preferred way to translate a 1165 * Unicode character stream into a byte stream in the local encoding is via 1166 * the <code>OutputStreamWriter</code>, <code>BufferedWriter</code>, and 1167 * <code>PrintWriter</code> classes. 1168 * 1169 * @param out OutputStream to localize 1170 * @return a localized output stream 1171 * @see java.io.OutputStream 1172 * @see java.io.BufferedWriter#BufferedWriter(java.io.Writer) 1173 * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream) 1174 * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream) 1175 */ 1176 @Deprecated getLocalizedOutputStream(OutputStream out)1177 public OutputStream getLocalizedOutputStream(OutputStream out) { 1178 return out; 1179 } 1180 1181 } 1182