2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "Zygote"
19 // sys/mount.h has to come before linux/fs.h due to redefinition of MS_RDONLY, MS_BIND, etc
20 #include <sys/mount.h>
29 #include <sys/capability.h>
30 #include <sys/personality.h>
31 #include <sys/prctl.h>
32 #include <sys/resource.h>
34 #include <sys/types.h>
35 #include <sys/utsname.h>
39 #include <cutils/fs.h>
40 #include <cutils/multiuser.h>
41 #include <cutils/sched_policy.h>
42 #include <private/android_filesystem_config.h>
43 #include <utils/String8.h>
44 #include <selinux/android.h>
45 #include <processgroup/processgroup.h>
48 #include "android_runtime/AndroidRuntime.h"
50 #include "ScopedLocalRef.h"
51 #include "ScopedPrimitiveArray.h"
52 #include "ScopedUtfChars.h"
54 #include "nativebridge/native_bridge.h"
58 using android::String8;
60 static pid_t gSystemServerPid = 0;
62 static const char kZygoteClassName[] = "com/android/internal/os/Zygote";
63 static jclass gZygoteClass;
64 static jmethodID gCallPostForkChildHooks;
66 // Must match values in com.android.internal.os.Zygote.
67 enum MountExternalKind {
68 MOUNT_EXTERNAL_NONE = 0,
69 MOUNT_EXTERNAL_SINGLEUSER = 1,
70 MOUNT_EXTERNAL_MULTIUSER = 2,
71 MOUNT_EXTERNAL_MULTIUSER_ALL = 3,
74 static void RuntimeAbort(JNIEnv* env) {
75 env->FatalError("RuntimeAbort");
78 // This signal handler is for zygote mode, since the zygote must reap its children
79 static void SigChldHandler(int /*signal_number*/) {
83 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
84 // Log process-death status that we care about. In general it is
85 // not safe to call LOG(...) from a signal handler because of
86 // possible reentrancy. However, we know a priori that the
87 // current implementation of LOG() is safe to call from a SIGCHLD
88 // handler in the zygote process. If the LOG() implementation
89 // changes its locking strategy or its use of syscalls within the
90 // lazy-init critical section, its use here may become unsafe.
91 if (WIFEXITED(status)) {
92 if (WEXITSTATUS(status)) {
93 ALOGI("Process %d exited cleanly (%d)", pid, WEXITSTATUS(status));
95 } else if (WIFSIGNALED(status)) {
96 if (WTERMSIG(status) != SIGKILL) {
97 ALOGI("Process %d exited due to signal (%d)", pid, WTERMSIG(status));
99 if (WCOREDUMP(status)) {
100 ALOGI("Process %d dumped core.", pid);
104 // If the just-crashed process is the system_server, bring down zygote
105 // so that it is restarted by init and system server will be restarted
107 if (pid == gSystemServerPid) {
108 ALOGE("Exit zygote because system server (%d) has terminated");
109 kill(getpid(), SIGKILL);
113 // Note that we shouldn't consider ECHILD an error because
114 // the secondary zygote might have no children left to wait for.
115 if (pid < 0 && errno != ECHILD) {
116 ALOGW("Zygote SIGCHLD error in waitpid: %s", strerror(errno));
120 // Configures the SIGCHLD handler for the zygote process. This is configured
121 // very late, because earlier in the runtime we may fork() and exec()
122 // other processes, and we want to waitpid() for those rather than
123 // have them be harvested immediately.
125 // This ends up being called repeatedly before each fork(), but there's
126 // no real harm in that.
127 static void SetSigChldHandler() {
129 memset(&sa, 0, sizeof(sa));
130 sa.sa_handler = SigChldHandler;
132 int err = sigaction(SIGCHLD, &sa, NULL);
134 ALOGW("Error setting SIGCHLD handler: %d", errno);
138 // Sets the SIGCHLD handler back to default behavior in zygote children.
139 static void UnsetSigChldHandler() {
141 memset(&sa, 0, sizeof(sa));
142 sa.sa_handler = SIG_DFL;
144 int err = sigaction(SIGCHLD, &sa, NULL);
146 ALOGW("Error unsetting SIGCHLD handler: %d", errno);
150 // Calls POSIX setgroups() using the int[] object as an argument.
151 // A NULL argument is tolerated.
152 static void SetGids(JNIEnv* env, jintArray javaGids) {
153 if (javaGids == NULL) {
157 ScopedIntArrayRO gids(env, javaGids);
158 if (gids.get() == NULL) {
161 int rc = setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0]));
163 ALOGE("setgroups failed");
168 // Sets the resource limits via setrlimit(2) for the values in the
169 // two-dimensional array of integers that's passed in. The second dimension
170 // contains a tuple of length 3: (resource, rlim_cur, rlim_max). NULL is
171 // treated as an empty array.
172 static void SetRLimits(JNIEnv* env, jobjectArray javaRlimits) {
173 if (javaRlimits == NULL) {
178 memset(&rlim, 0, sizeof(rlim));
180 for (int i = 0; i < env->GetArrayLength(javaRlimits); ++i) {
181 ScopedLocalRef<jobject> javaRlimitObject(env, env->GetObjectArrayElement(javaRlimits, i));
182 ScopedIntArrayRO javaRlimit(env, reinterpret_cast<jintArray>(javaRlimitObject.get()));
183 if (javaRlimit.size() != 3) {
184 ALOGE("rlimits array must have a second dimension of size 3");
188 rlim.rlim_cur = javaRlimit[1];
189 rlim.rlim_max = javaRlimit[2];
191 int rc = setrlimit(javaRlimit[0], &rlim);
193 ALOGE("setrlimit(%d, {%d, %d}) failed", javaRlimit[0], rlim.rlim_cur, rlim.rlim_max);
199 // The debug malloc library needs to know whether it's the zygote or a child.
200 extern "C" int gMallocLeakZygoteChild;
202 static void EnableKeepCapabilities(JNIEnv* env) {
203 int rc = prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
205 ALOGE("prctl(PR_SET_KEEPCAPS) failed");
210 static void DropCapabilitiesBoundingSet(JNIEnv* env) {
211 for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
212 int rc = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
214 if (errno == EINVAL) {
215 ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify "
216 "your kernel is compiled with file capabilities support");
218 ALOGE("prctl(PR_CAPBSET_DROP) failed");
225 static void SetCapabilities(JNIEnv* env, int64_t permitted, int64_t effective) {
226 __user_cap_header_struct capheader;
227 memset(&capheader, 0, sizeof(capheader));
228 capheader.version = _LINUX_CAPABILITY_VERSION_3;
231 __user_cap_data_struct capdata[2];
232 memset(&capdata, 0, sizeof(capdata));
233 capdata[0].effective = effective;
234 capdata[1].effective = effective >> 32;
235 capdata[0].permitted = permitted;
236 capdata[1].permitted = permitted >> 32;
238 if (capset(&capheader, &capdata[0]) == -1) {
239 ALOGE("capset(%lld, %lld) failed", permitted, effective);
244 static void SetSchedulerPolicy(JNIEnv* env) {
245 errno = -set_sched_policy(0, SP_DEFAULT);
247 ALOGE("set_sched_policy(0, SP_DEFAULT) failed");
252 // Create a private mount namespace and bind mount appropriate emulated
253 // storage for the given user.
254 static bool MountEmulatedStorage(uid_t uid, jint mount_mode, bool force_mount_namespace) {
255 if (mount_mode == MOUNT_EXTERNAL_NONE && !force_mount_namespace) {
259 // Create a second private mount namespace for our process
260 if (unshare(CLONE_NEWNS) == -1) {
261 ALOGW("Failed to unshare(): %d", errno);
265 if (mount_mode == MOUNT_EXTERNAL_NONE) {
269 // See storage config details at http://source.android.com/tech/storage/
270 userid_t user_id = multiuser_get_user_id(uid);
272 // Create bind mounts to expose external storage
273 if (mount_mode == MOUNT_EXTERNAL_MULTIUSER || mount_mode == MOUNT_EXTERNAL_MULTIUSER_ALL) {
274 // These paths must already be created by init.rc
275 const char* source = getenv("EMULATED_STORAGE_SOURCE");
276 const char* target = getenv("EMULATED_STORAGE_TARGET");
277 const char* legacy = getenv("EXTERNAL_STORAGE");
278 if (source == NULL || target == NULL || legacy == NULL) {
279 ALOGW("Storage environment undefined; unable to provide external storage");
283 // Prepare source paths
285 // /mnt/shell/emulated/0
286 const String8 source_user(String8::format("%s/%d", source, user_id));
287 // /storage/emulated/0
288 const String8 target_user(String8::format("%s/%d", target, user_id));
290 if (fs_prepare_dir(source_user.string(), 0000, 0, 0) == -1
291 || fs_prepare_dir(target_user.string(), 0000, 0, 0) == -1) {
295 if (mount_mode == MOUNT_EXTERNAL_MULTIUSER_ALL) {
296 // Mount entire external storage tree for all users
297 if (TEMP_FAILURE_RETRY(mount(source, target, NULL, MS_BIND, NULL)) == -1) {
298 ALOGW("Failed to mount %s to %s :%d", source, target, errno);
302 // Only mount user-specific external storage
303 if (TEMP_FAILURE_RETRY(
304 mount(source_user.string(), target_user.string(), NULL, MS_BIND, NULL)) == -1) {
305 ALOGW("Failed to mount %s to %s: %d", source_user.string(), target_user.string(), errno);
310 if (fs_prepare_dir(legacy, 0000, 0, 0) == -1) {
314 // Finally, mount user-specific path into place for legacy users
315 if (TEMP_FAILURE_RETRY(
316 mount(target_user.string(), legacy, NULL, MS_BIND | MS_REC, NULL)) == -1) {
317 ALOGW("Failed to mount %s to %s: %d", target_user.string(), legacy, errno);
321 ALOGW("Mount mode %d unsupported", mount_mode);
328 static bool NeedsNoRandomizeWorkaround() {
329 #if !defined(__arm__)
335 if (uname(&uts) == -1) {
339 if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) {
343 // Kernels before 3.4.* need the workaround.
344 return (major < 3) || ((major == 3) && (minor < 4));
348 // Utility to close down the Zygote socket file descriptors while
349 // the child is still running as root with Zygote's privileges. Each
350 // descriptor (if any) is closed via dup2(), replacing it with a valid
351 // (open) descriptor to /dev/null.
353 static void DetachDescriptors(JNIEnv* env, jintArray fdsToClose) {
357 jsize count = env->GetArrayLength(fdsToClose);
358 jint *ar = env->GetIntArrayElements(fdsToClose, 0);
360 ALOGE("Bad fd array");
365 for (i = 0; i < count; i++) {
366 devnull = open("/dev/null", O_RDWR);
368 ALOGE("Failed to open /dev/null");
372 ALOGV("Switching descriptor %d to /dev/null: %d", ar[i], errno);
373 if (dup2(devnull, ar[i]) < 0) {
374 ALOGE("Failed dup2() on descriptor %d", ar[i]);
381 void SetThreadName(const char* thread_name) {
384 const char* s = thread_name;
388 } else if (*s == '@') {
393 const int len = s - thread_name;
394 if (len < 15 || hasAt || !hasDot) {
397 s = thread_name + len - 15;
399 // pthread_setname_np fails rather than truncating long strings.
400 char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic
401 strlcpy(buf, s, sizeof(buf)-1);
402 errno = pthread_setname_np(pthread_self(), buf);
404 ALOGW("Unable to set the name of current thread to '%s'", buf);
408 // Temporary timing check.
411 clock_gettime(CLOCK_MONOTONIC, &now);
412 return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000) + now.tv_nsec / UINT64_C(1000000);
416 void ckTime(uint64_t start, const char* where) {
417 uint64_t now = MsTime();
418 if ((now-start) > 1000) {
419 // If we are taking more than a second, log about it.
420 ALOGW("Slow operation: %"PRIu64" ms in %s", (uint64_t)(now-start), where);
424 // Utility routine to fork zygote and specialize the child process.
425 static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids,
426 jint debug_flags, jobjectArray javaRlimits,
427 jlong permittedCapabilities, jlong effectiveCapabilities,
429 jstring java_se_info, jstring java_se_name,
430 bool is_system_server, jintArray fdsToClose,
431 jstring instructionSet, jstring dataDir) {
432 uint64_t start = MsTime();
434 ckTime(start, "ForkAndSpecializeCommon:SetSigChldHandler");
439 // The child process.
440 gMallocLeakZygoteChild = 1;
443 // Clean up any descriptors which must be closed immediately
444 DetachDescriptors(env, fdsToClose);
446 ckTime(start, "ForkAndSpecializeCommon:Fork and detach");
448 // Keep capabilities across UID change, unless we're staying root.
450 EnableKeepCapabilities(env);
453 DropCapabilitiesBoundingSet(env);
455 bool use_native_bridge = !is_system_server && (instructionSet != NULL)
456 && android::NativeBridgeAvailable();
457 if (use_native_bridge) {
458 ScopedUtfChars isa_string(env, instructionSet);
459 use_native_bridge = android::NeedsNativeBridge(isa_string.c_str());
461 if (use_native_bridge && dataDir == NULL) {
462 // dataDir should never be null if we need to use a native bridge.
463 // In general, dataDir will never be null for normal applications. It can only happen in
464 // special cases (for isolated processes which are not associated with any app). These are
465 // launched by the framework and should not be emulated anyway.
466 use_native_bridge = false;
467 ALOGW("Native bridge will not be used because dataDir == NULL.");
470 if (!MountEmulatedStorage(uid, mount_external, use_native_bridge)) {
471 ALOGW("Failed to mount emulated storage: %s", strerror(errno));
472 if (errno == ENOTCONN || errno == EROFS) {
473 // When device is actively encrypting, we get ENOTCONN here
474 // since FUSE was mounted before the framework restarted.
475 // When encrypted device is booting, we get EROFS since
476 // FUSE hasn't been created yet by init.
477 // In either case, continue without external storage.
479 ALOGE("Cannot continue without emulated storage");
484 if (!is_system_server) {
485 int rc = createProcessGroup(uid, getpid());
488 ALOGW("createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?");
490 ALOGE("createProcessGroup(%d, %d) failed: %s", uid, pid, strerror(-rc));
495 SetGids(env, javaGids);
497 SetRLimits(env, javaRlimits);
499 if (use_native_bridge) {
500 ScopedUtfChars isa_string(env, instructionSet);
501 ScopedUtfChars data_dir(env, dataDir);
502 android::PreInitializeNativeBridge(data_dir.c_str(), isa_string.c_str());
505 int rc = setresgid(gid, gid, gid);
507 ALOGE("setresgid(%d) failed", gid);
511 rc = setresuid(uid, uid, uid);
513 ALOGE("setresuid(%d) failed", uid);
517 if (NeedsNoRandomizeWorkaround()) {
518 // Work around ARM kernel ASLR lossage (http://b/5817320).
519 int old_personality = personality(0xffffffff);
520 int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE);
521 if (new_personality == -1) {
522 ALOGW("personality(%d) failed", new_personality);
526 SetCapabilities(env, permittedCapabilities, effectiveCapabilities);
528 SetSchedulerPolicy(env);
530 const char* se_info_c_str = NULL;
531 ScopedUtfChars* se_info = NULL;
532 if (java_se_info != NULL) {
533 se_info = new ScopedUtfChars(env, java_se_info);
534 se_info_c_str = se_info->c_str();
535 if (se_info_c_str == NULL) {
536 ALOGE("se_info_c_str == NULL");
540 const char* se_name_c_str = NULL;
541 ScopedUtfChars* se_name = NULL;
542 if (java_se_name != NULL) {
543 se_name = new ScopedUtfChars(env, java_se_name);
544 se_name_c_str = se_name->c_str();
545 if (se_name_c_str == NULL) {
546 ALOGE("se_name_c_str == NULL");
550 rc = selinux_android_setcontext(uid, is_system_server, se_info_c_str, se_name_c_str);
552 ALOGE("selinux_android_setcontext(%d, %d, \"%s\", \"%s\") failed", uid,
553 is_system_server, se_info_c_str, se_name_c_str);
557 // Make it easier to debug audit logs by setting the main thread's name to the
558 // nice name rather than "app_process".
559 if (se_info_c_str == NULL && is_system_server) {
560 se_name_c_str = "system_server";
562 if (se_info_c_str != NULL) {
563 SetThreadName(se_name_c_str);
569 UnsetSigChldHandler();
571 ckTime(start, "ForkAndSpecializeCommon:child process setup");
573 env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, debug_flags,
574 is_system_server ? NULL : instructionSet);
575 ckTime(start, "ForkAndSpecializeCommon:PostForkChildHooks returns");
576 if (env->ExceptionCheck()) {
577 ALOGE("Error calling post fork hooks.");
580 } else if (pid > 0) {
581 // the parent process
585 } // anonymous namespace
589 static jint com_android_internal_os_Zygote_nativeForkAndSpecialize(
590 JNIEnv* env, jclass, jint uid, jint gid, jintArray gids,
591 jint debug_flags, jobjectArray rlimits,
592 jint mount_external, jstring se_info, jstring se_name,
593 jintArray fdsToClose, jstring instructionSet, jstring appDataDir) {
594 // Grant CAP_WAKE_ALARM to the Bluetooth process.
595 jlong capabilities = 0;
596 if (uid == AID_BLUETOOTH) {
597 capabilities |= (1LL << CAP_WAKE_ALARM);
600 return ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags,
601 rlimits, capabilities, capabilities, mount_external, se_info,
602 se_name, false, fdsToClose, instructionSet, appDataDir);
605 static jint com_android_internal_os_Zygote_nativeForkSystemServer(
606 JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
607 jint debug_flags, jobjectArray rlimits, jlong permittedCapabilities,
608 jlong effectiveCapabilities) {
609 pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids,
610 debug_flags, rlimits,
611 permittedCapabilities, effectiveCapabilities,
612 MOUNT_EXTERNAL_NONE, NULL, NULL, true, NULL,
615 // The zygote process checks whether the child process has died or not.
616 ALOGI("System server process %d has been created", pid);
617 gSystemServerPid = pid;
618 // There is a slight window that the system server process has crashed
619 // but it went unnoticed because we haven't published its pid yet. So
620 // we recheck here just to make sure that all is well.
622 if (waitpid(pid, &status, WNOHANG) == pid) {
623 ALOGE("System server process %d has died. Restarting Zygote!", pid);
630 static JNINativeMethod gMethods[] = {
631 { "nativeForkAndSpecialize",
632 "(II[II[[IILjava/lang/String;Ljava/lang/String;[ILjava/lang/String;Ljava/lang/String;)I",
633 (void *) com_android_internal_os_Zygote_nativeForkAndSpecialize },
634 { "nativeForkSystemServer", "(II[II[[IJJ)I",
635 (void *) com_android_internal_os_Zygote_nativeForkSystemServer }
638 int register_com_android_internal_os_Zygote(JNIEnv* env) {
639 gZygoteClass = (jclass) env->NewGlobalRef(env->FindClass(kZygoteClassName));
640 if (gZygoteClass == NULL) {
643 gCallPostForkChildHooks = env->GetStaticMethodID(gZygoteClass, "callPostForkChildHooks",
644 "(ILjava/lang/String;)V");
646 return AndroidRuntime::registerNativeMethods(env, "com/android/internal/os/Zygote",
647 gMethods, NELEM(gMethods));
649 } // namespace android