1 /* 2 * Copyright (C) 2006 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 17 package dalvik.system; 18 19 /** 20 * Provides access to the Dalvik "zygote" feature, which allows a VM instance to 21 * be partially initialized and then fork()'d from the partially initialized 22 * state. 23 * 24 * @hide 25 */ 26 public class Zygote { 27 /* 28 * Bit values for "debugFlags" argument. The definitions are duplicated 29 * in the native code. 30 */ 31 /** enable debugging over JDWP */ 32 public static final int DEBUG_ENABLE_DEBUGGER = 1; 33 /** enable JNI checks */ 34 public static final int DEBUG_ENABLE_CHECKJNI = 1 << 1; 35 /** enable Java programming language "assert" statements */ 36 public static final int DEBUG_ENABLE_ASSERT = 1 << 2; 37 /** disable the JIT compiler */ 38 public static final int DEBUG_ENABLE_SAFEMODE = 1 << 3; 39 40 /** 41 * When set by the system server, all subsequent apps will be launched in 42 * VM safe mode. 43 * 44 * @hide 45 */ 46 public static boolean systemInSafeMode = false; 47 48 private Zygote() {} 49 50 /** 51 * Forks a new Zygote instance, but does not leave the zygote mode. 52 * The current VM must have been started with the -Xzygote flag. The 53 * new child is expected to eventually call forkAndSpecialize() 54 * 55 * @return 0 if this is the child, pid of the child 56 * if this is the parent, or -1 on error 57 */ 58 native public static int fork(); 59 60 /** 61 * Forks a new VM instance. The current VM must have been started 62 * with the -Xzygote flag. <b>NOTE: new instance keeps all 63 * root capabilities. The new process is expected to call capset()</b>. 64 * 65 * @param uid the UNIX uid that the new process should setuid() to after 66 * fork()ing and and before spawning any threads. 67 * @param gid the UNIX gid that the new process should setgid() to after 68 * fork()ing and and before spawning any threads. 69 * @param gids null-ok; a list of UNIX gids that the new process should 70 * setgroups() to after fork and before spawning any threads. 71 * @param debugFlags bit flags that enable debugging features. 72 * @param rlimits null-ok an array of rlimit tuples, with the second 73 * dimension having a length of 3 and representing 74 * (resource, rlim_cur, rlim_max). These are set via the posix 75 * setrlimit(2) call. 76 * 77 * @return 0 if this is the child, pid of the child 78 * if this is the parent, or -1 on error. 79 */ 80 native public static int forkAndSpecialize(int uid, int gid, int[] gids, 81 int debugFlags, int[][] rlimits); 82 83 /** 84 * Forks a new VM instance. 85 * @deprecated use {@link Zygote#forkAndSpecialize(int, int, int[], int, int[][])} 86 */ 87 @Deprecated 88 public static int forkAndSpecialize(int uid, int gid, int[] gids, 89 boolean enableDebugger, int[][] rlimits) { 90 int debugFlags = enableDebugger ? DEBUG_ENABLE_DEBUGGER : 0; 91 return forkAndSpecialize(uid, gid, gids, debugFlags, rlimits); 92 } 93 94 /** 95 * Special method to start the system server process. In addition to the 96 * common actions performed in forkAndSpecialize, the pid of the child 97 * process is recorded such that the death of the child process will cause 98 * zygote to exit. 99 * 100 * @param uid the UNIX uid that the new process should setuid() to after 101 * fork()ing and and before spawning any threads. 102 * @param gid the UNIX gid that the new process should setgid() to after 103 * fork()ing and and before spawning any threads. 104 * @param gids null-ok; a list of UNIX gids that the new process should 105 * setgroups() to after fork and before spawning any threads. 106 * @param debugFlags bit flags that enable debugging features. 107 * @param rlimits null-ok an array of rlimit tuples, with the second 108 * dimension having a length of 3 and representing 109 * (resource, rlim_cur, rlim_max). These are set via the posix 110 * setrlimit(2) call. 111 * @param permittedCapabilities argument for setcap() 112 * @param effectiveCapabilities argument for setcap() 113 * 114 * @return 0 if this is the child, pid of the child 115 * if this is the parent, or -1 on error. 116 */ 117 native public static int forkSystemServer(int uid, int gid, 118 int[] gids, int debugFlags, int[][] rlimits, 119 long permittedCapabilities, long effectiveCapabilities); 120 121 /** 122 * Special method to start the system server process. 123 * @deprecated use {@link Zygote#forkSystemServer(int, int, int[], int, int[][])} 124 */ 125 @Deprecated 126 public static int forkSystemServer(int uid, int gid, int[] gids, 127 boolean enableDebugger, int[][] rlimits) { 128 int debugFlags = enableDebugger ? DEBUG_ENABLE_DEBUGGER : 0; 129 return forkAndSpecialize(uid, gid, gids, debugFlags, rlimits); 130 } 131 } 132