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