blob: 3ecbf230db6ee8c5b6438bc4effe4c0093c31a9b [file] [log] [blame]
Narayan Kamath973b4662014-03-31 13:41:26 +01001/*
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
17package com.android.internal.os;
18
Chris Wailes2be26262019-01-11 16:14:43 -080019import static com.android.internal.os.ZygoteConnectionConstants.MAX_ZYGOTE_ARGC;
20
21import android.net.Credentials;
22import android.os.FactoryTest;
Jeff Sharkeyace874b2017-09-07 15:27:33 -060023import android.os.IVold;
Chris Wailes2be26262019-01-11 16:14:43 -080024import android.os.Process;
25import android.os.SystemProperties;
Narayan Kamathfbb32f62015-06-12 15:34:35 +010026import android.os.Trace;
Elliott Hughes860c5912014-04-28 19:19:13 -070027import android.system.ErrnoException;
28import android.system.Os;
Chris Wailes2be26262019-01-11 16:14:43 -080029import android.util.Log;
Narayan Kamath973b4662014-03-31 13:41:26 +010030
Jeff Sharkeyace874b2017-09-07 15:27:33 -060031import dalvik.system.ZygoteHooks;
Tobias Sargeantb9679dc2016-01-19 16:34:54 +000032
Chris Wailes2be26262019-01-11 16:14:43 -080033import java.io.BufferedReader;
34import java.io.IOException;
35
Narayan Kamath973b4662014-03-31 13:41:26 +010036/** @hide */
37public final class Zygote {
38 /*
Nicolas Geoffray81edac42017-09-07 14:13:29 +010039 * Bit values for "runtimeFlags" argument. The definitions are duplicated
Narayan Kamath973b4662014-03-31 13:41:26 +010040 * in the native code.
41 */
42
43 /** enable debugging over JDWP */
Nicolas Geoffray347b1df2016-12-20 14:05:05 +000044 public static final int DEBUG_ENABLE_JDWP = 1;
Narayan Kamath973b4662014-03-31 13:41:26 +010045 /** enable JNI checks */
46 public static final int DEBUG_ENABLE_CHECKJNI = 1 << 1;
47 /** enable Java programming language "assert" statements */
48 public static final int DEBUG_ENABLE_ASSERT = 1 << 2;
Mathieu Chartier7a490282015-03-17 09:51:36 -070049 /** disable the AOT compiler and JIT */
Narayan Kamath973b4662014-03-31 13:41:26 +010050 public static final int DEBUG_ENABLE_SAFEMODE = 1 << 3;
51 /** Enable logging of third-party JNI activity. */
52 public static final int DEBUG_ENABLE_JNI_LOGGING = 1 << 4;
David Srbecky065075e2015-05-28 17:16:09 +010053 /** Force generation of native debugging information. */
Nicolas Geoffray9abbf452015-11-05 11:29:42 +000054 public static final int DEBUG_GENERATE_DEBUG_INFO = 1 << 5;
Tamas Berghammerdf6cb282016-01-29 12:07:00 +000055 /** Always use JIT-ed code. */
56 public static final int DEBUG_ALWAYS_JIT = 1 << 6;
Nicolas Geoffray347b1df2016-12-20 14:05:05 +000057 /** Make the code native debuggable by turning off some optimizations. */
Tamas Berghammerdf6cb282016-01-29 12:07:00 +000058 public static final int DEBUG_NATIVE_DEBUGGABLE = 1 << 7;
Nicolas Geoffray347b1df2016-12-20 14:05:05 +000059 /** Make the code Java debuggable by turning off some optimizations. */
60 public static final int DEBUG_JAVA_DEBUGGABLE = 1 << 8;
Mathieu Chartier7a490282015-03-17 09:51:36 -070061
Nicolas Geoffray1f88ad62017-09-13 14:21:00 +010062 /** Turn off the verifier. */
63 public static final int DISABLE_VERIFIER = 1 << 9;
64 /** Only use oat files located in /system. Otherwise use dex/jar/apk . */
65 public static final int ONLY_USE_SYSTEM_OAT_FILES = 1 << 10;
David Srbecky156ed922018-01-30 14:37:37 +000066 /** Force generation of native debugging information for backtraces. */
Mathew Inwood16073b82018-03-23 10:05:01 +000067 public static final int DEBUG_GENERATE_MINI_DEBUG_INFO = 1 << 11;
68 /**
69 * Hidden API access restrictions. This is a mask for bits representing the API enforcement
70 * policy, defined by {@code @ApplicationInfo.HiddenApiEnforcementPolicy}.
71 */
72 public static final int API_ENFORCEMENT_POLICY_MASK = (1 << 12) | (1 << 13);
73 /**
74 * Bit shift for use with {@link #API_ENFORCEMENT_POLICY_MASK}.
75 *
76 * (flags & API_ENFORCEMENT_POLICY_MASK) >> API_ENFORCEMENT_POLICY_SHIFT gives
77 * @ApplicationInfo.ApiEnforcementPolicy values.
78 */
79 public static final int API_ENFORCEMENT_POLICY_SHIFT =
80 Integer.numberOfTrailingZeros(API_ENFORCEMENT_POLICY_MASK);
Calin Juravle8eb891b2018-05-03 19:51:18 -070081 /**
82 * Enable system server ART profiling.
83 */
84 public static final int PROFILE_SYSTEM_SERVER = 1 << 14;
Nicolas Geoffray1f88ad62017-09-13 14:21:00 +010085
Narayan Kamath973b4662014-03-31 13:41:26 +010086 /** No external storage should be mounted. */
Jeff Sharkeyace874b2017-09-07 15:27:33 -060087 public static final int MOUNT_EXTERNAL_NONE = IVold.REMOUNT_MODE_NONE;
Jeff Sharkey9527b222015-06-24 15:24:48 -070088 /** Default external storage should be mounted. */
Jeff Sharkeyace874b2017-09-07 15:27:33 -060089 public static final int MOUNT_EXTERNAL_DEFAULT = IVold.REMOUNT_MODE_DEFAULT;
Jeff Sharkey9527b222015-06-24 15:24:48 -070090 /** Read-only external storage should be mounted. */
Jeff Sharkeyace874b2017-09-07 15:27:33 -060091 public static final int MOUNT_EXTERNAL_READ = IVold.REMOUNT_MODE_READ;
Jeff Sharkey9527b222015-06-24 15:24:48 -070092 /** Read-write external storage should be mounted. */
Jeff Sharkeyace874b2017-09-07 15:27:33 -060093 public static final int MOUNT_EXTERNAL_WRITE = IVold.REMOUNT_MODE_WRITE;
Sudheer Shanka3a0df3b2018-12-12 12:43:43 -080094 /**
Sudheer Shanka0b6da532019-01-09 12:06:51 -080095 * Mount mode for apps that are already installed on the device before the isolated_storage
96 * feature is enabled.
97 */
98 public static final int MOUNT_EXTERNAL_LEGACY = IVold.REMOUNT_MODE_LEGACY;
99 /**
Sudheer Shanka3a0df3b2018-12-12 12:43:43 -0800100 * Mount mode for package installers which should give them access to
101 * all obb dirs in addition to their package sandboxes
102 */
103 public static final int MOUNT_EXTERNAL_INSTALLER = IVold.REMOUNT_MODE_INSTALLER;
Sudheer Shanka98cb3f02018-08-17 16:10:29 -0700104 /** Read-write external storage should be mounted instead of package sandbox */
105 public static final int MOUNT_EXTERNAL_FULL = IVold.REMOUNT_MODE_FULL;
Narayan Kamath973b4662014-03-31 13:41:26 +0100106
107 private static final ZygoteHooks VM_HOOKS = new ZygoteHooks();
108
Robert Sesekd0a190df2018-02-12 18:46:01 -0500109 /**
110 * An extraArg passed when a zygote process is forking a child-zygote, specifying a name
111 * in the abstract socket namespace. This socket name is what the new child zygote
112 * should listen for connections on.
113 */
114 public static final String CHILD_ZYGOTE_SOCKET_NAME_ARG = "--zygote-socket=";
115
Martijn Coenen7e6fa672018-11-05 11:45:26 +0100116 /**
117 * An extraArg passed when a zygote process is forking a child-zygote, specifying the
118 * requested ABI for the child Zygote.
119 */
120 public static final String CHILD_ZYGOTE_ABI_LIST_ARG = "--abi-list=";
121
Martijn Coenen86f08a52019-01-03 16:23:01 +0100122 /**
123 * An extraArg passed when a zygote process is forking a child-zygote, specifying the
124 * start of the UID range the children of the Zygote may setuid()/setgid() to. This
125 * will be enforced with a seccomp filter.
126 */
127 public static final String CHILD_ZYGOTE_UID_RANGE_START = "--uid-range-start=";
128
129 /**
130 * An extraArg passed when a zygote process is forking a child-zygote, specifying the
131 * end of the UID range the children of the Zygote may setuid()/setgid() to. This
132 * will be enforced with a seccomp filter.
133 */
134 public static final String CHILD_ZYGOTE_UID_RANGE_END = "--uid-range-end=";
135
Chris Wailes2be26262019-01-11 16:14:43 -0800136 /** a prototype instance for a future List.toArray() */
137 protected static final int[][] INT_ARRAY_2D = new int[0][0];
138
Narayan Kamath973b4662014-03-31 13:41:26 +0100139 private Zygote() {}
140
Victor Hsiehc8176ef2018-01-08 12:43:00 -0800141 /** Called for some security initialization before any fork. */
Chris Wailesaa1c9622019-01-10 16:55:32 -0800142 static native void nativeSecurityInit();
Victor Hsiehc8176ef2018-01-08 12:43:00 -0800143
Narayan Kamath973b4662014-03-31 13:41:26 +0100144 /**
145 * Forks a new VM instance. The current VM must have been started
146 * with the -Xzygote flag. <b>NOTE: new instance keeps all
147 * root capabilities. The new process is expected to call capset()</b>.
148 *
149 * @param uid the UNIX uid that the new process should setuid() to after
150 * fork()ing and and before spawning any threads.
151 * @param gid the UNIX gid that the new process should setgid() to after
152 * fork()ing and and before spawning any threads.
153 * @param gids null-ok; a list of UNIX gids that the new process should
154 * setgroups() to after fork and before spawning any threads.
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100155 * @param runtimeFlags bit flags that enable ART features.
Narayan Kamath973b4662014-03-31 13:41:26 +0100156 * @param rlimits null-ok an array of rlimit tuples, with the second
157 * dimension having a length of 3 and representing
158 * (resource, rlim_cur, rlim_max). These are set via the posix
159 * setrlimit(2) call.
160 * @param seInfo null-ok a string specifying SELinux information for
161 * the new process.
162 * @param niceName null-ok a string specifying the process name.
163 * @param fdsToClose an array of ints, holding one or more POSIX
164 * file descriptor numbers that are to be closed by the child
165 * (and replaced by /dev/null) after forking. An integer value
166 * of -1 in any entry in the array means "ignore this one".
Andreas Gampe8dfa1782017-01-05 12:45:58 -0800167 * @param fdsToIgnore null-ok an array of ints, either null or holding
168 * one or more POSIX file descriptor numbers that are to be ignored
169 * in the file descriptor table check.
Robert Sesekd0a190df2018-02-12 18:46:01 -0500170 * @param startChildZygote if true, the new child process will itself be a
171 * new zygote process.
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700172 * @param instructionSet null-ok the instruction set to use.
jgu212eacd062014-09-10 06:55:07 -0400173 * @param appDataDir null-ok the data directory of the app.
Narayan Kamath973b4662014-03-31 13:41:26 +0100174 *
175 * @return 0 if this is the child, pid of the child
176 * if this is the parent, or -1 on error.
177 */
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100178 public static int forkAndSpecialize(int uid, int gid, int[] gids, int runtimeFlags,
Sudheer Shanka3f0645b2018-09-18 13:07:59 -0700179 int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose,
180 int[] fdsToIgnore, boolean startChildZygote, String instructionSet, String appDataDir,
181 String packageName, String[] packagesForUid, String[] visibleVolIds) {
Narayan Kamath973b4662014-03-31 13:41:26 +0100182 VM_HOOKS.preFork();
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800183 // Resets nice priority for zygote process.
184 resetNicePriority();
Narayan Kamath973b4662014-03-31 13:41:26 +0100185 int pid = nativeForkAndSpecialize(
Chris Wailesaa1c9622019-01-10 16:55:32 -0800186 uid, gid, gids, runtimeFlags, rlimits, mountExternal, seInfo, niceName, fdsToClose,
187 fdsToIgnore, startChildZygote, instructionSet, appDataDir, packageName,
188 packagesForUid, visibleVolIds);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100189 // Enable tracing as soon as possible for the child process.
190 if (pid == 0) {
Andreas Gampe8f4eab22017-09-13 18:16:13 -0700191 Trace.setTracingEnabled(true, runtimeFlags);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100192
193 // Note that this event ends at the end of handleChildProc,
194 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "PostFork");
195 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100196 VM_HOOKS.postForkCommon();
197 return pid;
198 }
199
Chris Wailesaa1c9622019-01-10 16:55:32 -0800200 private static native int nativeForkAndSpecialize(int uid, int gid, int[] gids,
201 int runtimeFlags, int[][] rlimits, int mountExternal, String seInfo, String niceName,
202 int[] fdsToClose, int[] fdsToIgnore, boolean startChildZygote, String instructionSet,
203 String appDataDir, String packageName, String[] packagesForUid, String[] visibleVolIds);
204
205 private static native void nativeSpecializeBlastula(int uid, int gid, int[] gids,
206 int runtimeFlags, int[][] rlimits, int mountExternal, String seInfo, String niceName,
207 boolean startChildZygote, String instructionSet, String appDataDir, String packageName,
208 String[] packagesForUid, String[] visibleVolIds);
Narayan Kamath973b4662014-03-31 13:41:26 +0100209
210 /**
Christopher Ferris2980de42017-06-20 16:13:40 -0700211 * Called to do any initialization before starting an application.
212 */
Chris Wailesaa1c9622019-01-10 16:55:32 -0800213 static native void nativePreApplicationInit();
Christopher Ferris2980de42017-06-20 16:13:40 -0700214
215 /**
Narayan Kamath973b4662014-03-31 13:41:26 +0100216 * Special method to start the system server process. In addition to the
217 * common actions performed in forkAndSpecialize, the pid of the child
218 * process is recorded such that the death of the child process will cause
219 * zygote to exit.
220 *
221 * @param uid the UNIX uid that the new process should setuid() to after
222 * fork()ing and and before spawning any threads.
223 * @param gid the UNIX gid that the new process should setgid() to after
224 * fork()ing and and before spawning any threads.
225 * @param gids null-ok; a list of UNIX gids that the new process should
226 * setgroups() to after fork and before spawning any threads.
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100227 * @param runtimeFlags bit flags that enable ART features.
Narayan Kamath973b4662014-03-31 13:41:26 +0100228 * @param rlimits null-ok an array of rlimit tuples, with the second
229 * dimension having a length of 3 and representing
230 * (resource, rlim_cur, rlim_max). These are set via the posix
231 * setrlimit(2) call.
232 * @param permittedCapabilities argument for setcap()
233 * @param effectiveCapabilities argument for setcap()
234 *
235 * @return 0 if this is the child, pid of the child
236 * if this is the parent, or -1 on error.
237 */
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100238 public static int forkSystemServer(int uid, int gid, int[] gids, int runtimeFlags,
Narayan Kamath973b4662014-03-31 13:41:26 +0100239 int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) {
240 VM_HOOKS.preFork();
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800241 // Resets nice priority for zygote process.
242 resetNicePriority();
Narayan Kamath973b4662014-03-31 13:41:26 +0100243 int pid = nativeForkSystemServer(
Chris Wailesaa1c9622019-01-10 16:55:32 -0800244 uid, gid, gids, runtimeFlags, rlimits,
245 permittedCapabilities, effectiveCapabilities);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100246 // Enable tracing as soon as we enter the system_server.
247 if (pid == 0) {
Andreas Gampe8f4eab22017-09-13 18:16:13 -0700248 Trace.setTracingEnabled(true, runtimeFlags);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100249 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100250 VM_HOOKS.postForkCommon();
251 return pid;
252 }
253
Chris Wailesaa1c9622019-01-10 16:55:32 -0800254 private static native int nativeForkSystemServer(int uid, int gid, int[] gids, int runtimeFlags,
Narayan Kamath973b4662014-03-31 13:41:26 +0100255 int[][] rlimits, long permittedCapabilities, long effectiveCapabilities);
256
doheon1.lee885b7422016-01-20 13:07:27 +0900257 /**
Robert Sesek54e387d2016-12-02 17:27:50 -0500258 * Lets children of the zygote inherit open file descriptors to this path.
259 */
Chris Wailesaa1c9622019-01-10 16:55:32 -0800260 protected static native void nativeAllowFileAcrossFork(String path);
Robert Sesek54e387d2016-12-02 17:27:50 -0500261
262 /**
Martijn Coenen86f08a52019-01-03 16:23:01 +0100263 * Installs a seccomp filter that limits setresuid()/setresgid() to the passed-in range
264 * @param uidGidMin The smallest allowed uid/gid
265 * @param uidGidMax The largest allowed uid/gid
266 */
267 native protected static void nativeInstallSeccompUidGidFilter(int uidGidMin, int uidGidMax);
268
269 /**
doheon1.lee885b7422016-01-20 13:07:27 +0900270 * Zygote unmount storage space on initializing.
271 * This method is called once.
272 */
Chris Wailesaa1c9622019-01-10 16:55:32 -0800273 protected static native void nativeUnmountStorageOnInit();
274
275 protected static native void nativeGetSocketFDs(boolean isPrimary);
276
277 private static native int nativeGetBlastulaPoolCount();
278
279 private static native int nativeGetBlastulaPoolEventFD();
280
281 private static native int nativeForkBlastula(int readPipeFD,
282 int writePipeFD,
283 int[] sessionSocketRawFDs);
284
285 private static native int[] nativeGetBlastulaPipeFDs();
286
287 private static native boolean nativeRemoveBlastulaTableEntry(int blastulaPID);
288
Chris Wailes2be26262019-01-11 16:14:43 -0800289 /**
290 * uid 1000 (Process.SYSTEM_UID) may specify any uid &gt; 1000 in normal
291 * operation. It may also specify any gid and setgroups() list it chooses.
292 * In factory test mode, it may specify any UID.
293 *
294 * @param args non-null; zygote spawner arguments
295 * @param peer non-null; peer credentials
296 * @throws ZygoteSecurityException
297 */
298 protected static void applyUidSecurityPolicy(ZygoteArguments args, Credentials peer)
299 throws ZygoteSecurityException {
300
301 if (peer.getUid() == Process.SYSTEM_UID) {
302 /* In normal operation, SYSTEM_UID can only specify a restricted
303 * set of UIDs. In factory test mode, SYSTEM_UID may specify any uid.
304 */
305 boolean uidRestricted = FactoryTest.getMode() == FactoryTest.FACTORY_TEST_OFF;
306
307 if (uidRestricted && args.mUidSpecified && (args.mUid < Process.SYSTEM_UID)) {
308 throw new ZygoteSecurityException(
309 "System UID may not launch process with UID < "
310 + Process.SYSTEM_UID);
311 }
312 }
313
314 // If not otherwise specified, uid and gid are inherited from peer
315 if (!args.mUidSpecified) {
316 args.mUid = peer.getUid();
317 args.mUidSpecified = true;
318 }
319 if (!args.mGidSpecified) {
320 args.mGid = peer.getGid();
321 args.mGidSpecified = true;
322 }
323 }
324
325 /**
326 * Applies debugger system properties to the zygote arguments.
327 *
328 * If "ro.debuggable" is "1", all apps are debuggable. Otherwise,
329 * the debugger state is specified via the "--enable-jdwp" flag
330 * in the spawn request.
331 *
332 * @param args non-null; zygote spawner args
333 */
334 protected static void applyDebuggerSystemProperty(ZygoteArguments args) {
335 if (RoSystemProperties.DEBUGGABLE) {
336 args.mRuntimeFlags |= Zygote.DEBUG_ENABLE_JDWP;
337 }
338 }
339
340 /**
341 * Applies zygote security policy.
342 * Based on the credentials of the process issuing a zygote command:
343 * <ol>
344 * <li> uid 0 (root) may specify --invoke-with to launch Zygote with a
345 * wrapper command.
346 * <li> Any other uid may not specify any invoke-with argument.
347 * </ul>
348 *
349 * @param args non-null; zygote spawner arguments
350 * @param peer non-null; peer credentials
351 * @throws ZygoteSecurityException
352 */
353 protected static void applyInvokeWithSecurityPolicy(ZygoteArguments args, Credentials peer)
354 throws ZygoteSecurityException {
355 int peerUid = peer.getUid();
356
357 if (args.mInvokeWith != null && peerUid != 0
358 && (args.mRuntimeFlags & Zygote.DEBUG_ENABLE_JDWP) == 0) {
359 throw new ZygoteSecurityException("Peer is permitted to specify an"
360 + "explicit invoke-with wrapper command only for debuggable"
361 + "applications.");
362 }
363 }
364
365 /**
366 * Applies invoke-with system properties to the zygote arguments.
367 *
368 * @param args non-null; zygote args
369 */
370 protected static void applyInvokeWithSystemProperty(ZygoteArguments args) {
371 if (args.mInvokeWith == null && args.mNiceName != null) {
372 String property = "wrap." + args.mNiceName;
373 args.mInvokeWith = SystemProperties.get(property);
374 if (args.mInvokeWith != null && args.mInvokeWith.length() == 0) {
375 args.mInvokeWith = null;
376 }
377 }
378 }
379
380 /**
381 * Reads an argument list from the provided socket
382 * @return Argument list or null if EOF is reached
383 * @throws IOException passed straight through
384 */
385 static String[] readArgumentList(BufferedReader socketReader) throws IOException {
386
387 /**
388 * See android.os.Process.zygoteSendArgsAndGetPid()
389 * Presently the wire format to the zygote process is:
390 * a) a count of arguments (argc, in essence)
391 * b) a number of newline-separated argument strings equal to count
392 *
393 * After the zygote process reads these it will write the pid of
394 * the child or -1 on failure.
395 */
396
397 int argc;
398
399 try {
400 String argc_string = socketReader.readLine();
401
402 if (argc_string == null) {
403 // EOF reached.
404 return null;
405 }
406 argc = Integer.parseInt(argc_string);
407
408 } catch (NumberFormatException ex) {
409 Log.e("Zygote", "Invalid Zygote wire format: non-int at argc");
410 throw new IOException("Invalid wire format");
411 }
412
413 // See bug 1092107: large argc can be used for a DOS attack
414 if (argc > MAX_ZYGOTE_ARGC) {
415 throw new IOException("Max arg count exceeded");
416 }
417
418 String[] args = new String[argc];
419 for (int arg_index = 0; arg_index < argc; arg_index++) {
420 args[arg_index] = socketReader.readLine();
421 if (args[arg_index] == null) {
422 // We got an unexpected EOF.
423 throw new IOException("Truncated request");
424 }
425 }
426
427 return args;
428 }
429
doheon1.lee885b7422016-01-20 13:07:27 +0900430
Orion Hodson46724e72018-10-19 13:05:33 +0100431 private static void callPostForkSystemServerHooks() {
432 // SystemServer specific post fork hooks run before child post fork hooks.
433 VM_HOOKS.postForkSystemServer();
434 }
435
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100436 private static void callPostForkChildHooks(int runtimeFlags, boolean isSystemServer,
Robert Sesekd0a190df2018-02-12 18:46:01 -0500437 boolean isZygote, String instructionSet) {
438 VM_HOOKS.postForkChild(runtimeFlags, isSystemServer, isZygote, instructionSet);
Narayan Kamath973b4662014-03-31 13:41:26 +0100439 }
440
Narayan Kamathb49996d2017-02-06 20:24:08 +0000441 /**
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800442 * Resets the calling thread priority to the default value (Thread.NORM_PRIORITY
443 * or nice value 0). This updates both the priority value in java.lang.Thread and
444 * the nice value (setpriority).
Narayan Kamathb49996d2017-02-06 20:24:08 +0000445 */
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800446 static void resetNicePriority() {
447 Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
448 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100449
450 /**
451 * Executes "/system/bin/sh -c &lt;command&gt;" using the exec() system call.
452 * This method throws a runtime exception if exec() failed, otherwise, this
453 * method never returns.
454 *
455 * @param command The shell command to execute.
456 */
457 public static void execShell(String command) {
458 String[] args = { "/system/bin/sh", "-c", command };
459 try {
Elliott Hughes860c5912014-04-28 19:19:13 -0700460 Os.execv(args[0], args);
Narayan Kamath973b4662014-03-31 13:41:26 +0100461 } catch (ErrnoException e) {
462 throw new RuntimeException(e);
463 }
464 }
465
466 /**
467 * Appends quotes shell arguments to the specified string builder.
468 * The arguments are quoted using single-quotes, escaped if necessary,
469 * prefixed with a space, and appended to the command.
470 *
471 * @param command A string builder for the shell command being constructed.
472 * @param args An array of argument strings to be quoted and appended to the command.
473 * @see #execShell(String)
474 */
475 public static void appendQuotedShellArgs(StringBuilder command, String[] args) {
476 for (String arg : args) {
477 command.append(" '").append(arg.replace("'", "'\\''")).append("'");
478 }
479 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100480}