Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 17 | #include <grp.h> |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 18 | #include <paths.h> |
Elliott Hughes | ada2da9 | 2012-01-17 17:58:58 -0800 | [diff] [blame] | 19 | #include <signal.h> |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 21 | #include <sys/mount.h> |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | #include <sys/wait.h> |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | |
Elliott Hughes | b6636b8 | 2012-04-24 10:41:16 -0700 | [diff] [blame] | 26 | #include "cutils/sched_policy.h" |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 27 | #include "debugger.h" |
| 28 | #include "jni_internal.h" |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 29 | #include "JNIHelp.h" |
| 30 | #include "ScopedLocalRef.h" |
| 31 | #include "ScopedPrimitiveArray.h" |
| 32 | #include "ScopedUtfChars.h" |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 33 | #include "thread.h" |
| 34 | |
Elliott Hughes | b4807ec | 2012-01-17 18:33:04 -0800 | [diff] [blame] | 35 | #if defined(HAVE_PRCTL) |
| 36 | #include <sys/prctl.h> |
| 37 | #endif |
| 38 | |
Elliott Hughes | c151f90 | 2012-06-21 20:33:21 -0700 | [diff] [blame] | 39 | #if defined(HAVE_SELINUX) |
| 40 | #include <selinux/android.h> |
| 41 | #endif |
| 42 | |
Brian Carlstrom | 154cef6 | 2012-05-02 22:34:03 -0700 | [diff] [blame] | 43 | #if defined(__linux__) |
| 44 | #include <sys/personality.h> |
Nick Kralevich | b788278 | 2012-09-27 12:29:02 -0700 | [diff] [blame] | 45 | #include <sys/utsname.h> |
Brian Carlstrom | 154cef6 | 2012-05-02 22:34:03 -0700 | [diff] [blame] | 46 | #endif |
| 47 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 48 | namespace art { |
| 49 | |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 50 | static pid_t gSystemServerPid = 0; |
| 51 | |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 52 | // Must match values in dalvik.system.Zygote. |
| 53 | enum MountExternalKind { |
| 54 | MOUNT_EXTERNAL_NONE = 0, |
| 55 | MOUNT_EXTERNAL_SINGLEUSER = 1, |
| 56 | MOUNT_EXTERNAL_MULTIUSER = 2, |
| 57 | }; |
| 58 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 59 | static void Zygote_nativeExecShell(JNIEnv* env, jclass, jstring javaCommand) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 60 | ScopedUtfChars command(env, javaCommand); |
| 61 | if (command.c_str() == NULL) { |
| 62 | return; |
| 63 | } |
Elliott Hughes | c1f143d | 2011-12-01 17:31:10 -0800 | [diff] [blame] | 64 | const char* argp[] = {_PATH_BSHELL, "-c", command.c_str(), NULL}; |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 65 | LOG(INFO) << "Exec: " << argp[0] << ' ' << argp[1] << ' ' << argp[2]; |
| 66 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 67 | execv(_PATH_BSHELL, const_cast<char**>(argp)); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 68 | exit(127); |
| 69 | } |
| 70 | |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 71 | // This signal handler is for zygote mode, since the zygote must reap its children |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 72 | static void SigChldHandler(int /*signal_number*/) { |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 73 | pid_t pid; |
| 74 | int status; |
| 75 | |
| 76 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { |
| 77 | // Log process-death status that we care about. In general it is |
| 78 | // not safe to call LOG(...) from a signal handler because of |
| 79 | // possible reentrancy. However, we know a priori that the |
| 80 | // current implementation of LOG() is safe to call from a SIGCHLD |
| 81 | // handler in the zygote process. If the LOG() implementation |
| 82 | // changes its locking strategy or its use of syscalls within the |
| 83 | // lazy-init critical section, its use here may become unsafe. |
| 84 | if (WIFEXITED(status)) { |
| 85 | if (WEXITSTATUS(status)) { |
| 86 | LOG(INFO) << "Process " << pid << " exited cleanly (" << WEXITSTATUS(status) << ")"; |
| 87 | } else if (false) { |
| 88 | LOG(INFO) << "Process " << pid << " exited cleanly (" << WEXITSTATUS(status) << ")"; |
| 89 | } |
| 90 | } else if (WIFSIGNALED(status)) { |
| 91 | if (WTERMSIG(status) != SIGKILL) { |
| 92 | LOG(INFO) << "Process " << pid << " terminated by signal (" << WTERMSIG(status) << ")"; |
| 93 | } else if (false) { |
| 94 | LOG(INFO) << "Process " << pid << " terminated by signal (" << WTERMSIG(status) << ")"; |
| 95 | } |
| 96 | #ifdef WCOREDUMP |
| 97 | if (WCOREDUMP(status)) { |
| 98 | LOG(INFO) << "Process " << pid << " dumped core"; |
| 99 | } |
| 100 | #endif /* ifdef WCOREDUMP */ |
| 101 | } |
| 102 | |
| 103 | // If the just-crashed process is the system_server, bring down zygote |
| 104 | // so that it is restarted by init and system server will be restarted |
| 105 | // from there. |
| 106 | if (pid == gSystemServerPid) { |
Brian Carlstrom | 6a4be3a | 2011-10-20 16:34:03 -0700 | [diff] [blame] | 107 | LOG(ERROR) << "Exit zygote because system server (" << pid << ") has terminated"; |
| 108 | kill(getpid(), SIGKILL); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | if (pid < 0) { |
| 113 | PLOG(WARNING) << "Zygote SIGCHLD error in waitpid"; |
| 114 | } |
| 115 | } |
| 116 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 117 | // Configures the SIGCHLD handler for the zygote process. This is configured |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 118 | // very late, because earlier in the runtime we may fork() and exec() |
| 119 | // other processes, and we want to waitpid() for those rather than |
| 120 | // have them be harvested immediately. |
| 121 | // |
| 122 | // This ends up being called repeatedly before each fork(), but there's |
| 123 | // no real harm in that. |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 124 | static void SetSigChldHandler() { |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 125 | struct sigaction sa; |
| 126 | memset(&sa, 0, sizeof(sa)); |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 127 | sa.sa_handler = SigChldHandler; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 128 | |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 129 | int err = sigaction(SIGCHLD, &sa, NULL); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 130 | if (err < 0) { |
| 131 | PLOG(WARNING) << "Error setting SIGCHLD handler"; |
| 132 | } |
| 133 | } |
| 134 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 135 | // Sets the SIGCHLD handler back to default behavior in zygote children. |
| 136 | static void UnsetSigChldHandler() { |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 137 | struct sigaction sa; |
| 138 | memset(&sa, 0, sizeof(sa)); |
| 139 | sa.sa_handler = SIG_DFL; |
| 140 | |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 141 | int err = sigaction(SIGCHLD, &sa, NULL); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 142 | if (err < 0) { |
| 143 | PLOG(WARNING) << "Error unsetting SIGCHLD handler"; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // Calls POSIX setgroups() using the int[] object as an argument. |
| 148 | // A NULL argument is tolerated. |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 149 | static void SetGids(JNIEnv* env, jintArray javaGids) { |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 150 | if (javaGids == NULL) { |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 151 | return; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | COMPILE_ASSERT(sizeof(gid_t) == sizeof(jint), sizeof_gid_and_jint_are_differerent); |
| 155 | ScopedIntArrayRO gids(env, javaGids); |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 156 | CHECK(gids.get() != NULL); |
| 157 | int rc = setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0])); |
| 158 | if (rc == -1) { |
| 159 | PLOG(FATAL) << "setgroups failed"; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 160 | } |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // Sets the resource limits via setrlimit(2) for the values in the |
| 164 | // two-dimensional array of integers that's passed in. The second dimension |
| 165 | // contains a tuple of length 3: (resource, rlim_cur, rlim_max). NULL is |
| 166 | // treated as an empty array. |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 167 | static void SetRLimits(JNIEnv* env, jobjectArray javaRlimits) { |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 168 | if (javaRlimits == NULL) { |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 169 | return; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 172 | rlimit rlim; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 173 | memset(&rlim, 0, sizeof(rlim)); |
| 174 | |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 175 | for (int i = 0; i < env->GetArrayLength(javaRlimits); ++i) { |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 176 | ScopedLocalRef<jobject> javaRlimitObject(env, env->GetObjectArrayElement(javaRlimits, i)); |
| 177 | ScopedIntArrayRO javaRlimit(env, reinterpret_cast<jintArray>(javaRlimitObject.get())); |
| 178 | if (javaRlimit.size() != 3) { |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 179 | LOG(FATAL) << "rlimits array must have a second dimension of size 3"; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | rlim.rlim_cur = javaRlimit[1]; |
| 183 | rlim.rlim_max = javaRlimit[2]; |
| 184 | |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 185 | int rc = setrlimit(javaRlimit[0], &rlim); |
| 186 | if (rc == -1) { |
| 187 | PLOG(FATAL) << "setrlimit(" << javaRlimit[0] << ", " |
| 188 | << "{" << rlim.rlim_cur << ", " << rlim.rlim_max << "}) failed"; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Elliott Hughes | 76e3694 | 2012-03-16 13:44:56 -0700 | [diff] [blame] | 193 | #if defined(HAVE_ANDROID_OS) |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 194 | |
| 195 | // The debug malloc library needs to know whether it's the zygote or a child. |
| 196 | extern "C" int gMallocLeakZygoteChild; |
| 197 | |
| 198 | static void EnableDebugger() { |
| 199 | // To let a non-privileged gdbserver attach to this |
| 200 | // process, we must set our dumpable flag. |
| 201 | if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) { |
| 202 | PLOG(ERROR) << "prctl(PR_SET_DUMPABLE) failed for pid " << getpid(); |
| 203 | } |
| 204 | // We don't want core dumps, though, so set the core dump size to 0. |
| 205 | rlimit rl; |
| 206 | rl.rlim_cur = 0; |
| 207 | rl.rlim_max = RLIM_INFINITY; |
| 208 | if (setrlimit(RLIMIT_CORE, &rl) == -1) { |
| 209 | PLOG(ERROR) << "setrlimit(RLIMIT_CORE) failed for pid " << getpid(); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | static void EnableKeepCapabilities() { |
| 214 | int rc = prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0); |
| 215 | if (rc == -1) { |
| 216 | PLOG(FATAL) << "prctl(PR_SET_KEEPCAPS) failed"; |
| 217 | } |
| 218 | } |
| 219 | |
Elliott Hughes | 76e3694 | 2012-03-16 13:44:56 -0700 | [diff] [blame] | 220 | static void SetCapabilities(int64_t permitted, int64_t effective) { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 221 | __user_cap_header_struct capheader; |
| 222 | __user_cap_data_struct capdata; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 223 | |
| 224 | memset(&capheader, 0, sizeof(capheader)); |
| 225 | memset(&capdata, 0, sizeof(capdata)); |
| 226 | |
| 227 | capheader.version = _LINUX_CAPABILITY_VERSION; |
| 228 | capheader.pid = 0; |
| 229 | |
| 230 | capdata.effective = effective; |
| 231 | capdata.permitted = permitted; |
| 232 | |
| 233 | if (capset(&capheader, &capdata) != 0) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 234 | PLOG(FATAL) << "capset(" << permitted << ", " << effective << ") failed"; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 235 | } |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 236 | } |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 237 | |
| 238 | static void SetSchedulerPolicy() { |
| 239 | errno = -set_sched_policy(0, SP_DEFAULT); |
| 240 | if (errno != 0) { |
| 241 | PLOG(FATAL) << "set_sched_policy(0, SP_DEFAULT) failed"; |
| 242 | } |
| 243 | } |
| 244 | |
Elliott Hughes | 76e3694 | 2012-03-16 13:44:56 -0700 | [diff] [blame] | 245 | #else |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 246 | |
| 247 | static int gMallocLeakZygoteChild = 0; |
| 248 | |
| 249 | static void EnableDebugger() {} |
| 250 | static void EnableKeepCapabilities() {} |
Elliott Hughes | 76e3694 | 2012-03-16 13:44:56 -0700 | [diff] [blame] | 251 | static void SetCapabilities(int64_t, int64_t) {} |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 252 | static void SetSchedulerPolicy() {} |
| 253 | |
Elliott Hughes | 76e3694 | 2012-03-16 13:44:56 -0700 | [diff] [blame] | 254 | #endif |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 255 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 256 | static void EnableDebugFeatures(uint32_t debug_flags) { |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 257 | // Must match values in dalvik.system.Zygote. |
| 258 | enum { |
| 259 | DEBUG_ENABLE_DEBUGGER = 1, |
| 260 | DEBUG_ENABLE_CHECKJNI = 1 << 1, |
| 261 | DEBUG_ENABLE_ASSERT = 1 << 2, |
| 262 | DEBUG_ENABLE_SAFEMODE = 1 << 3, |
| 263 | DEBUG_ENABLE_JNI_LOGGING = 1 << 4, |
| 264 | }; |
| 265 | |
| 266 | if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) { |
| 267 | Runtime* runtime = Runtime::Current(); |
| 268 | JavaVMExt* vm = runtime->GetJavaVM(); |
| 269 | if (!vm->check_jni) { |
| 270 | LOG(DEBUG) << "Late-enabling -Xcheck:jni"; |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 271 | vm->SetCheckJniEnabled(true); |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 272 | // There's only one thread running at this point, so only one JNIEnv to fix up. |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 273 | Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true); |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 274 | } else { |
| 275 | LOG(DEBUG) << "Not late-enabling -Xcheck:jni (already on)"; |
| 276 | } |
| 277 | debug_flags &= ~DEBUG_ENABLE_CHECKJNI; |
| 278 | } |
| 279 | |
| 280 | if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 281 | gLogVerbosity.third_party_jni = true; |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 282 | debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING; |
| 283 | } |
| 284 | |
| 285 | Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0); |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 286 | if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) { |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 287 | EnableDebugger(); |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 288 | } |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 289 | debug_flags &= ~DEBUG_ENABLE_DEBUGGER; |
| 290 | |
| 291 | // These two are for backwards compatibility with Dalvik. |
| 292 | debug_flags &= ~DEBUG_ENABLE_ASSERT; |
| 293 | debug_flags &= ~DEBUG_ENABLE_SAFEMODE; |
| 294 | |
| 295 | if (debug_flags != 0) { |
| 296 | LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags); |
| 297 | } |
| 298 | } |
| 299 | |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 300 | // Create private mount space for this process and mount SD card |
| 301 | // into it, based on the active user. |
| 302 | static void MountExternalStorage(uid_t uid, jint mount_external) { |
| 303 | if (mount_external == MOUNT_EXTERNAL_NONE) { |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | #if 0 |
| 308 | userid_t user_id = multiuser_getUserId(uid); |
| 309 | |
| 310 | // Create private mount namespace for our process. |
| 311 | if (unshare(CLONE_NEWNS) == -1) { |
| 312 | PLOG(FATAL) << "unshare(CLONE_NEWNS) failed"; |
| 313 | } |
| 314 | |
| 315 | // Mark rootfs as being a slave in our process so that changes |
| 316 | // from parent namespace flow into our process. |
| 317 | if (mount("rootfs", "/", NULL, (MS_SLAVE | MS_REC), NULL) == -1) { |
| 318 | PLOG(FATAL) << "mount(\"rootfs\", \"/\", NULL, (MS_SLAVE | MS_REC), NULL) failed"; |
| 319 | } |
| 320 | |
| 321 | // Create bind mount from specific path. |
| 322 | if (mount_external == MOUNT_EXTERNAL_SINGLEUSER) { |
| 323 | if (mount(EXTERNAL_STORAGE_SYSTEM, EXTERNAL_STORAGE_APP, "none", MS_BIND, NULL) == -1) { |
| 324 | PLOG(FATAL) << "mount(\"" << EXTERNAL_STORAGE_SYSTEM << "\", \"" << EXTERNAL_STORAGE_APP << "\", \"none\", MS_BIND, NULL) failed"; |
| 325 | } |
| 326 | } else if (mount_external == MOUNT_EXTERNAL_MULTIUSER) { |
| 327 | // Assume path has already been created by installd. |
| 328 | std::string source_path(StringPrintf("%s/%d", EXTERNAL_STORAGE_SYSTEM, user_id)); |
| 329 | if (mount(source_path.c_str(), EXTERNAL_STORAGE_APP, "none", MS_BIND, NULL) == -1) { |
| 330 | PLOG(FATAL) << "mount(\"" << source_path.c_str() << "\", \"" << EXTERNAL_STORAGE_APP << "\", \"none\", MS_BIND, NULL) failed"; |
| 331 | } |
| 332 | } else { |
| 333 | LOG(FATAL) << "Mount mode unsupported: " << mount_external; |
| 334 | } |
| 335 | #else |
| 336 | UNUSED(uid); |
| 337 | UNIMPLEMENTED(FATAL); |
| 338 | #endif |
| 339 | } |
| 340 | |
Nick Kralevich | b788278 | 2012-09-27 12:29:02 -0700 | [diff] [blame] | 341 | #if defined(__linux__) |
| 342 | static bool NeedsNoRandomizeWorkaround() { |
| 343 | int major; |
| 344 | int minor; |
| 345 | struct utsname uts; |
| 346 | if (uname(&uts) == -1) { |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) { |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | // Kernels before 3.4.* need the workaround. |
| 355 | return (major < 3) || ((major == 3) && (minor < 4)); |
| 356 | } |
| 357 | #endif |
| 358 | |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 359 | // Utility routine to fork zygote and specialize the child process. |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 360 | static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids, |
| 361 | jint debug_flags, jobjectArray javaRlimits, |
Elliott Hughes | c151f90 | 2012-06-21 20:33:21 -0700 | [diff] [blame] | 362 | jlong permittedCapabilities, jlong effectiveCapabilities, |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 363 | jint mount_external, |
Elliott Hughes | c151f90 | 2012-06-21 20:33:21 -0700 | [diff] [blame] | 364 | jstring java_se_info, jstring java_se_name, bool is_system_server) { |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 365 | Runtime* runtime = Runtime::Current(); |
| 366 | CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote"; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 367 | if (!runtime->PreZygoteFork()) { |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 368 | LOG(FATAL) << "pre-fork heap failed"; |
| 369 | } |
| 370 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 371 | SetSigChldHandler(); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 372 | |
| 373 | // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable. |
| 374 | Thread* self = Thread::Current(); |
| 375 | |
| 376 | // dvmDumpLoaderStats("zygote"); // TODO: ? |
| 377 | pid_t pid = fork(); |
| 378 | |
| 379 | if (pid == 0) { |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 380 | // The child process. |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 381 | gMallocLeakZygoteChild = 1; |
| 382 | |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 383 | // Keep capabilities across UID change, unless we're staying root. |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 384 | if (uid != 0) { |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 385 | EnableKeepCapabilities(); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 388 | MountExternalStorage(uid, mount_external); |
| 389 | |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 390 | SetGids(env, javaGids); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 391 | |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 392 | SetRLimits(env, javaRlimits); |
| 393 | |
| 394 | int rc = setgid(gid); |
| 395 | if (rc == -1) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 396 | PLOG(FATAL) << "setgid(" << gid << ") failed"; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 399 | rc = setuid(uid); |
| 400 | if (rc == -1) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 401 | PLOG(FATAL) << "setuid(" << uid << ") failed"; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Brian Carlstrom | 154cef6 | 2012-05-02 22:34:03 -0700 | [diff] [blame] | 404 | #if defined(__linux__) |
Nick Kralevich | b788278 | 2012-09-27 12:29:02 -0700 | [diff] [blame] | 405 | if (NeedsNoRandomizeWorkaround()) { |
| 406 | // Work around ARM kernel ASLR lossage (http://b/5817320). |
| 407 | int old_personality = personality(0xffffffff); |
| 408 | int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE); |
| 409 | if (new_personality == -1) { |
| 410 | PLOG(WARNING) << "personality(" << new_personality << ") failed"; |
| 411 | } |
Elliott Hughes | 51e5386 | 2012-05-02 17:17:28 -0700 | [diff] [blame] | 412 | } |
Brian Carlstrom | 154cef6 | 2012-05-02 22:34:03 -0700 | [diff] [blame] | 413 | #endif |
Elliott Hughes | 51e5386 | 2012-05-02 17:17:28 -0700 | [diff] [blame] | 414 | |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 415 | SetCapabilities(permittedCapabilities, effectiveCapabilities); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 416 | |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 417 | SetSchedulerPolicy(); |
Elliott Hughes | b6636b8 | 2012-04-24 10:41:16 -0700 | [diff] [blame] | 418 | |
Elliott Hughes | c151f90 | 2012-06-21 20:33:21 -0700 | [diff] [blame] | 419 | #if defined(HAVE_SELINUX) && defined(HAVE_ANDROID_OS) |
| 420 | { |
| 421 | ScopedUtfChars se_info(env, java_se_info); |
| 422 | CHECK(se_info != NULL); |
| 423 | ScopedUtfChars se_name(env, java_se_name); |
| 424 | CHECK(se_name != NULL); |
| 425 | rc = selinux_android_setcontext(uid, is_system_server, se_info, se_name); |
| 426 | if (rc == -1) { |
| 427 | PLOG(FATAL) << "selinux_android_setcontext(" << uid << ", " |
| 428 | << (is_system_server ? "true" : "false") << ", " |
| 429 | << "\"" << se_info << "\", \"" << se_name << "\") failed"; |
| 430 | } |
| 431 | } |
| 432 | #else |
| 433 | UNUSED(is_system_server); |
| 434 | UNUSED(java_se_info); |
| 435 | UNUSED(java_se_name); |
| 436 | #endif |
| 437 | |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 438 | // Our system thread ID, etc, has changed so reset Thread state. |
| 439 | self->InitAfterFork(); |
| 440 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 441 | EnableDebugFeatures(debug_flags); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 442 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 443 | UnsetSigChldHandler(); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 444 | runtime->DidForkFromZygote(); |
| 445 | } else if (pid > 0) { |
| 446 | // the parent process |
| 447 | } |
| 448 | return pid; |
| 449 | } |
| 450 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 451 | static jint Zygote_nativeForkAndSpecialize(JNIEnv* env, jclass, jint uid, jint gid, jintArray gids, |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 452 | jint debug_flags, jobjectArray rlimits, jint mount_external, |
Elliott Hughes | c151f90 | 2012-06-21 20:33:21 -0700 | [diff] [blame] | 453 | jstring se_info, jstring se_name) { |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 454 | return ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags, rlimits, 0, 0, mount_external, se_info, se_name, false); |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 457 | static jint Zygote_nativeForkSystemServer(JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids, |
| 458 | jint debug_flags, jobjectArray rlimits, |
| 459 | jlong permittedCapabilities, jlong effectiveCapabilities) { |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 460 | pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids, |
| 461 | debug_flags, rlimits, |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 462 | permittedCapabilities, effectiveCapabilities, |
| 463 | MOUNT_EXTERNAL_NONE, NULL, NULL, true); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 464 | if (pid > 0) { |
| 465 | // The zygote process checks whether the child process has died or not. |
| 466 | LOG(INFO) << "System server process " << pid << " has been created"; |
| 467 | gSystemServerPid = pid; |
| 468 | // There is a slight window that the system server process has crashed |
| 469 | // but it went unnoticed because we haven't published its pid yet. So |
| 470 | // we recheck here just to make sure that all is well. |
| 471 | int status; |
| 472 | if (waitpid(pid, &status, WNOHANG) == pid) { |
| 473 | LOG(FATAL) << "System server process " << pid << " has died. Restarting Zygote!"; |
| 474 | } |
| 475 | } |
| 476 | return pid; |
| 477 | } |
| 478 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 479 | static JNINativeMethod gMethods[] = { |
| 480 | NATIVE_METHOD(Zygote, nativeExecShell, "(Ljava/lang/String;)V"), |
| 481 | //NATIVE_METHOD(Zygote, nativeFork, "()I"), |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 482 | NATIVE_METHOD(Zygote, nativeForkAndSpecialize, "(II[II[[IILjava/lang/String;Ljava/lang/String;)I"), |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 483 | NATIVE_METHOD(Zygote, nativeForkSystemServer, "(II[II[[IJJ)I"), |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 484 | }; |
| 485 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 486 | void register_dalvik_system_Zygote(JNIEnv* env) { |
Elliott Hughes | 7756d54 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 487 | REGISTER_NATIVE_METHODS("dalvik/system/Zygote"); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | } // namespace art |