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>
33 #include <sys/capability.h>
34 #include <sys/personality.h>
35 #include <sys/prctl.h>
36 #include <sys/resource.h>
38 #include <sys/types.h>
39 #include <sys/utsname.h>
43 #include <cutils/fs.h>
44 #include <cutils/multiuser.h>
45 #include <cutils/sched_policy.h>
46 #include <private/android_filesystem_config.h>
47 #include <utils/String8.h>
48 #include <selinux/android.h>
49 #include <processgroup/processgroup.h>
51 #include "core_jni_helpers.h"
53 #include "ScopedLocalRef.h"
54 #include "ScopedPrimitiveArray.h"
55 #include "ScopedUtfChars.h"
57 #include "nativebridge/native_bridge.h"
61 using android::String8;
63 static pid_t gSystemServerPid = 0;
65 static const char kZygoteClassName[] = "com/android/internal/os/Zygote";
66 static jclass gZygoteClass;
67 static jmethodID gCallPostForkChildHooks;
69 // Must match values in com.android.internal.os.Zygote.
70 enum MountExternalKind {
71 MOUNT_EXTERNAL_NONE = 0,
72 MOUNT_EXTERNAL_DEFAULT = 1,
73 MOUNT_EXTERNAL_READ = 2,
74 MOUNT_EXTERNAL_WRITE = 3,
77 static void RuntimeAbort(JNIEnv* env) {
78 env->FatalError("RuntimeAbort");
81 // This signal handler is for zygote mode, since the zygote must reap its children
82 static void SigChldHandler(int /*signal_number*/) {
86 // It's necessary to save and restore the errno during this function.
87 // Since errno is stored per thread, changing it here modifies the errno
88 // on the thread on which this signal handler executes. If a signal occurs
89 // between a call and an errno check, it's possible to get the errno set
91 // See b/23572286 for extra information.
92 int saved_errno = errno;
94 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
95 // Log process-death status that we care about. In general it is
96 // not safe to call LOG(...) from a signal handler because of
97 // possible reentrancy. However, we know a priori that the
98 // current implementation of LOG() is safe to call from a SIGCHLD
99 // handler in the zygote process. If the LOG() implementation
100 // changes its locking strategy or its use of syscalls within the
101 // lazy-init critical section, its use here may become unsafe.
102 if (WIFEXITED(status)) {
103 if (WEXITSTATUS(status)) {
104 ALOGI("Process %d exited cleanly (%d)", pid, WEXITSTATUS(status));
106 } else if (WIFSIGNALED(status)) {
107 if (WTERMSIG(status) != SIGKILL) {
108 ALOGI("Process %d exited due to signal (%d)", pid, WTERMSIG(status));
110 if (WCOREDUMP(status)) {
111 ALOGI("Process %d dumped core.", pid);
115 // If the just-crashed process is the system_server, bring down zygote
116 // so that it is restarted by init and system server will be restarted
118 if (pid == gSystemServerPid) {
119 ALOGE("Exit zygote because system server (%d) has terminated", pid);
120 kill(getpid(), SIGKILL);
124 // Note that we shouldn't consider ECHILD an error because
125 // the secondary zygote might have no children left to wait for.
126 if (pid < 0 && errno != ECHILD) {
127 ALOGW("Zygote SIGCHLD error in waitpid: %s", strerror(errno));
133 // Configures the SIGCHLD handler for the zygote process. This is configured
134 // very late, because earlier in the runtime we may fork() and exec()
135 // other processes, and we want to waitpid() for those rather than
136 // have them be harvested immediately.
138 // This ends up being called repeatedly before each fork(), but there's
139 // no real harm in that.
140 static void SetSigChldHandler() {
142 memset(&sa, 0, sizeof(sa));
143 sa.sa_handler = SigChldHandler;
145 int err = sigaction(SIGCHLD, &sa, NULL);
147 ALOGW("Error setting SIGCHLD handler: %s", strerror(errno));
151 // Sets the SIGCHLD handler back to default behavior in zygote children.
152 static void UnsetSigChldHandler() {
154 memset(&sa, 0, sizeof(sa));
155 sa.sa_handler = SIG_DFL;
157 int err = sigaction(SIGCHLD, &sa, NULL);
159 ALOGW("Error unsetting SIGCHLD handler: %s", strerror(errno));
163 // Calls POSIX setgroups() using the int[] object as an argument.
164 // A NULL argument is tolerated.
165 static void SetGids(JNIEnv* env, jintArray javaGids) {
166 if (javaGids == NULL) {
170 ScopedIntArrayRO gids(env, javaGids);
171 if (gids.get() == NULL) {
174 int rc = setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0]));
176 ALOGE("setgroups failed");
181 // Sets the resource limits via setrlimit(2) for the values in the
182 // two-dimensional array of integers that's passed in. The second dimension
183 // contains a tuple of length 3: (resource, rlim_cur, rlim_max). NULL is
184 // treated as an empty array.
185 static void SetRLimits(JNIEnv* env, jobjectArray javaRlimits) {
186 if (javaRlimits == NULL) {
191 memset(&rlim, 0, sizeof(rlim));
193 for (int i = 0; i < env->GetArrayLength(javaRlimits); ++i) {
194 ScopedLocalRef<jobject> javaRlimitObject(env, env->GetObjectArrayElement(javaRlimits, i));
195 ScopedIntArrayRO javaRlimit(env, reinterpret_cast<jintArray>(javaRlimitObject.get()));
196 if (javaRlimit.size() != 3) {
197 ALOGE("rlimits array must have a second dimension of size 3");
201 rlim.rlim_cur = javaRlimit[1];
202 rlim.rlim_max = javaRlimit[2];
204 int rc = setrlimit(javaRlimit[0], &rlim);
206 ALOGE("setrlimit(%d, {%ld, %ld}) failed", javaRlimit[0], rlim.rlim_cur,
213 // The debug malloc library needs to know whether it's the zygote or a child.
214 extern "C" int gMallocLeakZygoteChild;
216 static void EnableKeepCapabilities(JNIEnv* env) {
217 int rc = prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
219 ALOGE("prctl(PR_SET_KEEPCAPS) failed");
224 static void DropCapabilitiesBoundingSet(JNIEnv* env) {
225 for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
226 int rc = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
228 if (errno == EINVAL) {
229 ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify "
230 "your kernel is compiled with file capabilities support");
232 ALOGE("prctl(PR_CAPBSET_DROP) failed");
239 static void SetCapabilities(JNIEnv* env, int64_t permitted, int64_t effective) {
240 __user_cap_header_struct capheader;
241 memset(&capheader, 0, sizeof(capheader));
242 capheader.version = _LINUX_CAPABILITY_VERSION_3;
245 __user_cap_data_struct capdata[2];
246 memset(&capdata, 0, sizeof(capdata));
247 capdata[0].effective = effective;
248 capdata[1].effective = effective >> 32;
249 capdata[0].permitted = permitted;
250 capdata[1].permitted = permitted >> 32;
252 if (capset(&capheader, &capdata[0]) == -1) {
253 ALOGE("capset(%" PRId64 ", %" PRId64 ") failed", permitted, effective);
258 static void SetSchedulerPolicy(JNIEnv* env) {
259 errno = -set_sched_policy(0, SP_DEFAULT);
261 ALOGE("set_sched_policy(0, SP_DEFAULT) failed");
266 static int UnmountTree(const char* path) {
267 size_t path_len = strlen(path);
269 FILE* fp = setmntent("/proc/mounts", "r");
271 ALOGE("Error opening /proc/mounts: %s", strerror(errno));
275 // Some volumes can be stacked on each other, so force unmount in
276 // reverse order to give us the best chance of success.
277 std::list<std::string> toUnmount;
279 while ((mentry = getmntent(fp)) != NULL) {
280 if (strncmp(mentry->mnt_dir, path, path_len) == 0) {
281 toUnmount.push_front(std::string(mentry->mnt_dir));
286 for (auto path : toUnmount) {
287 if (umount2(path.c_str(), MNT_DETACH)) {
288 ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno));
294 // Create a private mount namespace and bind mount appropriate emulated
295 // storage for the given user.
296 static bool MountEmulatedStorage(uid_t uid, jint mount_mode,
297 bool force_mount_namespace) {
298 // See storage config details at http://source.android.com/tech/storage/
300 // Create a second private mount namespace for our process
301 if (unshare(CLONE_NEWNS) == -1) {
302 ALOGW("Failed to unshare(): %s", strerror(errno));
306 // Unmount storage provided by root namespace and mount requested view
307 UnmountTree("/storage");
309 String8 storageSource;
310 if (mount_mode == MOUNT_EXTERNAL_DEFAULT) {
311 storageSource = "/mnt/runtime/default";
312 } else if (mount_mode == MOUNT_EXTERNAL_READ) {
313 storageSource = "/mnt/runtime/read";
314 } else if (mount_mode == MOUNT_EXTERNAL_WRITE) {
315 storageSource = "/mnt/runtime/write";
317 // Sane default of no storage visible
320 if (TEMP_FAILURE_RETRY(mount(storageSource.string(), "/storage",
321 NULL, MS_BIND | MS_REC | MS_SLAVE, NULL)) == -1) {
322 ALOGW("Failed to mount %s to /storage: %s", storageSource.string(), strerror(errno));
326 // Mount user-specific symlink helper into place
327 userid_t user_id = multiuser_get_user_id(uid);
328 const String8 userSource(String8::format("/mnt/user/%d", user_id));
329 if (fs_prepare_dir(userSource.string(), 0751, 0, 0) == -1) {
332 if (TEMP_FAILURE_RETRY(mount(userSource.string(), "/storage/self",
333 NULL, MS_BIND, NULL)) == -1) {
334 ALOGW("Failed to mount %s to /storage/self: %s", userSource.string(), strerror(errno));
341 static bool NeedsNoRandomizeWorkaround() {
342 #if !defined(__arm__)
348 if (uname(&uts) == -1) {
352 if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) {
356 // Kernels before 3.4.* need the workaround.
357 return (major < 3) || ((major == 3) && (minor < 4));
361 // Utility to close down the Zygote socket file descriptors while
362 // the child is still running as root with Zygote's privileges. Each
363 // descriptor (if any) is closed via dup2(), replacing it with a valid
364 // (open) descriptor to /dev/null.
366 static void DetachDescriptors(JNIEnv* env, jintArray fdsToClose) {
370 jsize count = env->GetArrayLength(fdsToClose);
371 jint *ar = env->GetIntArrayElements(fdsToClose, 0);
373 ALOGE("Bad fd array");
378 for (i = 0; i < count; i++) {
379 devnull = open("/dev/null", O_RDWR);
381 ALOGE("Failed to open /dev/null: %s", strerror(errno));
385 ALOGV("Switching descriptor %d to /dev/null: %s", ar[i], strerror(errno));
386 if (dup2(devnull, ar[i]) < 0) {
387 ALOGE("Failed dup2() on descriptor %d: %s", ar[i], strerror(errno));
394 void SetThreadName(const char* thread_name) {
397 const char* s = thread_name;
401 } else if (*s == '@') {
406 const int len = s - thread_name;
407 if (len < 15 || hasAt || !hasDot) {
410 s = thread_name + len - 15;
412 // pthread_setname_np fails rather than truncating long strings.
413 char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic
414 strlcpy(buf, s, sizeof(buf)-1);
415 errno = pthread_setname_np(pthread_self(), buf);
417 ALOGW("Unable to set the name of current thread to '%s': %s", buf, strerror(errno));
421 #ifdef ENABLE_SCHED_BOOST
422 static void SetForkLoad(bool boost) {
423 // set scheduler knob to boost forked processes
424 pid_t currentPid = getpid();
425 // fits at most "/proc/XXXXXXX/sched_init_task_load\0"
427 snprintf(schedPath, sizeof(schedPath), "/proc/%u/sched_init_task_load", currentPid);
428 int schedBoostFile = open(schedPath, O_WRONLY);
429 if (schedBoostFile < 0) {
430 ALOGW("Unable to set zygote scheduler boost");
434 write(schedBoostFile, "100\0", 4);
436 write(schedBoostFile, "0\0", 2);
438 close(schedBoostFile);
442 // Utility routine to fork zygote and specialize the child process.
443 static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids,
444 jint debug_flags, jobjectArray javaRlimits,
445 jlong permittedCapabilities, jlong effectiveCapabilities,
447 jstring java_se_info, jstring java_se_name,
448 bool is_system_server, jintArray fdsToClose,
449 jstring instructionSet, jstring dataDir) {
452 #ifdef ENABLE_SCHED_BOOST
459 // The child process.
460 gMallocLeakZygoteChild = 1;
462 // Clean up any descriptors which must be closed immediately
463 DetachDescriptors(env, fdsToClose);
465 // Keep capabilities across UID change, unless we're staying root.
467 EnableKeepCapabilities(env);
470 DropCapabilitiesBoundingSet(env);
471 #ifdef _COMPATIBILITY_ENHANCEMENT_PACKAGE_
472 bool use_native_bridge = !is_system_server && android::NativeBridgeAvailable();
474 bool use_native_bridge = !is_system_server && (instructionSet != NULL)
475 && android::NativeBridgeAvailable();
477 if (use_native_bridge) {
478 #ifdef _COMPATIBILITY_ENHANCEMENT_PACKAGE_
479 if (instructionSet != NULL) {
480 ScopedUtfChars isa_string(env, instructionSet);
481 use_native_bridge = android::NeedsNativeBridge(isa_string.c_str());
483 use_native_bridge = android::NeedsNativeBridge(NULL);
484 instructionSet = env->NewStringUTF("arm"
491 ScopedUtfChars isa_string(env, instructionSet);
492 use_native_bridge = android::NeedsNativeBridge(isa_string.c_str());
496 #ifndef _COMPATIBILITY_ENHANCEMENT_PACKAGE_
497 if (use_native_bridge && dataDir == NULL) {
498 // dataDir should never be null if we need to use a native bridge.
499 // In general, dataDir will never be null for normal applications. It can only happen in
500 // special cases (for isolated processes which are not associated with any app). These are
501 // launched by the framework and should not be emulated anyway.
502 use_native_bridge = false;
503 ALOGW("Native bridge will not be used because dataDir == NULL.");
507 if (!MountEmulatedStorage(uid, mount_external, use_native_bridge)) {
508 ALOGW("Failed to mount emulated storage: %s", strerror(errno));
509 if (errno == ENOTCONN || errno == EROFS) {
510 // When device is actively encrypting, we get ENOTCONN here
511 // since FUSE was mounted before the framework restarted.
512 // When encrypted device is booting, we get EROFS since
513 // FUSE hasn't been created yet by init.
514 // In either case, continue without external storage.
516 ALOGE("Cannot continue without emulated storage");
521 if (!is_system_server) {
522 int rc = createProcessGroup(uid, getpid());
525 ALOGW("createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?");
527 ALOGE("createProcessGroup(%d, %d) failed: %s", uid, pid, strerror(-rc));
532 SetGids(env, javaGids);
534 SetRLimits(env, javaRlimits);
536 if (use_native_bridge) {
537 ScopedUtfChars isa_string(env, instructionSet);
538 #ifdef _COMPATIBILITY_ENHANCEMENT_PACKAGE_
539 if (dataDir != NULL) {
541 ScopedUtfChars data_dir(env, dataDir);
542 android::PreInitializeNativeBridge(data_dir.c_str(), isa_string.c_str());
543 #ifdef _COMPATIBILITY_ENHANCEMENT_PACKAGE_
545 android::PreInitializeNativeBridge(NULL, isa_string.c_str());
550 int rc = setresgid(gid, gid, gid);
552 ALOGE("setresgid(%d) failed: %s", gid, strerror(errno));
556 rc = setresuid(uid, uid, uid);
558 ALOGE("setresuid(%d) failed: %s", uid, strerror(errno));
562 if (NeedsNoRandomizeWorkaround()) {
563 // Work around ARM kernel ASLR lossage (http://b/5817320).
564 int old_personality = personality(0xffffffff);
565 int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE);
566 if (new_personality == -1) {
567 ALOGW("personality(%d) failed: %s", new_personality, strerror(errno));
571 SetCapabilities(env, permittedCapabilities, effectiveCapabilities);
573 SetSchedulerPolicy(env);
575 const char* se_info_c_str = NULL;
576 ScopedUtfChars* se_info = NULL;
577 if (java_se_info != NULL) {
578 se_info = new ScopedUtfChars(env, java_se_info);
579 se_info_c_str = se_info->c_str();
580 if (se_info_c_str == NULL) {
581 ALOGE("se_info_c_str == NULL");
585 const char* se_name_c_str = NULL;
586 ScopedUtfChars* se_name = NULL;
587 if (java_se_name != NULL) {
588 se_name = new ScopedUtfChars(env, java_se_name);
589 se_name_c_str = se_name->c_str();
590 if (se_name_c_str == NULL) {
591 ALOGE("se_name_c_str == NULL");
595 rc = selinux_android_setcontext(uid, is_system_server, se_info_c_str, se_name_c_str);
597 ALOGE("selinux_android_setcontext(%d, %d, \"%s\", \"%s\") failed", uid,
598 is_system_server, se_info_c_str, se_name_c_str);
602 // Make it easier to debug audit logs by setting the main thread's name to the
603 // nice name rather than "app_process".
604 if (se_info_c_str == NULL && is_system_server) {
605 se_name_c_str = "system_server";
607 if (se_info_c_str != NULL) {
608 SetThreadName(se_name_c_str);
614 UnsetSigChldHandler();
616 env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, debug_flags,
617 is_system_server ? NULL : instructionSet);
618 if (env->ExceptionCheck()) {
619 ALOGE("Error calling post fork hooks.");
622 } else if (pid > 0) {
623 // the parent process
625 #ifdef ENABLE_SCHED_BOOST
626 // unset scheduler knob
633 } // anonymous namespace
637 static jint com_android_internal_os_Zygote_nativeForkAndSpecialize(
638 JNIEnv* env, jclass, jint uid, jint gid, jintArray gids,
639 jint debug_flags, jobjectArray rlimits,
640 jint mount_external, jstring se_info, jstring se_name,
641 jintArray fdsToClose, jstring instructionSet, jstring appDataDir) {
642 // Grant CAP_WAKE_ALARM to the Bluetooth process.
643 jlong capabilities = 0;
644 if (uid == AID_BLUETOOTH) {
645 capabilities |= (1LL << CAP_WAKE_ALARM);
648 return ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags,
649 rlimits, capabilities, capabilities, mount_external, se_info,
650 se_name, false, fdsToClose, instructionSet, appDataDir);
653 static jint com_android_internal_os_Zygote_nativeForkSystemServer(
654 JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
655 jint debug_flags, jobjectArray rlimits, jlong permittedCapabilities,
656 jlong effectiveCapabilities) {
657 pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids,
658 debug_flags, rlimits,
659 permittedCapabilities, effectiveCapabilities,
660 MOUNT_EXTERNAL_DEFAULT, NULL, NULL, true, NULL,
663 // The zygote process checks whether the child process has died or not.
664 ALOGI("System server process %d has been created", pid);
665 gSystemServerPid = pid;
666 // There is a slight window that the system server process has crashed
667 // but it went unnoticed because we haven't published its pid yet. So
668 // we recheck here just to make sure that all is well.
670 if (waitpid(pid, &status, WNOHANG) == pid) {
671 ALOGE("System server process %d has died. Restarting Zygote!", pid);
678 static JNINativeMethod gMethods[] = {
679 { "nativeForkAndSpecialize",
680 "(II[II[[IILjava/lang/String;Ljava/lang/String;[ILjava/lang/String;Ljava/lang/String;)I",
681 (void *) com_android_internal_os_Zygote_nativeForkAndSpecialize },
682 { "nativeForkSystemServer", "(II[II[[IJJ)I",
683 (void *) com_android_internal_os_Zygote_nativeForkSystemServer }
686 int register_com_android_internal_os_Zygote(JNIEnv* env) {
687 gZygoteClass = MakeGlobalRefOrDie(env, FindClassOrDie(env, kZygoteClassName));
688 gCallPostForkChildHooks = GetStaticMethodIDOrDie(env, gZygoteClass, "callPostForkChildHooks",
689 "(ILjava/lang/String;)V");
691 return RegisterMethodsOrDie(env, "com/android/internal/os/Zygote", gMethods, NELEM(gMethods));
693 } // namespace android