blob: 7bedfc118bd6f67d6ecd48690f19dbf4d3575e09 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os;
18
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import android.net.LocalSocket;
Narayan Kamath973b4662014-03-31 13:41:26 +010020import android.net.LocalSocketAddress;
Elliott Hughes34385d32014-04-28 11:11:32 -070021import android.system.Os;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.util.Log;
Narayan Kamath973b4662014-03-31 13:41:26 +010023import com.android.internal.os.Zygote;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import java.io.BufferedWriter;
25import java.io.DataInputStream;
26import java.io.IOException;
27import java.io.OutputStreamWriter;
Narayan Kamath4444dcd2014-04-03 20:15:15 +010028import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import java.util.ArrayList;
Narayan Kamath4444dcd2014-04-03 20:15:15 +010030import java.util.Arrays;
31import java.util.List;
Jeff Hao406ec152013-07-30 10:13:41 -070032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033/*package*/ class ZygoteStartFailedEx extends Exception {
Narayan Kamath64cd9072014-05-13 13:35:14 +010034 ZygoteStartFailedEx(String s) {
35 super(s);
36 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
Narayan Kamath64cd9072014-05-13 13:35:14 +010038 ZygoteStartFailedEx(Throwable cause) {
39 super(cause);
40 }
41
42 ZygoteStartFailedEx(String s, Throwable cause) {
43 super(s, cause);
44 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045}
46
47/**
48 * Tools for managing OS processes.
49 */
50public class Process {
51 private static final String LOG_TAG = "Process";
52
Narayan Kamath64cd9072014-05-13 13:35:14 +010053 /**
54 * @hide for internal use only.
55 */
56 public static final String ZYGOTE_SOCKET = "zygote";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
Narayan Kamath64cd9072014-05-13 13:35:14 +010058 /**
59 * @hide for internal use only.
60 */
61 public static final String SECONDARY_ZYGOTE_SOCKET = "zygote_secondary";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
63 /**
64 * Defines the UID/GID under which system code runs.
65 */
66 public static final int SYSTEM_UID = 1000;
67
68 /**
69 * Defines the UID/GID under which the telephony code runs.
70 */
71 public static final int PHONE_UID = 1001;
72
73 /**
Dianne Hackborn854060af2009-07-09 18:14:31 -070074 * Defines the UID/GID for the user shell.
75 * @hide
76 */
77 public static final int SHELL_UID = 2000;
78
79 /**
Mike Lockwoodd42685d2009-09-03 09:25:22 -040080 * Defines the UID/GID for the log group.
81 * @hide
82 */
83 public static final int LOG_UID = 1007;
84
85 /**
Amith Yamasanid1582142009-07-08 20:04:55 -070086 * Defines the UID/GID for the WIFI supplicant process.
87 * @hide
88 */
89 public static final int WIFI_UID = 1010;
90
91 /**
Glenn Kasten8b7d1b42011-07-13 16:23:22 -070092 * Defines the UID/GID for the mediaserver process.
93 * @hide
94 */
95 public static final int MEDIA_UID = 1013;
96
97 /**
Jeff Sharkey5294a2f2012-04-24 17:07:22 -070098 * Defines the UID/GID for the DRM process.
99 * @hide
100 */
101 public static final int DRM_UID = 1019;
102
103 /**
Kenny Root26993b32012-03-19 15:07:51 -0700104 * Defines the UID/GID for the group that controls VPN services.
105 * @hide
106 */
107 public static final int VPN_UID = 1016;
108
109 /**
Nick Pellycd0e8392010-10-13 17:25:24 -0700110 * Defines the UID/GID for the NFC service process.
111 * @hide
112 */
Nick Pellya5cb9f42011-11-21 14:54:46 -0800113 public static final int NFC_UID = 1027;
Nick Pellycd0e8392010-10-13 17:25:24 -0700114
115 /**
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800116 * Defines the UID/GID for the Bluetooth service process.
117 * @hide
118 */
119 public static final int BLUETOOTH_UID = 1002;
120
121 /**
Mike Lockwooddcaa10c2010-12-16 12:50:44 -0800122 * Defines the GID for the group that allows write access to the internal media storage.
123 * @hide
124 */
125 public static final int MEDIA_RW_GID = 1023;
126
127 /**
Jeff Sharkey184a0102013-07-10 16:19:52 -0700128 * Access to installed package details
129 * @hide
130 */
131 public static final int PACKAGE_INFO_GID = 1032;
132
133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * Defines the start of a range of UIDs (and GIDs), going from this
135 * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning
136 * to applications.
137 */
138 public static final int FIRST_APPLICATION_UID = 10000;
Jeff Sharkey184a0102013-07-10 16:19:52 -0700139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 /**
141 * Last of application-specific UIDs starting at
142 * {@link #FIRST_APPLICATION_UID}.
143 */
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800144 public static final int LAST_APPLICATION_UID = 19999;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800145
146 /**
147 * First uid used for fully isolated sandboxed processes (with no permissions of their own)
148 * @hide
149 */
150 public static final int FIRST_ISOLATED_UID = 99000;
151
152 /**
153 * Last uid used for fully isolated sandboxed processes (with no permissions of their own)
154 * @hide
155 */
156 public static final int LAST_ISOLATED_UID = 99999;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157
158 /**
Robin Leee66b6892014-04-29 12:36:47 +0100159 * Defines the gid shared by all applications running under the same profile.
160 * @hide
161 */
162 public static final int SHARED_USER_GID = 9997;
163
164 /**
Kenny Roote091f222012-09-11 15:01:26 -0700165 * First gid for applications to share resources. Used when forward-locking
166 * is enabled but all UserHandles need to be able to read the resources.
167 * @hide
168 */
169 public static final int FIRST_SHARED_APPLICATION_GID = 50000;
170
171 /**
172 * Last gid for applications to share resources. Used when forward-locking
173 * is enabled but all UserHandles need to be able to read the resources.
174 * @hide
175 */
176 public static final int LAST_SHARED_APPLICATION_GID = 59999;
177
178 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 * Standard priority of application threads.
180 * Use with {@link #setThreadPriority(int)} and
181 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
182 * {@link java.lang.Thread} class.
183 */
184 public static final int THREAD_PRIORITY_DEFAULT = 0;
185
186 /*
187 * ***************************************
188 * ** Keep in sync with utils/threads.h **
189 * ***************************************
190 */
191
192 /**
193 * Lowest available thread priority. Only for those who really, really
194 * don't want to run if anything else is happening.
195 * Use with {@link #setThreadPriority(int)} and
196 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
197 * {@link java.lang.Thread} class.
198 */
199 public static final int THREAD_PRIORITY_LOWEST = 19;
200
201 /**
202 * Standard priority background threads. This gives your thread a slightly
203 * lower than normal priority, so that it will have less chance of impacting
204 * the responsiveness of the user interface.
205 * Use with {@link #setThreadPriority(int)} and
206 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
207 * {@link java.lang.Thread} class.
208 */
209 public static final int THREAD_PRIORITY_BACKGROUND = 10;
210
211 /**
212 * Standard priority of threads that are currently running a user interface
213 * that the user is interacting with. Applications can not normally
214 * change to this priority; the system will automatically adjust your
215 * application threads as the user moves through the UI.
216 * Use with {@link #setThreadPriority(int)} and
217 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
218 * {@link java.lang.Thread} class.
219 */
220 public static final int THREAD_PRIORITY_FOREGROUND = -2;
221
222 /**
223 * Standard priority of system display threads, involved in updating
224 * the user interface. Applications can not
225 * normally change to this priority.
226 * Use with {@link #setThreadPriority(int)} and
227 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
228 * {@link java.lang.Thread} class.
229 */
230 public static final int THREAD_PRIORITY_DISPLAY = -4;
231
232 /**
233 * Standard priority of the most important display threads, for compositing
234 * the screen and retrieving input events. Applications can not normally
235 * change to this priority.
236 * Use with {@link #setThreadPriority(int)} and
237 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
238 * {@link java.lang.Thread} class.
239 */
240 public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8;
241
242 /**
243 * Standard priority of audio threads. Applications can not normally
244 * change to this priority.
245 * Use with {@link #setThreadPriority(int)} and
246 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
247 * {@link java.lang.Thread} class.
248 */
249 public static final int THREAD_PRIORITY_AUDIO = -16;
250
251 /**
252 * Standard priority of the most important audio threads.
253 * Applications can not normally change to this priority.
254 * Use with {@link #setThreadPriority(int)} and
255 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
256 * {@link java.lang.Thread} class.
257 */
258 public static final int THREAD_PRIORITY_URGENT_AUDIO = -19;
259
260 /**
261 * Minimum increment to make a priority more favorable.
262 */
263 public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
264
265 /**
266 * Minimum increment to make a priority less favorable.
267 */
268 public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
269
San Mehate9d376b2009-04-21 14:06:36 -0700270 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700271 * Default scheduling policy
272 * @hide
273 */
274 public static final int SCHED_OTHER = 0;
275
276 /**
277 * First-In First-Out scheduling policy
278 * @hide
279 */
280 public static final int SCHED_FIFO = 1;
281
282 /**
283 * Round-Robin scheduling policy
284 * @hide
285 */
286 public static final int SCHED_RR = 2;
287
288 /**
289 * Batch scheduling policy
290 * @hide
291 */
292 public static final int SCHED_BATCH = 3;
293
294 /**
295 * Idle scheduling policy
296 * @hide
297 */
298 public static final int SCHED_IDLE = 5;
299
Glenn Kastenf1b56442012-03-15 16:33:43 -0700300 // Keep in sync with SP_* constants of enum type SchedPolicy
301 // declared in system/core/include/cutils/sched_policy.h,
302 // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
San Mehate9d376b2009-04-21 14:06:36 -0700303
304 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700305 * Default thread group -
306 * has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
307 * When used with setProcessGroup(), the group of each thread in the process
308 * is conditionally changed based on that thread's current priority, as follows:
309 * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
310 * are moved to foreground thread group. All other threads are left unchanged.
311 * @hide
312 */
313 public static final int THREAD_GROUP_DEFAULT = -1;
314
315 /**
316 * Background thread group - All threads in
San Mehate9d376b2009-04-21 14:06:36 -0700317 * this group are scheduled with a reduced share of the CPU.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700318 * Value is same as constant SP_BACKGROUND of enum SchedPolicy.
319 * FIXME rename to THREAD_GROUP_BACKGROUND.
San Mehate9d376b2009-04-21 14:06:36 -0700320 * @hide
321 */
Glenn Kastenf1b56442012-03-15 16:33:43 -0700322 public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0;
San Mehate9d376b2009-04-21 14:06:36 -0700323
324 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700325 * Foreground thread group - All threads in
326 * this group are scheduled with a normal share of the CPU.
327 * Value is same as constant SP_FOREGROUND of enum SchedPolicy.
328 * Not used at this level.
San Mehate9d376b2009-04-21 14:06:36 -0700329 * @hide
330 **/
Glenn Kastenf1b56442012-03-15 16:33:43 -0700331 private static final int THREAD_GROUP_FOREGROUND = 1;
San Mehate9d376b2009-04-21 14:06:36 -0700332
Glenn Kasten07b04652012-04-23 15:00:43 -0700333 /**
334 * System thread group.
335 * @hide
336 **/
337 public static final int THREAD_GROUP_SYSTEM = 2;
338
339 /**
340 * Application audio thread group.
341 * @hide
342 **/
343 public static final int THREAD_GROUP_AUDIO_APP = 3;
344
345 /**
346 * System audio thread group.
347 * @hide
348 **/
349 public static final int THREAD_GROUP_AUDIO_SYS = 4;
350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 public static final int SIGNAL_QUIT = 3;
352 public static final int SIGNAL_KILL = 9;
353 public static final int SIGNAL_USR1 = 10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100355 /**
356 * State for communicating with the zygote process.
Narayan Kamath64cd9072014-05-13 13:35:14 +0100357 *
358 * @hide for internal use only.
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100359 */
Narayan Kamath64cd9072014-05-13 13:35:14 +0100360 public static class ZygoteState {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100361 final LocalSocket socket;
362 final DataInputStream inputStream;
363 final BufferedWriter writer;
364 final List<String> abiList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100366 boolean mClosed;
367
368 private ZygoteState(LocalSocket socket, DataInputStream inputStream,
369 BufferedWriter writer, List<String> abiList) {
370 this.socket = socket;
371 this.inputStream = inputStream;
372 this.writer = writer;
373 this.abiList = abiList;
374 }
375
Narayan Kamath64cd9072014-05-13 13:35:14 +0100376 public static ZygoteState connect(String socketAddress) throws IOException {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100377 DataInputStream zygoteInputStream = null;
378 BufferedWriter zygoteWriter = null;
Narayan Kamath64cd9072014-05-13 13:35:14 +0100379 final LocalSocket zygoteSocket = new LocalSocket();
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100380
Narayan Kamath64cd9072014-05-13 13:35:14 +0100381 try {
382 zygoteSocket.connect(new LocalSocketAddress(socketAddress,
383 LocalSocketAddress.Namespace.RESERVED));
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100384
Narayan Kamath64cd9072014-05-13 13:35:14 +0100385 zygoteInputStream = new DataInputStream(zygoteSocket.getInputStream());
386
387 zygoteWriter = new BufferedWriter(new OutputStreamWriter(
388 zygoteSocket.getOutputStream()), 256);
389 } catch (IOException ex) {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100390 try {
Narayan Kamath64cd9072014-05-13 13:35:14 +0100391 zygoteSocket.close();
392 } catch (IOException ignore) {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100393 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100394
Narayan Kamath64cd9072014-05-13 13:35:14 +0100395 throw ex;
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100396 }
397
398 String abiListString = getAbiList(zygoteWriter, zygoteInputStream);
399 Log.i("Zygote", "Process: zygote socket opened, supported ABIS: " + abiListString);
400
401 return new ZygoteState(zygoteSocket, zygoteInputStream, zygoteWriter,
402 Arrays.asList(abiListString.split(",")));
403 }
404
405 boolean matches(String abi) {
406 return abiList.contains(abi);
407 }
408
Narayan Kamath64cd9072014-05-13 13:35:14 +0100409 public void close() {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100410 try {
411 socket.close();
412 } catch (IOException ex) {
413 Log.e(LOG_TAG,"I/O exception on routine close", ex);
414 }
415
416 mClosed = true;
417 }
418
419 boolean isClosed() {
420 return mClosed;
421 }
422 }
423
424 /**
425 * The state of the connection to the primary zygote.
426 */
427 static ZygoteState primaryZygoteState;
428
429 /**
430 * The state of the connection to the secondary zygote.
431 */
432 static ZygoteState secondaryZygoteState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433
434 /**
435 * Start a new process.
436 *
437 * <p>If processes are enabled, a new process is created and the
438 * static main() function of a <var>processClass</var> is executed there.
439 * The process will continue running after this function returns.
440 *
441 * <p>If processes are not enabled, a new thread in the caller's
442 * process is created and main() of <var>processClass</var> called there.
443 *
444 * <p>The niceName parameter, if not an empty string, is a custom name to
445 * give to the process instead of using processClass. This allows you to
446 * make easily identifyable processes even if you are using the same base
447 * <var>processClass</var> to start them.
448 *
449 * @param processClass The class to use as the process's main entry
450 * point.
451 * @param niceName A more readable name to use for the process.
452 * @param uid The user-id under which the process will run.
453 * @param gid The group-id under which the process will run.
454 * @param gids Additional group-ids associated with the process.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700455 * @param debugFlags Additional flags.
456 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalleybd19b9e2013-04-12 14:49:38 -0400457 * @param seInfo null-ok SELinux information for the new process.
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100458 * @param abi non-null the ABI this app should be started with.
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700459 * @param instructionSet null-ok the instruction set to use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 * @param zygoteArgs Additional arguments to supply to the zygote process.
461 *
Jeff Brown3f9dd282011-07-08 20:02:19 -0700462 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 * @throws RuntimeException on fatal start failure
464 *
465 * {@hide}
466 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700467 public static final ProcessStartResult start(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 final String niceName,
469 int uid, int gid, int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700470 int debugFlags, int mountExternal,
471 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500472 String seInfo,
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100473 String abi,
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700474 String instructionSet,
Jeff Brown10e89712011-07-08 18:52:57 -0700475 String[] zygoteArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 try {
Jeff Brown10e89712011-07-08 18:52:57 -0700477 return startViaZygote(processClass, niceName, uid, gid, gids,
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100478 debugFlags, mountExternal, targetSdkVersion, seInfo,
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700479 abi, instructionSet, zygoteArgs);
Jeff Brown10e89712011-07-08 18:52:57 -0700480 } catch (ZygoteStartFailedEx ex) {
481 Log.e(LOG_TAG,
482 "Starting VM process through Zygote failed");
483 throw new RuntimeException(
484 "Starting VM process through Zygote failed", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 }
487
488 /** retry interval for opening a zygote socket */
489 static final int ZYGOTE_RETRY_MILLIS = 500;
490
491 /**
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100492 * Queries the zygote for the list of ABIS it supports.
493 *
494 * @throws ZygoteStartFailedEx if the query failed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 */
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100496 private static String getAbiList(BufferedWriter writer, DataInputStream inputStream)
Narayan Kamath64cd9072014-05-13 13:35:14 +0100497 throws IOException {
498 // Each query starts with the argument count (1 in this case)
499 writer.write("1");
500 // ... followed by a new-line.
501 writer.newLine();
502 // ... followed by our only argument.
503 writer.write("--query-abi-list");
504 writer.newLine();
505 writer.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506
Narayan Kamath64cd9072014-05-13 13:35:14 +0100507 // The response is a length prefixed stream of ASCII bytes.
508 int numBytes = inputStream.readInt();
509 byte[] bytes = new byte[numBytes];
510 inputStream.readFully(bytes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511
Narayan Kamath64cd9072014-05-13 13:35:14 +0100512 return new String(bytes, StandardCharsets.US_ASCII);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 }
514
515 /**
516 * Sends an argument list to the zygote process, which starts a new child
517 * and returns the child's pid. Please note: the present implementation
518 * replaces newlines in the argument list with spaces.
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100519 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 * @throws ZygoteStartFailedEx if process start failed for any reason
521 */
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100522 private static ProcessStartResult zygoteSendArgsAndGetResult(
523 ZygoteState zygoteState, ArrayList<String> args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 try {
526 /**
527 * See com.android.internal.os.ZygoteInit.readArgumentList()
528 * Presently the wire format to the zygote process is:
529 * a) a count of arguments (argc, in essence)
530 * b) a number of newline-separated argument strings equal to count
531 *
532 * After the zygote process reads these it will write the pid of
Jeff Brown3f9dd282011-07-08 20:02:19 -0700533 * the child or -1 on failure, followed by boolean to
534 * indicate whether a wrapper process was used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 */
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100536 final BufferedWriter writer = zygoteState.writer;
537 final DataInputStream inputStream = zygoteState.inputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100539 writer.write(Integer.toString(args.size()));
540 writer.newLine();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541
542 int sz = args.size();
543 for (int i = 0; i < sz; i++) {
544 String arg = args.get(i);
545 if (arg.indexOf('\n') >= 0) {
546 throw new ZygoteStartFailedEx(
547 "embedded newlines not allowed");
548 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100549 writer.write(arg);
550 writer.newLine();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 }
552
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100553 writer.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554
555 // Should there be a timeout on this?
Jeff Brown3f9dd282011-07-08 20:02:19 -0700556 ProcessStartResult result = new ProcessStartResult();
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100557 result.pid = inputStream.readInt();
Jeff Brown3f9dd282011-07-08 20:02:19 -0700558 if (result.pid < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 throw new ZygoteStartFailedEx("fork() failed");
560 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100561 result.usingWrapper = inputStream.readBoolean();
Jeff Brown3f9dd282011-07-08 20:02:19 -0700562 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 } catch (IOException ex) {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100564 zygoteState.close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 throw new ZygoteStartFailedEx(ex);
566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 }
568
569 /**
570 * Starts a new process via the zygote mechanism.
571 *
572 * @param processClass Class name whose static main() to run
573 * @param niceName 'nice' process name to appear in ps
574 * @param uid a POSIX uid that the new process should setuid() to
575 * @param gid a POSIX gid that the new process shuold setgid() to
576 * @param gids null-ok; a list of supplementary group IDs that the
577 * new process should setgroup() to.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700578 * @param debugFlags Additional flags.
579 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalleybd19b9e2013-04-12 14:49:38 -0400580 * @param seInfo null-ok SELinux information for the new process.
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100581 * @param abi the ABI the process should use.
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700582 * @param instructionSet null-ok the instruction set to use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 * @param extraArgs Additional arguments to supply to the zygote process.
Jeff Brown3f9dd282011-07-08 20:02:19 -0700584 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 * @throws ZygoteStartFailedEx if process start failed for any reason
586 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700587 private static ProcessStartResult startViaZygote(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 final String niceName,
589 final int uid, final int gid,
590 final int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700591 int debugFlags, int mountExternal,
592 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500593 String seInfo,
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100594 String abi,
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700595 String instructionSet,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 String[] extraArgs)
597 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 synchronized(Process.class) {
599 ArrayList<String> argsForZygote = new ArrayList<String>();
600
601 // --runtime-init, --setuid=, --setgid=,
602 // and --setgroups= must go first
603 argsForZygote.add("--runtime-init");
604 argsForZygote.add("--setuid=" + uid);
605 argsForZygote.add("--setgid=" + gid);
Elliott Hughesae07ecf2011-07-06 17:33:27 -0700606 if ((debugFlags & Zygote.DEBUG_ENABLE_JNI_LOGGING) != 0) {
607 argsForZygote.add("--enable-jni-logging");
608 }
Ben Cheng23085b72010-02-08 16:06:32 -0800609 if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) {
610 argsForZygote.add("--enable-safemode");
611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) {
613 argsForZygote.add("--enable-debugger");
614 }
615 if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) {
616 argsForZygote.add("--enable-checkjni");
617 }
618 if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) {
619 argsForZygote.add("--enable-assert");
620 }
Jeff Sharkey2bca8682012-08-22 13:59:58 -0700621 if (mountExternal == Zygote.MOUNT_EXTERNAL_MULTIUSER) {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700622 argsForZygote.add("--mount-external-multiuser");
Jeff Sharkeye217ee42012-08-28 16:23:01 -0700623 } else if (mountExternal == Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL) {
624 argsForZygote.add("--mount-external-multiuser-all");
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700625 }
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700626 argsForZygote.add("--target-sdk-version=" + targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627
628 //TODO optionally enable debuger
629 //argsForZygote.add("--enable-debugger");
630
631 // --setgroups is a comma-separated list
632 if (gids != null && gids.length > 0) {
633 StringBuilder sb = new StringBuilder();
634 sb.append("--setgroups=");
635
636 int sz = gids.length;
637 for (int i = 0; i < sz; i++) {
638 if (i != 0) {
639 sb.append(',');
640 }
641 sb.append(gids[i]);
642 }
643
644 argsForZygote.add(sb.toString());
645 }
646
647 if (niceName != null) {
648 argsForZygote.add("--nice-name=" + niceName);
649 }
650
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500651 if (seInfo != null) {
652 argsForZygote.add("--seinfo=" + seInfo);
653 }
654
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700655 if (instructionSet != null) {
656 argsForZygote.add("--instruction-set=" + instructionSet);
657 }
658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 argsForZygote.add(processClass);
660
661 if (extraArgs != null) {
662 for (String arg : extraArgs) {
663 argsForZygote.add(arg);
664 }
665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100667 return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi), argsForZygote);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100670
671 /**
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100672 * Tries to open socket to Zygote process if not already open. If
673 * already open, does nothing. May block and retry.
674 */
675 private static ZygoteState openZygoteSocketIfNeeded(String abi) throws ZygoteStartFailedEx {
676 if (primaryZygoteState == null || primaryZygoteState.isClosed()) {
Narayan Kamath64cd9072014-05-13 13:35:14 +0100677 try {
678 primaryZygoteState = ZygoteState.connect(ZYGOTE_SOCKET);
679 } catch (IOException ioe) {
680 throw new ZygoteStartFailedEx("Error connecting to primary zygote", ioe);
681 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100682 }
683
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100684 if (primaryZygoteState.matches(abi)) {
685 return primaryZygoteState;
686 }
687
688 // The primary zygote didn't match. Try the secondary.
689 if (secondaryZygoteState == null || secondaryZygoteState.isClosed()) {
Narayan Kamath64cd9072014-05-13 13:35:14 +0100690 try {
691 secondaryZygoteState = ZygoteState.connect(SECONDARY_ZYGOTE_SOCKET);
692 } catch (IOException ioe) {
693 throw new ZygoteStartFailedEx("Error connecting to secondary zygote", ioe);
694 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100695 }
696
697 if (secondaryZygoteState.matches(abi)) {
698 return secondaryZygoteState;
699 }
700
701 throw new ZygoteStartFailedEx("Unsupported zygote ABI: " + abi);
702 }
703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 /**
705 * Returns elapsed milliseconds of the time this process has run.
706 * @return Returns the number of milliseconds this process has return.
707 */
708 public static final native long getElapsedCpuTime();
709
710 /**
711 * Returns the identifier of this process, which can be used with
712 * {@link #killProcess} and {@link #sendSignal}.
713 */
Jeff Hao406ec152013-07-30 10:13:41 -0700714 public static final int myPid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700715 return Os.getpid();
Jeff Hao406ec152013-07-30 10:13:41 -0700716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717
718 /**
Romain Guy80b12fc2013-05-29 15:54:25 -0700719 * Returns the identifier of this process' parent.
720 * @hide
721 */
Elliott Hughes2a805f92013-07-31 18:25:47 -0700722 public static final int myPpid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700723 return Os.getppid();
Elliott Hughes2a805f92013-07-31 18:25:47 -0700724 }
Romain Guy80b12fc2013-05-29 15:54:25 -0700725
726 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 * Returns the identifier of the calling thread, which be used with
728 * {@link #setThreadPriority(int, int)}.
729 */
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700730 public static final int myTid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700731 return Os.gettid();
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700732 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733
734 /**
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700735 * Returns the identifier of this process's uid. This is the kernel uid
736 * that the process is running under, which is the identity of its
737 * app-specific sandbox. It is different from {@link #myUserHandle} in that
738 * a uid identifies a specific app sandbox in a specific user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 */
Jeff Hao406ec152013-07-30 10:13:41 -0700740 public static final int myUid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700741 return Os.getuid();
Jeff Hao406ec152013-07-30 10:13:41 -0700742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743
744 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700745 * Returns this process's user handle. This is the
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700746 * user the process is running under. It is distinct from
747 * {@link #myUid()} in that a particular user will have multiple
748 * distinct apps running under it each with their own uid.
749 */
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700750 public static final UserHandle myUserHandle() {
751 return new UserHandle(UserHandle.getUserId(myUid()));
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700752 }
753
754 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800755 * Returns whether the current process is in an isolated sandbox.
756 * @hide
757 */
758 public static final boolean isIsolated() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700759 int uid = UserHandle.getAppId(myUid());
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800760 return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID;
761 }
762
763 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 * Returns the UID assigned to a particular user name, or -1 if there is
765 * none. If the given string consists of only numbers, it is converted
766 * directly to a uid.
767 */
768 public static final native int getUidForName(String name);
769
770 /**
771 * Returns the GID assigned to a particular user name, or -1 if there is
772 * none. If the given string consists of only numbers, it is converted
773 * directly to a gid.
774 */
775 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700776
777 /**
778 * Returns a uid for a currently running process.
779 * @param pid the process id
780 * @return the uid of the process, or -1 if the process is not running.
781 * @hide pending API council review
782 */
783 public static final int getUidForPid(int pid) {
784 String[] procStatusLabels = { "Uid:" };
785 long[] procStatusValues = new long[1];
786 procStatusValues[0] = -1;
787 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
788 return (int) procStatusValues[0];
789 }
790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700792 * Returns the parent process id for a currently running process.
793 * @param pid the process id
794 * @return the parent process id of the process, or -1 if the process is not running.
795 * @hide
796 */
797 public static final int getParentPid(int pid) {
798 String[] procStatusLabels = { "PPid:" };
799 long[] procStatusValues = new long[1];
800 procStatusValues[0] = -1;
801 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
802 return (int) procStatusValues[0];
803 }
804
805 /**
Glenn Kasten07b04652012-04-23 15:00:43 -0700806 * Returns the thread group leader id for a currently running thread.
807 * @param tid the thread id
808 * @return the thread group leader id of the thread, or -1 if the thread is not running.
809 * This is same as what getpid(2) would return if called by tid.
810 * @hide
811 */
812 public static final int getThreadGroupLeader(int tid) {
813 String[] procStatusLabels = { "Tgid:" };
814 long[] procStatusValues = new long[1];
815 procStatusValues[0] = -1;
816 Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues);
817 return (int) procStatusValues[0];
818 }
819
820 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 * Set the priority of a thread, based on Linux priorities.
822 *
823 * @param tid The identifier of the thread/process to change.
824 * @param priority A Linux priority level, from -20 for highest scheduling
825 * priority to 19 for lowest scheduling priority.
826 *
827 * @throws IllegalArgumentException Throws IllegalArgumentException if
828 * <var>tid</var> does not exist.
829 * @throws SecurityException Throws SecurityException if your process does
830 * not have permission to modify the given thread, or to use the given
831 * priority.
832 */
833 public static final native void setThreadPriority(int tid, int priority)
834 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700835
836 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700837 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
838 * throw an exception if passed a background-level thread priority. This is only
839 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
840 *
841 * @hide
842 */
843 public static final native void setCanSelfBackground(boolean backgroundOk);
844
845 /**
San Mehate9d376b2009-04-21 14:06:36 -0700846 * Sets the scheduling group for a thread.
847 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700848 * @param tid The identifier of the thread to change.
849 * @param group The target group for this thread from THREAD_GROUP_*.
San Mehate9d376b2009-04-21 14:06:36 -0700850 *
851 * @throws IllegalArgumentException Throws IllegalArgumentException if
852 * <var>tid</var> does not exist.
853 * @throws SecurityException Throws SecurityException if your process does
854 * not have permission to modify the given thread, or to use the given
855 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700856 * If the thread is a thread group leader, that is it's gettid() == getpid(),
857 * then the other threads in the same thread group are _not_ affected.
San Mehate9d376b2009-04-21 14:06:36 -0700858 */
859 public static final native void setThreadGroup(int tid, int group)
860 throws IllegalArgumentException, SecurityException;
Glenn Kastenf1b56442012-03-15 16:33:43 -0700861
San Mehat3e458242009-05-19 14:44:16 -0700862 /**
863 * Sets the scheduling group for a process and all child threads
864 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700865 * @param pid The identifier of the process to change.
866 * @param group The target group for this process from THREAD_GROUP_*.
San Mehat3e458242009-05-19 14:44:16 -0700867 *
868 * @throws IllegalArgumentException Throws IllegalArgumentException if
869 * <var>tid</var> does not exist.
870 * @throws SecurityException Throws SecurityException if your process does
871 * not have permission to modify the given thread, or to use the given
872 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700873 *
874 * group == THREAD_GROUP_DEFAULT means to move all non-background priority
875 * threads to the foreground scheduling group, but to leave background
876 * priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all
877 * threads, regardless of priority, to the background scheduling group.
878 * group == THREAD_GROUP_FOREGROUND is not allowed.
San Mehat3e458242009-05-19 14:44:16 -0700879 */
880 public static final native void setProcessGroup(int pid, int group)
881 throws IllegalArgumentException, SecurityException;
Jeff Sharkey9e57c412013-01-17 14:12:41 -0800882
883 /**
884 * Return the scheduling group of requested process.
885 *
886 * @hide
887 */
888 public static final native int getProcessGroup(int pid)
889 throws IllegalArgumentException, SecurityException;
890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 /**
892 * Set the priority of the calling thread, based on Linux priorities. See
893 * {@link #setThreadPriority(int, int)} for more information.
894 *
895 * @param priority A Linux priority level, from -20 for highest scheduling
896 * priority to 19 for lowest scheduling priority.
897 *
898 * @throws IllegalArgumentException Throws IllegalArgumentException if
899 * <var>tid</var> does not exist.
900 * @throws SecurityException Throws SecurityException if your process does
901 * not have permission to modify the given thread, or to use the given
902 * priority.
903 *
904 * @see #setThreadPriority(int, int)
905 */
906 public static final native void setThreadPriority(int priority)
907 throws IllegalArgumentException, SecurityException;
908
909 /**
910 * Return the current priority of a thread, based on Linux priorities.
911 *
912 * @param tid The identifier of the thread/process to change.
913 *
914 * @return Returns the current priority, as a Linux priority level,
915 * from -20 for highest scheduling priority to 19 for lowest scheduling
916 * priority.
917 *
918 * @throws IllegalArgumentException Throws IllegalArgumentException if
919 * <var>tid</var> does not exist.
920 */
921 public static final native int getThreadPriority(int tid)
922 throws IllegalArgumentException;
923
924 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700925 * Set the scheduling policy and priority of a thread, based on Linux.
926 *
927 * @param tid The identifier of the thread/process to change.
928 * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
929 * @param priority A Linux priority level in a range appropriate for the given policy.
930 *
931 * @throws IllegalArgumentException Throws IllegalArgumentException if
932 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
933 * @throws SecurityException Throws SecurityException if your process does
934 * not have permission to modify the given thread, or to use the given
935 * scheduling policy or priority.
936 *
937 * {@hide}
938 */
939 public static final native void setThreadScheduler(int tid, int policy, int priority)
940 throws IllegalArgumentException;
941
942 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 * Determine whether the current environment supports multiple processes.
944 *
945 * @return Returns true if the system can run in multiple processes, else
946 * false if everything is running in a single process.
Jeff Brown10e89712011-07-08 18:52:57 -0700947 *
948 * @deprecated This method always returns true. Do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 */
Jeff Brown10e89712011-07-08 18:52:57 -0700950 @Deprecated
951 public static final boolean supportsProcesses() {
952 return true;
953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954
955 /**
956 * Set the out-of-memory badness adjustment for a process.
957 *
958 * @param pid The process identifier to set.
959 * @param amt Adjustment value -- linux allows -16 to +15.
960 *
961 * @return Returns true if the underlying system supports this
962 * feature, else false.
963 *
964 * {@hide}
965 */
966 public static final native boolean setOomAdj(int pid, int amt);
967
968 /**
Rom Lemarchand5534ba92013-07-12 16:15:36 -0700969 * Adjust the swappiness level for a process.
970 *
971 * @param pid The process identifier to set.
972 * @param is_increased Whether swappiness should be increased or default.
973 *
974 * @return Returns true if the underlying system supports this
975 * feature, else false.
976 *
977 * {@hide}
978 */
979 public static final native boolean setSwappiness(int pid, boolean is_increased);
980
981 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 * Change this process's argv[0] parameter. This can be useful to show
983 * more descriptive information in things like the 'ps' command.
984 *
985 * @param text The new name of this process.
986 *
987 * {@hide}
988 */
989 public static final native void setArgV0(String text);
990
991 /**
992 * Kill the process with the given PID.
993 * Note that, though this API allows us to request to
994 * kill any process based on its PID, the kernel will
995 * still impose standard restrictions on which PIDs you
996 * are actually able to kill. Typically this means only
997 * the process running the caller's packages/application
998 * and any additional processes created by that app; packages
999 * sharing a common UID will also be able to kill each
1000 * other's processes.
1001 */
1002 public static final void killProcess(int pid) {
1003 sendSignal(pid, SIGNAL_KILL);
1004 }
1005
1006 /** @hide */
1007 public static final native int setUid(int uid);
1008
1009 /** @hide */
1010 public static final native int setGid(int uid);
1011
1012 /**
1013 * Send a signal to the given process.
1014 *
1015 * @param pid The pid of the target process.
1016 * @param signal The signal to send.
1017 */
1018 public static final native void sendSignal(int pid, int signal);
1019
Dianne Hackborn906497c2010-05-10 15:57:38 -07001020 /**
1021 * @hide
1022 * Private impl for avoiding a log message... DO NOT USE without doing
1023 * your own log, or the Android Illuminati will find you some night and
1024 * beat you up.
1025 */
1026 public static final void killProcessQuiet(int pid) {
1027 sendSignalQuiet(pid, SIGNAL_KILL);
1028 }
1029
1030 /**
1031 * @hide
1032 * Private impl for avoiding a log message... DO NOT USE without doing
1033 * your own log, or the Android Illuminati will find you some night and
1034 * beat you up.
1035 */
1036 public static final native void sendSignalQuiet(int pid, int signal);
1037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 /** @hide */
Marco Nelissen0bca96b2009-07-17 12:59:25 -07001039 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040
1041 /** @hide */
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001042 public static final native long getTotalMemory();
1043
1044 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 public static final native void readProcLines(String path,
1046 String[] reqFields, long[] outSizes);
1047
1048 /** @hide */
1049 public static final native int[] getPids(String path, int[] lastArray);
1050
1051 /** @hide */
1052 public static final int PROC_TERM_MASK = 0xff;
1053 /** @hide */
1054 public static final int PROC_ZERO_TERM = 0;
1055 /** @hide */
1056 public static final int PROC_SPACE_TERM = (int)' ';
1057 /** @hide */
Evan Millarc64edde2009-04-18 12:26:32 -07001058 public static final int PROC_TAB_TERM = (int)'\t';
1059 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 public static final int PROC_COMBINE = 0x100;
1061 /** @hide */
1062 public static final int PROC_PARENS = 0x200;
1063 /** @hide */
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001064 public static final int PROC_QUOTES = 0x400;
1065 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 public static final int PROC_OUT_STRING = 0x1000;
1067 /** @hide */
1068 public static final int PROC_OUT_LONG = 0x2000;
1069 /** @hide */
1070 public static final int PROC_OUT_FLOAT = 0x4000;
1071
1072 /** @hide */
1073 public static final native boolean readProcFile(String file, int[] format,
1074 String[] outStrings, long[] outLongs, float[] outFloats);
Evan Millarc64edde2009-04-18 12:26:32 -07001075
1076 /** @hide */
1077 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
1078 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001080 /** @hide */
1081 public static final native int[] getPidsForCommands(String[] cmds);
1082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 /**
1084 * Gets the total Pss value for a given process, in bytes.
1085 *
1086 * @param pid the process to the Pss for
1087 * @return the total Pss value for the given process in bytes,
1088 * or -1 if the value cannot be determined
1089 * @hide
1090 */
1091 public static final native long getPss(int pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -07001092
1093 /**
1094 * Specifies the outcome of having started a process.
1095 * @hide
1096 */
1097 public static final class ProcessStartResult {
1098 /**
1099 * The PID of the newly started process.
1100 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
1101 */
1102 public int pid;
1103
1104 /**
1105 * True if the process was started with a wrapper attached.
1106 */
1107 public boolean usingWrapper;
1108 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109}