diff options
Diffstat (limited to 'core/java/android/os/Process.java')
-rw-r--r-- | core/java/android/os/Process.java | 145 |
1 files changed, 52 insertions, 93 deletions
diff --git a/core/java/android/os/Process.java b/core/java/android/os/Process.java index 673b187..5b1f563 100644 --- a/core/java/android/os/Process.java +++ b/core/java/android/os/Process.java @@ -266,84 +266,29 @@ public class Process { * @param uid The user-id under which the process will run. * @param gid The group-id under which the process will run. * @param gids Additional group-ids associated with the process. - * @param enableDebugger True if debugging should be enabled for this process. + * @param debugFlags Additional flags. + * @param targetSdkVersion The target SDK version for the app. * @param zygoteArgs Additional arguments to supply to the zygote process. * - * @return int If > 0 the pid of the new process; if 0 the process is - * being emulated by a thread + * @return An object that describes the result of the attempt to start the process. * @throws RuntimeException on fatal start failure * * {@hide} */ - public static final int start(final String processClass, + public static final ProcessStartResult start(final String processClass, final String niceName, int uid, int gid, int[] gids, - int debugFlags, - String[] zygoteArgs) - { - if (supportsProcesses()) { - try { - return startViaZygote(processClass, niceName, uid, gid, gids, - debugFlags, zygoteArgs); - } catch (ZygoteStartFailedEx ex) { - Log.e(LOG_TAG, - "Starting VM process through Zygote failed"); - throw new RuntimeException( - "Starting VM process through Zygote failed", ex); - } - } else { - // Running in single-process mode - - Runnable runnable = new Runnable() { - public void run() { - Process.invokeStaticMain(processClass); - } - }; - - // Thread constructors must not be called with null names (see spec). - if (niceName != null) { - new Thread(runnable, niceName).start(); - } else { - new Thread(runnable).start(); - } - - return 0; - } - } - - /** - * Start a new process. Don't supply a custom nice name. - * {@hide} - */ - public static final int start(String processClass, int uid, int gid, - int[] gids, int debugFlags, String[] zygoteArgs) { - return start(processClass, "", uid, gid, gids, - debugFlags, zygoteArgs); - } - - private static void invokeStaticMain(String className) { - Class cl; - Object args[] = new Object[1]; - - args[0] = new String[0]; //this is argv - + int debugFlags, int targetSdkVersion, + String[] zygoteArgs) { try { - cl = Class.forName(className); - cl.getMethod("main", new Class[] { String[].class }) - .invoke(null, args); - } catch (Exception ex) { - // can be: ClassNotFoundException, - // NoSuchMethodException, SecurityException, - // IllegalAccessException, IllegalArgumentException - // InvocationTargetException - // or uncaught exception from main() - - Log.e(LOG_TAG, "Exception invoking static main on " - + className, ex); - - throw new RuntimeException(ex); + return startViaZygote(processClass, niceName, uid, gid, gids, + debugFlags, targetSdkVersion, zygoteArgs); + } catch (ZygoteStartFailedEx ex) { + Log.e(LOG_TAG, + "Starting VM process through Zygote failed"); + throw new RuntimeException( + "Starting VM process through Zygote failed", ex); } - } /** retry interval for opening a zygote socket */ @@ -430,14 +375,11 @@ public class Process { * and returns the child's pid. Please note: the present implementation * replaces newlines in the argument list with spaces. * @param args argument list - * @return PID of new child process + * @return An object that describes the result of the attempt to start the process. * @throws ZygoteStartFailedEx if process start failed for any reason */ - private static int zygoteSendArgsAndGetPid(ArrayList<String> args) + private static ProcessStartResult zygoteSendArgsAndGetResult(ArrayList<String> args) throws ZygoteStartFailedEx { - - int pid; - openZygoteSocketIfNeeded(); try { @@ -448,7 +390,8 @@ public class Process { * b) a number of newline-separated argument strings equal to count * * After the zygote process reads these it will write the pid of - * the child or -1 on failure. + * the child or -1 on failure, followed by boolean to + * indicate whether a wrapper process was used. */ sZygoteWriter.write(Integer.toString(args.size())); @@ -468,11 +411,13 @@ public class Process { sZygoteWriter.flush(); // Should there be a timeout on this? - pid = sZygoteInputStream.readInt(); - - if (pid < 0) { + ProcessStartResult result = new ProcessStartResult(); + result.pid = sZygoteInputStream.readInt(); + if (result.pid < 0) { throw new ZygoteStartFailedEx("fork() failed"); } + result.usingWrapper = sZygoteInputStream.readBoolean(); + return result; } catch (IOException ex) { try { if (sZygoteSocket != null) { @@ -487,8 +432,6 @@ public class Process { throw new ZygoteStartFailedEx(ex); } - - return pid; } /** @@ -500,20 +443,19 @@ public class Process { * @param gid a POSIX gid that the new process shuold setgid() to * @param gids null-ok; a list of supplementary group IDs that the * new process should setgroup() to. - * @param enableDebugger True if debugging should be enabled for this process. + * @param debugFlags Additional flags. + * @param targetSdkVersion The target SDK version for the app. * @param extraArgs Additional arguments to supply to the zygote process. - * @return PID + * @return An object that describes the result of the attempt to start the process. * @throws ZygoteStartFailedEx if process start failed for any reason */ - private static int startViaZygote(final String processClass, + private static ProcessStartResult startViaZygote(final String processClass, final String niceName, final int uid, final int gid, final int[] gids, - int debugFlags, + int debugFlags, int targetSdkVersion, String[] extraArgs) throws ZygoteStartFailedEx { - int pid; - synchronized(Process.class) { ArrayList<String> argsForZygote = new ArrayList<String>(); @@ -537,6 +479,7 @@ public class Process { if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) { argsForZygote.add("--enable-assert"); } + argsForZygote.add("--target-sdk-version=" + targetSdkVersion); //TODO optionally enable debuger //argsForZygote.add("--enable-debugger"); @@ -568,15 +511,9 @@ public class Process { argsForZygote.add(arg); } } - - pid = zygoteSendArgsAndGetPid(argsForZygote); - } - if (pid <= 0) { - throw new ZygoteStartFailedEx("zygote start failed:" + pid); + return zygoteSendArgsAndGetResult(argsForZygote); } - - return pid; } /** @@ -736,8 +673,13 @@ public class Process { * * @return Returns true if the system can run in multiple processes, else * false if everything is running in a single process. + * + * @deprecated This method always returns true. Do not use. */ - public static final native boolean supportsProcesses(); + @Deprecated + public static final boolean supportsProcesses() { + return true; + } /** * Set the out-of-memory badness adjustment for a process. @@ -855,4 +797,21 @@ public class Process { * @hide */ public static final native long getPss(int pid); + + /** + * Specifies the outcome of having started a process. + * @hide + */ + public static final class ProcessStartResult { + /** + * The PID of the newly started process. + * Always >= 0. (If the start failed, an exception will have been thrown instead.) + */ + public int pid; + + /** + * True if the process was started with a wrapper attached. + */ + public boolean usingWrapper; + } } |