blob: 7d2d0518108532ef523b0e8d1f9f7c3887c91483 [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.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 * @param zygoteArgs Additional arguments to supply to the zygote process.
470 *
Jeff Brown3f9dd282011-07-08 20:02:19 -0700471 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 * @throws RuntimeException on fatal start failure
473 *
474 * {@hide}
475 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700476 public static final ProcessStartResult start(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 final String niceName,
478 int uid, int gid, int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700479 int debugFlags, int mountExternal,
480 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500481 String seInfo,
Jeff Brown10e89712011-07-08 18:52:57 -0700482 String[] zygoteArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 try {
Jeff Brown10e89712011-07-08 18:52:57 -0700484 return startViaZygote(processClass, niceName, uid, gid, gids,
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100485 debugFlags, mountExternal, targetSdkVersion, seInfo,
486 null, /* zygoteAbi TODO: Replace this with the real ABI */
487 zygoteArgs);
Jeff Brown10e89712011-07-08 18:52:57 -0700488 } catch (ZygoteStartFailedEx ex) {
489 Log.e(LOG_TAG,
490 "Starting VM process through Zygote failed");
491 throw new RuntimeException(
492 "Starting VM process through Zygote failed", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 }
495
496 /** retry interval for opening a zygote socket */
497 static final int ZYGOTE_RETRY_MILLIS = 500;
498
499 /**
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100500 * Queries the zygote for the list of ABIS it supports.
501 *
502 * @throws ZygoteStartFailedEx if the query failed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 */
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100504 private static String getAbiList(BufferedWriter writer, DataInputStream inputStream)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 throws ZygoteStartFailedEx {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100506 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100508 // Each query starts with the argument count (1 in this case)
509 writer.write("1");
510 // ... followed by a new-line.
511 writer.newLine();
512 // ... followed by our only argument.
513 writer.write("--query-abi-list");
514 writer.newLine();
515 writer.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100517 // The response is a length prefixed stream of ASCII bytes.
518 int numBytes = inputStream.readInt();
519 byte[] bytes = new byte[numBytes];
520 inputStream.readFully(bytes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100522 return new String(bytes, StandardCharsets.US_ASCII);
523 } catch (IOException ioe) {
524 throw new ZygoteStartFailedEx(ioe);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 }
526 }
527
528 /**
529 * Sends an argument list to the zygote process, which starts a new child
530 * and returns the child's pid. Please note: the present implementation
531 * replaces newlines in the argument list with spaces.
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100532 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 * @throws ZygoteStartFailedEx if process start failed for any reason
534 */
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100535 private static ProcessStartResult zygoteSendArgsAndGetResult(
536 ZygoteState zygoteState, ArrayList<String> args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 try {
539 /**
540 * See com.android.internal.os.ZygoteInit.readArgumentList()
541 * Presently the wire format to the zygote process is:
542 * a) a count of arguments (argc, in essence)
543 * b) a number of newline-separated argument strings equal to count
544 *
545 * After the zygote process reads these it will write the pid of
Jeff Brown3f9dd282011-07-08 20:02:19 -0700546 * the child or -1 on failure, followed by boolean to
547 * indicate whether a wrapper process was used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 */
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100549 final BufferedWriter writer = zygoteState.writer;
550 final DataInputStream inputStream = zygoteState.inputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100552 writer.write(Integer.toString(args.size()));
553 writer.newLine();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554
555 int sz = args.size();
556 for (int i = 0; i < sz; i++) {
557 String arg = args.get(i);
558 if (arg.indexOf('\n') >= 0) {
559 throw new ZygoteStartFailedEx(
560 "embedded newlines not allowed");
561 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100562 writer.write(arg);
563 writer.newLine();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
565
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100566 writer.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567
568 // Should there be a timeout on this?
Jeff Brown3f9dd282011-07-08 20:02:19 -0700569 ProcessStartResult result = new ProcessStartResult();
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100570 result.pid = inputStream.readInt();
Jeff Brown3f9dd282011-07-08 20:02:19 -0700571 if (result.pid < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 throw new ZygoteStartFailedEx("fork() failed");
573 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100574 result.usingWrapper = inputStream.readBoolean();
Jeff Brown3f9dd282011-07-08 20:02:19 -0700575 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 } catch (IOException ex) {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100577 zygoteState.close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 throw new ZygoteStartFailedEx(ex);
579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 }
581
582 /**
583 * Starts a new process via the zygote mechanism.
584 *
585 * @param processClass Class name whose static main() to run
586 * @param niceName 'nice' process name to appear in ps
587 * @param uid a POSIX uid that the new process should setuid() to
588 * @param gid a POSIX gid that the new process shuold setgid() to
589 * @param gids null-ok; a list of supplementary group IDs that the
590 * new process should setgroup() to.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700591 * @param debugFlags Additional flags.
592 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalleybd19b9e2013-04-12 14:49:38 -0400593 * @param seInfo null-ok SELinux information for the new process.
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100594 * @param abi the ABI the process should use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 * @param extraArgs Additional arguments to supply to the zygote process.
Jeff Brown3f9dd282011-07-08 20:02:19 -0700596 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 * @throws ZygoteStartFailedEx if process start failed for any reason
598 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700599 private static ProcessStartResult startViaZygote(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 final String niceName,
601 final int uid, final int gid,
602 final int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700603 int debugFlags, int mountExternal,
604 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500605 String seInfo,
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100606 String abi,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 String[] extraArgs)
608 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 synchronized(Process.class) {
610 ArrayList<String> argsForZygote = new ArrayList<String>();
611
612 // --runtime-init, --setuid=, --setgid=,
613 // and --setgroups= must go first
614 argsForZygote.add("--runtime-init");
615 argsForZygote.add("--setuid=" + uid);
616 argsForZygote.add("--setgid=" + gid);
Elliott Hughesae07ecf2011-07-06 17:33:27 -0700617 if ((debugFlags & Zygote.DEBUG_ENABLE_JNI_LOGGING) != 0) {
618 argsForZygote.add("--enable-jni-logging");
619 }
Ben Cheng23085b72010-02-08 16:06:32 -0800620 if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) {
621 argsForZygote.add("--enable-safemode");
622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) {
624 argsForZygote.add("--enable-debugger");
625 }
626 if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) {
627 argsForZygote.add("--enable-checkjni");
628 }
629 if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) {
630 argsForZygote.add("--enable-assert");
631 }
Jeff Sharkey2bca8682012-08-22 13:59:58 -0700632 if (mountExternal == Zygote.MOUNT_EXTERNAL_MULTIUSER) {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700633 argsForZygote.add("--mount-external-multiuser");
Jeff Sharkeye217ee42012-08-28 16:23:01 -0700634 } else if (mountExternal == Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL) {
635 argsForZygote.add("--mount-external-multiuser-all");
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700636 }
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700637 argsForZygote.add("--target-sdk-version=" + targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638
639 //TODO optionally enable debuger
640 //argsForZygote.add("--enable-debugger");
641
642 // --setgroups is a comma-separated list
643 if (gids != null && gids.length > 0) {
644 StringBuilder sb = new StringBuilder();
645 sb.append("--setgroups=");
646
647 int sz = gids.length;
648 for (int i = 0; i < sz; i++) {
649 if (i != 0) {
650 sb.append(',');
651 }
652 sb.append(gids[i]);
653 }
654
655 argsForZygote.add(sb.toString());
656 }
657
658 if (niceName != null) {
659 argsForZygote.add("--nice-name=" + niceName);
660 }
661
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500662 if (seInfo != null) {
663 argsForZygote.add("--seinfo=" + seInfo);
664 }
665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 argsForZygote.add(processClass);
667
668 if (extraArgs != null) {
669 for (String arg : extraArgs) {
670 argsForZygote.add(arg);
671 }
672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100674 return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi), argsForZygote);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100677
678 /**
679 * Returns the number of times we attempt a connection to the zygote. We
680 * sleep for {@link #ZYGOTE_RETRY_MILLIS} milliseconds between each try.
681 *
682 * This could probably be removed, see TODO in {@code ZygoteState#connect}.
683 */
684 private static int getNumTries(ZygoteState state) {
685 // Retry 10 times for the first connection to each zygote.
686 if (state == null) {
687 return 11;
688 }
689
690 // This means the connection has already been established, but subsequently
691 // closed, possibly due to an IOException. We retry just once if that's the
692 // case.
693 return 1;
694 }
695
696 /**
697 * Tries to open socket to Zygote process if not already open. If
698 * already open, does nothing. May block and retry.
699 */
700 private static ZygoteState openZygoteSocketIfNeeded(String abi) throws ZygoteStartFailedEx {
701 if (primaryZygoteState == null || primaryZygoteState.isClosed()) {
702 primaryZygoteState = ZygoteState.connect(ZYGOTE_SOCKET, getNumTries(primaryZygoteState));
703 }
704
705 // TODO: Revert this temporary change. This is required to test
706 // and submit this change ahead of the package manager changes
707 // that supply this abi.
708 if (abi == null) {
709 return primaryZygoteState;
710 }
711
712 if (primaryZygoteState.matches(abi)) {
713 return primaryZygoteState;
714 }
715
716 // The primary zygote didn't match. Try the secondary.
717 if (secondaryZygoteState == null || secondaryZygoteState.isClosed()) {
718 secondaryZygoteState = ZygoteState.connect(SECONDARY_ZYGOTE_SOCKET,
719 getNumTries(secondaryZygoteState));
720 }
721
722 if (secondaryZygoteState.matches(abi)) {
723 return secondaryZygoteState;
724 }
725
726 throw new ZygoteStartFailedEx("Unsupported zygote ABI: " + abi);
727 }
728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 /**
730 * Returns elapsed milliseconds of the time this process has run.
731 * @return Returns the number of milliseconds this process has return.
732 */
733 public static final native long getElapsedCpuTime();
734
735 /**
736 * Returns the identifier of this process, which can be used with
737 * {@link #killProcess} and {@link #sendSignal}.
738 */
Jeff Hao406ec152013-07-30 10:13:41 -0700739 public static final int myPid() {
740 return Libcore.os.getpid();
741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742
743 /**
Romain Guy80b12fc2013-05-29 15:54:25 -0700744 * Returns the identifier of this process' parent.
745 * @hide
746 */
Elliott Hughes2a805f92013-07-31 18:25:47 -0700747 public static final int myPpid() {
748 return Libcore.os.getppid();
749 }
Romain Guy80b12fc2013-05-29 15:54:25 -0700750
751 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 * Returns the identifier of the calling thread, which be used with
753 * {@link #setThreadPriority(int, int)}.
754 */
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700755 public static final int myTid() {
756 return Libcore.os.gettid();
757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758
759 /**
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700760 * Returns the identifier of this process's uid. This is the kernel uid
761 * that the process is running under, which is the identity of its
762 * app-specific sandbox. It is different from {@link #myUserHandle} in that
763 * a uid identifies a specific app sandbox in a specific user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 */
Jeff Hao406ec152013-07-30 10:13:41 -0700765 public static final int myUid() {
766 return Libcore.os.getuid();
767 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768
769 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700770 * Returns this process's user handle. This is the
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700771 * user the process is running under. It is distinct from
772 * {@link #myUid()} in that a particular user will have multiple
773 * distinct apps running under it each with their own uid.
774 */
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700775 public static final UserHandle myUserHandle() {
776 return new UserHandle(UserHandle.getUserId(myUid()));
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700777 }
778
779 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800780 * Returns whether the current process is in an isolated sandbox.
781 * @hide
782 */
783 public static final boolean isIsolated() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700784 int uid = UserHandle.getAppId(myUid());
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800785 return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID;
786 }
787
788 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 * Returns the UID assigned to a particular user name, or -1 if there is
790 * none. If the given string consists of only numbers, it is converted
791 * directly to a uid.
792 */
793 public static final native int getUidForName(String name);
794
795 /**
796 * Returns the GID assigned to a particular user name, or -1 if there is
797 * none. If the given string consists of only numbers, it is converted
798 * directly to a gid.
799 */
800 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700801
802 /**
803 * Returns a uid for a currently running process.
804 * @param pid the process id
805 * @return the uid of the process, or -1 if the process is not running.
806 * @hide pending API council review
807 */
808 public static final int getUidForPid(int pid) {
809 String[] procStatusLabels = { "Uid:" };
810 long[] procStatusValues = new long[1];
811 procStatusValues[0] = -1;
812 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
813 return (int) procStatusValues[0];
814 }
815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700817 * Returns the parent process id for a currently running process.
818 * @param pid the process id
819 * @return the parent process id of the process, or -1 if the process is not running.
820 * @hide
821 */
822 public static final int getParentPid(int pid) {
823 String[] procStatusLabels = { "PPid:" };
824 long[] procStatusValues = new long[1];
825 procStatusValues[0] = -1;
826 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
827 return (int) procStatusValues[0];
828 }
829
830 /**
Glenn Kasten07b04652012-04-23 15:00:43 -0700831 * Returns the thread group leader id for a currently running thread.
832 * @param tid the thread id
833 * @return the thread group leader id of the thread, or -1 if the thread is not running.
834 * This is same as what getpid(2) would return if called by tid.
835 * @hide
836 */
837 public static final int getThreadGroupLeader(int tid) {
838 String[] procStatusLabels = { "Tgid:" };
839 long[] procStatusValues = new long[1];
840 procStatusValues[0] = -1;
841 Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues);
842 return (int) procStatusValues[0];
843 }
844
845 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 * Set the priority of a thread, based on Linux priorities.
847 *
848 * @param tid The identifier of the thread/process to change.
849 * @param priority A Linux priority level, from -20 for highest scheduling
850 * priority to 19 for lowest scheduling priority.
851 *
852 * @throws IllegalArgumentException Throws IllegalArgumentException if
853 * <var>tid</var> does not exist.
854 * @throws SecurityException Throws SecurityException if your process does
855 * not have permission to modify the given thread, or to use the given
856 * priority.
857 */
858 public static final native void setThreadPriority(int tid, int priority)
859 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700860
861 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700862 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
863 * throw an exception if passed a background-level thread priority. This is only
864 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
865 *
866 * @hide
867 */
868 public static final native void setCanSelfBackground(boolean backgroundOk);
869
870 /**
San Mehate9d376b2009-04-21 14:06:36 -0700871 * Sets the scheduling group for a thread.
872 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700873 * @param tid The identifier of the thread to change.
874 * @param group The target group for this thread from THREAD_GROUP_*.
San Mehate9d376b2009-04-21 14:06:36 -0700875 *
876 * @throws IllegalArgumentException Throws IllegalArgumentException if
877 * <var>tid</var> does not exist.
878 * @throws SecurityException Throws SecurityException if your process does
879 * not have permission to modify the given thread, or to use the given
880 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700881 * If the thread is a thread group leader, that is it's gettid() == getpid(),
882 * then the other threads in the same thread group are _not_ affected.
San Mehate9d376b2009-04-21 14:06:36 -0700883 */
884 public static final native void setThreadGroup(int tid, int group)
885 throws IllegalArgumentException, SecurityException;
Glenn Kastenf1b56442012-03-15 16:33:43 -0700886
San Mehat3e458242009-05-19 14:44:16 -0700887 /**
888 * Sets the scheduling group for a process and all child threads
889 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700890 * @param pid The identifier of the process to change.
891 * @param group The target group for this process from THREAD_GROUP_*.
San Mehat3e458242009-05-19 14:44:16 -0700892 *
893 * @throws IllegalArgumentException Throws IllegalArgumentException if
894 * <var>tid</var> does not exist.
895 * @throws SecurityException Throws SecurityException if your process does
896 * not have permission to modify the given thread, or to use the given
897 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700898 *
899 * group == THREAD_GROUP_DEFAULT means to move all non-background priority
900 * threads to the foreground scheduling group, but to leave background
901 * priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all
902 * threads, regardless of priority, to the background scheduling group.
903 * group == THREAD_GROUP_FOREGROUND is not allowed.
San Mehat3e458242009-05-19 14:44:16 -0700904 */
905 public static final native void setProcessGroup(int pid, int group)
906 throws IllegalArgumentException, SecurityException;
Jeff Sharkey9e57c412013-01-17 14:12:41 -0800907
908 /**
909 * Return the scheduling group of requested process.
910 *
911 * @hide
912 */
913 public static final native int getProcessGroup(int pid)
914 throws IllegalArgumentException, SecurityException;
915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 /**
917 * Set the priority of the calling thread, based on Linux priorities. See
918 * {@link #setThreadPriority(int, int)} for more information.
919 *
920 * @param priority A Linux priority level, from -20 for highest scheduling
921 * priority to 19 for lowest scheduling priority.
922 *
923 * @throws IllegalArgumentException Throws IllegalArgumentException if
924 * <var>tid</var> does not exist.
925 * @throws SecurityException Throws SecurityException if your process does
926 * not have permission to modify the given thread, or to use the given
927 * priority.
928 *
929 * @see #setThreadPriority(int, int)
930 */
931 public static final native void setThreadPriority(int priority)
932 throws IllegalArgumentException, SecurityException;
933
934 /**
935 * Return the current priority of a thread, based on Linux priorities.
936 *
937 * @param tid The identifier of the thread/process to change.
938 *
939 * @return Returns the current priority, as a Linux priority level,
940 * from -20 for highest scheduling priority to 19 for lowest scheduling
941 * priority.
942 *
943 * @throws IllegalArgumentException Throws IllegalArgumentException if
944 * <var>tid</var> does not exist.
945 */
946 public static final native int getThreadPriority(int tid)
947 throws IllegalArgumentException;
948
949 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700950 * Set the scheduling policy and priority of a thread, based on Linux.
951 *
952 * @param tid The identifier of the thread/process to change.
953 * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
954 * @param priority A Linux priority level in a range appropriate for the given policy.
955 *
956 * @throws IllegalArgumentException Throws IllegalArgumentException if
957 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
958 * @throws SecurityException Throws SecurityException if your process does
959 * not have permission to modify the given thread, or to use the given
960 * scheduling policy or priority.
961 *
962 * {@hide}
963 */
964 public static final native void setThreadScheduler(int tid, int policy, int priority)
965 throws IllegalArgumentException;
966
967 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 * Determine whether the current environment supports multiple processes.
969 *
970 * @return Returns true if the system can run in multiple processes, else
971 * false if everything is running in a single process.
Jeff Brown10e89712011-07-08 18:52:57 -0700972 *
973 * @deprecated This method always returns true. Do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 */
Jeff Brown10e89712011-07-08 18:52:57 -0700975 @Deprecated
976 public static final boolean supportsProcesses() {
977 return true;
978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979
980 /**
981 * Set the out-of-memory badness adjustment for a process.
982 *
983 * @param pid The process identifier to set.
984 * @param amt Adjustment value -- linux allows -16 to +15.
985 *
986 * @return Returns true if the underlying system supports this
987 * feature, else false.
988 *
989 * {@hide}
990 */
991 public static final native boolean setOomAdj(int pid, int amt);
992
993 /**
Rom Lemarchand5534ba92013-07-12 16:15:36 -0700994 * Adjust the swappiness level for a process.
995 *
996 * @param pid The process identifier to set.
997 * @param is_increased Whether swappiness should be increased or default.
998 *
999 * @return Returns true if the underlying system supports this
1000 * feature, else false.
1001 *
1002 * {@hide}
1003 */
1004 public static final native boolean setSwappiness(int pid, boolean is_increased);
1005
1006 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 * Change this process's argv[0] parameter. This can be useful to show
1008 * more descriptive information in things like the 'ps' command.
1009 *
1010 * @param text The new name of this process.
1011 *
1012 * {@hide}
1013 */
1014 public static final native void setArgV0(String text);
1015
1016 /**
1017 * Kill the process with the given PID.
1018 * Note that, though this API allows us to request to
1019 * kill any process based on its PID, the kernel will
1020 * still impose standard restrictions on which PIDs you
1021 * are actually able to kill. Typically this means only
1022 * the process running the caller's packages/application
1023 * and any additional processes created by that app; packages
1024 * sharing a common UID will also be able to kill each
1025 * other's processes.
1026 */
1027 public static final void killProcess(int pid) {
1028 sendSignal(pid, SIGNAL_KILL);
1029 }
1030
1031 /** @hide */
1032 public static final native int setUid(int uid);
1033
1034 /** @hide */
1035 public static final native int setGid(int uid);
1036
1037 /**
1038 * Send a signal to the given process.
1039 *
1040 * @param pid The pid of the target process.
1041 * @param signal The signal to send.
1042 */
1043 public static final native void sendSignal(int pid, int signal);
1044
Dianne Hackborn906497c2010-05-10 15:57:38 -07001045 /**
1046 * @hide
1047 * Private impl for avoiding a log message... DO NOT USE without doing
1048 * your own log, or the Android Illuminati will find you some night and
1049 * beat you up.
1050 */
1051 public static final void killProcessQuiet(int pid) {
1052 sendSignalQuiet(pid, SIGNAL_KILL);
1053 }
1054
1055 /**
1056 * @hide
1057 * Private impl for avoiding a log message... DO NOT USE without doing
1058 * your own log, or the Android Illuminati will find you some night and
1059 * beat you up.
1060 */
1061 public static final native void sendSignalQuiet(int pid, int signal);
1062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 /** @hide */
Marco Nelissen0bca96b2009-07-17 12:59:25 -07001064 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065
1066 /** @hide */
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001067 public static final native long getTotalMemory();
1068
1069 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 public static final native void readProcLines(String path,
1071 String[] reqFields, long[] outSizes);
1072
1073 /** @hide */
1074 public static final native int[] getPids(String path, int[] lastArray);
1075
1076 /** @hide */
1077 public static final int PROC_TERM_MASK = 0xff;
1078 /** @hide */
1079 public static final int PROC_ZERO_TERM = 0;
1080 /** @hide */
1081 public static final int PROC_SPACE_TERM = (int)' ';
1082 /** @hide */
Evan Millarc64edde2009-04-18 12:26:32 -07001083 public static final int PROC_TAB_TERM = (int)'\t';
1084 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 public static final int PROC_COMBINE = 0x100;
1086 /** @hide */
1087 public static final int PROC_PARENS = 0x200;
1088 /** @hide */
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001089 public static final int PROC_QUOTES = 0x400;
1090 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 public static final int PROC_OUT_STRING = 0x1000;
1092 /** @hide */
1093 public static final int PROC_OUT_LONG = 0x2000;
1094 /** @hide */
1095 public static final int PROC_OUT_FLOAT = 0x4000;
1096
1097 /** @hide */
1098 public static final native boolean readProcFile(String file, int[] format,
1099 String[] outStrings, long[] outLongs, float[] outFloats);
Evan Millarc64edde2009-04-18 12:26:32 -07001100
1101 /** @hide */
1102 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
1103 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001105 /** @hide */
1106 public static final native int[] getPidsForCommands(String[] cmds);
1107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 /**
1109 * Gets the total Pss value for a given process, in bytes.
1110 *
1111 * @param pid the process to the Pss for
1112 * @return the total Pss value for the given process in bytes,
1113 * or -1 if the value cannot be determined
1114 * @hide
1115 */
1116 public static final native long getPss(int pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -07001117
1118 /**
1119 * Specifies the outcome of having started a process.
1120 * @hide
1121 */
1122 public static final class ProcessStartResult {
1123 /**
1124 * The PID of the newly started process.
1125 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
1126 */
1127 public int pid;
1128
1129 /**
1130 * True if the process was started with a wrapper attached.
1131 */
1132 public boolean usingWrapper;
1133 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134}