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