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