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