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