blob: 7a725ae75e90e4c3d9682c2d58d8e300a1ecdde3 [file] [log] [blame]
Narayan Kamath973b4662014-03-31 13:41:26 +01001/*
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 Cross18cd9f52014-06-13 12:58:55 -070017#define LOG_TAG "Zygote"
Narayan Kamath973b4662014-03-31 13:41:26 +010018
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 Sharkeyfaf3f692015-06-30 15:56:33 -070023#include <list>
24#include <string>
25
Colin Cross18cd9f52014-06-13 12:58:55 -070026#include <fcntl.h>
Dan Albert46d84442014-11-18 16:07:51 -080027#include <grp.h>
28#include <inttypes.h>
Jeff Sharkeyfaf3f692015-06-30 15:56:33 -070029#include <mntent.h>
Narayan Kamath973b4662014-03-31 13:41:26 +010030#include <paths.h>
31#include <signal.h>
32#include <stdlib.h>
Colin Cross18cd9f52014-06-13 12:58:55 -070033#include <sys/capability.h>
34#include <sys/personality.h>
35#include <sys/prctl.h>
36#include <sys/resource.h>
37#include <sys/stat.h>
38#include <sys/types.h>
39#include <sys/utsname.h>
40#include <sys/wait.h>
Dan Albert46d84442014-11-18 16:07:51 -080041#include <unistd.h>
Colin Cross18cd9f52014-06-13 12:58:55 -070042
43#include <cutils/fs.h>
44#include <cutils/multiuser.h>
45#include <cutils/sched_policy.h>
Sharvil Nanavati4990e4f2014-06-29 17:06:52 -070046#include <private/android_filesystem_config.h>
Colin Cross18cd9f52014-06-13 12:58:55 -070047#include <utils/String8.h>
48#include <selinux/android.h>
Colin Cross0161bbc2014-06-03 13:26:58 -070049#include <processgroup/processgroup.h>
Colin Cross18cd9f52014-06-13 12:58:55 -070050
Andreas Gampeed6b9df2014-11-20 22:02:20 -080051#include "core_jni_helpers.h"
Narayan Kamath973b4662014-03-31 13:41:26 +010052#include "JNIHelp.h"
53#include "ScopedLocalRef.h"
54#include "ScopedPrimitiveArray.h"
55#include "ScopedUtfChars.h"
56
jgu212eacd062014-09-10 06:55:07 -040057#include "nativebridge/native_bridge.h"
58
Narayan Kamath973b4662014-03-31 13:41:26 +010059namespace {
60
61using android::String8;
62
63static pid_t gSystemServerPid = 0;
64
65static const char kZygoteClassName[] = "com/android/internal/os/Zygote";
66static jclass gZygoteClass;
67static jmethodID gCallPostForkChildHooks;
68
69// Must match values in com.android.internal.os.Zygote.
70enum MountExternalKind {
71 MOUNT_EXTERNAL_NONE = 0,
Jeff Sharkey48877892015-03-18 11:27:19 -070072 MOUNT_EXTERNAL_DEFAULT = 1,
Jeff Sharkey9527b222015-06-24 15:24:48 -070073 MOUNT_EXTERNAL_READ = 2,
74 MOUNT_EXTERNAL_WRITE = 3,
Narayan Kamath973b4662014-03-31 13:41:26 +010075};
76
77static void RuntimeAbort(JNIEnv* env) {
78 env->FatalError("RuntimeAbort");
79}
80
81// This signal handler is for zygote mode, since the zygote must reap its children
82static void SigChldHandler(int /*signal_number*/) {
83 pid_t pid;
84 int status;
85
86 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
87 // Log process-death status that we care about. In general it is
88 // not safe to call LOG(...) from a signal handler because of
89 // possible reentrancy. However, we know a priori that the
90 // current implementation of LOG() is safe to call from a SIGCHLD
91 // handler in the zygote process. If the LOG() implementation
92 // changes its locking strategy or its use of syscalls within the
93 // lazy-init critical section, its use here may become unsafe.
94 if (WIFEXITED(status)) {
95 if (WEXITSTATUS(status)) {
96 ALOGI("Process %d exited cleanly (%d)", pid, WEXITSTATUS(status));
Narayan Kamath973b4662014-03-31 13:41:26 +010097 }
98 } else if (WIFSIGNALED(status)) {
99 if (WTERMSIG(status) != SIGKILL) {
Narayan Kamath160992d2014-04-14 14:46:07 +0100100 ALOGI("Process %d exited due to signal (%d)", pid, WTERMSIG(status));
Narayan Kamath973b4662014-03-31 13:41:26 +0100101 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100102 if (WCOREDUMP(status)) {
103 ALOGI("Process %d dumped core.", pid);
104 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100105 }
106
107 // If the just-crashed process is the system_server, bring down zygote
108 // so that it is restarted by init and system server will be restarted
109 // from there.
110 if (pid == gSystemServerPid) {
Dan Albert46d84442014-11-18 16:07:51 -0800111 ALOGE("Exit zygote because system server (%d) has terminated", pid);
Narayan Kamath973b4662014-03-31 13:41:26 +0100112 kill(getpid(), SIGKILL);
113 }
114 }
115
Narayan Kamath160992d2014-04-14 14:46:07 +0100116 // Note that we shouldn't consider ECHILD an error because
117 // the secondary zygote might have no children left to wait for.
118 if (pid < 0 && errno != ECHILD) {
119 ALOGW("Zygote SIGCHLD error in waitpid: %s", strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100120 }
121}
122
123// Configures the SIGCHLD handler for the zygote process. This is configured
124// very late, because earlier in the runtime we may fork() and exec()
125// other processes, and we want to waitpid() for those rather than
126// have them be harvested immediately.
127//
128// This ends up being called repeatedly before each fork(), but there's
129// no real harm in that.
130static void SetSigChldHandler() {
131 struct sigaction sa;
132 memset(&sa, 0, sizeof(sa));
133 sa.sa_handler = SigChldHandler;
134
135 int err = sigaction(SIGCHLD, &sa, NULL);
136 if (err < 0) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700137 ALOGW("Error setting SIGCHLD handler: %s", strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100138 }
139}
140
141// Sets the SIGCHLD handler back to default behavior in zygote children.
142static void UnsetSigChldHandler() {
143 struct sigaction sa;
144 memset(&sa, 0, sizeof(sa));
145 sa.sa_handler = SIG_DFL;
146
147 int err = sigaction(SIGCHLD, &sa, NULL);
148 if (err < 0) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700149 ALOGW("Error unsetting SIGCHLD handler: %s", strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100150 }
151}
152
153// Calls POSIX setgroups() using the int[] object as an argument.
154// A NULL argument is tolerated.
155static void SetGids(JNIEnv* env, jintArray javaGids) {
156 if (javaGids == NULL) {
157 return;
158 }
159
160 ScopedIntArrayRO gids(env, javaGids);
161 if (gids.get() == NULL) {
162 RuntimeAbort(env);
163 }
164 int rc = setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0]));
165 if (rc == -1) {
166 ALOGE("setgroups failed");
167 RuntimeAbort(env);
168 }
169}
170
171// Sets the resource limits via setrlimit(2) for the values in the
172// two-dimensional array of integers that's passed in. The second dimension
173// contains a tuple of length 3: (resource, rlim_cur, rlim_max). NULL is
174// treated as an empty array.
175static void SetRLimits(JNIEnv* env, jobjectArray javaRlimits) {
176 if (javaRlimits == NULL) {
177 return;
178 }
179
180 rlimit rlim;
181 memset(&rlim, 0, sizeof(rlim));
182
183 for (int i = 0; i < env->GetArrayLength(javaRlimits); ++i) {
184 ScopedLocalRef<jobject> javaRlimitObject(env, env->GetObjectArrayElement(javaRlimits, i));
185 ScopedIntArrayRO javaRlimit(env, reinterpret_cast<jintArray>(javaRlimitObject.get()));
186 if (javaRlimit.size() != 3) {
187 ALOGE("rlimits array must have a second dimension of size 3");
188 RuntimeAbort(env);
189 }
190
191 rlim.rlim_cur = javaRlimit[1];
192 rlim.rlim_max = javaRlimit[2];
193
194 int rc = setrlimit(javaRlimit[0], &rlim);
195 if (rc == -1) {
Dan Albert46d84442014-11-18 16:07:51 -0800196 ALOGE("setrlimit(%d, {%ld, %ld}) failed", javaRlimit[0], rlim.rlim_cur,
197 rlim.rlim_max);
Narayan Kamath973b4662014-03-31 13:41:26 +0100198 RuntimeAbort(env);
199 }
200 }
201}
202
Narayan Kamath973b4662014-03-31 13:41:26 +0100203// The debug malloc library needs to know whether it's the zygote or a child.
204extern "C" int gMallocLeakZygoteChild;
205
206static void EnableKeepCapabilities(JNIEnv* env) {
207 int rc = prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
208 if (rc == -1) {
209 ALOGE("prctl(PR_SET_KEEPCAPS) failed");
210 RuntimeAbort(env);
211 }
212}
213
214static void DropCapabilitiesBoundingSet(JNIEnv* env) {
215 for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
216 int rc = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
217 if (rc == -1) {
218 if (errno == EINVAL) {
219 ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify "
220 "your kernel is compiled with file capabilities support");
221 } else {
222 ALOGE("prctl(PR_CAPBSET_DROP) failed");
223 RuntimeAbort(env);
224 }
225 }
226 }
227}
228
229static void SetCapabilities(JNIEnv* env, int64_t permitted, int64_t effective) {
230 __user_cap_header_struct capheader;
231 memset(&capheader, 0, sizeof(capheader));
232 capheader.version = _LINUX_CAPABILITY_VERSION_3;
233 capheader.pid = 0;
234
235 __user_cap_data_struct capdata[2];
236 memset(&capdata, 0, sizeof(capdata));
237 capdata[0].effective = effective;
238 capdata[1].effective = effective >> 32;
239 capdata[0].permitted = permitted;
240 capdata[1].permitted = permitted >> 32;
241
242 if (capset(&capheader, &capdata[0]) == -1) {
Dan Albert46d84442014-11-18 16:07:51 -0800243 ALOGE("capset(%" PRId64 ", %" PRId64 ") failed", permitted, effective);
Narayan Kamath973b4662014-03-31 13:41:26 +0100244 RuntimeAbort(env);
245 }
246}
247
248static void SetSchedulerPolicy(JNIEnv* env) {
249 errno = -set_sched_policy(0, SP_DEFAULT);
250 if (errno != 0) {
251 ALOGE("set_sched_policy(0, SP_DEFAULT) failed");
252 RuntimeAbort(env);
253 }
254}
255
Jeff Sharkeyfaf3f692015-06-30 15:56:33 -0700256static int UnmountTree(const char* path) {
257 size_t path_len = strlen(path);
258
259 FILE* fp = setmntent("/proc/mounts", "r");
260 if (fp == NULL) {
261 ALOGE("Error opening /proc/mounts: %s", strerror(errno));
262 return -errno;
263 }
264
265 // Some volumes can be stacked on each other, so force unmount in
266 // reverse order to give us the best chance of success.
267 std::list<std::string> toUnmount;
268 mntent* mentry;
269 while ((mentry = getmntent(fp)) != NULL) {
270 if (strncmp(mentry->mnt_dir, path, path_len) == 0) {
271 toUnmount.push_front(std::string(mentry->mnt_dir));
272 }
273 }
274 endmntent(fp);
275
276 for (auto path : toUnmount) {
277 if (umount2(path.c_str(), MNT_DETACH)) {
278 ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno));
279 }
280 }
281 return 0;
282}
283
Narayan Kamath973b4662014-03-31 13:41:26 +0100284// Create a private mount namespace and bind mount appropriate emulated
285// storage for the given user.
Jeff Sharkey9527b222015-06-24 15:24:48 -0700286static bool MountEmulatedStorage(uid_t uid, jint mount_mode,
287 bool force_mount_namespace) {
288 // See storage config details at http://source.android.com/tech/storage/
289
290 // Create a second private mount namespace for our process
291 if (unshare(CLONE_NEWNS) == -1) {
292 ALOGW("Failed to unshare(): %s", strerror(errno));
293 return false;
294 }
295
296 // Unmount storage provided by root namespace and mount requested view
Jeff Sharkeyfaf3f692015-06-30 15:56:33 -0700297 UnmountTree("/storage");
Jeff Sharkey9527b222015-06-24 15:24:48 -0700298
299 String8 storageSource;
300 if (mount_mode == MOUNT_EXTERNAL_DEFAULT) {
Jeff Sharkey928e1ec2015-08-06 11:40:21 -0700301 storageSource = "/mnt/runtime/default";
Jeff Sharkey9527b222015-06-24 15:24:48 -0700302 } else if (mount_mode == MOUNT_EXTERNAL_READ) {
Jeff Sharkey928e1ec2015-08-06 11:40:21 -0700303 storageSource = "/mnt/runtime/read";
Jeff Sharkey9527b222015-06-24 15:24:48 -0700304 } else if (mount_mode == MOUNT_EXTERNAL_WRITE) {
Jeff Sharkey928e1ec2015-08-06 11:40:21 -0700305 storageSource = "/mnt/runtime/write";
Jeff Sharkey9527b222015-06-24 15:24:48 -0700306 } else {
307 // Sane default of no storage visible
308 return true;
309 }
310 if (TEMP_FAILURE_RETRY(mount(storageSource.string(), "/storage",
311 NULL, MS_BIND | MS_REC | MS_SLAVE, NULL)) == -1) {
312 ALOGW("Failed to mount %s to /storage: %s", storageSource.string(), strerror(errno));
313 return false;
314 }
315
Jeff Sharkeyfaf3f692015-06-30 15:56:33 -0700316 // Mount user-specific symlink helper into place
Jeff Sharkey9527b222015-06-24 15:24:48 -0700317 userid_t user_id = multiuser_get_user_id(uid);
318 const String8 userSource(String8::format("/mnt/user/%d", user_id));
319 if (fs_prepare_dir(userSource.string(), 0751, 0, 0) == -1) {
320 return false;
321 }
322 if (TEMP_FAILURE_RETRY(mount(userSource.string(), "/storage/self",
323 NULL, MS_BIND, NULL)) == -1) {
324 ALOGW("Failed to mount %s to /storage/self: %s", userSource.string(), strerror(errno));
325 return false;
326 }
327
Narayan Kamath973b4662014-03-31 13:41:26 +0100328 return true;
Narayan Kamath973b4662014-03-31 13:41:26 +0100329}
330
Narayan Kamath973b4662014-03-31 13:41:26 +0100331static bool NeedsNoRandomizeWorkaround() {
332#if !defined(__arm__)
333 return false;
334#else
335 int major;
336 int minor;
337 struct utsname uts;
338 if (uname(&uts) == -1) {
339 return false;
340 }
341
342 if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) {
343 return false;
344 }
345
346 // Kernels before 3.4.* need the workaround.
347 return (major < 3) || ((major == 3) && (minor < 4));
348#endif
349}
Narayan Kamath973b4662014-03-31 13:41:26 +0100350
351// Utility to close down the Zygote socket file descriptors while
352// the child is still running as root with Zygote's privileges. Each
353// descriptor (if any) is closed via dup2(), replacing it with a valid
354// (open) descriptor to /dev/null.
355
356static void DetachDescriptors(JNIEnv* env, jintArray fdsToClose) {
357 if (!fdsToClose) {
358 return;
359 }
360 jsize count = env->GetArrayLength(fdsToClose);
361 jint *ar = env->GetIntArrayElements(fdsToClose, 0);
362 if (!ar) {
363 ALOGE("Bad fd array");
364 RuntimeAbort(env);
365 }
366 jsize i;
367 int devnull;
368 for (i = 0; i < count; i++) {
369 devnull = open("/dev/null", O_RDWR);
370 if (devnull < 0) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700371 ALOGE("Failed to open /dev/null: %s", strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100372 RuntimeAbort(env);
373 continue;
374 }
Elliott Hughes960e8312014-09-30 08:49:01 -0700375 ALOGV("Switching descriptor %d to /dev/null: %s", ar[i], strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100376 if (dup2(devnull, ar[i]) < 0) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700377 ALOGE("Failed dup2() on descriptor %d: %s", ar[i], strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100378 RuntimeAbort(env);
379 }
380 close(devnull);
381 }
382}
383
384void SetThreadName(const char* thread_name) {
385 bool hasAt = false;
386 bool hasDot = false;
387 const char* s = thread_name;
388 while (*s) {
389 if (*s == '.') {
390 hasDot = true;
391 } else if (*s == '@') {
392 hasAt = true;
393 }
394 s++;
395 }
396 const int len = s - thread_name;
397 if (len < 15 || hasAt || !hasDot) {
398 s = thread_name;
399 } else {
400 s = thread_name + len - 15;
401 }
402 // pthread_setname_np fails rather than truncating long strings.
403 char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic
404 strlcpy(buf, s, sizeof(buf)-1);
405 errno = pthread_setname_np(pthread_self(), buf);
406 if (errno != 0) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700407 ALOGW("Unable to set the name of current thread to '%s': %s", buf, strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100408 }
409}
410
411// Utility routine to fork zygote and specialize the child process.
412static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids,
413 jint debug_flags, jobjectArray javaRlimits,
414 jlong permittedCapabilities, jlong effectiveCapabilities,
415 jint mount_external,
416 jstring java_se_info, jstring java_se_name,
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700417 bool is_system_server, jintArray fdsToClose,
jgu212eacd062014-09-10 06:55:07 -0400418 jstring instructionSet, jstring dataDir) {
Narayan Kamath973b4662014-03-31 13:41:26 +0100419 SetSigChldHandler();
420
421 pid_t pid = fork();
422
423 if (pid == 0) {
424 // The child process.
425 gMallocLeakZygoteChild = 1;
426
427 // Clean up any descriptors which must be closed immediately
428 DetachDescriptors(env, fdsToClose);
429
430 // Keep capabilities across UID change, unless we're staying root.
431 if (uid != 0) {
432 EnableKeepCapabilities(env);
433 }
434
435 DropCapabilitiesBoundingSet(env);
436
Calin Juravle79ec4c12014-10-24 16:16:49 +0100437 bool use_native_bridge = !is_system_server && (instructionSet != NULL)
438 && android::NativeBridgeAvailable();
439 if (use_native_bridge) {
jgu212eacd062014-09-10 06:55:07 -0400440 ScopedUtfChars isa_string(env, instructionSet);
Calin Juravle79ec4c12014-10-24 16:16:49 +0100441 use_native_bridge = android::NeedsNativeBridge(isa_string.c_str());
jgu212eacd062014-09-10 06:55:07 -0400442 }
Calin Juravle6a4d2362014-10-28 12:16:21 +0000443 if (use_native_bridge && dataDir == NULL) {
444 // dataDir should never be null if we need to use a native bridge.
445 // In general, dataDir will never be null for normal applications. It can only happen in
446 // special cases (for isolated processes which are not associated with any app). These are
447 // launched by the framework and should not be emulated anyway.
448 use_native_bridge = false;
449 ALOGW("Native bridge will not be used because dataDir == NULL.");
450 }
jgu212eacd062014-09-10 06:55:07 -0400451
Calin Juravle79ec4c12014-10-24 16:16:49 +0100452 if (!MountEmulatedStorage(uid, mount_external, use_native_bridge)) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700453 ALOGW("Failed to mount emulated storage: %s", strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100454 if (errno == ENOTCONN || errno == EROFS) {
455 // When device is actively encrypting, we get ENOTCONN here
456 // since FUSE was mounted before the framework restarted.
457 // When encrypted device is booting, we get EROFS since
458 // FUSE hasn't been created yet by init.
459 // In either case, continue without external storage.
460 } else {
461 ALOGE("Cannot continue without emulated storage");
462 RuntimeAbort(env);
463 }
464 }
465
Colin Cross0161bbc2014-06-03 13:26:58 -0700466 if (!is_system_server) {
467 int rc = createProcessGroup(uid, getpid());
468 if (rc != 0) {
Colin Cross3089bed2014-07-14 15:07:04 -0700469 if (rc == -EROFS) {
470 ALOGW("createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?");
471 } else {
472 ALOGE("createProcessGroup(%d, %d) failed: %s", uid, pid, strerror(-rc));
473 }
Colin Cross0161bbc2014-06-03 13:26:58 -0700474 }
475 }
476
Narayan Kamath973b4662014-03-31 13:41:26 +0100477 SetGids(env, javaGids);
478
479 SetRLimits(env, javaRlimits);
480
Calin Juravle79ec4c12014-10-24 16:16:49 +0100481 if (use_native_bridge) {
482 ScopedUtfChars isa_string(env, instructionSet);
483 ScopedUtfChars data_dir(env, dataDir);
484 android::PreInitializeNativeBridge(data_dir.c_str(), isa_string.c_str());
jgu212eacd062014-09-10 06:55:07 -0400485 }
486
Narayan Kamath973b4662014-03-31 13:41:26 +0100487 int rc = setresgid(gid, gid, gid);
488 if (rc == -1) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700489 ALOGE("setresgid(%d) failed: %s", gid, strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100490 RuntimeAbort(env);
491 }
492
493 rc = setresuid(uid, uid, uid);
494 if (rc == -1) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700495 ALOGE("setresuid(%d) failed: %s", uid, strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100496 RuntimeAbort(env);
497 }
498
Narayan Kamath973b4662014-03-31 13:41:26 +0100499 if (NeedsNoRandomizeWorkaround()) {
500 // Work around ARM kernel ASLR lossage (http://b/5817320).
501 int old_personality = personality(0xffffffff);
502 int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE);
503 if (new_personality == -1) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700504 ALOGW("personality(%d) failed: %s", new_personality, strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100505 }
506 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100507
508 SetCapabilities(env, permittedCapabilities, effectiveCapabilities);
509
510 SetSchedulerPolicy(env);
511
Colin Cross18cd9f52014-06-13 12:58:55 -0700512 const char* se_info_c_str = NULL;
513 ScopedUtfChars* se_info = NULL;
514 if (java_se_info != NULL) {
515 se_info = new ScopedUtfChars(env, java_se_info);
516 se_info_c_str = se_info->c_str();
517 if (se_info_c_str == NULL) {
518 ALOGE("se_info_c_str == NULL");
519 RuntimeAbort(env);
520 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100521 }
Colin Cross18cd9f52014-06-13 12:58:55 -0700522 const char* se_name_c_str = NULL;
523 ScopedUtfChars* se_name = NULL;
524 if (java_se_name != NULL) {
525 se_name = new ScopedUtfChars(env, java_se_name);
526 se_name_c_str = se_name->c_str();
527 if (se_name_c_str == NULL) {
528 ALOGE("se_name_c_str == NULL");
529 RuntimeAbort(env);
530 }
531 }
532 rc = selinux_android_setcontext(uid, is_system_server, se_info_c_str, se_name_c_str);
533 if (rc == -1) {
534 ALOGE("selinux_android_setcontext(%d, %d, \"%s\", \"%s\") failed", uid,
535 is_system_server, se_info_c_str, se_name_c_str);
536 RuntimeAbort(env);
537 }
538
539 // Make it easier to debug audit logs by setting the main thread's name to the
540 // nice name rather than "app_process".
541 if (se_info_c_str == NULL && is_system_server) {
542 se_name_c_str = "system_server";
543 }
544 if (se_info_c_str != NULL) {
545 SetThreadName(se_name_c_str);
546 }
547
548 delete se_info;
549 delete se_name;
Narayan Kamath973b4662014-03-31 13:41:26 +0100550
551 UnsetSigChldHandler();
552
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700553 env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, debug_flags,
554 is_system_server ? NULL : instructionSet);
Narayan Kamath973b4662014-03-31 13:41:26 +0100555 if (env->ExceptionCheck()) {
556 ALOGE("Error calling post fork hooks.");
557 RuntimeAbort(env);
558 }
559 } else if (pid > 0) {
560 // the parent process
561 }
562 return pid;
563}
564} // anonymous namespace
565
566namespace android {
567
568static jint com_android_internal_os_Zygote_nativeForkAndSpecialize(
569 JNIEnv* env, jclass, jint uid, jint gid, jintArray gids,
570 jint debug_flags, jobjectArray rlimits,
571 jint mount_external, jstring se_info, jstring se_name,
jgu212eacd062014-09-10 06:55:07 -0400572 jintArray fdsToClose, jstring instructionSet, jstring appDataDir) {
Sharvil Nanavati4990e4f2014-06-29 17:06:52 -0700573 // Grant CAP_WAKE_ALARM to the Bluetooth process.
574 jlong capabilities = 0;
575 if (uid == AID_BLUETOOTH) {
576 capabilities |= (1LL << CAP_WAKE_ALARM);
577 }
578
Narayan Kamath973b4662014-03-31 13:41:26 +0100579 return ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags,
Sharvil Nanavati4990e4f2014-06-29 17:06:52 -0700580 rlimits, capabilities, capabilities, mount_external, se_info,
Andreas Gampea103ebe2014-09-24 15:37:53 -0700581 se_name, false, fdsToClose, instructionSet, appDataDir);
Narayan Kamath973b4662014-03-31 13:41:26 +0100582}
583
584static jint com_android_internal_os_Zygote_nativeForkSystemServer(
585 JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
586 jint debug_flags, jobjectArray rlimits, jlong permittedCapabilities,
587 jlong effectiveCapabilities) {
588 pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids,
589 debug_flags, rlimits,
590 permittedCapabilities, effectiveCapabilities,
Jeff Sharkey9527b222015-06-24 15:24:48 -0700591 MOUNT_EXTERNAL_DEFAULT, NULL, NULL, true, NULL,
jgu212eacd062014-09-10 06:55:07 -0400592 NULL, NULL);
Narayan Kamath973b4662014-03-31 13:41:26 +0100593 if (pid > 0) {
594 // The zygote process checks whether the child process has died or not.
595 ALOGI("System server process %d has been created", pid);
596 gSystemServerPid = pid;
597 // There is a slight window that the system server process has crashed
598 // but it went unnoticed because we haven't published its pid yet. So
599 // we recheck here just to make sure that all is well.
600 int status;
601 if (waitpid(pid, &status, WNOHANG) == pid) {
602 ALOGE("System server process %d has died. Restarting Zygote!", pid);
603 RuntimeAbort(env);
604 }
605 }
606 return pid;
607}
608
609static JNINativeMethod gMethods[] = {
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700610 { "nativeForkAndSpecialize",
jgu212eacd062014-09-10 06:55:07 -0400611 "(II[II[[IILjava/lang/String;Ljava/lang/String;[ILjava/lang/String;Ljava/lang/String;)I",
Narayan Kamath973b4662014-03-31 13:41:26 +0100612 (void *) com_android_internal_os_Zygote_nativeForkAndSpecialize },
613 { "nativeForkSystemServer", "(II[II[[IJJ)I",
614 (void *) com_android_internal_os_Zygote_nativeForkSystemServer }
615};
616
617int register_com_android_internal_os_Zygote(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800618 gZygoteClass = MakeGlobalRefOrDie(env, FindClassOrDie(env, kZygoteClassName));
619 gCallPostForkChildHooks = GetStaticMethodIDOrDie(env, gZygoteClass, "callPostForkChildHooks",
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700620 "(ILjava/lang/String;)V");
Narayan Kamath973b4662014-03-31 13:41:26 +0100621
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800622 return RegisterMethodsOrDie(env, "com/android/internal/os/Zygote", gMethods, NELEM(gMethods));
Narayan Kamath973b4662014-03-31 13:41:26 +0100623}
624} // namespace android
625