OSDN Git Service

am e54dbc4a: am 3ba16c1d: LayoutLib: fix layout when nav bar is absent. [DO NOT MERGE]
[android-x86/frameworks-base.git] / core / java / com / android / internal / os / Zygote.java
1 /*
2  * Copyright (C) 2014 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 com.android.internal.os;
18
19
20 import dalvik.system.ZygoteHooks;
21 import android.system.ErrnoException;
22 import android.system.Os;
23
24 /** @hide */
25 public final class Zygote {
26     /*
27     * Bit values for "debugFlags" argument.  The definitions are duplicated
28     * in the native code.
29     */
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 AOT compiler and JIT */
38     public static final int DEBUG_ENABLE_SAFEMODE   = 1 << 3;
39     /** Enable logging of third-party JNI activity. */
40     public static final int DEBUG_ENABLE_JNI_LOGGING = 1 << 4;
41     /** enable the JIT compiler */
42     public static final int DEBUG_ENABLE_JIT         = 1 << 5;
43
44
45     /** No external storage should be mounted. */
46     public static final int MOUNT_EXTERNAL_NONE = 0;
47     /** Single-user external storage should be mounted. */
48     public static final int MOUNT_EXTERNAL_SINGLEUSER = 1;
49     /** Multi-user external storage should be mounted. */
50     public static final int MOUNT_EXTERNAL_MULTIUSER = 2;
51     /** All multi-user external storage should be mounted. */
52     public static final int MOUNT_EXTERNAL_MULTIUSER_ALL = 3;
53
54     private static final ZygoteHooks VM_HOOKS = new ZygoteHooks();
55
56     private Zygote() {}
57
58     /**
59      * Forks a new VM instance.  The current VM must have been started
60      * with the -Xzygote flag. <b>NOTE: new instance keeps all
61      * root capabilities. The new process is expected to call capset()</b>.
62      *
63      * @param uid the UNIX uid that the new process should setuid() to after
64      * fork()ing and and before spawning any threads.
65      * @param gid the UNIX gid that the new process should setgid() to after
66      * fork()ing and and before spawning any threads.
67      * @param gids null-ok; a list of UNIX gids that the new process should
68      * setgroups() to after fork and before spawning any threads.
69      * @param debugFlags bit flags that enable debugging features.
70      * @param rlimits null-ok an array of rlimit tuples, with the second
71      * dimension having a length of 3 and representing
72      * (resource, rlim_cur, rlim_max). These are set via the posix
73      * setrlimit(2) call.
74      * @param seInfo null-ok a string specifying SELinux information for
75      * the new process.
76      * @param niceName null-ok a string specifying the process name.
77      * @param fdsToClose an array of ints, holding one or more POSIX
78      * file descriptor numbers that are to be closed by the child
79      * (and replaced by /dev/null) after forking.  An integer value
80      * of -1 in any entry in the array means "ignore this one".
81      * @param instructionSet null-ok the instruction set to use.
82      * @param appDataDir null-ok the data directory of the app.
83      *
84      * @return 0 if this is the child, pid of the child
85      * if this is the parent, or -1 on error.
86      */
87     public static int forkAndSpecialize(int uid, int gid, int[] gids, int debugFlags,
88           int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose,
89           String instructionSet, String appDataDir) {
90         VM_HOOKS.preFork();
91         int pid = nativeForkAndSpecialize(
92                   uid, gid, gids, debugFlags, rlimits, mountExternal, seInfo, niceName, fdsToClose,
93                   instructionSet, appDataDir);
94         VM_HOOKS.postForkCommon();
95         return pid;
96     }
97
98     native private static int nativeForkAndSpecialize(int uid, int gid, int[] gids,int debugFlags,
99           int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose,
100           String instructionSet, String appDataDir);
101
102     /**
103      * Special method to start the system server process. In addition to the
104      * common actions performed in forkAndSpecialize, the pid of the child
105      * process is recorded such that the death of the child process will cause
106      * zygote to exit.
107      *
108      * @param uid the UNIX uid that the new process should setuid() to after
109      * fork()ing and and before spawning any threads.
110      * @param gid the UNIX gid that the new process should setgid() to after
111      * fork()ing and and before spawning any threads.
112      * @param gids null-ok; a list of UNIX gids that the new process should
113      * setgroups() to after fork and before spawning any threads.
114      * @param debugFlags bit flags that enable debugging features.
115      * @param rlimits null-ok an array of rlimit tuples, with the second
116      * dimension having a length of 3 and representing
117      * (resource, rlim_cur, rlim_max). These are set via the posix
118      * setrlimit(2) call.
119      * @param permittedCapabilities argument for setcap()
120      * @param effectiveCapabilities argument for setcap()
121      *
122      * @return 0 if this is the child, pid of the child
123      * if this is the parent, or -1 on error.
124      */
125     public static int forkSystemServer(int uid, int gid, int[] gids, int debugFlags,
126             int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) {
127         VM_HOOKS.preFork();
128         int pid = nativeForkSystemServer(
129                 uid, gid, gids, debugFlags, rlimits, permittedCapabilities, effectiveCapabilities);
130         VM_HOOKS.postForkCommon();
131         return pid;
132     }
133
134     native private static int nativeForkSystemServer(int uid, int gid, int[] gids, int debugFlags,
135             int[][] rlimits, long permittedCapabilities, long effectiveCapabilities);
136
137     private static void callPostForkChildHooks(int debugFlags, String instructionSet) {
138         VM_HOOKS.postForkChild(debugFlags, instructionSet);
139     }
140
141
142     /**
143      * Executes "/system/bin/sh -c &lt;command&gt;" using the exec() system call.
144      * This method throws a runtime exception if exec() failed, otherwise, this
145      * method never returns.
146      *
147      * @param command The shell command to execute.
148      */
149     public static void execShell(String command) {
150         String[] args = { "/system/bin/sh", "-c", command };
151         try {
152             Os.execv(args[0], args);
153         } catch (ErrnoException e) {
154             throw new RuntimeException(e);
155         }
156     }
157
158     /**
159      * Appends quotes shell arguments to the specified string builder.
160      * The arguments are quoted using single-quotes, escaped if necessary,
161      * prefixed with a space, and appended to the command.
162      *
163      * @param command A string builder for the shell command being constructed.
164      * @param args An array of argument strings to be quoted and appended to the command.
165      * @see #execShell(String)
166      */
167     public static void appendQuotedShellArgs(StringBuilder command, String[] args) {
168         for (String arg : args) {
169             command.append(" '").append(arg.replace("'", "'\\''")).append("'");
170         }
171     }
172 }