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