blob: 666541f9fdfb5b8cd834d5af69994e3e2cf8e0e5 [file] [log] [blame]
Elliott Hughes01158d72011-09-19 19:47:10 -07001/*
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 Carlstromcaabb1b2011-10-11 18:09:13 -070017#include <grp.h>
Elliott Hughes01158d72011-09-19 19:47:10 -070018#include <paths.h>
Elliott Hughesada2da92012-01-17 17:58:58 -080019#include <signal.h>
Elliott Hughes01158d72011-09-19 19:47:10 -070020#include <stdlib.h>
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070021#include <sys/types.h>
22#include <sys/wait.h>
Elliott Hughes01158d72011-09-19 19:47:10 -070023#include <unistd.h>
24
Elliott Hughes4ffd3132011-10-24 12:06:42 -070025#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 Carlstromcaabb1b2011-10-11 18:09:13 -070032#include "thread.h"
33
Elliott Hughesb4807ec2012-01-17 18:33:04 -080034#if defined(HAVE_PRCTL)
35#include <sys/prctl.h>
36#endif
37
Elliott Hughes01158d72011-09-19 19:47:10 -070038namespace art {
39
40namespace {
41
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070042static pid_t gSystemServerPid = 0;
43
Elliott Hughes01158d72011-09-19 19:47:10 -070044void Zygote_nativeExecShell(JNIEnv* env, jclass, jstring javaCommand) {
45 ScopedUtfChars command(env, javaCommand);
46 if (command.c_str() == NULL) {
47 return;
48 }
Elliott Hughesc1f143d2011-12-01 17:31:10 -080049 const char* argp[] = {_PATH_BSHELL, "-c", command.c_str(), NULL};
Elliott Hughes01158d72011-09-19 19:47:10 -070050 LOG(INFO) << "Exec: " << argp[0] << ' ' << argp[1] << ' ' << argp[2];
51
Elliott Hughesba8eee12012-01-24 20:25:24 -080052 execv(_PATH_BSHELL, const_cast<char**>(argp));
Elliott Hughes01158d72011-09-19 19:47:10 -070053 exit(127);
54}
55
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070056
57// This signal handler is for zygote mode, since the zygote must reap its children
Elliott Hughes4ffd3132011-10-24 12:06:42 -070058void SigChldHandler(int s) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070059 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 Carlstrom6a4be3a2011-10-20 16:34:03 -070093 LOG(ERROR) << "Exit zygote because system server (" << pid << ") has terminated";
94 kill(getpid(), SIGKILL);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070095 }
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 Hughes4ffd3132011-10-24 12:06:42 -0700110void SetSigChldHandler() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700111 struct sigaction sa;
112 memset(&sa, 0, sizeof(sa));
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700113 sa.sa_handler = SigChldHandler;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700114
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700115 int err = sigaction(SIGCHLD, &sa, NULL);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700116 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 Hughes4ffd3132011-10-24 12:06:42 -0700122void UnsetSigChldHandler() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700123 struct sigaction sa;
124 memset(&sa, 0, sizeof(sa));
125 sa.sa_handler = SIG_DFL;
126
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700127 int err = sigaction(SIGCHLD, &sa, NULL);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700128 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 Hughes4ffd3132011-10-24 12:06:42 -0700135int SetGids(JNIEnv* env, jintArray javaGids) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700136 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 Hughes4ffd3132011-10-24 12:06:42 -0700154int SetRLimits(JNIEnv* env, jobjectArray javaRlimits) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700155 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 Hughes3d30d9b2011-12-07 17:35:48 -0800181void SetCapabilities(int64_t permitted, int64_t effective) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700182#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 Hughes3d30d9b2011-12-07 17:35:48 -0800196 PLOG(FATAL) << "capset(" << permitted << ", " << effective << ") failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700197 }
198#endif /*HAVE_ANDROID_OS*/
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700199}
200
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700201void 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 Hughes4dd9b4d2011-12-12 18:29:24 -0800226 gLogVerbosity.third_party_jni = true;
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700227 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 Carlstromcaabb1b2011-10-11 18:09:13 -0700262#ifdef HAVE_ANDROID_OS
263extern "C" int gMallocLeakZygoteChild;
264#endif
265
266// Utility routine to fork zygote and specialize the child process.
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700267pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids,
268 jint debug_flags, jobjectArray javaRlimits,
Elliott Hughesba8eee12012-01-24 20:25:24 -0800269 jlong permittedCapabilities, jlong effectiveCapabilities) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700270 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 Hughes4ffd3132011-10-24 12:06:42 -0700276 SetSigChldHandler();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700277
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 Hughes4ffd3132011-10-24 12:06:42 -0700299 int err = SetGids(env, javaGids);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700300 if (err < 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800301 PLOG(FATAL) << "setgroups failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700302 }
303
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700304 err = SetRLimits(env, javaRlimits);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700305 if (err < 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800306 PLOG(FATAL) << "setrlimit failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700307 }
308
309 err = setgid(gid);
310 if (err < 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800311 PLOG(FATAL) << "setgid(" << gid << ") failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700312 }
313
314 err = setuid(uid);
315 if (err < 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800316 PLOG(FATAL) << "setuid(" << uid << ") failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700317 }
318
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800319 SetCapabilities(permittedCapabilities, effectiveCapabilities);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700320
321 // Our system thread ID, etc, has changed so reset Thread state.
322 self->InitAfterFork();
323
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700324 EnableDebugFeatures(debug_flags);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700325
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700326 UnsetSigChldHandler();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700327 runtime->DidForkFromZygote();
328 } else if (pid > 0) {
329 // the parent process
330 }
331 return pid;
332}
333
Brian Carlstroma9f19782011-10-13 00:14:47 -0700334jint Zygote_nativeForkAndSpecialize(JNIEnv* env, jclass, jint uid, jint gid, jintArray gids,
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700335 jint debug_flags, jobjectArray rlimits) {
336 return ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags, rlimits, 0, 0);
Brian Carlstroma9f19782011-10-13 00:14:47 -0700337}
338
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700339jint Zygote_nativeForkSystemServer(JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700340 jint debug_flags, jobjectArray rlimits,
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700341 jlong permittedCapabilities, jlong effectiveCapabilities) {
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700342 pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids,
343 debug_flags, rlimits,
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700344 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 Hughes01158d72011-09-19 19:47:10 -0700360static JNINativeMethod gMethods[] = {
361 NATIVE_METHOD(Zygote, nativeExecShell, "(Ljava/lang/String;)V"),
362 //NATIVE_METHOD(Zygote, nativeFork, "()I"),
Brian Carlstroma9f19782011-10-13 00:14:47 -0700363 NATIVE_METHOD(Zygote, nativeForkAndSpecialize, "(II[II[[I)I"),
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700364 NATIVE_METHOD(Zygote, nativeForkSystemServer, "(II[II[[IJJ)I"),
Elliott Hughes01158d72011-09-19 19:47:10 -0700365};
366
367} // namespace
368
369void register_dalvik_system_Zygote(JNIEnv* env) {
370 jniRegisterNativeMethods(env, "dalvik/system/Zygote", gMethods, NELEM(gMethods));
371}
372
373} // namespace art