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