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