Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.internal.os; |
| 18 | |
| 19 | |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame^] | 20 | import android.os.Trace; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 21 | import dalvik.system.ZygoteHooks; |
Elliott Hughes | 860c591 | 2014-04-28 19:19:13 -0700 | [diff] [blame] | 22 | import android.system.ErrnoException; |
| 23 | import android.system.Os; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 24 | |
| 25 | /** @hide */ |
| 26 | public final class Zygote { |
| 27 | /* |
| 28 | * Bit values for "debugFlags" argument. The definitions are duplicated |
| 29 | * in the native code. |
| 30 | */ |
| 31 | |
| 32 | /** enable debugging over JDWP */ |
| 33 | public static final int DEBUG_ENABLE_DEBUGGER = 1; |
| 34 | /** enable JNI checks */ |
| 35 | public static final int DEBUG_ENABLE_CHECKJNI = 1 << 1; |
| 36 | /** enable Java programming language "assert" statements */ |
| 37 | public static final int DEBUG_ENABLE_ASSERT = 1 << 2; |
Mathieu Chartier | 7a49028 | 2015-03-17 09:51:36 -0700 | [diff] [blame] | 38 | /** disable the AOT compiler and JIT */ |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 39 | public static final int DEBUG_ENABLE_SAFEMODE = 1 << 3; |
| 40 | /** Enable logging of third-party JNI activity. */ |
| 41 | public static final int DEBUG_ENABLE_JNI_LOGGING = 1 << 4; |
Mathieu Chartier | 7a49028 | 2015-03-17 09:51:36 -0700 | [diff] [blame] | 42 | /** enable the JIT compiler */ |
| 43 | public static final int DEBUG_ENABLE_JIT = 1 << 5; |
Andreas Gampe | 27c39f1 | 2015-04-27 18:28:18 +0000 | [diff] [blame] | 44 | /** Force generation of CFI code */ |
| 45 | public static final int DEBUG_GENERATE_CFI = 1 << 6; |
Mathieu Chartier | 7a49028 | 2015-03-17 09:51:36 -0700 | [diff] [blame] | 46 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 47 | /** No external storage should be mounted. */ |
| 48 | public static final int MOUNT_EXTERNAL_NONE = 0; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 49 | /** Default user-specific external storage should be mounted. */ |
| 50 | public static final int MOUNT_EXTERNAL_DEFAULT = 1; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 51 | |
| 52 | private static final ZygoteHooks VM_HOOKS = new ZygoteHooks(); |
| 53 | |
| 54 | private Zygote() {} |
| 55 | |
| 56 | /** |
| 57 | * Forks a new VM instance. The current VM must have been started |
| 58 | * with the -Xzygote flag. <b>NOTE: new instance keeps all |
| 59 | * root capabilities. The new process is expected to call capset()</b>. |
| 60 | * |
| 61 | * @param uid the UNIX uid that the new process should setuid() to after |
| 62 | * fork()ing and and before spawning any threads. |
| 63 | * @param gid the UNIX gid that the new process should setgid() to after |
| 64 | * fork()ing and and before spawning any threads. |
| 65 | * @param gids null-ok; a list of UNIX gids that the new process should |
| 66 | * setgroups() to after fork and before spawning any threads. |
| 67 | * @param debugFlags bit flags that enable debugging features. |
| 68 | * @param rlimits null-ok an array of rlimit tuples, with the second |
| 69 | * dimension having a length of 3 and representing |
| 70 | * (resource, rlim_cur, rlim_max). These are set via the posix |
| 71 | * setrlimit(2) call. |
| 72 | * @param seInfo null-ok a string specifying SELinux information for |
| 73 | * the new process. |
| 74 | * @param niceName null-ok a string specifying the process name. |
| 75 | * @param fdsToClose an array of ints, holding one or more POSIX |
| 76 | * file descriptor numbers that are to be closed by the child |
| 77 | * (and replaced by /dev/null) after forking. An integer value |
| 78 | * of -1 in any entry in the array means "ignore this one". |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 79 | * @param instructionSet null-ok the instruction set to use. |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 80 | * @param appDataDir null-ok the data directory of the app. |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 81 | * |
| 82 | * @return 0 if this is the child, pid of the child |
| 83 | * if this is the parent, or -1 on error. |
| 84 | */ |
| 85 | public static int forkAndSpecialize(int uid, int gid, int[] gids, int debugFlags, |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 86 | int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose, |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 87 | String instructionSet, String appDataDir) { |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 88 | VM_HOOKS.preFork(); |
| 89 | int pid = nativeForkAndSpecialize( |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 90 | uid, gid, gids, debugFlags, rlimits, mountExternal, seInfo, niceName, fdsToClose, |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 91 | instructionSet, appDataDir); |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame^] | 92 | // Enable tracing as soon as possible for the child process. |
| 93 | if (pid == 0) { |
| 94 | Trace.setTracingEnabled(true); |
| 95 | |
| 96 | // Note that this event ends at the end of handleChildProc, |
| 97 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "PostFork"); |
| 98 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 99 | VM_HOOKS.postForkCommon(); |
| 100 | return pid; |
| 101 | } |
| 102 | |
| 103 | native private static int nativeForkAndSpecialize(int uid, int gid, int[] gids,int debugFlags, |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 104 | int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose, |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 105 | String instructionSet, String appDataDir); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * Special method to start the system server process. In addition to the |
| 109 | * common actions performed in forkAndSpecialize, the pid of the child |
| 110 | * process is recorded such that the death of the child process will cause |
| 111 | * zygote to exit. |
| 112 | * |
| 113 | * @param uid the UNIX uid that the new process should setuid() to after |
| 114 | * fork()ing and and before spawning any threads. |
| 115 | * @param gid the UNIX gid that the new process should setgid() to after |
| 116 | * fork()ing and and before spawning any threads. |
| 117 | * @param gids null-ok; a list of UNIX gids that the new process should |
| 118 | * setgroups() to after fork and before spawning any threads. |
| 119 | * @param debugFlags bit flags that enable debugging features. |
| 120 | * @param rlimits null-ok an array of rlimit tuples, with the second |
| 121 | * dimension having a length of 3 and representing |
| 122 | * (resource, rlim_cur, rlim_max). These are set via the posix |
| 123 | * setrlimit(2) call. |
| 124 | * @param permittedCapabilities argument for setcap() |
| 125 | * @param effectiveCapabilities argument for setcap() |
| 126 | * |
| 127 | * @return 0 if this is the child, pid of the child |
| 128 | * if this is the parent, or -1 on error. |
| 129 | */ |
| 130 | public static int forkSystemServer(int uid, int gid, int[] gids, int debugFlags, |
| 131 | int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) { |
| 132 | VM_HOOKS.preFork(); |
| 133 | int pid = nativeForkSystemServer( |
| 134 | uid, gid, gids, debugFlags, rlimits, permittedCapabilities, effectiveCapabilities); |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame^] | 135 | // Enable tracing as soon as we enter the system_server. |
| 136 | if (pid == 0) { |
| 137 | Trace.setTracingEnabled(true); |
| 138 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 139 | VM_HOOKS.postForkCommon(); |
| 140 | return pid; |
| 141 | } |
| 142 | |
| 143 | native private static int nativeForkSystemServer(int uid, int gid, int[] gids, int debugFlags, |
| 144 | int[][] rlimits, long permittedCapabilities, long effectiveCapabilities); |
| 145 | |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 146 | private static void callPostForkChildHooks(int debugFlags, String instructionSet) { |
| 147 | VM_HOOKS.postForkChild(debugFlags, instructionSet); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
| 151 | /** |
| 152 | * Executes "/system/bin/sh -c <command>" using the exec() system call. |
| 153 | * This method throws a runtime exception if exec() failed, otherwise, this |
| 154 | * method never returns. |
| 155 | * |
| 156 | * @param command The shell command to execute. |
| 157 | */ |
| 158 | public static void execShell(String command) { |
| 159 | String[] args = { "/system/bin/sh", "-c", command }; |
| 160 | try { |
Elliott Hughes | 860c591 | 2014-04-28 19:19:13 -0700 | [diff] [blame] | 161 | Os.execv(args[0], args); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 162 | } catch (ErrnoException e) { |
| 163 | throw new RuntimeException(e); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Appends quotes shell arguments to the specified string builder. |
| 169 | * The arguments are quoted using single-quotes, escaped if necessary, |
| 170 | * prefixed with a space, and appended to the command. |
| 171 | * |
| 172 | * @param command A string builder for the shell command being constructed. |
| 173 | * @param args An array of argument strings to be quoted and appended to the command. |
| 174 | * @see #execShell(String) |
| 175 | */ |
| 176 | public static void appendQuotedShellArgs(StringBuilder command, String[] args) { |
| 177 | for (String arg : args) { |
| 178 | command.append(" '").append(arg.replace("'", "'\\''")).append("'"); |
| 179 | } |
| 180 | } |
| 181 | } |