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>
47 #include "core_jni_helpers.h"
49 #include "ScopedLocalRef.h"
50 #include "ScopedPrimitiveArray.h"
51 #include "ScopedUtfChars.h"
53 #include "nativebridge/native_bridge.h"
57 using android::String8;
59 static pid_t gSystemServerPid = 0;
61 static const char kZygoteClassName[] = "com/android/internal/os/Zygote";
62 static jclass gZygoteClass;
63 static jmethodID gCallPostForkChildHooks;
65 // Must match values in com.android.internal.os.Zygote.
66 enum MountExternalKind {
67 MOUNT_EXTERNAL_NONE = 0,
68 MOUNT_EXTERNAL_SINGLEUSER = 1,
69 MOUNT_EXTERNAL_MULTIUSER = 2,
70 MOUNT_EXTERNAL_MULTIUSER_ALL = 3,
73 static void RuntimeAbort(JNIEnv* env) {
74 env->FatalError("RuntimeAbort");
77 // This signal handler is for zygote mode, since the zygote must reap its children
78 static void SigChldHandler(int /*signal_number*/) {
82 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
83 // Log process-death status that we care about. In general it is
84 // not safe to call LOG(...) from a signal handler because of
85 // possible reentrancy. However, we know a priori that the
86 // current implementation of LOG() is safe to call from a SIGCHLD
87 // handler in the zygote process. If the LOG() implementation
88 // changes its locking strategy or its use of syscalls within the
89 // lazy-init critical section, its use here may become unsafe.
90 if (WIFEXITED(status)) {
91 if (WEXITSTATUS(status)) {
92 ALOGI("Process %d exited cleanly (%d)", pid, WEXITSTATUS(status));
94 } else if (WIFSIGNALED(status)) {
95 if (WTERMSIG(status) != SIGKILL) {
96 ALOGI("Process %d exited due to signal (%d)", pid, WTERMSIG(status));
98 if (WCOREDUMP(status)) {
99 ALOGI("Process %d dumped core.", pid);
103 // If the just-crashed process is the system_server, bring down zygote
104 // so that it is restarted by init and system server will be restarted
106 if (pid == gSystemServerPid) {
107 ALOGE("Exit zygote because system server (%d) has terminated", pid);
108 kill(getpid(), SIGKILL);
112 // Note that we shouldn't consider ECHILD an error because
113 // the secondary zygote might have no children left to wait for.
114 if (pid < 0 && errno != ECHILD) {
115 ALOGW("Zygote SIGCHLD error in waitpid: %s", strerror(errno));
119 // Configures the SIGCHLD handler for the zygote process. This is configured
120 // very late, because earlier in the runtime we may fork() and exec()
121 // other processes, and we want to waitpid() for those rather than
122 // have them be harvested immediately.
124 // This ends up being called repeatedly before each fork(), but there's
125 // no real harm in that.
126 static void SetSigChldHandler() {
128 memset(&sa, 0, sizeof(sa));
129 sa.sa_handler = SigChldHandler;
131 int err = sigaction(SIGCHLD, &sa, NULL);
133 ALOGW("Error setting SIGCHLD handler: %s", strerror(errno));
137 // Sets the SIGCHLD handler back to default behavior in zygote children.
138 static void UnsetSigChldHandler() {
140 memset(&sa, 0, sizeof(sa));
141 sa.sa_handler = SIG_DFL;
143 int err = sigaction(SIGCHLD, &sa, NULL);
145 ALOGW("Error unsetting SIGCHLD handler: %s", strerror(errno));
149 // Calls POSIX setgroups() using the int[] object as an argument.
150 // A NULL argument is tolerated.
151 static void SetGids(JNIEnv* env, jintArray javaGids) {
152 if (javaGids == NULL) {
156 ScopedIntArrayRO gids(env, javaGids);
157 if (gids.get() == NULL) {
160 int rc = setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0]));
162 ALOGE("setgroups failed");
167 // Sets the resource limits via setrlimit(2) for the values in the
168 // two-dimensional array of integers that's passed in. The second dimension
169 // contains a tuple of length 3: (resource, rlim_cur, rlim_max). NULL is
170 // treated as an empty array.
171 static void SetRLimits(JNIEnv* env, jobjectArray javaRlimits) {
172 if (javaRlimits == NULL) {
177 memset(&rlim, 0, sizeof(rlim));
179 for (int i = 0; i < env->GetArrayLength(javaRlimits); ++i) {
180 ScopedLocalRef<jobject> javaRlimitObject(env, env->GetObjectArrayElement(javaRlimits, i));
181 ScopedIntArrayRO javaRlimit(env, reinterpret_cast<jintArray>(javaRlimitObject.get()));
182 if (javaRlimit.size() != 3) {
183 ALOGE("rlimits array must have a second dimension of size 3");
187 rlim.rlim_cur = javaRlimit[1];
188 rlim.rlim_max = javaRlimit[2];
190 int rc = setrlimit(javaRlimit[0], &rlim);
192 ALOGE("setrlimit(%d, {%ld, %ld}) failed", javaRlimit[0], rlim.rlim_cur,
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(%" PRId64 ", %" PRId64 ") 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(): %s", strerror(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: %s", source, target, strerror(errno));
302 // Only mount user-specific external storage
303 if (TEMP_FAILURE_RETRY(mount(source_user.string(), target_user.string(), NULL,
304 MS_BIND, NULL)) == -1) {
305 ALOGW("Failed to mount %s to %s: %s", source_user.string(), target_user.string(),
311 if (fs_prepare_dir(legacy, 0000, 0, 0) == -1) {
315 // Finally, mount user-specific path into place for legacy users
316 if (TEMP_FAILURE_RETRY(
317 mount(target_user.string(), legacy, NULL, MS_BIND | MS_REC, NULL)) == -1) {
318 ALOGW("Failed to mount %s to %s: %s", target_user.string(), legacy, strerror(errno));
322 ALOGW("Mount mode %d unsupported", mount_mode);
329 static bool NeedsNoRandomizeWorkaround() {
330 #if !defined(__arm__)
336 if (uname(&uts) == -1) {
340 if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) {
344 // Kernels before 3.4.* need the workaround.
345 return (major < 3) || ((major == 3) && (minor < 4));
349 // Utility to close down the Zygote socket file descriptors while
350 // the child is still running as root with Zygote's privileges. Each
351 // descriptor (if any) is closed via dup2(), replacing it with a valid
352 // (open) descriptor to /dev/null.
354 static void DetachDescriptors(JNIEnv* env, jintArray fdsToClose) {
358 jsize count = env->GetArrayLength(fdsToClose);
359 jint *ar = env->GetIntArrayElements(fdsToClose, 0);
361 ALOGE("Bad fd array");
366 for (i = 0; i < count; i++) {
367 devnull = open("/dev/null", O_RDWR);
369 ALOGE("Failed to open /dev/null: %s", strerror(errno));
373 ALOGV("Switching descriptor %d to /dev/null: %s", ar[i], strerror(errno));
374 if (dup2(devnull, ar[i]) < 0) {
375 ALOGE("Failed dup2() on descriptor %d: %s", ar[i], strerror(errno));
382 void SetThreadName(const char* thread_name) {
385 const char* s = thread_name;
389 } else if (*s == '@') {
394 const int len = s - thread_name;
395 if (len < 15 || hasAt || !hasDot) {
398 s = thread_name + len - 15;
400 // pthread_setname_np fails rather than truncating long strings.
401 char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic
402 strlcpy(buf, s, sizeof(buf)-1);
403 errno = pthread_setname_np(pthread_self(), buf);
405 ALOGW("Unable to set the name of current thread to '%s': %s", buf, strerror(errno));
409 // Utility routine to fork zygote and specialize the child process.
410 static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids,
411 jint debug_flags, jobjectArray javaRlimits,
412 jlong permittedCapabilities, jlong effectiveCapabilities,
414 jstring java_se_info, jstring java_se_name,
415 bool is_system_server, jintArray fdsToClose,
416 jstring instructionSet, jstring dataDir) {
422 // The child process.
423 gMallocLeakZygoteChild = 1;
425 // Clean up any descriptors which must be closed immediately
426 DetachDescriptors(env, fdsToClose);
428 // Keep capabilities across UID change, unless we're staying root.
430 EnableKeepCapabilities(env);
433 DropCapabilitiesBoundingSet(env);
435 bool use_native_bridge = !is_system_server && (instructionSet != NULL)
436 && android::NativeBridgeAvailable();
437 if (use_native_bridge) {
438 ScopedUtfChars isa_string(env, instructionSet);
439 use_native_bridge = android::NeedsNativeBridge(isa_string.c_str());
441 if (use_native_bridge && dataDir == NULL) {
442 // dataDir should never be null if we need to use a native bridge.
443 // In general, dataDir will never be null for normal applications. It can only happen in
444 // special cases (for isolated processes which are not associated with any app). These are
445 // launched by the framework and should not be emulated anyway.
446 use_native_bridge = false;
447 ALOGW("Native bridge will not be used because dataDir == NULL.");
450 if (!MountEmulatedStorage(uid, mount_external, use_native_bridge)) {
451 ALOGW("Failed to mount emulated storage: %s", strerror(errno));
452 if (errno == ENOTCONN || errno == EROFS) {
453 // When device is actively encrypting, we get ENOTCONN here
454 // since FUSE was mounted before the framework restarted.
455 // When encrypted device is booting, we get EROFS since
456 // FUSE hasn't been created yet by init.
457 // In either case, continue without external storage.
459 ALOGE("Cannot continue without emulated storage");
464 if (!is_system_server) {
465 int rc = createProcessGroup(uid, getpid());
468 ALOGW("createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?");
470 ALOGE("createProcessGroup(%d, %d) failed: %s", uid, pid, strerror(-rc));
475 SetGids(env, javaGids);
477 SetRLimits(env, javaRlimits);
479 if (use_native_bridge) {
480 ScopedUtfChars isa_string(env, instructionSet);
481 ScopedUtfChars data_dir(env, dataDir);
482 android::PreInitializeNativeBridge(data_dir.c_str(), isa_string.c_str());
485 int rc = setresgid(gid, gid, gid);
487 ALOGE("setresgid(%d) failed: %s", gid, strerror(errno));
491 rc = setresuid(uid, uid, uid);
493 ALOGE("setresuid(%d) failed: %s", uid, strerror(errno));
497 if (NeedsNoRandomizeWorkaround()) {
498 // Work around ARM kernel ASLR lossage (http://b/5817320).
499 int old_personality = personality(0xffffffff);
500 int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE);
501 if (new_personality == -1) {
502 ALOGW("personality(%d) failed: %s", new_personality, strerror(errno));
506 SetCapabilities(env, permittedCapabilities, effectiveCapabilities);
508 SetSchedulerPolicy(env);
510 const char* se_info_c_str = NULL;
511 ScopedUtfChars* se_info = NULL;
512 if (java_se_info != NULL) {
513 se_info = new ScopedUtfChars(env, java_se_info);
514 se_info_c_str = se_info->c_str();
515 if (se_info_c_str == NULL) {
516 ALOGE("se_info_c_str == NULL");
520 const char* se_name_c_str = NULL;
521 ScopedUtfChars* se_name = NULL;
522 if (java_se_name != NULL) {
523 se_name = new ScopedUtfChars(env, java_se_name);
524 se_name_c_str = se_name->c_str();
525 if (se_name_c_str == NULL) {
526 ALOGE("se_name_c_str == NULL");
530 rc = selinux_android_setcontext(uid, is_system_server, se_info_c_str, se_name_c_str);
532 ALOGE("selinux_android_setcontext(%d, %d, \"%s\", \"%s\") failed", uid,
533 is_system_server, se_info_c_str, se_name_c_str);
537 // Make it easier to debug audit logs by setting the main thread's name to the
538 // nice name rather than "app_process".
539 if (se_info_c_str == NULL && is_system_server) {
540 se_name_c_str = "system_server";
542 if (se_info_c_str != NULL) {
543 SetThreadName(se_name_c_str);
549 UnsetSigChldHandler();
551 env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, debug_flags,
552 is_system_server ? NULL : instructionSet);
553 if (env->ExceptionCheck()) {
554 ALOGE("Error calling post fork hooks.");
557 } else if (pid > 0) {
558 // the parent process
562 } // anonymous namespace
566 static jint com_android_internal_os_Zygote_nativeForkAndSpecialize(
567 JNIEnv* env, jclass, jint uid, jint gid, jintArray gids,
568 jint debug_flags, jobjectArray rlimits,
569 jint mount_external, jstring se_info, jstring se_name,
570 jintArray fdsToClose, jstring instructionSet, jstring appDataDir) {
571 // Grant CAP_WAKE_ALARM to the Bluetooth process.
572 jlong capabilities = 0;
573 if (uid == AID_BLUETOOTH) {
574 capabilities |= (1LL << CAP_WAKE_ALARM);
577 return ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags,
578 rlimits, capabilities, capabilities, mount_external, se_info,
579 se_name, false, fdsToClose, instructionSet, appDataDir);
582 static jint com_android_internal_os_Zygote_nativeForkSystemServer(
583 JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
584 jint debug_flags, jobjectArray rlimits, jlong permittedCapabilities,
585 jlong effectiveCapabilities) {
586 pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids,
587 debug_flags, rlimits,
588 permittedCapabilities, effectiveCapabilities,
589 MOUNT_EXTERNAL_NONE, NULL, NULL, true, NULL,
592 // The zygote process checks whether the child process has died or not.
593 ALOGI("System server process %d has been created", pid);
594 gSystemServerPid = pid;
595 // There is a slight window that the system server process has crashed
596 // but it went unnoticed because we haven't published its pid yet. So
597 // we recheck here just to make sure that all is well.
599 if (waitpid(pid, &status, WNOHANG) == pid) {
600 ALOGE("System server process %d has died. Restarting Zygote!", pid);
607 static JNINativeMethod gMethods[] = {
608 { "nativeForkAndSpecialize",
609 "(II[II[[IILjava/lang/String;Ljava/lang/String;[ILjava/lang/String;Ljava/lang/String;)I",
610 (void *) com_android_internal_os_Zygote_nativeForkAndSpecialize },
611 { "nativeForkSystemServer", "(II[II[[IJJ)I",
612 (void *) com_android_internal_os_Zygote_nativeForkSystemServer }
615 int register_com_android_internal_os_Zygote(JNIEnv* env) {
616 gZygoteClass = MakeGlobalRefOrDie(env, FindClassOrDie(env, kZygoteClassName));
617 gCallPostForkChildHooks = GetStaticMethodIDOrDie(env, gZygoteClass, "callPostForkChildHooks",
618 "(ILjava/lang/String;)V");
620 return RegisterMethodsOrDie(env, "com/android/internal/os/Zygote", gMethods, NELEM(gMethods));
622 } // namespace android