blob: e3a2cdd4f533b64da755f1770a6a341bc1193d96 [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>
19#include <stdlib.h>
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070020#include <sys/prctl.h>
21#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 Hughes01158d72011-09-19 19:47:10 -070034namespace art {
35
36namespace {
37
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070038static pid_t gSystemServerPid = 0;
39
Elliott Hughes01158d72011-09-19 19:47:10 -070040void Zygote_nativeExecShell(JNIEnv* env, jclass, jstring javaCommand) {
41 ScopedUtfChars command(env, javaCommand);
42 if (command.c_str() == NULL) {
43 return;
44 }
Elliott Hughesc1f143d2011-12-01 17:31:10 -080045 const char* argp[] = {_PATH_BSHELL, "-c", command.c_str(), NULL};
Elliott Hughes01158d72011-09-19 19:47:10 -070046 LOG(INFO) << "Exec: " << argp[0] << ' ' << argp[1] << ' ' << argp[2];
47
48 execv(_PATH_BSHELL, (char**)argp);
49 exit(127);
50}
51
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070052
53// This signal handler is for zygote mode, since the zygote must reap its children
Elliott Hughes4ffd3132011-10-24 12:06:42 -070054void SigChldHandler(int s) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070055 pid_t pid;
56 int status;
57
58 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
59 // Log process-death status that we care about. In general it is
60 // not safe to call LOG(...) from a signal handler because of
61 // possible reentrancy. However, we know a priori that the
62 // current implementation of LOG() is safe to call from a SIGCHLD
63 // handler in the zygote process. If the LOG() implementation
64 // changes its locking strategy or its use of syscalls within the
65 // lazy-init critical section, its use here may become unsafe.
66 if (WIFEXITED(status)) {
67 if (WEXITSTATUS(status)) {
68 LOG(INFO) << "Process " << pid << " exited cleanly (" << WEXITSTATUS(status) << ")";
69 } else if (false) {
70 LOG(INFO) << "Process " << pid << " exited cleanly (" << WEXITSTATUS(status) << ")";
71 }
72 } else if (WIFSIGNALED(status)) {
73 if (WTERMSIG(status) != SIGKILL) {
74 LOG(INFO) << "Process " << pid << " terminated by signal (" << WTERMSIG(status) << ")";
75 } else if (false) {
76 LOG(INFO) << "Process " << pid << " terminated by signal (" << WTERMSIG(status) << ")";
77 }
78#ifdef WCOREDUMP
79 if (WCOREDUMP(status)) {
80 LOG(INFO) << "Process " << pid << " dumped core";
81 }
82#endif /* ifdef WCOREDUMP */
83 }
84
85 // If the just-crashed process is the system_server, bring down zygote
86 // so that it is restarted by init and system server will be restarted
87 // from there.
88 if (pid == gSystemServerPid) {
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -070089 LOG(ERROR) << "Exit zygote because system server (" << pid << ") has terminated";
90 kill(getpid(), SIGKILL);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070091 }
92 }
93
94 if (pid < 0) {
95 PLOG(WARNING) << "Zygote SIGCHLD error in waitpid";
96 }
97}
98
99// configure sigchld handler for the zygote process This is configured
100// very late, because earlier in the runtime we may fork() and exec()
101// other processes, and we want to waitpid() for those rather than
102// have them be harvested immediately.
103//
104// This ends up being called repeatedly before each fork(), but there's
105// no real harm in that.
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700106void SetSigChldHandler() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700107 struct sigaction sa;
108 memset(&sa, 0, sizeof(sa));
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700109 sa.sa_handler = SigChldHandler;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700110
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700111 int err = sigaction(SIGCHLD, &sa, NULL);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700112 if (err < 0) {
113 PLOG(WARNING) << "Error setting SIGCHLD handler";
114 }
115}
116
117// Set the SIGCHLD handler back to default behavior in zygote children
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700118void UnsetSigChldHandler() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700119 struct sigaction sa;
120 memset(&sa, 0, sizeof(sa));
121 sa.sa_handler = SIG_DFL;
122
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700123 int err = sigaction(SIGCHLD, &sa, NULL);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700124 if (err < 0) {
125 PLOG(WARNING) << "Error unsetting SIGCHLD handler";
126 }
127}
128
129// Calls POSIX setgroups() using the int[] object as an argument.
130// A NULL argument is tolerated.
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700131int SetGids(JNIEnv* env, jintArray javaGids) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700132 if (javaGids == NULL) {
133 return 0;
134 }
135
136 COMPILE_ASSERT(sizeof(gid_t) == sizeof(jint), sizeof_gid_and_jint_are_differerent);
137 ScopedIntArrayRO gids(env, javaGids);
138 if (gids.get() == NULL) {
139 return -1;
140 }
141 return setgroups(gids.size(), (const gid_t *) &gids[0]);
142}
143
144// Sets the resource limits via setrlimit(2) for the values in the
145// two-dimensional array of integers that's passed in. The second dimension
146// contains a tuple of length 3: (resource, rlim_cur, rlim_max). NULL is
147// treated as an empty array.
148//
149// -1 is returned on error.
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700150int SetRLimits(JNIEnv* env, jobjectArray javaRlimits) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700151 if (javaRlimits == NULL) {
152 return 0;
153 }
154
155 struct rlimit rlim;
156 memset(&rlim, 0, sizeof(rlim));
157
158 for (int i = 0; i < env->GetArrayLength(javaRlimits); i++) {
159 ScopedLocalRef<jobject> javaRlimitObject(env, env->GetObjectArrayElement(javaRlimits, i));
160 ScopedIntArrayRO javaRlimit(env, reinterpret_cast<jintArray>(javaRlimitObject.get()));
161 if (javaRlimit.size() != 3) {
162 LOG(ERROR) << "rlimits array must have a second dimension of size 3";
163 return -1;
164 }
165
166 rlim.rlim_cur = javaRlimit[1];
167 rlim.rlim_max = javaRlimit[2];
168
169 int err = setrlimit(javaRlimit[0], &rlim);
170 if (err < 0) {
171 return -1;
172 }
173 }
174 return 0;
175}
176
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800177void SetCapabilities(int64_t permitted, int64_t effective) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700178#ifdef HAVE_ANDROID_OS
179 struct __user_cap_header_struct capheader;
180 struct __user_cap_data_struct capdata;
181
182 memset(&capheader, 0, sizeof(capheader));
183 memset(&capdata, 0, sizeof(capdata));
184
185 capheader.version = _LINUX_CAPABILITY_VERSION;
186 capheader.pid = 0;
187
188 capdata.effective = effective;
189 capdata.permitted = permitted;
190
191 if (capset(&capheader, &capdata) != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800192 PLOG(FATAL) << "capset(" << permitted << ", " << effective << ") failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700193 }
194#endif /*HAVE_ANDROID_OS*/
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700195}
196
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700197void EnableDebugFeatures(uint32_t debug_flags) {
198 // Must match values in dalvik.system.Zygote.
199 enum {
200 DEBUG_ENABLE_DEBUGGER = 1,
201 DEBUG_ENABLE_CHECKJNI = 1 << 1,
202 DEBUG_ENABLE_ASSERT = 1 << 2,
203 DEBUG_ENABLE_SAFEMODE = 1 << 3,
204 DEBUG_ENABLE_JNI_LOGGING = 1 << 4,
205 };
206
207 if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) {
208 Runtime* runtime = Runtime::Current();
209 JavaVMExt* vm = runtime->GetJavaVM();
210 if (!vm->check_jni) {
211 LOG(DEBUG) << "Late-enabling -Xcheck:jni";
212 vm->EnableCheckJni();
213 // There's only one thread running at this point, so only one JNIEnv to fix up.
214 Thread::Current()->GetJniEnv()->EnableCheckJni();
215 } else {
216 LOG(DEBUG) << "Not late-enabling -Xcheck:jni (already on)";
217 }
218 debug_flags &= ~DEBUG_ENABLE_CHECKJNI;
219 }
220
221 if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) {
222 Runtime::Current()->GetJavaVM()->log_third_party_jni = true;
223 debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING;
224 }
225
226 Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0);
227#ifdef HAVE_ANDROID_OS
228 if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) {
229 /* To let a non-privileged gdbserver attach to this
230 * process, we must set its dumpable bit flag. However
231 * we are not interested in generating a coredump in
232 * case of a crash, so also set the coredump size to 0
233 * to disable that
234 */
235 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) < 0) {
236 PLOG(ERROR) << "could not set dumpable bit flag for pid " << getpid();
237 } else {
238 struct rlimit rl;
239 rl.rlim_cur = 0;
240 rl.rlim_max = RLIM_INFINITY;
241 if (setrlimit(RLIMIT_CORE, &rl) < 0) {
242 PLOG(ERROR) << "could not disable core file generation for pid " << getpid();
243 }
244 }
245 }
246#endif
247 debug_flags &= ~DEBUG_ENABLE_DEBUGGER;
248
249 // These two are for backwards compatibility with Dalvik.
250 debug_flags &= ~DEBUG_ENABLE_ASSERT;
251 debug_flags &= ~DEBUG_ENABLE_SAFEMODE;
252
253 if (debug_flags != 0) {
254 LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags);
255 }
256}
257
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700258#ifdef HAVE_ANDROID_OS
259extern "C" int gMallocLeakZygoteChild;
260#endif
261
262// Utility routine to fork zygote and specialize the child process.
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700263pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids,
264 jint debug_flags, jobjectArray javaRlimits,
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700265 jlong permittedCapabilities, jlong effectiveCapabilities)
266{
267 Runtime* runtime = Runtime::Current();
268 CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote";
269 if (false) { // TODO: do we need do anything special like !dvmGcPreZygoteFork()?
270 LOG(FATAL) << "pre-fork heap failed";
271 }
272
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700273 SetSigChldHandler();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700274
275 // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable.
276 Thread* self = Thread::Current();
277
278 // dvmDumpLoaderStats("zygote"); // TODO: ?
279 pid_t pid = fork();
280
281 if (pid == 0) {
282 // The child process
283
284#ifdef HAVE_ANDROID_OS
285 gMallocLeakZygoteChild = 1;
286
287 // keep caps across UID change, unless we're staying root */
288 if (uid != 0) {
289 int err = prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
290 if (err < 0) {
291 PLOG(FATAL) << "cannot PR_SET_KEEPCAPS";
292 }
293 }
294#endif // HAVE_ANDROID_OS
295
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700296 int err = SetGids(env, javaGids);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700297 if (err < 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800298 PLOG(FATAL) << "setgroups failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700299 }
300
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700301 err = SetRLimits(env, javaRlimits);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700302 if (err < 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800303 PLOG(FATAL) << "setrlimit failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700304 }
305
306 err = setgid(gid);
307 if (err < 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800308 PLOG(FATAL) << "setgid(" << gid << ") failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700309 }
310
311 err = setuid(uid);
312 if (err < 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800313 PLOG(FATAL) << "setuid(" << uid << ") failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700314 }
315
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800316 SetCapabilities(permittedCapabilities, effectiveCapabilities);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700317
318 // Our system thread ID, etc, has changed so reset Thread state.
319 self->InitAfterFork();
320
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700321 EnableDebugFeatures(debug_flags);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700322
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700323 UnsetSigChldHandler();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700324 runtime->DidForkFromZygote();
325 } else if (pid > 0) {
326 // the parent process
327 }
328 return pid;
329}
330
Brian Carlstroma9f19782011-10-13 00:14:47 -0700331jint Zygote_nativeForkAndSpecialize(JNIEnv* env, jclass, jint uid, jint gid, jintArray gids,
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700332 jint debug_flags, jobjectArray rlimits) {
333 return ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags, rlimits, 0, 0);
Brian Carlstroma9f19782011-10-13 00:14:47 -0700334}
335
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700336jint Zygote_nativeForkSystemServer(JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700337 jint debug_flags, jobjectArray rlimits,
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700338 jlong permittedCapabilities, jlong effectiveCapabilities) {
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700339 pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids,
340 debug_flags, rlimits,
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700341 permittedCapabilities, effectiveCapabilities);
342 if (pid > 0) {
343 // The zygote process checks whether the child process has died or not.
344 LOG(INFO) << "System server process " << pid << " has been created";
345 gSystemServerPid = pid;
346 // There is a slight window that the system server process has crashed
347 // but it went unnoticed because we haven't published its pid yet. So
348 // we recheck here just to make sure that all is well.
349 int status;
350 if (waitpid(pid, &status, WNOHANG) == pid) {
351 LOG(FATAL) << "System server process " << pid << " has died. Restarting Zygote!";
352 }
353 }
354 return pid;
355}
356
Elliott Hughes01158d72011-09-19 19:47:10 -0700357static JNINativeMethod gMethods[] = {
358 NATIVE_METHOD(Zygote, nativeExecShell, "(Ljava/lang/String;)V"),
359 //NATIVE_METHOD(Zygote, nativeFork, "()I"),
Brian Carlstroma9f19782011-10-13 00:14:47 -0700360 NATIVE_METHOD(Zygote, nativeForkAndSpecialize, "(II[II[[I)I"),
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700361 NATIVE_METHOD(Zygote, nativeForkSystemServer, "(II[II[[IJJ)I"),
Elliott Hughes01158d72011-09-19 19:47:10 -0700362};
363
364} // namespace
365
366void register_dalvik_system_Zygote(JNIEnv* env) {
367 jniRegisterNativeMethods(env, "dalvik/system/Zygote", gMethods, NELEM(gMethods));
368}
369
370} // namespace art