The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* //device/libs/android_runtime/android_util_Process.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8 | ** |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10 | ** |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #define LOG_TAG "Process" |
| 19 | |
| 20 | #include <utils/Log.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 21 | #include <binder/IPCThreadState.h> |
| 22 | #include <binder/ProcessState.h> |
| 23 | #include <binder/IServiceManager.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | #include <utils/String8.h> |
| 25 | #include <utils/Vector.h> |
| 26 | |
| 27 | #include <android_runtime/AndroidRuntime.h> |
| 28 | |
| 29 | #include "android_util_Binder.h" |
| 30 | #include "JNIHelp.h" |
| 31 | |
| 32 | #include <sys/errno.h> |
| 33 | #include <sys/resource.h> |
| 34 | #include <sys/types.h> |
San Mehat | 242d65b | 2009-09-12 10:10:37 -0700 | [diff] [blame] | 35 | #include <cutils/sched_policy.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | #include <dirent.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <grp.h> |
| 39 | #include <pwd.h> |
| 40 | #include <signal.h> |
Glenn Kasten | 6af763b | 2011-05-04 17:58:57 -0700 | [diff] [blame] | 41 | #include <unistd.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
San Mehat | 957e586 | 2009-10-29 13:56:49 -0700 | [diff] [blame] | 43 | #define POLICY_DEBUG 0 |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 44 | #define GUARD_THREAD_PRIORITY 0 |
San Mehat | a5109a8 | 2009-10-29 11:48:50 -0700 | [diff] [blame] | 45 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | using namespace android; |
| 47 | |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 48 | #if GUARD_THREAD_PRIORITY |
| 49 | Mutex gKeyCreateMutex; |
| 50 | static pthread_key_t gBgKey = -1; |
| 51 | #endif |
| 52 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | static void signalExceptionForPriorityError(JNIEnv* env, jobject obj, int err) |
| 54 | { |
| 55 | switch (err) { |
| 56 | case EINVAL: |
| 57 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 58 | break; |
| 59 | case ESRCH: |
| 60 | jniThrowException(env, "java/lang/IllegalArgumentException", "Given thread does not exist"); |
| 61 | break; |
| 62 | case EPERM: |
| 63 | jniThrowException(env, "java/lang/SecurityException", "No permission to modify given thread"); |
| 64 | break; |
| 65 | case EACCES: |
| 66 | jniThrowException(env, "java/lang/SecurityException", "No permission to set to given priority"); |
| 67 | break; |
| 68 | default: |
| 69 | jniThrowException(env, "java/lang/RuntimeException", "Unknown error"); |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 74 | static void signalExceptionForGroupError(JNIEnv* env, jobject obj, int err) |
| 75 | { |
| 76 | switch (err) { |
| 77 | case EINVAL: |
| 78 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 79 | break; |
| 80 | case ESRCH: |
| 81 | jniThrowException(env, "java/lang/IllegalArgumentException", "Given thread does not exist"); |
| 82 | break; |
| 83 | case EPERM: |
| 84 | jniThrowException(env, "java/lang/SecurityException", "No permission to modify given thread"); |
| 85 | break; |
| 86 | case EACCES: |
| 87 | jniThrowException(env, "java/lang/SecurityException", "No permission to set to given group"); |
| 88 | break; |
| 89 | default: |
| 90 | jniThrowException(env, "java/lang/RuntimeException", "Unknown error"); |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | jint android_os_Process_myPid(JNIEnv* env, jobject clazz) |
| 96 | { |
| 97 | return getpid(); |
| 98 | } |
| 99 | |
| 100 | jint android_os_Process_myUid(JNIEnv* env, jobject clazz) |
| 101 | { |
| 102 | return getuid(); |
| 103 | } |
| 104 | |
| 105 | jint android_os_Process_myTid(JNIEnv* env, jobject clazz) |
| 106 | { |
Dianne Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 107 | return androidGetTid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | jint android_os_Process_getUidForName(JNIEnv* env, jobject clazz, jstring name) |
| 111 | { |
| 112 | if (name == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 113 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | return -1; |
| 115 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 116 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | const jchar* str16 = env->GetStringCritical(name, 0); |
| 118 | String8 name8; |
| 119 | if (str16) { |
| 120 | name8 = String8(str16, env->GetStringLength(name)); |
| 121 | env->ReleaseStringCritical(name, str16); |
| 122 | } |
| 123 | |
| 124 | const size_t N = name8.size(); |
| 125 | if (N > 0) { |
| 126 | const char* str = name8.string(); |
| 127 | for (size_t i=0; i<N; i++) { |
| 128 | if (str[i] < '0' || str[i] > '9') { |
| 129 | struct passwd* pwd = getpwnam(str); |
| 130 | if (pwd == NULL) { |
| 131 | return -1; |
| 132 | } |
| 133 | return pwd->pw_uid; |
| 134 | } |
| 135 | } |
| 136 | return atoi(str); |
| 137 | } |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | jint android_os_Process_getGidForName(JNIEnv* env, jobject clazz, jstring name) |
| 142 | { |
| 143 | if (name == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 144 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | return -1; |
| 146 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 147 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | const jchar* str16 = env->GetStringCritical(name, 0); |
| 149 | String8 name8; |
| 150 | if (str16) { |
| 151 | name8 = String8(str16, env->GetStringLength(name)); |
| 152 | env->ReleaseStringCritical(name, str16); |
| 153 | } |
| 154 | |
| 155 | const size_t N = name8.size(); |
| 156 | if (N > 0) { |
| 157 | const char* str = name8.string(); |
| 158 | for (size_t i=0; i<N; i++) { |
| 159 | if (str[i] < '0' || str[i] > '9') { |
| 160 | struct group* grp = getgrnam(str); |
| 161 | if (grp == NULL) { |
| 162 | return -1; |
| 163 | } |
| 164 | return grp->gr_gid; |
| 165 | } |
| 166 | } |
| 167 | return atoi(str); |
| 168 | } |
| 169 | return -1; |
| 170 | } |
| 171 | |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 172 | void android_os_Process_setThreadGroup(JNIEnv* env, jobject clazz, int pid, jint grp) |
| 173 | { |
Dianne Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 174 | int res = androidSetThreadSchedulingGroup(pid, grp); |
| 175 | if (res != NO_ERROR) { |
| 176 | signalExceptionForGroupError(env, clazz, res == BAD_VALUE ? EINVAL : errno); |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 177 | return; |
| 178 | } |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 181 | void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jint grp) |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 182 | { |
| 183 | DIR *d; |
| 184 | FILE *fp; |
| 185 | char proc_path[255]; |
| 186 | struct dirent *de; |
| 187 | |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 188 | if (grp > ANDROID_TGROUP_MAX || grp < 0) { |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 189 | signalExceptionForGroupError(env, clazz, EINVAL); |
| 190 | return; |
| 191 | } |
| 192 | |
San Mehat | a5109a8 | 2009-10-29 11:48:50 -0700 | [diff] [blame] | 193 | #if POLICY_DEBUG |
| 194 | char cmdline[32]; |
| 195 | int fd; |
| 196 | |
| 197 | strcpy(cmdline, "unknown"); |
| 198 | |
| 199 | sprintf(proc_path, "/proc/%d/cmdline", pid); |
| 200 | fd = open(proc_path, O_RDONLY); |
| 201 | if (fd >= 0) { |
| 202 | int rc = read(fd, cmdline, sizeof(cmdline)-1); |
| 203 | cmdline[rc] = 0; |
| 204 | close(fd); |
| 205 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 206 | |
San Mehat | a5109a8 | 2009-10-29 11:48:50 -0700 | [diff] [blame] | 207 | if (grp == ANDROID_TGROUP_BG_NONINTERACT) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 208 | ALOGD("setProcessGroup: vvv pid %d (%s)", pid, cmdline); |
San Mehat | a5109a8 | 2009-10-29 11:48:50 -0700 | [diff] [blame] | 209 | } else { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 210 | ALOGD("setProcessGroup: ^^^ pid %d (%s)", pid, cmdline); |
San Mehat | a5109a8 | 2009-10-29 11:48:50 -0700 | [diff] [blame] | 211 | } |
| 212 | #endif |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 213 | sprintf(proc_path, "/proc/%d/task", pid); |
| 214 | if (!(d = opendir(proc_path))) { |
San Mehat | 1fd0ec7 | 2009-06-15 16:56:52 -0700 | [diff] [blame] | 215 | // If the process exited on us, don't generate an exception |
| 216 | if (errno != ENOENT) |
| 217 | signalExceptionForGroupError(env, clazz, errno); |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 218 | return; |
| 219 | } |
| 220 | |
| 221 | while ((de = readdir(d))) { |
San Mehat | 7e63789 | 2009-08-06 13:19:19 -0700 | [diff] [blame] | 222 | int t_pid; |
| 223 | int t_pri; |
| 224 | |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 225 | if (de->d_name[0] == '.') |
| 226 | continue; |
San Mehat | 7e63789 | 2009-08-06 13:19:19 -0700 | [diff] [blame] | 227 | t_pid = atoi(de->d_name); |
San Mehat | 1fd0ec7 | 2009-06-15 16:56:52 -0700 | [diff] [blame] | 228 | |
San Mehat | 7e63789 | 2009-08-06 13:19:19 -0700 | [diff] [blame] | 229 | if (!t_pid) { |
| 230 | LOGE("Error getting pid for '%s'\n", de->d_name); |
| 231 | continue; |
| 232 | } |
| 233 | |
| 234 | t_pri = getpriority(PRIO_PROCESS, t_pid); |
| 235 | |
| 236 | if (grp == ANDROID_TGROUP_DEFAULT && |
| 237 | t_pri >= ANDROID_PRIORITY_BACKGROUND) { |
| 238 | // This task wants to stay at background |
| 239 | continue; |
| 240 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 241 | |
Dianne Hackborn | 84bb52e | 2010-09-03 17:07:07 -0700 | [diff] [blame] | 242 | if (androidSetThreadSchedulingGroup(t_pid, grp) != NO_ERROR) { |
San Mehat | 242d65b | 2009-09-12 10:10:37 -0700 | [diff] [blame] | 243 | signalExceptionForGroupError(env, clazz, errno); |
San Mehat | 0a42b81 | 2009-12-03 12:21:28 -0800 | [diff] [blame] | 244 | break; |
San Mehat | 242d65b | 2009-09-12 10:10:37 -0700 | [diff] [blame] | 245 | } |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 246 | } |
| 247 | closedir(d); |
| 248 | } |
| 249 | |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 250 | static void android_os_Process_setCanSelfBackground(JNIEnv* env, jobject clazz, jboolean bgOk) { |
| 251 | // Establishes the calling thread as illegal to put into the background. |
| 252 | // Typically used only for the system process's main looper. |
| 253 | #if GUARD_THREAD_PRIORITY |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 254 | ALOGV("Process.setCanSelfBackground(%d) : tid=%d", bgOk, androidGetTid()); |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 255 | { |
| 256 | Mutex::Autolock _l(gKeyCreateMutex); |
| 257 | if (gBgKey == -1) { |
| 258 | pthread_key_create(&gBgKey, NULL); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | // inverted: not-okay, we set a sentinel value |
| 263 | pthread_setspecific(gBgKey, (void*)(bgOk ? 0 : 0xbaad)); |
| 264 | #endif |
| 265 | } |
| 266 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | void android_os_Process_setThreadPriority(JNIEnv* env, jobject clazz, |
| 268 | jint pid, jint pri) |
| 269 | { |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 270 | #if GUARD_THREAD_PRIORITY |
| 271 | // if we're putting the current thread into the background, check the TLS |
| 272 | // to make sure this thread isn't guarded. If it is, raise an exception. |
| 273 | if (pri >= ANDROID_PRIORITY_BACKGROUND) { |
| 274 | if (pid == androidGetTid()) { |
| 275 | void* bgOk = pthread_getspecific(gBgKey); |
| 276 | if (bgOk == ((void*)0xbaad)) { |
| 277 | LOGE("Thread marked fg-only put self in background!"); |
| 278 | jniThrowException(env, "java/lang/SecurityException", "May not put this thread into background"); |
| 279 | return; |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | #endif |
| 284 | |
Dianne Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 285 | int rc = androidSetThreadPriority(pid, pri); |
| 286 | if (rc != 0) { |
| 287 | if (rc == INVALID_OPERATION) { |
| 288 | signalExceptionForPriorityError(env, clazz, errno); |
| 289 | } else { |
| 290 | signalExceptionForGroupError(env, clazz, errno); |
| 291 | } |
San Mehat | 242d65b | 2009-09-12 10:10:37 -0700 | [diff] [blame] | 292 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 293 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 294 | //ALOGI("Setting priority of %d: %d, getpriority returns %d\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | // pid, pri, getpriority(PRIO_PROCESS, pid)); |
| 296 | } |
| 297 | |
| 298 | void android_os_Process_setCallingThreadPriority(JNIEnv* env, jobject clazz, |
| 299 | jint pri) |
| 300 | { |
| 301 | jint tid = android_os_Process_myTid(env, clazz); |
| 302 | android_os_Process_setThreadPriority(env, clazz, tid, pri); |
| 303 | } |
| 304 | |
| 305 | jint android_os_Process_getThreadPriority(JNIEnv* env, jobject clazz, |
| 306 | jint pid) |
| 307 | { |
| 308 | errno = 0; |
| 309 | jint pri = getpriority(PRIO_PROCESS, pid); |
| 310 | if (errno != 0) { |
| 311 | signalExceptionForPriorityError(env, clazz, errno); |
| 312 | } |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 313 | //ALOGI("Returning priority of %d: %d\n", pid, pri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | return pri; |
| 315 | } |
| 316 | |
| 317 | jboolean android_os_Process_setOomAdj(JNIEnv* env, jobject clazz, |
| 318 | jint pid, jint adj) |
| 319 | { |
| 320 | #ifdef HAVE_OOM_ADJ |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 321 | char text[64]; |
| 322 | sprintf(text, "/proc/%d/oom_adj", pid); |
| 323 | int fd = open(text, O_WRONLY); |
| 324 | if (fd >= 0) { |
| 325 | sprintf(text, "%d", adj); |
| 326 | write(fd, text, strlen(text)); |
| 327 | close(fd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | } |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 329 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | #endif |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | void android_os_Process_setArgV0(JNIEnv* env, jobject clazz, jstring name) |
| 335 | { |
| 336 | if (name == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 337 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | return; |
| 339 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 340 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | const jchar* str = env->GetStringCritical(name, 0); |
| 342 | String8 name8; |
| 343 | if (str) { |
| 344 | name8 = String8(str, env->GetStringLength(name)); |
| 345 | env->ReleaseStringCritical(name, str); |
| 346 | } |
| 347 | |
| 348 | if (name8.size() > 0) { |
| 349 | ProcessState::self()->setArgV0(name8.string()); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | jint android_os_Process_setUid(JNIEnv* env, jobject clazz, jint uid) |
| 354 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | return setuid(uid) == 0 ? 0 : errno; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | jint android_os_Process_setGid(JNIEnv* env, jobject clazz, jint uid) |
| 359 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | return setgid(uid) == 0 ? 0 : errno; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | } |
| 362 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | static int pid_compare(const void* v1, const void* v2) |
| 364 | { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 365 | //ALOGI("Compare %d vs %d\n", *((const jint*)v1), *((const jint*)v2)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | return *((const jint*)v1) - *((const jint*)v2); |
| 367 | } |
| 368 | |
Marco Nelissen | 0bca96b | 2009-07-17 12:59:25 -0700 | [diff] [blame] | 369 | static jlong android_os_Process_getFreeMemory(JNIEnv* env, jobject clazz) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | { |
| 371 | int fd = open("/proc/meminfo", O_RDONLY); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 372 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | if (fd < 0) { |
| 374 | LOGW("Unable to open /proc/meminfo"); |
| 375 | return -1; |
| 376 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 377 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | char buffer[256]; |
| 379 | const int len = read(fd, buffer, sizeof(buffer)-1); |
| 380 | close(fd); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 381 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | if (len < 0) { |
| 383 | LOGW("Unable to read /proc/meminfo"); |
| 384 | return -1; |
| 385 | } |
| 386 | buffer[len] = 0; |
| 387 | |
| 388 | int numFound = 0; |
Marco Nelissen | 0bca96b | 2009-07-17 12:59:25 -0700 | [diff] [blame] | 389 | jlong mem = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 390 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | static const char* const sums[] = { "MemFree:", "Cached:", NULL }; |
| 392 | static const int sumsLen[] = { strlen("MemFree:"), strlen("Cached:"), NULL }; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 393 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | char* p = buffer; |
| 395 | while (*p && numFound < 2) { |
| 396 | int i = 0; |
| 397 | while (sums[i]) { |
| 398 | if (strncmp(p, sums[i], sumsLen[i]) == 0) { |
| 399 | p += sumsLen[i]; |
| 400 | while (*p == ' ') p++; |
| 401 | char* num = p; |
| 402 | while (*p >= '0' && *p <= '9') p++; |
| 403 | if (*p != 0) { |
| 404 | *p = 0; |
| 405 | p++; |
| 406 | if (*p == 0) p--; |
| 407 | } |
Marco Nelissen | 0bca96b | 2009-07-17 12:59:25 -0700 | [diff] [blame] | 408 | mem += atoll(num) * 1024; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | numFound++; |
| 410 | break; |
| 411 | } |
| 412 | i++; |
| 413 | } |
| 414 | p++; |
| 415 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 416 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | return numFound > 0 ? mem : -1; |
| 418 | } |
| 419 | |
| 420 | void android_os_Process_readProcLines(JNIEnv* env, jobject clazz, jstring fileStr, |
| 421 | jobjectArray reqFields, jlongArray outFields) |
| 422 | { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 423 | //ALOGI("getMemInfo: %p %p", reqFields, outFields); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 424 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | if (fileStr == NULL || reqFields == NULL || outFields == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 426 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | return; |
| 428 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 429 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | const char* file8 = env->GetStringUTFChars(fileStr, NULL); |
| 431 | if (file8 == NULL) { |
| 432 | return; |
| 433 | } |
| 434 | String8 file(file8); |
| 435 | env->ReleaseStringUTFChars(fileStr, file8); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 436 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | jsize count = env->GetArrayLength(reqFields); |
| 438 | if (count > env->GetArrayLength(outFields)) { |
| 439 | jniThrowException(env, "java/lang/IllegalArgumentException", "Array lengths differ"); |
| 440 | return; |
| 441 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 442 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | Vector<String8> fields; |
| 444 | int i; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 445 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | for (i=0; i<count; i++) { |
| 447 | jobject obj = env->GetObjectArrayElement(reqFields, i); |
| 448 | if (obj != NULL) { |
| 449 | const char* str8 = env->GetStringUTFChars((jstring)obj, NULL); |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 450 | //ALOGI("String at %d: %p = %s", i, obj, str8); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | if (str8 == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 452 | jniThrowNullPointerException(env, "Element in reqFields"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | return; |
| 454 | } |
| 455 | fields.add(String8(str8)); |
| 456 | env->ReleaseStringUTFChars((jstring)obj, str8); |
| 457 | } else { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 458 | jniThrowNullPointerException(env, "Element in reqFields"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | return; |
| 460 | } |
| 461 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 462 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | jlong* sizesArray = env->GetLongArrayElements(outFields, 0); |
| 464 | if (sizesArray == NULL) { |
| 465 | return; |
| 466 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 467 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 468 | //ALOGI("Clearing %d sizes", count); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | for (i=0; i<count; i++) { |
| 470 | sizesArray[i] = 0; |
| 471 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 472 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | int fd = open(file.string(), O_RDONLY); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 474 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | if (fd >= 0) { |
| 476 | const size_t BUFFER_SIZE = 2048; |
| 477 | char* buffer = (char*)malloc(BUFFER_SIZE); |
| 478 | int len = read(fd, buffer, BUFFER_SIZE-1); |
| 479 | close(fd); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 480 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | if (len < 0) { |
| 482 | LOGW("Unable to read %s", file.string()); |
| 483 | len = 0; |
| 484 | } |
| 485 | buffer[len] = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 486 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | int foundCount = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 488 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | char* p = buffer; |
| 490 | while (*p && foundCount < count) { |
| 491 | bool skipToEol = true; |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 492 | //ALOGI("Parsing at: %s", p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | for (i=0; i<count; i++) { |
| 494 | const String8& field = fields[i]; |
| 495 | if (strncmp(p, field.string(), field.length()) == 0) { |
| 496 | p += field.length(); |
Amith Yamasani | add868c | 2009-06-25 11:57:40 -0700 | [diff] [blame] | 497 | while (*p == ' ' || *p == '\t') p++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | char* num = p; |
| 499 | while (*p >= '0' && *p <= '9') p++; |
| 500 | skipToEol = *p != '\n'; |
| 501 | if (*p != 0) { |
| 502 | *p = 0; |
| 503 | p++; |
| 504 | } |
| 505 | char* end; |
| 506 | sizesArray[i] = strtoll(num, &end, 10); |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 507 | //ALOGI("Field %s = %d", field.string(), sizesArray[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | foundCount++; |
| 509 | break; |
| 510 | } |
| 511 | } |
| 512 | if (skipToEol) { |
| 513 | while (*p && *p != '\n') { |
| 514 | p++; |
| 515 | } |
| 516 | if (*p == '\n') { |
| 517 | p++; |
| 518 | } |
| 519 | } |
| 520 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 521 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | free(buffer); |
| 523 | } else { |
| 524 | LOGW("Unable to open %s", file.string()); |
| 525 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 526 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 527 | //ALOGI("Done!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | env->ReleaseLongArrayElements(outFields, sizesArray, 0); |
| 529 | } |
| 530 | |
| 531 | jintArray android_os_Process_getPids(JNIEnv* env, jobject clazz, |
| 532 | jstring file, jintArray lastArray) |
| 533 | { |
| 534 | if (file == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 535 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | return NULL; |
| 537 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 538 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 539 | const char* file8 = env->GetStringUTFChars(file, NULL); |
| 540 | if (file8 == NULL) { |
| 541 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 542 | return NULL; |
| 543 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 544 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 545 | DIR* dirp = opendir(file8); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 546 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | env->ReleaseStringUTFChars(file, file8); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 548 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | if(dirp == NULL) { |
| 550 | return NULL; |
| 551 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 552 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | jsize curCount = 0; |
| 554 | jint* curData = NULL; |
| 555 | if (lastArray != NULL) { |
| 556 | curCount = env->GetArrayLength(lastArray); |
| 557 | curData = env->GetIntArrayElements(lastArray, 0); |
| 558 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 559 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | jint curPos = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 561 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | struct dirent* entry; |
| 563 | while ((entry=readdir(dirp)) != NULL) { |
| 564 | const char* p = entry->d_name; |
| 565 | while (*p) { |
| 566 | if (*p < '0' || *p > '9') break; |
| 567 | p++; |
| 568 | } |
| 569 | if (*p != 0) continue; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 570 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | char* end; |
| 572 | int pid = strtol(entry->d_name, &end, 10); |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 573 | //ALOGI("File %s pid=%d\n", entry->d_name, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | if (curPos >= curCount) { |
| 575 | jsize newCount = (curCount == 0) ? 10 : (curCount*2); |
| 576 | jintArray newArray = env->NewIntArray(newCount); |
| 577 | if (newArray == NULL) { |
| 578 | closedir(dirp); |
| 579 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 580 | return NULL; |
| 581 | } |
| 582 | jint* newData = env->GetIntArrayElements(newArray, 0); |
| 583 | if (curData != NULL) { |
| 584 | memcpy(newData, curData, sizeof(jint)*curCount); |
| 585 | env->ReleaseIntArrayElements(lastArray, curData, 0); |
| 586 | } |
| 587 | lastArray = newArray; |
| 588 | curCount = newCount; |
| 589 | curData = newData; |
| 590 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 591 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | curData[curPos] = pid; |
| 593 | curPos++; |
| 594 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 595 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | closedir(dirp); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 597 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | if (curData != NULL && curPos > 0) { |
| 599 | qsort(curData, curPos, sizeof(jint), pid_compare); |
| 600 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 601 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 602 | while (curPos < curCount) { |
| 603 | curData[curPos] = -1; |
| 604 | curPos++; |
| 605 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 606 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | if (curData != NULL) { |
| 608 | env->ReleaseIntArrayElements(lastArray, curData, 0); |
| 609 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 610 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | return lastArray; |
| 612 | } |
| 613 | |
| 614 | enum { |
| 615 | PROC_TERM_MASK = 0xff, |
| 616 | PROC_ZERO_TERM = 0, |
| 617 | PROC_SPACE_TERM = ' ', |
| 618 | PROC_COMBINE = 0x100, |
| 619 | PROC_PARENS = 0x200, |
| 620 | PROC_OUT_STRING = 0x1000, |
| 621 | PROC_OUT_LONG = 0x2000, |
| 622 | PROC_OUT_FLOAT = 0x4000, |
| 623 | }; |
| 624 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 625 | jboolean android_os_Process_parseProcLineArray(JNIEnv* env, jobject clazz, |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 626 | char* buffer, jint startIndex, jint endIndex, jintArray format, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 627 | jobjectArray outStrings, jlongArray outLongs, jfloatArray outFloats) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 629 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 630 | const jsize NF = env->GetArrayLength(format); |
| 631 | const jsize NS = outStrings ? env->GetArrayLength(outStrings) : 0; |
| 632 | const jsize NL = outLongs ? env->GetArrayLength(outLongs) : 0; |
| 633 | const jsize NR = outFloats ? env->GetArrayLength(outFloats) : 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 634 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | jint* formatData = env->GetIntArrayElements(format, 0); |
| 636 | jlong* longsData = outLongs ? |
| 637 | env->GetLongArrayElements(outLongs, 0) : NULL; |
| 638 | jfloat* floatsData = outFloats ? |
| 639 | env->GetFloatArrayElements(outFloats, 0) : NULL; |
| 640 | if (formatData == NULL || (NL > 0 && longsData == NULL) |
| 641 | || (NR > 0 && floatsData == NULL)) { |
| 642 | if (formatData != NULL) { |
| 643 | env->ReleaseIntArrayElements(format, formatData, 0); |
| 644 | } |
| 645 | if (longsData != NULL) { |
| 646 | env->ReleaseLongArrayElements(outLongs, longsData, 0); |
| 647 | } |
| 648 | if (floatsData != NULL) { |
| 649 | env->ReleaseFloatArrayElements(outFloats, floatsData, 0); |
| 650 | } |
| 651 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 652 | return JNI_FALSE; |
| 653 | } |
| 654 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 655 | jsize i = startIndex; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 656 | jsize di = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 657 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 658 | jboolean res = JNI_TRUE; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 659 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 660 | for (jsize fi=0; fi<NF; fi++) { |
| 661 | const jint mode = formatData[fi]; |
| 662 | if ((mode&PROC_PARENS) != 0) { |
| 663 | i++; |
| 664 | } |
| 665 | const char term = (char)(mode&PROC_TERM_MASK); |
| 666 | const jsize start = i; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 667 | if (i >= endIndex) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | res = JNI_FALSE; |
| 669 | break; |
| 670 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 671 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | jsize end = -1; |
| 673 | if ((mode&PROC_PARENS) != 0) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 674 | while (buffer[i] != ')' && i < endIndex) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | i++; |
| 676 | } |
| 677 | end = i; |
| 678 | i++; |
| 679 | } |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 680 | while (buffer[i] != term && i < endIndex) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | i++; |
| 682 | } |
| 683 | if (end < 0) { |
| 684 | end = i; |
| 685 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 686 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 687 | if (i < endIndex) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 688 | i++; |
| 689 | if ((mode&PROC_COMBINE) != 0) { |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 690 | while (buffer[i] == term && i < endIndex) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | i++; |
| 692 | } |
| 693 | } |
| 694 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 695 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 696 | //ALOGI("Field %d: %d-%d dest=%d mode=0x%x\n", i, start, end, di, mode); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 697 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | if ((mode&(PROC_OUT_FLOAT|PROC_OUT_LONG|PROC_OUT_STRING)) != 0) { |
| 699 | char c = buffer[end]; |
| 700 | buffer[end] = 0; |
| 701 | if ((mode&PROC_OUT_FLOAT) != 0 && di < NR) { |
| 702 | char* end; |
| 703 | floatsData[di] = strtof(buffer+start, &end); |
| 704 | } |
| 705 | if ((mode&PROC_OUT_LONG) != 0 && di < NL) { |
| 706 | char* end; |
| 707 | longsData[di] = strtoll(buffer+start, &end, 10); |
| 708 | } |
| 709 | if ((mode&PROC_OUT_STRING) != 0 && di < NS) { |
| 710 | jstring str = env->NewStringUTF(buffer+start); |
| 711 | env->SetObjectArrayElement(outStrings, di, str); |
| 712 | } |
| 713 | buffer[end] = c; |
| 714 | di++; |
| 715 | } |
| 716 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 717 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | env->ReleaseIntArrayElements(format, formatData, 0); |
| 719 | if (longsData != NULL) { |
| 720 | env->ReleaseLongArrayElements(outLongs, longsData, 0); |
| 721 | } |
| 722 | if (floatsData != NULL) { |
| 723 | env->ReleaseFloatArrayElements(outFloats, floatsData, 0); |
| 724 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 725 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | return res; |
| 727 | } |
| 728 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 729 | jboolean android_os_Process_parseProcLine(JNIEnv* env, jobject clazz, |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 730 | jbyteArray buffer, jint startIndex, jint endIndex, jintArray format, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 731 | jobjectArray outStrings, jlongArray outLongs, jfloatArray outFloats) |
| 732 | { |
| 733 | jbyte* bufferArray = env->GetByteArrayElements(buffer, NULL); |
| 734 | |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 735 | jboolean result = android_os_Process_parseProcLineArray(env, clazz, |
| 736 | (char*) bufferArray, startIndex, endIndex, format, outStrings, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 737 | outLongs, outFloats); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 738 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 739 | env->ReleaseByteArrayElements(buffer, bufferArray, 0); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 740 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 741 | return result; |
| 742 | } |
| 743 | |
| 744 | jboolean android_os_Process_readProcFile(JNIEnv* env, jobject clazz, |
| 745 | jstring file, jintArray format, jobjectArray outStrings, |
| 746 | jlongArray outLongs, jfloatArray outFloats) |
| 747 | { |
| 748 | if (file == NULL || format == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 749 | jniThrowNullPointerException(env, NULL); |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 750 | return JNI_FALSE; |
| 751 | } |
| 752 | |
| 753 | const char* file8 = env->GetStringUTFChars(file, NULL); |
| 754 | if (file8 == NULL) { |
| 755 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 756 | return JNI_FALSE; |
| 757 | } |
| 758 | int fd = open(file8, O_RDONLY); |
| 759 | env->ReleaseStringUTFChars(file, file8); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 760 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 761 | if (fd < 0) { |
| 762 | //LOGW("Unable to open process file: %s\n", file8); |
| 763 | return JNI_FALSE; |
| 764 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 765 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 766 | char buffer[256]; |
| 767 | const int len = read(fd, buffer, sizeof(buffer)-1); |
| 768 | close(fd); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 769 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 770 | if (len < 0) { |
| 771 | //LOGW("Unable to open process file: %s fd=%d\n", file8, fd); |
| 772 | return JNI_FALSE; |
| 773 | } |
| 774 | buffer[len] = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 775 | |
| 776 | return android_os_Process_parseProcLineArray(env, clazz, buffer, 0, len, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 777 | format, outStrings, outLongs, outFloats); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 778 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 779 | } |
| 780 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 781 | void android_os_Process_setApplicationObject(JNIEnv* env, jobject clazz, |
| 782 | jobject binderObject) |
| 783 | { |
| 784 | if (binderObject == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 785 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 786 | return; |
| 787 | } |
| 788 | |
| 789 | sp<IBinder> binder = ibinderForJavaObject(env, binderObject); |
| 790 | } |
| 791 | |
| 792 | void android_os_Process_sendSignal(JNIEnv* env, jobject clazz, jint pid, jint sig) |
| 793 | { |
| 794 | if (pid > 0) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 795 | ALOGI("Sending signal. PID: %d SIG: %d", pid, sig); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | kill(pid, sig); |
| 797 | } |
| 798 | } |
| 799 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 800 | void android_os_Process_sendSignalQuiet(JNIEnv* env, jobject clazz, jint pid, jint sig) |
| 801 | { |
| 802 | if (pid > 0) { |
| 803 | kill(pid, sig); |
| 804 | } |
| 805 | } |
| 806 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | static jlong android_os_Process_getElapsedCpuTime(JNIEnv* env, jobject clazz) |
| 808 | { |
| 809 | struct timespec ts; |
| 810 | |
| 811 | int res = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 812 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 813 | if (res != 0) { |
| 814 | return (jlong) 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 815 | } |
| 816 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | nsecs_t when = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec; |
| 818 | return (jlong) nanoseconds_to_milliseconds(when); |
| 819 | } |
| 820 | |
| 821 | static jlong android_os_Process_getPss(JNIEnv* env, jobject clazz, jint pid) |
| 822 | { |
| 823 | char filename[64]; |
| 824 | |
| 825 | snprintf(filename, sizeof(filename), "/proc/%d/smaps", pid); |
| 826 | |
| 827 | FILE * file = fopen(filename, "r"); |
| 828 | if (!file) { |
| 829 | return (jlong) -1; |
| 830 | } |
| 831 | |
| 832 | // Tally up all of the Pss from the various maps |
| 833 | char line[256]; |
| 834 | jlong pss = 0; |
| 835 | while (fgets(line, sizeof(line), file)) { |
| 836 | jlong v; |
| 837 | if (sscanf(line, "Pss: %lld kB", &v) == 1) { |
| 838 | pss += v; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | fclose(file); |
| 843 | |
| 844 | // Return the Pss value in bytes, not kilobytes |
| 845 | return pss * 1024; |
| 846 | } |
| 847 | |
| 848 | static const JNINativeMethod methods[] = { |
| 849 | {"myPid", "()I", (void*)android_os_Process_myPid}, |
| 850 | {"myTid", "()I", (void*)android_os_Process_myTid}, |
| 851 | {"myUid", "()I", (void*)android_os_Process_myUid}, |
| 852 | {"getUidForName", "(Ljava/lang/String;)I", (void*)android_os_Process_getUidForName}, |
| 853 | {"getGidForName", "(Ljava/lang/String;)I", (void*)android_os_Process_getGidForName}, |
| 854 | {"setThreadPriority", "(II)V", (void*)android_os_Process_setThreadPriority}, |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 855 | {"setCanSelfBackground", "(Z)V", (void*)android_os_Process_setCanSelfBackground}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 856 | {"setThreadPriority", "(I)V", (void*)android_os_Process_setCallingThreadPriority}, |
| 857 | {"getThreadPriority", "(I)I", (void*)android_os_Process_getThreadPriority}, |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 858 | {"setThreadGroup", "(II)V", (void*)android_os_Process_setThreadGroup}, |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 859 | {"setProcessGroup", "(II)V", (void*)android_os_Process_setProcessGroup}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 860 | {"setOomAdj", "(II)Z", (void*)android_os_Process_setOomAdj}, |
| 861 | {"setArgV0", "(Ljava/lang/String;)V", (void*)android_os_Process_setArgV0}, |
| 862 | {"setUid", "(I)I", (void*)android_os_Process_setUid}, |
| 863 | {"setGid", "(I)I", (void*)android_os_Process_setGid}, |
| 864 | {"sendSignal", "(II)V", (void*)android_os_Process_sendSignal}, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 865 | {"sendSignalQuiet", "(II)V", (void*)android_os_Process_sendSignalQuiet}, |
Marco Nelissen | 0bca96b | 2009-07-17 12:59:25 -0700 | [diff] [blame] | 866 | {"getFreeMemory", "()J", (void*)android_os_Process_getFreeMemory}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | {"readProcLines", "(Ljava/lang/String;[Ljava/lang/String;[J)V", (void*)android_os_Process_readProcLines}, |
| 868 | {"getPids", "(Ljava/lang/String;[I)[I", (void*)android_os_Process_getPids}, |
| 869 | {"readProcFile", "(Ljava/lang/String;[I[Ljava/lang/String;[J[F)Z", (void*)android_os_Process_readProcFile}, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 870 | {"parseProcLine", "([BII[I[Ljava/lang/String;[J[F)Z", (void*)android_os_Process_parseProcLine}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | {"getElapsedCpuTime", "()J", (void*)android_os_Process_getElapsedCpuTime}, |
| 872 | {"getPss", "(I)J", (void*)android_os_Process_getPss}, |
| 873 | //{"setApplicationObject", "(Landroid/os/IBinder;)V", (void*)android_os_Process_setApplicationObject}, |
| 874 | }; |
| 875 | |
| 876 | const char* const kProcessPathName = "android/os/Process"; |
| 877 | |
| 878 | int register_android_os_Process(JNIEnv* env) |
| 879 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | return AndroidRuntime::registerNativeMethods( |
| 881 | env, kProcessPathName, |
| 882 | methods, NELEM(methods)); |
| 883 | } |