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