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