diff options
author | Narayan Kamath <narayan@google.com> | 2014-04-02 10:48:05 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2014-04-02 10:48:05 +0000 |
commit | acede2c288e0350871346e59ba5649a532913915 (patch) | |
tree | b61b718123993a33bcc696a66beeeff174862d89 /dalvik/src | |
parent | 14271bd6ef45754aecd0bbef5e6d92dbc56caa4c (diff) | |
parent | 0c2165add0458c9f27c3e15875f936d9d3157a4b (diff) | |
download | libcore-acede2c288e0350871346e59ba5649a532913915.zip libcore-acede2c288e0350871346e59ba5649a532913915.tar.gz libcore-acede2c288e0350871346e59ba5649a532913915.tar.bz2 |
am 0c2165ad: am e471f800: Merge "Rename Zygote -> ZygoteHooks."
* commit '0c2165add0458c9f27c3e15875f936d9d3157a4b':
Rename Zygote -> ZygoteHooks.
Diffstat (limited to 'dalvik/src')
-rw-r--r-- | dalvik/src/main/java/dalvik/system/Zygote.java | 187 | ||||
-rw-r--r-- | dalvik/src/main/java/dalvik/system/ZygoteHooks.java | 78 |
2 files changed, 78 insertions, 187 deletions
diff --git a/dalvik/src/main/java/dalvik/system/Zygote.java b/dalvik/src/main/java/dalvik/system/Zygote.java deleted file mode 100644 index aa9bf20..0000000 --- a/dalvik/src/main/java/dalvik/system/Zygote.java +++ /dev/null @@ -1,187 +0,0 @@ -/* - * Copyright (C) 2006 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package dalvik.system; - -import libcore.io.ErrnoException; -import libcore.io.Libcore; - -import java.io.File; - -/** - * Provides access to the Dalvik "zygote" feature, which allows a VM instance to - * be partially initialized and then fork()'d from the partially initialized - * state. - * - * @hide - */ -public class Zygote { - /* - * Bit values for "debugFlags" argument. The definitions are duplicated - * in the native code. - */ - /** enable debugging over JDWP */ - public static final int DEBUG_ENABLE_DEBUGGER = 1; - /** enable JNI checks */ - public static final int DEBUG_ENABLE_CHECKJNI = 1 << 1; - /** enable Java programming language "assert" statements */ - public static final int DEBUG_ENABLE_ASSERT = 1 << 2; - /** disable the JIT compiler */ - public static final int DEBUG_ENABLE_SAFEMODE = 1 << 3; - /** Enable logging of third-party JNI activity. */ - public static final int DEBUG_ENABLE_JNI_LOGGING = 1 << 4; - - /** No external storage should be mounted. */ - public static final int MOUNT_EXTERNAL_NONE = 0; - /** Single-user external storage should be mounted. */ - public static final int MOUNT_EXTERNAL_SINGLEUSER = 1; - /** Multi-user external storage should be mounted. */ - public static final int MOUNT_EXTERNAL_MULTIUSER = 2; - /** All multi-user external storage should be mounted. */ - public static final int MOUNT_EXTERNAL_MULTIUSER_ALL = 3; - - private Zygote() {} - - private static void preFork() { - Daemons.stop(); - waitUntilAllThreadsStopped(); - } - - /** - * We must not fork until we're single-threaded again. Wait until /proc shows we're - * down to just one thread. - */ - private static void waitUntilAllThreadsStopped() { - File tasks = new File("/proc/self/task"); - while (tasks.list().length > 1) { - try { - // Experimentally, booting and playing about with a stingray, I never saw us - // go round this loop more than once with a 10ms sleep. - Thread.sleep(10); - } catch (InterruptedException ignored) { - } - } - } - - private static void postFork() { - Daemons.start(); - } - - /** - * Forks a new VM instance. The current VM must have been started - * with the -Xzygote flag. <b>NOTE: new instance keeps all - * root capabilities. The new process is expected to call capset()</b>. - * - * @param uid the UNIX uid that the new process should setuid() to after - * fork()ing and and before spawning any threads. - * @param gid the UNIX gid that the new process should setgid() to after - * fork()ing and and before spawning any threads. - * @param gids null-ok; a list of UNIX gids that the new process should - * setgroups() to after fork and before spawning any threads. - * @param debugFlags bit flags that enable debugging features. - * @param rlimits null-ok an array of rlimit tuples, with the second - * dimension having a length of 3 and representing - * (resource, rlim_cur, rlim_max). These are set via the posix - * setrlimit(2) call. - * @param seInfo null-ok a string specifying SELinux information for - * the new process. - * @param niceName null-ok a string specifying the process name. - * @param fdsToClose an array of ints, holding one or more POSIX - * file descriptor numbers that are to be closed by the child - * (and replaced by /dev/null) after forking. An integer value - * of -1 in any entry in the array means "ignore this one". - * - * @return 0 if this is the child, pid of the child - * if this is the parent, or -1 on error. - */ - public static int forkAndSpecialize(int uid, int gid, int[] gids, int debugFlags, - int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose) { - preFork(); - int pid = nativeForkAndSpecialize( - uid, gid, gids, debugFlags, rlimits, mountExternal, seInfo, niceName, fdsToClose); - postFork(); - return pid; - } - - native public static int nativeForkAndSpecialize(int uid, int gid, int[] gids, int debugFlags, - int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose); - - /** - * Special method to start the system server process. In addition to the - * common actions performed in forkAndSpecialize, the pid of the child - * process is recorded such that the death of the child process will cause - * zygote to exit. - * - * @param uid the UNIX uid that the new process should setuid() to after - * fork()ing and and before spawning any threads. - * @param gid the UNIX gid that the new process should setgid() to after - * fork()ing and and before spawning any threads. - * @param gids null-ok; a list of UNIX gids that the new process should - * setgroups() to after fork and before spawning any threads. - * @param debugFlags bit flags that enable debugging features. - * @param rlimits null-ok an array of rlimit tuples, with the second - * dimension having a length of 3 and representing - * (resource, rlim_cur, rlim_max). These are set via the posix - * setrlimit(2) call. - * @param permittedCapabilities argument for setcap() - * @param effectiveCapabilities argument for setcap() - * - * @return 0 if this is the child, pid of the child - * if this is the parent, or -1 on error. - */ - public static int forkSystemServer(int uid, int gid, int[] gids, int debugFlags, - int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) { - preFork(); - int pid = nativeForkSystemServer( - uid, gid, gids, debugFlags, rlimits, permittedCapabilities, effectiveCapabilities); - postFork(); - return pid; - } - - native public static int nativeForkSystemServer(int uid, int gid, int[] gids, int debugFlags, - int[][] rlimits, long permittedCapabilities, long effectiveCapabilities); - - /** - * Executes "/system/bin/sh -c <command>" using the exec() system call. - * This method throws a runtime exception if exec() failed, otherwise, this - * method never returns. - * - * @param command The shell command to execute. - */ - public static void execShell(String command) { - String[] args = { "/system/bin/sh", "-c", command }; - try { - Libcore.os.execv(args[0], args); - } catch (ErrnoException e) { - throw new RuntimeException(e); - } - } - - /** - * Appends quotes shell arguments to the specified string builder. - * The arguments are quoted using single-quotes, escaped if necessary, - * prefixed with a space, and appended to the command. - * - * @param command A string builder for the shell command being constructed. - * @param args An array of argument strings to be quoted and appended to the command. - * @see #execShell(String) - */ - public static void appendQuotedShellArgs(StringBuilder command, String[] args) { - for (String arg : args) { - command.append(" '").append(arg.replace("'", "'\\''")).append("'"); - } - } -} diff --git a/dalvik/src/main/java/dalvik/system/ZygoteHooks.java b/dalvik/src/main/java/dalvik/system/ZygoteHooks.java new file mode 100644 index 0000000..fa4f392 --- /dev/null +++ b/dalvik/src/main/java/dalvik/system/ZygoteHooks.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2006 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dalvik.system; + +import java.io.File; + +/** + * Provides hooks for the zygote to call back into the runtime to perform + * parent or child specific initialization.. + * + * @hide + */ +public final class ZygoteHooks { + private long token; + + /** + * Called by the zygote prior to every fork. Each call to {@code preFork} + * is followed by a matching call to {@link #postForkChild(int)} on the child + * process and {@link #postForkCommon()} on both the parent and the child + * process. {@code postForkCommon} is called after {@code postForkChild} in + * the child process. + */ + public void preFork() { + Daemons.stop(); + waitUntilAllThreadsStopped(); + token = nativePreFork(); + } + + /** + * Called by the zygote in the child process after every fork. The debug + * flags from {@code debugFlags} are applied to the child process. + */ + public void postForkChild(int debugFlags) { + nativePostForkChild(token, debugFlags); + } + + /** + * Called by the zygote in both the parent and child processes after + * every fork. In the child process, this method is called after + * {@code postForkChild}. + */ + public void postForkCommon() { + Daemons.start(); + } + + private static native long nativePreFork(); + private static native void nativePostForkChild(long token, int debugFlags); + + /** + * We must not fork until we're single-threaded again. Wait until /proc shows we're + * down to just one thread. + */ + private static void waitUntilAllThreadsStopped() { + File tasks = new File("/proc/self/task"); + while (tasks.list().length > 1) { + try { + // Experimentally, booting and playing about with a stingray, I never saw us + // go round this loop more than once with a 10ms sleep. + Thread.sleep(10); + } catch (InterruptedException ignored) { + } + } + } +} |