com_android_internal_os_Zygote.cpp revision dfcc79ee8ecd4166cba19be7493c6175cb0c65a9
1/* 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 17#define LOG_TAG "Zygote" 18 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> 21#include <linux/fs.h> 22 23#include <list> 24#include <sstream> 25#include <string> 26 27#include <fcntl.h> 28#include <grp.h> 29#include <inttypes.h> 30#include <mntent.h> 31#include <paths.h> 32#include <signal.h> 33#include <stdlib.h> 34#include <sys/capability.h> 35#include <sys/personality.h> 36#include <sys/prctl.h> 37#include <sys/resource.h> 38#include <sys/stat.h> 39#include <sys/types.h> 40#include <sys/utsname.h> 41#include <sys/wait.h> 42#include <unistd.h> 43 44#include <cutils/fs.h> 45#include <cutils/multiuser.h> 46#include <cutils/sched_policy.h> 47#include <private/android_filesystem_config.h> 48#include <utils/String8.h> 49#include <selinux/android.h> 50#include <processgroup/processgroup.h> 51 52#include "core_jni_helpers.h" 53#include "JNIHelp.h" 54#include "ScopedLocalRef.h" 55#include "ScopedPrimitiveArray.h" 56#include "ScopedUtfChars.h" 57#include "fd_utils-inl.h" 58 59#include "nativebridge/native_bridge.h" 60 61namespace { 62 63using android::String8; 64 65static pid_t gSystemServerPid = 0; 66 67static const char kZygoteClassName[] = "com/android/internal/os/Zygote"; 68static jclass gZygoteClass; 69static jmethodID gCallPostForkChildHooks; 70 71// Must match values in com.android.internal.os.Zygote. 72enum MountExternalKind { 73 MOUNT_EXTERNAL_NONE = 0, 74 MOUNT_EXTERNAL_DEFAULT = 1, 75 MOUNT_EXTERNAL_READ = 2, 76 MOUNT_EXTERNAL_WRITE = 3, 77}; 78 79static void RuntimeAbort(JNIEnv* env, int line, const char* msg) { 80 std::ostringstream oss; 81 oss << __FILE__ << ":" << line << ": " << msg; 82 env->FatalError(oss.str().c_str()); 83} 84 85// This signal handler is for zygote mode, since the zygote must reap its children 86static void SigChldHandler(int /*signal_number*/) { 87 pid_t pid; 88 int status; 89 90 // It's necessary to save and restore the errno during this function. 91 // Since errno is stored per thread, changing it here modifies the errno 92 // on the thread on which this signal handler executes. If a signal occurs 93 // between a call and an errno check, it's possible to get the errno set 94 // here. 95 // See b/23572286 for extra information. 96 int saved_errno = errno; 97 98 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { 99 // Log process-death status that we care about. In general it is 100 // not safe to call LOG(...) from a signal handler because of 101 // possible reentrancy. However, we know a priori that the 102 // current implementation of LOG() is safe to call from a SIGCHLD 103 // handler in the zygote process. If the LOG() implementation 104 // changes its locking strategy or its use of syscalls within the 105 // lazy-init critical section, its use here may become unsafe. 106 if (WIFEXITED(status)) { 107 if (WEXITSTATUS(status)) { 108 ALOGI("Process %d exited cleanly (%d)", pid, WEXITSTATUS(status)); 109 } 110 } else if (WIFSIGNALED(status)) { 111 if (WTERMSIG(status) != SIGKILL) { 112 ALOGI("Process %d exited due to signal (%d)", pid, WTERMSIG(status)); 113 } 114 if (WCOREDUMP(status)) { 115 ALOGI("Process %d dumped core.", pid); 116 } 117 } 118 119 // If the just-crashed process is the system_server, bring down zygote 120 // so that it is restarted by init and system server will be restarted 121 // from there. 122 if (pid == gSystemServerPid) { 123 ALOGE("Exit zygote because system server (%d) has terminated", pid); 124 kill(getpid(), SIGKILL); 125 } 126 } 127 128 // Note that we shouldn't consider ECHILD an error because 129 // the secondary zygote might have no children left to wait for. 130 if (pid < 0 && errno != ECHILD) { 131 ALOGW("Zygote SIGCHLD error in waitpid: %s", strerror(errno)); 132 } 133 134 errno = saved_errno; 135} 136 137// Configures the SIGCHLD handler for the zygote process. This is configured 138// very late, because earlier in the runtime we may fork() and exec() 139// other processes, and we want to waitpid() for those rather than 140// have them be harvested immediately. 141// 142// This ends up being called repeatedly before each fork(), but there's 143// no real harm in that. 144static void SetSigChldHandler() { 145 struct sigaction sa; 146 memset(&sa, 0, sizeof(sa)); 147 sa.sa_handler = SigChldHandler; 148 149 int err = sigaction(SIGCHLD, &sa, NULL); 150 if (err < 0) { 151 ALOGW("Error setting SIGCHLD handler: %s", strerror(errno)); 152 } 153} 154 155// Sets the SIGCHLD handler back to default behavior in zygote children. 156static void UnsetSigChldHandler() { 157 struct sigaction sa; 158 memset(&sa, 0, sizeof(sa)); 159 sa.sa_handler = SIG_DFL; 160 161 int err = sigaction(SIGCHLD, &sa, NULL); 162 if (err < 0) { 163 ALOGW("Error unsetting SIGCHLD handler: %s", strerror(errno)); 164 } 165} 166 167// Calls POSIX setgroups() using the int[] object as an argument. 168// A NULL argument is tolerated. 169static void SetGids(JNIEnv* env, jintArray javaGids) { 170 if (javaGids == NULL) { 171 return; 172 } 173 174 ScopedIntArrayRO gids(env, javaGids); 175 if (gids.get() == NULL) { 176 RuntimeAbort(env, __LINE__, "Getting gids int array failed"); 177 } 178 int rc = setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0])); 179 if (rc == -1) { 180 RuntimeAbort(env, __LINE__, "setgroups failed"); 181 } 182} 183 184// Sets the resource limits via setrlimit(2) for the values in the 185// two-dimensional array of integers that's passed in. The second dimension 186// contains a tuple of length 3: (resource, rlim_cur, rlim_max). NULL is 187// treated as an empty array. 188static void SetRLimits(JNIEnv* env, jobjectArray javaRlimits) { 189 if (javaRlimits == NULL) { 190 return; 191 } 192 193 rlimit rlim; 194 memset(&rlim, 0, sizeof(rlim)); 195 196 for (int i = 0; i < env->GetArrayLength(javaRlimits); ++i) { 197 ScopedLocalRef<jobject> javaRlimitObject(env, env->GetObjectArrayElement(javaRlimits, i)); 198 ScopedIntArrayRO javaRlimit(env, reinterpret_cast<jintArray>(javaRlimitObject.get())); 199 if (javaRlimit.size() != 3) { 200 RuntimeAbort(env, __LINE__, "rlimits array must have a second dimension of size 3"); 201 } 202 203 rlim.rlim_cur = javaRlimit[1]; 204 rlim.rlim_max = javaRlimit[2]; 205 206 int rc = setrlimit(javaRlimit[0], &rlim); 207 if (rc == -1) { 208 ALOGE("setrlimit(%d, {%ld, %ld}) failed", javaRlimit[0], rlim.rlim_cur, 209 rlim.rlim_max); 210 RuntimeAbort(env, __LINE__, "setrlimit failed"); 211 } 212 } 213} 214 215// The debug malloc library needs to know whether it's the zygote or a child. 216extern "C" int gMallocLeakZygoteChild; 217 218static void EnableKeepCapabilities(JNIEnv* env) { 219 int rc = prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0); 220 if (rc == -1) { 221 RuntimeAbort(env, __LINE__, "prctl(PR_SET_KEEPCAPS) failed"); 222 } 223} 224 225static void DropCapabilitiesBoundingSet(JNIEnv* env) { 226 for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) { 227 int rc = prctl(PR_CAPBSET_DROP, i, 0, 0, 0); 228 if (rc == -1) { 229 if (errno == EINVAL) { 230 ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify " 231 "your kernel is compiled with file capabilities support"); 232 } else { 233 RuntimeAbort(env, __LINE__, "prctl(PR_CAPBSET_DROP) failed"); 234 } 235 } 236 } 237} 238 239static 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; 243 capheader.pid = 0; 244 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; 251 252 if (capset(&capheader, &capdata[0]) == -1) { 253 ALOGE("capset(%" PRId64 ", %" PRId64 ") failed", permitted, effective); 254 RuntimeAbort(env, __LINE__, "capset failed"); 255 } 256} 257 258static void SetSchedulerPolicy(JNIEnv* env) { 259 errno = -set_sched_policy(0, SP_DEFAULT); 260 if (errno != 0) { 261 ALOGE("set_sched_policy(0, SP_DEFAULT) failed"); 262 RuntimeAbort(env, __LINE__, "set_sched_policy(0, SP_DEFAULT) failed"); 263 } 264} 265 266static int UnmountTree(const char* path) { 267 size_t path_len = strlen(path); 268 269 FILE* fp = setmntent("/proc/mounts", "r"); 270 if (fp == NULL) { 271 ALOGE("Error opening /proc/mounts: %s", strerror(errno)); 272 return -errno; 273 } 274 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; 278 mntent* mentry; 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)); 282 } 283 } 284 endmntent(fp); 285 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)); 289 } 290 } 291 return 0; 292} 293 294// Create a private mount namespace and bind mount appropriate emulated 295// storage for the given user. 296static 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/ 299 300 // Create a second private mount namespace for our process 301 if (unshare(CLONE_NEWNS) == -1) { 302 ALOGW("Failed to unshare(): %s", strerror(errno)); 303 return false; 304 } 305 306 String8 storageSource; 307 if (mount_mode == MOUNT_EXTERNAL_DEFAULT) { 308 storageSource = "/mnt/runtime/default"; 309 } else if (mount_mode == MOUNT_EXTERNAL_READ) { 310 storageSource = "/mnt/runtime/read"; 311 } else if (mount_mode == MOUNT_EXTERNAL_WRITE) { 312 storageSource = "/mnt/runtime/write"; 313 } else { 314 // Sane default of no storage visible 315 return true; 316 } 317 if (TEMP_FAILURE_RETRY(mount(storageSource.string(), "/storage", 318 NULL, MS_BIND | MS_REC | MS_SLAVE, NULL)) == -1) { 319 ALOGW("Failed to mount %s to /storage: %s", storageSource.string(), strerror(errno)); 320 return false; 321 } 322 323 // Mount user-specific symlink helper into place 324 userid_t user_id = multiuser_get_user_id(uid); 325 const String8 userSource(String8::format("/mnt/user/%d", user_id)); 326 if (fs_prepare_dir(userSource.string(), 0751, 0, 0) == -1) { 327 return false; 328 } 329 if (TEMP_FAILURE_RETRY(mount(userSource.string(), "/storage/self", 330 NULL, MS_BIND, NULL)) == -1) { 331 ALOGW("Failed to mount %s to /storage/self: %s", userSource.string(), strerror(errno)); 332 return false; 333 } 334 335 return true; 336} 337 338static bool NeedsNoRandomizeWorkaround() { 339#if !defined(__arm__) 340 return false; 341#else 342 int major; 343 int minor; 344 struct utsname uts; 345 if (uname(&uts) == -1) { 346 return false; 347 } 348 349 if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) { 350 return false; 351 } 352 353 // Kernels before 3.4.* need the workaround. 354 return (major < 3) || ((major == 3) && (minor < 4)); 355#endif 356} 357 358// Utility to close down the Zygote socket file descriptors while 359// the child is still running as root with Zygote's privileges. Each 360// descriptor (if any) is closed via dup2(), replacing it with a valid 361// (open) descriptor to /dev/null. 362 363static void DetachDescriptors(JNIEnv* env, jintArray fdsToClose) { 364 if (!fdsToClose) { 365 return; 366 } 367 jsize count = env->GetArrayLength(fdsToClose); 368 ScopedIntArrayRO ar(env, fdsToClose); 369 if (ar.get() == NULL) { 370 RuntimeAbort(env, __LINE__, "Bad fd array"); 371 } 372 jsize i; 373 int devnull; 374 for (i = 0; i < count; i++) { 375 devnull = open("/dev/null", O_RDWR); 376 if (devnull < 0) { 377 ALOGE("Failed to open /dev/null: %s", strerror(errno)); 378 RuntimeAbort(env, __LINE__, "Failed to open /dev/null"); 379 continue; 380 } 381 ALOGV("Switching descriptor %d to /dev/null: %s", ar[i], strerror(errno)); 382 if (dup2(devnull, ar[i]) < 0) { 383 ALOGE("Failed dup2() on descriptor %d: %s", ar[i], strerror(errno)); 384 RuntimeAbort(env, __LINE__, "Failed dup2()"); 385 } 386 close(devnull); 387 } 388} 389 390void SetThreadName(const char* thread_name) { 391 bool hasAt = false; 392 bool hasDot = false; 393 const char* s = thread_name; 394 while (*s) { 395 if (*s == '.') { 396 hasDot = true; 397 } else if (*s == '@') { 398 hasAt = true; 399 } 400 s++; 401 } 402 const int len = s - thread_name; 403 if (len < 15 || hasAt || !hasDot) { 404 s = thread_name; 405 } else { 406 s = thread_name + len - 15; 407 } 408 // pthread_setname_np fails rather than truncating long strings. 409 char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic 410 strlcpy(buf, s, sizeof(buf)-1); 411 errno = pthread_setname_np(pthread_self(), buf); 412 if (errno != 0) { 413 ALOGW("Unable to set the name of current thread to '%s': %s", buf, strerror(errno)); 414 } 415} 416 417#ifdef ENABLE_SCHED_BOOST 418static void SetForkLoad(bool boost) { 419 // set scheduler knob to boost forked processes 420 pid_t currentPid = getpid(); 421 // fits at most "/proc/XXXXXXX/sched_init_task_load\0" 422 char schedPath[35]; 423 snprintf(schedPath, sizeof(schedPath), "/proc/%u/sched_init_task_load", currentPid); 424 int schedBoostFile = open(schedPath, O_WRONLY); 425 if (schedBoostFile < 0) { 426 ALOGW("Unable to set zygote scheduler boost"); 427 return; 428 } 429 if (boost) { 430 write(schedBoostFile, "100\0", 4); 431 } else { 432 write(schedBoostFile, "0\0", 2); 433 } 434 close(schedBoostFile); 435} 436#endif 437 438// The list of open zygote file descriptors. 439static FileDescriptorTable* gOpenFdTable = NULL; 440 441// Utility routine to fork zygote and specialize the child process. 442static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids, 443 jint debug_flags, jobjectArray javaRlimits, 444 jlong permittedCapabilities, jlong effectiveCapabilities, 445 jint mount_external, 446 jstring java_se_info, jstring java_se_name, 447 bool is_system_server, jintArray fdsToClose, 448 jstring instructionSet, jstring dataDir) { 449 SetSigChldHandler(); 450 451#ifdef ENABLE_SCHED_BOOST 452 SetForkLoad(true); 453#endif 454 455 sigset_t sigchld; 456 sigemptyset(&sigchld); 457 sigaddset(&sigchld, SIGCHLD); 458 459 // Temporarily block SIGCHLD during forks. The SIGCHLD handler might 460 // log, which would result in the logging FDs we close being reopened. 461 // This would cause failures because the FDs are not whitelisted. 462 // 463 // Note that the zygote process is single threaded at this point. 464 if (sigprocmask(SIG_BLOCK, &sigchld, nullptr) == -1) { 465 ALOGE("sigprocmask(SIG_SETMASK, { SIGCHLD }) failed: %s", strerror(errno)); 466 RuntimeAbort(env, __LINE__, "Call to sigprocmask(SIG_BLOCK, { SIGCHLD }) failed."); 467 } 468 469 // Close any logging related FDs before we start evaluating the list of 470 // file descriptors. 471 __android_log_close(); 472 473 // If this is the first fork for this zygote, create the open FD table. 474 // If it isn't, we just need to check whether the list of open files has 475 // changed (and it shouldn't in the normal case). 476 if (gOpenFdTable == NULL) { 477 gOpenFdTable = FileDescriptorTable::Create(); 478 if (gOpenFdTable == NULL) { 479 RuntimeAbort(env, __LINE__, "Unable to construct file descriptor table."); 480 } 481 } else if (!gOpenFdTable->Restat()) { 482 RuntimeAbort(env, __LINE__, "Unable to restat file descriptor table."); 483 } 484 485 pid_t pid = fork(); 486 487 if (pid == 0) { 488 // The child process. 489 gMallocLeakZygoteChild = 1; 490 491 // Clean up any descriptors which must be closed immediately 492 DetachDescriptors(env, fdsToClose); 493 494 // Re-open all remaining open file descriptors so that they aren't shared 495 // with the zygote across a fork. 496 if (!gOpenFdTable->ReopenOrDetach()) { 497 RuntimeAbort(env, __LINE__, "Unable to reopen whitelisted descriptors."); 498 } 499 500 if (sigprocmask(SIG_UNBLOCK, &sigchld, nullptr) == -1) { 501 ALOGE("sigprocmask(SIG_SETMASK, { SIGCHLD }) failed: %s", strerror(errno)); 502 RuntimeAbort(env, __LINE__, "Call to sigprocmask(SIG_UNBLOCK, { SIGCHLD }) failed."); 503 } 504 505 // Keep capabilities across UID change, unless we're staying root. 506 if (uid != 0) { 507 EnableKeepCapabilities(env); 508 } 509 510 DropCapabilitiesBoundingSet(env); 511 512 bool use_native_bridge = !is_system_server && (instructionSet != NULL) 513 && android::NativeBridgeAvailable(); 514 if (use_native_bridge) { 515 ScopedUtfChars isa_string(env, instructionSet); 516 use_native_bridge = android::NeedsNativeBridge(isa_string.c_str()); 517 } 518 if (use_native_bridge && dataDir == NULL) { 519 // dataDir should never be null if we need to use a native bridge. 520 // In general, dataDir will never be null for normal applications. It can only happen in 521 // special cases (for isolated processes which are not associated with any app). These are 522 // launched by the framework and should not be emulated anyway. 523 use_native_bridge = false; 524 ALOGW("Native bridge will not be used because dataDir == NULL."); 525 } 526 527 if (!MountEmulatedStorage(uid, mount_external, use_native_bridge)) { 528 ALOGW("Failed to mount emulated storage: %s", strerror(errno)); 529 if (errno == ENOTCONN || errno == EROFS) { 530 // When device is actively encrypting, we get ENOTCONN here 531 // since FUSE was mounted before the framework restarted. 532 // When encrypted device is booting, we get EROFS since 533 // FUSE hasn't been created yet by init. 534 // In either case, continue without external storage. 535 } else { 536 RuntimeAbort(env, __LINE__, "Cannot continue without emulated storage"); 537 } 538 } 539 540 if (!is_system_server) { 541 int rc = createProcessGroup(uid, getpid()); 542 if (rc != 0) { 543 if (rc == -EROFS) { 544 ALOGW("createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?"); 545 } else { 546 ALOGE("createProcessGroup(%d, %d) failed: %s", uid, pid, strerror(-rc)); 547 } 548 } 549 } 550 551 SetGids(env, javaGids); 552 553 SetRLimits(env, javaRlimits); 554 555 if (use_native_bridge) { 556 ScopedUtfChars isa_string(env, instructionSet); 557 ScopedUtfChars data_dir(env, dataDir); 558 android::PreInitializeNativeBridge(data_dir.c_str(), isa_string.c_str()); 559 } 560 561 int rc = setresgid(gid, gid, gid); 562 if (rc == -1) { 563 ALOGE("setresgid(%d) failed: %s", gid, strerror(errno)); 564 RuntimeAbort(env, __LINE__, "setresgid failed"); 565 } 566 567 rc = setresuid(uid, uid, uid); 568 if (rc == -1) { 569 ALOGE("setresuid(%d) failed: %s", uid, strerror(errno)); 570 RuntimeAbort(env, __LINE__, "setresuid failed"); 571 } 572 573 if (NeedsNoRandomizeWorkaround()) { 574 // Work around ARM kernel ASLR lossage (http://b/5817320). 575 int old_personality = personality(0xffffffff); 576 int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE); 577 if (new_personality == -1) { 578 ALOGW("personality(%d) failed: %s", new_personality, strerror(errno)); 579 } 580 } 581 582 SetCapabilities(env, permittedCapabilities, effectiveCapabilities); 583 584 SetSchedulerPolicy(env); 585 586 const char* se_info_c_str = NULL; 587 ScopedUtfChars* se_info = NULL; 588 if (java_se_info != NULL) { 589 se_info = new ScopedUtfChars(env, java_se_info); 590 se_info_c_str = se_info->c_str(); 591 if (se_info_c_str == NULL) { 592 RuntimeAbort(env, __LINE__, "se_info_c_str == NULL"); 593 } 594 } 595 const char* se_name_c_str = NULL; 596 ScopedUtfChars* se_name = NULL; 597 if (java_se_name != NULL) { 598 se_name = new ScopedUtfChars(env, java_se_name); 599 se_name_c_str = se_name->c_str(); 600 if (se_name_c_str == NULL) { 601 RuntimeAbort(env, __LINE__, "se_name_c_str == NULL"); 602 } 603 } 604 rc = selinux_android_setcontext(uid, is_system_server, se_info_c_str, se_name_c_str); 605 if (rc == -1) { 606 ALOGE("selinux_android_setcontext(%d, %d, \"%s\", \"%s\") failed", uid, 607 is_system_server, se_info_c_str, se_name_c_str); 608 RuntimeAbort(env, __LINE__, "selinux_android_setcontext failed"); 609 } 610 611 // Make it easier to debug audit logs by setting the main thread's name to the 612 // nice name rather than "app_process". 613 if (se_info_c_str == NULL && is_system_server) { 614 se_name_c_str = "system_server"; 615 } 616 if (se_info_c_str != NULL) { 617 SetThreadName(se_name_c_str); 618 } 619 620 delete se_info; 621 delete se_name; 622 623 UnsetSigChldHandler(); 624 625 env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, debug_flags, 626 is_system_server, instructionSet); 627 if (env->ExceptionCheck()) { 628 RuntimeAbort(env, __LINE__, "Error calling post fork hooks."); 629 } 630 } else if (pid > 0) { 631 // the parent process 632 633#ifdef ENABLE_SCHED_BOOST 634 // unset scheduler knob 635 SetForkLoad(false); 636#endif 637 638 // We blocked SIGCHLD prior to a fork, we unblock it here. 639 if (sigprocmask(SIG_UNBLOCK, &sigchld, nullptr) == -1) { 640 ALOGE("sigprocmask(SIG_SETMASK, { SIGCHLD }) failed: %s", strerror(errno)); 641 RuntimeAbort(env, __LINE__, "Call to sigprocmask(SIG_UNBLOCK, { SIGCHLD }) failed."); 642 } 643 } 644 return pid; 645} 646} // anonymous namespace 647 648namespace android { 649 650static jint com_android_internal_os_Zygote_nativeForkAndSpecialize( 651 JNIEnv* env, jclass, jint uid, jint gid, jintArray gids, 652 jint debug_flags, jobjectArray rlimits, 653 jint mount_external, jstring se_info, jstring se_name, 654 jintArray fdsToClose, jstring instructionSet, jstring appDataDir) { 655 jlong capabilities = 0; 656 657 // Grant CAP_WAKE_ALARM to the Bluetooth process. 658 // Additionally, allow bluetooth to open packet sockets so it can start the DHCP client. 659 // TODO: consider making such functionality an RPC to netd. 660 if (multiuser_get_app_id(uid) == AID_BLUETOOTH) { 661 capabilities |= (1LL << CAP_WAKE_ALARM); 662 capabilities |= (1LL << CAP_NET_RAW); 663 capabilities |= (1LL << CAP_NET_BIND_SERVICE); 664 } 665 666 // Grant CAP_BLOCK_SUSPEND to processes that belong to GID "wakelock" 667 bool gid_wakelock_found = false; 668 if (gid == AID_WAKELOCK) { 669 gid_wakelock_found = true; 670 } else if (gids != NULL) { 671 jsize gids_num = env->GetArrayLength(gids); 672 ScopedIntArrayRO ar(env, gids); 673 if (ar.get() == NULL) { 674 RuntimeAbort(env, __LINE__, "Bad gids array"); 675 } 676 for (int i = 0; i < gids_num; i++) { 677 if (ar[i] == AID_WAKELOCK) { 678 gid_wakelock_found = true; 679 break; 680 } 681 } 682 } 683 if (gid_wakelock_found) { 684 capabilities |= (1LL << CAP_BLOCK_SUSPEND); 685 } 686 687 return ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags, 688 rlimits, capabilities, capabilities, mount_external, se_info, 689 se_name, false, fdsToClose, instructionSet, appDataDir); 690} 691 692static jint com_android_internal_os_Zygote_nativeForkSystemServer( 693 JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids, 694 jint debug_flags, jobjectArray rlimits, jlong permittedCapabilities, 695 jlong effectiveCapabilities) { 696 pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids, 697 debug_flags, rlimits, 698 permittedCapabilities, effectiveCapabilities, 699 MOUNT_EXTERNAL_DEFAULT, NULL, NULL, true, NULL, 700 NULL, NULL); 701 if (pid > 0) { 702 // The zygote process checks whether the child process has died or not. 703 ALOGI("System server process %d has been created", pid); 704 gSystemServerPid = pid; 705 // There is a slight window that the system server process has crashed 706 // but it went unnoticed because we haven't published its pid yet. So 707 // we recheck here just to make sure that all is well. 708 int status; 709 if (waitpid(pid, &status, WNOHANG) == pid) { 710 ALOGE("System server process %d has died. Restarting Zygote!", pid); 711 RuntimeAbort(env, __LINE__, "System server process has died. Restarting Zygote!"); 712 } 713 } 714 return pid; 715} 716 717static void com_android_internal_os_Zygote_nativeUnmountStorageOnInit(JNIEnv* env, jclass) { 718 // Zygote process unmount root storage space initially before every child processes are forked. 719 // Every forked child processes (include SystemServer) only mount their own root storage space 720 // And no need unmount storage operation in MountEmulatedStorage method. 721 // Zygote process does not utilize root storage spaces and unshared its mount namespace from the ART. 722 723 UnmountTree("/storage"); 724 return; 725} 726 727static const JNINativeMethod gMethods[] = { 728 { "nativeForkAndSpecialize", 729 "(II[II[[IILjava/lang/String;Ljava/lang/String;[ILjava/lang/String;Ljava/lang/String;)I", 730 (void *) com_android_internal_os_Zygote_nativeForkAndSpecialize }, 731 { "nativeForkSystemServer", "(II[II[[IJJ)I", 732 (void *) com_android_internal_os_Zygote_nativeForkSystemServer }, 733 { "nativeUnmountStorageOnInit", "()V", 734 (void *) com_android_internal_os_Zygote_nativeUnmountStorageOnInit } 735}; 736 737int register_com_android_internal_os_Zygote(JNIEnv* env) { 738 gZygoteClass = MakeGlobalRefOrDie(env, FindClassOrDie(env, kZygoteClassName)); 739 gCallPostForkChildHooks = GetStaticMethodIDOrDie(env, gZygoteClass, "callPostForkChildHooks", 740 "(IZLjava/lang/String;)V"); 741 742 return RegisterMethodsOrDie(env, "com/android/internal/os/Zygote", gMethods, NELEM(gMethods)); 743} 744} // namespace android 745 746