com_android_internal_os_Zygote.cpp revision 6d747cacae4f3c39d9360b62aca4ecf01b180727
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 <malloc.h>
31#include <mntent.h>
32#include <paths.h>
33#include <signal.h>
34#include <stdlib.h>
35#include <sys/capability.h>
36#include <sys/cdefs.h>
37#include <sys/personality.h>
38#include <sys/prctl.h>
39#include <sys/resource.h>
40#include <sys/stat.h>
41#include <sys/time.h>
42#include <sys/types.h>
43#include <sys/utsname.h>
44#include <sys/wait.h>
45#include <unistd.h>
46
47#include "android-base/logging.h"
48#include <cutils/fs.h>
49#include <cutils/multiuser.h>
50#include <cutils/sched_policy.h>
51#include <private/android_filesystem_config.h>
52#include <utils/String8.h>
53#include <selinux/android.h>
54#include <processgroup/processgroup.h>
55
56#include "core_jni_helpers.h"
57#include "JNIHelp.h"
58#include "ScopedLocalRef.h"
59#include "ScopedPrimitiveArray.h"
60#include "ScopedUtfChars.h"
61#include "fd_utils.h"
62
63#include "nativebridge/native_bridge.h"
64
65namespace {
66
67using android::String8;
68
69static pid_t gSystemServerPid = 0;
70
71static const char kZygoteClassName[] = "com/android/internal/os/Zygote";
72static jclass gZygoteClass;
73static jmethodID gCallPostForkChildHooks;
74
75// Must match values in com.android.internal.os.Zygote.
76enum MountExternalKind {
77  MOUNT_EXTERNAL_NONE = 0,
78  MOUNT_EXTERNAL_DEFAULT = 1,
79  MOUNT_EXTERNAL_READ = 2,
80  MOUNT_EXTERNAL_WRITE = 3,
81};
82
83static void RuntimeAbort(JNIEnv* env, int line, const char* msg) {
84  std::ostringstream oss;
85  oss << __FILE__ << ":" << line << ": " << msg;
86  env->FatalError(oss.str().c_str());
87}
88
89// This signal handler is for zygote mode, since the zygote must reap its children
90static void SigChldHandler(int /*signal_number*/) {
91  pid_t pid;
92  int status;
93
94  // It's necessary to save and restore the errno during this function.
95  // Since errno is stored per thread, changing it here modifies the errno
96  // on the thread on which this signal handler executes. If a signal occurs
97  // between a call and an errno check, it's possible to get the errno set
98  // here.
99  // See b/23572286 for extra information.
100  int saved_errno = errno;
101
102  while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
103     // Log process-death status that we care about.  In general it is
104     // not safe to call LOG(...) from a signal handler because of
105     // possible reentrancy.  However, we know a priori that the
106     // current implementation of LOG() is safe to call from a SIGCHLD
107     // handler in the zygote process.  If the LOG() implementation
108     // changes its locking strategy or its use of syscalls within the
109     // lazy-init critical section, its use here may become unsafe.
110    if (WIFEXITED(status)) {
111      ALOGI("Process %d exited cleanly (%d)", pid, WEXITSTATUS(status));
112    } else if (WIFSIGNALED(status)) {
113      ALOGI("Process %d exited due to signal (%d)", pid, WTERMSIG(status));
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    std::ostringstream oss;
181    oss << "setgroups failed: " << strerror(errno) << ", gids.size=" << gids.size();
182    RuntimeAbort(env, __LINE__, oss.str().c_str());
183  }
184}
185
186// Sets the resource limits via setrlimit(2) for the values in the
187// two-dimensional array of integers that's passed in. The second dimension
188// contains a tuple of length 3: (resource, rlim_cur, rlim_max). NULL is
189// treated as an empty array.
190static void SetRLimits(JNIEnv* env, jobjectArray javaRlimits) {
191  if (javaRlimits == NULL) {
192    return;
193  }
194
195  rlimit rlim;
196  memset(&rlim, 0, sizeof(rlim));
197
198  for (int i = 0; i < env->GetArrayLength(javaRlimits); ++i) {
199    ScopedLocalRef<jobject> javaRlimitObject(env, env->GetObjectArrayElement(javaRlimits, i));
200    ScopedIntArrayRO javaRlimit(env, reinterpret_cast<jintArray>(javaRlimitObject.get()));
201    if (javaRlimit.size() != 3) {
202      RuntimeAbort(env, __LINE__, "rlimits array must have a second dimension of size 3");
203    }
204
205    rlim.rlim_cur = javaRlimit[1];
206    rlim.rlim_max = javaRlimit[2];
207
208    int rc = setrlimit(javaRlimit[0], &rlim);
209    if (rc == -1) {
210      ALOGE("setrlimit(%d, {%ld, %ld}) failed", javaRlimit[0], rlim.rlim_cur,
211            rlim.rlim_max);
212      RuntimeAbort(env, __LINE__, "setrlimit failed");
213    }
214  }
215}
216
217// The debug malloc library needs to know whether it's the zygote or a child.
218extern "C" int gMallocLeakZygoteChild;
219
220static void PreApplicationInit() {
221  // The child process sets this to indicate it's not the zygote.
222  gMallocLeakZygoteChild = 1;
223
224  // Set the jemalloc decay time to 1.
225  mallopt(M_DECAY_TIME, 1);
226}
227
228static void EnableKeepCapabilities(JNIEnv* env) {
229  int rc = prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
230  if (rc == -1) {
231    RuntimeAbort(env, __LINE__, "prctl(PR_SET_KEEPCAPS) failed");
232  }
233}
234
235static void DropCapabilitiesBoundingSet(JNIEnv* env) {
236  for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
237    int rc = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
238    if (rc == -1) {
239      if (errno == EINVAL) {
240        ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify "
241              "your kernel is compiled with file capabilities support");
242      } else {
243        ALOGE("prctl(PR_CAPBSET_DROP, %d) failed: %s", i, strerror(errno));
244        RuntimeAbort(env, __LINE__, "prctl(PR_CAPBSET_DROP) failed");
245      }
246    }
247  }
248}
249
250static void SetInheritable(JNIEnv* env, uint64_t inheritable) {
251  __user_cap_header_struct capheader;
252  memset(&capheader, 0, sizeof(capheader));
253  capheader.version = _LINUX_CAPABILITY_VERSION_3;
254  capheader.pid = 0;
255
256  __user_cap_data_struct capdata[2];
257  if (capget(&capheader, &capdata[0]) == -1) {
258    ALOGE("capget failed: %s", strerror(errno));
259    RuntimeAbort(env, __LINE__, "capget failed");
260  }
261
262  capdata[0].inheritable = inheritable;
263  capdata[1].inheritable = inheritable >> 32;
264
265  if (capset(&capheader, &capdata[0]) == -1) {
266    ALOGE("capset(inh=%" PRIx64 ") failed: %s", inheritable, strerror(errno));
267    RuntimeAbort(env, __LINE__, "capset failed");
268  }
269}
270
271static void SetCapabilities(JNIEnv* env, uint64_t permitted, uint64_t effective,
272                            uint64_t inheritable) {
273  __user_cap_header_struct capheader;
274  memset(&capheader, 0, sizeof(capheader));
275  capheader.version = _LINUX_CAPABILITY_VERSION_3;
276  capheader.pid = 0;
277
278  __user_cap_data_struct capdata[2];
279  memset(&capdata, 0, sizeof(capdata));
280  capdata[0].effective = effective;
281  capdata[1].effective = effective >> 32;
282  capdata[0].permitted = permitted;
283  capdata[1].permitted = permitted >> 32;
284  capdata[0].inheritable = inheritable;
285  capdata[1].inheritable = inheritable >> 32;
286
287  if (capset(&capheader, &capdata[0]) == -1) {
288    ALOGE("capset(perm=%" PRIx64 ", eff=%" PRIx64 ", inh=%" PRIx64 ") failed: %s", permitted,
289          effective, inheritable, strerror(errno));
290    RuntimeAbort(env, __LINE__, "capset failed");
291  }
292}
293
294static void SetSchedulerPolicy(JNIEnv* env) {
295  errno = -set_sched_policy(0, SP_DEFAULT);
296  if (errno != 0) {
297    ALOGE("set_sched_policy(0, SP_DEFAULT) failed");
298    RuntimeAbort(env, __LINE__, "set_sched_policy(0, SP_DEFAULT) failed");
299  }
300}
301
302static int UnmountTree(const char* path) {
303    size_t path_len = strlen(path);
304
305    FILE* fp = setmntent("/proc/mounts", "r");
306    if (fp == NULL) {
307        ALOGE("Error opening /proc/mounts: %s", strerror(errno));
308        return -errno;
309    }
310
311    // Some volumes can be stacked on each other, so force unmount in
312    // reverse order to give us the best chance of success.
313    std::list<std::string> toUnmount;
314    mntent* mentry;
315    while ((mentry = getmntent(fp)) != NULL) {
316        if (strncmp(mentry->mnt_dir, path, path_len) == 0) {
317            toUnmount.push_front(std::string(mentry->mnt_dir));
318        }
319    }
320    endmntent(fp);
321
322    for (auto path : toUnmount) {
323        if (umount2(path.c_str(), MNT_DETACH)) {
324            ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno));
325        }
326    }
327    return 0;
328}
329
330// Create a private mount namespace and bind mount appropriate emulated
331// storage for the given user.
332static bool MountEmulatedStorage(uid_t uid, jint mount_mode,
333        bool force_mount_namespace) {
334    // See storage config details at http://source.android.com/tech/storage/
335
336    String8 storageSource;
337    if (mount_mode == MOUNT_EXTERNAL_DEFAULT) {
338        storageSource = "/mnt/runtime/default";
339    } else if (mount_mode == MOUNT_EXTERNAL_READ) {
340        storageSource = "/mnt/runtime/read";
341    } else if (mount_mode == MOUNT_EXTERNAL_WRITE) {
342        storageSource = "/mnt/runtime/write";
343    } else if (!force_mount_namespace) {
344        // Sane default of no storage visible
345        return true;
346    }
347
348    // Create a second private mount namespace for our process
349    if (unshare(CLONE_NEWNS) == -1) {
350        ALOGW("Failed to unshare(): %s", strerror(errno));
351        return false;
352    }
353
354    // Handle force_mount_namespace with MOUNT_EXTERNAL_NONE.
355    if (mount_mode == MOUNT_EXTERNAL_NONE) {
356        return true;
357    }
358
359    if (TEMP_FAILURE_RETRY(mount(storageSource.string(), "/storage",
360            NULL, MS_BIND | MS_REC | MS_SLAVE, NULL)) == -1) {
361        ALOGW("Failed to mount %s to /storage: %s", storageSource.string(), strerror(errno));
362        return false;
363    }
364
365    // Mount user-specific symlink helper into place
366    userid_t user_id = multiuser_get_user_id(uid);
367    const String8 userSource(String8::format("/mnt/user/%d", user_id));
368    if (fs_prepare_dir(userSource.string(), 0751, 0, 0) == -1) {
369        return false;
370    }
371    if (TEMP_FAILURE_RETRY(mount(userSource.string(), "/storage/self",
372            NULL, MS_BIND, NULL)) == -1) {
373        ALOGW("Failed to mount %s to /storage/self: %s", userSource.string(), strerror(errno));
374        return false;
375    }
376
377    return true;
378}
379
380static bool NeedsNoRandomizeWorkaround() {
381#if !defined(__arm__)
382    return false;
383#else
384    int major;
385    int minor;
386    struct utsname uts;
387    if (uname(&uts) == -1) {
388        return false;
389    }
390
391    if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) {
392        return false;
393    }
394
395    // Kernels before 3.4.* need the workaround.
396    return (major < 3) || ((major == 3) && (minor < 4));
397#endif
398}
399
400// Utility to close down the Zygote socket file descriptors while
401// the child is still running as root with Zygote's privileges.  Each
402// descriptor (if any) is closed via dup2(), replacing it with a valid
403// (open) descriptor to /dev/null.
404
405static void DetachDescriptors(JNIEnv* env, jintArray fdsToClose) {
406  if (!fdsToClose) {
407    return;
408  }
409  jsize count = env->GetArrayLength(fdsToClose);
410  ScopedIntArrayRO ar(env, fdsToClose);
411  if (ar.get() == NULL) {
412      RuntimeAbort(env, __LINE__, "Bad fd array");
413  }
414  jsize i;
415  int devnull;
416  for (i = 0; i < count; i++) {
417    devnull = open("/dev/null", O_RDWR);
418    if (devnull < 0) {
419      ALOGE("Failed to open /dev/null: %s", strerror(errno));
420      RuntimeAbort(env, __LINE__, "Failed to open /dev/null");
421      continue;
422    }
423    ALOGV("Switching descriptor %d to /dev/null: %s", ar[i], strerror(errno));
424    if (dup2(devnull, ar[i]) < 0) {
425      ALOGE("Failed dup2() on descriptor %d: %s", ar[i], strerror(errno));
426      RuntimeAbort(env, __LINE__, "Failed dup2()");
427    }
428    close(devnull);
429  }
430}
431
432void SetThreadName(const char* thread_name) {
433  bool hasAt = false;
434  bool hasDot = false;
435  const char* s = thread_name;
436  while (*s) {
437    if (*s == '.') {
438      hasDot = true;
439    } else if (*s == '@') {
440      hasAt = true;
441    }
442    s++;
443  }
444  const int len = s - thread_name;
445  if (len < 15 || hasAt || !hasDot) {
446    s = thread_name;
447  } else {
448    s = thread_name + len - 15;
449  }
450  // pthread_setname_np fails rather than truncating long strings.
451  char buf[16];       // MAX_TASK_COMM_LEN=16 is hard-coded into bionic
452  strlcpy(buf, s, sizeof(buf)-1);
453  errno = pthread_setname_np(pthread_self(), buf);
454  if (errno != 0) {
455    ALOGW("Unable to set the name of current thread to '%s': %s", buf, strerror(errno));
456  }
457}
458
459// The list of open zygote file descriptors.
460static FileDescriptorTable* gOpenFdTable = NULL;
461
462static void FillFileDescriptorVector(JNIEnv* env,
463                                     jintArray java_fds,
464                                     std::vector<int>* fds) {
465  CHECK(fds != nullptr);
466  if (java_fds != nullptr) {
467    ScopedIntArrayRO ar(env, java_fds);
468    if (ar.get() == nullptr) {
469      RuntimeAbort(env, __LINE__, "Bad fd array");
470    }
471    fds->reserve(ar.size());
472    for (size_t i = 0; i < ar.size(); ++i) {
473      fds->push_back(ar[i]);
474    }
475  }
476}
477
478// Utility routine to fork zygote and specialize the child process.
479static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids,
480                                     jint debug_flags, jobjectArray javaRlimits,
481                                     jlong permittedCapabilities, jlong effectiveCapabilities,
482                                     jint mount_external,
483                                     jstring java_se_info, jstring java_se_name,
484                                     bool is_system_server, jintArray fdsToClose,
485                                     jintArray fdsToIgnore,
486                                     jstring instructionSet, jstring dataDir) {
487  SetSigChldHandler();
488
489  sigset_t sigchld;
490  sigemptyset(&sigchld);
491  sigaddset(&sigchld, SIGCHLD);
492
493  // Temporarily block SIGCHLD during forks. The SIGCHLD handler might
494  // log, which would result in the logging FDs we close being reopened.
495  // This would cause failures because the FDs are not whitelisted.
496  //
497  // Note that the zygote process is single threaded at this point.
498  if (sigprocmask(SIG_BLOCK, &sigchld, nullptr) == -1) {
499    ALOGE("sigprocmask(SIG_SETMASK, { SIGCHLD }) failed: %s", strerror(errno));
500    RuntimeAbort(env, __LINE__, "Call to sigprocmask(SIG_BLOCK, { SIGCHLD }) failed.");
501  }
502
503  // Close any logging related FDs before we start evaluating the list of
504  // file descriptors.
505  __android_log_close();
506
507  // If this is the first fork for this zygote, create the open FD table.
508  // If it isn't, we just need to check whether the list of open files has
509  // changed (and it shouldn't in the normal case).
510  std::vector<int> fds_to_ignore;
511  FillFileDescriptorVector(env, fdsToIgnore, &fds_to_ignore);
512  if (gOpenFdTable == NULL) {
513    gOpenFdTable = FileDescriptorTable::Create(fds_to_ignore);
514    if (gOpenFdTable == NULL) {
515      RuntimeAbort(env, __LINE__, "Unable to construct file descriptor table.");
516    }
517  } else if (!gOpenFdTable->Restat(fds_to_ignore)) {
518    RuntimeAbort(env, __LINE__, "Unable to restat file descriptor table.");
519  }
520
521  pid_t pid = fork();
522
523  if (pid == 0) {
524    PreApplicationInit();
525
526    // Clean up any descriptors which must be closed immediately
527    DetachDescriptors(env, fdsToClose);
528
529    // Re-open all remaining open file descriptors so that they aren't shared
530    // with the zygote across a fork.
531    if (!gOpenFdTable->ReopenOrDetach()) {
532      RuntimeAbort(env, __LINE__, "Unable to reopen whitelisted descriptors.");
533    }
534
535    if (sigprocmask(SIG_UNBLOCK, &sigchld, nullptr) == -1) {
536      ALOGE("sigprocmask(SIG_SETMASK, { SIGCHLD }) failed: %s", strerror(errno));
537      RuntimeAbort(env, __LINE__, "Call to sigprocmask(SIG_UNBLOCK, { SIGCHLD }) failed.");
538    }
539
540    // Keep capabilities across UID change, unless we're staying root.
541    if (uid != 0) {
542      EnableKeepCapabilities(env);
543    }
544
545    SetInheritable(env, permittedCapabilities);
546    DropCapabilitiesBoundingSet(env);
547
548    bool use_native_bridge = !is_system_server && (instructionSet != NULL)
549        && android::NativeBridgeAvailable();
550    if (use_native_bridge) {
551      ScopedUtfChars isa_string(env, instructionSet);
552      use_native_bridge = android::NeedsNativeBridge(isa_string.c_str());
553    }
554    if (use_native_bridge && dataDir == NULL) {
555      // dataDir should never be null if we need to use a native bridge.
556      // In general, dataDir will never be null for normal applications. It can only happen in
557      // special cases (for isolated processes which are not associated with any app). These are
558      // launched by the framework and should not be emulated anyway.
559      use_native_bridge = false;
560      ALOGW("Native bridge will not be used because dataDir == NULL.");
561    }
562
563    if (!MountEmulatedStorage(uid, mount_external, use_native_bridge)) {
564      ALOGW("Failed to mount emulated storage: %s", strerror(errno));
565      if (errno == ENOTCONN || errno == EROFS) {
566        // When device is actively encrypting, we get ENOTCONN here
567        // since FUSE was mounted before the framework restarted.
568        // When encrypted device is booting, we get EROFS since
569        // FUSE hasn't been created yet by init.
570        // In either case, continue without external storage.
571      } else {
572        RuntimeAbort(env, __LINE__, "Cannot continue without emulated storage");
573      }
574    }
575
576    if (!is_system_server) {
577        int rc = createProcessGroup(uid, getpid());
578        if (rc != 0) {
579            if (rc == -EROFS) {
580                ALOGW("createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?");
581            } else {
582                ALOGE("createProcessGroup(%d, %d) failed: %s", uid, pid, strerror(-rc));
583            }
584        }
585    }
586
587    SetGids(env, javaGids);
588
589    SetRLimits(env, javaRlimits);
590
591    if (use_native_bridge) {
592      ScopedUtfChars isa_string(env, instructionSet);
593      ScopedUtfChars data_dir(env, dataDir);
594      android::PreInitializeNativeBridge(data_dir.c_str(), isa_string.c_str());
595    }
596
597    int rc = setresgid(gid, gid, gid);
598    if (rc == -1) {
599      ALOGE("setresgid(%d) failed: %s", gid, strerror(errno));
600      RuntimeAbort(env, __LINE__, "setresgid failed");
601    }
602
603    rc = setresuid(uid, uid, uid);
604    if (rc == -1) {
605      ALOGE("setresuid(%d) failed: %s", uid, strerror(errno));
606      RuntimeAbort(env, __LINE__, "setresuid failed");
607    }
608
609    if (NeedsNoRandomizeWorkaround()) {
610        // Work around ARM kernel ASLR lossage (http://b/5817320).
611        int old_personality = personality(0xffffffff);
612        int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE);
613        if (new_personality == -1) {
614            ALOGW("personality(%d) failed: %s", new_personality, strerror(errno));
615        }
616    }
617
618    SetCapabilities(env, permittedCapabilities, effectiveCapabilities, permittedCapabilities);
619
620    SetSchedulerPolicy(env);
621
622    const char* se_info_c_str = NULL;
623    ScopedUtfChars* se_info = NULL;
624    if (java_se_info != NULL) {
625        se_info = new ScopedUtfChars(env, java_se_info);
626        se_info_c_str = se_info->c_str();
627        if (se_info_c_str == NULL) {
628          RuntimeAbort(env, __LINE__, "se_info_c_str == NULL");
629        }
630    }
631    const char* se_name_c_str = NULL;
632    ScopedUtfChars* se_name = NULL;
633    if (java_se_name != NULL) {
634        se_name = new ScopedUtfChars(env, java_se_name);
635        se_name_c_str = se_name->c_str();
636        if (se_name_c_str == NULL) {
637          RuntimeAbort(env, __LINE__, "se_name_c_str == NULL");
638        }
639    }
640    rc = selinux_android_setcontext(uid, is_system_server, se_info_c_str, se_name_c_str);
641    if (rc == -1) {
642      ALOGE("selinux_android_setcontext(%d, %d, \"%s\", \"%s\") failed", uid,
643            is_system_server, se_info_c_str, se_name_c_str);
644      RuntimeAbort(env, __LINE__, "selinux_android_setcontext failed");
645    }
646
647    // Make it easier to debug audit logs by setting the main thread's name to the
648    // nice name rather than "app_process".
649    if (se_info_c_str == NULL && is_system_server) {
650      se_name_c_str = "system_server";
651    }
652    if (se_info_c_str != NULL) {
653      SetThreadName(se_name_c_str);
654    }
655
656    delete se_info;
657    delete se_name;
658
659    UnsetSigChldHandler();
660
661    env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, debug_flags,
662                              is_system_server, instructionSet);
663    if (env->ExceptionCheck()) {
664      RuntimeAbort(env, __LINE__, "Error calling post fork hooks.");
665    }
666  } else if (pid > 0) {
667    // the parent process
668
669    // We blocked SIGCHLD prior to a fork, we unblock it here.
670    if (sigprocmask(SIG_UNBLOCK, &sigchld, nullptr) == -1) {
671      ALOGE("sigprocmask(SIG_SETMASK, { SIGCHLD }) failed: %s", strerror(errno));
672      RuntimeAbort(env, __LINE__, "Call to sigprocmask(SIG_UNBLOCK, { SIGCHLD }) failed.");
673    }
674  }
675  return pid;
676}
677}  // anonymous namespace
678
679namespace android {
680
681static void com_android_internal_os_Zygote_nativePreApplicationInit(JNIEnv*, jclass) {
682  PreApplicationInit();
683}
684
685static jint com_android_internal_os_Zygote_nativeForkAndSpecialize(
686        JNIEnv* env, jclass, jint uid, jint gid, jintArray gids,
687        jint debug_flags, jobjectArray rlimits,
688        jint mount_external, jstring se_info, jstring se_name,
689        jintArray fdsToClose,
690        jintArray fdsToIgnore,
691        jstring instructionSet, jstring appDataDir) {
692    jlong capabilities = 0;
693
694    // Grant CAP_WAKE_ALARM to the Bluetooth process.
695    // Additionally, allow bluetooth to open packet sockets so it can start the DHCP client.
696    // Grant CAP_SYS_NICE to allow Bluetooth to set RT priority for
697    // audio-related threads.
698    // TODO: consider making such functionality an RPC to netd.
699    if (multiuser_get_app_id(uid) == AID_BLUETOOTH) {
700      capabilities |= (1LL << CAP_WAKE_ALARM);
701      capabilities |= (1LL << CAP_NET_RAW);
702      capabilities |= (1LL << CAP_NET_BIND_SERVICE);
703      capabilities |= (1LL << CAP_SYS_NICE);
704    }
705
706    // Grant CAP_BLOCK_SUSPEND to processes that belong to GID "wakelock"
707    bool gid_wakelock_found = false;
708    if (gid == AID_WAKELOCK) {
709      gid_wakelock_found = true;
710    } else if (gids != NULL) {
711      jsize gids_num = env->GetArrayLength(gids);
712      ScopedIntArrayRO ar(env, gids);
713      if (ar.get() == NULL) {
714        RuntimeAbort(env, __LINE__, "Bad gids array");
715      }
716      for (int i = 0; i < gids_num; i++) {
717        if (ar[i] == AID_WAKELOCK) {
718          gid_wakelock_found = true;
719          break;
720        }
721      }
722    }
723    if (gid_wakelock_found) {
724      capabilities |= (1LL << CAP_BLOCK_SUSPEND);
725    }
726
727    return ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags,
728            rlimits, capabilities, capabilities, mount_external, se_info,
729            se_name, false, fdsToClose, fdsToIgnore, instructionSet, appDataDir);
730}
731
732static jint com_android_internal_os_Zygote_nativeForkSystemServer(
733        JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
734        jint debug_flags, jobjectArray rlimits, jlong permittedCapabilities,
735        jlong effectiveCapabilities) {
736  pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids,
737                                      debug_flags, rlimits,
738                                      permittedCapabilities, effectiveCapabilities,
739                                      MOUNT_EXTERNAL_DEFAULT, NULL, NULL, true, NULL,
740                                      NULL, NULL, NULL);
741  if (pid > 0) {
742      // The zygote process checks whether the child process has died or not.
743      ALOGI("System server process %d has been created", pid);
744      gSystemServerPid = pid;
745      // There is a slight window that the system server process has crashed
746      // but it went unnoticed because we haven't published its pid yet. So
747      // we recheck here just to make sure that all is well.
748      int status;
749      if (waitpid(pid, &status, WNOHANG) == pid) {
750          ALOGE("System server process %d has died. Restarting Zygote!", pid);
751          RuntimeAbort(env, __LINE__, "System server process has died. Restarting Zygote!");
752      }
753  }
754  return pid;
755}
756
757static void com_android_internal_os_Zygote_nativeAllowFileAcrossFork(
758        JNIEnv* env, jclass, jstring path) {
759    ScopedUtfChars path_native(env, path);
760    const char* path_cstr = path_native.c_str();
761    if (!path_cstr) {
762        RuntimeAbort(env, __LINE__, "path_cstr == NULL");
763    }
764    FileDescriptorWhitelist::Get()->Allow(path_cstr);
765}
766
767static void com_android_internal_os_Zygote_nativeUnmountStorageOnInit(JNIEnv* env, jclass) {
768    // Zygote process unmount root storage space initially before every child processes are forked.
769    // Every forked child processes (include SystemServer) only mount their own root storage space
770    // and no need unmount storage operation in MountEmulatedStorage method.
771    // Zygote process does not utilize root storage spaces and unshares its mount namespace below.
772
773    // See storage config details at http://source.android.com/tech/storage/
774    // Create private mount namespace shared by all children
775    if (unshare(CLONE_NEWNS) == -1) {
776        RuntimeAbort(env, __LINE__, "Failed to unshare()");
777        return;
778    }
779
780    // Mark rootfs as being a slave so that changes from default
781    // namespace only flow into our children.
782    if (mount("rootfs", "/", nullptr, (MS_SLAVE | MS_REC), nullptr) == -1) {
783        RuntimeAbort(env, __LINE__, "Failed to mount() rootfs as MS_SLAVE");
784        return;
785    }
786
787    // Create a staging tmpfs that is shared by our children; they will
788    // bind mount storage into their respective private namespaces, which
789    // are isolated from each other.
790    const char* target_base = getenv("EMULATED_STORAGE_TARGET");
791    if (target_base != nullptr) {
792#define STRINGIFY_UID(x) __STRING(x)
793        if (mount("tmpfs", target_base, "tmpfs", MS_NOSUID | MS_NODEV,
794                  "uid=0,gid=" STRINGIFY_UID(AID_SDCARD_R) ",mode=0751") == -1) {
795            ALOGE("Failed to mount tmpfs to %s", target_base);
796            RuntimeAbort(env, __LINE__, "Failed to mount tmpfs");
797            return;
798        }
799#undef STRINGIFY_UID
800    }
801
802    UnmountTree("/storage");
803}
804
805static const JNINativeMethod gMethods[] = {
806    { "nativeForkAndSpecialize",
807      "(II[II[[IILjava/lang/String;Ljava/lang/String;[I[ILjava/lang/String;Ljava/lang/String;)I",
808      (void *) com_android_internal_os_Zygote_nativeForkAndSpecialize },
809    { "nativeForkSystemServer", "(II[II[[IJJ)I",
810      (void *) com_android_internal_os_Zygote_nativeForkSystemServer },
811    { "nativeAllowFileAcrossFork", "(Ljava/lang/String;)V",
812      (void *) com_android_internal_os_Zygote_nativeAllowFileAcrossFork },
813    { "nativeUnmountStorageOnInit", "()V",
814      (void *) com_android_internal_os_Zygote_nativeUnmountStorageOnInit },
815    { "nativePreApplicationInit", "()V",
816      (void *) com_android_internal_os_Zygote_nativePreApplicationInit }
817};
818
819int register_com_android_internal_os_Zygote(JNIEnv* env) {
820  gZygoteClass = MakeGlobalRefOrDie(env, FindClassOrDie(env, kZygoteClassName));
821  gCallPostForkChildHooks = GetStaticMethodIDOrDie(env, gZygoteClass, "callPostForkChildHooks",
822                                                   "(IZLjava/lang/String;)V");
823
824  return RegisterMethodsOrDie(env, "com/android/internal/os/Zygote", gMethods, NELEM(gMethods));
825}
826}  // namespace android
827