5151#include " ScopedPrimitiveArray.h"
5252#include " ScopedUtfChars.h"
5353
54+ #include " nativebridge/native_bridge.h"
55+
5456namespace {
5557
5658using android::String8;
@@ -249,20 +251,24 @@ static void SetSchedulerPolicy(JNIEnv* env) {
249251
250252// Create a private mount namespace and bind mount appropriate emulated
251253// storage for the given user.
252- static bool MountEmulatedStorage (uid_t uid, jint mount_mode) {
253- if (mount_mode == MOUNT_EXTERNAL_NONE) {
254+ static bool MountEmulatedStorage (uid_t uid, jint mount_mode, bool force_mount_namespace ) {
255+ if (mount_mode == MOUNT_EXTERNAL_NONE && !force_mount_namespace ) {
254256 return true ;
255257 }
256258
257- // See storage config details at http://source.android.com/tech/storage/
258- userid_t user_id = multiuser_get_user_id (uid);
259-
260259 // Create a second private mount namespace for our process
261260 if (unshare (CLONE_NEWNS) == -1 ) {
262261 ALOGW (" Failed to unshare(): %d" , errno);
263262 return false ;
264263 }
265264
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+
266272 // Create bind mounts to expose external storage
267273 if (mount_mode == MOUNT_EXTERNAL_MULTIUSER || mount_mode == MOUNT_EXTERNAL_MULTIUSER_ALL) {
268274 // These paths must already be created by init.rc
@@ -422,7 +428,7 @@ static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArra
422428 jint mount_external,
423429 jstring java_se_info, jstring java_se_name,
424430 bool is_system_server, jintArray fdsToClose,
425- jstring instructionSet) {
431+ jstring instructionSet, jstring dataDir ) {
426432 uint64_t start = MsTime ();
427433 SetSigChldHandler ();
428434 ckTime (start, " ForkAndSpecializeCommon:SetSigChldHandler" );
@@ -446,7 +452,13 @@ static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArra
446452
447453 DropCapabilitiesBoundingSet (env);
448454
449- if (!MountEmulatedStorage (uid, mount_external)) {
455+ bool need_native_bridge = false ;
456+ if (instructionSet != NULL ) {
457+ ScopedUtfChars isa_string (env, instructionSet);
458+ need_native_bridge = android::NeedsNativeBridge (isa_string.c_str ());
459+ }
460+
461+ if (!MountEmulatedStorage (uid, mount_external, need_native_bridge)) {
450462 ALOGW (" Failed to mount emulated storage: %d" , errno);
451463 if (errno == ENOTCONN || errno == EROFS) {
452464 // When device is actively encrypting, we get ENOTCONN here
@@ -475,6 +487,17 @@ static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArra
475487
476488 SetRLimits (env, javaRlimits);
477489
490+ if (!is_system_server && need_native_bridge) {
491+ // Set the environment for the apps running with native bridge.
492+ ScopedUtfChars isa_string (env, instructionSet); // Known non-null because of need_native_...
493+ if (dataDir == NULL ) {
494+ android::PreInitializeNativeBridge (NULL , isa_string.c_str ());
495+ } else {
496+ ScopedUtfChars data_dir (env, dataDir);
497+ android::PreInitializeNativeBridge (data_dir.c_str (), isa_string.c_str ());
498+ }
499+ }
500+
478501 int rc = setresgid (gid, gid, gid);
479502 if (rc == -1 ) {
480503 ALOGE (" setresgid(%d) failed" , gid);
@@ -563,7 +586,7 @@ static jint com_android_internal_os_Zygote_nativeForkAndSpecialize(
563586 JNIEnv* env, jclass, jint uid, jint gid, jintArray gids,
564587 jint debug_flags, jobjectArray rlimits,
565588 jint mount_external, jstring se_info, jstring se_name,
566- jintArray fdsToClose, jstring instructionSet) {
589+ jintArray fdsToClose, jstring instructionSet, jstring appDataDir ) {
567590 // Grant CAP_WAKE_ALARM to the Bluetooth process.
568591 jlong capabilities = 0 ;
569592 if (uid == AID_BLUETOOTH) {
@@ -572,7 +595,7 @@ static jint com_android_internal_os_Zygote_nativeForkAndSpecialize(
572595
573596 return ForkAndSpecializeCommon (env, uid, gid, gids, debug_flags,
574597 rlimits, capabilities, capabilities, mount_external, se_info,
575- se_name, false , fdsToClose, instructionSet);
598+ se_name, false , fdsToClose, instructionSet, appDataDir );
576599}
577600
578601static jint com_android_internal_os_Zygote_nativeForkSystemServer (
@@ -582,7 +605,8 @@ static jint com_android_internal_os_Zygote_nativeForkSystemServer(
582605 pid_t pid = ForkAndSpecializeCommon (env, uid, gid, gids,
583606 debug_flags, rlimits,
584607 permittedCapabilities, effectiveCapabilities,
585- MOUNT_EXTERNAL_NONE, NULL , NULL , true , NULL , NULL );
608+ MOUNT_EXTERNAL_NONE, NULL , NULL , true , NULL ,
609+ NULL , NULL );
586610 if (pid > 0 ) {
587611 // The zygote process checks whether the child process has died or not.
588612 ALOGI (" System server process %d has been created" , pid);
@@ -601,7 +625,7 @@ static jint com_android_internal_os_Zygote_nativeForkSystemServer(
601625
602626static JNINativeMethod gMethods [] = {
603627 { " nativeForkAndSpecialize" ,
604- " (II[II[[IILjava/lang/String;Ljava/lang/String;[ILjava/lang/String;)I" ,
628+ " (II[II[[IILjava/lang/String;Ljava/lang/String;[ILjava/lang/String;Ljava/lang/String; )I" ,
605629 (void *) com_android_internal_os_Zygote_nativeForkAndSpecialize },
606630 { " nativeForkSystemServer" , " (II[II[[IJJ)I" ,
607631 (void *) com_android_internal_os_Zygote_nativeForkSystemServer }
0 commit comments