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 | |
Srinath Sridharan | 1b15d13 | 2016-07-19 15:16:11 -0700 | [diff] [blame] | 19 | import android.annotation.TestApi; |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 20 | import android.system.Os; |
Tim Murray | b89970e | 2016-07-01 10:42:20 -0700 | [diff] [blame] | 21 | import android.system.OsConstants; |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 22 | import android.webkit.WebViewZygote; |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 23 | |
Andreas Gampe | ee0cdce | 2015-06-08 20:47:50 -0700 | [diff] [blame] | 24 | import dalvik.system.VMRuntime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Tools for managing OS processes. |
| 28 | */ |
| 29 | public class Process { |
| 30 | private static final String LOG_TAG = "Process"; |
| 31 | |
Narayan Kamath | 64cd907 | 2014-05-13 13:35:14 +0100 | [diff] [blame] | 32 | /** |
| 33 | * @hide for internal use only. |
| 34 | */ |
| 35 | public static final String ZYGOTE_SOCKET = "zygote"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
Narayan Kamath | 64cd907 | 2014-05-13 13:35:14 +0100 | [diff] [blame] | 37 | /** |
| 38 | * @hide for internal use only. |
| 39 | */ |
| 40 | public static final String SECONDARY_ZYGOTE_SOCKET = "zygote_secondary"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 43 | * Defines the root UID. |
| 44 | * @hide |
| 45 | */ |
| 46 | public static final int ROOT_UID = 0; |
| 47 | |
| 48 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | * Defines the UID/GID under which system code runs. |
| 50 | */ |
| 51 | public static final int SYSTEM_UID = 1000; |
| 52 | |
| 53 | /** |
| 54 | * Defines the UID/GID under which the telephony code runs. |
| 55 | */ |
| 56 | public static final int PHONE_UID = 1001; |
| 57 | |
| 58 | /** |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 59 | * Defines the UID/GID for the user shell. |
| 60 | * @hide |
| 61 | */ |
| 62 | public static final int SHELL_UID = 2000; |
| 63 | |
| 64 | /** |
Mike Lockwood | d42685d | 2009-09-03 09:25:22 -0400 | [diff] [blame] | 65 | * Defines the UID/GID for the log group. |
| 66 | * @hide |
| 67 | */ |
| 68 | public static final int LOG_UID = 1007; |
| 69 | |
| 70 | /** |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 71 | * Defines the UID/GID for the WIFI supplicant process. |
| 72 | * @hide |
| 73 | */ |
| 74 | public static final int WIFI_UID = 1010; |
| 75 | |
| 76 | /** |
Glenn Kasten | 8b7d1b4 | 2011-07-13 16:23:22 -0700 | [diff] [blame] | 77 | * Defines the UID/GID for the mediaserver process. |
| 78 | * @hide |
| 79 | */ |
| 80 | public static final int MEDIA_UID = 1013; |
| 81 | |
| 82 | /** |
Jeff Sharkey | 5294a2f | 2012-04-24 17:07:22 -0700 | [diff] [blame] | 83 | * Defines the UID/GID for the DRM process. |
| 84 | * @hide |
| 85 | */ |
| 86 | public static final int DRM_UID = 1019; |
| 87 | |
| 88 | /** |
Kenny Root | 26993b3 | 2012-03-19 15:07:51 -0700 | [diff] [blame] | 89 | * Defines the UID/GID for the group that controls VPN services. |
| 90 | * @hide |
| 91 | */ |
| 92 | public static final int VPN_UID = 1016; |
| 93 | |
| 94 | /** |
Janis Danisevskis | 6ab9bb6 | 2017-03-08 10:35:00 -0800 | [diff] [blame] | 95 | * Defines the UID/GID for keystore. |
| 96 | * @hide |
| 97 | */ |
| 98 | public static final int KEYSTORE_UID = 1017; |
| 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 | /** |
Jaikumar Ganesh | 1abb1cb | 2012-01-25 16:14:50 -0800 | [diff] [blame] | 107 | * Defines the UID/GID for the Bluetooth service process. |
| 108 | * @hide |
| 109 | */ |
| 110 | public static final int BLUETOOTH_UID = 1002; |
| 111 | |
| 112 | /** |
Mike Lockwood | dcaa10c | 2010-12-16 12:50:44 -0800 | [diff] [blame] | 113 | * Defines the GID for the group that allows write access to the internal media storage. |
| 114 | * @hide |
| 115 | */ |
| 116 | public static final int MEDIA_RW_GID = 1023; |
| 117 | |
| 118 | /** |
Jeff Sharkey | 184a010 | 2013-07-10 16:19:52 -0700 | [diff] [blame] | 119 | * Access to installed package details |
| 120 | * @hide |
| 121 | */ |
| 122 | public static final int PACKAGE_INFO_GID = 1032; |
| 123 | |
| 124 | /** |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 125 | * Defines the UID/GID for the shared RELRO file updater process. |
| 126 | * @hide |
| 127 | */ |
| 128 | public static final int SHARED_RELRO_UID = 1037; |
| 129 | |
| 130 | /** |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 131 | * Defines the UID/GID for the audioserver process. |
| 132 | * @hide |
| 133 | */ |
| 134 | public static final int AUDIOSERVER_UID = 1041; |
| 135 | |
| 136 | /** |
Chien-Yu Chen | 75cade0 | 2016-01-11 10:56:21 -0800 | [diff] [blame] | 137 | * Defines the UID/GID for the cameraserver process |
| 138 | * @hide |
| 139 | */ |
Chien-Yu Chen | b8b4e85 | 2016-01-29 12:02:28 -0800 | [diff] [blame] | 140 | public static final int CAMERASERVER_UID = 1047; |
Chien-Yu Chen | 75cade0 | 2016-01-11 10:56:21 -0800 | [diff] [blame] | 141 | |
| 142 | /** |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 143 | * Defines the UID/GID for the WebView zygote process. |
| 144 | * @hide |
| 145 | */ |
| 146 | public static final int WEBVIEW_ZYGOTE_UID = 1051; |
| 147 | |
| 148 | /** |
Jeff Sharkey | 07c5c6f | 2017-03-21 10:09:50 -0600 | [diff] [blame] | 149 | * Defines the UID used for resource tracking for OTA updates. |
| 150 | * @hide |
| 151 | */ |
| 152 | public static final int OTA_UPDATE_UID = 1061; |
| 153 | |
| 154 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | * Defines the start of a range of UIDs (and GIDs), going from this |
| 156 | * number to {@link #LAST_APPLICATION_UID} that are reserved for assigning |
| 157 | * to applications. |
| 158 | */ |
| 159 | public static final int FIRST_APPLICATION_UID = 10000; |
Jeff Sharkey | 184a010 | 2013-07-10 16:19:52 -0700 | [diff] [blame] | 160 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | /** |
| 162 | * Last of application-specific UIDs starting at |
| 163 | * {@link #FIRST_APPLICATION_UID}. |
| 164 | */ |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 165 | public static final int LAST_APPLICATION_UID = 19999; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 166 | |
| 167 | /** |
| 168 | * First uid used for fully isolated sandboxed processes (with no permissions of their own) |
| 169 | * @hide |
| 170 | */ |
| 171 | public static final int FIRST_ISOLATED_UID = 99000; |
| 172 | |
| 173 | /** |
| 174 | * Last uid used for fully isolated sandboxed processes (with no permissions of their own) |
| 175 | * @hide |
| 176 | */ |
| 177 | public static final int LAST_ISOLATED_UID = 99999; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | |
| 179 | /** |
Robin Lee | e66b689 | 2014-04-29 12:36:47 +0100 | [diff] [blame] | 180 | * Defines the gid shared by all applications running under the same profile. |
| 181 | * @hide |
| 182 | */ |
| 183 | public static final int SHARED_USER_GID = 9997; |
| 184 | |
| 185 | /** |
Kenny Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 186 | * First gid for applications to share resources. Used when forward-locking |
| 187 | * is enabled but all UserHandles need to be able to read the resources. |
| 188 | * @hide |
| 189 | */ |
| 190 | public static final int FIRST_SHARED_APPLICATION_GID = 50000; |
| 191 | |
| 192 | /** |
| 193 | * Last gid for applications to share resources. Used when forward-locking |
| 194 | * is enabled but all UserHandles need to be able to read the resources. |
| 195 | * @hide |
| 196 | */ |
| 197 | public static final int LAST_SHARED_APPLICATION_GID = 59999; |
| 198 | |
Jeff Sharkey | 5eb3eb5 | 2016-12-13 08:44:51 -0700 | [diff] [blame] | 199 | /** {@hide} */ |
| 200 | public static final int FIRST_APPLICATION_CACHE_GID = 20000; |
| 201 | /** {@hide} */ |
| 202 | public static final int LAST_APPLICATION_CACHE_GID = 29999; |
| 203 | |
Kenny Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 204 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | * Standard priority of application threads. |
| 206 | * Use with {@link #setThreadPriority(int)} and |
| 207 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 208 | * {@link java.lang.Thread} class. |
| 209 | */ |
| 210 | public static final int THREAD_PRIORITY_DEFAULT = 0; |
| 211 | |
| 212 | /* |
| 213 | * *************************************** |
| 214 | * ** Keep in sync with utils/threads.h ** |
| 215 | * *************************************** |
| 216 | */ |
| 217 | |
| 218 | /** |
| 219 | * Lowest available thread priority. Only for those who really, really |
| 220 | * don't want to run if anything else is happening. |
| 221 | * Use with {@link #setThreadPriority(int)} and |
| 222 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 223 | * {@link java.lang.Thread} class. |
| 224 | */ |
| 225 | public static final int THREAD_PRIORITY_LOWEST = 19; |
| 226 | |
| 227 | /** |
| 228 | * Standard priority background threads. This gives your thread a slightly |
| 229 | * lower than normal priority, so that it will have less chance of impacting |
| 230 | * the responsiveness of the user interface. |
| 231 | * Use with {@link #setThreadPriority(int)} and |
| 232 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 233 | * {@link java.lang.Thread} class. |
| 234 | */ |
| 235 | public static final int THREAD_PRIORITY_BACKGROUND = 10; |
| 236 | |
| 237 | /** |
| 238 | * Standard priority of threads that are currently running a user interface |
| 239 | * that the user is interacting with. Applications can not normally |
| 240 | * change to this priority; the system will automatically adjust your |
| 241 | * application threads as the user moves through the UI. |
| 242 | * Use with {@link #setThreadPriority(int)} and |
| 243 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 244 | * {@link java.lang.Thread} class. |
| 245 | */ |
| 246 | public static final int THREAD_PRIORITY_FOREGROUND = -2; |
| 247 | |
| 248 | /** |
| 249 | * Standard priority of system display threads, involved in updating |
| 250 | * the user interface. Applications can not |
| 251 | * normally change to this priority. |
| 252 | * Use with {@link #setThreadPriority(int)} and |
| 253 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 254 | * {@link java.lang.Thread} class. |
| 255 | */ |
| 256 | public static final int THREAD_PRIORITY_DISPLAY = -4; |
| 257 | |
| 258 | /** |
| 259 | * Standard priority of the most important display threads, for compositing |
| 260 | * the screen and retrieving input events. Applications can not normally |
| 261 | * change to this priority. |
| 262 | * Use with {@link #setThreadPriority(int)} and |
| 263 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 264 | * {@link java.lang.Thread} class. |
| 265 | */ |
| 266 | public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8; |
| 267 | |
| 268 | /** |
| 269 | * Standard priority of audio threads. Applications can not normally |
| 270 | * change to this priority. |
| 271 | * Use with {@link #setThreadPriority(int)} and |
| 272 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 273 | * {@link java.lang.Thread} class. |
| 274 | */ |
| 275 | public static final int THREAD_PRIORITY_AUDIO = -16; |
| 276 | |
| 277 | /** |
| 278 | * Standard priority of the most important audio threads. |
| 279 | * Applications can not normally change to this priority. |
| 280 | * Use with {@link #setThreadPriority(int)} and |
| 281 | * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal |
| 282 | * {@link java.lang.Thread} class. |
| 283 | */ |
| 284 | public static final int THREAD_PRIORITY_URGENT_AUDIO = -19; |
| 285 | |
| 286 | /** |
| 287 | * Minimum increment to make a priority more favorable. |
| 288 | */ |
| 289 | public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1; |
| 290 | |
| 291 | /** |
| 292 | * Minimum increment to make a priority less favorable. |
| 293 | */ |
| 294 | public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1; |
| 295 | |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 296 | /** |
Glenn Kasten | 6793ac9 | 2011-07-13 12:44:12 -0700 | [diff] [blame] | 297 | * Default scheduling policy |
| 298 | * @hide |
| 299 | */ |
| 300 | public static final int SCHED_OTHER = 0; |
| 301 | |
| 302 | /** |
| 303 | * First-In First-Out scheduling policy |
| 304 | * @hide |
| 305 | */ |
| 306 | public static final int SCHED_FIFO = 1; |
| 307 | |
| 308 | /** |
| 309 | * Round-Robin scheduling policy |
| 310 | * @hide |
| 311 | */ |
| 312 | public static final int SCHED_RR = 2; |
| 313 | |
| 314 | /** |
| 315 | * Batch scheduling policy |
| 316 | * @hide |
| 317 | */ |
| 318 | public static final int SCHED_BATCH = 3; |
| 319 | |
| 320 | /** |
| 321 | * Idle scheduling policy |
| 322 | * @hide |
| 323 | */ |
| 324 | public static final int SCHED_IDLE = 5; |
| 325 | |
Srinath Sridharan | e535a58 | 2016-06-27 18:13:47 -0700 | [diff] [blame] | 326 | /** |
| 327 | * Reset scheduler choice on fork. |
| 328 | * @hide |
| 329 | */ |
| 330 | public static final int SCHED_RESET_ON_FORK = 0x40000000; |
| 331 | |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 332 | // Keep in sync with SP_* constants of enum type SchedPolicy |
| 333 | // declared in system/core/include/cutils/sched_policy.h, |
| 334 | // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value. |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 335 | |
| 336 | /** |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 337 | * Default thread group - |
| 338 | * has meaning with setProcessGroup() only, cannot be used with setThreadGroup(). |
| 339 | * When used with setProcessGroup(), the group of each thread in the process |
| 340 | * is conditionally changed based on that thread's current priority, as follows: |
| 341 | * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND |
| 342 | * are moved to foreground thread group. All other threads are left unchanged. |
| 343 | * @hide |
| 344 | */ |
| 345 | public static final int THREAD_GROUP_DEFAULT = -1; |
| 346 | |
| 347 | /** |
| 348 | * Background thread group - All threads in |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 349 | * this group are scheduled with a reduced share of the CPU. |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 350 | * Value is same as constant SP_BACKGROUND of enum SchedPolicy. |
| 351 | * FIXME rename to THREAD_GROUP_BACKGROUND. |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 352 | * @hide |
| 353 | */ |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 354 | public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0; |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 355 | |
| 356 | /** |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 357 | * Foreground thread group - All threads in |
| 358 | * this group are scheduled with a normal share of the CPU. |
| 359 | * Value is same as constant SP_FOREGROUND of enum SchedPolicy. |
| 360 | * Not used at this level. |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 361 | * @hide |
| 362 | **/ |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 363 | private static final int THREAD_GROUP_FOREGROUND = 1; |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 364 | |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 365 | /** |
| 366 | * System thread group. |
| 367 | * @hide |
| 368 | **/ |
| 369 | public static final int THREAD_GROUP_SYSTEM = 2; |
| 370 | |
| 371 | /** |
| 372 | * Application audio thread group. |
| 373 | * @hide |
| 374 | **/ |
| 375 | public static final int THREAD_GROUP_AUDIO_APP = 3; |
| 376 | |
| 377 | /** |
| 378 | * System audio thread group. |
| 379 | * @hide |
| 380 | **/ |
| 381 | public static final int THREAD_GROUP_AUDIO_SYS = 4; |
| 382 | |
Tim Murray | ece6a22 | 2016-01-14 11:22:57 -0800 | [diff] [blame] | 383 | /** |
| 384 | * Thread group for top foreground app. |
| 385 | * @hide |
| 386 | **/ |
| 387 | public static final int THREAD_GROUP_TOP_APP = 5; |
| 388 | |
Joel Fernandes | 5977600 | 2017-04-17 13:40:16 -0700 | [diff] [blame] | 389 | /** |
| 390 | * Thread group for RT app. |
| 391 | * @hide |
| 392 | **/ |
| 393 | public static final int THREAD_GROUP_RT_APP = 6; |
| 394 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | public static final int SIGNAL_QUIT = 3; |
| 396 | public static final int SIGNAL_KILL = 9; |
| 397 | public static final int SIGNAL_USR1 = 10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | |
Dianne Hackborn | d98885c | 2016-03-01 17:13:03 -0800 | [diff] [blame] | 399 | private static long sStartElapsedRealtime; |
| 400 | private static long sStartUptimeMillis; |
| 401 | |
Narayan Kamath | 4444dcd | 2014-04-03 20:15:15 +0100 | [diff] [blame] | 402 | /** |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 403 | * State associated with the zygote process. |
| 404 | * @hide |
Narayan Kamath | 4444dcd | 2014-04-03 20:15:15 +0100 | [diff] [blame] | 405 | */ |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 406 | public static final ZygoteProcess zygoteProcess = |
| 407 | new ZygoteProcess(ZYGOTE_SOCKET, SECONDARY_ZYGOTE_SOCKET); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | |
| 409 | /** |
| 410 | * Start a new process. |
| 411 | * |
| 412 | * <p>If processes are enabled, a new process is created and the |
| 413 | * static main() function of a <var>processClass</var> is executed there. |
| 414 | * The process will continue running after this function returns. |
| 415 | * |
| 416 | * <p>If processes are not enabled, a new thread in the caller's |
| 417 | * process is created and main() of <var>processClass</var> called there. |
| 418 | * |
| 419 | * <p>The niceName parameter, if not an empty string, is a custom name to |
| 420 | * give to the process instead of using processClass. This allows you to |
| 421 | * make easily identifyable processes even if you are using the same base |
| 422 | * <var>processClass</var> to start them. |
| 423 | * |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 424 | * When invokeWith is not null, the process will be started as a fresh app |
Tamas Berghammer | 0ca16fa | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 425 | * and not a zygote fork. Note that this is only allowed for uid 0 or when |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 426 | * runtimeFlags contains DEBUG_ENABLE_DEBUGGER. |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 427 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | * @param processClass The class to use as the process's main entry |
| 429 | * point. |
| 430 | * @param niceName A more readable name to use for the process. |
| 431 | * @param uid The user-id under which the process will run. |
| 432 | * @param gid The group-id under which the process will run. |
| 433 | * @param gids Additional group-ids associated with the process. |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 434 | * @param runtimeFlags Additional flags for the runtime. |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 435 | * @param targetSdkVersion The target SDK version for the app. |
Stephen Smalley | bd19b9e | 2013-04-12 14:49:38 -0400 | [diff] [blame] | 436 | * @param seInfo null-ok SELinux information for the new process. |
Ramin Zaghi | ff0c470 | 2014-04-01 15:02:29 +0100 | [diff] [blame] | 437 | * @param abi non-null the ABI this app should be started with. |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 438 | * @param instructionSet null-ok the instruction set to use. |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 439 | * @param appDataDir null-ok the data directory of the app. |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 440 | * @param invokeWith null-ok the command to invoke with. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | * @param zygoteArgs Additional arguments to supply to the zygote process. |
| 442 | * |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 443 | * @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] | 444 | * @throws RuntimeException on fatal start failure |
| 445 | * |
| 446 | * {@hide} |
| 447 | */ |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 448 | public static final ProcessStartResult start(final String processClass, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | final String niceName, |
| 450 | int uid, int gid, int[] gids, |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 451 | int runtimeFlags, int mountExternal, |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 452 | int targetSdkVersion, |
Stephen Smalley | 83d9eda | 2012-01-13 08:34:17 -0500 | [diff] [blame] | 453 | String seInfo, |
Ramin Zaghi | ff0c470 | 2014-04-01 15:02:29 +0100 | [diff] [blame] | 454 | String abi, |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 455 | String instructionSet, |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 456 | String appDataDir, |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 457 | String invokeWith, |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 458 | String[] zygoteArgs) { |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 459 | return zygoteProcess.start(processClass, niceName, uid, gid, gids, |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 460 | runtimeFlags, mountExternal, targetSdkVersion, seInfo, |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 461 | abi, instructionSet, appDataDir, invokeWith, zygoteArgs); |
Narayan Kamath | 4444dcd | 2014-04-03 20:15:15 +0100 | [diff] [blame] | 462 | } |
| 463 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 464 | /** @hide */ |
| 465 | public static final ProcessStartResult startWebView(final String processClass, |
| 466 | final String niceName, |
| 467 | int uid, int gid, int[] gids, |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 468 | int runtimeFlags, int mountExternal, |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 469 | int targetSdkVersion, |
| 470 | String seInfo, |
| 471 | String abi, |
| 472 | String instructionSet, |
| 473 | String appDataDir, |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 474 | String invokeWith, |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 475 | String[] zygoteArgs) { |
| 476 | return WebViewZygote.getProcess().start(processClass, niceName, uid, gid, gids, |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 477 | runtimeFlags, mountExternal, targetSdkVersion, seInfo, |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 478 | abi, instructionSet, appDataDir, invokeWith, zygoteArgs); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 479 | } |
| 480 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | /** |
| 482 | * Returns elapsed milliseconds of the time this process has run. |
| 483 | * @return Returns the number of milliseconds this process has return. |
| 484 | */ |
| 485 | public static final native long getElapsedCpuTime(); |
Andreas Gampe | ee0cdce | 2015-06-08 20:47:50 -0700 | [diff] [blame] | 486 | |
| 487 | /** |
Dianne Hackborn | d98885c | 2016-03-01 17:13:03 -0800 | [diff] [blame] | 488 | * Return the {@link SystemClock#elapsedRealtime()} at which this process was started. |
| 489 | */ |
| 490 | public static final long getStartElapsedRealtime() { |
| 491 | return sStartElapsedRealtime; |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Return the {@link SystemClock#uptimeMillis()} at which this process was started. |
| 496 | */ |
| 497 | public static final long getStartUptimeMillis() { |
| 498 | return sStartUptimeMillis; |
| 499 | } |
| 500 | |
| 501 | /** @hide */ |
| 502 | public static final void setStartTimes(long elapsedRealtime, long uptimeMillis) { |
| 503 | sStartElapsedRealtime = elapsedRealtime; |
| 504 | sStartUptimeMillis = uptimeMillis; |
| 505 | } |
| 506 | |
| 507 | /** |
Andreas Gampe | ee0cdce | 2015-06-08 20:47:50 -0700 | [diff] [blame] | 508 | * Returns true if the current process is a 64-bit runtime. |
| 509 | */ |
| 510 | public static final boolean is64Bit() { |
| 511 | return VMRuntime.getRuntime().is64Bit(); |
| 512 | } |
| 513 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | /** |
| 515 | * Returns the identifier of this process, which can be used with |
| 516 | * {@link #killProcess} and {@link #sendSignal}. |
| 517 | */ |
Jeff Hao | 406ec15 | 2013-07-30 10:13:41 -0700 | [diff] [blame] | 518 | public static final int myPid() { |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 519 | return Os.getpid(); |
Jeff Hao | 406ec15 | 2013-07-30 10:13:41 -0700 | [diff] [blame] | 520 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | |
| 522 | /** |
Romain Guy | 80b12fc | 2013-05-29 15:54:25 -0700 | [diff] [blame] | 523 | * Returns the identifier of this process' parent. |
| 524 | * @hide |
| 525 | */ |
Elliott Hughes | 2a805f9 | 2013-07-31 18:25:47 -0700 | [diff] [blame] | 526 | public static final int myPpid() { |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 527 | return Os.getppid(); |
Elliott Hughes | 2a805f9 | 2013-07-31 18:25:47 -0700 | [diff] [blame] | 528 | } |
Romain Guy | 80b12fc | 2013-05-29 15:54:25 -0700 | [diff] [blame] | 529 | |
| 530 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | * Returns the identifier of the calling thread, which be used with |
| 532 | * {@link #setThreadPriority(int, int)}. |
| 533 | */ |
Elliott Hughes | 6d4b1e2 | 2013-07-31 17:38:11 -0700 | [diff] [blame] | 534 | public static final int myTid() { |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 535 | return Os.gettid(); |
Elliott Hughes | 6d4b1e2 | 2013-07-31 17:38:11 -0700 | [diff] [blame] | 536 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | |
| 538 | /** |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 539 | * Returns the identifier of this process's uid. This is the kernel uid |
| 540 | * that the process is running under, which is the identity of its |
| 541 | * app-specific sandbox. It is different from {@link #myUserHandle} in that |
| 542 | * a uid identifies a specific app sandbox in a specific user. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | */ |
Jeff Hao | 406ec15 | 2013-07-30 10:13:41 -0700 | [diff] [blame] | 544 | public static final int myUid() { |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 545 | return Os.getuid(); |
Jeff Hao | 406ec15 | 2013-07-30 10:13:41 -0700 | [diff] [blame] | 546 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | |
| 548 | /** |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 549 | * Returns this process's user handle. This is the |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 550 | * user the process is running under. It is distinct from |
| 551 | * {@link #myUid()} in that a particular user will have multiple |
| 552 | * distinct apps running under it each with their own uid. |
| 553 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 554 | public static UserHandle myUserHandle() { |
| 555 | return UserHandle.of(UserHandle.getUserId(myUid())); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /** |
Jeff Davidson | 48aa86b | 2016-02-10 14:15:42 -0800 | [diff] [blame] | 559 | * Returns whether the given uid belongs to an application. |
| 560 | * @param uid A kernel uid. |
| 561 | * @return Whether the uid corresponds to an application sandbox running in |
| 562 | * a specific user. |
| 563 | */ |
| 564 | public static boolean isApplicationUid(int uid) { |
| 565 | return UserHandle.isApp(uid); |
| 566 | } |
| 567 | |
| 568 | /** |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 569 | * Returns whether the current process is in an isolated sandbox. |
| 570 | * @hide |
| 571 | */ |
| 572 | public static final boolean isIsolated() { |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 573 | return isIsolated(myUid()); |
| 574 | } |
| 575 | |
| 576 | /** {@hide} */ |
| 577 | public static final boolean isIsolated(int uid) { |
| 578 | uid = UserHandle.getAppId(uid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 579 | return uid >= FIRST_ISOLATED_UID && uid <= LAST_ISOLATED_UID; |
| 580 | } |
| 581 | |
| 582 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | * Returns the UID assigned to a particular user name, or -1 if there is |
| 584 | * none. If the given string consists of only numbers, it is converted |
| 585 | * directly to a uid. |
| 586 | */ |
| 587 | public static final native int getUidForName(String name); |
| 588 | |
| 589 | /** |
| 590 | * Returns the GID assigned to a particular user name, or -1 if there is |
| 591 | * none. If the given string consists of only numbers, it is converted |
| 592 | * directly to a gid. |
| 593 | */ |
| 594 | public static final native int getGidForName(String name); |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 595 | |
| 596 | /** |
| 597 | * Returns a uid for a currently running process. |
| 598 | * @param pid the process id |
| 599 | * @return the uid of the process, or -1 if the process is not running. |
| 600 | * @hide pending API council review |
| 601 | */ |
| 602 | public static final int getUidForPid(int pid) { |
| 603 | String[] procStatusLabels = { "Uid:" }; |
| 604 | long[] procStatusValues = new long[1]; |
| 605 | procStatusValues[0] = -1; |
| 606 | Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues); |
| 607 | return (int) procStatusValues[0]; |
| 608 | } |
| 609 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | /** |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 611 | * Returns the parent process id for a currently running process. |
| 612 | * @param pid the process id |
| 613 | * @return the parent process id of the process, or -1 if the process is not running. |
| 614 | * @hide |
| 615 | */ |
| 616 | public static final int getParentPid(int pid) { |
| 617 | String[] procStatusLabels = { "PPid:" }; |
| 618 | long[] procStatusValues = new long[1]; |
| 619 | procStatusValues[0] = -1; |
| 620 | Process.readProcLines("/proc/" + pid + "/status", procStatusLabels, procStatusValues); |
| 621 | return (int) procStatusValues[0]; |
| 622 | } |
| 623 | |
| 624 | /** |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 625 | * Returns the thread group leader id for a currently running thread. |
| 626 | * @param tid the thread id |
| 627 | * @return the thread group leader id of the thread, or -1 if the thread is not running. |
| 628 | * This is same as what getpid(2) would return if called by tid. |
| 629 | * @hide |
| 630 | */ |
| 631 | public static final int getThreadGroupLeader(int tid) { |
| 632 | String[] procStatusLabels = { "Tgid:" }; |
| 633 | long[] procStatusValues = new long[1]; |
| 634 | procStatusValues[0] = -1; |
| 635 | Process.readProcLines("/proc/" + tid + "/status", procStatusLabels, procStatusValues); |
| 636 | return (int) procStatusValues[0]; |
| 637 | } |
| 638 | |
| 639 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 640 | * Set the priority of a thread, based on Linux priorities. |
| 641 | * |
| 642 | * @param tid The identifier of the thread/process to change. |
| 643 | * @param priority A Linux priority level, from -20 for highest scheduling |
| 644 | * priority to 19 for lowest scheduling priority. |
| 645 | * |
| 646 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 647 | * <var>tid</var> does not exist. |
| 648 | * @throws SecurityException Throws SecurityException if your process does |
| 649 | * not have permission to modify the given thread, or to use the given |
| 650 | * priority. |
| 651 | */ |
| 652 | public static final native void setThreadPriority(int tid, int priority) |
| 653 | throws IllegalArgumentException, SecurityException; |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 654 | |
| 655 | /** |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 656 | * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to |
| 657 | * throw an exception if passed a background-level thread priority. This is only |
| 658 | * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1. |
| 659 | * |
| 660 | * @hide |
| 661 | */ |
| 662 | public static final native void setCanSelfBackground(boolean backgroundOk); |
| 663 | |
| 664 | /** |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 665 | * Sets the scheduling group for a thread. |
| 666 | * @hide |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 667 | * @param tid The identifier of the thread to change. |
| 668 | * @param group The target group for this thread from THREAD_GROUP_*. |
Joel Fernandes | 474d311 | 2017-04-04 16:32:15 -0700 | [diff] [blame] | 669 | * |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 670 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 671 | * <var>tid</var> does not exist. |
| 672 | * @throws SecurityException Throws SecurityException if your process does |
| 673 | * not have permission to modify the given thread, or to use the given |
| 674 | * priority. |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 675 | * If the thread is a thread group leader, that is it's gettid() == getpid(), |
| 676 | * then the other threads in the same thread group are _not_ affected. |
Tim Murray | 4074ad0 | 2016-07-08 15:45:45 -0700 | [diff] [blame] | 677 | * |
| 678 | * Does not set cpuset for some historical reason, just calls |
| 679 | * libcutils::set_sched_policy(). |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 680 | */ |
| 681 | public static final native void setThreadGroup(int tid, int group) |
| 682 | throws IllegalArgumentException, SecurityException; |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 683 | |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 684 | /** |
Joel Fernandes | 474d311 | 2017-04-04 16:32:15 -0700 | [diff] [blame] | 685 | * Sets the scheduling group and the corresponding cpuset group |
| 686 | * @hide |
| 687 | * @param tid The identifier of the thread to change. |
| 688 | * @param group The target group for this thread from THREAD_GROUP_*. |
| 689 | * |
| 690 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 691 | * <var>tid</var> does not exist. |
| 692 | * @throws SecurityException Throws SecurityException if your process does |
| 693 | * not have permission to modify the given thread, or to use the given |
| 694 | * priority. |
| 695 | */ |
| 696 | public static final native void setThreadGroupAndCpuset(int tid, int group) |
| 697 | throws IllegalArgumentException, SecurityException; |
| 698 | |
| 699 | /** |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 700 | * Sets the scheduling group for a process and all child threads |
| 701 | * @hide |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 702 | * @param pid The identifier of the process to change. |
| 703 | * @param group The target group for this process from THREAD_GROUP_*. |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 704 | * |
| 705 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 706 | * <var>tid</var> does not exist. |
| 707 | * @throws SecurityException Throws SecurityException if your process does |
| 708 | * not have permission to modify the given thread, or to use the given |
| 709 | * priority. |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 710 | * |
| 711 | * group == THREAD_GROUP_DEFAULT means to move all non-background priority |
| 712 | * threads to the foreground scheduling group, but to leave background |
| 713 | * priority threads alone. group == THREAD_GROUP_BG_NONINTERACTIVE moves all |
| 714 | * threads, regardless of priority, to the background scheduling group. |
| 715 | * group == THREAD_GROUP_FOREGROUND is not allowed. |
Tim Murray | 4074ad0 | 2016-07-08 15:45:45 -0700 | [diff] [blame] | 716 | * |
| 717 | * Always sets cpusets. |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 718 | */ |
| 719 | public static final native void setProcessGroup(int pid, int group) |
| 720 | throws IllegalArgumentException, SecurityException; |
Jeff Sharkey | 9e57c41 | 2013-01-17 14:12:41 -0800 | [diff] [blame] | 721 | |
| 722 | /** |
| 723 | * Return the scheduling group of requested process. |
| 724 | * |
| 725 | * @hide |
| 726 | */ |
| 727 | public static final native int getProcessGroup(int pid) |
| 728 | throws IllegalArgumentException, SecurityException; |
| 729 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 730 | /** |
Martijn Coenen | cd4bdf3 | 2016-03-03 17:30:52 +0100 | [diff] [blame] | 731 | * On some devices, the foreground process may have one or more CPU |
| 732 | * cores exclusively reserved for it. This method can be used to |
| 733 | * retrieve which cores that are (if any), so the calling process |
| 734 | * can then use sched_setaffinity() to lock a thread to these cores. |
| 735 | * Note that the calling process must currently be running in the |
| 736 | * foreground for this method to return any cores. |
| 737 | * |
| 738 | * The CPU core(s) exclusively reserved for the foreground process will |
| 739 | * stay reserved for as long as the process stays in the foreground. |
| 740 | * |
| 741 | * As soon as a process leaves the foreground, those CPU cores will |
| 742 | * no longer be reserved for it, and will most likely be reserved for |
| 743 | * the new foreground process. It's not necessary to change the affinity |
| 744 | * of your process when it leaves the foreground (if you had previously |
| 745 | * set it to use a reserved core); the OS will automatically take care |
| 746 | * of resetting the affinity at that point. |
| 747 | * |
| 748 | * @return an array of integers, indicating the CPU cores exclusively |
| 749 | * reserved for this process. The array will have length zero if no |
| 750 | * CPU cores are exclusively reserved for this process at this point |
| 751 | * in time. |
| 752 | */ |
| 753 | public static final native int[] getExclusiveCores(); |
| 754 | |
| 755 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 756 | * Set the priority of the calling thread, based on Linux priorities. See |
| 757 | * {@link #setThreadPriority(int, int)} for more information. |
| 758 | * |
| 759 | * @param priority A Linux priority level, from -20 for highest scheduling |
| 760 | * priority to 19 for lowest scheduling priority. |
| 761 | * |
| 762 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 763 | * <var>tid</var> does not exist. |
| 764 | * @throws SecurityException Throws SecurityException if your process does |
| 765 | * not have permission to modify the given thread, or to use the given |
| 766 | * priority. |
| 767 | * |
| 768 | * @see #setThreadPriority(int, int) |
| 769 | */ |
| 770 | public static final native void setThreadPriority(int priority) |
| 771 | throws IllegalArgumentException, SecurityException; |
| 772 | |
| 773 | /** |
| 774 | * Return the current priority of a thread, based on Linux priorities. |
| 775 | * |
Alessio Balsini | 68ae301 | 2017-06-07 11:41:33 +0200 | [diff] [blame] | 776 | * @param tid The identifier of the thread/process. If tid equals zero, the priority of the |
| 777 | * calling process/thread will be returned. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 778 | * |
| 779 | * @return Returns the current priority, as a Linux priority level, |
| 780 | * from -20 for highest scheduling priority to 19 for lowest scheduling |
| 781 | * priority. |
| 782 | * |
| 783 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 784 | * <var>tid</var> does not exist. |
| 785 | */ |
| 786 | public static final native int getThreadPriority(int tid) |
| 787 | throws IllegalArgumentException; |
| 788 | |
| 789 | /** |
Srinath Sridharan | 1b15d13 | 2016-07-19 15:16:11 -0700 | [diff] [blame] | 790 | * Return the current scheduling policy of a thread, based on Linux. |
| 791 | * |
| 792 | * @param tid The identifier of the thread/process to get the scheduling policy. |
| 793 | * |
| 794 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 795 | * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy. |
| 796 | * @throws SecurityException Throws SecurityException if your process does |
| 797 | * not have permission to modify the given thread, or to use the given |
| 798 | * scheduling policy or priority. |
| 799 | * |
| 800 | * {@hide} |
| 801 | */ |
| 802 | |
| 803 | @TestApi |
| 804 | public static final native int getThreadScheduler(int tid) |
| 805 | throws IllegalArgumentException; |
| 806 | |
| 807 | /** |
Glenn Kasten | 6793ac9 | 2011-07-13 12:44:12 -0700 | [diff] [blame] | 808 | * Set the scheduling policy and priority of a thread, based on Linux. |
| 809 | * |
| 810 | * @param tid The identifier of the thread/process to change. |
| 811 | * @param policy A Linux scheduling policy such as SCHED_OTHER etc. |
| 812 | * @param priority A Linux priority level in a range appropriate for the given policy. |
| 813 | * |
| 814 | * @throws IllegalArgumentException Throws IllegalArgumentException if |
| 815 | * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy. |
| 816 | * @throws SecurityException Throws SecurityException if your process does |
| 817 | * not have permission to modify the given thread, or to use the given |
| 818 | * scheduling policy or priority. |
| 819 | * |
| 820 | * {@hide} |
| 821 | */ |
Srinath Sridharan | 1b15d13 | 2016-07-19 15:16:11 -0700 | [diff] [blame] | 822 | |
Glenn Kasten | 6793ac9 | 2011-07-13 12:44:12 -0700 | [diff] [blame] | 823 | public static final native void setThreadScheduler(int tid, int policy, int priority) |
| 824 | throws IllegalArgumentException; |
| 825 | |
| 826 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 827 | * Determine whether the current environment supports multiple processes. |
| 828 | * |
| 829 | * @return Returns true if the system can run in multiple processes, else |
| 830 | * false if everything is running in a single process. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 831 | * |
| 832 | * @deprecated This method always returns true. Do not use. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 833 | */ |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 834 | @Deprecated |
| 835 | public static final boolean supportsProcesses() { |
| 836 | return true; |
| 837 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 838 | |
| 839 | /** |
Rom Lemarchand | 5534ba9 | 2013-07-12 16:15:36 -0700 | [diff] [blame] | 840 | * Adjust the swappiness level for a process. |
| 841 | * |
| 842 | * @param pid The process identifier to set. |
| 843 | * @param is_increased Whether swappiness should be increased or default. |
| 844 | * |
| 845 | * @return Returns true if the underlying system supports this |
| 846 | * feature, else false. |
| 847 | * |
| 848 | * {@hide} |
| 849 | */ |
| 850 | public static final native boolean setSwappiness(int pid, boolean is_increased); |
| 851 | |
| 852 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 853 | * Change this process's argv[0] parameter. This can be useful to show |
| 854 | * more descriptive information in things like the 'ps' command. |
| 855 | * |
| 856 | * @param text The new name of this process. |
| 857 | * |
| 858 | * {@hide} |
| 859 | */ |
| 860 | public static final native void setArgV0(String text); |
| 861 | |
| 862 | /** |
| 863 | * Kill the process with the given PID. |
| 864 | * Note that, though this API allows us to request to |
| 865 | * kill any process based on its PID, the kernel will |
| 866 | * still impose standard restrictions on which PIDs you |
| 867 | * are actually able to kill. Typically this means only |
| 868 | * the process running the caller's packages/application |
| 869 | * and any additional processes created by that app; packages |
| 870 | * sharing a common UID will also be able to kill each |
| 871 | * other's processes. |
| 872 | */ |
| 873 | public static final void killProcess(int pid) { |
| 874 | sendSignal(pid, SIGNAL_KILL); |
| 875 | } |
| 876 | |
| 877 | /** @hide */ |
| 878 | public static final native int setUid(int uid); |
| 879 | |
| 880 | /** @hide */ |
| 881 | public static final native int setGid(int uid); |
| 882 | |
| 883 | /** |
| 884 | * Send a signal to the given process. |
| 885 | * |
| 886 | * @param pid The pid of the target process. |
| 887 | * @param signal The signal to send. |
| 888 | */ |
| 889 | public static final native void sendSignal(int pid, int signal); |
| 890 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 891 | /** |
| 892 | * @hide |
| 893 | * Private impl for avoiding a log message... DO NOT USE without doing |
| 894 | * your own log, or the Android Illuminati will find you some night and |
| 895 | * beat you up. |
| 896 | */ |
| 897 | public static final void killProcessQuiet(int pid) { |
| 898 | sendSignalQuiet(pid, SIGNAL_KILL); |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * @hide |
| 903 | * Private impl for avoiding a log message... DO NOT USE without doing |
| 904 | * your own log, or the Android Illuminati will find you some night and |
| 905 | * beat you up. |
| 906 | */ |
| 907 | public static final native void sendSignalQuiet(int pid, int signal); |
| 908 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 909 | /** @hide */ |
Marco Nelissen | 0bca96b | 2009-07-17 12:59:25 -0700 | [diff] [blame] | 910 | public static final native long getFreeMemory(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 911 | |
| 912 | /** @hide */ |
Dianne Hackborn | 59325eb | 2012-05-09 18:45:20 -0700 | [diff] [blame] | 913 | public static final native long getTotalMemory(); |
| 914 | |
| 915 | /** @hide */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 916 | public static final native void readProcLines(String path, |
| 917 | String[] reqFields, long[] outSizes); |
| 918 | |
| 919 | /** @hide */ |
| 920 | public static final native int[] getPids(String path, int[] lastArray); |
| 921 | |
| 922 | /** @hide */ |
| 923 | public static final int PROC_TERM_MASK = 0xff; |
| 924 | /** @hide */ |
| 925 | public static final int PROC_ZERO_TERM = 0; |
| 926 | /** @hide */ |
| 927 | public static final int PROC_SPACE_TERM = (int)' '; |
| 928 | /** @hide */ |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 929 | public static final int PROC_TAB_TERM = (int)'\t'; |
| 930 | /** @hide */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 931 | public static final int PROC_COMBINE = 0x100; |
| 932 | /** @hide */ |
| 933 | public static final int PROC_PARENS = 0x200; |
| 934 | /** @hide */ |
Dianne Hackborn | 13ac041 | 2013-06-25 19:34:49 -0700 | [diff] [blame] | 935 | public static final int PROC_QUOTES = 0x400; |
| 936 | /** @hide */ |
Dianne Hackborn | 4de5a3a | 2016-06-20 15:20:15 -0700 | [diff] [blame] | 937 | public static final int PROC_CHAR = 0x800; |
| 938 | /** @hide */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | public static final int PROC_OUT_STRING = 0x1000; |
| 940 | /** @hide */ |
| 941 | public static final int PROC_OUT_LONG = 0x2000; |
| 942 | /** @hide */ |
| 943 | public static final int PROC_OUT_FLOAT = 0x4000; |
| 944 | |
| 945 | /** @hide */ |
| 946 | public static final native boolean readProcFile(String file, int[] format, |
| 947 | String[] outStrings, long[] outLongs, float[] outFloats); |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 948 | |
| 949 | /** @hide */ |
| 950 | public static final native boolean parseProcLine(byte[] buffer, int startIndex, |
| 951 | 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] | 952 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 953 | /** @hide */ |
| 954 | public static final native int[] getPidsForCommands(String[] cmds); |
| 955 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 956 | /** |
| 957 | * Gets the total Pss value for a given process, in bytes. |
| 958 | * |
| 959 | * @param pid the process to the Pss for |
| 960 | * @return the total Pss value for the given process in bytes, |
| 961 | * or -1 if the value cannot be determined |
| 962 | * @hide |
| 963 | */ |
| 964 | public static final native long getPss(int pid); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 965 | |
| 966 | /** |
| 967 | * Specifies the outcome of having started a process. |
| 968 | * @hide |
| 969 | */ |
| 970 | public static final class ProcessStartResult { |
| 971 | /** |
| 972 | * The PID of the newly started process. |
| 973 | * Always >= 0. (If the start failed, an exception will have been thrown instead.) |
| 974 | */ |
| 975 | public int pid; |
| 976 | |
| 977 | /** |
| 978 | * True if the process was started with a wrapper attached. |
| 979 | */ |
| 980 | public boolean usingWrapper; |
| 981 | } |
Colin Cross | 0769e55 | 2014-06-03 13:25:35 -0700 | [diff] [blame] | 982 | |
| 983 | /** |
| 984 | * Kill all processes in a process group started for the given |
| 985 | * pid. |
| 986 | * @hide |
| 987 | */ |
| 988 | public static final native int killProcessGroup(int uid, int pid); |
| 989 | |
| 990 | /** |
| 991 | * Remove all process groups. Expected to be called when ActivityManager |
| 992 | * is restarted. |
| 993 | * @hide |
| 994 | */ |
| 995 | public static final native void removeAllProcessGroups(); |
Tim Murray | b89970e | 2016-07-01 10:42:20 -0700 | [diff] [blame] | 996 | |
| 997 | /** |
| 998 | * Check to see if a thread belongs to a given process. This may require |
| 999 | * more permissions than apps generally have. |
| 1000 | * @return true if this thread belongs to a process |
| 1001 | * @hide |
| 1002 | */ |
| 1003 | public static final boolean isThreadInProcess(int tid, int pid) { |
Tim Murray | 9bc12a8 | 2016-07-16 11:28:40 -0700 | [diff] [blame] | 1004 | StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
Tim Murray | b89970e | 2016-07-01 10:42:20 -0700 | [diff] [blame] | 1005 | try { |
| 1006 | if (Os.access("/proc/" + tid + "/task/" + pid, OsConstants.F_OK)) { |
| 1007 | return true; |
| 1008 | } else { |
| 1009 | return false; |
| 1010 | } |
| 1011 | } catch (Exception e) { |
| 1012 | return false; |
Tim Murray | 9bc12a8 | 2016-07-16 11:28:40 -0700 | [diff] [blame] | 1013 | } finally { |
| 1014 | StrictMode.setThreadPolicy(oldPolicy); |
Tim Murray | b89970e | 2016-07-01 10:42:20 -0700 | [diff] [blame] | 1015 | } |
Tim Murray | 9bc12a8 | 2016-07-16 11:28:40 -0700 | [diff] [blame] | 1016 | |
Tim Murray | b89970e | 2016-07-01 10:42:20 -0700 | [diff] [blame] | 1017 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1018 | } |