blob: 28797cebde144d9317f083e0fd406f2803a77c59 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.os;
18
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import android.net.LocalSocket;
Narayan Kamath973b4662014-03-31 13:41:26 +010020import android.net.LocalSocketAddress;
Elliott Hughes34385d32014-04-28 11:11:32 -070021import android.system.Os;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.util.Log;
Narayan Kamath973b4662014-03-31 13:41:26 +010023import com.android.internal.os.Zygote;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import java.io.BufferedWriter;
25import java.io.DataInputStream;
26import java.io.IOException;
27import java.io.OutputStreamWriter;
Narayan Kamath4444dcd2014-04-03 20:15:15 +010028import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import java.util.ArrayList;
Narayan Kamath4444dcd2014-04-03 20:15:15 +010030import java.util.Arrays;
31import java.util.List;
Jeff Hao406ec152013-07-30 10:13:41 -070032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033/*package*/ class ZygoteStartFailedEx extends Exception {
Narayan Kamath64cd9072014-05-13 13:35:14 +010034 ZygoteStartFailedEx(String s) {
35 super(s);
36 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
Narayan Kamath64cd9072014-05-13 13:35:14 +010038 ZygoteStartFailedEx(Throwable cause) {
39 super(cause);
40 }
41
42 ZygoteStartFailedEx(String s, Throwable cause) {
43 super(s, cause);
44 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045}
46
47/**
48 * Tools for managing OS processes.
49 */
50public class Process {
51 private static final String LOG_TAG = "Process";
52
Narayan Kamath64cd9072014-05-13 13:35:14 +010053 /**
54 * @hide for internal use only.
55 */
56 public static final String ZYGOTE_SOCKET = "zygote";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
Narayan Kamath64cd9072014-05-13 13:35:14 +010058 /**
59 * @hide for internal use only.
60 */
61 public static final String SECONDARY_ZYGOTE_SOCKET = "zygote_secondary";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
63 /**
64 * Defines the UID/GID under which system code runs.
65 */
66 public static final int SYSTEM_UID = 1000;
67
68 /**
69 * Defines the UID/GID under which the telephony code runs.
70 */
71 public static final int PHONE_UID = 1001;
72
73 /**
Dianne Hackborn854060af2009-07-09 18:14:31 -070074 * Defines the UID/GID for the user shell.
75 * @hide
76 */
77 public static final int SHELL_UID = 2000;
78
79 /**
Mike Lockwoodd42685d2009-09-03 09:25:22 -040080 * Defines the UID/GID for the log group.
81 * @hide
82 */
83 public static final int LOG_UID = 1007;
84
85 /**
Amith Yamasanid1582142009-07-08 20:04:55 -070086 * Defines the UID/GID for the WIFI supplicant process.
87 * @hide
88 */
89 public static final int WIFI_UID = 1010;
90
91 /**
Glenn Kasten8b7d1b42011-07-13 16:23:22 -070092 * Defines the UID/GID for the mediaserver process.
93 * @hide
94 */
95 public static final int MEDIA_UID = 1013;
96
97 /**
Jeff Sharkey5294a2f2012-04-24 17:07:22 -070098 * Defines the UID/GID for the DRM process.
99 * @hide
100 */
101 public static final int DRM_UID = 1019;
102
103 /**
Kenny Root26993b32012-03-19 15:07:51 -0700104 * Defines the UID/GID for the group that controls VPN services.
105 * @hide
106 */
107 public static final int VPN_UID = 1016;
108
109 /**
Nick Pellycd0e8392010-10-13 17:25:24 -0700110 * Defines the UID/GID for the NFC service process.
111 * @hide
112 */
Nick Pellya5cb9f42011-11-21 14:54:46 -0800113 public static final int NFC_UID = 1027;
Nick Pellycd0e8392010-10-13 17:25:24 -0700114
115 /**
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800116 * Defines the UID/GID for the Bluetooth service process.
117 * @hide
118 */
119 public static final int BLUETOOTH_UID = 1002;
120
121 /**
Mike Lockwooddcaa10c2010-12-16 12:50:44 -0800122 * Defines the GID for the group that allows write access to the internal media storage.
123 * @hide
124 */
125 public static final int MEDIA_RW_GID = 1023;
126
127 /**
Jeff Sharkey184a0102013-07-10 16:19:52 -0700128 * Access to installed package details
129 * @hide
130 */
131 public static final int PACKAGE_INFO_GID = 1032;
132
133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * Defines the start of a range of UIDs (and GIDs), going from this
135 * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning
136 * to applications.
137 */
138 public static final int FIRST_APPLICATION_UID = 10000;
Jeff Sharkey184a0102013-07-10 16:19:52 -0700139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 /**
141 * Last of application-specific UIDs starting at
142 * {@link #FIRST_APPLICATION_UID}.
143 */
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800144 public static final int LAST_APPLICATION_UID = 19999;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800145
146 /**
147 * First uid used for fully isolated sandboxed processes (with no permissions of their own)
148 * @hide
149 */
150 public static final int FIRST_ISOLATED_UID = 99000;
151
152 /**
153 * Last uid used for fully isolated sandboxed processes (with no permissions of their own)
154 * @hide
155 */
156 public static final int LAST_ISOLATED_UID = 99999;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157
158 /**
Kenny Roote091f222012-09-11 15:01:26 -0700159 * First gid for applications to share resources. Used when forward-locking
160 * is enabled but all UserHandles need to be able to read the resources.
161 * @hide
162 */
163 public static final int FIRST_SHARED_APPLICATION_GID = 50000;
164
165 /**
166 * Last gid for applications to share resources. Used when forward-locking
167 * is enabled but all UserHandles need to be able to read the resources.
168 * @hide
169 */
170 public static final int LAST_SHARED_APPLICATION_GID = 59999;
171
172 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 * Standard priority of application threads.
174 * Use with {@link #setThreadPriority(int)} and
175 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
176 * {@link java.lang.Thread} class.
177 */
178 public static final int THREAD_PRIORITY_DEFAULT = 0;
179
180 /*
181 * ***************************************
182 * ** Keep in sync with utils/threads.h **
183 * ***************************************
184 */
185
186 /**
187 * Lowest available thread priority. Only for those who really, really
188 * don't want to run if anything else is happening.
189 * Use with {@link #setThreadPriority(int)} and
190 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
191 * {@link java.lang.Thread} class.
192 */
193 public static final int THREAD_PRIORITY_LOWEST = 19;
194
195 /**
196 * Standard priority background threads. This gives your thread a slightly
197 * lower than normal priority, so that it will have less chance of impacting
198 * the responsiveness of the user interface.
199 * Use with {@link #setThreadPriority(int)} and
200 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
201 * {@link java.lang.Thread} class.
202 */
203 public static final int THREAD_PRIORITY_BACKGROUND = 10;
204
205 /**
206 * Standard priority of threads that are currently running a user interface
207 * that the user is interacting with. Applications can not normally
208 * change to this priority; the system will automatically adjust your
209 * application threads as the user moves through the UI.
210 * Use with {@link #setThreadPriority(int)} and
211 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
212 * {@link java.lang.Thread} class.
213 */
214 public static final int THREAD_PRIORITY_FOREGROUND = -2;
215
216 /**
217 * Standard priority of system display threads, involved in updating
218 * the user interface. Applications can not
219 * normally change to this priority.
220 * Use with {@link #setThreadPriority(int)} and
221 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
222 * {@link java.lang.Thread} class.
223 */
224 public static final int THREAD_PRIORITY_DISPLAY = -4;
225
226 /**
227 * Standard priority of the most important display threads, for compositing
228 * the screen and retrieving input events. Applications can not normally
229 * change to this priority.
230 * Use with {@link #setThreadPriority(int)} and
231 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
232 * {@link java.lang.Thread} class.
233 */
234 public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8;
235
236 /**
237 * Standard priority of audio threads. Applications can not normally
238 * change to this priority.
239 * Use with {@link #setThreadPriority(int)} and
240 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
241 * {@link java.lang.Thread} class.
242 */
243 public static final int THREAD_PRIORITY_AUDIO = -16;
244
245 /**
246 * Standard priority of the most important audio threads.
247 * Applications can not normally change to this priority.
248 * Use with {@link #setThreadPriority(int)} and
249 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
250 * {@link java.lang.Thread} class.
251 */
252 public static final int THREAD_PRIORITY_URGENT_AUDIO = -19;
253
254 /**
255 * Minimum increment to make a priority more favorable.
256 */
257 public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
258
259 /**
260 * Minimum increment to make a priority less favorable.
261 */
262 public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
263
San Mehate9d376b2009-04-21 14:06:36 -0700264 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700265 * Default scheduling policy
266 * @hide
267 */
268 public static final int SCHED_OTHER = 0;
269
270 /**
271 * First-In First-Out scheduling policy
272 * @hide
273 */
274 public static final int SCHED_FIFO = 1;
275
276 /**
277 * Round-Robin scheduling policy
278 * @hide
279 */
280 public static final int SCHED_RR = 2;
281
282 /**
283 * Batch scheduling policy
284 * @hide
285 */
286 public static final int SCHED_BATCH = 3;
287
288 /**
289 * Idle scheduling policy
290 * @hide
291 */
292 public static final int SCHED_IDLE = 5;
293
Glenn Kastenf1b56442012-03-15 16:33:43 -0700294 // Keep in sync with SP_* constants of enum type SchedPolicy
295 // declared in system/core/include/cutils/sched_policy.h,
296 // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
San Mehate9d376b2009-04-21 14:06:36 -0700297
298 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700299 * Default thread group -
300 * has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
301 * When used with setProcessGroup(), the group of each thread in the process
302 * is conditionally changed based on that thread's current priority, as follows:
303 * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
304 * are moved to foreground thread group. All other threads are left unchanged.
305 * @hide
306 */
307 public static final int THREAD_GROUP_DEFAULT = -1;
308
309 /**
310 * Background thread group - All threads in
San Mehate9d376b2009-04-21 14:06:36 -0700311 * this group are scheduled with a reduced share of the CPU.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700312 * Value is same as constant SP_BACKGROUND of enum SchedPolicy.
313 * FIXME rename to THREAD_GROUP_BACKGROUND.
San Mehate9d376b2009-04-21 14:06:36 -0700314 * @hide
315 */
Glenn Kastenf1b56442012-03-15 16:33:43 -0700316 public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0;
San Mehate9d376b2009-04-21 14:06:36 -0700317
318 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700319 * Foreground thread group - All threads in
320 * this group are scheduled with a normal share of the CPU.
321 * Value is same as constant SP_FOREGROUND of enum SchedPolicy.
322 * Not used at this level.
San Mehate9d376b2009-04-21 14:06:36 -0700323 * @hide
324 **/
Glenn Kastenf1b56442012-03-15 16:33:43 -0700325 private static final int THREAD_GROUP_FOREGROUND = 1;
San Mehate9d376b2009-04-21 14:06:36 -0700326
Glenn Kasten07b04652012-04-23 15:00:43 -0700327 /**
328 * System thread group.
329 * @hide
330 **/
331 public static final int THREAD_GROUP_SYSTEM = 2;
332
333 /**
334 * Application audio thread group.
335 * @hide
336 **/
337 public static final int THREAD_GROUP_AUDIO_APP = 3;
338
339 /**
340 * System audio thread group.
341 * @hide
342 **/
343 public static final int THREAD_GROUP_AUDIO_SYS = 4;
344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 public static final int SIGNAL_QUIT = 3;
346 public static final int SIGNAL_KILL = 9;
347 public static final int SIGNAL_USR1 = 10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100349 /**
350 * State for communicating with the zygote process.
Narayan Kamath64cd9072014-05-13 13:35:14 +0100351 *
352 * @hide for internal use only.
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100353 */
Narayan Kamath64cd9072014-05-13 13:35:14 +0100354 public static class ZygoteState {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100355 final LocalSocket socket;
356 final DataInputStream inputStream;
357 final BufferedWriter writer;
358 final List<String> abiList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100360 boolean mClosed;
361
362 private ZygoteState(LocalSocket socket, DataInputStream inputStream,
363 BufferedWriter writer, List<String> abiList) {
364 this.socket = socket;
365 this.inputStream = inputStream;
366 this.writer = writer;
367 this.abiList = abiList;
368 }
369
Narayan Kamath64cd9072014-05-13 13:35:14 +0100370 public static ZygoteState connect(String socketAddress) throws IOException {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100371 DataInputStream zygoteInputStream = null;
372 BufferedWriter zygoteWriter = null;
Narayan Kamath64cd9072014-05-13 13:35:14 +0100373 final LocalSocket zygoteSocket = new LocalSocket();
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100374
Narayan Kamath64cd9072014-05-13 13:35:14 +0100375 try {
376 zygoteSocket.connect(new LocalSocketAddress(socketAddress,
377 LocalSocketAddress.Namespace.RESERVED));
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100378
Narayan Kamath64cd9072014-05-13 13:35:14 +0100379 zygoteInputStream = new DataInputStream(zygoteSocket.getInputStream());
380
381 zygoteWriter = new BufferedWriter(new OutputStreamWriter(
382 zygoteSocket.getOutputStream()), 256);
383 } catch (IOException ex) {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100384 try {
Narayan Kamath64cd9072014-05-13 13:35:14 +0100385 zygoteSocket.close();
386 } catch (IOException ignore) {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100387 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100388
Narayan Kamath64cd9072014-05-13 13:35:14 +0100389 throw ex;
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100390 }
391
392 String abiListString = getAbiList(zygoteWriter, zygoteInputStream);
393 Log.i("Zygote", "Process: zygote socket opened, supported ABIS: " + abiListString);
394
395 return new ZygoteState(zygoteSocket, zygoteInputStream, zygoteWriter,
396 Arrays.asList(abiListString.split(",")));
397 }
398
399 boolean matches(String abi) {
400 return abiList.contains(abi);
401 }
402
Narayan Kamath64cd9072014-05-13 13:35:14 +0100403 public void close() {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100404 try {
405 socket.close();
406 } catch (IOException ex) {
407 Log.e(LOG_TAG,"I/O exception on routine close", ex);
408 }
409
410 mClosed = true;
411 }
412
413 boolean isClosed() {
414 return mClosed;
415 }
416 }
417
418 /**
419 * The state of the connection to the primary zygote.
420 */
421 static ZygoteState primaryZygoteState;
422
423 /**
424 * The state of the connection to the secondary zygote.
425 */
426 static ZygoteState secondaryZygoteState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427
428 /**
429 * Start a new process.
430 *
431 * <p>If processes are enabled, a new process is created and the
432 * static main() function of a <var>processClass</var> is executed there.
433 * The process will continue running after this function returns.
434 *
435 * <p>If processes are not enabled, a new thread in the caller's
436 * process is created and main() of <var>processClass</var> called there.
437 *
438 * <p>The niceName parameter, if not an empty string, is a custom name to
439 * give to the process instead of using processClass. This allows you to
440 * make easily identifyable processes even if you are using the same base
441 * <var>processClass</var> to start them.
442 *
443 * @param processClass The class to use as the process's main entry
444 * point.
445 * @param niceName A more readable name to use for the process.
446 * @param uid The user-id under which the process will run.
447 * @param gid The group-id under which the process will run.
448 * @param gids Additional group-ids associated with the process.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700449 * @param debugFlags Additional flags.
450 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalleybd19b9e2013-04-12 14:49:38 -0400451 * @param seInfo null-ok SELinux information for the new process.
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100452 * @param abi non-null the ABI this app should be started with.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 * @param zygoteArgs Additional arguments to supply to the zygote process.
454 *
Jeff Brown3f9dd282011-07-08 20:02:19 -0700455 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 * @throws RuntimeException on fatal start failure
457 *
458 * {@hide}
459 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700460 public static final ProcessStartResult start(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 final String niceName,
462 int uid, int gid, int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700463 int debugFlags, int mountExternal,
464 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500465 String seInfo,
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100466 String abi,
Jeff Brown10e89712011-07-08 18:52:57 -0700467 String[] zygoteArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 try {
Jeff Brown10e89712011-07-08 18:52:57 -0700469 return startViaZygote(processClass, niceName, uid, gid, gids,
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100470 debugFlags, mountExternal, targetSdkVersion, seInfo,
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100471 abi, zygoteArgs);
Jeff Brown10e89712011-07-08 18:52:57 -0700472 } catch (ZygoteStartFailedEx ex) {
473 Log.e(LOG_TAG,
474 "Starting VM process through Zygote failed");
475 throw new RuntimeException(
476 "Starting VM process through Zygote failed", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 }
479
480 /** retry interval for opening a zygote socket */
481 static final int ZYGOTE_RETRY_MILLIS = 500;
482
483 /**
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100484 * Queries the zygote for the list of ABIS it supports.
485 *
486 * @throws ZygoteStartFailedEx if the query failed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 */
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100488 private static String getAbiList(BufferedWriter writer, DataInputStream inputStream)
Narayan Kamath64cd9072014-05-13 13:35:14 +0100489 throws IOException {
490 // Each query starts with the argument count (1 in this case)
491 writer.write("1");
492 // ... followed by a new-line.
493 writer.newLine();
494 // ... followed by our only argument.
495 writer.write("--query-abi-list");
496 writer.newLine();
497 writer.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498
Narayan Kamath64cd9072014-05-13 13:35:14 +0100499 // The response is a length prefixed stream of ASCII bytes.
500 int numBytes = inputStream.readInt();
501 byte[] bytes = new byte[numBytes];
502 inputStream.readFully(bytes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503
Narayan Kamath64cd9072014-05-13 13:35:14 +0100504 return new String(bytes, StandardCharsets.US_ASCII);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 }
506
507 /**
508 * Sends an argument list to the zygote process, which starts a new child
509 * and returns the child's pid. Please note: the present implementation
510 * replaces newlines in the argument list with spaces.
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100511 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 * @throws ZygoteStartFailedEx if process start failed for any reason
513 */
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100514 private static ProcessStartResult zygoteSendArgsAndGetResult(
515 ZygoteState zygoteState, ArrayList<String> args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 try {
518 /**
519 * See com.android.internal.os.ZygoteInit.readArgumentList()
520 * Presently the wire format to the zygote process is:
521 * a) a count of arguments (argc, in essence)
522 * b) a number of newline-separated argument strings equal to count
523 *
524 * After the zygote process reads these it will write the pid of
Jeff Brown3f9dd282011-07-08 20:02:19 -0700525 * the child or -1 on failure, followed by boolean to
526 * indicate whether a wrapper process was used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 */
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100528 final BufferedWriter writer = zygoteState.writer;
529 final DataInputStream inputStream = zygoteState.inputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100531 writer.write(Integer.toString(args.size()));
532 writer.newLine();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533
534 int sz = args.size();
535 for (int i = 0; i < sz; i++) {
536 String arg = args.get(i);
537 if (arg.indexOf('\n') >= 0) {
538 throw new ZygoteStartFailedEx(
539 "embedded newlines not allowed");
540 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100541 writer.write(arg);
542 writer.newLine();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 }
544
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100545 writer.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546
547 // Should there be a timeout on this?
Jeff Brown3f9dd282011-07-08 20:02:19 -0700548 ProcessStartResult result = new ProcessStartResult();
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100549 result.pid = inputStream.readInt();
Jeff Brown3f9dd282011-07-08 20:02:19 -0700550 if (result.pid < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 throw new ZygoteStartFailedEx("fork() failed");
552 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100553 result.usingWrapper = inputStream.readBoolean();
Jeff Brown3f9dd282011-07-08 20:02:19 -0700554 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 } catch (IOException ex) {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100556 zygoteState.close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 throw new ZygoteStartFailedEx(ex);
558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
560
561 /**
562 * Starts a new process via the zygote mechanism.
563 *
564 * @param processClass Class name whose static main() to run
565 * @param niceName 'nice' process name to appear in ps
566 * @param uid a POSIX uid that the new process should setuid() to
567 * @param gid a POSIX gid that the new process shuold setgid() to
568 * @param gids null-ok; a list of supplementary group IDs that the
569 * new process should setgroup() to.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700570 * @param debugFlags Additional flags.
571 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalleybd19b9e2013-04-12 14:49:38 -0400572 * @param seInfo null-ok SELinux information for the new process.
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100573 * @param abi the ABI the process should use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 * @param extraArgs Additional arguments to supply to the zygote process.
Jeff Brown3f9dd282011-07-08 20:02:19 -0700575 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 * @throws ZygoteStartFailedEx if process start failed for any reason
577 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700578 private static ProcessStartResult startViaZygote(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 final String niceName,
580 final int uid, final int gid,
581 final int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700582 int debugFlags, int mountExternal,
583 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500584 String seInfo,
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100585 String abi,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 String[] extraArgs)
587 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 synchronized(Process.class) {
589 ArrayList<String> argsForZygote = new ArrayList<String>();
590
591 // --runtime-init, --setuid=, --setgid=,
592 // and --setgroups= must go first
593 argsForZygote.add("--runtime-init");
594 argsForZygote.add("--setuid=" + uid);
595 argsForZygote.add("--setgid=" + gid);
Elliott Hughesae07ecf2011-07-06 17:33:27 -0700596 if ((debugFlags & Zygote.DEBUG_ENABLE_JNI_LOGGING) != 0) {
597 argsForZygote.add("--enable-jni-logging");
598 }
Ben Cheng23085b72010-02-08 16:06:32 -0800599 if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) {
600 argsForZygote.add("--enable-safemode");
601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) {
603 argsForZygote.add("--enable-debugger");
604 }
605 if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) {
606 argsForZygote.add("--enable-checkjni");
607 }
608 if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) {
609 argsForZygote.add("--enable-assert");
610 }
Jeff Sharkey2bca8682012-08-22 13:59:58 -0700611 if (mountExternal == Zygote.MOUNT_EXTERNAL_MULTIUSER) {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700612 argsForZygote.add("--mount-external-multiuser");
Jeff Sharkeye217ee42012-08-28 16:23:01 -0700613 } else if (mountExternal == Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL) {
614 argsForZygote.add("--mount-external-multiuser-all");
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700615 }
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700616 argsForZygote.add("--target-sdk-version=" + targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617
618 //TODO optionally enable debuger
619 //argsForZygote.add("--enable-debugger");
620
621 // --setgroups is a comma-separated list
622 if (gids != null && gids.length > 0) {
623 StringBuilder sb = new StringBuilder();
624 sb.append("--setgroups=");
625
626 int sz = gids.length;
627 for (int i = 0; i < sz; i++) {
628 if (i != 0) {
629 sb.append(',');
630 }
631 sb.append(gids[i]);
632 }
633
634 argsForZygote.add(sb.toString());
635 }
636
637 if (niceName != null) {
638 argsForZygote.add("--nice-name=" + niceName);
639 }
640
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500641 if (seInfo != null) {
642 argsForZygote.add("--seinfo=" + seInfo);
643 }
644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 argsForZygote.add(processClass);
646
647 if (extraArgs != null) {
648 for (String arg : extraArgs) {
649 argsForZygote.add(arg);
650 }
651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100653 return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi), argsForZygote);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100656
657 /**
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100658 * Tries to open socket to Zygote process if not already open. If
659 * already open, does nothing. May block and retry.
660 */
661 private static ZygoteState openZygoteSocketIfNeeded(String abi) throws ZygoteStartFailedEx {
662 if (primaryZygoteState == null || primaryZygoteState.isClosed()) {
Narayan Kamath64cd9072014-05-13 13:35:14 +0100663 try {
664 primaryZygoteState = ZygoteState.connect(ZYGOTE_SOCKET);
665 } catch (IOException ioe) {
666 throw new ZygoteStartFailedEx("Error connecting to primary zygote", ioe);
667 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100668 }
669
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100670 if (primaryZygoteState.matches(abi)) {
671 return primaryZygoteState;
672 }
673
674 // The primary zygote didn't match. Try the secondary.
675 if (secondaryZygoteState == null || secondaryZygoteState.isClosed()) {
Narayan Kamath64cd9072014-05-13 13:35:14 +0100676 try {
677 secondaryZygoteState = ZygoteState.connect(SECONDARY_ZYGOTE_SOCKET);
678 } catch (IOException ioe) {
679 throw new ZygoteStartFailedEx("Error connecting to secondary zygote", ioe);
680 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100681 }
682
683 if (secondaryZygoteState.matches(abi)) {
684 return secondaryZygoteState;
685 }
686
687 throw new ZygoteStartFailedEx("Unsupported zygote ABI: " + abi);
688 }
689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 /**
691 * Returns elapsed milliseconds of the time this process has run.
692 * @return Returns the number of milliseconds this process has return.
693 */
694 public static final native long getElapsedCpuTime();
695
696 /**
697 * Returns the identifier of this process, which can be used with
698 * {@link #killProcess} and {@link #sendSignal}.
699 */
Jeff Hao406ec152013-07-30 10:13:41 -0700700 public static final int myPid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700701 return Os.getpid();
Jeff Hao406ec152013-07-30 10:13:41 -0700702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703
704 /**
Romain Guy80b12fc2013-05-29 15:54:25 -0700705 * Returns the identifier of this process' parent.
706 * @hide
707 */
Elliott Hughes2a805f92013-07-31 18:25:47 -0700708 public static final int myPpid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700709 return Os.getppid();
Elliott Hughes2a805f92013-07-31 18:25:47 -0700710 }
Romain Guy80b12fc2013-05-29 15:54:25 -0700711
712 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 * Returns the identifier of the calling thread, which be used with
714 * {@link #setThreadPriority(int, int)}.
715 */
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700716 public static final int myTid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700717 return Os.gettid();
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719
720 /**
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700721 * Returns the identifier of this process's uid. This is the kernel uid
722 * that the process is running under, which is the identity of its
723 * app-specific sandbox. It is different from {@link #myUserHandle} in that
724 * a uid identifies a specific app sandbox in a specific user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 */
Jeff Hao406ec152013-07-30 10:13:41 -0700726 public static final int myUid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700727 return Os.getuid();
Jeff Hao406ec152013-07-30 10:13:41 -0700728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729
730 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700731 * Returns this process's user handle. This is the
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700732 * user the process is running under. It is distinct from
733 * {@link #myUid()} in that a particular user will have multiple
734 * distinct apps running under it each with their own uid.
735 */
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700736 public static final UserHandle myUserHandle() {
737 return new UserHandle(UserHandle.getUserId(myUid()));
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700738 }
739
740 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800741 * Returns whether the current process is in an isolated sandbox.
742 * @hide
743 */
744 public static final boolean isIsolated() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700745 int uid = UserHandle.getAppId(myUid());
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800746 return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID;
747 }
748
749 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 * Returns the UID assigned to a particular user name, or -1 if there is
751 * none. If the given string consists of only numbers, it is converted
752 * directly to a uid.
753 */
754 public static final native int getUidForName(String name);
755
756 /**
757 * Returns the GID assigned to a particular user name, or -1 if there is
758 * none. If the given string consists of only numbers, it is converted
759 * directly to a gid.
760 */
761 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700762
763 /**
764 * Returns a uid for a currently running process.
765 * @param pid the process id
766 * @return the uid of the process, or -1 if the process is not running.
767 * @hide pending API council review
768 */
769 public static final int getUidForPid(int pid) {
770 String[] procStatusLabels = { "Uid:" };
771 long[] procStatusValues = new long[1];
772 procStatusValues[0] = -1;
773 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
774 return (int) procStatusValues[0];
775 }
776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700778 * Returns the parent process id for a currently running process.
779 * @param pid the process id
780 * @return the parent process id of the process, or -1 if the process is not running.
781 * @hide
782 */
783 public static final int getParentPid(int pid) {
784 String[] procStatusLabels = { "PPid:" };
785 long[] procStatusValues = new long[1];
786 procStatusValues[0] = -1;
787 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
788 return (int) procStatusValues[0];
789 }
790
791 /**
Glenn Kasten07b04652012-04-23 15:00:43 -0700792 * Returns the thread group leader id for a currently running thread.
793 * @param tid the thread id
794 * @return the thread group leader id of the thread, or -1 if the thread is not running.
795 * This is same as what getpid(2) would return if called by tid.
796 * @hide
797 */
798 public static final int getThreadGroupLeader(int tid) {
799 String[] procStatusLabels = { "Tgid:" };
800 long[] procStatusValues = new long[1];
801 procStatusValues[0] = -1;
802 Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues);
803 return (int) procStatusValues[0];
804 }
805
806 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 * Set the priority of a thread, based on Linux priorities.
808 *
809 * @param tid The identifier of the thread/process to change.
810 * @param priority A Linux priority level, from -20 for highest scheduling
811 * priority to 19 for lowest scheduling priority.
812 *
813 * @throws IllegalArgumentException Throws IllegalArgumentException if
814 * <var>tid</var> does not exist.
815 * @throws SecurityException Throws SecurityException if your process does
816 * not have permission to modify the given thread, or to use the given
817 * priority.
818 */
819 public static final native void setThreadPriority(int tid, int priority)
820 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700821
822 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700823 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
824 * throw an exception if passed a background-level thread priority. This is only
825 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
826 *
827 * @hide
828 */
829 public static final native void setCanSelfBackground(boolean backgroundOk);
830
831 /**
San Mehate9d376b2009-04-21 14:06:36 -0700832 * Sets the scheduling group for a thread.
833 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700834 * @param tid The identifier of the thread to change.
835 * @param group The target group for this thread from THREAD_GROUP_*.
San Mehate9d376b2009-04-21 14:06:36 -0700836 *
837 * @throws IllegalArgumentException Throws IllegalArgumentException if
838 * <var>tid</var> does not exist.
839 * @throws SecurityException Throws SecurityException if your process does
840 * not have permission to modify the given thread, or to use the given
841 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700842 * If the thread is a thread group leader, that is it's gettid() == getpid(),
843 * then the other threads in the same thread group are _not_ affected.
San Mehate9d376b2009-04-21 14:06:36 -0700844 */
845 public static final native void setThreadGroup(int tid, int group)
846 throws IllegalArgumentException, SecurityException;
Glenn Kastenf1b56442012-03-15 16:33:43 -0700847
San Mehat3e458242009-05-19 14:44:16 -0700848 /**
849 * Sets the scheduling group for a process and all child threads
850 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700851 * @param pid The identifier of the process to change.
852 * @param group The target group for this process from THREAD_GROUP_*.
San Mehat3e458242009-05-19 14:44:16 -0700853 *
854 * @throws IllegalArgumentException Throws IllegalArgumentException if
855 * <var>tid</var> does not exist.
856 * @throws SecurityException Throws SecurityException if your process does
857 * not have permission to modify the given thread, or to use the given
858 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700859 *
860 * group == THREAD_GROUP_DEFAULT means to move all non-background priority
861 * threads to the foreground scheduling group, but to leave background
862 * priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all
863 * threads, regardless of priority, to the background scheduling group.
864 * group == THREAD_GROUP_FOREGROUND is not allowed.
San Mehat3e458242009-05-19 14:44:16 -0700865 */
866 public static final native void setProcessGroup(int pid, int group)
867 throws IllegalArgumentException, SecurityException;
Jeff Sharkey9e57c412013-01-17 14:12:41 -0800868
869 /**
870 * Return the scheduling group of requested process.
871 *
872 * @hide
873 */
874 public static final native int getProcessGroup(int pid)
875 throws IllegalArgumentException, SecurityException;
876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 /**
878 * Set the priority of the calling thread, based on Linux priorities. See
879 * {@link #setThreadPriority(int, int)} for more information.
880 *
881 * @param priority A Linux priority level, from -20 for highest scheduling
882 * priority to 19 for lowest scheduling priority.
883 *
884 * @throws IllegalArgumentException Throws IllegalArgumentException if
885 * <var>tid</var> does not exist.
886 * @throws SecurityException Throws SecurityException if your process does
887 * not have permission to modify the given thread, or to use the given
888 * priority.
889 *
890 * @see #setThreadPriority(int, int)
891 */
892 public static final native void setThreadPriority(int priority)
893 throws IllegalArgumentException, SecurityException;
894
895 /**
896 * Return the current priority of a thread, based on Linux priorities.
897 *
898 * @param tid The identifier of the thread/process to change.
899 *
900 * @return Returns the current priority, as a Linux priority level,
901 * from -20 for highest scheduling priority to 19 for lowest scheduling
902 * priority.
903 *
904 * @throws IllegalArgumentException Throws IllegalArgumentException if
905 * <var>tid</var> does not exist.
906 */
907 public static final native int getThreadPriority(int tid)
908 throws IllegalArgumentException;
909
910 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700911 * Set the scheduling policy and priority of a thread, based on Linux.
912 *
913 * @param tid The identifier of the thread/process to change.
914 * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
915 * @param priority A Linux priority level in a range appropriate for the given policy.
916 *
917 * @throws IllegalArgumentException Throws IllegalArgumentException if
918 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
919 * @throws SecurityException Throws SecurityException if your process does
920 * not have permission to modify the given thread, or to use the given
921 * scheduling policy or priority.
922 *
923 * {@hide}
924 */
925 public static final native void setThreadScheduler(int tid, int policy, int priority)
926 throws IllegalArgumentException;
927
928 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 * Determine whether the current environment supports multiple processes.
930 *
931 * @return Returns true if the system can run in multiple processes, else
932 * false if everything is running in a single process.
Jeff Brown10e89712011-07-08 18:52:57 -0700933 *
934 * @deprecated This method always returns true. Do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 */
Jeff Brown10e89712011-07-08 18:52:57 -0700936 @Deprecated
937 public static final boolean supportsProcesses() {
938 return true;
939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940
941 /**
942 * Set the out-of-memory badness adjustment for a process.
943 *
944 * @param pid The process identifier to set.
945 * @param amt Adjustment value -- linux allows -16 to +15.
946 *
947 * @return Returns true if the underlying system supports this
948 * feature, else false.
949 *
950 * {@hide}
951 */
952 public static final native boolean setOomAdj(int pid, int amt);
953
954 /**
Rom Lemarchand5534ba92013-07-12 16:15:36 -0700955 * Adjust the swappiness level for a process.
956 *
957 * @param pid The process identifier to set.
958 * @param is_increased Whether swappiness should be increased or default.
959 *
960 * @return Returns true if the underlying system supports this
961 * feature, else false.
962 *
963 * {@hide}
964 */
965 public static final native boolean setSwappiness(int pid, boolean is_increased);
966
967 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 * Change this process's argv[0] parameter. This can be useful to show
969 * more descriptive information in things like the 'ps' command.
970 *
971 * @param text The new name of this process.
972 *
973 * {@hide}
974 */
975 public static final native void setArgV0(String text);
976
977 /**
978 * Kill the process with the given PID.
979 * Note that, though this API allows us to request to
980 * kill any process based on its PID, the kernel will
981 * still impose standard restrictions on which PIDs you
982 * are actually able to kill. Typically this means only
983 * the process running the caller's packages/application
984 * and any additional processes created by that app; packages
985 * sharing a common UID will also be able to kill each
986 * other's processes.
987 */
988 public static final void killProcess(int pid) {
989 sendSignal(pid, SIGNAL_KILL);
990 }
991
992 /** @hide */
993 public static final native int setUid(int uid);
994
995 /** @hide */
996 public static final native int setGid(int uid);
997
998 /**
999 * Send a signal to the given process.
1000 *
1001 * @param pid The pid of the target process.
1002 * @param signal The signal to send.
1003 */
1004 public static final native void sendSignal(int pid, int signal);
1005
Dianne Hackborn906497c2010-05-10 15:57:38 -07001006 /**
1007 * @hide
1008 * Private impl for avoiding a log message... DO NOT USE without doing
1009 * your own log, or the Android Illuminati will find you some night and
1010 * beat you up.
1011 */
1012 public static final void killProcessQuiet(int pid) {
1013 sendSignalQuiet(pid, SIGNAL_KILL);
1014 }
1015
1016 /**
1017 * @hide
1018 * Private impl for avoiding a log message... DO NOT USE without doing
1019 * your own log, or the Android Illuminati will find you some night and
1020 * beat you up.
1021 */
1022 public static final native void sendSignalQuiet(int pid, int signal);
1023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 /** @hide */
Marco Nelissen0bca96b2009-07-17 12:59:25 -07001025 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026
1027 /** @hide */
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001028 public static final native long getTotalMemory();
1029
1030 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 public static final native void readProcLines(String path,
1032 String[] reqFields, long[] outSizes);
1033
1034 /** @hide */
1035 public static final native int[] getPids(String path, int[] lastArray);
1036
1037 /** @hide */
1038 public static final int PROC_TERM_MASK = 0xff;
1039 /** @hide */
1040 public static final int PROC_ZERO_TERM = 0;
1041 /** @hide */
1042 public static final int PROC_SPACE_TERM = (int)' ';
1043 /** @hide */
Evan Millarc64edde2009-04-18 12:26:32 -07001044 public static final int PROC_TAB_TERM = (int)'\t';
1045 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 public static final int PROC_COMBINE = 0x100;
1047 /** @hide */
1048 public static final int PROC_PARENS = 0x200;
1049 /** @hide */
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001050 public static final int PROC_QUOTES = 0x400;
1051 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 public static final int PROC_OUT_STRING = 0x1000;
1053 /** @hide */
1054 public static final int PROC_OUT_LONG = 0x2000;
1055 /** @hide */
1056 public static final int PROC_OUT_FLOAT = 0x4000;
1057
1058 /** @hide */
1059 public static final native boolean readProcFile(String file, int[] format,
1060 String[] outStrings, long[] outLongs, float[] outFloats);
Evan Millarc64edde2009-04-18 12:26:32 -07001061
1062 /** @hide */
1063 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
1064 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001066 /** @hide */
1067 public static final native int[] getPidsForCommands(String[] cmds);
1068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 /**
1070 * Gets the total Pss value for a given process, in bytes.
1071 *
1072 * @param pid the process to the Pss for
1073 * @return the total Pss value for the given process in bytes,
1074 * or -1 if the value cannot be determined
1075 * @hide
1076 */
1077 public static final native long getPss(int pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -07001078
1079 /**
1080 * Specifies the outcome of having started a process.
1081 * @hide
1082 */
1083 public static final class ProcessStartResult {
1084 /**
1085 * The PID of the newly started process.
1086 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
1087 */
1088 public int pid;
1089
1090 /**
1091 * True if the process was started with a wrapper attached.
1092 */
1093 public boolean usingWrapper;
1094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095}