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 | /** |
Brian Carlstrom | 8c2a1a9 | 2011-04-08 13:44:08 -0700 | [diff] [blame] | 95 | * Defines the UID for the KeyChain service. |
| 96 | * @hide |
| 97 | */ |
| 98 | public static final int KEYCHAIN_UID = 1020; |
| 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 | */ |
Jeff Hamilton | 84d3407 | 2011-04-01 13:43:28 -0500 | [diff] [blame] | 104 | public static final int NFC_UID = 1025; |
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. |
| 275 | * @param enableDebugger True if debugging should be enabled for this process. |
| 276 | * @param zygoteArgs Additional arguments to supply to the zygote process. |
| 277 | * |
| 278 | * @return int If > 0 the pid of the new process; if 0 the process is |
| 279 | * being emulated by a thread |
| 280 | * @throws RuntimeException on fatal start failure |
| 281 | * |
| 282 | * {@hide} |
| 283 | */ |
| 284 | public static final int start(final String processClass, |
| 285 | final String niceName, |
| 286 | int uid, int gid, int[] gids, |
| 287 | int debugFlags, |
| 288 | String[] zygoteArgs) |
| 289 | { |
| 290 | if (supportsProcesses()) { |
| 291 | try { |
| 292 | return startViaZygote(processClass, niceName, uid, gid, gids, |
| 293 | debugFlags, zygoteArgs); |
| 294 | } catch (ZygoteStartFailedEx ex) { |
| 295 | Log.e(LOG_TAG, |
| 296 | "Starting VM process through Zygote failed"); |
| 297 | throw new RuntimeException( |
| 298 | "Starting VM process through Zygote failed", ex); |
| 299 | } |
| 300 | } else { |
| 301 | // Running in single-process mode |
| 302 | |
| 303 | Runnable runnable = new Runnable() { |
| 304 | public void run() { |
| 305 | Process.invokeStaticMain(processClass); |
| 306 | } |
| 307 | }; |
| 308 | |
| 309 | // Thread constructors must not be called with null names (see spec). |
| 310 | if (niceName != null) { |
| 311 | new Thread(runnable, niceName).start(); |
| 312 | } else { |
| 313 | new Thread(runnable).start(); |
| 314 | } |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Start a new process. Don't supply a custom nice name. |
| 322 | * {@hide} |
| 323 | */ |
| 324 | public static final int start(String processClass, int uid, int gid, |
| 325 | int[] gids, int debugFlags, String[] zygoteArgs) { |
| 326 | return start(processClass, "", uid, gid, gids, |
| 327 | debugFlags, zygoteArgs); |
| 328 | } |
| 329 | |
| 330 | private static void invokeStaticMain(String className) { |
| 331 | Class cl; |
| 332 | Object args[] = new Object[1]; |
| 333 | |
| 334 | args[0] = new String[0]; //this is argv |
| 335 | |
| 336 | try { |
| 337 | cl = Class.forName(className); |
| 338 | cl.getMethod("main", new Class[] { String[].class }) |
| 339 | .invoke(null, args); |
| 340 | } catch (Exception ex) { |
| 341 | // can be: ClassNotFoundException, |
| 342 | // NoSuchMethodException, SecurityException, |
| 343 | // IllegalAccessException, IllegalArgumentException |
| 344 | // InvocationTargetException |
| 345 | // or uncaught exception from main() |
| 346 | |
| 347 | Log.e(LOG_TAG, "Exception invoking static main on " |
| 348 | + className, ex); |
| 349 | |
| 350 | throw new RuntimeException(ex); |
| 351 | } |
| 352 | |
| 353 | } |
| 354 | |
| 355 | /** retry interval for opening a zygote socket */ |
| 356 | static final int ZYGOTE_RETRY_MILLIS = 500; |
| 357 | |
| 358 | /** |
| 359 | * Tries to open socket to Zygote process if not already open. If |
| 360 | * already open, does nothing. May block and retry. |
| 361 | */ |
| 362 | private static void openZygoteSocketIfNeeded() |
| 363 | throws ZygoteStartFailedEx { |
| 364 | |
| 365 | int retryCount; |
| 366 | |
| 367 | if (sPreviousZygoteOpenFailed) { |
| 368 | /* |
| 369 | * If we've failed before, expect that we'll fail again and |
| 370 | * don't pause for retries. |
| 371 | */ |
| 372 | retryCount = 0; |
| 373 | } else { |
| 374 | retryCount = 10; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * See bug #811181: Sometimes runtime can make it up before zygote. |
| 379 | * Really, we'd like to do something better to avoid this condition, |
| 380 | * but for now just wait a bit... |
| 381 | */ |
| 382 | for (int retry = 0 |
| 383 | ; (sZygoteSocket == null) && (retry < (retryCount + 1)) |
| 384 | ; retry++ ) { |
| 385 | |
| 386 | if (retry > 0) { |
| 387 | try { |
| 388 | Log.i("Zygote", "Zygote not up yet, sleeping..."); |
| 389 | Thread.sleep(ZYGOTE_RETRY_MILLIS); |
| 390 | } catch (InterruptedException ex) { |
| 391 | // should never happen |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | try { |
| 396 | sZygoteSocket = new LocalSocket(); |
| 397 | |
| 398 | sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET, |
| 399 | LocalSocketAddress.Namespace.RESERVED)); |
| 400 | |
| 401 | sZygoteInputStream |
| 402 | = new DataInputStream(sZygoteSocket.getInputStream()); |
| 403 | |
| 404 | sZygoteWriter = |
| 405 | new BufferedWriter( |
| 406 | new OutputStreamWriter( |
| 407 | sZygoteSocket.getOutputStream()), |
| 408 | 256); |
| 409 | |
| 410 | Log.i("Zygote", "Process: zygote socket opened"); |
| 411 | |
| 412 | sPreviousZygoteOpenFailed = false; |
| 413 | break; |
| 414 | } catch (IOException ex) { |
| 415 | if (sZygoteSocket != null) { |
| 416 | try { |
| 417 | sZygoteSocket.close(); |
| 418 | } catch (IOException ex2) { |
| 419 | Log.e(LOG_TAG,"I/O exception on close after exception", |
| 420 | ex2); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | sZygoteSocket = null; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | if (sZygoteSocket == null) { |
| 429 | sPreviousZygoteOpenFailed = true; |
| 430 | throw new ZygoteStartFailedEx("connect failed"); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Sends an argument list to the zygote process, which starts a new child |
| 436 | * and returns the child's pid. Please note: the present implementation |
| 437 | * replaces newlines in the argument list with spaces. |
| 438 | * @param args argument list |
| 439 | * @return PID of new child process |
| 440 | * @throws ZygoteStartFailedEx if process start failed for any reason |
| 441 | */ |
| 442 | private static int zygoteSendArgsAndGetPid(ArrayList<String> args) |
| 443 | throws ZygoteStartFailedEx { |
| 444 | |
| 445 | int pid; |
| 446 | |
| 447 | openZygoteSocketIfNeeded(); |
| 448 | |
| 449 | try { |
| 450 | /** |
| 451 | * See com.android.internal.os.ZygoteInit.readArgumentList() |
| 452 | * Presently the wire format to the zygote process is: |
| 453 | * a) a count of arguments (argc, in essence) |
| 454 | * b) a number of newline-separated argument strings equal to count |
| 455 | * |
| 456 | * After the zygote process reads these it will write the pid of |
| 457 | * the child or -1 on failure. |
| 458 | */ |
| 459 | |
| 460 | sZygoteWriter.write(Integer.toString(args.size())); |
| 461 | sZygoteWriter.newLine(); |
| 462 | |
| 463 | int sz = args.size(); |
| 464 | for (int i = 0; i < sz; i++) { |
| 465 | String arg = args.get(i); |
| 466 | if (arg.indexOf('\n') >= 0) { |
| 467 | throw new ZygoteStartFailedEx( |
| 468 | "embedded newlines not allowed"); |
| 469 | } |
| 470 | sZygoteWriter.write(arg); |
| 471 | sZygoteWriter.newLine(); |
| 472 | } |
| 473 | |
| 474 | sZygoteWriter.flush(); |
| 475 | |
| 476 | // Should there be a timeout on this? |
| 477 | pid = sZygoteInputStream.readInt(); |
| 478 | |
| 479 | if (pid < 0) { |
| 480 | throw new ZygoteStartFailedEx("fork() failed"); |
| 481 | } |
| 482 | } catch (IOException ex) { |
| 483 | try { |
| 484 | if (sZygoteSocket != null) { |
| 485 | sZygoteSocket.close(); |
| 486 | } |
| 487 | } catch (IOException ex2) { |
| 488 | // we're going to fail anyway |
| 489 | Log.e(LOG_TAG,"I/O exception on routine close", ex2); |
| 490 | } |
| 491 | |
| 492 | sZygoteSocket = null; |
| 493 | |
| 494 | throw new ZygoteStartFailedEx(ex); |
| 495 | } |
| 496 | |
| 497 | return pid; |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Starts a new process via the zygote mechanism. |
| 502 | * |
| 503 | * @param processClass Class name whose static main() to run |
| 504 | * @param niceName 'nice' process name to appear in ps |
| 505 | * @param uid a POSIX uid that the new process should setuid() to |
| 506 | * @param gid a POSIX gid that the new process shuold setgid() to |
| 507 | * @param gids null-ok; a list of supplementary group IDs that the |
| 508 | * new process should setgroup() to. |
| 509 | * @param enableDebugger True if debugging should be enabled for this process. |
| 510 | * @param extraArgs Additional arguments to supply to the zygote process. |
| 511 | * @return PID |
| 512 | * @throws ZygoteStartFailedEx if process start failed for any reason |
| 513 | */ |
| 514 | private static int startViaZygote(final String processClass, |
| 515 | final String niceName, |
| 516 | final int uid, final int gid, |
| 517 | final int[] gids, |
| 518 | int debugFlags, |
| 519 | String[] extraArgs) |
| 520 | throws ZygoteStartFailedEx { |
| 521 | int pid; |
| 522 | |
| 523 | synchronized(Process.class) { |
| 524 | ArrayList<String> argsForZygote = new ArrayList<String>(); |
| 525 | |
| 526 | // --runtime-init, --setuid=, --setgid=, |
| 527 | // and --setgroups= must go first |
| 528 | argsForZygote.add("--runtime-init"); |
| 529 | argsForZygote.add("--setuid=" + uid); |
| 530 | argsForZygote.add("--setgid=" + gid); |
Ben Cheng | 23085b7 | 2010-02-08 16:06:32 -0800 | [diff] [blame] | 531 | if ((debugFlags & Zygote.DEBUG_ENABLE_SAFEMODE) != 0) { |
| 532 | argsForZygote.add("--enable-safemode"); |
| 533 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | if ((debugFlags & Zygote.DEBUG_ENABLE_DEBUGGER) != 0) { |
| 535 | argsForZygote.add("--enable-debugger"); |
| 536 | } |
| 537 | if ((debugFlags & Zygote.DEBUG_ENABLE_CHECKJNI) != 0) { |
| 538 | argsForZygote.add("--enable-checkjni"); |
| 539 | } |
| 540 | if ((debugFlags & Zygote.DEBUG_ENABLE_ASSERT) != 0) { |
| 541 | argsForZygote.add("--enable-assert"); |
| 542 | } |
| 543 | |
| 544 | //TODO optionally enable debuger |
| 545 | //argsForZygote.add("--enable-debugger"); |
| 546 | |
| 547 | // --setgroups is a comma-separated list |
| 548 | if (gids != null && gids.length > 0) { |
| 549 | StringBuilder sb = new StringBuilder(); |
| 550 | sb.append("--setgroups="); |
| 551 | |
| 552 | int sz = gids.length; |
| 553 | for (int i = 0; i < sz; i++) { |
| 554 | if (i != 0) { |
| 555 | sb.append(','); |
| 556 | } |
| 557 | sb.append(gids[i]); |
| 558 | } |
| 559 | |
| 560 | argsForZygote.add(sb.toString()); |
| 561 | } |
| 562 | |
| 563 | if (niceName != null) { |
| 564 | argsForZygote.add("--nice-name=" + niceName); |
| 565 | } |
| 566 | |
| 567 | argsForZygote.add(processClass); |
| 568 | |
| 569 | if (extraArgs != null) { |
| 570 | for (String arg : extraArgs) { |
| 571 | argsForZygote.add(arg); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | pid = zygoteSendArgsAndGetPid(argsForZygote); |
| 576 | } |
| 577 | |
| 578 | if (pid <= 0) { |
| 579 | throw new ZygoteStartFailedEx("zygote start failed:" + pid); |
| 580 | } |
| 581 | |
| 582 | return pid; |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Returns elapsed milliseconds of the time this process has run. |
| 587 | * @return Returns the number of milliseconds this process has return. |
| 588 | */ |
| 589 | public static final native long getElapsedCpuTime(); |
| 590 | |
| 591 | /** |
| 592 | * Returns the identifier of this process, which can be used with |
| 593 | * {@link #killProcess} and {@link #sendSignal}. |
| 594 | */ |
| 595 | public static final native int myPid(); |
| 596 | |
| 597 | /** |
| 598 | * Returns the identifier of the calling thread, which be used with |
| 599 | * {@link #setThreadPriority(int, int)}. |
| 600 | */ |
| 601 | public static final native int myTid(); |
| 602 | |
| 603 | /** |
| 604 | * Returns the identifier of this process's user. |
| 605 | */ |
| 606 | public static final native int myUid(); |
| 607 | |
| 608 | /** |
| 609 | * Returns the UID assigned to a particular user name, or -1 if there is |
| 610 | * none. If the given string consists of only numbers, it is converted |
| 611 | * directly to a uid. |
| 612 | */ |
| 613 | public static final native int getUidForName(String name); |
| 614 | |
| 615 | /** |
| 616 | * Returns the GID assigned to a particular user name, or -1 if there is |
| 617 | * none. If the given string consists of only numbers, it is converted |
| 618 | * directly to a gid. |
| 619 | */ |
| 620 | public static final native int getGidForName(String name); |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 621 | |
| 622 | /** |
| 623 | * Returns a uid for a currently running process. |
| 624 | * @param pid the process id |
| 625 | * @return the uid of the process, or -1 if the process is not running. |
| 626 | * @hide pending API council review |
| 627 | */ |
| 628 | public static final int getUidForPid(int pid) { |
| 629 | String[] procStatusLabels = { "Uid:" }; |
| 630 | long[] procStatusValues = new long[1]; |
| 631 | procStatusValues[0] = -1; |
| 632 | Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues); |
| 633 | return (int) procStatusValues[0]; |
| 634 | } |
| 635 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 636 | /** |
| 637 | * Set the priority of a thread, based on Linux priorities. |
| 638 | * |
| 639 | * @param tid The identifier of the thread/process to change. |
| 640 | * @param priority A Linux priority level, from -20 for highest scheduling |
| 641 | * priority to 19 for lowest scheduling priority. |
| 642 | * |
| 643 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 644 | * <var>tid</var> does not exist. |
| 645 | * @throws SecurityException Throws SecurityException if your process does |
| 646 | * not have permission to modify the given thread, or to use the given |
| 647 | * priority. |
| 648 | */ |
| 649 | public static final native void setThreadPriority(int tid, int priority) |
| 650 | throws IllegalArgumentException, SecurityException; |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 651 | |
| 652 | /** |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 653 | * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to |
| 654 | * throw an exception if passed a background-level thread priority. This is only |
| 655 | * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1. |
| 656 | * |
| 657 | * @hide |
| 658 | */ |
| 659 | public static final native void setCanSelfBackground(boolean backgroundOk); |
| 660 | |
| 661 | /** |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 662 | * Sets the scheduling group for a thread. |
| 663 | * @hide |
| 664 | * @param tid The indentifier of the thread/process to change. |
| 665 | * @param group The target group for this thread/process. |
| 666 | * |
| 667 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 668 | * <var>tid</var> does not exist. |
| 669 | * @throws SecurityException Throws SecurityException if your process does |
| 670 | * not have permission to modify the given thread, or to use the given |
| 671 | * priority. |
| 672 | */ |
| 673 | public static final native void setThreadGroup(int tid, int group) |
| 674 | throws IllegalArgumentException, SecurityException; |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 675 | /** |
| 676 | * Sets the scheduling group for a process and all child threads |
| 677 | * @hide |
| 678 | * @param pid The indentifier of the process to change. |
| 679 | * @param group The target group for this process. |
| 680 | * |
| 681 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 682 | * <var>tid</var> does not exist. |
| 683 | * @throws SecurityException Throws SecurityException if your process does |
| 684 | * not have permission to modify the given thread, or to use the given |
| 685 | * priority. |
| 686 | */ |
| 687 | public static final native void setProcessGroup(int pid, int group) |
| 688 | throws IllegalArgumentException, SecurityException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 689 | |
| 690 | /** |
| 691 | * Set the priority of the calling thread, based on Linux priorities. See |
| 692 | * {@link #setThreadPriority(int, int)} for more information. |
| 693 | * |
| 694 | * @param priority A Linux priority level, from -20 for highest scheduling |
| 695 | * priority to 19 for lowest scheduling priority. |
| 696 | * |
| 697 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 698 | * <var>tid</var> does not exist. |
| 699 | * @throws SecurityException Throws SecurityException if your process does |
| 700 | * not have permission to modify the given thread, or to use the given |
| 701 | * priority. |
| 702 | * |
| 703 | * @see #setThreadPriority(int, int) |
| 704 | */ |
| 705 | public static final native void setThreadPriority(int priority) |
| 706 | throws IllegalArgumentException, SecurityException; |
| 707 | |
| 708 | /** |
| 709 | * Return the current priority of a thread, based on Linux priorities. |
| 710 | * |
| 711 | * @param tid The identifier of the thread/process to change. |
| 712 | * |
| 713 | * @return Returns the current priority, as a Linux priority level, |
| 714 | * from -20 for highest scheduling priority to 19 for lowest scheduling |
| 715 | * priority. |
| 716 | * |
| 717 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 718 | * <var>tid</var> does not exist. |
| 719 | */ |
| 720 | public static final native int getThreadPriority(int tid) |
| 721 | throws IllegalArgumentException; |
| 722 | |
| 723 | /** |
| 724 | * Determine whether the current environment supports multiple processes. |
| 725 | * |
| 726 | * @return Returns true if the system can run in multiple processes, else |
| 727 | * false if everything is running in a single process. |
| 728 | */ |
| 729 | public static final native boolean supportsProcesses(); |
| 730 | |
| 731 | /** |
| 732 | * Set the out-of-memory badness adjustment for a process. |
| 733 | * |
| 734 | * @param pid The process identifier to set. |
| 735 | * @param amt Adjustment value -- linux allows -16 to +15. |
| 736 | * |
| 737 | * @return Returns true if the underlying system supports this |
| 738 | * feature, else false. |
| 739 | * |
| 740 | * {@hide} |
| 741 | */ |
| 742 | public static final native boolean setOomAdj(int pid, int amt); |
| 743 | |
| 744 | /** |
| 745 | * Change this process's argv[0] parameter. This can be useful to show |
| 746 | * more descriptive information in things like the 'ps' command. |
| 747 | * |
| 748 | * @param text The new name of this process. |
| 749 | * |
| 750 | * {@hide} |
| 751 | */ |
| 752 | public static final native void setArgV0(String text); |
| 753 | |
| 754 | /** |
| 755 | * Kill the process with the given PID. |
| 756 | * Note that, though this API allows us to request to |
| 757 | * kill any process based on its PID, the kernel will |
| 758 | * still impose standard restrictions on which PIDs you |
| 759 | * are actually able to kill. Typically this means only |
| 760 | * the process running the caller's packages/application |
| 761 | * and any additional processes created by that app; packages |
| 762 | * sharing a common UID will also be able to kill each |
| 763 | * other's processes. |
| 764 | */ |
| 765 | public static final void killProcess(int pid) { |
| 766 | sendSignal(pid, SIGNAL_KILL); |
| 767 | } |
| 768 | |
| 769 | /** @hide */ |
| 770 | public static final native int setUid(int uid); |
| 771 | |
| 772 | /** @hide */ |
| 773 | public static final native int setGid(int uid); |
| 774 | |
| 775 | /** |
| 776 | * Send a signal to the given process. |
| 777 | * |
| 778 | * @param pid The pid of the target process. |
| 779 | * @param signal The signal to send. |
| 780 | */ |
| 781 | public static final native void sendSignal(int pid, int signal); |
| 782 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 783 | /** |
| 784 | * @hide |
| 785 | * Private impl for avoiding a log message... DO NOT USE without doing |
| 786 | * your own log, or the Android Illuminati will find you some night and |
| 787 | * beat you up. |
| 788 | */ |
| 789 | public static final void killProcessQuiet(int pid) { |
| 790 | sendSignalQuiet(pid, SIGNAL_KILL); |
| 791 | } |
| 792 | |
| 793 | /** |
| 794 | * @hide |
| 795 | * Private impl for avoiding a log message... DO NOT USE without doing |
| 796 | * your own log, or the Android Illuminati will find you some night and |
| 797 | * beat you up. |
| 798 | */ |
| 799 | public static final native void sendSignalQuiet(int pid, int signal); |
| 800 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | /** @hide */ |
Marco Nelissen | 0bca96b | 2009-07-17 12:59:25 -0700 | [diff] [blame] | 802 | public static final native long getFreeMemory(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 803 | |
| 804 | /** @hide */ |
| 805 | public static final native void readProcLines(String path, |
| 806 | String[] reqFields, long[] outSizes); |
| 807 | |
| 808 | /** @hide */ |
| 809 | public static final native int[] getPids(String path, int[] lastArray); |
| 810 | |
| 811 | /** @hide */ |
| 812 | public static final int PROC_TERM_MASK = 0xff; |
| 813 | /** @hide */ |
| 814 | public static final int PROC_ZERO_TERM = 0; |
| 815 | /** @hide */ |
| 816 | public static final int PROC_SPACE_TERM = (int)' '; |
| 817 | /** @hide */ |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 818 | public static final int PROC_TAB_TERM = (int)'\t'; |
| 819 | /** @hide */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 820 | public static final int PROC_COMBINE = 0x100; |
| 821 | /** @hide */ |
| 822 | public static final int PROC_PARENS = 0x200; |
| 823 | /** @hide */ |
| 824 | public static final int PROC_OUT_STRING = 0x1000; |
| 825 | /** @hide */ |
| 826 | public static final int PROC_OUT_LONG = 0x2000; |
| 827 | /** @hide */ |
| 828 | public static final int PROC_OUT_FLOAT = 0x4000; |
| 829 | |
| 830 | /** @hide */ |
| 831 | public static final native boolean readProcFile(String file, int[] format, |
| 832 | String[] outStrings, long[] outLongs, float[] outFloats); |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 833 | |
| 834 | /** @hide */ |
| 835 | public static final native boolean parseProcLine(byte[] buffer, int startIndex, |
| 836 | 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] | 837 | |
| 838 | /** |
| 839 | * Gets the total Pss value for a given process, in bytes. |
| 840 | * |
| 841 | * @param pid the process to the Pss for |
| 842 | * @return the total Pss value for the given process in bytes, |
| 843 | * or -1 if the value cannot be determined |
| 844 | * @hide |
| 845 | */ |
| 846 | public static final native long getPss(int pid); |
| 847 | } |