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 | 854060af | 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 | /** |
Mike Lockwood | 58fd98a | 2010-09-24 11:02:47 -0400 | [diff] [blame] | 95 | * Defines the GID for the group that allows write access to the SD card. |
| 96 | * @hide |
| 97 | */ |
| 98 | public static final int SDCARD_RW_GID = 1015; |
| 99 | |
| 100 | /** |
Nick Pelly | cd0e839 | 2010-10-13 17:25:24 -0700 | [diff] [blame] | 101 | * Defines the UID/GID for the NFC service process. |
| 102 | * @hide |
| 103 | */ |
Nick Pelly | a5cb9f4 | 2011-11-21 14:54:46 -0800 | [diff] [blame^] | 104 | public static final int NFC_UID = 1027; |
Nick Pelly | cd0e839 | 2010-10-13 17:25:24 -0700 | [diff] [blame] | 105 | |
| 106 | /** |
Mike Lockwood | dcaa10c | 2010-12-16 12:50:44 -0800 | [diff] [blame] | 107 | * Defines the GID for the group that allows write access to the internal media storage. |
| 108 | * @hide |
| 109 | */ |
| 110 | public static final int MEDIA_RW_GID = 1023; |
| 111 | |
| 112 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | * Defines the start of a range of UIDs (and GIDs), going from this |
| 114 | * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning |
| 115 | * to applications. |
| 116 | */ |
| 117 | public static final int FIRST_APPLICATION_UID = 10000; |
| 118 | /** |
| 119 | * Last of application-specific UIDs starting at |
| 120 | * {@link #FIRST_APPLICATION_UID}. |
| 121 | */ |
| 122 | public static final int LAST_APPLICATION_UID = 99999; |
| 123 | |
| 124 | /** |
| 125 | * Defines a secondary group id for access to the bluetooth hardware. |
| 126 | */ |
| 127 | public static final int BLUETOOTH_GID = 2000; |
| 128 | |
| 129 | /** |
| 130 | * Standard priority of application threads. |
| 131 | * Use with {@link #setThreadPriority(int)} and |
| 132 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 133 | * {@link java.lang.Thread} class. |
| 134 | */ |
| 135 | public static final int THREAD_PRIORITY_DEFAULT = 0; |
| 136 | |
| 137 | /* |
| 138 | * *************************************** |
| 139 | * ** Keep in sync with utils/threads.h ** |
| 140 | * *************************************** |
| 141 | */ |
| 142 | |
| 143 | /** |
| 144 | * Lowest available thread priority. Only for those who really, really |
| 145 | * don't want to run if anything else is happening. |
| 146 | * Use with {@link #setThreadPriority(int)} and |
| 147 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 148 | * {@link java.lang.Thread} class. |
| 149 | */ |
| 150 | public static final int THREAD_PRIORITY_LOWEST = 19; |
| 151 | |
| 152 | /** |
| 153 | * Standard priority background threads. This gives your thread a slightly |
| 154 | * lower than normal priority, so that it will have less chance of impacting |
| 155 | * the responsiveness of the user interface. |
| 156 | * Use with {@link #setThreadPriority(int)} and |
| 157 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 158 | * {@link java.lang.Thread} class. |
| 159 | */ |
| 160 | public static final int THREAD_PRIORITY_BACKGROUND = 10; |
| 161 | |
| 162 | /** |
| 163 | * Standard priority of threads that are currently running a user interface |
| 164 | * that the user is interacting with. Applications can not normally |
| 165 | * change to this priority; the system will automatically adjust your |
| 166 | * application threads as the user moves through the UI. |
| 167 | * Use with {@link #setThreadPriority(int)} and |
| 168 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 169 | * {@link java.lang.Thread} class. |
| 170 | */ |
| 171 | public static final int THREAD_PRIORITY_FOREGROUND = -2; |
| 172 | |
| 173 | /** |
| 174 | * Standard priority of system display threads, involved in updating |
| 175 | * the user interface. Applications can not |
| 176 | * normally change to this priority. |
| 177 | * Use with {@link #setThreadPriority(int)} and |
| 178 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 179 | * {@link java.lang.Thread} class. |
| 180 | */ |
| 181 | public static final int THREAD_PRIORITY_DISPLAY = -4; |
| 182 | |
| 183 | /** |
| 184 | * Standard priority of the most important display threads, for compositing |
| 185 | * the screen and retrieving input events. Applications can not normally |
| 186 | * change to this priority. |
| 187 | * Use with {@link #setThreadPriority(int)} and |
| 188 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 189 | * {@link java.lang.Thread} class. |
| 190 | */ |
| 191 | public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8; |
| 192 | |
| 193 | /** |
| 194 | * Standard priority of audio threads. Applications can not normally |
| 195 | * change to this priority. |
| 196 | * Use with {@link #setThreadPriority(int)} and |
| 197 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 198 | * {@link java.lang.Thread} class. |
| 199 | */ |
| 200 | public static final int THREAD_PRIORITY_AUDIO = -16; |
| 201 | |
| 202 | /** |
| 203 | * Standard priority of the most important audio threads. |
| 204 | * Applications can not normally change to this priority. |
| 205 | * Use with {@link #setThreadPriority(int)} and |
| 206 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 207 | * {@link java.lang.Thread} class. |
| 208 | */ |
| 209 | public static final int THREAD_PRIORITY_URGENT_AUDIO = -19; |
| 210 | |
| 211 | /** |
| 212 | * Minimum increment to make a priority more favorable. |
| 213 | */ |
| 214 | public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1; |
| 215 | |
| 216 | /** |
| 217 | * Minimum increment to make a priority less favorable. |
| 218 | */ |
| 219 | public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1; |
| 220 | |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 221 | /** |
| 222 | * Default thread group - gets a 'normal' share of the CPU |
| 223 | * @hide |
| 224 | */ |
| 225 | public static final int THREAD_GROUP_DEFAULT = 0; |
| 226 | |
| 227 | /** |
| 228 | * Background non-interactive thread group - All threads in |
| 229 | * this group are scheduled with a reduced share of the CPU. |
| 230 | * @hide |
| 231 | */ |
| 232 | public static final int THREAD_GROUP_BG_NONINTERACTIVE = 1; |
| 233 | |
| 234 | /** |
| 235 | * Foreground 'boost' thread group - All threads in |
| 236 | * this group are scheduled with an increased share of the CPU |
| 237 | * @hide |
| 238 | **/ |
| 239 | public static final int THREAD_GROUP_FG_BOOST = 2; |
| 240 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | public static final int SIGNAL_QUIT = 3; |
| 242 | public static final int SIGNAL_KILL = 9; |
| 243 | public static final int SIGNAL_USR1 = 10; |
| 244 | |
| 245 | // State for communicating with zygote process |
| 246 | |
| 247 | static LocalSocket sZygoteSocket; |
| 248 | static DataInputStream sZygoteInputStream; |
| 249 | static BufferedWriter sZygoteWriter; |
| 250 | |
| 251 | /** true if previous zygote open failed */ |
| 252 | static boolean sPreviousZygoteOpenFailed; |
| 253 | |
| 254 | /** |
| 255 | * Start a new process. |
| 256 | * |
| 257 | * <p>If processes are enabled, a new process is created and the |
| 258 | * static main() function of a <var>processClass</var> is executed there. |
| 259 | * The process will continue running after this function returns. |
| 260 | * |
| 261 | * <p>If processes are not enabled, a new thread in the caller's |
| 262 | * process is created and main() of <var>processClass</var> called there. |
| 263 | * |
| 264 | * <p>The niceName parameter, if not an empty string, is a custom name to |
| 265 | * give to the process instead of using processClass. This allows you to |
| 266 | * make easily identifyable processes even if you are using the same base |
| 267 | * <var>processClass</var> to start them. |
| 268 | * |
| 269 | * @param processClass The class to use as the process's main entry |
| 270 | * point. |
| 271 | * @param niceName A more readable name to use for the process. |
| 272 | * @param uid The user-id under which the process will run. |
| 273 | * @param gid The group-id under which the process will run. |
| 274 | * @param gids Additional group-ids associated with the process. |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 275 | * @param debugFlags Additional flags. |
| 276 | * @param targetSdkVersion The target SDK version for the app. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | * @param zygoteArgs Additional arguments to supply to the zygote process. |
| 278 | * |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 279 | * @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] | 280 | * @throws RuntimeException on fatal start failure |
| 281 | * |
| 282 | * {@hide} |
| 283 | */ |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 284 | public static final ProcessStartResult start(final String processClass, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | final String niceName, |
| 286 | int uid, int gid, int[] gids, |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 287 | int debugFlags, int targetSdkVersion, |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 288 | String[] zygoteArgs) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 290 | return startViaZygote(processClass, niceName, uid, gid, gids, |
| 291 | debugFlags, targetSdkVersion, zygoteArgs); |
| 292 | } catch (ZygoteStartFailedEx ex) { |
| 293 | Log.e(LOG_TAG, |
| 294 | "Starting VM process through Zygote failed"); |
| 295 | throw new RuntimeException( |
| 296 | "Starting VM process through Zygote failed", ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /** retry interval for opening a zygote socket */ |
| 301 | static final int ZYGOTE_RETRY_MILLIS = 500; |
| 302 | |
| 303 | /** |
| 304 | * Tries to open socket to Zygote process if not already open. If |
| 305 | * already open, does nothing. May block and retry. |
| 306 | */ |
| 307 | private static void openZygoteSocketIfNeeded() |
| 308 | throws ZygoteStartFailedEx { |
| 309 | |
| 310 | int retryCount; |
| 311 | |
| 312 | if (sPreviousZygoteOpenFailed) { |
| 313 | /* |
| 314 | * If we've failed before, expect that we'll fail again and |
| 315 | * don't pause for retries. |
| 316 | */ |
| 317 | retryCount = 0; |
| 318 | } else { |
| 319 | retryCount = 10; |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * See bug #811181: Sometimes runtime can make it up before zygote. |
| 324 | * Really, we'd like to do something better to avoid this condition, |
| 325 | * but for now just wait a bit... |
| 326 | */ |
| 327 | for (int retry = 0 |
| 328 | ; (sZygoteSocket == null) && (retry < (retryCount + 1)) |
| 329 | ; retry++ ) { |
| 330 | |
| 331 | if (retry > 0) { |
| 332 | try { |
| 333 | Log.i("Zygote", "Zygote not up yet, sleeping..."); |
| 334 | Thread.sleep(ZYGOTE_RETRY_MILLIS); |
| 335 | } catch (InterruptedException ex) { |
| 336 | // should never happen |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | try { |
| 341 | sZygoteSocket = new LocalSocket(); |
| 342 | |
| 343 | sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET, |
| 344 | LocalSocketAddress.Namespace.RESERVED)); |
| 345 | |
| 346 | sZygoteInputStream |
| 347 | = new DataInputStream(sZygoteSocket.getInputStream()); |
| 348 | |
| 349 | sZygoteWriter = |
| 350 | new BufferedWriter( |
| 351 | new OutputStreamWriter( |
| 352 | sZygoteSocket.getOutputStream()), |
| 353 | 256); |
| 354 | |
| 355 | Log.i("Zygote", "Process: zygote socket opened"); |
| 356 | |
| 357 | sPreviousZygoteOpenFailed = false; |
| 358 | break; |
| 359 | } catch (IOException ex) { |
| 360 | if (sZygoteSocket != null) { |
| 361 | try { |
| 362 | sZygoteSocket.close(); |
| 363 | } catch (IOException ex2) { |
| 364 | Log.e(LOG_TAG,"I/O exception on close after exception", |
| 365 | ex2); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | sZygoteSocket = null; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | if (sZygoteSocket == null) { |
| 374 | sPreviousZygoteOpenFailed = true; |
| 375 | throw new ZygoteStartFailedEx("connect failed"); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Sends an argument list to the zygote process, which starts a new child |
| 381 | * and returns the child's pid. Please note: the present implementation |
| 382 | * replaces newlines in the argument list with spaces. |
| 383 | * @param args argument list |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 384 | * @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] | 385 | * @throws ZygoteStartFailedEx if process start failed for any reason |
| 386 | */ |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 387 | private static ProcessStartResult zygoteSendArgsAndGetResult(ArrayList<String> args) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | throws ZygoteStartFailedEx { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | openZygoteSocketIfNeeded(); |
| 390 | |
| 391 | try { |
| 392 | /** |
| 393 | * See com.android.internal.os.ZygoteInit.readArgumentList() |
| 394 | * Presently the wire format to the zygote process is: |
| 395 | * a) a count of arguments (argc, in essence) |
| 396 | * b) a number of newline-separated argument strings equal to count |
| 397 | * |
| 398 | * After the zygote process reads these it will write the pid of |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 399 | * the child or -1 on failure, followed by boolean to |
| 400 | * indicate whether a wrapper process was used. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | */ |
| 402 | |
| 403 | sZygoteWriter.write(Integer.toString(args.size())); |
| 404 | sZygoteWriter.newLine(); |
| 405 | |
| 406 | int sz = args.size(); |
| 407 | for (int i = 0; i < sz; i++) { |
| 408 | String arg = args.get(i); |
| 409 | if (arg.indexOf('\n') >= 0) { |
| 410 | throw new ZygoteStartFailedEx( |
| 411 | "embedded newlines not allowed"); |
| 412 | } |
| 413 | sZygoteWriter.write(arg); |
| 414 | sZygoteWriter.newLine(); |
| 415 | } |
| 416 | |
| 417 | sZygoteWriter.flush(); |
| 418 | |
| 419 | // Should there be a timeout on this? |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 420 | ProcessStartResult result = new ProcessStartResult(); |
| 421 | result.pid = sZygoteInputStream.readInt(); |
| 422 | if (result.pid < 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | throw new ZygoteStartFailedEx("fork() failed"); |
| 424 | } |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 425 | result.usingWrapper = sZygoteInputStream.readBoolean(); |
| 426 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | } catch (IOException ex) { |
| 428 | try { |
| 429 | if (sZygoteSocket != null) { |
| 430 | sZygoteSocket.close(); |
| 431 | } |
| 432 | } catch (IOException ex2) { |
| 433 | // we're going to fail anyway |
| 434 | Log.e(LOG_TAG,"I/O exception on routine close", ex2); |
| 435 | } |
| 436 | |
| 437 | sZygoteSocket = null; |
| 438 | |
| 439 | throw new ZygoteStartFailedEx(ex); |
| 440 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Starts a new process via the zygote mechanism. |
| 445 | * |
| 446 | * @param processClass Class name whose static main() to run |
| 447 | * @param niceName 'nice' process name to appear in ps |
| 448 | * @param uid a POSIX uid that the new process should setuid() to |
| 449 | * @param gid a POSIX gid that the new process shuold setgid() to |
| 450 | * @param gids null-ok; a list of supplementary group IDs that the |
| 451 | * new process should setgroup() to. |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 452 | * @param debugFlags Additional flags. |
| 453 | * @param targetSdkVersion The target SDK version for the app. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | * @param extraArgs Additional arguments to supply to the zygote process. |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 455 | * @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] | 456 | * @throws ZygoteStartFailedEx if process start failed for any reason |
| 457 | */ |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 458 | private static ProcessStartResult startViaZygote(final String processClass, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | final String niceName, |
| 460 | final int uid, final int gid, |
| 461 | final int[] gids, |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 462 | int debugFlags, int targetSdkVersion, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | String[] extraArgs) |
| 464 | throws ZygoteStartFailedEx { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | synchronized(Process.class) { |
| 466 | ArrayList<String> argsForZygote = new ArrayList<String>(); |
| 467 | |
| 468 | // --runtime-init, --setuid=, --setgid=, |
| 469 | // and --setgroups= must go first |
| 470 | argsForZygote.add("--runtime-init"); |
| 471 | argsForZygote.add("--setuid=" + uid); |
| 472 | argsForZygote.add("--setgid=" + gid); |
Elliott Hughes | ae07ecf | 2011-07-06 17:33:27 -0700 | [diff] [blame] | 473 | if ((debugFlags & Zygote.DEBUG_ENABLE_JNI_LOGGING) != 0) { |
| 474 | argsForZygote.add("--enable-jni-logging"); |
| 475 | } |
Ben Cheng | 23085b7 | 2010-02-08 16:06:32 -0800 | [diff] [blame] | 476 | if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) { |
| 477 | argsForZygote.add("--enable-safemode"); |
| 478 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) { |
| 480 | argsForZygote.add("--enable-debugger"); |
| 481 | } |
| 482 | if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) { |
| 483 | argsForZygote.add("--enable-checkjni"); |
| 484 | } |
| 485 | if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) { |
| 486 | argsForZygote.add("--enable-assert"); |
| 487 | } |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 488 | argsForZygote.add("--target-sdk-version=" + targetSdkVersion); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | |
| 490 | //TODO optionally enable debuger |
| 491 | //argsForZygote.add("--enable-debugger"); |
| 492 | |
| 493 | // --setgroups is a comma-separated list |
| 494 | if (gids != null && gids.length > 0) { |
| 495 | StringBuilder sb = new StringBuilder(); |
| 496 | sb.append("--setgroups="); |
| 497 | |
| 498 | int sz = gids.length; |
| 499 | for (int i = 0; i < sz; i++) { |
| 500 | if (i != 0) { |
| 501 | sb.append(','); |
| 502 | } |
| 503 | sb.append(gids[i]); |
| 504 | } |
| 505 | |
| 506 | argsForZygote.add(sb.toString()); |
| 507 | } |
| 508 | |
| 509 | if (niceName != null) { |
| 510 | argsForZygote.add("--nice-name=" + niceName); |
| 511 | } |
| 512 | |
| 513 | argsForZygote.add(processClass); |
| 514 | |
| 515 | if (extraArgs != null) { |
| 516 | for (String arg : extraArgs) { |
| 517 | argsForZygote.add(arg); |
| 518 | } |
| 519 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 521 | return zygoteSendArgsAndGetResult(argsForZygote); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Returns elapsed milliseconds of the time this process has run. |
| 527 | * @return Returns the number of milliseconds this process has return. |
| 528 | */ |
| 529 | public static final native long getElapsedCpuTime(); |
| 530 | |
| 531 | /** |
| 532 | * Returns the identifier of this process, which can be used with |
| 533 | * {@link #killProcess} and {@link #sendSignal}. |
| 534 | */ |
| 535 | public static final native int myPid(); |
| 536 | |
| 537 | /** |
| 538 | * Returns the identifier of the calling thread, which be used with |
| 539 | * {@link #setThreadPriority(int, int)}. |
| 540 | */ |
| 541 | public static final native int myTid(); |
| 542 | |
| 543 | /** |
| 544 | * Returns the identifier of this process's user. |
| 545 | */ |
| 546 | public static final native int myUid(); |
| 547 | |
| 548 | /** |
| 549 | * Returns the UID assigned to a particular user name, or -1 if there is |
| 550 | * none. If the given string consists of only numbers, it is converted |
| 551 | * directly to a uid. |
| 552 | */ |
| 553 | public static final native int getUidForName(String name); |
| 554 | |
| 555 | /** |
| 556 | * Returns the GID assigned to a particular user name, or -1 if there is |
| 557 | * none. If the given string consists of only numbers, it is converted |
| 558 | * directly to a gid. |
| 559 | */ |
| 560 | public static final native int getGidForName(String name); |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 561 | |
| 562 | /** |
| 563 | * Returns a uid for a currently running process. |
| 564 | * @param pid the process id |
| 565 | * @return the uid of the process, or -1 if the process is not running. |
| 566 | * @hide pending API council review |
| 567 | */ |
| 568 | public static final int getUidForPid(int pid) { |
| 569 | String[] procStatusLabels = { "Uid:" }; |
| 570 | long[] procStatusValues = new long[1]; |
| 571 | procStatusValues[0] = -1; |
| 572 | Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues); |
| 573 | return (int) procStatusValues[0]; |
| 574 | } |
| 575 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | /** |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 577 | * Returns the parent process id for a currently running process. |
| 578 | * @param pid the process id |
| 579 | * @return the parent process id of the process, or -1 if the process is not running. |
| 580 | * @hide |
| 581 | */ |
| 582 | public static final int getParentPid(int pid) { |
| 583 | String[] procStatusLabels = { "PPid:" }; |
| 584 | long[] procStatusValues = new long[1]; |
| 585 | procStatusValues[0] = -1; |
| 586 | Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues); |
| 587 | return (int) procStatusValues[0]; |
| 588 | } |
| 589 | |
| 590 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | * Set the priority of a thread, based on Linux priorities. |
| 592 | * |
| 593 | * @param tid The identifier of the thread/process to change. |
| 594 | * @param priority A Linux priority level, from -20 for highest scheduling |
| 595 | * priority to 19 for lowest scheduling priority. |
| 596 | * |
| 597 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 598 | * <var>tid</var> does not exist. |
| 599 | * @throws SecurityException Throws SecurityException if your process does |
| 600 | * not have permission to modify the given thread, or to use the given |
| 601 | * priority. |
| 602 | */ |
| 603 | public static final native void setThreadPriority(int tid, int priority) |
| 604 | throws IllegalArgumentException, SecurityException; |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 605 | |
| 606 | /** |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 607 | * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to |
| 608 | * throw an exception if passed a background-level thread priority. This is only |
| 609 | * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1. |
| 610 | * |
| 611 | * @hide |
| 612 | */ |
| 613 | public static final native void setCanSelfBackground(boolean backgroundOk); |
| 614 | |
| 615 | /** |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 616 | * Sets the scheduling group for a thread. |
| 617 | * @hide |
| 618 | * @param tid The indentifier of the thread/process to change. |
| 619 | * @param group The target group for this thread/process. |
| 620 | * |
| 621 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 622 | * <var>tid</var> does not exist. |
| 623 | * @throws SecurityException Throws SecurityException if your process does |
| 624 | * not have permission to modify the given thread, or to use the given |
| 625 | * priority. |
| 626 | */ |
| 627 | public static final native void setThreadGroup(int tid, int group) |
| 628 | throws IllegalArgumentException, SecurityException; |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 629 | /** |
| 630 | * Sets the scheduling group for a process and all child threads |
| 631 | * @hide |
| 632 | * @param pid The indentifier of the process to change. |
| 633 | * @param group The target group for this process. |
| 634 | * |
| 635 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 636 | * <var>tid</var> does not exist. |
| 637 | * @throws SecurityException Throws SecurityException if your process does |
| 638 | * not have permission to modify the given thread, or to use the given |
| 639 | * priority. |
| 640 | */ |
| 641 | public static final native void setProcessGroup(int pid, int group) |
| 642 | throws IllegalArgumentException, SecurityException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | |
| 644 | /** |
| 645 | * Set the priority of the calling thread, based on Linux priorities. See |
| 646 | * {@link #setThreadPriority(int, int)} for more information. |
| 647 | * |
| 648 | * @param priority A Linux priority level, from -20 for highest scheduling |
| 649 | * priority to 19 for lowest scheduling priority. |
| 650 | * |
| 651 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 652 | * <var>tid</var> does not exist. |
| 653 | * @throws SecurityException Throws SecurityException if your process does |
| 654 | * not have permission to modify the given thread, or to use the given |
| 655 | * priority. |
| 656 | * |
| 657 | * @see #setThreadPriority(int, int) |
| 658 | */ |
| 659 | public static final native void setThreadPriority(int priority) |
| 660 | throws IllegalArgumentException, SecurityException; |
| 661 | |
| 662 | /** |
| 663 | * Return the current priority of a thread, based on Linux priorities. |
| 664 | * |
| 665 | * @param tid The identifier of the thread/process to change. |
| 666 | * |
| 667 | * @return Returns the current priority, as a Linux priority level, |
| 668 | * from -20 for highest scheduling priority to 19 for lowest scheduling |
| 669 | * priority. |
| 670 | * |
| 671 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 672 | * <var>tid</var> does not exist. |
| 673 | */ |
| 674 | public static final native int getThreadPriority(int tid) |
| 675 | throws IllegalArgumentException; |
| 676 | |
| 677 | /** |
| 678 | * Determine whether the current environment supports multiple processes. |
| 679 | * |
| 680 | * @return Returns true if the system can run in multiple processes, else |
| 681 | * false if everything is running in a single process. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 682 | * |
| 683 | * @deprecated This method always returns true. Do not use. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | */ |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 685 | @Deprecated |
| 686 | public static final boolean supportsProcesses() { |
| 687 | return true; |
| 688 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 689 | |
| 690 | /** |
| 691 | * Set the out-of-memory badness adjustment for a process. |
| 692 | * |
| 693 | * @param pid The process identifier to set. |
| 694 | * @param amt Adjustment value -- linux allows -16 to +15. |
| 695 | * |
| 696 | * @return Returns true if the underlying system supports this |
| 697 | * feature, else false. |
| 698 | * |
| 699 | * {@hide} |
| 700 | */ |
| 701 | public static final native boolean setOomAdj(int pid, int amt); |
| 702 | |
| 703 | /** |
| 704 | * Change this process's argv[0] parameter. This can be useful to show |
| 705 | * more descriptive information in things like the 'ps' command. |
| 706 | * |
| 707 | * @param text The new name of this process. |
| 708 | * |
| 709 | * {@hide} |
| 710 | */ |
| 711 | public static final native void setArgV0(String text); |
| 712 | |
| 713 | /** |
| 714 | * Kill the process with the given PID. |
| 715 | * Note that, though this API allows us to request to |
| 716 | * kill any process based on its PID, the kernel will |
| 717 | * still impose standard restrictions on which PIDs you |
| 718 | * are actually able to kill. Typically this means only |
| 719 | * the process running the caller's packages/application |
| 720 | * and any additional processes created by that app; packages |
| 721 | * sharing a common UID will also be able to kill each |
| 722 | * other's processes. |
| 723 | */ |
| 724 | public static final void killProcess(int pid) { |
| 725 | sendSignal(pid, SIGNAL_KILL); |
| 726 | } |
| 727 | |
| 728 | /** @hide */ |
| 729 | public static final native int setUid(int uid); |
| 730 | |
| 731 | /** @hide */ |
| 732 | public static final native int setGid(int uid); |
| 733 | |
| 734 | /** |
| 735 | * Send a signal to the given process. |
| 736 | * |
| 737 | * @param pid The pid of the target process. |
| 738 | * @param signal The signal to send. |
| 739 | */ |
| 740 | public static final native void sendSignal(int pid, int signal); |
| 741 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 742 | /** |
| 743 | * @hide |
| 744 | * Private impl for avoiding a log message... DO NOT USE without doing |
| 745 | * your own log, or the Android Illuminati will find you some night and |
| 746 | * beat you up. |
| 747 | */ |
| 748 | public static final void killProcessQuiet(int pid) { |
| 749 | sendSignalQuiet(pid, SIGNAL_KILL); |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * @hide |
| 754 | * Private impl for avoiding a log message... DO NOT USE without doing |
| 755 | * your own log, or the Android Illuminati will find you some night and |
| 756 | * beat you up. |
| 757 | */ |
| 758 | public static final native void sendSignalQuiet(int pid, int signal); |
| 759 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 760 | /** @hide */ |
Marco Nelissen | 0bca96b | 2009-07-17 12:59:25 -0700 | [diff] [blame] | 761 | public static final native long getFreeMemory(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 762 | |
| 763 | /** @hide */ |
| 764 | public static final native void readProcLines(String path, |
| 765 | String[] reqFields, long[] outSizes); |
| 766 | |
| 767 | /** @hide */ |
| 768 | public static final native int[] getPids(String path, int[] lastArray); |
| 769 | |
| 770 | /** @hide */ |
| 771 | public static final int PROC_TERM_MASK = 0xff; |
| 772 | /** @hide */ |
| 773 | public static final int PROC_ZERO_TERM = 0; |
| 774 | /** @hide */ |
| 775 | public static final int PROC_SPACE_TERM = (int)' '; |
| 776 | /** @hide */ |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 777 | public static final int PROC_TAB_TERM = (int)'\t'; |
| 778 | /** @hide */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 779 | public static final int PROC_COMBINE = 0x100; |
| 780 | /** @hide */ |
| 781 | public static final int PROC_PARENS = 0x200; |
| 782 | /** @hide */ |
| 783 | public static final int PROC_OUT_STRING = 0x1000; |
| 784 | /** @hide */ |
| 785 | public static final int PROC_OUT_LONG = 0x2000; |
| 786 | /** @hide */ |
| 787 | public static final int PROC_OUT_FLOAT = 0x4000; |
| 788 | |
| 789 | /** @hide */ |
| 790 | public static final native boolean readProcFile(String file, int[] format, |
| 791 | String[] outStrings, long[] outLongs, float[] outFloats); |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 792 | |
| 793 | /** @hide */ |
| 794 | public static final native boolean parseProcLine(byte[] buffer, int startIndex, |
| 795 | 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] | 796 | |
| 797 | /** |
| 798 | * Gets the total Pss value for a given process, in bytes. |
| 799 | * |
| 800 | * @param pid the process to the Pss for |
| 801 | * @return the total Pss value for the given process in bytes, |
| 802 | * or -1 if the value cannot be determined |
| 803 | * @hide |
| 804 | */ |
| 805 | public static final native long getPss(int pid); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 806 | |
| 807 | /** |
| 808 | * Specifies the outcome of having started a process. |
| 809 | * @hide |
| 810 | */ |
| 811 | public static final class ProcessStartResult { |
| 812 | /** |
| 813 | * The PID of the newly started process. |
| 814 | * Always >= 0. (If the start failed, an exception will have been thrown instead.) |
| 815 | */ |
| 816 | public int pid; |
| 817 | |
| 818 | /** |
| 819 | * True if the process was started with a wrapper attached. |
| 820 | */ |
| 821 | public boolean usingWrapper; |
| 822 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 823 | } |