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 | |
| 23 | #include <grp.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 24 | #include <fcntl.h> |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 25 | #include <paths.h> |
| 26 | #include <signal.h> |
| 27 | #include <stdlib.h> |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 28 | #include <unistd.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 29 | #include <sys/capability.h> |
| 30 | #include <sys/personality.h> |
| 31 | #include <sys/prctl.h> |
| 32 | #include <sys/resource.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/utsname.h> |
| 36 | #include <sys/wait.h> |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 37 | |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 38 | |
| 39 | #include <cutils/fs.h> |
| 40 | #include <cutils/multiuser.h> |
| 41 | #include <cutils/sched_policy.h> |
Sharvil Nanavati | 4990e4f | 2014-06-29 17:06:52 -0700 | [diff] [blame] | 42 | #include <private/android_filesystem_config.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 43 | #include <utils/String8.h> |
| 44 | #include <selinux/android.h> |
Colin Cross | 0161bbc | 2014-06-03 13:26:58 -0700 | [diff] [blame] | 45 | #include <processgroup/processgroup.h> |
Anwar Ghuloum | 629dc18 | 2014-09-04 08:05:34 -0700 | [diff] [blame] | 46 | #include <inttypes.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 47 | |
| 48 | #include "android_runtime/AndroidRuntime.h" |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 49 | #include "JNIHelp.h" |
| 50 | #include "ScopedLocalRef.h" |
| 51 | #include "ScopedPrimitiveArray.h" |
| 52 | #include "ScopedUtfChars.h" |
| 53 | |
jgu21 | 2086787 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 54 | #include "nativebridge/native_bridge.h" |
| 55 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 56 | namespace { |
| 57 | |
| 58 | using android::String8; |
| 59 | |
| 60 | static pid_t gSystemServerPid = 0; |
| 61 | |
| 62 | static const char kZygoteClassName[] = "com/android/internal/os/Zygote"; |
| 63 | static jclass gZygoteClass; |
| 64 | static jmethodID gCallPostForkChildHooks; |
| 65 | |
| 66 | // Must match values in com.android.internal.os.Zygote. |
| 67 | enum MountExternalKind { |
| 68 | MOUNT_EXTERNAL_NONE = 0, |
| 69 | MOUNT_EXTERNAL_SINGLEUSER = 1, |
| 70 | MOUNT_EXTERNAL_MULTIUSER = 2, |
| 71 | MOUNT_EXTERNAL_MULTIUSER_ALL = 3, |
| 72 | }; |
| 73 | |
| 74 | static void RuntimeAbort(JNIEnv* env) { |
| 75 | env->FatalError("RuntimeAbort"); |
| 76 | } |
| 77 | |
| 78 | // This signal handler is for zygote mode, since the zygote must reap its children |
| 79 | static void SigChldHandler(int /*signal_number*/) { |
| 80 | pid_t pid; |
| 81 | int status; |
| 82 | |
| 83 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { |
| 84 | // Log process-death status that we care about. In general it is |
| 85 | // not safe to call LOG(...) from a signal handler because of |
| 86 | // possible reentrancy. However, we know a priori that the |
| 87 | // current implementation of LOG() is safe to call from a SIGCHLD |
| 88 | // handler in the zygote process. If the LOG() implementation |
| 89 | // changes its locking strategy or its use of syscalls within the |
| 90 | // lazy-init critical section, its use here may become unsafe. |
| 91 | if (WIFEXITED(status)) { |
| 92 | if (WEXITSTATUS(status)) { |
| 93 | ALOGI("Process %d exited cleanly (%d)", pid, WEXITSTATUS(status)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 94 | } |
| 95 | } else if (WIFSIGNALED(status)) { |
| 96 | if (WTERMSIG(status) != SIGKILL) { |
Narayan Kamath | 160992d | 2014-04-14 14:46:07 +0100 | [diff] [blame] | 97 | ALOGI("Process %d exited due to signal (%d)", pid, WTERMSIG(status)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 98 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 99 | if (WCOREDUMP(status)) { |
| 100 | ALOGI("Process %d dumped core.", pid); |
| 101 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // If the just-crashed process is the system_server, bring down zygote |
| 105 | // so that it is restarted by init and system server will be restarted |
| 106 | // from there. |
| 107 | if (pid == gSystemServerPid) { |
| 108 | ALOGE("Exit zygote because system server (%d) has terminated"); |
| 109 | kill(getpid(), SIGKILL); |
| 110 | } |
| 111 | } |
| 112 | |
Narayan Kamath | 160992d | 2014-04-14 14:46:07 +0100 | [diff] [blame] | 113 | // Note that we shouldn't consider ECHILD an error because |
| 114 | // the secondary zygote might have no children left to wait for. |
| 115 | if (pid < 0 && errno != ECHILD) { |
| 116 | ALOGW("Zygote SIGCHLD error in waitpid: %s", strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | // Configures the SIGCHLD handler for the zygote process. This is configured |
| 121 | // very late, because earlier in the runtime we may fork() and exec() |
| 122 | // other processes, and we want to waitpid() for those rather than |
| 123 | // have them be harvested immediately. |
| 124 | // |
| 125 | // This ends up being called repeatedly before each fork(), but there's |
| 126 | // no real harm in that. |
| 127 | static void SetSigChldHandler() { |
| 128 | struct sigaction sa; |
| 129 | memset(&sa, 0, sizeof(sa)); |
| 130 | sa.sa_handler = SigChldHandler; |
| 131 | |
| 132 | int err = sigaction(SIGCHLD, &sa, NULL); |
| 133 | if (err < 0) { |
| 134 | ALOGW("Error setting SIGCHLD handler: %d", errno); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Sets the SIGCHLD handler back to default behavior in zygote children. |
| 139 | static void UnsetSigChldHandler() { |
| 140 | struct sigaction sa; |
| 141 | memset(&sa, 0, sizeof(sa)); |
| 142 | sa.sa_handler = SIG_DFL; |
| 143 | |
| 144 | int err = sigaction(SIGCHLD, &sa, NULL); |
| 145 | if (err < 0) { |
| 146 | ALOGW("Error unsetting SIGCHLD handler: %d", errno); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // Calls POSIX setgroups() using the int[] object as an argument. |
| 151 | // A NULL argument is tolerated. |
| 152 | static void SetGids(JNIEnv* env, jintArray javaGids) { |
| 153 | if (javaGids == NULL) { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | ScopedIntArrayRO gids(env, javaGids); |
| 158 | if (gids.get() == NULL) { |
| 159 | RuntimeAbort(env); |
| 160 | } |
| 161 | int rc = setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0])); |
| 162 | if (rc == -1) { |
| 163 | ALOGE("setgroups failed"); |
| 164 | RuntimeAbort(env); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // Sets the resource limits via setrlimit(2) for the values in the |
| 169 | // two-dimensional array of integers that's passed in. The second dimension |
| 170 | // contains a tuple of length 3: (resource, rlim_cur, rlim_max). NULL is |
| 171 | // treated as an empty array. |
| 172 | static void SetRLimits(JNIEnv* env, jobjectArray javaRlimits) { |
| 173 | if (javaRlimits == NULL) { |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | rlimit rlim; |
| 178 | memset(&rlim, 0, sizeof(rlim)); |
| 179 | |
| 180 | for (int i = 0; i < env->GetArrayLength(javaRlimits); ++i) { |
| 181 | ScopedLocalRef<jobject> javaRlimitObject(env, env->GetObjectArrayElement(javaRlimits, i)); |
| 182 | ScopedIntArrayRO javaRlimit(env, reinterpret_cast<jintArray>(javaRlimitObject.get())); |
| 183 | if (javaRlimit.size() != 3) { |
| 184 | ALOGE("rlimits array must have a second dimension of size 3"); |
| 185 | RuntimeAbort(env); |
| 186 | } |
| 187 | |
| 188 | rlim.rlim_cur = javaRlimit[1]; |
| 189 | rlim.rlim_max = javaRlimit[2]; |
| 190 | |
| 191 | int rc = setrlimit(javaRlimit[0], &rlim); |
| 192 | if (rc == -1) { |
| 193 | ALOGE("setrlimit(%d, {%d, %d}) failed", javaRlimit[0], rlim.rlim_cur, rlim.rlim_max); |
| 194 | RuntimeAbort(env); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 199 | // The debug malloc library needs to know whether it's the zygote or a child. |
| 200 | extern "C" int gMallocLeakZygoteChild; |
| 201 | |
| 202 | static void EnableKeepCapabilities(JNIEnv* env) { |
| 203 | int rc = prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0); |
| 204 | if (rc == -1) { |
| 205 | ALOGE("prctl(PR_SET_KEEPCAPS) failed"); |
| 206 | RuntimeAbort(env); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | static void DropCapabilitiesBoundingSet(JNIEnv* env) { |
| 211 | for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) { |
| 212 | int rc = prctl(PR_CAPBSET_DROP, i, 0, 0, 0); |
| 213 | if (rc == -1) { |
| 214 | if (errno == EINVAL) { |
| 215 | ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify " |
| 216 | "your kernel is compiled with file capabilities support"); |
| 217 | } else { |
| 218 | ALOGE("prctl(PR_CAPBSET_DROP) failed"); |
| 219 | RuntimeAbort(env); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | static void SetCapabilities(JNIEnv* env, int64_t permitted, int64_t effective) { |
| 226 | __user_cap_header_struct capheader; |
| 227 | memset(&capheader, 0, sizeof(capheader)); |
| 228 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 229 | capheader.pid = 0; |
| 230 | |
| 231 | __user_cap_data_struct capdata[2]; |
| 232 | memset(&capdata, 0, sizeof(capdata)); |
| 233 | capdata[0].effective = effective; |
| 234 | capdata[1].effective = effective >> 32; |
| 235 | capdata[0].permitted = permitted; |
| 236 | capdata[1].permitted = permitted >> 32; |
| 237 | |
| 238 | if (capset(&capheader, &capdata[0]) == -1) { |
| 239 | ALOGE("capset(%lld, %lld) failed", permitted, effective); |
| 240 | RuntimeAbort(env); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | static void SetSchedulerPolicy(JNIEnv* env) { |
| 245 | errno = -set_sched_policy(0, SP_DEFAULT); |
| 246 | if (errno != 0) { |
| 247 | ALOGE("set_sched_policy(0, SP_DEFAULT) failed"); |
| 248 | RuntimeAbort(env); |
| 249 | } |
| 250 | } |
| 251 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 252 | // Create a private mount namespace and bind mount appropriate emulated |
| 253 | // storage for the given user. |
jgu21 | 2086787 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 254 | static bool MountEmulatedStorage(uid_t uid, jint mount_mode, bool force_mount_namespace) { |
| 255 | if (mount_mode == MOUNT_EXTERNAL_NONE && !force_mount_namespace) { |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 256 | return true; |
| 257 | } |
| 258 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 259 | // Create a second private mount namespace for our process |
| 260 | if (unshare(CLONE_NEWNS) == -1) { |
| 261 | ALOGW("Failed to unshare(): %d", errno); |
| 262 | return false; |
| 263 | } |
| 264 | |
jgu21 | 2086787 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 265 | if (mount_mode == MOUNT_EXTERNAL_NONE) { |
| 266 | return true; |
| 267 | } |
| 268 | |
| 269 | // See storage config details at http://source.android.com/tech/storage/ |
| 270 | userid_t user_id = multiuser_get_user_id(uid); |
| 271 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 272 | // Create bind mounts to expose external storage |
| 273 | if (mount_mode == MOUNT_EXTERNAL_MULTIUSER || mount_mode == MOUNT_EXTERNAL_MULTIUSER_ALL) { |
| 274 | // These paths must already be created by init.rc |
| 275 | const char* source = getenv("EMULATED_STORAGE_SOURCE"); |
| 276 | const char* target = getenv("EMULATED_STORAGE_TARGET"); |
| 277 | const char* legacy = getenv("EXTERNAL_STORAGE"); |
| 278 | if (source == NULL || target == NULL || legacy == NULL) { |
| 279 | ALOGW("Storage environment undefined; unable to provide external storage"); |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | // Prepare source paths |
| 284 | |
| 285 | // /mnt/shell/emulated/0 |
| 286 | const String8 source_user(String8::format("%s/%d", source, user_id)); |
| 287 | // /storage/emulated/0 |
| 288 | const String8 target_user(String8::format("%s/%d", target, user_id)); |
| 289 | |
| 290 | if (fs_prepare_dir(source_user.string(), 0000, 0, 0) == -1 |
| 291 | || fs_prepare_dir(target_user.string(), 0000, 0, 0) == -1) { |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | if (mount_mode == MOUNT_EXTERNAL_MULTIUSER_ALL) { |
| 296 | // Mount entire external storage tree for all users |
| 297 | if (TEMP_FAILURE_RETRY(mount(source, target, NULL, MS_BIND, NULL)) == -1) { |
| 298 | ALOGW("Failed to mount %s to %s :%d", source, target, errno); |
| 299 | return false; |
| 300 | } |
| 301 | } else { |
| 302 | // Only mount user-specific external storage |
| 303 | if (TEMP_FAILURE_RETRY( |
| 304 | mount(source_user.string(), target_user.string(), NULL, MS_BIND, NULL)) == -1) { |
| 305 | ALOGW("Failed to mount %s to %s: %d", source_user.string(), target_user.string(), errno); |
| 306 | return false; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | if (fs_prepare_dir(legacy, 0000, 0, 0) == -1) { |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | // Finally, mount user-specific path into place for legacy users |
| 315 | if (TEMP_FAILURE_RETRY( |
| 316 | mount(target_user.string(), legacy, NULL, MS_BIND | MS_REC, NULL)) == -1) { |
| 317 | ALOGW("Failed to mount %s to %s: %d", target_user.string(), legacy, errno); |
| 318 | return false; |
| 319 | } |
| 320 | } else { |
| 321 | ALOGW("Mount mode %d unsupported", mount_mode); |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | return true; |
| 326 | } |
| 327 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 328 | static bool NeedsNoRandomizeWorkaround() { |
| 329 | #if !defined(__arm__) |
| 330 | return false; |
| 331 | #else |
| 332 | int major; |
| 333 | int minor; |
| 334 | struct utsname uts; |
| 335 | if (uname(&uts) == -1) { |
| 336 | return false; |
| 337 | } |
| 338 | |
| 339 | if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) { |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | // Kernels before 3.4.* need the workaround. |
| 344 | return (major < 3) || ((major == 3) && (minor < 4)); |
| 345 | #endif |
| 346 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 347 | |
| 348 | // Utility to close down the Zygote socket file descriptors while |
| 349 | // the child is still running as root with Zygote's privileges. Each |
| 350 | // descriptor (if any) is closed via dup2(), replacing it with a valid |
| 351 | // (open) descriptor to /dev/null. |
| 352 | |
| 353 | static void DetachDescriptors(JNIEnv* env, jintArray fdsToClose) { |
| 354 | if (!fdsToClose) { |
| 355 | return; |
| 356 | } |
| 357 | jsize count = env->GetArrayLength(fdsToClose); |
| 358 | jint *ar = env->GetIntArrayElements(fdsToClose, 0); |
| 359 | if (!ar) { |
| 360 | ALOGE("Bad fd array"); |
| 361 | RuntimeAbort(env); |
| 362 | } |
| 363 | jsize i; |
| 364 | int devnull; |
| 365 | for (i = 0; i < count; i++) { |
| 366 | devnull = open("/dev/null", O_RDWR); |
| 367 | if (devnull < 0) { |
| 368 | ALOGE("Failed to open /dev/null"); |
| 369 | RuntimeAbort(env); |
| 370 | continue; |
| 371 | } |
| 372 | ALOGV("Switching descriptor %d to /dev/null: %d", ar[i], errno); |
| 373 | if (dup2(devnull, ar[i]) < 0) { |
| 374 | ALOGE("Failed dup2() on descriptor %d", ar[i]); |
| 375 | RuntimeAbort(env); |
| 376 | } |
| 377 | close(devnull); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | void SetThreadName(const char* thread_name) { |
| 382 | bool hasAt = false; |
| 383 | bool hasDot = false; |
| 384 | const char* s = thread_name; |
| 385 | while (*s) { |
| 386 | if (*s == '.') { |
| 387 | hasDot = true; |
| 388 | } else if (*s == '@') { |
| 389 | hasAt = true; |
| 390 | } |
| 391 | s++; |
| 392 | } |
| 393 | const int len = s - thread_name; |
| 394 | if (len < 15 || hasAt || !hasDot) { |
| 395 | s = thread_name; |
| 396 | } else { |
| 397 | s = thread_name + len - 15; |
| 398 | } |
| 399 | // pthread_setname_np fails rather than truncating long strings. |
| 400 | char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic |
| 401 | strlcpy(buf, s, sizeof(buf)-1); |
| 402 | errno = pthread_setname_np(pthread_self(), buf); |
| 403 | if (errno != 0) { |
| 404 | ALOGW("Unable to set the name of current thread to '%s'", buf); |
| 405 | } |
| 406 | } |
| 407 | |
Anwar Ghuloum | 629dc18 | 2014-09-04 08:05:34 -0700 | [diff] [blame] | 408 | // Temporary timing check. |
| 409 | uint64_t MsTime() { |
| 410 | timespec now; |
| 411 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 412 | return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000) + now.tv_nsec / UINT64_C(1000000); |
| 413 | } |
| 414 | |
| 415 | |
| 416 | void ckTime(uint64_t start, const char* where) { |
| 417 | uint64_t now = MsTime(); |
| 418 | if ((now-start) > 1000) { |
| 419 | // If we are taking more than a second, log about it. |
| 420 | ALOGW("Slow operation: %"PRIu64" ms in %s", (uint64_t)(now-start), where); |
| 421 | } |
| 422 | } |
| 423 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 424 | // Utility routine to fork zygote and specialize the child process. |
| 425 | static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids, |
| 426 | jint debug_flags, jobjectArray javaRlimits, |
| 427 | jlong permittedCapabilities, jlong effectiveCapabilities, |
| 428 | jint mount_external, |
| 429 | jstring java_se_info, jstring java_se_name, |
Andreas Gampe | 6b0ed64 | 2014-09-04 15:43:11 -0700 | [diff] [blame] | 430 | bool is_system_server, jintArray fdsToClose, |
jgu21 | 2086787 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 431 | jstring instructionSet, jstring dataDir) { |
Anwar Ghuloum | 629dc18 | 2014-09-04 08:05:34 -0700 | [diff] [blame] | 432 | uint64_t start = MsTime(); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 433 | SetSigChldHandler(); |
Anwar Ghuloum | 629dc18 | 2014-09-04 08:05:34 -0700 | [diff] [blame] | 434 | ckTime(start, "ForkAndSpecializeCommon:SetSigChldHandler"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 435 | |
| 436 | pid_t pid = fork(); |
| 437 | |
| 438 | if (pid == 0) { |
| 439 | // The child process. |
| 440 | gMallocLeakZygoteChild = 1; |
| 441 | |
Anwar Ghuloum | 629dc18 | 2014-09-04 08:05:34 -0700 | [diff] [blame] | 442 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 443 | // Clean up any descriptors which must be closed immediately |
| 444 | DetachDescriptors(env, fdsToClose); |
| 445 | |
Anwar Ghuloum | 629dc18 | 2014-09-04 08:05:34 -0700 | [diff] [blame] | 446 | ckTime(start, "ForkAndSpecializeCommon:Fork and detach"); |
| 447 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 448 | // Keep capabilities across UID change, unless we're staying root. |
| 449 | if (uid != 0) { |
| 450 | EnableKeepCapabilities(env); |
| 451 | } |
| 452 | |
| 453 | DropCapabilitiesBoundingSet(env); |
| 454 | |
Calin Juravle | e9d6743 | 2014-10-24 16:16:49 +0100 | [diff] [blame] | 455 | bool use_native_bridge = !is_system_server && (instructionSet != NULL) |
| 456 | && android::NativeBridgeAvailable(); |
| 457 | if (use_native_bridge) { |
jgu21 | 2086787 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 458 | ScopedUtfChars isa_string(env, instructionSet); |
Calin Juravle | e9d6743 | 2014-10-24 16:16:49 +0100 | [diff] [blame] | 459 | use_native_bridge = android::NeedsNativeBridge(isa_string.c_str()); |
jgu21 | 2086787 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 460 | } |
Calin Juravle | e11952e | 2014-10-28 12:16:21 +0000 | [diff] [blame] | 461 | if (use_native_bridge && dataDir == NULL) { |
| 462 | // dataDir should never be null if we need to use a native bridge. |
| 463 | // In general, dataDir will never be null for normal applications. It can only happen in |
| 464 | // special cases (for isolated processes which are not associated with any app). These are |
| 465 | // launched by the framework and should not be emulated anyway. |
| 466 | use_native_bridge = false; |
| 467 | ALOGW("Native bridge will not be used because dataDir == NULL."); |
| 468 | } |
jgu21 | 2086787 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 469 | |
Calin Juravle | e9d6743 | 2014-10-24 16:16:49 +0100 | [diff] [blame] | 470 | if (!MountEmulatedStorage(uid, mount_external, use_native_bridge)) { |
| 471 | ALOGW("Failed to mount emulated storage: %s", strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 472 | if (errno == ENOTCONN || errno == EROFS) { |
| 473 | // When device is actively encrypting, we get ENOTCONN here |
| 474 | // since FUSE was mounted before the framework restarted. |
| 475 | // When encrypted device is booting, we get EROFS since |
| 476 | // FUSE hasn't been created yet by init. |
| 477 | // In either case, continue without external storage. |
| 478 | } else { |
| 479 | ALOGE("Cannot continue without emulated storage"); |
| 480 | RuntimeAbort(env); |
| 481 | } |
| 482 | } |
| 483 | |
Colin Cross | 0161bbc | 2014-06-03 13:26:58 -0700 | [diff] [blame] | 484 | if (!is_system_server) { |
| 485 | int rc = createProcessGroup(uid, getpid()); |
| 486 | if (rc != 0) { |
Colin Cross | 3089bed | 2014-07-14 15:07:04 -0700 | [diff] [blame] | 487 | if (rc == -EROFS) { |
| 488 | ALOGW("createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?"); |
| 489 | } else { |
| 490 | ALOGE("createProcessGroup(%d, %d) failed: %s", uid, pid, strerror(-rc)); |
| 491 | } |
Colin Cross | 0161bbc | 2014-06-03 13:26:58 -0700 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 495 | SetGids(env, javaGids); |
| 496 | |
| 497 | SetRLimits(env, javaRlimits); |
| 498 | |
Calin Juravle | e9d6743 | 2014-10-24 16:16:49 +0100 | [diff] [blame] | 499 | if (use_native_bridge) { |
| 500 | ScopedUtfChars isa_string(env, instructionSet); |
| 501 | ScopedUtfChars data_dir(env, dataDir); |
| 502 | android::PreInitializeNativeBridge(data_dir.c_str(), isa_string.c_str()); |
jgu21 | 2086787 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 503 | } |
| 504 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 505 | int rc = setresgid(gid, gid, gid); |
| 506 | if (rc == -1) { |
| 507 | ALOGE("setresgid(%d) failed", gid); |
| 508 | RuntimeAbort(env); |
| 509 | } |
| 510 | |
| 511 | rc = setresuid(uid, uid, uid); |
| 512 | if (rc == -1) { |
| 513 | ALOGE("setresuid(%d) failed", uid); |
| 514 | RuntimeAbort(env); |
| 515 | } |
| 516 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 517 | if (NeedsNoRandomizeWorkaround()) { |
| 518 | // Work around ARM kernel ASLR lossage (http://b/5817320). |
| 519 | int old_personality = personality(0xffffffff); |
| 520 | int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE); |
| 521 | if (new_personality == -1) { |
| 522 | ALOGW("personality(%d) failed", new_personality); |
| 523 | } |
| 524 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 525 | |
| 526 | SetCapabilities(env, permittedCapabilities, effectiveCapabilities); |
| 527 | |
| 528 | SetSchedulerPolicy(env); |
| 529 | |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 530 | const char* se_info_c_str = NULL; |
| 531 | ScopedUtfChars* se_info = NULL; |
| 532 | if (java_se_info != NULL) { |
| 533 | se_info = new ScopedUtfChars(env, java_se_info); |
| 534 | se_info_c_str = se_info->c_str(); |
| 535 | if (se_info_c_str == NULL) { |
| 536 | ALOGE("se_info_c_str == NULL"); |
| 537 | RuntimeAbort(env); |
| 538 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 539 | } |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 540 | const char* se_name_c_str = NULL; |
| 541 | ScopedUtfChars* se_name = NULL; |
| 542 | if (java_se_name != NULL) { |
| 543 | se_name = new ScopedUtfChars(env, java_se_name); |
| 544 | se_name_c_str = se_name->c_str(); |
| 545 | if (se_name_c_str == NULL) { |
| 546 | ALOGE("se_name_c_str == NULL"); |
| 547 | RuntimeAbort(env); |
| 548 | } |
| 549 | } |
| 550 | rc = selinux_android_setcontext(uid, is_system_server, se_info_c_str, se_name_c_str); |
| 551 | if (rc == -1) { |
| 552 | ALOGE("selinux_android_setcontext(%d, %d, \"%s\", \"%s\") failed", uid, |
| 553 | is_system_server, se_info_c_str, se_name_c_str); |
| 554 | RuntimeAbort(env); |
| 555 | } |
| 556 | |
| 557 | // Make it easier to debug audit logs by setting the main thread's name to the |
| 558 | // nice name rather than "app_process". |
| 559 | if (se_info_c_str == NULL && is_system_server) { |
| 560 | se_name_c_str = "system_server"; |
| 561 | } |
| 562 | if (se_info_c_str != NULL) { |
| 563 | SetThreadName(se_name_c_str); |
| 564 | } |
| 565 | |
| 566 | delete se_info; |
| 567 | delete se_name; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 568 | |
| 569 | UnsetSigChldHandler(); |
| 570 | |
Anwar Ghuloum | 629dc18 | 2014-09-04 08:05:34 -0700 | [diff] [blame] | 571 | ckTime(start, "ForkAndSpecializeCommon:child process setup"); |
| 572 | |
Andreas Gampe | 6b0ed64 | 2014-09-04 15:43:11 -0700 | [diff] [blame] | 573 | env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, debug_flags, |
| 574 | is_system_server ? NULL : instructionSet); |
Anwar Ghuloum | 629dc18 | 2014-09-04 08:05:34 -0700 | [diff] [blame] | 575 | ckTime(start, "ForkAndSpecializeCommon:PostForkChildHooks returns"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 576 | if (env->ExceptionCheck()) { |
| 577 | ALOGE("Error calling post fork hooks."); |
| 578 | RuntimeAbort(env); |
| 579 | } |
| 580 | } else if (pid > 0) { |
| 581 | // the parent process |
| 582 | } |
| 583 | return pid; |
| 584 | } |
| 585 | } // anonymous namespace |
| 586 | |
| 587 | namespace android { |
| 588 | |
| 589 | static jint com_android_internal_os_Zygote_nativeForkAndSpecialize( |
| 590 | JNIEnv* env, jclass, jint uid, jint gid, jintArray gids, |
| 591 | jint debug_flags, jobjectArray rlimits, |
| 592 | jint mount_external, jstring se_info, jstring se_name, |
jgu21 | 2086787 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 593 | jintArray fdsToClose, jstring instructionSet, jstring appDataDir) { |
Sharvil Nanavati | 4990e4f | 2014-06-29 17:06:52 -0700 | [diff] [blame] | 594 | // Grant CAP_WAKE_ALARM to the Bluetooth process. |
| 595 | jlong capabilities = 0; |
| 596 | if (uid == AID_BLUETOOTH) { |
| 597 | capabilities |= (1LL << CAP_WAKE_ALARM); |
| 598 | } |
| 599 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 600 | return ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags, |
Sharvil Nanavati | 4990e4f | 2014-06-29 17:06:52 -0700 | [diff] [blame] | 601 | rlimits, capabilities, capabilities, mount_external, se_info, |
jgu21 | 2086787 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 602 | se_name, false, fdsToClose, instructionSet, appDataDir); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static jint com_android_internal_os_Zygote_nativeForkSystemServer( |
| 606 | JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids, |
| 607 | jint debug_flags, jobjectArray rlimits, jlong permittedCapabilities, |
| 608 | jlong effectiveCapabilities) { |
| 609 | pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids, |
| 610 | debug_flags, rlimits, |
| 611 | permittedCapabilities, effectiveCapabilities, |
jgu21 | 2086787 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 612 | MOUNT_EXTERNAL_NONE, NULL, NULL, true, NULL, |
| 613 | NULL, NULL); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 614 | if (pid > 0) { |
| 615 | // The zygote process checks whether the child process has died or not. |
| 616 | ALOGI("System server process %d has been created", pid); |
| 617 | gSystemServerPid = pid; |
| 618 | // There is a slight window that the system server process has crashed |
| 619 | // but it went unnoticed because we haven't published its pid yet. So |
| 620 | // we recheck here just to make sure that all is well. |
| 621 | int status; |
| 622 | if (waitpid(pid, &status, WNOHANG) == pid) { |
| 623 | ALOGE("System server process %d has died. Restarting Zygote!", pid); |
| 624 | RuntimeAbort(env); |
| 625 | } |
| 626 | } |
| 627 | return pid; |
| 628 | } |
| 629 | |
| 630 | static JNINativeMethod gMethods[] = { |
Andreas Gampe | 6b0ed64 | 2014-09-04 15:43:11 -0700 | [diff] [blame] | 631 | { "nativeForkAndSpecialize", |
jgu21 | 2086787 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 632 | "(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] | 633 | (void *) com_android_internal_os_Zygote_nativeForkAndSpecialize }, |
| 634 | { "nativeForkSystemServer", "(II[II[[IJJ)I", |
| 635 | (void *) com_android_internal_os_Zygote_nativeForkSystemServer } |
| 636 | }; |
| 637 | |
| 638 | int register_com_android_internal_os_Zygote(JNIEnv* env) { |
| 639 | gZygoteClass = (jclass) env->NewGlobalRef(env->FindClass(kZygoteClassName)); |
| 640 | if (gZygoteClass == NULL) { |
| 641 | RuntimeAbort(env); |
| 642 | } |
Andreas Gampe | 6b0ed64 | 2014-09-04 15:43:11 -0700 | [diff] [blame] | 643 | gCallPostForkChildHooks = env->GetStaticMethodID(gZygoteClass, "callPostForkChildHooks", |
| 644 | "(ILjava/lang/String;)V"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 645 | |
| 646 | return AndroidRuntime::registerNativeMethods(env, "com/android/internal/os/Zygote", |
| 647 | gMethods, NELEM(gMethods)); |
| 648 | } |
| 649 | } // namespace android |
| 650 | |