blob: 355ec8c94c685b1fe0b0c16b26cc8a0d5389ea57 [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 /**
Jeff Sharkey1cb2d0d2014-07-30 16:45:01 -070064 * Defines the root UID.
65 * @hide
66 */
67 public static final int ROOT_UID = 0;
68
69 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 * Defines the UID/GID under which system code runs.
71 */
72 public static final int SYSTEM_UID = 1000;
73
74 /**
75 * Defines the UID/GID under which the telephony code runs.
76 */
77 public static final int PHONE_UID = 1001;
78
79 /**
Dianne Hackborn854060af2009-07-09 18:14:31 -070080 * Defines the UID/GID for the user shell.
81 * @hide
82 */
83 public static final int SHELL_UID = 2000;
84
85 /**
Mike Lockwoodd42685d2009-09-03 09:25:22 -040086 * Defines the UID/GID for the log group.
87 * @hide
88 */
89 public static final int LOG_UID = 1007;
90
91 /**
Amith Yamasanid1582142009-07-08 20:04:55 -070092 * Defines the UID/GID for the WIFI supplicant process.
93 * @hide
94 */
95 public static final int WIFI_UID = 1010;
96
97 /**
Glenn Kasten8b7d1b42011-07-13 16:23:22 -070098 * Defines the UID/GID for the mediaserver process.
99 * @hide
100 */
101 public static final int MEDIA_UID = 1013;
102
103 /**
Jeff Sharkey5294a2f2012-04-24 17:07:22 -0700104 * Defines the UID/GID for the DRM process.
105 * @hide
106 */
107 public static final int DRM_UID = 1019;
108
109 /**
Kenny Root26993b32012-03-19 15:07:51 -0700110 * Defines the UID/GID for the group that controls VPN services.
111 * @hide
112 */
113 public static final int VPN_UID = 1016;
114
115 /**
Nick Pellycd0e8392010-10-13 17:25:24 -0700116 * Defines the UID/GID for the NFC service process.
117 * @hide
118 */
Nick Pellya5cb9f42011-11-21 14:54:46 -0800119 public static final int NFC_UID = 1027;
Nick Pellycd0e8392010-10-13 17:25:24 -0700120
121 /**
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800122 * Defines the UID/GID for the Bluetooth service process.
123 * @hide
124 */
125 public static final int BLUETOOTH_UID = 1002;
126
127 /**
Mike Lockwooddcaa10c2010-12-16 12:50:44 -0800128 * Defines the GID for the group that allows write access to the internal media storage.
129 * @hide
130 */
131 public static final int MEDIA_RW_GID = 1023;
132
133 /**
Jeff Sharkey184a0102013-07-10 16:19:52 -0700134 * Access to installed package details
135 * @hide
136 */
137 public static final int PACKAGE_INFO_GID = 1032;
138
139 /**
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100140 * Defines the UID/GID for the shared RELRO file updater process.
141 * @hide
142 */
143 public static final int SHARED_RELRO_UID = 1037;
144
145 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 * Defines the start of a range of UIDs (and GIDs), going from this
147 * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning
148 * to applications.
149 */
150 public static final int FIRST_APPLICATION_UID = 10000;
Jeff Sharkey184a0102013-07-10 16:19:52 -0700151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 /**
153 * Last of application-specific UIDs starting at
154 * {@link #FIRST_APPLICATION_UID}.
155 */
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -0800156 public static final int LAST_APPLICATION_UID = 19999;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800157
158 /**
159 * First uid used for fully isolated sandboxed processes (with no permissions of their own)
160 * @hide
161 */
162 public static final int FIRST_ISOLATED_UID = 99000;
163
164 /**
165 * Last uid used for fully isolated sandboxed processes (with no permissions of their own)
166 * @hide
167 */
168 public static final int LAST_ISOLATED_UID = 99999;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169
170 /**
Robin Leee66b6892014-04-29 12:36:47 +0100171 * Defines the gid shared by all applications running under the same profile.
172 * @hide
173 */
174 public static final int SHARED_USER_GID = 9997;
175
176 /**
Kenny Roote091f222012-09-11 15:01:26 -0700177 * First gid for applications to share resources. Used when forward-locking
178 * is enabled but all UserHandles need to be able to read the resources.
179 * @hide
180 */
181 public static final int FIRST_SHARED_APPLICATION_GID = 50000;
182
183 /**
184 * Last gid for applications to share resources. Used when forward-locking
185 * is enabled but all UserHandles need to be able to read the resources.
186 * @hide
187 */
188 public static final int LAST_SHARED_APPLICATION_GID = 59999;
189
190 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 * Standard priority of application threads.
192 * Use with {@link #setThreadPriority(int)} and
193 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
194 * {@link java.lang.Thread} class.
195 */
196 public static final int THREAD_PRIORITY_DEFAULT = 0;
197
198 /*
199 * ***************************************
200 * ** Keep in sync with utils/threads.h **
201 * ***************************************
202 */
203
204 /**
205 * Lowest available thread priority. Only for those who really, really
206 * don't want to run if anything else is happening.
207 * Use with {@link #setThreadPriority(int)} and
208 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
209 * {@link java.lang.Thread} class.
210 */
211 public static final int THREAD_PRIORITY_LOWEST = 19;
212
213 /**
214 * Standard priority background threads. This gives your thread a slightly
215 * lower than normal priority, so that it will have less chance of impacting
216 * the responsiveness of the user interface.
217 * Use with {@link #setThreadPriority(int)} and
218 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
219 * {@link java.lang.Thread} class.
220 */
221 public static final int THREAD_PRIORITY_BACKGROUND = 10;
222
223 /**
224 * Standard priority of threads that are currently running a user interface
225 * that the user is interacting with. Applications can not normally
226 * change to this priority; the system will automatically adjust your
227 * application threads as the user moves through the UI.
228 * Use with {@link #setThreadPriority(int)} and
229 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
230 * {@link java.lang.Thread} class.
231 */
232 public static final int THREAD_PRIORITY_FOREGROUND = -2;
233
234 /**
235 * Standard priority of system display threads, involved in updating
236 * the user interface. Applications can not
237 * 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_DISPLAY = -4;
243
244 /**
245 * Standard priority of the most important display threads, for compositing
246 * the screen and retrieving input events. Applications can not normally
247 * change to this priority.
248 * Use with {@link #setThreadPriority(int)} and
249 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
250 * {@link java.lang.Thread} class.
251 */
252 public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8;
253
254 /**
255 * Standard priority of audio threads. Applications can not normally
256 * change to this priority.
257 * Use with {@link #setThreadPriority(int)} and
258 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
259 * {@link java.lang.Thread} class.
260 */
261 public static final int THREAD_PRIORITY_AUDIO = -16;
262
263 /**
264 * Standard priority of the most important audio threads.
265 * Applications can not normally change to this priority.
266 * Use with {@link #setThreadPriority(int)} and
267 * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal
268 * {@link java.lang.Thread} class.
269 */
270 public static final int THREAD_PRIORITY_URGENT_AUDIO = -19;
271
272 /**
273 * Minimum increment to make a priority more favorable.
274 */
275 public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
276
277 /**
278 * Minimum increment to make a priority less favorable.
279 */
280 public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;
281
San Mehate9d376b2009-04-21 14:06:36 -0700282 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700283 * Default scheduling policy
284 * @hide
285 */
286 public static final int SCHED_OTHER = 0;
287
288 /**
289 * First-In First-Out scheduling policy
290 * @hide
291 */
292 public static final int SCHED_FIFO = 1;
293
294 /**
295 * Round-Robin scheduling policy
296 * @hide
297 */
298 public static final int SCHED_RR = 2;
299
300 /**
301 * Batch scheduling policy
302 * @hide
303 */
304 public static final int SCHED_BATCH = 3;
305
306 /**
307 * Idle scheduling policy
308 * @hide
309 */
310 public static final int SCHED_IDLE = 5;
311
Glenn Kastenf1b56442012-03-15 16:33:43 -0700312 // Keep in sync with SP_* constants of enum type SchedPolicy
313 // declared in system/core/include/cutils/sched_policy.h,
314 // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
San Mehate9d376b2009-04-21 14:06:36 -0700315
316 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700317 * Default thread group -
318 * has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
319 * When used with setProcessGroup(), the group of each thread in the process
320 * is conditionally changed based on that thread's current priority, as follows:
321 * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
322 * are moved to foreground thread group. All other threads are left unchanged.
323 * @hide
324 */
325 public static final int THREAD_GROUP_DEFAULT = -1;
326
327 /**
328 * Background thread group - All threads in
San Mehate9d376b2009-04-21 14:06:36 -0700329 * this group are scheduled with a reduced share of the CPU.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700330 * Value is same as constant SP_BACKGROUND of enum SchedPolicy.
331 * FIXME rename to THREAD_GROUP_BACKGROUND.
San Mehate9d376b2009-04-21 14:06:36 -0700332 * @hide
333 */
Glenn Kastenf1b56442012-03-15 16:33:43 -0700334 public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0;
San Mehate9d376b2009-04-21 14:06:36 -0700335
336 /**
Glenn Kastenf1b56442012-03-15 16:33:43 -0700337 * Foreground thread group - All threads in
338 * this group are scheduled with a normal share of the CPU.
339 * Value is same as constant SP_FOREGROUND of enum SchedPolicy.
340 * Not used at this level.
San Mehate9d376b2009-04-21 14:06:36 -0700341 * @hide
342 **/
Glenn Kastenf1b56442012-03-15 16:33:43 -0700343 private static final int THREAD_GROUP_FOREGROUND = 1;
San Mehate9d376b2009-04-21 14:06:36 -0700344
Glenn Kasten07b04652012-04-23 15:00:43 -0700345 /**
346 * System thread group.
347 * @hide
348 **/
349 public static final int THREAD_GROUP_SYSTEM = 2;
350
351 /**
352 * Application audio thread group.
353 * @hide
354 **/
355 public static final int THREAD_GROUP_AUDIO_APP = 3;
356
357 /**
358 * System audio thread group.
359 * @hide
360 **/
361 public static final int THREAD_GROUP_AUDIO_SYS = 4;
362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 public static final int SIGNAL_QUIT = 3;
364 public static final int SIGNAL_KILL = 9;
365 public static final int SIGNAL_USR1 = 10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100367 /**
368 * State for communicating with the zygote process.
Narayan Kamath64cd9072014-05-13 13:35:14 +0100369 *
370 * @hide for internal use only.
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100371 */
Narayan Kamath64cd9072014-05-13 13:35:14 +0100372 public static class ZygoteState {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100373 final LocalSocket socket;
374 final DataInputStream inputStream;
375 final BufferedWriter writer;
376 final List<String> abiList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100378 boolean mClosed;
379
380 private ZygoteState(LocalSocket socket, DataInputStream inputStream,
381 BufferedWriter writer, List<String> abiList) {
382 this.socket = socket;
383 this.inputStream = inputStream;
384 this.writer = writer;
385 this.abiList = abiList;
386 }
387
Narayan Kamath64cd9072014-05-13 13:35:14 +0100388 public static ZygoteState connect(String socketAddress) throws IOException {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100389 DataInputStream zygoteInputStream = null;
390 BufferedWriter zygoteWriter = null;
Narayan Kamath64cd9072014-05-13 13:35:14 +0100391 final LocalSocket zygoteSocket = new LocalSocket();
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100392
Narayan Kamath64cd9072014-05-13 13:35:14 +0100393 try {
394 zygoteSocket.connect(new LocalSocketAddress(socketAddress,
395 LocalSocketAddress.Namespace.RESERVED));
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100396
Narayan Kamath64cd9072014-05-13 13:35:14 +0100397 zygoteInputStream = new DataInputStream(zygoteSocket.getInputStream());
398
399 zygoteWriter = new BufferedWriter(new OutputStreamWriter(
400 zygoteSocket.getOutputStream()), 256);
401 } catch (IOException ex) {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100402 try {
Narayan Kamath64cd9072014-05-13 13:35:14 +0100403 zygoteSocket.close();
404 } catch (IOException ignore) {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100405 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100406
Narayan Kamath64cd9072014-05-13 13:35:14 +0100407 throw ex;
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100408 }
409
410 String abiListString = getAbiList(zygoteWriter, zygoteInputStream);
411 Log.i("Zygote", "Process: zygote socket opened, supported ABIS: " + abiListString);
412
413 return new ZygoteState(zygoteSocket, zygoteInputStream, zygoteWriter,
414 Arrays.asList(abiListString.split(",")));
415 }
416
417 boolean matches(String abi) {
418 return abiList.contains(abi);
419 }
420
Narayan Kamath64cd9072014-05-13 13:35:14 +0100421 public void close() {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100422 try {
423 socket.close();
424 } catch (IOException ex) {
425 Log.e(LOG_TAG,"I/O exception on routine close", ex);
426 }
427
428 mClosed = true;
429 }
430
431 boolean isClosed() {
432 return mClosed;
433 }
434 }
435
436 /**
437 * The state of the connection to the primary zygote.
438 */
439 static ZygoteState primaryZygoteState;
440
441 /**
442 * The state of the connection to the secondary zygote.
443 */
444 static ZygoteState secondaryZygoteState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445
446 /**
447 * Start a new process.
448 *
449 * <p>If processes are enabled, a new process is created and the
450 * static main() function of a <var>processClass</var> is executed there.
451 * The process will continue running after this function returns.
452 *
453 * <p>If processes are not enabled, a new thread in the caller's
454 * process is created and main() of <var>processClass</var> called there.
455 *
456 * <p>The niceName parameter, if not an empty string, is a custom name to
457 * give to the process instead of using processClass. This allows you to
458 * make easily identifyable processes even if you are using the same base
459 * <var>processClass</var> to start them.
460 *
461 * @param processClass The class to use as the process's main entry
462 * point.
463 * @param niceName A more readable name to use for the process.
464 * @param uid The user-id under which the process will run.
465 * @param gid The group-id under which the process will run.
466 * @param gids Additional group-ids associated with the process.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700467 * @param debugFlags Additional flags.
468 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalleybd19b9e2013-04-12 14:49:38 -0400469 * @param seInfo null-ok SELinux information for the new process.
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100470 * @param abi non-null the ABI this app should be started with.
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700471 * @param instructionSet null-ok the instruction set to use.
jgu212eacd062014-09-10 06:55:07 -0400472 * @param appDataDir null-ok the data directory of the app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 * @param zygoteArgs Additional arguments to supply to the zygote process.
474 *
Jeff Brown3f9dd282011-07-08 20:02:19 -0700475 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 * @throws RuntimeException on fatal start failure
477 *
478 * {@hide}
479 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700480 public static final ProcessStartResult start(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 final String niceName,
482 int uid, int gid, int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700483 int debugFlags, int mountExternal,
484 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500485 String seInfo,
Ramin Zaghiff0c4702014-04-01 15:02:29 +0100486 String abi,
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700487 String instructionSet,
jgu212eacd062014-09-10 06:55:07 -0400488 String appDataDir,
Jeff Brown10e89712011-07-08 18:52:57 -0700489 String[] zygoteArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 try {
Jeff Brown10e89712011-07-08 18:52:57 -0700491 return startViaZygote(processClass, niceName, uid, gid, gids,
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100492 debugFlags, mountExternal, targetSdkVersion, seInfo,
jgu212eacd062014-09-10 06:55:07 -0400493 abi, instructionSet, appDataDir, zygoteArgs);
Jeff Brown10e89712011-07-08 18:52:57 -0700494 } catch (ZygoteStartFailedEx ex) {
495 Log.e(LOG_TAG,
496 "Starting VM process through Zygote failed");
497 throw new RuntimeException(
498 "Starting VM process through Zygote failed", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 }
501
502 /** retry interval for opening a zygote socket */
503 static final int ZYGOTE_RETRY_MILLIS = 500;
504
505 /**
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100506 * Queries the zygote for the list of ABIS it supports.
507 *
508 * @throws ZygoteStartFailedEx if the query failed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 */
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100510 private static String getAbiList(BufferedWriter writer, DataInputStream inputStream)
Narayan Kamath64cd9072014-05-13 13:35:14 +0100511 throws IOException {
512 // Each query starts with the argument count (1 in this case)
513 writer.write("1");
514 // ... followed by a new-line.
515 writer.newLine();
516 // ... followed by our only argument.
517 writer.write("--query-abi-list");
518 writer.newLine();
519 writer.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520
Narayan Kamath64cd9072014-05-13 13:35:14 +0100521 // The response is a length prefixed stream of ASCII bytes.
522 int numBytes = inputStream.readInt();
523 byte[] bytes = new byte[numBytes];
524 inputStream.readFully(bytes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525
Narayan Kamath64cd9072014-05-13 13:35:14 +0100526 return new String(bytes, StandardCharsets.US_ASCII);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 }
528
529 /**
530 * Sends an argument list to the zygote process, which starts a new child
531 * and returns the child's pid. Please note: the present implementation
532 * replaces newlines in the argument list with spaces.
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100533 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 * @throws ZygoteStartFailedEx if process start failed for any reason
535 */
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100536 private static ProcessStartResult zygoteSendArgsAndGetResult(
537 ZygoteState zygoteState, ArrayList<String> args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 try {
540 /**
541 * See com.android.internal.os.ZygoteInit.readArgumentList()
542 * Presently the wire format to the zygote process is:
543 * a) a count of arguments (argc, in essence)
544 * b) a number of newline-separated argument strings equal to count
545 *
546 * After the zygote process reads these it will write the pid of
Jeff Brown3f9dd282011-07-08 20:02:19 -0700547 * the child or -1 on failure, followed by boolean to
548 * indicate whether a wrapper process was used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 */
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100550 final BufferedWriter writer = zygoteState.writer;
551 final DataInputStream inputStream = zygoteState.inputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100553 writer.write(Integer.toString(args.size()));
554 writer.newLine();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555
556 int sz = args.size();
557 for (int i = 0; i < sz; i++) {
558 String arg = args.get(i);
559 if (arg.indexOf('\n') >= 0) {
560 throw new ZygoteStartFailedEx(
561 "embedded newlines not allowed");
562 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100563 writer.write(arg);
564 writer.newLine();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
566
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100567 writer.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568
569 // Should there be a timeout on this?
Jeff Brown3f9dd282011-07-08 20:02:19 -0700570 ProcessStartResult result = new ProcessStartResult();
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100571 result.pid = inputStream.readInt();
Jeff Brown3f9dd282011-07-08 20:02:19 -0700572 if (result.pid < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 throw new ZygoteStartFailedEx("fork() failed");
574 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100575 result.usingWrapper = inputStream.readBoolean();
Jeff Brown3f9dd282011-07-08 20:02:19 -0700576 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 } catch (IOException ex) {
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100578 zygoteState.close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 throw new ZygoteStartFailedEx(ex);
580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
582
583 /**
584 * Starts a new process via the zygote mechanism.
585 *
586 * @param processClass Class name whose static main() to run
587 * @param niceName 'nice' process name to appear in ps
588 * @param uid a POSIX uid that the new process should setuid() to
589 * @param gid a POSIX gid that the new process shuold setgid() to
590 * @param gids null-ok; a list of supplementary group IDs that the
591 * new process should setgroup() to.
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700592 * @param debugFlags Additional flags.
593 * @param targetSdkVersion The target SDK version for the app.
Stephen Smalleybd19b9e2013-04-12 14:49:38 -0400594 * @param seInfo null-ok SELinux information for the new process.
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100595 * @param abi the ABI the process should use.
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700596 * @param instructionSet null-ok the instruction set to use.
jgu212eacd062014-09-10 06:55:07 -0400597 * @param appDataDir null-ok the data directory of the app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 * @param extraArgs Additional arguments to supply to the zygote process.
Jeff Brown3f9dd282011-07-08 20:02:19 -0700599 * @return An object that describes the result of the attempt to start the process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 * @throws ZygoteStartFailedEx if process start failed for any reason
601 */
Jeff Brown3f9dd282011-07-08 20:02:19 -0700602 private static ProcessStartResult startViaZygote(final String processClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 final String niceName,
604 final int uid, final int gid,
605 final int[] gids,
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700606 int debugFlags, int mountExternal,
607 int targetSdkVersion,
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500608 String seInfo,
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100609 String abi,
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700610 String instructionSet,
jgu212eacd062014-09-10 06:55:07 -0400611 String appDataDir,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 String[] extraArgs)
613 throws ZygoteStartFailedEx {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 synchronized(Process.class) {
615 ArrayList<String> argsForZygote = new ArrayList<String>();
616
Narayan Kamathf48029f2015-01-08 12:45:37 +0000617 // --runtime-args, --setuid=, --setgid=,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 // and --setgroups= must go first
Narayan Kamathf48029f2015-01-08 12:45:37 +0000619 argsForZygote.add("--runtime-args");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 argsForZygote.add("--setuid=" + uid);
621 argsForZygote.add("--setgid=" + gid);
Elliott Hughesae07ecf2011-07-06 17:33:27 -0700622 if ((debugFlags & Zygote.DEBUG_ENABLE_JNI_LOGGING) != 0) {
623 argsForZygote.add("--enable-jni-logging");
624 }
Ben Cheng23085b72010-02-08 16:06:32 -0800625 if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) {
626 argsForZygote.add("--enable-safemode");
627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) {
629 argsForZygote.add("--enable-debugger");
630 }
631 if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) {
632 argsForZygote.add("--enable-checkjni");
633 }
Mathieu Chartier7a490282015-03-17 09:51:36 -0700634 if ((debugFlags & Zygote.DEBUG_ENABLE_JIT) != 0) {
635 argsForZygote.add("--enable-jit");
636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) {
638 argsForZygote.add("--enable-assert");
639 }
Jeff Sharkey48877892015-03-18 11:27:19 -0700640 if (mountExternal == Zygote.MOUNT_EXTERNAL_DEFAULT) {
641 argsForZygote.add("--mount-external-default");
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700642 }
Elliott Hughese1dfcb72011-07-08 11:08:07 -0700643 argsForZygote.add("--target-sdk-version=" + targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644
645 //TODO optionally enable debuger
646 //argsForZygote.add("--enable-debugger");
647
648 // --setgroups is a comma-separated list
649 if (gids != null && gids.length > 0) {
650 StringBuilder sb = new StringBuilder();
651 sb.append("--setgroups=");
652
653 int sz = gids.length;
654 for (int i = 0; i < sz; i++) {
655 if (i != 0) {
656 sb.append(',');
657 }
658 sb.append(gids[i]);
659 }
660
661 argsForZygote.add(sb.toString());
662 }
663
664 if (niceName != null) {
665 argsForZygote.add("--nice-name=" + niceName);
666 }
667
Stephen Smalley83d9eda2012-01-13 08:34:17 -0500668 if (seInfo != null) {
669 argsForZygote.add("--seinfo=" + seInfo);
670 }
671
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700672 if (instructionSet != null) {
673 argsForZygote.add("--instruction-set=" + instructionSet);
674 }
675
jgu212eacd062014-09-10 06:55:07 -0400676 if (appDataDir != null) {
677 argsForZygote.add("--app-data-dir=" + appDataDir);
678 }
679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 argsForZygote.add(processClass);
681
682 if (extraArgs != null) {
683 for (String arg : extraArgs) {
684 argsForZygote.add(arg);
685 }
686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100688 return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi), argsForZygote);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100691
692 /**
Narayan Kamath343f4782014-11-10 14:23:41 +0000693 * Tries to establish a connection to the zygote that handles a given {@code abi}. Might block and retry if the
694 * zygote is unresponsive. This method is a no-op if a connection is already open.
695 *
696 * @hide
697 */
698 public static void establishZygoteConnectionForAbi(String abi) {
699 try {
700 openZygoteSocketIfNeeded(abi);
701 } catch (ZygoteStartFailedEx ex) {
702 throw new RuntimeException("Unable to connect to zygote for abi: " + abi, ex);
703 }
704 }
705
706 /**
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100707 * Tries to open socket to Zygote process if not already open. If
708 * already open, does nothing. May block and retry.
709 */
710 private static ZygoteState openZygoteSocketIfNeeded(String abi) throws ZygoteStartFailedEx {
711 if (primaryZygoteState == null || primaryZygoteState.isClosed()) {
Narayan Kamath64cd9072014-05-13 13:35:14 +0100712 try {
713 primaryZygoteState = ZygoteState.connect(ZYGOTE_SOCKET);
714 } catch (IOException ioe) {
715 throw new ZygoteStartFailedEx("Error connecting to primary zygote", ioe);
716 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100717 }
718
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100719 if (primaryZygoteState.matches(abi)) {
720 return primaryZygoteState;
721 }
722
723 // The primary zygote didn't match. Try the secondary.
724 if (secondaryZygoteState == null || secondaryZygoteState.isClosed()) {
Narayan Kamath64cd9072014-05-13 13:35:14 +0100725 try {
726 secondaryZygoteState = ZygoteState.connect(SECONDARY_ZYGOTE_SOCKET);
727 } catch (IOException ioe) {
728 throw new ZygoteStartFailedEx("Error connecting to secondary zygote", ioe);
729 }
Narayan Kamath4444dcd2014-04-03 20:15:15 +0100730 }
731
732 if (secondaryZygoteState.matches(abi)) {
733 return secondaryZygoteState;
734 }
735
736 throw new ZygoteStartFailedEx("Unsupported zygote ABI: " + abi);
737 }
738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 /**
740 * Returns elapsed milliseconds of the time this process has run.
741 * @return Returns the number of milliseconds this process has return.
742 */
743 public static final native long getElapsedCpuTime();
744
745 /**
746 * Returns the identifier of this process, which can be used with
747 * {@link #killProcess} and {@link #sendSignal}.
748 */
Jeff Hao406ec152013-07-30 10:13:41 -0700749 public static final int myPid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700750 return Os.getpid();
Jeff Hao406ec152013-07-30 10:13:41 -0700751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752
753 /**
Romain Guy80b12fc2013-05-29 15:54:25 -0700754 * Returns the identifier of this process' parent.
755 * @hide
756 */
Elliott Hughes2a805f92013-07-31 18:25:47 -0700757 public static final int myPpid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700758 return Os.getppid();
Elliott Hughes2a805f92013-07-31 18:25:47 -0700759 }
Romain Guy80b12fc2013-05-29 15:54:25 -0700760
761 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 * Returns the identifier of the calling thread, which be used with
763 * {@link #setThreadPriority(int, int)}.
764 */
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700765 public static final int myTid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700766 return Os.gettid();
Elliott Hughes6d4b1e22013-07-31 17:38:11 -0700767 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768
769 /**
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700770 * Returns the identifier of this process's uid. This is the kernel uid
771 * that the process is running under, which is the identity of its
772 * app-specific sandbox. It is different from {@link #myUserHandle} in that
773 * a uid identifies a specific app sandbox in a specific user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 */
Jeff Hao406ec152013-07-30 10:13:41 -0700775 public static final int myUid() {
Elliott Hughes34385d32014-04-28 11:11:32 -0700776 return Os.getuid();
Jeff Hao406ec152013-07-30 10:13:41 -0700777 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778
779 /**
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700780 * Returns this process's user handle. This is the
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700781 * user the process is running under. It is distinct from
782 * {@link #myUid()} in that a particular user will have multiple
783 * distinct apps running under it each with their own uid.
784 */
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700785 public static final UserHandle myUserHandle() {
786 return new UserHandle(UserHandle.getUserId(myUid()));
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700787 }
788
789 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800790 * Returns whether the current process is in an isolated sandbox.
791 * @hide
792 */
793 public static final boolean isIsolated() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700794 int uid = UserHandle.getAppId(myUid());
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800795 return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID;
796 }
797
798 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 * Returns the UID assigned to a particular user name, or -1 if there is
800 * none. If the given string consists of only numbers, it is converted
801 * directly to a uid.
802 */
803 public static final native int getUidForName(String name);
804
805 /**
806 * Returns the GID assigned to a particular user name, or -1 if there is
807 * none. If the given string consists of only numbers, it is converted
808 * directly to a gid.
809 */
810 public static final native int getGidForName(String name);
Amith Yamasani819f9282009-06-24 23:18:15 -0700811
812 /**
813 * Returns a uid for a currently running process.
814 * @param pid the process id
815 * @return the uid of the process, or -1 if the process is not running.
816 * @hide pending API council review
817 */
818 public static final int getUidForPid(int pid) {
819 String[] procStatusLabels = { "Uid:" };
820 long[] procStatusValues = new long[1];
821 procStatusValues[0] = -1;
822 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
823 return (int) procStatusValues[0];
824 }
825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 /**
Jeff Brownebed7d62011-05-16 17:08:42 -0700827 * Returns the parent process id for a currently running process.
828 * @param pid the process id
829 * @return the parent process id of the process, or -1 if the process is not running.
830 * @hide
831 */
832 public static final int getParentPid(int pid) {
833 String[] procStatusLabels = { "PPid:" };
834 long[] procStatusValues = new long[1];
835 procStatusValues[0] = -1;
836 Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues);
837 return (int) procStatusValues[0];
838 }
839
840 /**
Glenn Kasten07b04652012-04-23 15:00:43 -0700841 * Returns the thread group leader id for a currently running thread.
842 * @param tid the thread id
843 * @return the thread group leader id of the thread, or -1 if the thread is not running.
844 * This is same as what getpid(2) would return if called by tid.
845 * @hide
846 */
847 public static final int getThreadGroupLeader(int tid) {
848 String[] procStatusLabels = { "Tgid:" };
849 long[] procStatusValues = new long[1];
850 procStatusValues[0] = -1;
851 Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues);
852 return (int) procStatusValues[0];
853 }
854
855 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 * Set the priority of a thread, based on Linux priorities.
857 *
858 * @param tid The identifier of the thread/process to change.
859 * @param priority A Linux priority level, from -20 for highest scheduling
860 * priority to 19 for lowest scheduling priority.
861 *
862 * @throws IllegalArgumentException Throws IllegalArgumentException if
863 * <var>tid</var> does not exist.
864 * @throws SecurityException Throws SecurityException if your process does
865 * not have permission to modify the given thread, or to use the given
866 * priority.
867 */
868 public static final native void setThreadPriority(int tid, int priority)
869 throws IllegalArgumentException, SecurityException;
San Mehate9d376b2009-04-21 14:06:36 -0700870
871 /**
Christopher Tate160edb32010-06-30 17:46:30 -0700872 * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
873 * throw an exception if passed a background-level thread priority. This is only
874 * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
875 *
876 * @hide
877 */
878 public static final native void setCanSelfBackground(boolean backgroundOk);
879
880 /**
San Mehate9d376b2009-04-21 14:06:36 -0700881 * Sets the scheduling group for a thread.
882 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700883 * @param tid The identifier of the thread to change.
884 * @param group The target group for this thread from THREAD_GROUP_*.
San Mehate9d376b2009-04-21 14:06:36 -0700885 *
886 * @throws IllegalArgumentException Throws IllegalArgumentException if
887 * <var>tid</var> does not exist.
888 * @throws SecurityException Throws SecurityException if your process does
889 * not have permission to modify the given thread, or to use the given
890 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700891 * If the thread is a thread group leader, that is it's gettid() == getpid(),
892 * then the other threads in the same thread group are _not_ affected.
San Mehate9d376b2009-04-21 14:06:36 -0700893 */
894 public static final native void setThreadGroup(int tid, int group)
895 throws IllegalArgumentException, SecurityException;
Glenn Kastenf1b56442012-03-15 16:33:43 -0700896
San Mehat3e458242009-05-19 14:44:16 -0700897 /**
898 * Sets the scheduling group for a process and all child threads
899 * @hide
Glenn Kastenf1b56442012-03-15 16:33:43 -0700900 * @param pid The identifier of the process to change.
901 * @param group The target group for this process from THREAD_GROUP_*.
San Mehat3e458242009-05-19 14:44:16 -0700902 *
903 * @throws IllegalArgumentException Throws IllegalArgumentException if
904 * <var>tid</var> does not exist.
905 * @throws SecurityException Throws SecurityException if your process does
906 * not have permission to modify the given thread, or to use the given
907 * priority.
Glenn Kastenf1b56442012-03-15 16:33:43 -0700908 *
909 * group == THREAD_GROUP_DEFAULT means to move all non-background priority
910 * threads to the foreground scheduling group, but to leave background
911 * priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all
912 * threads, regardless of priority, to the background scheduling group.
913 * group == THREAD_GROUP_FOREGROUND is not allowed.
San Mehat3e458242009-05-19 14:44:16 -0700914 */
915 public static final native void setProcessGroup(int pid, int group)
916 throws IllegalArgumentException, SecurityException;
Jeff Sharkey9e57c412013-01-17 14:12:41 -0800917
918 /**
919 * Return the scheduling group of requested process.
920 *
921 * @hide
922 */
923 public static final native int getProcessGroup(int pid)
924 throws IllegalArgumentException, SecurityException;
925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 /**
927 * Set the priority of the calling thread, based on Linux priorities. See
928 * {@link #setThreadPriority(int, int)} for more information.
929 *
930 * @param priority A Linux priority level, from -20 for highest scheduling
931 * priority to 19 for lowest scheduling priority.
932 *
933 * @throws IllegalArgumentException Throws IllegalArgumentException if
934 * <var>tid</var> does not exist.
935 * @throws SecurityException Throws SecurityException if your process does
936 * not have permission to modify the given thread, or to use the given
937 * priority.
938 *
939 * @see #setThreadPriority(int, int)
940 */
941 public static final native void setThreadPriority(int priority)
942 throws IllegalArgumentException, SecurityException;
943
944 /**
945 * Return the current priority of a thread, based on Linux priorities.
946 *
947 * @param tid The identifier of the thread/process to change.
948 *
949 * @return Returns the current priority, as a Linux priority level,
950 * from -20 for highest scheduling priority to 19 for lowest scheduling
951 * priority.
952 *
953 * @throws IllegalArgumentException Throws IllegalArgumentException if
954 * <var>tid</var> does not exist.
955 */
956 public static final native int getThreadPriority(int tid)
957 throws IllegalArgumentException;
958
959 /**
Glenn Kasten6793ac92011-07-13 12:44:12 -0700960 * Set the scheduling policy and priority of a thread, based on Linux.
961 *
962 * @param tid The identifier of the thread/process to change.
963 * @param policy A Linux scheduling policy such as SCHED_OTHER etc.
964 * @param priority A Linux priority level in a range appropriate for the given policy.
965 *
966 * @throws IllegalArgumentException Throws IllegalArgumentException if
967 * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
968 * @throws SecurityException Throws SecurityException if your process does
969 * not have permission to modify the given thread, or to use the given
970 * scheduling policy or priority.
971 *
972 * {@hide}
973 */
974 public static final native void setThreadScheduler(int tid, int policy, int priority)
975 throws IllegalArgumentException;
976
977 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 * Determine whether the current environment supports multiple processes.
979 *
980 * @return Returns true if the system can run in multiple processes, else
981 * false if everything is running in a single process.
Jeff Brown10e89712011-07-08 18:52:57 -0700982 *
983 * @deprecated This method always returns true. Do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 */
Jeff Brown10e89712011-07-08 18:52:57 -0700985 @Deprecated
986 public static final boolean supportsProcesses() {
987 return true;
988 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989
990 /**
Rom Lemarchand5534ba92013-07-12 16:15:36 -0700991 * Adjust the swappiness level for a process.
992 *
993 * @param pid The process identifier to set.
994 * @param is_increased Whether swappiness should be increased or default.
995 *
996 * @return Returns true if the underlying system supports this
997 * feature, else false.
998 *
999 * {@hide}
1000 */
1001 public static final native boolean setSwappiness(int pid, boolean is_increased);
1002
1003 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 * Change this process's argv[0] parameter. This can be useful to show
1005 * more descriptive information in things like the 'ps' command.
1006 *
1007 * @param text The new name of this process.
1008 *
1009 * {@hide}
1010 */
1011 public static final native void setArgV0(String text);
1012
1013 /**
1014 * Kill the process with the given PID.
1015 * Note that, though this API allows us to request to
1016 * kill any process based on its PID, the kernel will
1017 * still impose standard restrictions on which PIDs you
1018 * are actually able to kill. Typically this means only
1019 * the process running the caller's packages/application
1020 * and any additional processes created by that app; packages
1021 * sharing a common UID will also be able to kill each
1022 * other's processes.
1023 */
1024 public static final void killProcess(int pid) {
1025 sendSignal(pid, SIGNAL_KILL);
1026 }
1027
1028 /** @hide */
1029 public static final native int setUid(int uid);
1030
1031 /** @hide */
1032 public static final native int setGid(int uid);
1033
1034 /**
1035 * Send a signal to the given process.
1036 *
1037 * @param pid The pid of the target process.
1038 * @param signal The signal to send.
1039 */
1040 public static final native void sendSignal(int pid, int signal);
1041
Dianne Hackborn906497c2010-05-10 15:57:38 -07001042 /**
1043 * @hide
1044 * Private impl for avoiding a log message... DO NOT USE without doing
1045 * your own log, or the Android Illuminati will find you some night and
1046 * beat you up.
1047 */
1048 public static final void killProcessQuiet(int pid) {
1049 sendSignalQuiet(pid, SIGNAL_KILL);
1050 }
1051
1052 /**
1053 * @hide
1054 * Private impl for avoiding a log message... DO NOT USE without doing
1055 * your own log, or the Android Illuminati will find you some night and
1056 * beat you up.
1057 */
1058 public static final native void sendSignalQuiet(int pid, int signal);
1059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 /** @hide */
Marco Nelissen0bca96b2009-07-17 12:59:25 -07001061 public static final native long getFreeMemory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062
1063 /** @hide */
Dianne Hackborn59325eb2012-05-09 18:45:20 -07001064 public static final native long getTotalMemory();
1065
1066 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 public static final native void readProcLines(String path,
1068 String[] reqFields, long[] outSizes);
1069
1070 /** @hide */
1071 public static final native int[] getPids(String path, int[] lastArray);
1072
1073 /** @hide */
1074 public static final int PROC_TERM_MASK = 0xff;
1075 /** @hide */
1076 public static final int PROC_ZERO_TERM = 0;
1077 /** @hide */
1078 public static final int PROC_SPACE_TERM = (int)' ';
1079 /** @hide */
Evan Millarc64edde2009-04-18 12:26:32 -07001080 public static final int PROC_TAB_TERM = (int)'\t';
1081 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 public static final int PROC_COMBINE = 0x100;
1083 /** @hide */
1084 public static final int PROC_PARENS = 0x200;
1085 /** @hide */
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001086 public static final int PROC_QUOTES = 0x400;
1087 /** @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 public static final int PROC_OUT_STRING = 0x1000;
1089 /** @hide */
1090 public static final int PROC_OUT_LONG = 0x2000;
1091 /** @hide */
1092 public static final int PROC_OUT_FLOAT = 0x4000;
1093
1094 /** @hide */
1095 public static final native boolean readProcFile(String file, int[] format,
1096 String[] outStrings, long[] outLongs, float[] outFloats);
Evan Millarc64edde2009-04-18 12:26:32 -07001097
1098 /** @hide */
1099 public static final native boolean parseProcLine(byte[] buffer, int startIndex,
1100 int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001102 /** @hide */
1103 public static final native int[] getPidsForCommands(String[] cmds);
1104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 /**
1106 * Gets the total Pss value for a given process, in bytes.
1107 *
1108 * @param pid the process to the Pss for
1109 * @return the total Pss value for the given process in bytes,
1110 * or -1 if the value cannot be determined
1111 * @hide
1112 */
1113 public static final native long getPss(int pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -07001114
1115 /**
1116 * Specifies the outcome of having started a process.
1117 * @hide
1118 */
1119 public static final class ProcessStartResult {
1120 /**
1121 * The PID of the newly started process.
1122 * Always >= 0. (If the start failed, an exception will have been thrown instead.)
1123 */
1124 public int pid;
1125
1126 /**
1127 * True if the process was started with a wrapper attached.
1128 */
1129 public boolean usingWrapper;
1130 }
Colin Cross0769e552014-06-03 13:25:35 -07001131
1132 /**
1133 * Kill all processes in a process group started for the given
1134 * pid.
1135 * @hide
1136 */
1137 public static final native int killProcessGroup(int uid, int pid);
1138
1139 /**
1140 * Remove all process groups. Expected to be called when ActivityManager
1141 * is restarted.
1142 * @hide
1143 */
1144 public static final native void removeAllProcessGroups();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145}