The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.os; |
| 18 | |
| 19 | import android.net.LocalSocketAddress; |
| 20 | import android.net.LocalSocket; |
| 21 | import android.util.Log; |
| 22 | import dalvik.system.Zygote; |
| 23 | |
| 24 | import java.io.BufferedWriter; |
| 25 | import java.io.DataInputStream; |
| 26 | import java.io.IOException; |
| 27 | import java.io.OutputStreamWriter; |
| 28 | import java.util.ArrayList; |
| 29 | |
| 30 | /*package*/ class ZygoteStartFailedEx extends Exception { |
| 31 | /** |
| 32 | * Something prevented the zygote process startup from happening normally |
| 33 | */ |
| 34 | |
| 35 | ZygoteStartFailedEx() {}; |
| 36 | ZygoteStartFailedEx(String s) {super(s);} |
| 37 | ZygoteStartFailedEx(Throwable cause) {super(cause);} |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Tools for managing OS processes. |
| 42 | */ |
| 43 | public class Process { |
| 44 | private static final String LOG_TAG = "Process"; |
| 45 | |
| 46 | private static final String ZYGOTE_SOCKET = "zygote"; |
| 47 | |
| 48 | /** |
| 49 | * Name of a process for running the platform's media services. |
| 50 | * {@hide} |
| 51 | */ |
| 52 | public static final String ANDROID_SHARED_MEDIA = "com.android.process.media"; |
| 53 | |
| 54 | /** |
| 55 | * Name of the process that Google content providers can share. |
| 56 | * {@hide} |
| 57 | */ |
| 58 | public static final String GOOGLE_SHARED_APP_CONTENT = "com.google.process.content"; |
| 59 | |
| 60 | /** |
| 61 | * Defines the UID/GID under which system code runs. |
| 62 | */ |
| 63 | public static final int SYSTEM_UID = 1000; |
| 64 | |
| 65 | /** |
| 66 | * Defines the UID/GID under which the telephony code runs. |
| 67 | */ |
| 68 | public static final int PHONE_UID = 1001; |
| 69 | |
| 70 | /** |
Dianne Hackborn | 854060a | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 71 | * Defines the UID/GID for the user shell. |
| 72 | * @hide |
| 73 | */ |
| 74 | public static final int SHELL_UID = 2000; |
| 75 | |
| 76 | /** |
Mike Lockwood | d42685d | 2009-09-03 09:25:22 -0400 | [diff] [blame] | 77 | * Defines the UID/GID for the log group. |
| 78 | * @hide |
| 79 | */ |
| 80 | public static final int LOG_UID = 1007; |
| 81 | |
| 82 | /** |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 83 | * Defines the UID/GID for the WIFI supplicant process. |
| 84 | * @hide |
| 85 | */ |
| 86 | public static final int WIFI_UID = 1010; |
| 87 | |
| 88 | /** |
Glenn Kasten | 8b7d1b4 | 2011-07-13 16:23:22 -0700 | [diff] [blame] | 89 | * Defines the UID/GID for the mediaserver process. |
| 90 | * @hide |
| 91 | */ |
| 92 | public static final int MEDIA_UID = 1013; |
| 93 | |
| 94 | /** |
Jeff Sharkey | 5294a2f | 2012-04-24 17:07:22 -0700 | [diff] [blame] | 95 | * Defines the UID/GID for the DRM process. |
| 96 | * @hide |
| 97 | */ |
| 98 | public static final int DRM_UID = 1019; |
| 99 | |
| 100 | /** |
Mike Lockwood | 58fd98a | 2010-09-24 11:02:47 -0400 | [diff] [blame] | 101 | * Defines the GID for the group that allows write access to the SD card. |
| 102 | * @hide |
| 103 | */ |
| 104 | public static final int SDCARD_RW_GID = 1015; |
| 105 | |
| 106 | /** |
Kenny Root | 26993b3 | 2012-03-19 15:07:51 -0700 | [diff] [blame] | 107 | * Defines the UID/GID for the group that controls VPN services. |
| 108 | * @hide |
| 109 | */ |
| 110 | public static final int VPN_UID = 1016; |
| 111 | |
| 112 | /** |
Nick Pelly | cd0e839 | 2010-10-13 17:25:24 -0700 | [diff] [blame] | 113 | * Defines the UID/GID for the NFC service process. |
| 114 | * @hide |
| 115 | */ |
Nick Pelly | a5cb9f4 | 2011-11-21 14:54:46 -0800 | [diff] [blame] | 116 | public static final int NFC_UID = 1027; |
Nick Pelly | cd0e839 | 2010-10-13 17:25:24 -0700 | [diff] [blame] | 117 | |
| 118 | /** |
Jaikumar Ganesh | 1abb1cb | 2012-01-25 16:14:50 -0800 | [diff] [blame] | 119 | * Defines the UID/GID for the Bluetooth service process. |
| 120 | * @hide |
| 121 | */ |
| 122 | public static final int BLUETOOTH_UID = 1002; |
| 123 | |
| 124 | /** |
Mike Lockwood | dcaa10c | 2010-12-16 12:50:44 -0800 | [diff] [blame] | 125 | * Defines the GID for the group that allows write access to the internal media storage. |
| 126 | * @hide |
| 127 | */ |
| 128 | public static final int MEDIA_RW_GID = 1023; |
| 129 | |
| 130 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | * Defines the start of a range of UIDs (and GIDs), going from this |
| 132 | * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning |
| 133 | * to applications. |
| 134 | */ |
| 135 | public static final int FIRST_APPLICATION_UID = 10000; |
| 136 | /** |
| 137 | * Last of application-specific UIDs starting at |
| 138 | * {@link #FIRST_APPLICATION_UID}. |
| 139 | */ |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 140 | public static final int LAST_APPLICATION_UID = 19999; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * First uid used for fully isolated sandboxed processes (with no permissions of their own) |
| 144 | * @hide |
| 145 | */ |
| 146 | public static final int FIRST_ISOLATED_UID = 99000; |
| 147 | |
| 148 | /** |
| 149 | * Last uid used for fully isolated sandboxed processes (with no permissions of their own) |
| 150 | * @hide |
| 151 | */ |
| 152 | public static final int LAST_ISOLATED_UID = 99999; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * Defines a secondary group id for access to the bluetooth hardware. |
| 156 | */ |
| 157 | public static final int BLUETOOTH_GID = 2000; |
| 158 | |
| 159 | /** |
| 160 | * Standard priority of application threads. |
| 161 | * Use with {@link #setThreadPriority(int)} and |
| 162 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 163 | * {@link java.lang.Thread} class. |
| 164 | */ |
| 165 | public static final int THREAD_PRIORITY_DEFAULT = 0; |
| 166 | |
| 167 | /* |
| 168 | * *************************************** |
| 169 | * ** Keep in sync with utils/threads.h ** |
| 170 | * *************************************** |
| 171 | */ |
| 172 | |
| 173 | /** |
| 174 | * Lowest available thread priority. Only for those who really, really |
| 175 | * don't want to run if anything else is happening. |
| 176 | * Use with {@link #setThreadPriority(int)} and |
| 177 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 178 | * {@link java.lang.Thread} class. |
| 179 | */ |
| 180 | public static final int THREAD_PRIORITY_LOWEST = 19; |
| 181 | |
| 182 | /** |
| 183 | * Standard priority background threads. This gives your thread a slightly |
| 184 | * lower than normal priority, so that it will have less chance of impacting |
| 185 | * the responsiveness of the user interface. |
| 186 | * Use with {@link #setThreadPriority(int)} and |
| 187 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 188 | * {@link java.lang.Thread} class. |
| 189 | */ |
| 190 | public static final int THREAD_PRIORITY_BACKGROUND = 10; |
| 191 | |
| 192 | /** |
| 193 | * Standard priority of threads that are currently running a user interface |
| 194 | * that the user is interacting with. Applications can not normally |
| 195 | * change to this priority; the system will automatically adjust your |
| 196 | * application threads as the user moves through the UI. |
| 197 | * Use with {@link #setThreadPriority(int)} and |
| 198 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 199 | * {@link java.lang.Thread} class. |
| 200 | */ |
| 201 | public static final int THREAD_PRIORITY_FOREGROUND = -2; |
| 202 | |
| 203 | /** |
| 204 | * Standard priority of system display threads, involved in updating |
| 205 | * the user interface. Applications can not |
| 206 | * normally change to this priority. |
| 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_DISPLAY = -4; |
| 212 | |
| 213 | /** |
| 214 | * Standard priority of the most important display threads, for compositing |
| 215 | * the screen and retrieving input events. Applications can not normally |
| 216 | * change to this priority. |
| 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_URGENT_DISPLAY = -8; |
| 222 | |
| 223 | /** |
| 224 | * Standard priority of audio threads. Applications can not normally |
| 225 | * 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_AUDIO = -16; |
| 231 | |
| 232 | /** |
| 233 | * Standard priority of the most important audio threads. |
| 234 | * Applications can not normally change to this priority. |
| 235 | * Use with {@link #setThreadPriority(int)} and |
| 236 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 237 | * {@link java.lang.Thread} class. |
| 238 | */ |
| 239 | public static final int THREAD_PRIORITY_URGENT_AUDIO = -19; |
| 240 | |
| 241 | /** |
| 242 | * Minimum increment to make a priority more favorable. |
| 243 | */ |
| 244 | public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1; |
| 245 | |
| 246 | /** |
| 247 | * Minimum increment to make a priority less favorable. |
| 248 | */ |
| 249 | public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1; |
| 250 | |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 251 | /** |
Glenn Kasten | 6793ac9 | 2011-07-13 12:44:12 -0700 | [diff] [blame] | 252 | * Default scheduling policy |
| 253 | * @hide |
| 254 | */ |
| 255 | public static final int SCHED_OTHER = 0; |
| 256 | |
| 257 | /** |
| 258 | * First-In First-Out scheduling policy |
| 259 | * @hide |
| 260 | */ |
| 261 | public static final int SCHED_FIFO = 1; |
| 262 | |
| 263 | /** |
| 264 | * Round-Robin scheduling policy |
| 265 | * @hide |
| 266 | */ |
| 267 | public static final int SCHED_RR = 2; |
| 268 | |
| 269 | /** |
| 270 | * Batch scheduling policy |
| 271 | * @hide |
| 272 | */ |
| 273 | public static final int SCHED_BATCH = 3; |
| 274 | |
| 275 | /** |
| 276 | * Idle scheduling policy |
| 277 | * @hide |
| 278 | */ |
| 279 | public static final int SCHED_IDLE = 5; |
| 280 | |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 281 | // Keep in sync with SP_* constants of enum type SchedPolicy |
| 282 | // declared in system/core/include/cutils/sched_policy.h, |
| 283 | // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value. |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 284 | |
| 285 | /** |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 286 | * Default thread group - |
| 287 | * has meaning with setProcessGroup() only, cannot be used with setThreadGroup(). |
| 288 | * When used with setProcessGroup(), the group of each thread in the process |
| 289 | * is conditionally changed based on that thread's current priority, as follows: |
| 290 | * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND |
| 291 | * are moved to foreground thread group. All other threads are left unchanged. |
| 292 | * @hide |
| 293 | */ |
| 294 | public static final int THREAD_GROUP_DEFAULT = -1; |
| 295 | |
| 296 | /** |
| 297 | * Background thread group - All threads in |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 298 | * this group are scheduled with a reduced share of the CPU. |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 299 | * Value is same as constant SP_BACKGROUND of enum SchedPolicy. |
| 300 | * FIXME rename to THREAD_GROUP_BACKGROUND. |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 301 | * @hide |
| 302 | */ |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 303 | public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0; |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 304 | |
| 305 | /** |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 306 | * Foreground thread group - All threads in |
| 307 | * this group are scheduled with a normal share of the CPU. |
| 308 | * Value is same as constant SP_FOREGROUND of enum SchedPolicy. |
| 309 | * Not used at this level. |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 310 | * @hide |
| 311 | **/ |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 312 | private static final int THREAD_GROUP_FOREGROUND = 1; |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 313 | |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 314 | /** |
| 315 | * System thread group. |
| 316 | * @hide |
| 317 | **/ |
| 318 | public static final int THREAD_GROUP_SYSTEM = 2; |
| 319 | |
| 320 | /** |
| 321 | * Application audio thread group. |
| 322 | * @hide |
| 323 | **/ |
| 324 | public static final int THREAD_GROUP_AUDIO_APP = 3; |
| 325 | |
| 326 | /** |
| 327 | * System audio thread group. |
| 328 | * @hide |
| 329 | **/ |
| 330 | public static final int THREAD_GROUP_AUDIO_SYS = 4; |
| 331 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | public static final int SIGNAL_QUIT = 3; |
| 333 | public static final int SIGNAL_KILL = 9; |
| 334 | public static final int SIGNAL_USR1 = 10; |
| 335 | |
| 336 | // State for communicating with zygote process |
| 337 | |
| 338 | static LocalSocket sZygoteSocket; |
| 339 | static DataInputStream sZygoteInputStream; |
| 340 | static BufferedWriter sZygoteWriter; |
| 341 | |
| 342 | /** true if previous zygote open failed */ |
| 343 | static boolean sPreviousZygoteOpenFailed; |
| 344 | |
| 345 | /** |
| 346 | * Start a new process. |
| 347 | * |
| 348 | * <p>If processes are enabled, a new process is created and the |
| 349 | * static main() function of a <var>processClass</var> is executed there. |
| 350 | * The process will continue running after this function returns. |
| 351 | * |
| 352 | * <p>If processes are not enabled, a new thread in the caller's |
| 353 | * process is created and main() of <var>processClass</var> called there. |
| 354 | * |
| 355 | * <p>The niceName parameter, if not an empty string, is a custom name to |
| 356 | * give to the process instead of using processClass. This allows you to |
| 357 | * make easily identifyable processes even if you are using the same base |
| 358 | * <var>processClass</var> to start them. |
| 359 | * |
| 360 | * @param processClass The class to use as the process's main entry |
| 361 | * point. |
| 362 | * @param niceName A more readable name to use for the process. |
| 363 | * @param uid The user-id under which the process will run. |
| 364 | * @param gid The group-id under which the process will run. |
| 365 | * @param gids Additional group-ids associated with the process. |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 366 | * @param debugFlags Additional flags. |
| 367 | * @param targetSdkVersion The target SDK version for the app. |
Stephen Smalley | 83d9eda | 2012-01-13 08:34:17 -0500 | [diff] [blame] | 368 | * @param seInfo null-ok SE Android information for the new process. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | * @param zygoteArgs Additional arguments to supply to the zygote process. |
| 370 | * |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 371 | * @return An object that describes the result of the attempt to start the process. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | * @throws RuntimeException on fatal start failure |
| 373 | * |
| 374 | * {@hide} |
| 375 | */ |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 376 | public static final ProcessStartResult start(final String processClass, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | final String niceName, |
| 378 | int uid, int gid, int[] gids, |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 379 | int debugFlags, int mountExternal, |
| 380 | int targetSdkVersion, |
Stephen Smalley | 83d9eda | 2012-01-13 08:34:17 -0500 | [diff] [blame] | 381 | String seInfo, |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 382 | String[] zygoteArgs) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 384 | return startViaZygote(processClass, niceName, uid, gid, gids, |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 385 | debugFlags, mountExternal, targetSdkVersion, seInfo, zygoteArgs); |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 386 | } catch (ZygoteStartFailedEx ex) { |
| 387 | Log.e(LOG_TAG, |
| 388 | "Starting VM process through Zygote failed"); |
| 389 | throw new RuntimeException( |
| 390 | "Starting VM process through Zygote failed", ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /** retry interval for opening a zygote socket */ |
| 395 | static final int ZYGOTE_RETRY_MILLIS = 500; |
| 396 | |
| 397 | /** |
| 398 | * Tries to open socket to Zygote process if not already open. If |
| 399 | * already open, does nothing. May block and retry. |
| 400 | */ |
| 401 | private static void openZygoteSocketIfNeeded() |
| 402 | throws ZygoteStartFailedEx { |
| 403 | |
| 404 | int retryCount; |
| 405 | |
| 406 | if (sPreviousZygoteOpenFailed) { |
| 407 | /* |
| 408 | * If we've failed before, expect that we'll fail again and |
| 409 | * don't pause for retries. |
| 410 | */ |
| 411 | retryCount = 0; |
| 412 | } else { |
| 413 | retryCount = 10; |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * See bug #811181: Sometimes runtime can make it up before zygote. |
| 418 | * Really, we'd like to do something better to avoid this condition, |
| 419 | * but for now just wait a bit... |
| 420 | */ |
| 421 | for (int retry = 0 |
| 422 | ; (sZygoteSocket == null) && (retry < (retryCount + 1)) |
| 423 | ; retry++ ) { |
| 424 | |
| 425 | if (retry > 0) { |
| 426 | try { |
| 427 | Log.i("Zygote", "Zygote not up yet, sleeping..."); |
| 428 | Thread.sleep(ZYGOTE_RETRY_MILLIS); |
| 429 | } catch (InterruptedException ex) { |
| 430 | // should never happen |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | try { |
| 435 | sZygoteSocket = new LocalSocket(); |
| 436 | |
| 437 | sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET, |
| 438 | LocalSocketAddress.Namespace.RESERVED)); |
| 439 | |
| 440 | sZygoteInputStream |
| 441 | = new DataInputStream(sZygoteSocket.getInputStream()); |
| 442 | |
| 443 | sZygoteWriter = |
| 444 | new BufferedWriter( |
| 445 | new OutputStreamWriter( |
| 446 | sZygoteSocket.getOutputStream()), |
| 447 | 256); |
| 448 | |
| 449 | Log.i("Zygote", "Process: zygote socket opened"); |
| 450 | |
| 451 | sPreviousZygoteOpenFailed = false; |
| 452 | break; |
| 453 | } catch (IOException ex) { |
| 454 | if (sZygoteSocket != null) { |
| 455 | try { |
| 456 | sZygoteSocket.close(); |
| 457 | } catch (IOException ex2) { |
| 458 | Log.e(LOG_TAG,"I/O exception on close after exception", |
| 459 | ex2); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | sZygoteSocket = null; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | if (sZygoteSocket == null) { |
| 468 | sPreviousZygoteOpenFailed = true; |
| 469 | throw new ZygoteStartFailedEx("connect failed"); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Sends an argument list to the zygote process, which starts a new child |
| 475 | * and returns the child's pid. Please note: the present implementation |
| 476 | * replaces newlines in the argument list with spaces. |
| 477 | * @param args argument list |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 478 | * @return An object that describes the result of the attempt to start the process. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | * @throws ZygoteStartFailedEx if process start failed for any reason |
| 480 | */ |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 481 | private static ProcessStartResult zygoteSendArgsAndGetResult(ArrayList<String> args) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | throws ZygoteStartFailedEx { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | openZygoteSocketIfNeeded(); |
| 484 | |
| 485 | try { |
| 486 | /** |
| 487 | * See com.android.internal.os.ZygoteInit.readArgumentList() |
| 488 | * Presently the wire format to the zygote process is: |
| 489 | * a) a count of arguments (argc, in essence) |
| 490 | * b) a number of newline-separated argument strings equal to count |
| 491 | * |
| 492 | * After the zygote process reads these it will write the pid of |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 493 | * the child or -1 on failure, followed by boolean to |
| 494 | * indicate whether a wrapper process was used. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | */ |
| 496 | |
| 497 | sZygoteWriter.write(Integer.toString(args.size())); |
| 498 | sZygoteWriter.newLine(); |
| 499 | |
| 500 | int sz = args.size(); |
| 501 | for (int i = 0; i < sz; i++) { |
| 502 | String arg = args.get(i); |
| 503 | if (arg.indexOf('\n') >= 0) { |
| 504 | throw new ZygoteStartFailedEx( |
| 505 | "embedded newlines not allowed"); |
| 506 | } |
| 507 | sZygoteWriter.write(arg); |
| 508 | sZygoteWriter.newLine(); |
| 509 | } |
| 510 | |
| 511 | sZygoteWriter.flush(); |
| 512 | |
| 513 | // Should there be a timeout on this? |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 514 | ProcessStartResult result = new ProcessStartResult(); |
| 515 | result.pid = sZygoteInputStream.readInt(); |
| 516 | if (result.pid < 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | throw new ZygoteStartFailedEx("fork() failed"); |
| 518 | } |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 519 | result.usingWrapper = sZygoteInputStream.readBoolean(); |
| 520 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | } catch (IOException ex) { |
| 522 | try { |
| 523 | if (sZygoteSocket != null) { |
| 524 | sZygoteSocket.close(); |
| 525 | } |
| 526 | } catch (IOException ex2) { |
| 527 | // we're going to fail anyway |
| 528 | Log.e(LOG_TAG,"I/O exception on routine close", ex2); |
| 529 | } |
| 530 | |
| 531 | sZygoteSocket = null; |
| 532 | |
| 533 | throw new ZygoteStartFailedEx(ex); |
| 534 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Starts a new process via the zygote mechanism. |
| 539 | * |
| 540 | * @param processClass Class name whose static main() to run |
| 541 | * @param niceName 'nice' process name to appear in ps |
| 542 | * @param uid a POSIX uid that the new process should setuid() to |
| 543 | * @param gid a POSIX gid that the new process shuold setgid() to |
| 544 | * @param gids null-ok; a list of supplementary group IDs that the |
| 545 | * new process should setgroup() to. |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 546 | * @param debugFlags Additional flags. |
| 547 | * @param targetSdkVersion The target SDK version for the app. |
Stephen Smalley | 83d9eda | 2012-01-13 08:34:17 -0500 | [diff] [blame] | 548 | * @param seInfo null-ok SE Android information for the new process. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | * @param extraArgs Additional arguments to supply to the zygote process. |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 550 | * @return An object that describes the result of the attempt to start the process. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | * @throws ZygoteStartFailedEx if process start failed for any reason |
| 552 | */ |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 553 | private static ProcessStartResult startViaZygote(final String processClass, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 554 | final String niceName, |
| 555 | final int uid, final int gid, |
| 556 | final int[] gids, |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 557 | int debugFlags, int mountExternal, |
| 558 | int targetSdkVersion, |
Stephen Smalley | 83d9eda | 2012-01-13 08:34:17 -0500 | [diff] [blame] | 559 | String seInfo, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | String[] extraArgs) |
| 561 | throws ZygoteStartFailedEx { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | synchronized(Process.class) { |
| 563 | ArrayList<String> argsForZygote = new ArrayList<String>(); |
| 564 | |
| 565 | // --runtime-init, --setuid=, --setgid=, |
| 566 | // and --setgroups= must go first |
| 567 | argsForZygote.add("--runtime-init"); |
| 568 | argsForZygote.add("--setuid=" + uid); |
| 569 | argsForZygote.add("--setgid=" + gid); |
Elliott Hughes | ae07ecf | 2011-07-06 17:33:27 -0700 | [diff] [blame] | 570 | if ((debugFlags & Zygote.DEBUG_ENABLE_JNI_LOGGING) != 0) { |
| 571 | argsForZygote.add("--enable-jni-logging"); |
| 572 | } |
Ben Cheng | 23085b7 | 2010-02-08 16:06:32 -0800 | [diff] [blame] | 573 | if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) { |
| 574 | argsForZygote.add("--enable-safemode"); |
| 575 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) { |
| 577 | argsForZygote.add("--enable-debugger"); |
| 578 | } |
| 579 | if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) { |
| 580 | argsForZygote.add("--enable-checkjni"); |
| 581 | } |
| 582 | if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) { |
| 583 | argsForZygote.add("--enable-assert"); |
| 584 | } |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 585 | if (mountExternal == Zygote.MOUNT_EXTERNAL_SINGLEUSER) { |
| 586 | argsForZygote.add("--mount-external-singleuser"); |
| 587 | } else if (mountExternal == Zygote.MOUNT_EXTERNAL_MULTIUSER) { |
| 588 | argsForZygote.add("--mount-external-multiuser"); |
| 589 | } |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 590 | argsForZygote.add("--target-sdk-version=" + targetSdkVersion); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | |
| 592 | //TODO optionally enable debuger |
| 593 | //argsForZygote.add("--enable-debugger"); |
| 594 | |
| 595 | // --setgroups is a comma-separated list |
| 596 | if (gids != null && gids.length > 0) { |
| 597 | StringBuilder sb = new StringBuilder(); |
| 598 | sb.append("--setgroups="); |
| 599 | |
| 600 | int sz = gids.length; |
| 601 | for (int i = 0; i < sz; i++) { |
| 602 | if (i != 0) { |
| 603 | sb.append(','); |
| 604 | } |
| 605 | sb.append(gids[i]); |
| 606 | } |
| 607 | |
| 608 | argsForZygote.add(sb.toString()); |
| 609 | } |
| 610 | |
| 611 | if (niceName != null) { |
| 612 | argsForZygote.add("--nice-name=" + niceName); |
| 613 | } |
| 614 | |
Stephen Smalley | 83d9eda | 2012-01-13 08:34:17 -0500 | [diff] [blame] | 615 | if (seInfo != null) { |
| 616 | argsForZygote.add("--seinfo=" + seInfo); |
| 617 | } |
| 618 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | argsForZygote.add(processClass); |
| 620 | |
| 621 | if (extraArgs != null) { |
| 622 | for (String arg : extraArgs) { |
| 623 | argsForZygote.add(arg); |
| 624 | } |
| 625 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 627 | return zygoteSendArgsAndGetResult(argsForZygote); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /** |
| 632 | * Returns elapsed milliseconds of the time this process has run. |
| 633 | * @return Returns the number of milliseconds this process has return. |
| 634 | */ |
| 635 | public static final native long getElapsedCpuTime(); |
| 636 | |
| 637 | /** |
| 638 | * Returns the identifier of this process, which can be used with |
| 639 | * {@link #killProcess} and {@link #sendSignal}. |
| 640 | */ |
| 641 | public static final native int myPid(); |
| 642 | |
| 643 | /** |
| 644 | * Returns the identifier of the calling thread, which be used with |
| 645 | * {@link #setThreadPriority(int, int)}. |
| 646 | */ |
| 647 | public static final native int myTid(); |
| 648 | |
| 649 | /** |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 650 | * Returns the identifier of this process's uid. This is the kernel uid |
| 651 | * that the process is running under, which is the identity of its |
| 652 | * app-specific sandbox. It is different from {@link #myUserHandle} in that |
| 653 | * a uid identifies a specific app sandbox in a specific user. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | */ |
| 655 | public static final native int myUid(); |
| 656 | |
| 657 | /** |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame^] | 658 | * Returns this process's user handle. This is the |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 659 | * user the process is running under. It is distinct from |
| 660 | * {@link #myUid()} in that a particular user will have multiple |
| 661 | * distinct apps running under it each with their own uid. |
| 662 | */ |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame^] | 663 | public static final UserHandle myUserHandle() { |
| 664 | return new UserHandle(UserHandle.getUserId(myUid())); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /** |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 668 | * Returns whether the current process is in an isolated sandbox. |
| 669 | * @hide |
| 670 | */ |
| 671 | public static final boolean isIsolated() { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 672 | int uid = UserHandle.getAppId(myUid()); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 673 | return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID; |
| 674 | } |
| 675 | |
| 676 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 | * Returns the UID assigned to a particular user name, or -1 if there is |
| 678 | * none. If the given string consists of only numbers, it is converted |
| 679 | * directly to a uid. |
| 680 | */ |
| 681 | public static final native int getUidForName(String name); |
| 682 | |
| 683 | /** |
| 684 | * Returns the GID assigned to a particular user name, or -1 if there is |
| 685 | * none. If the given string consists of only numbers, it is converted |
| 686 | * directly to a gid. |
| 687 | */ |
| 688 | public static final native int getGidForName(String name); |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 689 | |
| 690 | /** |
| 691 | * Returns a uid for a currently running process. |
| 692 | * @param pid the process id |
| 693 | * @return the uid of the process, or -1 if the process is not running. |
| 694 | * @hide pending API council review |
| 695 | */ |
| 696 | public static final int getUidForPid(int pid) { |
| 697 | String[] procStatusLabels = { "Uid:" }; |
| 698 | long[] procStatusValues = new long[1]; |
| 699 | procStatusValues[0] = -1; |
| 700 | Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues); |
| 701 | return (int) procStatusValues[0]; |
| 702 | } |
| 703 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | /** |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 705 | * Returns the parent process id for a currently running process. |
| 706 | * @param pid the process id |
| 707 | * @return the parent process id of the process, or -1 if the process is not running. |
| 708 | * @hide |
| 709 | */ |
| 710 | public static final int getParentPid(int pid) { |
| 711 | String[] procStatusLabels = { "PPid:" }; |
| 712 | long[] procStatusValues = new long[1]; |
| 713 | procStatusValues[0] = -1; |
| 714 | Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues); |
| 715 | return (int) procStatusValues[0]; |
| 716 | } |
| 717 | |
| 718 | /** |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 719 | * Returns the thread group leader id for a currently running thread. |
| 720 | * @param tid the thread id |
| 721 | * @return the thread group leader id of the thread, or -1 if the thread is not running. |
| 722 | * This is same as what getpid(2) would return if called by tid. |
| 723 | * @hide |
| 724 | */ |
| 725 | public static final int getThreadGroupLeader(int tid) { |
| 726 | String[] procStatusLabels = { "Tgid:" }; |
| 727 | long[] procStatusValues = new long[1]; |
| 728 | procStatusValues[0] = -1; |
| 729 | Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues); |
| 730 | return (int) procStatusValues[0]; |
| 731 | } |
| 732 | |
| 733 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | * Set the priority of a thread, based on Linux priorities. |
| 735 | * |
| 736 | * @param tid The identifier of the thread/process to change. |
| 737 | * @param priority A Linux priority level, from -20 for highest scheduling |
| 738 | * priority to 19 for lowest scheduling priority. |
| 739 | * |
| 740 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 741 | * <var>tid</var> does not exist. |
| 742 | * @throws SecurityException Throws SecurityException if your process does |
| 743 | * not have permission to modify the given thread, or to use the given |
| 744 | * priority. |
| 745 | */ |
| 746 | public static final native void setThreadPriority(int tid, int priority) |
| 747 | throws IllegalArgumentException, SecurityException; |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 748 | |
| 749 | /** |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 750 | * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to |
| 751 | * throw an exception if passed a background-level thread priority. This is only |
| 752 | * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1. |
| 753 | * |
| 754 | * @hide |
| 755 | */ |
| 756 | public static final native void setCanSelfBackground(boolean backgroundOk); |
| 757 | |
| 758 | /** |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 759 | * Sets the scheduling group for a thread. |
| 760 | * @hide |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 761 | * @param tid The identifier of the thread to change. |
| 762 | * @param group The target group for this thread from THREAD_GROUP_*. |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 763 | * |
| 764 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 765 | * <var>tid</var> does not exist. |
| 766 | * @throws SecurityException Throws SecurityException if your process does |
| 767 | * not have permission to modify the given thread, or to use the given |
| 768 | * priority. |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 769 | * If the thread is a thread group leader, that is it's gettid() == getpid(), |
| 770 | * then the other threads in the same thread group are _not_ affected. |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 771 | */ |
| 772 | public static final native void setThreadGroup(int tid, int group) |
| 773 | throws IllegalArgumentException, SecurityException; |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 774 | |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 775 | /** |
| 776 | * Sets the scheduling group for a process and all child threads |
| 777 | * @hide |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 778 | * @param pid The identifier of the process to change. |
| 779 | * @param group The target group for this process from THREAD_GROUP_*. |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 780 | * |
| 781 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 782 | * <var>tid</var> does not exist. |
| 783 | * @throws SecurityException Throws SecurityException if your process does |
| 784 | * not have permission to modify the given thread, or to use the given |
| 785 | * priority. |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 786 | * |
| 787 | * group == THREAD_GROUP_DEFAULT means to move all non-background priority |
| 788 | * threads to the foreground scheduling group, but to leave background |
| 789 | * priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all |
| 790 | * threads, regardless of priority, to the background scheduling group. |
| 791 | * group == THREAD_GROUP_FOREGROUND is not allowed. |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 792 | */ |
| 793 | public static final native void setProcessGroup(int pid, int group) |
| 794 | throws IllegalArgumentException, SecurityException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 795 | |
| 796 | /** |
| 797 | * Set the priority of the calling thread, based on Linux priorities. See |
| 798 | * {@link #setThreadPriority(int, int)} for more information. |
| 799 | * |
| 800 | * @param priority A Linux priority level, from -20 for highest scheduling |
| 801 | * priority to 19 for lowest scheduling priority. |
| 802 | * |
| 803 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 804 | * <var>tid</var> does not exist. |
| 805 | * @throws SecurityException Throws SecurityException if your process does |
| 806 | * not have permission to modify the given thread, or to use the given |
| 807 | * priority. |
| 808 | * |
| 809 | * @see #setThreadPriority(int, int) |
| 810 | */ |
| 811 | public static final native void setThreadPriority(int priority) |
| 812 | throws IllegalArgumentException, SecurityException; |
| 813 | |
| 814 | /** |
| 815 | * Return the current priority of a thread, based on Linux priorities. |
| 816 | * |
| 817 | * @param tid The identifier of the thread/process to change. |
| 818 | * |
| 819 | * @return Returns the current priority, as a Linux priority level, |
| 820 | * from -20 for highest scheduling priority to 19 for lowest scheduling |
| 821 | * priority. |
| 822 | * |
| 823 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 824 | * <var>tid</var> does not exist. |
| 825 | */ |
| 826 | public static final native int getThreadPriority(int tid) |
| 827 | throws IllegalArgumentException; |
| 828 | |
| 829 | /** |
Glenn Kasten | 6793ac9 | 2011-07-13 12:44:12 -0700 | [diff] [blame] | 830 | * Set the scheduling policy and priority of a thread, based on Linux. |
| 831 | * |
| 832 | * @param tid The identifier of the thread/process to change. |
| 833 | * @param policy A Linux scheduling policy such as SCHED_OTHER etc. |
| 834 | * @param priority A Linux priority level in a range appropriate for the given policy. |
| 835 | * |
| 836 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 837 | * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy. |
| 838 | * @throws SecurityException Throws SecurityException if your process does |
| 839 | * not have permission to modify the given thread, or to use the given |
| 840 | * scheduling policy or priority. |
| 841 | * |
| 842 | * {@hide} |
| 843 | */ |
| 844 | public static final native void setThreadScheduler(int tid, int policy, int priority) |
| 845 | throws IllegalArgumentException; |
| 846 | |
| 847 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | * Determine whether the current environment supports multiple processes. |
| 849 | * |
| 850 | * @return Returns true if the system can run in multiple processes, else |
| 851 | * false if everything is running in a single process. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 852 | * |
| 853 | * @deprecated This method always returns true. Do not use. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 854 | */ |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 855 | @Deprecated |
| 856 | public static final boolean supportsProcesses() { |
| 857 | return true; |
| 858 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 859 | |
| 860 | /** |
| 861 | * Set the out-of-memory badness adjustment for a process. |
| 862 | * |
| 863 | * @param pid The process identifier to set. |
| 864 | * @param amt Adjustment value -- linux allows -16 to +15. |
| 865 | * |
| 866 | * @return Returns true if the underlying system supports this |
| 867 | * feature, else false. |
| 868 | * |
| 869 | * {@hide} |
| 870 | */ |
| 871 | public static final native boolean setOomAdj(int pid, int amt); |
| 872 | |
| 873 | /** |
| 874 | * Change this process's argv[0] parameter. This can be useful to show |
| 875 | * more descriptive information in things like the 'ps' command. |
| 876 | * |
| 877 | * @param text The new name of this process. |
| 878 | * |
| 879 | * {@hide} |
| 880 | */ |
| 881 | public static final native void setArgV0(String text); |
| 882 | |
| 883 | /** |
| 884 | * Kill the process with the given PID. |
| 885 | * Note that, though this API allows us to request to |
| 886 | * kill any process based on its PID, the kernel will |
| 887 | * still impose standard restrictions on which PIDs you |
| 888 | * are actually able to kill. Typically this means only |
| 889 | * the process running the caller's packages/application |
| 890 | * and any additional processes created by that app; packages |
| 891 | * sharing a common UID will also be able to kill each |
| 892 | * other's processes. |
| 893 | */ |
| 894 | public static final void killProcess(int pid) { |
| 895 | sendSignal(pid, SIGNAL_KILL); |
| 896 | } |
| 897 | |
| 898 | /** @hide */ |
| 899 | public static final native int setUid(int uid); |
| 900 | |
| 901 | /** @hide */ |
| 902 | public static final native int setGid(int uid); |
| 903 | |
| 904 | /** |
| 905 | * Send a signal to the given process. |
| 906 | * |
| 907 | * @param pid The pid of the target process. |
| 908 | * @param signal The signal to send. |
| 909 | */ |
| 910 | public static final native void sendSignal(int pid, int signal); |
| 911 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 912 | /** |
| 913 | * @hide |
| 914 | * Private impl for avoiding a log message... DO NOT USE without doing |
| 915 | * your own log, or the Android Illuminati will find you some night and |
| 916 | * beat you up. |
| 917 | */ |
| 918 | public static final void killProcessQuiet(int pid) { |
| 919 | sendSignalQuiet(pid, SIGNAL_KILL); |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * @hide |
| 924 | * Private impl for avoiding a log message... DO NOT USE without doing |
| 925 | * your own log, or the Android Illuminati will find you some night and |
| 926 | * beat you up. |
| 927 | */ |
| 928 | public static final native void sendSignalQuiet(int pid, int signal); |
| 929 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 930 | /** @hide */ |
Marco Nelissen | 0bca96b | 2009-07-17 12:59:25 -0700 | [diff] [blame] | 931 | public static final native long getFreeMemory(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | |
| 933 | /** @hide */ |
Dianne Hackborn | 59325eb | 2012-05-09 18:45:20 -0700 | [diff] [blame] | 934 | public static final native long getTotalMemory(); |
| 935 | |
| 936 | /** @hide */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | public static final native void readProcLines(String path, |
| 938 | String[] reqFields, long[] outSizes); |
| 939 | |
| 940 | /** @hide */ |
| 941 | public static final native int[] getPids(String path, int[] lastArray); |
| 942 | |
| 943 | /** @hide */ |
| 944 | public static final int PROC_TERM_MASK = 0xff; |
| 945 | /** @hide */ |
| 946 | public static final int PROC_ZERO_TERM = 0; |
| 947 | /** @hide */ |
| 948 | public static final int PROC_SPACE_TERM = (int)' '; |
| 949 | /** @hide */ |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 950 | public static final int PROC_TAB_TERM = (int)'\t'; |
| 951 | /** @hide */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 952 | public static final int PROC_COMBINE = 0x100; |
| 953 | /** @hide */ |
| 954 | public static final int PROC_PARENS = 0x200; |
| 955 | /** @hide */ |
| 956 | public static final int PROC_OUT_STRING = 0x1000; |
| 957 | /** @hide */ |
| 958 | public static final int PROC_OUT_LONG = 0x2000; |
| 959 | /** @hide */ |
| 960 | public static final int PROC_OUT_FLOAT = 0x4000; |
| 961 | |
| 962 | /** @hide */ |
| 963 | public static final native boolean readProcFile(String file, int[] format, |
| 964 | String[] outStrings, long[] outLongs, float[] outFloats); |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 965 | |
| 966 | /** @hide */ |
| 967 | public static final native boolean parseProcLine(byte[] buffer, int startIndex, |
| 968 | int endIndex, int[] format, String[] outStrings, long[] outLongs, float[] outFloats); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 969 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 970 | /** @hide */ |
| 971 | public static final native int[] getPidsForCommands(String[] cmds); |
| 972 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 973 | /** |
| 974 | * Gets the total Pss value for a given process, in bytes. |
| 975 | * |
| 976 | * @param pid the process to the Pss for |
| 977 | * @return the total Pss value for the given process in bytes, |
| 978 | * or -1 if the value cannot be determined |
| 979 | * @hide |
| 980 | */ |
| 981 | public static final native long getPss(int pid); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 982 | |
| 983 | /** |
| 984 | * Specifies the outcome of having started a process. |
| 985 | * @hide |
| 986 | */ |
| 987 | public static final class ProcessStartResult { |
| 988 | /** |
| 989 | * The PID of the newly started process. |
| 990 | * Always >= 0. (If the start failed, an exception will have been thrown instead.) |
| 991 | */ |
| 992 | public int pid; |
| 993 | |
| 994 | /** |
| 995 | * True if the process was started with a wrapper attached. |
| 996 | */ |
| 997 | public boolean usingWrapper; |
| 998 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 999 | } |