blob: 2bfeadb18e20d9921d58f9fd8427fd75dd113410 [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
Colin Cross18cd9f52014-06-13 12:58:55 -070023#include <fcntl.h>
Dan Albert46d84442014-11-18 16:07:51 -080024#include <grp.h>
25#include <inttypes.h>
Narayan Kamath973b4662014-03-31 13:41:26 +010026#include <paths.h>
27#include <signal.h>
28#include <stdlib.h>
Colin Cross18cd9f52014-06-13 12:58:55 -070029#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>
Dan Albert46d84442014-11-18 16:07:51 -080037#include <unistd.h>
Colin Cross18cd9f52014-06-13 12:58:55 -070038
39#include <cutils/fs.h>
40#include <cutils/multiuser.h>
41#include <cutils/sched_policy.h>
Sharvil Nanavati4990e4f2014-06-29 17:06:52 -070042#include <private/android_filesystem_config.h>
Colin Cross18cd9f52014-06-13 12:58:55 -070043#include <utils/String8.h>
44#include <selinux/android.h>
Colin Cross0161bbc2014-06-03 13:26:58 -070045#include <processgroup/processgroup.h>
Colin Cross18cd9f52014-06-13 12:58:55 -070046
Andreas Gampeed6b9df2014-11-20 22:02:20 -080047#include "core_jni_helpers.h"
Narayan Kamath973b4662014-03-31 13:41:26 +010048#include "JNIHelp.h"
49#include "ScopedLocalRef.h"
50#include "ScopedPrimitiveArray.h"
51#include "ScopedUtfChars.h"
52
jgu212eacd062014-09-10 06:55:07 -040053#include "nativebridge/native_bridge.h"
54
Narayan Kamath973b4662014-03-31 13:41:26 +010055namespace {
56
57using android::String8;
58
59static pid_t gSystemServerPid = 0;
60
61static const char kZygoteClassName[] = "com/android/internal/os/Zygote";
62static jclass gZygoteClass;
63static jmethodID gCallPostForkChildHooks;
64
65// Must match values in com.android.internal.os.Zygote.
66enum MountExternalKind {
67 MOUNT_EXTERNAL_NONE = 0,
68 MOUNT_EXTERNAL_SINGLEUSER = 1,
69 MOUNT_EXTERNAL_MULTIUSER = 2,
70 MOUNT_EXTERNAL_MULTIUSER_ALL = 3,
71};
72
73static void RuntimeAbort(JNIEnv* env) {
74 env->FatalError("RuntimeAbort");
75}
76
77// This signal handler is for zygote mode, since the zygote must reap its children
78static void SigChldHandler(int /*signal_number*/) {
79 pid_t pid;
80 int status;
81
82 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
83 // Log process-death status that we care about. In general it is
84 // not safe to call LOG(...) from a signal handler because of
85 // possible reentrancy. However, we know a priori that the
86 // current implementation of LOG() is safe to call from a SIGCHLD
87 // handler in the zygote process. If the LOG() implementation
88 // changes its locking strategy or its use of syscalls within the
89 // lazy-init critical section, its use here may become unsafe.
90 if (WIFEXITED(status)) {
91 if (WEXITSTATUS(status)) {
92 ALOGI("Process %d exited cleanly (%d)", pid, WEXITSTATUS(status));
Narayan Kamath973b4662014-03-31 13:41:26 +010093 }
94 } else if (WIFSIGNALED(status)) {
95 if (WTERMSIG(status) != SIGKILL) {
Narayan Kamath160992d2014-04-14 14:46:07 +010096 ALOGI("Process %d exited due to signal (%d)", pid, WTERMSIG(status));
Narayan Kamath973b4662014-03-31 13:41:26 +010097 }
Narayan Kamath973b4662014-03-31 13:41:26 +010098 if (WCOREDUMP(status)) {
99 ALOGI("Process %d dumped core.", pid);
100 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100101 }
102
103 // If the just-crashed process is the system_server, bring down zygote
104 // so that it is restarted by init and system server will be restarted
105 // from there.
106 if (pid == gSystemServerPid) {
Dan Albert46d84442014-11-18 16:07:51 -0800107 ALOGE("Exit zygote because system server (%d) has terminated", pid);
Narayan Kamath973b4662014-03-31 13:41:26 +0100108 kill(getpid(), SIGKILL);
109 }
110 }
111
Narayan Kamath160992d2014-04-14 14:46:07 +0100112 // Note that we shouldn't consider ECHILD an error because
113 // the secondary zygote might have no children left to wait for.
114 if (pid < 0 && errno != ECHILD) {
115 ALOGW("Zygote SIGCHLD error in waitpid: %s", strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100116 }
117}
118
119// Configures the SIGCHLD handler for the zygote process. This is configured
120// very late, because earlier in the runtime we may fork() and exec()
121// other processes, and we want to waitpid() for those rather than
122// have them be harvested immediately.
123//
124// This ends up being called repeatedly before each fork(), but there's
125// no real harm in that.
126static void SetSigChldHandler() {
127 struct sigaction sa;
128 memset(&sa, 0, sizeof(sa));
129 sa.sa_handler = SigChldHandler;
130
131 int err = sigaction(SIGCHLD, &sa, NULL);
132 if (err < 0) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700133 ALOGW("Error setting SIGCHLD handler: %s", strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100134 }
135}
136
137// Sets the SIGCHLD handler back to default behavior in zygote children.
138static void UnsetSigChldHandler() {
139 struct sigaction sa;
140 memset(&sa, 0, sizeof(sa));
141 sa.sa_handler = SIG_DFL;
142
143 int err = sigaction(SIGCHLD, &sa, NULL);
144 if (err < 0) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700145 ALOGW("Error unsetting SIGCHLD handler: %s", strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100146 }
147}
148
149// Calls POSIX setgroups() using the int[] object as an argument.
150// A NULL argument is tolerated.
151static void SetGids(JNIEnv* env, jintArray javaGids) {
152 if (javaGids == NULL) {
153 return;
154 }
155
156 ScopedIntArrayRO gids(env, javaGids);
157 if (gids.get() == NULL) {
158 RuntimeAbort(env);
159 }
160 int rc = setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0]));
161 if (rc == -1) {
162 ALOGE("setgroups failed");
163 RuntimeAbort(env);
164 }
165}
166
167// Sets the resource limits via setrlimit(2) for the values in the
168// two-dimensional array of integers that's passed in. The second dimension
169// contains a tuple of length 3: (resource, rlim_cur, rlim_max). NULL is
170// treated as an empty array.
171static void SetRLimits(JNIEnv* env, jobjectArray javaRlimits) {
172 if (javaRlimits == NULL) {
173 return;
174 }
175
176 rlimit rlim;
177 memset(&rlim, 0, sizeof(rlim));
178
179 for (int i = 0; i < env->GetArrayLength(javaRlimits); ++i) {
180 ScopedLocalRef<jobject> javaRlimitObject(env, env->GetObjectArrayElement(javaRlimits, i));
181 ScopedIntArrayRO javaRlimit(env, reinterpret_cast<jintArray>(javaRlimitObject.get()));
182 if (javaRlimit.size() != 3) {
183 ALOGE("rlimits array must have a second dimension of size 3");
184 RuntimeAbort(env);
185 }
186
187 rlim.rlim_cur = javaRlimit[1];
188 rlim.rlim_max = javaRlimit[2];
189
190 int rc = setrlimit(javaRlimit[0], &rlim);
191 if (rc == -1) {
Dan Albert46d84442014-11-18 16:07:51 -0800192 ALOGE("setrlimit(%d, {%ld, %ld}) failed", javaRlimit[0], rlim.rlim_cur,
193 rlim.rlim_max);
Narayan Kamath973b4662014-03-31 13:41:26 +0100194 RuntimeAbort(env);
195 }
196 }
197}
198
Narayan Kamath973b4662014-03-31 13:41:26 +0100199// The debug malloc library needs to know whether it's the zygote or a child.
200extern "C" int gMallocLeakZygoteChild;
201
202static 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
210static 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
225static 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) {
Dan Albert46d84442014-11-18 16:07:51 -0800239 ALOGE("capset(%" PRId64 ", %" PRId64 ") failed", permitted, effective);
Narayan Kamath973b4662014-03-31 13:41:26 +0100240 RuntimeAbort(env);
241 }
242}
243
244static 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 Kamath973b4662014-03-31 13:41:26 +0100252// Create a private mount namespace and bind mount appropriate emulated
253// storage for the given user.
jgu212eacd062014-09-10 06:55:07 -0400254static bool MountEmulatedStorage(uid_t uid, jint mount_mode, bool force_mount_namespace) {
255 if (mount_mode == MOUNT_EXTERNAL_NONE && !force_mount_namespace) {
Narayan Kamath973b4662014-03-31 13:41:26 +0100256 return true;
257 }
258
Narayan Kamath973b4662014-03-31 13:41:26 +0100259 // Create a second private mount namespace for our process
260 if (unshare(CLONE_NEWNS) == -1) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700261 ALOGW("Failed to unshare(): %s", strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100262 return false;
263 }
264
jgu212eacd062014-09-10 06:55:07 -0400265 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 Kamath973b4662014-03-31 13:41:26 +0100272 // 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) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700298 ALOGW("Failed to mount %s to %s: %s", source, target, strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100299 return false;
300 }
301 } else {
302 // Only mount user-specific external storage
Elliott Hughes960e8312014-09-30 08:49:01 -0700303 if (TEMP_FAILURE_RETRY(mount(source_user.string(), target_user.string(), NULL,
304 MS_BIND, NULL)) == -1) {
305 ALOGW("Failed to mount %s to %s: %s", source_user.string(), target_user.string(),
306 strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100307 return false;
308 }
309 }
310
311 if (fs_prepare_dir(legacy, 0000, 0, 0) == -1) {
312 return false;
313 }
314
315 // Finally, mount user-specific path into place for legacy users
316 if (TEMP_FAILURE_RETRY(
317 mount(target_user.string(), legacy, NULL, MS_BIND | MS_REC, NULL)) == -1) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700318 ALOGW("Failed to mount %s to %s: %s", target_user.string(), legacy, strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100319 return false;
320 }
321 } else {
322 ALOGW("Mount mode %d unsupported", mount_mode);
323 return false;
324 }
325
326 return true;
327}
328
Narayan Kamath973b4662014-03-31 13:41:26 +0100329static bool NeedsNoRandomizeWorkaround() {
330#if !defined(__arm__)
331 return false;
332#else
333 int major;
334 int minor;
335 struct utsname uts;
336 if (uname(&uts) == -1) {
337 return false;
338 }
339
340 if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) {
341 return false;
342 }
343
344 // Kernels before 3.4.* need the workaround.
345 return (major < 3) || ((major == 3) && (minor < 4));
346#endif
347}
Narayan Kamath973b4662014-03-31 13:41:26 +0100348
349// Utility to close down the Zygote socket file descriptors while
350// the child is still running as root with Zygote's privileges. Each
351// descriptor (if any) is closed via dup2(), replacing it with a valid
352// (open) descriptor to /dev/null.
353
354static void DetachDescriptors(JNIEnv* env, jintArray fdsToClose) {
355 if (!fdsToClose) {
356 return;
357 }
358 jsize count = env->GetArrayLength(fdsToClose);
359 jint *ar = env->GetIntArrayElements(fdsToClose, 0);
360 if (!ar) {
361 ALOGE("Bad fd array");
362 RuntimeAbort(env);
363 }
364 jsize i;
365 int devnull;
366 for (i = 0; i < count; i++) {
367 devnull = open("/dev/null", O_RDWR);
368 if (devnull < 0) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700369 ALOGE("Failed to open /dev/null: %s", strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100370 RuntimeAbort(env);
371 continue;
372 }
Elliott Hughes960e8312014-09-30 08:49:01 -0700373 ALOGV("Switching descriptor %d to /dev/null: %s", ar[i], strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100374 if (dup2(devnull, ar[i]) < 0) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700375 ALOGE("Failed dup2() on descriptor %d: %s", ar[i], strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100376 RuntimeAbort(env);
377 }
378 close(devnull);
379 }
380}
381
382void SetThreadName(const char* thread_name) {
383 bool hasAt = false;
384 bool hasDot = false;
385 const char* s = thread_name;
386 while (*s) {
387 if (*s == '.') {
388 hasDot = true;
389 } else if (*s == '@') {
390 hasAt = true;
391 }
392 s++;
393 }
394 const int len = s - thread_name;
395 if (len < 15 || hasAt || !hasDot) {
396 s = thread_name;
397 } else {
398 s = thread_name + len - 15;
399 }
400 // pthread_setname_np fails rather than truncating long strings.
401 char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic
402 strlcpy(buf, s, sizeof(buf)-1);
403 errno = pthread_setname_np(pthread_self(), buf);
404 if (errno != 0) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700405 ALOGW("Unable to set the name of current thread to '%s': %s", buf, strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100406 }
407}
408
409// Utility routine to fork zygote and specialize the child process.
410static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids,
411 jint debug_flags, jobjectArray javaRlimits,
412 jlong permittedCapabilities, jlong effectiveCapabilities,
413 jint mount_external,
414 jstring java_se_info, jstring java_se_name,
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700415 bool is_system_server, jintArray fdsToClose,
jgu212eacd062014-09-10 06:55:07 -0400416 jstring instructionSet, jstring dataDir) {
Narayan Kamath973b4662014-03-31 13:41:26 +0100417 SetSigChldHandler();
418
419 pid_t pid = fork();
420
421 if (pid == 0) {
422 // The child process.
423 gMallocLeakZygoteChild = 1;
424
425 // Clean up any descriptors which must be closed immediately
426 DetachDescriptors(env, fdsToClose);
427
428 // Keep capabilities across UID change, unless we're staying root.
429 if (uid != 0) {
430 EnableKeepCapabilities(env);
431 }
432
433 DropCapabilitiesBoundingSet(env);
434
Calin Juravle79ec4c12014-10-24 16:16:49 +0100435 bool use_native_bridge = !is_system_server && (instructionSet != NULL)
436 && android::NativeBridgeAvailable();
437 if (use_native_bridge) {
jgu212eacd062014-09-10 06:55:07 -0400438 ScopedUtfChars isa_string(env, instructionSet);
Calin Juravle79ec4c12014-10-24 16:16:49 +0100439 use_native_bridge = android::NeedsNativeBridge(isa_string.c_str());
jgu212eacd062014-09-10 06:55:07 -0400440 }
Calin Juravle6a4d2362014-10-28 12:16:21 +0000441 if (use_native_bridge && dataDir == NULL) {
442 // dataDir should never be null if we need to use a native bridge.
443 // In general, dataDir will never be null for normal applications. It can only happen in
444 // special cases (for isolated processes which are not associated with any app). These are
445 // launched by the framework and should not be emulated anyway.
446 use_native_bridge = false;
447 ALOGW("Native bridge will not be used because dataDir == NULL.");
448 }
jgu212eacd062014-09-10 06:55:07 -0400449
Calin Juravle79ec4c12014-10-24 16:16:49 +0100450 if (!MountEmulatedStorage(uid, mount_external, use_native_bridge)) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700451 ALOGW("Failed to mount emulated storage: %s", strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100452 if (errno == ENOTCONN || errno == EROFS) {
453 // When device is actively encrypting, we get ENOTCONN here
454 // since FUSE was mounted before the framework restarted.
455 // When encrypted device is booting, we get EROFS since
456 // FUSE hasn't been created yet by init.
457 // In either case, continue without external storage.
458 } else {
459 ALOGE("Cannot continue without emulated storage");
460 RuntimeAbort(env);
461 }
462 }
463
Colin Cross0161bbc2014-06-03 13:26:58 -0700464 if (!is_system_server) {
465 int rc = createProcessGroup(uid, getpid());
466 if (rc != 0) {
Colin Cross3089bed2014-07-14 15:07:04 -0700467 if (rc == -EROFS) {
468 ALOGW("createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?");
469 } else {
470 ALOGE("createProcessGroup(%d, %d) failed: %s", uid, pid, strerror(-rc));
471 }
Colin Cross0161bbc2014-06-03 13:26:58 -0700472 }
473 }
474
Narayan Kamath973b4662014-03-31 13:41:26 +0100475 SetGids(env, javaGids);
476
477 SetRLimits(env, javaRlimits);
478
Calin Juravle79ec4c12014-10-24 16:16:49 +0100479 if (use_native_bridge) {
480 ScopedUtfChars isa_string(env, instructionSet);
481 ScopedUtfChars data_dir(env, dataDir);
482 android::PreInitializeNativeBridge(data_dir.c_str(), isa_string.c_str());
jgu212eacd062014-09-10 06:55:07 -0400483 }
484
Narayan Kamath973b4662014-03-31 13:41:26 +0100485 int rc = setresgid(gid, gid, gid);
486 if (rc == -1) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700487 ALOGE("setresgid(%d) failed: %s", gid, strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100488 RuntimeAbort(env);
489 }
490
491 rc = setresuid(uid, uid, uid);
492 if (rc == -1) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700493 ALOGE("setresuid(%d) failed: %s", uid, strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100494 RuntimeAbort(env);
495 }
496
Narayan Kamath973b4662014-03-31 13:41:26 +0100497 if (NeedsNoRandomizeWorkaround()) {
498 // Work around ARM kernel ASLR lossage (http://b/5817320).
499 int old_personality = personality(0xffffffff);
500 int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE);
501 if (new_personality == -1) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700502 ALOGW("personality(%d) failed: %s", new_personality, strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100503 }
504 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100505
506 SetCapabilities(env, permittedCapabilities, effectiveCapabilities);
507
508 SetSchedulerPolicy(env);
509
Colin Cross18cd9f52014-06-13 12:58:55 -0700510 const char* se_info_c_str = NULL;
511 ScopedUtfChars* se_info = NULL;
512 if (java_se_info != NULL) {
513 se_info = new ScopedUtfChars(env, java_se_info);
514 se_info_c_str = se_info->c_str();
515 if (se_info_c_str == NULL) {
516 ALOGE("se_info_c_str == NULL");
517 RuntimeAbort(env);
518 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100519 }
Colin Cross18cd9f52014-06-13 12:58:55 -0700520 const char* se_name_c_str = NULL;
521 ScopedUtfChars* se_name = NULL;
522 if (java_se_name != NULL) {
523 se_name = new ScopedUtfChars(env, java_se_name);
524 se_name_c_str = se_name->c_str();
525 if (se_name_c_str == NULL) {
526 ALOGE("se_name_c_str == NULL");
527 RuntimeAbort(env);
528 }
529 }
530 rc = selinux_android_setcontext(uid, is_system_server, se_info_c_str, se_name_c_str);
531 if (rc == -1) {
532 ALOGE("selinux_android_setcontext(%d, %d, \"%s\", \"%s\") failed", uid,
533 is_system_server, se_info_c_str, se_name_c_str);
534 RuntimeAbort(env);
535 }
536
537 // Make it easier to debug audit logs by setting the main thread's name to the
538 // nice name rather than "app_process".
539 if (se_info_c_str == NULL && is_system_server) {
540 se_name_c_str = "system_server";
541 }
542 if (se_info_c_str != NULL) {
543 SetThreadName(se_name_c_str);
544 }
545
546 delete se_info;
547 delete se_name;
Narayan Kamath973b4662014-03-31 13:41:26 +0100548
549 UnsetSigChldHandler();
550
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700551 env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, debug_flags,
552 is_system_server ? NULL : instructionSet);
Narayan Kamath973b4662014-03-31 13:41:26 +0100553 if (env->ExceptionCheck()) {
554 ALOGE("Error calling post fork hooks.");
555 RuntimeAbort(env);
556 }
557 } else if (pid > 0) {
558 // the parent process
559 }
560 return pid;
561}
562} // anonymous namespace
563
564namespace android {
565
566static jint com_android_internal_os_Zygote_nativeForkAndSpecialize(
567 JNIEnv* env, jclass, jint uid, jint gid, jintArray gids,
568 jint debug_flags, jobjectArray rlimits,
569 jint mount_external, jstring se_info, jstring se_name,
jgu212eacd062014-09-10 06:55:07 -0400570 jintArray fdsToClose, jstring instructionSet, jstring appDataDir) {
Sharvil Nanavati4990e4f2014-06-29 17:06:52 -0700571 // Grant CAP_WAKE_ALARM to the Bluetooth process.
572 jlong capabilities = 0;
573 if (uid == AID_BLUETOOTH) {
574 capabilities |= (1LL << CAP_WAKE_ALARM);
575 }
576
Narayan Kamath973b4662014-03-31 13:41:26 +0100577 return ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags,
Sharvil Nanavati4990e4f2014-06-29 17:06:52 -0700578 rlimits, capabilities, capabilities, mount_external, se_info,
Andreas Gampea103ebe2014-09-24 15:37:53 -0700579 se_name, false, fdsToClose, instructionSet, appDataDir);
Narayan Kamath973b4662014-03-31 13:41:26 +0100580}
581
582static jint com_android_internal_os_Zygote_nativeForkSystemServer(
583 JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
584 jint debug_flags, jobjectArray rlimits, jlong permittedCapabilities,
585 jlong effectiveCapabilities) {
586 pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids,
587 debug_flags, rlimits,
588 permittedCapabilities, effectiveCapabilities,
jgu212eacd062014-09-10 06:55:07 -0400589 MOUNT_EXTERNAL_NONE, NULL, NULL, true, NULL,
590 NULL, NULL);
Narayan Kamath973b4662014-03-31 13:41:26 +0100591 if (pid > 0) {
592 // The zygote process checks whether the child process has died or not.
593 ALOGI("System server process %d has been created", pid);
594 gSystemServerPid = pid;
595 // There is a slight window that the system server process has crashed
596 // but it went unnoticed because we haven't published its pid yet. So
597 // we recheck here just to make sure that all is well.
598 int status;
599 if (waitpid(pid, &status, WNOHANG) == pid) {
600 ALOGE("System server process %d has died. Restarting Zygote!", pid);
601 RuntimeAbort(env);
602 }
603 }
604 return pid;
605}
606
607static JNINativeMethod gMethods[] = {
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700608 { "nativeForkAndSpecialize",
jgu212eacd062014-09-10 06:55:07 -0400609 "(II[II[[IILjava/lang/String;Ljava/lang/String;[ILjava/lang/String;Ljava/lang/String;)I",
Narayan Kamath973b4662014-03-31 13:41:26 +0100610 (void *) com_android_internal_os_Zygote_nativeForkAndSpecialize },
611 { "nativeForkSystemServer", "(II[II[[IJJ)I",
612 (void *) com_android_internal_os_Zygote_nativeForkSystemServer }
613};
614
615int register_com_android_internal_os_Zygote(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800616 gZygoteClass = MakeGlobalRefOrDie(env, FindClassOrDie(env, kZygoteClassName));
617 gCallPostForkChildHooks = GetStaticMethodIDOrDie(env, gZygoteClass, "callPostForkChildHooks",
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700618 "(ILjava/lang/String;)V");
Narayan Kamath973b4662014-03-31 13:41:26 +0100619
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800620 return RegisterMethodsOrDie(env, "com/android/internal/os/Zygote", gMethods, NELEM(gMethods));
Narayan Kamath973b4662014-03-31 13:41:26 +0100621}
622} // namespace android
623