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 | |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 25 | import java.lang.reflect.InvocationTargetException; |
| 26 | import java.lang.reflect.Method; |
| 27 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 28 | /** @hide */ |
| 29 | public final class Zygote { |
| 30 | /* |
| 31 | * Bit values for "debugFlags" argument. The definitions are duplicated |
| 32 | * in the native code. |
| 33 | */ |
| 34 | |
| 35 | /** enable debugging over JDWP */ |
Nicolas Geoffray | 347b1df | 2016-12-20 14:05:05 +0000 | [diff] [blame] | 36 | public static final int DEBUG_ENABLE_JDWP = 1; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 37 | /** enable JNI checks */ |
| 38 | public static final int DEBUG_ENABLE_CHECKJNI = 1 << 1; |
| 39 | /** enable Java programming language "assert" statements */ |
| 40 | public static final int DEBUG_ENABLE_ASSERT = 1 << 2; |
Mathieu Chartier | 7a49028 | 2015-03-17 09:51:36 -0700 | [diff] [blame] | 41 | /** disable the AOT compiler and JIT */ |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 42 | public static final int DEBUG_ENABLE_SAFEMODE = 1 << 3; |
| 43 | /** Enable logging of third-party JNI activity. */ |
| 44 | public static final int DEBUG_ENABLE_JNI_LOGGING = 1 << 4; |
David Srbecky | 065075e | 2015-05-28 17:16:09 +0100 | [diff] [blame] | 45 | /** Force generation of native debugging information. */ |
Nicolas Geoffray | 9abbf45 | 2015-11-05 11:29:42 +0000 | [diff] [blame] | 46 | public static final int DEBUG_GENERATE_DEBUG_INFO = 1 << 5; |
Tamas Berghammer | df6cb28 | 2016-01-29 12:07:00 +0000 | [diff] [blame] | 47 | /** Always use JIT-ed code. */ |
| 48 | public static final int DEBUG_ALWAYS_JIT = 1 << 6; |
Nicolas Geoffray | 347b1df | 2016-12-20 14:05:05 +0000 | [diff] [blame] | 49 | /** Make the code native debuggable by turning off some optimizations. */ |
Tamas Berghammer | df6cb28 | 2016-01-29 12:07:00 +0000 | [diff] [blame] | 50 | public static final int DEBUG_NATIVE_DEBUGGABLE = 1 << 7; |
Nicolas Geoffray | 347b1df | 2016-12-20 14:05:05 +0000 | [diff] [blame] | 51 | /** Make the code Java debuggable by turning off some optimizations. */ |
| 52 | public static final int DEBUG_JAVA_DEBUGGABLE = 1 << 8; |
Mathieu Chartier | 7a49028 | 2015-03-17 09:51:36 -0700 | [diff] [blame] | 53 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 54 | /** No external storage should be mounted. */ |
| 55 | public static final int MOUNT_EXTERNAL_NONE = 0; |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 56 | /** Default external storage should be mounted. */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 57 | public static final int MOUNT_EXTERNAL_DEFAULT = 1; |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 58 | /** Read-only external storage should be mounted. */ |
| 59 | public static final int MOUNT_EXTERNAL_READ = 2; |
| 60 | /** Read-write external storage should be mounted. */ |
| 61 | public static final int MOUNT_EXTERNAL_WRITE = 3; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 62 | |
| 63 | private static final ZygoteHooks VM_HOOKS = new ZygoteHooks(); |
| 64 | |
| 65 | private Zygote() {} |
| 66 | |
| 67 | /** |
| 68 | * Forks a new VM instance. The current VM must have been started |
| 69 | * with the -Xzygote flag. <b>NOTE: new instance keeps all |
| 70 | * root capabilities. The new process is expected to call capset()</b>. |
| 71 | * |
| 72 | * @param uid the UNIX uid that the new process should setuid() to after |
| 73 | * fork()ing and and before spawning any threads. |
| 74 | * @param gid the UNIX gid that the new process should setgid() to after |
| 75 | * fork()ing and and before spawning any threads. |
| 76 | * @param gids null-ok; a list of UNIX gids that the new process should |
| 77 | * setgroups() to after fork and before spawning any threads. |
| 78 | * @param debugFlags bit flags that enable debugging features. |
| 79 | * @param rlimits null-ok an array of rlimit tuples, with the second |
| 80 | * dimension having a length of 3 and representing |
| 81 | * (resource, rlim_cur, rlim_max). These are set via the posix |
| 82 | * setrlimit(2) call. |
| 83 | * @param seInfo null-ok a string specifying SELinux information for |
| 84 | * the new process. |
| 85 | * @param niceName null-ok a string specifying the process name. |
| 86 | * @param fdsToClose an array of ints, holding one or more POSIX |
| 87 | * file descriptor numbers that are to be closed by the child |
| 88 | * (and replaced by /dev/null) after forking. An integer value |
| 89 | * of -1 in any entry in the array means "ignore this one". |
Andreas Gampe | 8dfa178 | 2017-01-05 12:45:58 -0800 | [diff] [blame] | 90 | * @param fdsToIgnore null-ok an array of ints, either null or holding |
| 91 | * one or more POSIX file descriptor numbers that are to be ignored |
| 92 | * in the file descriptor table check. |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 93 | * @param instructionSet null-ok the instruction set to use. |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 94 | * @param appDataDir null-ok the data directory of the app. |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 95 | * |
| 96 | * @return 0 if this is the child, pid of the child |
| 97 | * if this is the parent, or -1 on error. |
| 98 | */ |
| 99 | public static int forkAndSpecialize(int uid, int gid, int[] gids, int debugFlags, |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 100 | int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose, |
Andreas Gampe | 8dfa178 | 2017-01-05 12:45:58 -0800 | [diff] [blame] | 101 | int[] fdsToIgnore, String instructionSet, String appDataDir) { |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 102 | VM_HOOKS.preFork(); |
Hiroshi Yamauchi | 04ed650 | 2017-03-02 13:39:07 -0800 | [diff] [blame] | 103 | // Resets nice priority for zygote process. |
| 104 | resetNicePriority(); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 105 | int pid = nativeForkAndSpecialize( |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 106 | uid, gid, gids, debugFlags, rlimits, mountExternal, seInfo, niceName, fdsToClose, |
Andreas Gampe | 8dfa178 | 2017-01-05 12:45:58 -0800 | [diff] [blame] | 107 | fdsToIgnore, instructionSet, appDataDir); |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame] | 108 | // Enable tracing as soon as possible for the child process. |
| 109 | if (pid == 0) { |
| 110 | Trace.setTracingEnabled(true); |
| 111 | |
| 112 | // Note that this event ends at the end of handleChildProc, |
| 113 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "PostFork"); |
| 114 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 115 | VM_HOOKS.postForkCommon(); |
| 116 | return pid; |
| 117 | } |
| 118 | |
| 119 | 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] | 120 | int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose, |
Andreas Gampe | 8dfa178 | 2017-01-05 12:45:58 -0800 | [diff] [blame] | 121 | int[] fdsToIgnore, String instructionSet, String appDataDir); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 122 | |
| 123 | /** |
Christopher Ferris | 76de39e | 2017-06-20 16:13:40 -0700 | [diff] [blame] | 124 | * Called to do any initialization before starting an application. |
| 125 | */ |
| 126 | native static void nativePreApplicationInit(); |
| 127 | |
| 128 | /** |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 129 | * Special method to start the system server process. In addition to the |
| 130 | * common actions performed in forkAndSpecialize, the pid of the child |
| 131 | * process is recorded such that the death of the child process will cause |
| 132 | * zygote to exit. |
| 133 | * |
| 134 | * @param uid the UNIX uid that the new process should setuid() to after |
| 135 | * fork()ing and and before spawning any threads. |
| 136 | * @param gid the UNIX gid that the new process should setgid() to after |
| 137 | * fork()ing and and before spawning any threads. |
| 138 | * @param gids null-ok; a list of UNIX gids that the new process should |
| 139 | * setgroups() to after fork and before spawning any threads. |
| 140 | * @param debugFlags bit flags that enable debugging features. |
| 141 | * @param rlimits null-ok an array of rlimit tuples, with the second |
| 142 | * dimension having a length of 3 and representing |
| 143 | * (resource, rlim_cur, rlim_max). These are set via the posix |
| 144 | * setrlimit(2) call. |
| 145 | * @param permittedCapabilities argument for setcap() |
| 146 | * @param effectiveCapabilities argument for setcap() |
| 147 | * |
| 148 | * @return 0 if this is the child, pid of the child |
| 149 | * if this is the parent, or -1 on error. |
| 150 | */ |
| 151 | public static int forkSystemServer(int uid, int gid, int[] gids, int debugFlags, |
| 152 | int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) { |
| 153 | VM_HOOKS.preFork(); |
Hiroshi Yamauchi | 04ed650 | 2017-03-02 13:39:07 -0800 | [diff] [blame] | 154 | // Resets nice priority for zygote process. |
| 155 | resetNicePriority(); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 156 | int pid = nativeForkSystemServer( |
| 157 | uid, gid, gids, debugFlags, rlimits, permittedCapabilities, effectiveCapabilities); |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame] | 158 | // Enable tracing as soon as we enter the system_server. |
| 159 | if (pid == 0) { |
| 160 | Trace.setTracingEnabled(true); |
| 161 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 162 | VM_HOOKS.postForkCommon(); |
| 163 | return pid; |
| 164 | } |
| 165 | |
| 166 | native private static int nativeForkSystemServer(int uid, int gid, int[] gids, int debugFlags, |
| 167 | int[][] rlimits, long permittedCapabilities, long effectiveCapabilities); |
| 168 | |
doheon1.lee | 885b742 | 2016-01-20 13:07:27 +0900 | [diff] [blame] | 169 | /** |
Robert Sesek | 54e387d | 2016-12-02 17:27:50 -0500 | [diff] [blame] | 170 | * Lets children of the zygote inherit open file descriptors to this path. |
| 171 | */ |
| 172 | native protected static void nativeAllowFileAcrossFork(String path); |
| 173 | |
| 174 | /** |
doheon1.lee | 885b742 | 2016-01-20 13:07:27 +0900 | [diff] [blame] | 175 | * Zygote unmount storage space on initializing. |
| 176 | * This method is called once. |
| 177 | */ |
| 178 | native protected static void nativeUnmountStorageOnInit(); |
| 179 | |
Nicolas Geoffray | 3c43b38 | 2015-12-11 15:01:04 +0000 | [diff] [blame] | 180 | private static void callPostForkChildHooks(int debugFlags, boolean isSystemServer, |
| 181 | String instructionSet) { |
| 182 | VM_HOOKS.postForkChild(debugFlags, isSystemServer, instructionSet); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Hiroshi Yamauchi | 04ed650 | 2017-03-02 13:39:07 -0800 | [diff] [blame] | 185 | /** |
| 186 | * Resets the calling thread priority to the default value (Thread.NORM_PRIORITY |
| 187 | * or nice value 0). This updates both the priority value in java.lang.Thread and |
| 188 | * the nice value (setpriority). |
| 189 | */ |
| 190 | static void resetNicePriority() { |
| 191 | Thread.currentThread().setPriority(Thread.NORM_PRIORITY); |
| 192 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 193 | |
| 194 | /** |
| 195 | * Executes "/system/bin/sh -c <command>" using the exec() system call. |
| 196 | * This method throws a runtime exception if exec() failed, otherwise, this |
| 197 | * method never returns. |
| 198 | * |
| 199 | * @param command The shell command to execute. |
| 200 | */ |
| 201 | public static void execShell(String command) { |
| 202 | String[] args = { "/system/bin/sh", "-c", command }; |
| 203 | try { |
Elliott Hughes | 860c591 | 2014-04-28 19:19:13 -0700 | [diff] [blame] | 204 | Os.execv(args[0], args); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 205 | } catch (ErrnoException e) { |
| 206 | throw new RuntimeException(e); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Appends quotes shell arguments to the specified string builder. |
| 212 | * The arguments are quoted using single-quotes, escaped if necessary, |
| 213 | * prefixed with a space, and appended to the command. |
| 214 | * |
| 215 | * @param command A string builder for the shell command being constructed. |
| 216 | * @param args An array of argument strings to be quoted and appended to the command. |
| 217 | * @see #execShell(String) |
| 218 | */ |
| 219 | public static void appendQuotedShellArgs(StringBuilder command, String[] args) { |
| 220 | for (String arg : args) { |
| 221 | command.append(" '").append(arg.replace("'", "'\\''")).append("'"); |
| 222 | } |
| 223 | } |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 224 | |
| 225 | /** |
| 226 | * Helper exception class which holds a method and arguments and |
| 227 | * can call them. This is used as part of a trampoline to get rid of |
| 228 | * the initial process setup stack frames. |
| 229 | */ |
| 230 | public static class MethodAndArgsCaller extends Exception |
| 231 | implements Runnable { |
| 232 | /** method to call */ |
| 233 | private final Method mMethod; |
| 234 | |
| 235 | /** argument array */ |
| 236 | private final String[] mArgs; |
| 237 | |
| 238 | public MethodAndArgsCaller(Method method, String[] args) { |
| 239 | mMethod = method; |
| 240 | mArgs = args; |
| 241 | } |
| 242 | |
| 243 | public void run() { |
| 244 | try { |
| 245 | mMethod.invoke(null, new Object[] { mArgs }); |
| 246 | } catch (IllegalAccessException ex) { |
| 247 | throw new RuntimeException(ex); |
| 248 | } catch (InvocationTargetException ex) { |
| 249 | Throwable cause = ex.getCause(); |
| 250 | if (cause instanceof RuntimeException) { |
| 251 | throw (RuntimeException) cause; |
| 252 | } else if (cause instanceof Error) { |
| 253 | throw (Error) cause; |
| 254 | } |
| 255 | throw new RuntimeException(ex); |
| 256 | } |
| 257 | } |
| 258 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 259 | } |