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