blob: 10ff27eb38f8cad995eee0cb401e62ffd36eba9e [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.util.Log;
Narayan Kamath973b4662014-03-31 13:41:26 +010022import com.android.internal.os.Zygote;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import java.io.BufferedWriter;
24import java.io.DataInputStream;
25import java.io.IOException;
26import java.io.OutputStreamWriter;
Narayan Kamath4444dcd2014-04-03 20:15:15 +010027import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import java.util.ArrayList;
Narayan Kamath4444dcd2014-04-03 20:15:15 +010029import java.util.Arrays;
30import java.util.List;
Jeff Hao406ec152013-07-30 10:13:41 -070031import libcore.io.Libcore;
32
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033/*package*/ class ZygoteStartFailedEx extends Exception {
34 /**
35 * Something prevented the zygote process startup from happening normally
36 */
37
38 ZygoteStartFailedEx() {};
39 ZygoteStartFailedEx(String s) {super(s);}
40 ZygoteStartFailedEx(Throwable cause) {super(cause);}
41}
42
43/**
44 * Tools for managing OS processes.
45 */
46public class Process {
47 private static final String LOG_TAG = "Process";
48
49 private static final String ZYGOTE_SOCKET = "zygote";
50
Narayan Kamath4444dcd2014-04-03 20:15:15 +010051 private static final String SECONDARY_ZYGOTE_SOCKET = "zygote_secondary";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
53 /**
54 * Defines the UID/GID under which system code runs.
55 */
56 public static final int SYSTEM_UID = 1000;
57
58 /**
59 * Defines the UID/GID under which the telephony code runs.
60 */
61 public static final int PHONE_UID = 1001;
62
63 /**
Dianne Hackborn854060af2009-07-09 18:14:31 -070064 * Defines the UID/GID for the user shell.
65 * @hide
66 */
67 public static final int SHELL_UID = 2000;
68
69 /**
Mike Lockwoodd42685d2009-09-03 09:25:22 -040070 * Defines the UID/GID for the log group.
71 * @hide
72 */
73 public static final int LOG_UID = 1007;
74
75 /**
Amith Yamasanid1582142009-07-08 20:04:55 -070076 * Defines the UID/GID for the WIFI supplicant process.
77 * @hide
78 */
79 public static final int WIFI_UID = 1010;
80
81 /**
Glenn Kasten8b7d1b42011-07-13 16:23:22 -070082 * Defines the UID/GID for the mediaserver process.
83 * @hide
84 */
85 public static final int MEDIA_UID = 1013;
86
87 /**
Jeff Sharkey5294a2f2012-04-24 17:07:22 -070088 * Defines the UID/GID for the DRM process.
89 * @hide
90 */
91 public static final int DRM_UID = 1019;
92
93 /**
Kenny Root26993b32012-03-19 15:07:51 -070094 * Defines the UID/GID for the group that controls VPN services.
95 * @hide
96 */
97 public static final int VPN_UID = 1016;
98
99 /**
Nick Pellycd0e8392010-10-13 17:25:24 -0700100 * Defines the UID/GID for the NFC service process.
101 * @hide
102 */
Nick Pellya5cb9f42011-11-21 14:54:46 -0800103 public static final int NFC_UID = 1027;
Nick Pellycd0e8392010-10-13 17:25:24 -0700104
105 /**
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800106 * Defines the UID/GID for the Bluetooth service process.
107 * @hide
108 */
109 public static final int BLUETOOTH_UID = 1002;
110
111 /**
Mike Lockwooddcaa10c2010-12-16 12:50:44 -0800112 * Defines the GID for the group that allows write access to the internal media storage.
113 * @hide
114 */
115 public static final int MEDIA_RW_GID = 1023;
116
117 /**
Jeff Sharkey184a0102013-07-10 16:19:52 -0700118 * Access to installed package details
119 * @hide
120 */
121 public static final int PACKAGE_INFO_GID = 1032;
122
123 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 * Defines the start of a range of UIDs (and GIDs), going from this
125 * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning
126 * to applications.
127 */
128 public static final int FIRST_APPLICATION_UID = 10000;
Jeff Sharkey184a0102013-07-10 16:19:52 -0700129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 /**
131 * Last of application-specific UIDs starting at
132 * {@link #FIRST_APPLICATION_UID}.
133 */
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800134 public static final int LAST_APPLICATION_UID = 19999;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800135
136 /**
137 * First uid used for fully isolated sandboxed processes (with no permissions of their own)
138 * @hide
139 */
140 public static final int FIRST_ISOLATED_UID = 99000;
141
142 /**
143 * Last uid used for fully isolated sandboxed processes (with no permissions of their own)
144 * @hide
145 */
146 public static final int LAST_ISOLATED_UID = 99999;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147
148 /**
Kenny Roote091f222012-09-11 15:01:26 -0700149 * First gid for applications to share resources. Used when forward-locking
150 * is enabled but all UserHandles need to be able to read the resources.
151 * @hide
152 */
153 public static final int FIRST_SHARED_APPLICATION_GID = 50000;
154
155 /**
156 * Last gid for applications to share resources. Used when forward-locking
157 * is enabled but all UserHandles need to be able to read the resources.
158 * @hide
159 */
160 public static final int LAST_SHARED_APPLICATION_GID = 59999;
161
162 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 * Standard priority of application threads.
164 * Use with {@link #setThreadPriority(int)} and
165 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
166 * {@link java.lang.Thread} class.
167 */
168 public static final int THREAD_PRIORITY_DEFAULT = 0;
169
170 /*
171 * ***************************************
172 * ** Keep in sync with utils/threads.h **
173 * ***************************************
174 */
175
176 /**
177 * Lowest available thread priority. Only for those who really, really
178 * don't want to run if anything else is happening.
179 * Use with {@link #setThreadPriority(int)} and
180 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
181 * {@link java.lang.Thread} class.
182 */
183 public static final int THREAD_PRIORITY_LOWEST = 19;
184
185 /**
186 * Standard priority background threads. This gives your thread a slightly
187 * lower than normal priority, so that it will have less chance of impacting
188 * the responsiveness of the user interface.
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_BACKGROUND = 10;
194
195 /**
196 * Standard priority of threads that are currently running a user interface
197 * that the user is interacting with. Applications can not normally
198 * change to this priority; the system will automatically adjust your
199 * application threads as the user moves through the UI.
200 * Use with {@link #setThreadPriority(int)} and
201 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
202 * {@link java.lang.Thread} class.
203 */
204 public static final int THREAD_PRIORITY_FOREGROUND = -2;
205
206 /**
207 * Standard priority of system display threads, involved in updating
208 * the user interface. Applications can not
209 * normally change to this priority.
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_DISPLAY = -4;
215
216 /**
217 * Standard priority of the most important display threads, for compositing
218 * the screen and retrieving input events. Applications can not normally
219 * 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_URGENT_DISPLAY = -8;
225
226 /**
227 * Standard priority of audio threads. Applications can not normally
228 * change to this priority.
229 * Use with {@link #setThreadPriority(int)} and
230 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
231 * {@link java.lang.Thread} class.
232 */
233 public static final int THREAD_PRIORITY_AUDIO = -16;
234
235 /**
236 * Standard priority of the most important audio threads.
237 * Applications can not normally change to this priority.
238 * Use with {@link #setThreadPriority(int)} and
239 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
240 * {@link java.lang.Thread} class.
241 */
242 public static final int THREAD_PRIORITY_URGENT_AUDIO = -19;
243
244 /**
245 * Minimum increment to make a priority more favorable.
246 */
247 public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
248
249 /**
250 * Minimum increment to make a priority less favorable.
251 */
252 public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
253
San Mehate9d376b2009-04-21 14:06:36 -0700254 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700255 * Default scheduling policy
256 * @hide
257 */
258 public static final int SCHED_OTHER = 0;
259
260 /**
261 * First-In First-Out scheduling policy
262 * @hide
263 */
264 public static final int SCHED_FIFO = 1;
265
266 /**
267 * Round-Robin scheduling policy
268 * @hide
269 */
270 public static final int SCHED_RR = 2;
271
272 /**
273 * Batch scheduling policy
274 * @hide
275 */
276 public static final int SCHED_BATCH = 3;
277
278 /**
279 * Idle scheduling policy
280 * @hide
281 */
282 public static final int SCHED_IDLE = 5;
283
Glenn Kastenf1b56442012-03-15 16:33:43 -0700284 // Keep in sync with SP_* constants of enum type SchedPolicy
285 // declared in system/core/include/cutils/sched_policy.h,
286 // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
San Mehate9d376b2009-04-21 14:06:36 -0700287
288 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700289 * Default thread group -
290 * has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
291 * When used with setProcessGroup(), the group of each thread in the process
292 * is conditionally changed based on that thread's current priority, as follows:
293 * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
294 * are moved to foreground thread group. All other threads are left unchanged.
295 * @hide
296 */
297 public static final int THREAD_GROUP_DEFAULT = -1;
298
299 /**
300 * Background thread group - All threads in
San Mehate9d376b2009-04-21 14:06:36 -0700301 * this group are scheduled with a reduced share of the CPU.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700302 * Value is same as constant SP_BACKGROUND of enum SchedPolicy.
303 * FIXME rename to THREAD_GROUP_BACKGROUND.
San Mehate9d376b2009-04-21 14:06:36 -0700304 * @hide
305 */
Glenn Kastenf1b56442012-03-15 16:33:43 -0700306 public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0;
San Mehate9d376b2009-04-21 14:06:36 -0700307
308 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700309 * Foreground thread group - All threads in
310 * this group are scheduled with a normal share of the CPU.
311 * Value is same as constant SP_FOREGROUND of enum SchedPolicy.
312 * Not used at this level.
San Mehate9d376b2009-04-21 14:06:36 -0700313 * @hide
314 **/
Glenn Kastenf1b56442012-03-15 16:33:43 -0700315 private static final int THREAD_GROUP_FOREGROUND = 1;
San Mehate9d376b2009-04-21 14:06:36 -0700316
Glenn Kasten07b04652012-04-23 15:00:43 -0700317 /**
318 * System thread group.
319 * @hide
320 **/
321 public static final int THREAD_GROUP_SYSTEM = 2;
322
323 /**
324 * Application audio thread group.
325 * @hide
326 **/
327 public static final int THREAD_GROUP_AUDIO_APP = 3;
328
329 /**
330 * System audio thread group.
331 * @hide
332 **/
333 public static final int THREAD_GROUP_AUDIO_SYS = 4;
334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 public static final int SIGNAL_QUIT = 3;
336 public static final int SIGNAL_KILL = 9;
337 public static final int SIGNAL_USR1 = 10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100339 /**
340 * State for communicating with the zygote process.
341 */
342 static class ZygoteState {
343 final LocalSocket socket;
344 final DataInputStream inputStream;
345 final BufferedWriter writer;
346 final List<String> abiList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100348 boolean mClosed;
349
350 private ZygoteState(LocalSocket socket, DataInputStream inputStream,
351 BufferedWriter writer, List<String> abiList) {
352 this.socket = socket;
353 this.inputStream = inputStream;
354 this.writer = writer;
355 this.abiList = abiList;
356 }
357
358 static ZygoteState connect(String socketAddress, int tries) throws ZygoteStartFailedEx {
359 LocalSocket zygoteSocket = null;
360 DataInputStream zygoteInputStream = null;
361 BufferedWriter zygoteWriter = null;
362
363 /*
364 * See bug #811181: Sometimes runtime can make it up before zygote.
365 * Really, we'd like to do something better to avoid this condition,
366 * but for now just wait a bit...
367 *
368 * TODO: This bug was filed in 2007. Get rid of this code. The zygote
369 * forks the system_server so it shouldn't be possible for the zygote
370 * socket to be brought up after the system_server is.
371 */
372 for (int i = 0; i < tries; i++) {
373 if (i > 0) {
374 try {
375 Log.i("Zygote", "Zygote not up yet, sleeping...");
376 Thread.sleep(ZYGOTE_RETRY_MILLIS);
377 } catch (InterruptedException ex) {
378 throw new ZygoteStartFailedEx(ex);
379 }
380 }
381
382 try {
383 zygoteSocket = new LocalSocket();
384 zygoteSocket.connect(new LocalSocketAddress(socketAddress,
385 LocalSocketAddress.Namespace.RESERVED));
386
387 zygoteInputStream = new DataInputStream(zygoteSocket.getInputStream());
388
389 zygoteWriter = new BufferedWriter(new OutputStreamWriter(
390 zygoteSocket.getOutputStream()), 256);
391 break;
392 } catch (IOException ex) {
393 if (zygoteSocket != null) {
394 try {
395 zygoteSocket.close();
396 } catch (IOException ex2) {
397 Log.e(LOG_TAG,"I/O exception on close after exception", ex2);
398 }
399 }
400
401 zygoteSocket = null;
402 }
403 }
404
405 if (zygoteSocket == null) {
406 throw new ZygoteStartFailedEx("connect failed");
407 }
408
409 String abiListString = getAbiList(zygoteWriter, zygoteInputStream);
410 Log.i("Zygote", "Process: zygote socket opened, supported ABIS: " + abiListString);
411
412 return new ZygoteState(zygoteSocket, zygoteInputStream, zygoteWriter,
413 Arrays.asList(abiListString.split(",")));
414 }
415
416 boolean matches(String abi) {
417 return abiList.contains(abi);
418 }
419
420 void close() {
421 try {
422 socket.close();
423 } catch (IOException ex) {
424 Log.e(LOG_TAG,"I/O exception on routine close", ex);
425 }
426
427 mClosed = true;
428 }
429
430 boolean isClosed() {
431 return mClosed;
432 }
433 }
434
435 /**
436 * The state of the connection to the primary zygote.
437 */
438 static ZygoteState primaryZygoteState;
439
440 /**
441 * The state of the connection to the secondary zygote.
442 */
443 static ZygoteState secondaryZygoteState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444
445 /**
446 * Start a new process.
447 *
448 * <p>If processes are enabled, a new process is created and the
449 * static main() function of a <var>processClass</var> is executed there.
450 * The process will continue running after this function returns.
451 *
452 * <p>If processes are not enabled, a new thread in the caller's
453 * process is created and main() of <var>processClass</var> called there.
454 *
455 * <p>The niceName parameter, if not an empty string, is a custom name to
456 * give to the process instead of using processClass. This allows you to
457 * make easily identifyable processes even if you are using the same base
458 * <var>processClass</var> to start them.
459 *
460 * @param processClass The class to use as the process's main entry
461 * point.
462 * @param niceName A more readable name to use for the process.
463 * @param uid The user-id under which the process will run.
464 * @param gid The group-id under which the process will run.
465 * @param gids Additional group-ids associated with the process.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700466 * @param debugFlags Additional flags.
467 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalleybd19b9e2013-04-12 14:49:38 -0400468 * @param seInfo null-ok SELinux information for the new process.
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100469 * @param abi non-null the ABI this app should be started with.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 * @param zygoteArgs Additional arguments to supply to the zygote process.
471 *
Jeff Brown3f9dd282011-07-08 20:02:19 -0700472 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 * @throws RuntimeException on fatal start failure
474 *
475 * {@hide}
476 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700477 public static final ProcessStartResult start(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 final String niceName,
479 int uid, int gid, int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700480 int debugFlags, int mountExternal,
481 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500482 String seInfo,
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100483 String abi,
Jeff Brown10e89712011-07-08 18:52:57 -0700484 String[] zygoteArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 try {
Jeff Brown10e89712011-07-08 18:52:57 -0700486 return startViaZygote(processClass, niceName, uid, gid, gids,
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100487 debugFlags, mountExternal, targetSdkVersion, seInfo,
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100488 abi, zygoteArgs);
Jeff Brown10e89712011-07-08 18:52:57 -0700489 } catch (ZygoteStartFailedEx ex) {
490 Log.e(LOG_TAG,
491 "Starting VM process through Zygote failed");
492 throw new RuntimeException(
493 "Starting VM process through Zygote failed", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 }
496
497 /** retry interval for opening a zygote socket */
498 static final int ZYGOTE_RETRY_MILLIS = 500;
499
500 /**
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100501 * Queries the zygote for the list of ABIS it supports.
502 *
503 * @throws ZygoteStartFailedEx if the query failed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 */
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100505 private static String getAbiList(BufferedWriter writer, DataInputStream inputStream)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 throws ZygoteStartFailedEx {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100507 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100509 // Each query starts with the argument count (1 in this case)
510 writer.write("1");
511 // ... followed by a new-line.
512 writer.newLine();
513 // ... followed by our only argument.
514 writer.write("--query-abi-list");
515 writer.newLine();
516 writer.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100518 // The response is a length prefixed stream of ASCII bytes.
519 int numBytes = inputStream.readInt();
520 byte[] bytes = new byte[numBytes];
521 inputStream.readFully(bytes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100523 return new String(bytes, StandardCharsets.US_ASCII);
524 } catch (IOException ioe) {
525 throw new ZygoteStartFailedEx(ioe);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
527 }
528
529 /**
530 * Sends an argument list to the zygote process, which starts a new child
531 * and returns the child's pid. Please note: the present implementation
532 * replaces newlines in the argument list with spaces.
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100533 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 * @throws ZygoteStartFailedEx if process start failed for any reason
535 */
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100536 private static ProcessStartResult zygoteSendArgsAndGetResult(
537 ZygoteState zygoteState, ArrayList<String> args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 try {
540 /**
541 * See com.android.internal.os.ZygoteInit.readArgumentList()
542 * Presently the wire format to the zygote process is:
543 * a) a count of arguments (argc, in essence)
544 * b) a number of newline-separated argument strings equal to count
545 *
546 * After the zygote process reads these it will write the pid of
Jeff Brown3f9dd282011-07-08 20:02:19 -0700547 * the child or -1 on failure, followed by boolean to
548 * indicate whether a wrapper process was used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 */
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100550 final BufferedWriter writer = zygoteState.writer;
551 final DataInputStream inputStream = zygoteState.inputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100553 writer.write(Integer.toString(args.size()));
554 writer.newLine();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555
556 int sz = args.size();
557 for (int i = 0; i < sz; i++) {
558 String arg = args.get(i);
559 if (arg.indexOf('\n') >= 0) {
560 throw new ZygoteStartFailedEx(
561 "embedded newlines not allowed");
562 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100563 writer.write(arg);
564 writer.newLine();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
566
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100567 writer.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568
569 // Should there be a timeout on this?
Jeff Brown3f9dd282011-07-08 20:02:19 -0700570 ProcessStartResult result = new ProcessStartResult();
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100571 result.pid = inputStream.readInt();
Jeff Brown3f9dd282011-07-08 20:02:19 -0700572 if (result.pid < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 throw new ZygoteStartFailedEx("fork() failed");
574 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100575 result.usingWrapper = inputStream.readBoolean();
Jeff Brown3f9dd282011-07-08 20:02:19 -0700576 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 } catch (IOException ex) {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100578 zygoteState.close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 throw new ZygoteStartFailedEx(ex);
580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
582
583 /**
584 * Starts a new process via the zygote mechanism.
585 *
586 * @param processClass Class name whose static main() to run
587 * @param niceName 'nice' process name to appear in ps
588 * @param uid a POSIX uid that the new process should setuid() to
589 * @param gid a POSIX gid that the new process shuold setgid() to
590 * @param gids null-ok; a list of supplementary group IDs that the
591 * new process should setgroup() to.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700592 * @param debugFlags Additional flags.
593 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalleybd19b9e2013-04-12 14:49:38 -0400594 * @param seInfo null-ok SELinux information for the new process.
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100595 * @param abi the ABI the process should use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 * @param extraArgs Additional arguments to supply to the zygote process.
Jeff Brown3f9dd282011-07-08 20:02:19 -0700597 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 * @throws ZygoteStartFailedEx if process start failed for any reason
599 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700600 private static ProcessStartResult startViaZygote(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 final String niceName,
602 final int uid, final int gid,
603 final int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700604 int debugFlags, int mountExternal,
605 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500606 String seInfo,
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100607 String abi,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 String[] extraArgs)
609 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 synchronized(Process.class) {
611 ArrayList<String> argsForZygote = new ArrayList<String>();
612
613 // --runtime-init, --setuid=, --setgid=,
614 // and --setgroups= must go first
615 argsForZygote.add("--runtime-init");
616 argsForZygote.add("--setuid=" + uid);
617 argsForZygote.add("--setgid=" + gid);
Elliott Hughesae07ecf2011-07-06 17:33:27 -0700618 if ((debugFlags & Zygote.DEBUG_ENABLE_JNI_LOGGING) != 0) {
619 argsForZygote.add("--enable-jni-logging");
620 }
Ben Cheng23085b72010-02-08 16:06:32 -0800621 if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) {
622 argsForZygote.add("--enable-safemode");
623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) {
625 argsForZygote.add("--enable-debugger");
626 }
627 if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) {
628 argsForZygote.add("--enable-checkjni");
629 }
630 if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) {
631 argsForZygote.add("--enable-assert");
632 }
Jeff Sharkey2bca8682012-08-22 13:59:58 -0700633 if (mountExternal == Zygote.MOUNT_EXTERNAL_MULTIUSER) {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700634 argsForZygote.add("--mount-external-multiuser");
Jeff Sharkeye217ee42012-08-28 16:23:01 -0700635 } else if (mountExternal == Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL) {
636 argsForZygote.add("--mount-external-multiuser-all");
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700637 }
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700638 argsForZygote.add("--target-sdk-version=" + targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639
640 //TODO optionally enable debuger
641 //argsForZygote.add("--enable-debugger");
642
643 // --setgroups is a comma-separated list
644 if (gids != null && gids.length > 0) {
645 StringBuilder sb = new StringBuilder();
646 sb.append("--setgroups=");
647
648 int sz = gids.length;
649 for (int i = 0; i < sz; i++) {
650 if (i != 0) {
651 sb.append(',');
652 }
653 sb.append(gids[i]);
654 }
655
656 argsForZygote.add(sb.toString());
657 }
658
659 if (niceName != null) {
660 argsForZygote.add("--nice-name=" + niceName);
661 }
662
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500663 if (seInfo != null) {
664 argsForZygote.add("--seinfo=" + seInfo);
665 }
666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 argsForZygote.add(processClass);
668
669 if (extraArgs != null) {
670 for (String arg : extraArgs) {
671 argsForZygote.add(arg);
672 }
673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100675 return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi), argsForZygote);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100678
679 /**
680 * Returns the number of times we attempt a connection to the zygote. We
681 * sleep for {@link #ZYGOTE_RETRY_MILLIS} milliseconds between each try.
682 *
683 * This could probably be removed, see TODO in {@code ZygoteState#connect}.
684 */
685 private static int getNumTries(ZygoteState state) {
686 // Retry 10 times for the first connection to each zygote.
687 if (state == null) {
688 return 11;
689 }
690
691 // This means the connection has already been established, but subsequently
692 // closed, possibly due to an IOException. We retry just once if that's the
693 // case.
694 return 1;
695 }
696
697 /**
698 * Tries to open socket to Zygote process if not already open. If
699 * already open, does nothing. May block and retry.
700 */
701 private static ZygoteState openZygoteSocketIfNeeded(String abi) throws ZygoteStartFailedEx {
702 if (primaryZygoteState == null || primaryZygoteState.isClosed()) {
703 primaryZygoteState = ZygoteState.connect(ZYGOTE_SOCKET, getNumTries(primaryZygoteState));
704 }
705
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100706 if (primaryZygoteState.matches(abi)) {
707 return primaryZygoteState;
708 }
709
710 // The primary zygote didn't match. Try the secondary.
711 if (secondaryZygoteState == null || secondaryZygoteState.isClosed()) {
712 secondaryZygoteState = ZygoteState.connect(SECONDARY_ZYGOTE_SOCKET,
713 getNumTries(secondaryZygoteState));
714 }
715
716 if (secondaryZygoteState.matches(abi)) {
717 return secondaryZygoteState;
718 }
719
720 throw new ZygoteStartFailedEx("Unsupported zygote ABI: " + abi);
721 }
722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 /**
724 * Returns elapsed milliseconds of the time this process has run.
725 * @return Returns the number of milliseconds this process has return.
726 */
727 public static final native long getElapsedCpuTime();
728
729 /**
730 * Returns the identifier of this process, which can be used with
731 * {@link #killProcess} and {@link #sendSignal}.
732 */
Jeff Hao406ec152013-07-30 10:13:41 -0700733 public static final int myPid() {
734 return Libcore.os.getpid();
735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736
737 /**
Romain Guy80b12fc2013-05-29 15:54:25 -0700738 * Returns the identifier of this process' parent.
739 * @hide
740 */
Elliott Hughes2a805f92013-07-31 18:25:47 -0700741 public static final int myPpid() {
742 return Libcore.os.getppid();
743 }
Romain Guy80b12fc2013-05-29 15:54:25 -0700744
745 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 * Returns the identifier of the calling thread, which be used with
747 * {@link #setThreadPriority(int, int)}.
748 */
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700749 public static final int myTid() {
750 return Libcore.os.gettid();
751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752
753 /**
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700754 * Returns the identifier of this process's uid. This is the kernel uid
755 * that the process is running under, which is the identity of its
756 * app-specific sandbox. It is different from {@link #myUserHandle} in that
757 * a uid identifies a specific app sandbox in a specific user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 */
Jeff Hao406ec152013-07-30 10:13:41 -0700759 public static final int myUid() {
760 return Libcore.os.getuid();
761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762
763 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700764 * Returns this process's user handle. This is the
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700765 * user the process is running under. It is distinct from
766 * {@link #myUid()} in that a particular user will have multiple
767 * distinct apps running under it each with their own uid.
768 */
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700769 public static final UserHandle myUserHandle() {
770 return new UserHandle(UserHandle.getUserId(myUid()));
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700771 }
772
773 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800774 * Returns whether the current process is in an isolated sandbox.
775 * @hide
776 */
777 public static final boolean isIsolated() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700778 int uid = UserHandle.getAppId(myUid());
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800779 return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID;
780 }
781
782 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 * Returns the UID assigned to a particular user name, or -1 if there is
784 * none. If the given string consists of only numbers, it is converted
785 * directly to a uid.
786 */
787 public static final native int getUidForName(String name);
788
789 /**
790 * Returns the GID assigned to a particular user name, or -1 if there is
791 * none. If the given string consists of only numbers, it is converted
792 * directly to a gid.
793 */
794 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700795
796 /**
797 * Returns a uid for a currently running process.
798 * @param pid the process id
799 * @return the uid of the process, or -1 if the process is not running.
800 * @hide pending API council review
801 */
802 public static final int getUidForPid(int pid) {
803 String[] procStatusLabels = { "Uid:" };
804 long[] procStatusValues = new long[1];
805 procStatusValues[0] = -1;
806 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
807 return (int) procStatusValues[0];
808 }
809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700811 * Returns the parent process id for a currently running process.
812 * @param pid the process id
813 * @return the parent process id of the process, or -1 if the process is not running.
814 * @hide
815 */
816 public static final int getParentPid(int pid) {
817 String[] procStatusLabels = { "PPid:" };
818 long[] procStatusValues = new long[1];
819 procStatusValues[0] = -1;
820 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
821 return (int) procStatusValues[0];
822 }
823
824 /**
Glenn Kasten07b04652012-04-23 15:00:43 -0700825 * Returns the thread group leader id for a currently running thread.
826 * @param tid the thread id
827 * @return the thread group leader id of the thread, or -1 if the thread is not running.
828 * This is same as what getpid(2) would return if called by tid.
829 * @hide
830 */
831 public static final int getThreadGroupLeader(int tid) {
832 String[] procStatusLabels = { "Tgid:" };
833 long[] procStatusValues = new long[1];
834 procStatusValues[0] = -1;
835 Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues);
836 return (int) procStatusValues[0];
837 }
838
839 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 * Set the priority of a thread, based on Linux priorities.
841 *
842 * @param tid The identifier of the thread/process to change.
843 * @param priority A Linux priority level, from -20 for highest scheduling
844 * priority to 19 for lowest scheduling priority.
845 *
846 * @throws IllegalArgumentException Throws IllegalArgumentException if
847 * <var>tid</var> does not exist.
848 * @throws SecurityException Throws SecurityException if your process does
849 * not have permission to modify the given thread, or to use the given
850 * priority.
851 */
852 public static final native void setThreadPriority(int tid, int priority)
853 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700854
855 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700856 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
857 * throw an exception if passed a background-level thread priority. This is only
858 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
859 *
860 * @hide
861 */
862 public static final native void setCanSelfBackground(boolean backgroundOk);
863
864 /**
San Mehate9d376b2009-04-21 14:06:36 -0700865 * Sets the scheduling group for a thread.
866 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700867 * @param tid The identifier of the thread to change.
868 * @param group The target group for this thread from THREAD_GROUP_*.
San Mehate9d376b2009-04-21 14:06:36 -0700869 *
870 * @throws IllegalArgumentException Throws IllegalArgumentException if
871 * <var>tid</var> does not exist.
872 * @throws SecurityException Throws SecurityException if your process does
873 * not have permission to modify the given thread, or to use the given
874 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700875 * If the thread is a thread group leader, that is it's gettid() == getpid(),
876 * then the other threads in the same thread group are _not_ affected.
San Mehate9d376b2009-04-21 14:06:36 -0700877 */
878 public static final native void setThreadGroup(int tid, int group)
879 throws IllegalArgumentException, SecurityException;
Glenn Kastenf1b56442012-03-15 16:33:43 -0700880
San Mehat3e458242009-05-19 14:44:16 -0700881 /**
882 * Sets the scheduling group for a process and all child threads
883 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700884 * @param pid The identifier of the process to change.
885 * @param group The target group for this process from THREAD_GROUP_*.
San Mehat3e458242009-05-19 14:44:16 -0700886 *
887 * @throws IllegalArgumentException Throws IllegalArgumentException if
888 * <var>tid</var> does not exist.
889 * @throws SecurityException Throws SecurityException if your process does
890 * not have permission to modify the given thread, or to use the given
891 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700892 *
893 * group == THREAD_GROUP_DEFAULT means to move all non-background priority
894 * threads to the foreground scheduling group, but to leave background
895 * priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all
896 * threads, regardless of priority, to the background scheduling group.
897 * group == THREAD_GROUP_FOREGROUND is not allowed.
San Mehat3e458242009-05-19 14:44:16 -0700898 */
899 public static final native void setProcessGroup(int pid, int group)
900 throws IllegalArgumentException, SecurityException;
Jeff Sharkey9e57c412013-01-17 14:12:41 -0800901
902 /**
903 * Return the scheduling group of requested process.
904 *
905 * @hide
906 */
907 public static final native int getProcessGroup(int pid)
908 throws IllegalArgumentException, SecurityException;
909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 /**
911 * Set the priority of the calling thread, based on Linux priorities. See
912 * {@link #setThreadPriority(int, int)} for more information.
913 *
914 * @param priority A Linux priority level, from -20 for highest scheduling
915 * priority to 19 for lowest scheduling priority.
916 *
917 * @throws IllegalArgumentException Throws IllegalArgumentException if
918 * <var>tid</var> does not exist.
919 * @throws SecurityException Throws SecurityException if your process does
920 * not have permission to modify the given thread, or to use the given
921 * priority.
922 *
923 * @see #setThreadPriority(int, int)
924 */
925 public static final native void setThreadPriority(int priority)
926 throws IllegalArgumentException, SecurityException;
927
928 /**
929 * Return the current priority of a thread, based on Linux priorities.
930 *
931 * @param tid The identifier of the thread/process to change.
932 *
933 * @return Returns the current priority, as a Linux priority level,
934 * from -20 for highest scheduling priority to 19 for lowest scheduling
935 * priority.
936 *
937 * @throws IllegalArgumentException Throws IllegalArgumentException if
938 * <var>tid</var> does not exist.
939 */
940 public static final native int getThreadPriority(int tid)
941 throws IllegalArgumentException;
942
943 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700944 * Set the scheduling policy and priority of a thread, based on Linux.
945 *
946 * @param tid The identifier of the thread/process to change.
947 * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
948 * @param priority A Linux priority level in a range appropriate for the given policy.
949 *
950 * @throws IllegalArgumentException Throws IllegalArgumentException if
951 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
952 * @throws SecurityException Throws SecurityException if your process does
953 * not have permission to modify the given thread, or to use the given
954 * scheduling policy or priority.
955 *
956 * {@hide}
957 */
958 public static final native void setThreadScheduler(int tid, int policy, int priority)
959 throws IllegalArgumentException;
960
961 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 * Determine whether the current environment supports multiple processes.
963 *
964 * @return Returns true if the system can run in multiple processes, else
965 * false if everything is running in a single process.
Jeff Brown10e89712011-07-08 18:52:57 -0700966 *
967 * @deprecated This method always returns true. Do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 */
Jeff Brown10e89712011-07-08 18:52:57 -0700969 @Deprecated
970 public static final boolean supportsProcesses() {
971 return true;
972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973
974 /**
975 * Set the out-of-memory badness adjustment for a process.
976 *
977 * @param pid The process identifier to set.
978 * @param amt Adjustment value -- linux allows -16 to +15.
979 *
980 * @return Returns true if the underlying system supports this
981 * feature, else false.
982 *
983 * {@hide}
984 */
985 public static final native boolean setOomAdj(int pid, int amt);
986
987 /**
Rom Lemarchand5534ba92013-07-12 16:15:36 -0700988 * Adjust the swappiness level for a process.
989 *
990 * @param pid The process identifier to set.
991 * @param is_increased Whether swappiness should be increased or default.
992 *
993 * @return Returns true if the underlying system supports this
994 * feature, else false.
995 *
996 * {@hide}
997 */
998 public static final native boolean setSwappiness(int pid, boolean is_increased);
999
1000 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 * Change this process's argv[0] parameter. This can be useful to show
1002 * more descriptive information in things like the 'ps' command.
1003 *
1004 * @param text The new name of this process.
1005 *
1006 * {@hide}
1007 */
1008 public static final native void setArgV0(String text);
1009
1010 /**
1011 * Kill the process with the given PID.
1012 * Note that, though this API allows us to request to
1013 * kill any process based on its PID, the kernel will
1014 * still impose standard restrictions on which PIDs you
1015 * are actually able to kill. Typically this means only
1016 * the process running the caller's packages/application
1017 * and any additional processes created by that app; packages
1018 * sharing a common UID will also be able to kill each
1019 * other's processes.
1020 */
1021 public static final void killProcess(int pid) {
1022 sendSignal(pid, SIGNAL_KILL);
1023 }
1024
1025 /** @hide */
1026 public static final native int setUid(int uid);
1027
1028 /** @hide */
1029 public static final native int setGid(int uid);
1030
1031 /**
1032 * Send a signal to the given process.
1033 *
1034 * @param pid The pid of the target process.
1035 * @param signal The signal to send.
1036 */
1037 public static final native void sendSignal(int pid, int signal);
1038
Dianne Hackborn906497c2010-05-10 15:57:38 -07001039 /**
1040 * @hide
1041 * Private impl for avoiding a log message... DO NOT USE without doing
1042 * your own log, or the Android Illuminati will find you some night and
1043 * beat you up.
1044 */
1045 public static final void killProcessQuiet(int pid) {
1046 sendSignalQuiet(pid, SIGNAL_KILL);
1047 }
1048
1049 /**
1050 * @hide
1051 * Private impl for avoiding a log message... DO NOT USE without doing
1052 * your own log, or the Android Illuminati will find you some night and
1053 * beat you up.
1054 */
1055 public static final native void sendSignalQuiet(int pid, int signal);
1056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 /** @hide */
Marco Nelissen0bca96b2009-07-17 12:59:25 -07001058 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059
1060 /** @hide */
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001061 public static final native long getTotalMemory();
1062
1063 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 public static final native void readProcLines(String path,
1065 String[] reqFields, long[] outSizes);
1066
1067 /** @hide */
1068 public static final native int[] getPids(String path, int[] lastArray);
1069
1070 /** @hide */
1071 public static final int PROC_TERM_MASK = 0xff;
1072 /** @hide */
1073 public static final int PROC_ZERO_TERM = 0;
1074 /** @hide */
1075 public static final int PROC_SPACE_TERM = (int)' ';
1076 /** @hide */
Evan Millarc64edde2009-04-18 12:26:32 -07001077 public static final int PROC_TAB_TERM = (int)'\t';
1078 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 public static final int PROC_COMBINE = 0x100;
1080 /** @hide */
1081 public static final int PROC_PARENS = 0x200;
1082 /** @hide */
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001083 public static final int PROC_QUOTES = 0x400;
1084 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 public static final int PROC_OUT_STRING = 0x1000;
1086 /** @hide */
1087 public static final int PROC_OUT_LONG = 0x2000;
1088 /** @hide */
1089 public static final int PROC_OUT_FLOAT = 0x4000;
1090
1091 /** @hide */
1092 public static final native boolean readProcFile(String file, int[] format,
1093 String[] outStrings, long[] outLongs, float[] outFloats);
Evan Millarc64edde2009-04-18 12:26:32 -07001094
1095 /** @hide */
1096 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
1097 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001099 /** @hide */
1100 public static final native int[] getPidsForCommands(String[] cmds);
1101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 /**
1103 * Gets the total Pss value for a given process, in bytes.
1104 *
1105 * @param pid the process to the Pss for
1106 * @return the total Pss value for the given process in bytes,
1107 * or -1 if the value cannot be determined
1108 * @hide
1109 */
1110 public static final native long getPss(int pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -07001111
1112 /**
1113 * Specifies the outcome of having started a process.
1114 * @hide
1115 */
1116 public static final class ProcessStartResult {
1117 /**
1118 * The PID of the newly started process.
1119 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
1120 */
1121 public int pid;
1122
1123 /**
1124 * True if the process was started with a wrapper attached.
1125 */
1126 public boolean usingWrapper;
1127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128}