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 | |
Martijn Coenen | cd4bdf3 | 2016-03-03 17:30:52 +0100 | [diff] [blame] | 20 | // To make sure cpu_set_t is included from sched.h |
| 21 | #define _GNU_SOURCE 1 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <utils/Log.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 23 | #include <binder/IPCThreadState.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 24 | #include <binder/IServiceManager.h> |
Narayan Kamath | a23fcd7 | 2014-03-28 13:39:21 +0000 | [diff] [blame] | 25 | #include <cutils/process_name.h> |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 26 | #include <cutils/sched_policy.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | #include <utils/String8.h> |
| 28 | #include <utils/Vector.h> |
Colin Cross | 0769e55 | 2014-06-03 13:25:35 -0700 | [diff] [blame] | 29 | #include <processgroup/processgroup.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 31 | #include "core_jni_helpers.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
| 33 | #include "android_util_Binder.h" |
| 34 | #include "JNIHelp.h" |
| 35 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | #include <dirent.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <grp.h> |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 39 | #include <inttypes.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | #include <pwd.h> |
| 41 | #include <signal.h> |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 42 | #include <sys/errno.h> |
| 43 | #include <sys/resource.h> |
| 44 | #include <sys/stat.h> |
| 45 | #include <sys/types.h> |
Glenn Kasten | 6af763b | 2011-05-04 17:58:57 -0700 | [diff] [blame] | 46 | #include <unistd.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 48 | #define GUARD_THREAD_PRIORITY 0 |
San Mehat | a5109a8 | 2009-10-29 11:48:50 -0700 | [diff] [blame] | 49 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | using namespace android; |
| 51 | |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 52 | static const bool kDebugPolicy = false; |
| 53 | static const bool kDebugProc = false; |
| 54 | |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 55 | #if GUARD_THREAD_PRIORITY |
| 56 | Mutex gKeyCreateMutex; |
| 57 | static pthread_key_t gBgKey = -1; |
| 58 | #endif |
| 59 | |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 60 | // For both of these, err should be in the errno range (positive), not a status_t (negative) |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 61 | static void signalExceptionForError(JNIEnv* env, int err, int tid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | switch (err) { |
| 63 | case EINVAL: |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 64 | jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", |
| 65 | "Invalid argument: %d", tid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | break; |
| 67 | case ESRCH: |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 68 | jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", |
| 69 | "Given thread %d does not exist", tid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | break; |
| 71 | case EPERM: |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 72 | jniThrowExceptionFmt(env, "java/lang/SecurityException", |
| 73 | "No permission to modify given thread %d", tid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | break; |
| 75 | default: |
| 76 | jniThrowException(env, "java/lang/RuntimeException", "Unknown error"); |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 81 | static void signalExceptionForPriorityError(JNIEnv* env, int err, int tid) { |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 82 | switch (err) { |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 83 | case EACCES: |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 84 | jniThrowExceptionFmt(env, "java/lang/SecurityException", |
| 85 | "No permission to set the priority of %d", tid); |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 86 | break; |
| 87 | default: |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 88 | signalExceptionForError(env, err, tid); |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | } |
| 93 | |
| 94 | static void signalExceptionForGroupError(JNIEnv* env, int err, int tid) { |
| 95 | switch (err) { |
| 96 | case EACCES: |
| 97 | jniThrowExceptionFmt(env, "java/lang/SecurityException", |
| 98 | "No permission to set the group of %d", tid); |
| 99 | break; |
| 100 | default: |
| 101 | signalExceptionForError(env, err, tid); |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 102 | break; |
| 103 | } |
| 104 | } |
| 105 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | jint android_os_Process_getUidForName(JNIEnv* env, jobject clazz, jstring name) |
| 107 | { |
| 108 | if (name == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 109 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | return -1; |
| 111 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 112 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | const jchar* str16 = env->GetStringCritical(name, 0); |
| 114 | String8 name8; |
| 115 | if (str16) { |
Dan Albert | 6698749 | 2014-11-20 11:41:21 -0800 | [diff] [blame] | 116 | name8 = String8(reinterpret_cast<const char16_t*>(str16), |
| 117 | env->GetStringLength(name)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | env->ReleaseStringCritical(name, str16); |
| 119 | } |
| 120 | |
| 121 | const size_t N = name8.size(); |
| 122 | if (N > 0) { |
| 123 | const char* str = name8.string(); |
| 124 | for (size_t i=0; i<N; i++) { |
| 125 | if (str[i] < '0' || str[i] > '9') { |
| 126 | struct passwd* pwd = getpwnam(str); |
| 127 | if (pwd == NULL) { |
| 128 | return -1; |
| 129 | } |
| 130 | return pwd->pw_uid; |
| 131 | } |
| 132 | } |
| 133 | return atoi(str); |
| 134 | } |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | jint android_os_Process_getGidForName(JNIEnv* env, jobject clazz, jstring name) |
| 139 | { |
| 140 | if (name == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 141 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | return -1; |
| 143 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 144 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | const jchar* str16 = env->GetStringCritical(name, 0); |
| 146 | String8 name8; |
| 147 | if (str16) { |
Dan Albert | 6698749 | 2014-11-20 11:41:21 -0800 | [diff] [blame] | 148 | name8 = String8(reinterpret_cast<const char16_t*>(str16), |
| 149 | env->GetStringLength(name)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | env->ReleaseStringCritical(name, str16); |
| 151 | } |
| 152 | |
| 153 | const size_t N = name8.size(); |
| 154 | if (N > 0) { |
| 155 | const char* str = name8.string(); |
| 156 | for (size_t i=0; i<N; i++) { |
| 157 | if (str[i] < '0' || str[i] > '9') { |
| 158 | struct group* grp = getgrnam(str); |
| 159 | if (grp == NULL) { |
| 160 | return -1; |
| 161 | } |
| 162 | return grp->gr_gid; |
| 163 | } |
| 164 | } |
| 165 | return atoi(str); |
| 166 | } |
| 167 | return -1; |
| 168 | } |
| 169 | |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 170 | 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] | 171 | { |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 172 | ALOGV("%s tid=%d grp=%" PRId32, __func__, tid, grp); |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 173 | SchedPolicy sp = (SchedPolicy) grp; |
| 174 | int res = set_sched_policy(tid, sp); |
Dianne Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 175 | if (res != NO_ERROR) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 176 | signalExceptionForGroupError(env, -res, tid); |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 177 | } |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 180 | 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] | 181 | { |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 182 | ALOGV("%s pid=%d grp=%" PRId32, __func__, pid, grp); |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 183 | DIR *d; |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 184 | char proc_path[255]; |
| 185 | struct dirent *de; |
| 186 | |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 187 | if ((grp == SP_FOREGROUND) || (grp > SP_MAX)) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 188 | signalExceptionForGroupError(env, EINVAL, pid); |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 189 | return; |
| 190 | } |
| 191 | |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 192 | bool isDefault = false; |
| 193 | if (grp < 0) { |
| 194 | grp = SP_FOREGROUND; |
| 195 | isDefault = true; |
| 196 | } |
| 197 | SchedPolicy sp = (SchedPolicy) grp; |
| 198 | |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 199 | if (kDebugPolicy) { |
| 200 | char cmdline[32]; |
| 201 | int fd; |
San Mehat | a5109a8 | 2009-10-29 11:48:50 -0700 | [diff] [blame] | 202 | |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 203 | strcpy(cmdline, "unknown"); |
San Mehat | a5109a8 | 2009-10-29 11:48:50 -0700 | [diff] [blame] | 204 | |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 205 | sprintf(proc_path, "/proc/%d/cmdline", pid); |
| 206 | fd = open(proc_path, O_RDONLY); |
| 207 | if (fd >= 0) { |
| 208 | int rc = read(fd, cmdline, sizeof(cmdline)-1); |
| 209 | cmdline[rc] = 0; |
| 210 | close(fd); |
| 211 | } |
| 212 | |
| 213 | if (sp == SP_BACKGROUND) { |
| 214 | ALOGD("setProcessGroup: vvv pid %d (%s)", pid, cmdline); |
| 215 | } else { |
| 216 | ALOGD("setProcessGroup: ^^^ pid %d (%s)", pid, cmdline); |
| 217 | } |
San Mehat | a5109a8 | 2009-10-29 11:48:50 -0700 | [diff] [blame] | 218 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 219 | |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 220 | sprintf(proc_path, "/proc/%d/task", pid); |
| 221 | if (!(d = opendir(proc_path))) { |
San Mehat | 1fd0ec7 | 2009-06-15 16:56:52 -0700 | [diff] [blame] | 222 | // If the process exited on us, don't generate an exception |
| 223 | if (errno != ENOENT) |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 224 | signalExceptionForGroupError(env, errno, pid); |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 225 | return; |
| 226 | } |
| 227 | |
| 228 | while ((de = readdir(d))) { |
San Mehat | 7e63789 | 2009-08-06 13:19:19 -0700 | [diff] [blame] | 229 | int t_pid; |
| 230 | int t_pri; |
| 231 | |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 232 | if (de->d_name[0] == '.') |
| 233 | continue; |
San Mehat | 7e63789 | 2009-08-06 13:19:19 -0700 | [diff] [blame] | 234 | t_pid = atoi(de->d_name); |
San Mehat | 1fd0ec7 | 2009-06-15 16:56:52 -0700 | [diff] [blame] | 235 | |
San Mehat | 7e63789 | 2009-08-06 13:19:19 -0700 | [diff] [blame] | 236 | if (!t_pid) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 237 | ALOGE("Error getting pid for '%s'\n", de->d_name); |
San Mehat | 7e63789 | 2009-08-06 13:19:19 -0700 | [diff] [blame] | 238 | continue; |
| 239 | } |
| 240 | |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 241 | t_pri = getpriority(PRIO_PROCESS, t_pid); |
San Mehat | 7e63789 | 2009-08-06 13:19:19 -0700 | [diff] [blame] | 242 | |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 243 | if (t_pri <= ANDROID_PRIORITY_AUDIO) { |
| 244 | int scheduler = sched_getscheduler(t_pid); |
| 245 | if ((scheduler == SCHED_FIFO) || (scheduler == SCHED_RR)) { |
Tim Murray | 9e41c74 | 2015-06-08 14:56:53 -0700 | [diff] [blame] | 246 | // This task wants to stay in its current audio group so it can keep its budget |
| 247 | // don't update its cpuset or cgroup |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 248 | continue; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | if (isDefault) { |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 253 | if (t_pri >= ANDROID_PRIORITY_BACKGROUND) { |
| 254 | // This task wants to stay at background |
Tim Murray | 9e41c74 | 2015-06-08 14:56:53 -0700 | [diff] [blame] | 255 | // update its cpuset so it doesn't only run on bg core(s) |
| 256 | #ifdef ENABLE_CPUSETS |
| 257 | int err = set_cpuset_policy(t_pid, sp); |
| 258 | if (err != NO_ERROR) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 259 | signalExceptionForGroupError(env, -err, t_pid); |
Tim Murray | 9e41c74 | 2015-06-08 14:56:53 -0700 | [diff] [blame] | 260 | break; |
| 261 | } |
| 262 | #endif |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 263 | continue; |
| 264 | } |
San Mehat | 7e63789 | 2009-08-06 13:19:19 -0700 | [diff] [blame] | 265 | } |
Tim Murray | 9e41c74 | 2015-06-08 14:56:53 -0700 | [diff] [blame] | 266 | int err; |
| 267 | #ifdef ENABLE_CPUSETS |
| 268 | // set both cpuset and cgroup for general threads |
| 269 | err = set_cpuset_policy(t_pid, sp); |
Glenn Kasten | f1b5644 | 2012-03-15 16:33:43 -0700 | [diff] [blame] | 270 | if (err != NO_ERROR) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 271 | signalExceptionForGroupError(env, -err, t_pid); |
San Mehat | 0a42b81 | 2009-12-03 12:21:28 -0800 | [diff] [blame] | 272 | break; |
San Mehat | 242d65b | 2009-09-12 10:10:37 -0700 | [diff] [blame] | 273 | } |
Tim Murray | 9e41c74 | 2015-06-08 14:56:53 -0700 | [diff] [blame] | 274 | #endif |
| 275 | |
| 276 | err = set_sched_policy(t_pid, sp); |
| 277 | if (err != NO_ERROR) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 278 | signalExceptionForGroupError(env, -err, t_pid); |
Tim Murray | 9e41c74 | 2015-06-08 14:56:53 -0700 | [diff] [blame] | 279 | break; |
| 280 | } |
| 281 | |
San Mehat | 3e45824 | 2009-05-19 14:44:16 -0700 | [diff] [blame] | 282 | } |
| 283 | closedir(d); |
| 284 | } |
| 285 | |
Jeff Sharkey | 9e57c41 | 2013-01-17 14:12:41 -0800 | [diff] [blame] | 286 | jint android_os_Process_getProcessGroup(JNIEnv* env, jobject clazz, jint pid) |
| 287 | { |
| 288 | SchedPolicy sp; |
| 289 | if (get_sched_policy(pid, &sp) != 0) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 290 | signalExceptionForGroupError(env, errno, pid); |
Jeff Sharkey | 9e57c41 | 2013-01-17 14:12:41 -0800 | [diff] [blame] | 291 | } |
| 292 | return (int) sp; |
| 293 | } |
| 294 | |
Martijn Coenen | cd4bdf3 | 2016-03-03 17:30:52 +0100 | [diff] [blame] | 295 | #ifdef ENABLE_CPUSETS |
| 296 | /** Sample CPUset list format: |
| 297 | * 0-3,4,6-8 |
| 298 | */ |
| 299 | static void parse_cpuset_cpus(char *cpus, cpu_set_t *cpu_set) { |
| 300 | unsigned int start, end, matched, i; |
| 301 | char *cpu_range = strtok(cpus, ","); |
| 302 | while (cpu_range != NULL) { |
| 303 | start = end = 0; |
| 304 | matched = sscanf(cpu_range, "%u-%u", &start, &end); |
| 305 | cpu_range = strtok(NULL, ","); |
| 306 | if (start >= CPU_SETSIZE) { |
| 307 | ALOGE("parse_cpuset_cpus: ignoring CPU number larger than %d.", CPU_SETSIZE); |
| 308 | continue; |
| 309 | } else if (end >= CPU_SETSIZE) { |
| 310 | ALOGE("parse_cpuset_cpus: ignoring CPU numbers larger than %d.", CPU_SETSIZE); |
| 311 | end = CPU_SETSIZE - 1; |
| 312 | } |
| 313 | if (matched == 1) { |
| 314 | CPU_SET(start, cpu_set); |
| 315 | } else if (matched == 2) { |
| 316 | for (i = start; i <= end; i++) { |
| 317 | CPU_SET(i, cpu_set); |
| 318 | } |
| 319 | } else { |
| 320 | ALOGE("Failed to match cpus"); |
| 321 | } |
| 322 | } |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Stores the CPUs assigned to the cpuset corresponding to the |
| 328 | * SchedPolicy in the passed in cpu_set. |
| 329 | */ |
| 330 | static void get_cpuset_cores_for_policy(SchedPolicy policy, cpu_set_t *cpu_set) |
| 331 | { |
| 332 | FILE *file; |
| 333 | const char *filename; |
| 334 | |
| 335 | CPU_ZERO(cpu_set); |
| 336 | |
| 337 | switch (policy) { |
| 338 | case SP_BACKGROUND: |
| 339 | filename = "/dev/cpuset/background/cpus"; |
| 340 | break; |
| 341 | case SP_FOREGROUND: |
| 342 | case SP_AUDIO_APP: |
| 343 | case SP_AUDIO_SYS: |
| 344 | filename = "/dev/cpuset/foreground/cpus"; |
| 345 | break; |
| 346 | case SP_TOP_APP: |
| 347 | filename = "/dev/cpuset/top-app/cpus"; |
| 348 | break; |
| 349 | default: |
| 350 | filename = NULL; |
| 351 | } |
| 352 | |
| 353 | if (!filename) return; |
| 354 | |
| 355 | file = fopen(filename, "re"); |
| 356 | if (file != NULL) { |
| 357 | // Parse cpus string |
| 358 | char *line = NULL; |
| 359 | size_t len = 0; |
| 360 | ssize_t num_read = getline(&line, &len, file); |
| 361 | fclose (file); |
| 362 | if (num_read > 0) { |
| 363 | parse_cpuset_cpus(line, cpu_set); |
| 364 | } else { |
| 365 | ALOGE("Failed to read %s", filename); |
| 366 | } |
| 367 | free(line); |
| 368 | } |
| 369 | return; |
| 370 | } |
| 371 | #endif |
| 372 | |
| 373 | |
| 374 | /** |
| 375 | * Determine CPU cores exclusively assigned to the |
| 376 | * cpuset corresponding to the SchedPolicy and store |
| 377 | * them in the passed in cpu_set_t |
| 378 | */ |
| 379 | void get_exclusive_cpuset_cores(SchedPolicy policy, cpu_set_t *cpu_set) { |
| 380 | #ifdef ENABLE_CPUSETS |
| 381 | int i; |
| 382 | cpu_set_t tmp_set; |
| 383 | get_cpuset_cores_for_policy(policy, cpu_set); |
| 384 | for (i = 0; i < SP_CNT; i++) { |
| 385 | if ((SchedPolicy) i == policy) continue; |
| 386 | get_cpuset_cores_for_policy((SchedPolicy)i, &tmp_set); |
| 387 | // First get cores exclusive to one set or the other |
| 388 | CPU_XOR(&tmp_set, cpu_set, &tmp_set); |
| 389 | // Then get the ones only in cpu_set |
| 390 | CPU_AND(cpu_set, cpu_set, &tmp_set); |
| 391 | } |
| 392 | #else |
| 393 | (void) policy; |
| 394 | CPU_ZERO(cpu_set); |
| 395 | #endif |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | jintArray android_os_Process_getExclusiveCores(JNIEnv* env, jobject clazz) { |
| 400 | SchedPolicy sp; |
| 401 | cpu_set_t cpu_set; |
| 402 | jintArray cpus; |
| 403 | int pid = getpid(); |
| 404 | if (get_sched_policy(pid, &sp) != 0) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 405 | signalExceptionForGroupError(env, errno, pid); |
Martijn Coenen | cd4bdf3 | 2016-03-03 17:30:52 +0100 | [diff] [blame] | 406 | return NULL; |
| 407 | } |
| 408 | get_exclusive_cpuset_cores(sp, &cpu_set); |
| 409 | int num_cpus = CPU_COUNT(&cpu_set); |
| 410 | cpus = env->NewIntArray(num_cpus); |
| 411 | if (cpus == NULL) { |
| 412 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 413 | return NULL; |
| 414 | } |
| 415 | |
| 416 | jint* cpu_elements = env->GetIntArrayElements(cpus, 0); |
| 417 | int count = 0; |
| 418 | for (int i = 0; i < CPU_SETSIZE && count < num_cpus; i++) { |
| 419 | if (CPU_ISSET(i, &cpu_set)) { |
| 420 | cpu_elements[count++] = i; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | env->ReleaseIntArrayElements(cpus, cpu_elements, 0); |
| 425 | return cpus; |
| 426 | } |
| 427 | |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 428 | static void android_os_Process_setCanSelfBackground(JNIEnv* env, jobject clazz, jboolean bgOk) { |
| 429 | // Establishes the calling thread as illegal to put into the background. |
| 430 | // Typically used only for the system process's main looper. |
| 431 | #if GUARD_THREAD_PRIORITY |
Elliott Hughes | 06451fe | 2014-08-18 10:26:52 -0700 | [diff] [blame] | 432 | ALOGV("Process.setCanSelfBackground(%d) : tid=%d", bgOk, gettid()); |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 433 | { |
| 434 | Mutex::Autolock _l(gKeyCreateMutex); |
| 435 | if (gBgKey == -1) { |
| 436 | pthread_key_create(&gBgKey, NULL); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | // inverted: not-okay, we set a sentinel value |
| 441 | pthread_setspecific(gBgKey, (void*)(bgOk ? 0 : 0xbaad)); |
| 442 | #endif |
| 443 | } |
| 444 | |
Srinath Sridharan | 1b15d13 | 2016-07-19 15:16:11 -0700 | [diff] [blame] | 445 | jint android_os_Process_getThreadScheduler(JNIEnv* env, jclass clazz, |
| 446 | jint tid) |
| 447 | { |
| 448 | int policy = 0; |
| 449 | // linux has sched_getscheduler(), others don't. |
| 450 | #if defined(__linux__) |
| 451 | errno = 0; |
| 452 | policy = sched_getscheduler(tid); |
| 453 | if (errno != 0) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 454 | signalExceptionForPriorityError(env, errno, tid); |
Srinath Sridharan | 1b15d13 | 2016-07-19 15:16:11 -0700 | [diff] [blame] | 455 | } |
| 456 | #else |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 457 | signalExceptionForPriorityError(env, ENOSYS, tid); |
Srinath Sridharan | 1b15d13 | 2016-07-19 15:16:11 -0700 | [diff] [blame] | 458 | #endif |
| 459 | return policy; |
| 460 | } |
| 461 | |
Glenn Kasten | 6793ac9 | 2011-07-13 12:44:12 -0700 | [diff] [blame] | 462 | void android_os_Process_setThreadScheduler(JNIEnv* env, jclass clazz, |
| 463 | jint tid, jint policy, jint pri) |
| 464 | { |
Yabin Cui | 65b4a68 | 2014-11-10 12:15:46 -0800 | [diff] [blame] | 465 | // linux has sched_setscheduler(), others don't. |
| 466 | #if defined(__linux__) |
Glenn Kasten | 6793ac9 | 2011-07-13 12:44:12 -0700 | [diff] [blame] | 467 | struct sched_param param; |
| 468 | param.sched_priority = pri; |
| 469 | int rc = sched_setscheduler(tid, policy, ¶m); |
| 470 | if (rc) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 471 | signalExceptionForPriorityError(env, errno, tid); |
Glenn Kasten | 6793ac9 | 2011-07-13 12:44:12 -0700 | [diff] [blame] | 472 | } |
Glenn Kasten | cc76719 | 2012-01-17 08:38:51 -0800 | [diff] [blame] | 473 | #else |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 474 | signalExceptionForPriorityError(env, ENOSYS, tid); |
Glenn Kasten | cc76719 | 2012-01-17 08:38:51 -0800 | [diff] [blame] | 475 | #endif |
Glenn Kasten | 6793ac9 | 2011-07-13 12:44:12 -0700 | [diff] [blame] | 476 | } |
| 477 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | void android_os_Process_setThreadPriority(JNIEnv* env, jobject clazz, |
| 479 | jint pid, jint pri) |
| 480 | { |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 481 | #if GUARD_THREAD_PRIORITY |
| 482 | // if we're putting the current thread into the background, check the TLS |
| 483 | // to make sure this thread isn't guarded. If it is, raise an exception. |
| 484 | if (pri >= ANDROID_PRIORITY_BACKGROUND) { |
Elliott Hughes | 06451fe | 2014-08-18 10:26:52 -0700 | [diff] [blame] | 485 | if (pid == gettid()) { |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 486 | void* bgOk = pthread_getspecific(gBgKey); |
| 487 | if (bgOk == ((void*)0xbaad)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 488 | ALOGE("Thread marked fg-only put self in background!"); |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 489 | jniThrowException(env, "java/lang/SecurityException", "May not put this thread into background"); |
| 490 | return; |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | #endif |
| 495 | |
Dianne Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 496 | int rc = androidSetThreadPriority(pid, pri); |
| 497 | if (rc != 0) { |
| 498 | if (rc == INVALID_OPERATION) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 499 | signalExceptionForPriorityError(env, errno, pid); |
Dianne Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 500 | } else { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 501 | signalExceptionForGroupError(env, errno, pid); |
Dianne Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 502 | } |
San Mehat | 242d65b | 2009-09-12 10:10:37 -0700 | [diff] [blame] | 503 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 504 | |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 505 | //ALOGI("Setting priority of %" PRId32 ": %" PRId32 ", getpriority returns %d\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | // pid, pri, getpriority(PRIO_PROCESS, pid)); |
| 507 | } |
| 508 | |
| 509 | void android_os_Process_setCallingThreadPriority(JNIEnv* env, jobject clazz, |
| 510 | jint pri) |
| 511 | { |
Elliott Hughes | 06451fe | 2014-08-18 10:26:52 -0700 | [diff] [blame] | 512 | android_os_Process_setThreadPriority(env, clazz, gettid(), pri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | jint android_os_Process_getThreadPriority(JNIEnv* env, jobject clazz, |
| 516 | jint pid) |
| 517 | { |
| 518 | errno = 0; |
| 519 | jint pri = getpriority(PRIO_PROCESS, pid); |
| 520 | if (errno != 0) { |
Ruben Brunk | 4c0c4df | 2016-08-09 12:46:13 -0700 | [diff] [blame] | 521 | signalExceptionForPriorityError(env, errno, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | } |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 523 | //ALOGI("Returning priority of %" PRId32 ": %" PRId32 "\n", pid, pri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | return pri; |
| 525 | } |
| 526 | |
Rom Lemarchand | 5534ba9 | 2013-07-12 16:15:36 -0700 | [diff] [blame] | 527 | jboolean android_os_Process_setSwappiness(JNIEnv *env, jobject clazz, |
| 528 | jint pid, jboolean is_increased) |
| 529 | { |
| 530 | char text[64]; |
| 531 | |
| 532 | if (is_increased) { |
| 533 | strcpy(text, "/sys/fs/cgroup/memory/sw/tasks"); |
| 534 | } else { |
| 535 | strcpy(text, "/sys/fs/cgroup/memory/tasks"); |
| 536 | } |
| 537 | |
| 538 | struct stat st; |
| 539 | if (stat(text, &st) || !S_ISREG(st.st_mode)) { |
| 540 | return false; |
| 541 | } |
| 542 | |
| 543 | int fd = open(text, O_WRONLY); |
| 544 | if (fd >= 0) { |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 545 | sprintf(text, "%" PRId32, pid); |
Rom Lemarchand | 5534ba9 | 2013-07-12 16:15:36 -0700 | [diff] [blame] | 546 | write(fd, text, strlen(text)); |
| 547 | close(fd); |
| 548 | } |
| 549 | |
| 550 | return true; |
| 551 | } |
| 552 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | void android_os_Process_setArgV0(JNIEnv* env, jobject clazz, jstring name) |
| 554 | { |
| 555 | if (name == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 556 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | return; |
| 558 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 559 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | const jchar* str = env->GetStringCritical(name, 0); |
| 561 | String8 name8; |
| 562 | if (str) { |
Dan Albert | 6698749 | 2014-11-20 11:41:21 -0800 | [diff] [blame] | 563 | name8 = String8(reinterpret_cast<const char16_t*>(str), |
| 564 | env->GetStringLength(name)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | env->ReleaseStringCritical(name, str); |
| 566 | } |
| 567 | |
Dmitriy Filchenko | 342c7dc | 2016-07-12 15:40:54 -0700 | [diff] [blame] | 568 | if (!name8.isEmpty()) { |
Dmitriy Filchenko | f5b6e55 | 2016-07-18 16:00:35 -0700 | [diff] [blame] | 569 | AndroidRuntime::getRuntime()->setArgv0(name8.string(), true /* setProcName */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
| 573 | jint android_os_Process_setUid(JNIEnv* env, jobject clazz, jint uid) |
| 574 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | return setuid(uid) == 0 ? 0 : errno; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | jint android_os_Process_setGid(JNIEnv* env, jobject clazz, jint uid) |
| 579 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | return setgid(uid) == 0 ? 0 : errno; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | } |
| 582 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | static int pid_compare(const void* v1, const void* v2) |
| 584 | { |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 585 | //ALOGI("Compare %" PRId32 " vs %" PRId32 "\n", *((const jint*)v1), *((const jint*)v2)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | return *((const jint*)v1) - *((const jint*)v2); |
| 587 | } |
| 588 | |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 589 | static jlong getFreeMemoryImpl(const char* const sums[], const size_t sumsLen[], size_t num) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | { |
| 591 | int fd = open("/proc/meminfo", O_RDONLY); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 592 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | if (fd < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 594 | ALOGW("Unable to open /proc/meminfo"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | return -1; |
| 596 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 597 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | char buffer[256]; |
| 599 | const int len = read(fd, buffer, sizeof(buffer)-1); |
| 600 | close(fd); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 601 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 602 | if (len < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 603 | ALOGW("Unable to read /proc/meminfo"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 604 | return -1; |
| 605 | } |
| 606 | buffer[len] = 0; |
| 607 | |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 608 | size_t numFound = 0; |
Marco Nelissen | 0bca96b | 2009-07-17 12:59:25 -0700 | [diff] [blame] | 609 | jlong mem = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 610 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | char* p = buffer; |
Dianne Hackborn | 59325eb | 2012-05-09 18:45:20 -0700 | [diff] [blame] | 612 | while (*p && numFound < num) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | int i = 0; |
| 614 | while (sums[i]) { |
| 615 | if (strncmp(p, sums[i], sumsLen[i]) == 0) { |
| 616 | p += sumsLen[i]; |
| 617 | while (*p == ' ') p++; |
| 618 | char* num = p; |
| 619 | while (*p >= '0' && *p <= '9') p++; |
| 620 | if (*p != 0) { |
| 621 | *p = 0; |
| 622 | p++; |
| 623 | if (*p == 0) p--; |
| 624 | } |
Marco Nelissen | 0bca96b | 2009-07-17 12:59:25 -0700 | [diff] [blame] | 625 | mem += atoll(num) * 1024; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | numFound++; |
| 627 | break; |
| 628 | } |
| 629 | i++; |
| 630 | } |
| 631 | p++; |
| 632 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 633 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 634 | return numFound > 0 ? mem : -1; |
| 635 | } |
| 636 | |
Dianne Hackborn | 59325eb | 2012-05-09 18:45:20 -0700 | [diff] [blame] | 637 | static jlong android_os_Process_getFreeMemory(JNIEnv* env, jobject clazz) |
| 638 | { |
| 639 | static const char* const sums[] = { "MemFree:", "Cached:", NULL }; |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 640 | static const size_t sumsLen[] = { strlen("MemFree:"), strlen("Cached:"), 0 }; |
Dianne Hackborn | 59325eb | 2012-05-09 18:45:20 -0700 | [diff] [blame] | 641 | return getFreeMemoryImpl(sums, sumsLen, 2); |
| 642 | } |
| 643 | |
| 644 | static jlong android_os_Process_getTotalMemory(JNIEnv* env, jobject clazz) |
| 645 | { |
| 646 | static const char* const sums[] = { "MemTotal:", NULL }; |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 647 | static const size_t sumsLen[] = { strlen("MemTotal:"), 0 }; |
Dianne Hackborn | 59325eb | 2012-05-09 18:45:20 -0700 | [diff] [blame] | 648 | return getFreeMemoryImpl(sums, sumsLen, 1); |
| 649 | } |
| 650 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 651 | void android_os_Process_readProcLines(JNIEnv* env, jobject clazz, jstring fileStr, |
| 652 | jobjectArray reqFields, jlongArray outFields) |
| 653 | { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 654 | //ALOGI("getMemInfo: %p %p", reqFields, outFields); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 655 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 656 | if (fileStr == NULL || reqFields == NULL || outFields == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 657 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 658 | return; |
| 659 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 660 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 661 | const char* file8 = env->GetStringUTFChars(fileStr, NULL); |
| 662 | if (file8 == NULL) { |
| 663 | return; |
| 664 | } |
| 665 | String8 file(file8); |
| 666 | env->ReleaseStringUTFChars(fileStr, file8); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 667 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | jsize count = env->GetArrayLength(reqFields); |
| 669 | if (count > env->GetArrayLength(outFields)) { |
| 670 | jniThrowException(env, "java/lang/IllegalArgumentException", "Array lengths differ"); |
| 671 | return; |
| 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 | Vector<String8> fields; |
| 675 | int i; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 676 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 | for (i=0; i<count; i++) { |
| 678 | jobject obj = env->GetObjectArrayElement(reqFields, i); |
| 679 | if (obj != NULL) { |
| 680 | const char* str8 = env->GetStringUTFChars((jstring)obj, NULL); |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 681 | //ALOGI("String at %d: %p = %s", i, obj, str8); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 682 | if (str8 == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 683 | jniThrowNullPointerException(env, "Element in reqFields"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | return; |
| 685 | } |
| 686 | fields.add(String8(str8)); |
| 687 | env->ReleaseStringUTFChars((jstring)obj, str8); |
| 688 | } else { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 689 | jniThrowNullPointerException(env, "Element in reqFields"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | return; |
| 691 | } |
| 692 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 693 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 694 | jlong* sizesArray = env->GetLongArrayElements(outFields, 0); |
| 695 | if (sizesArray == NULL) { |
| 696 | return; |
| 697 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 698 | |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 699 | //ALOGI("Clearing %" PRId32 " sizes", count); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 700 | for (i=0; i<count; i++) { |
| 701 | sizesArray[i] = 0; |
| 702 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 703 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | int fd = open(file.string(), O_RDONLY); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 705 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | if (fd >= 0) { |
| 707 | const size_t BUFFER_SIZE = 2048; |
| 708 | char* buffer = (char*)malloc(BUFFER_SIZE); |
| 709 | int len = read(fd, buffer, BUFFER_SIZE-1); |
| 710 | close(fd); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 711 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | if (len < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 713 | ALOGW("Unable to read %s", file.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | len = 0; |
| 715 | } |
| 716 | buffer[len] = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 717 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | int foundCount = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 719 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 720 | char* p = buffer; |
| 721 | while (*p && foundCount < count) { |
| 722 | bool skipToEol = true; |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 723 | //ALOGI("Parsing at: %s", p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 724 | for (i=0; i<count; i++) { |
| 725 | const String8& field = fields[i]; |
| 726 | if (strncmp(p, field.string(), field.length()) == 0) { |
| 727 | p += field.length(); |
Amith Yamasani | add868c | 2009-06-25 11:57:40 -0700 | [diff] [blame] | 728 | while (*p == ' ' || *p == '\t') p++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | char* num = p; |
| 730 | while (*p >= '0' && *p <= '9') p++; |
| 731 | skipToEol = *p != '\n'; |
| 732 | if (*p != 0) { |
| 733 | *p = 0; |
| 734 | p++; |
| 735 | } |
| 736 | char* end; |
| 737 | sizesArray[i] = strtoll(num, &end, 10); |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 738 | //ALOGI("Field %s = %" PRId64, field.string(), sizesArray[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 739 | foundCount++; |
| 740 | break; |
| 741 | } |
| 742 | } |
| 743 | if (skipToEol) { |
| 744 | while (*p && *p != '\n') { |
| 745 | p++; |
| 746 | } |
| 747 | if (*p == '\n') { |
| 748 | p++; |
| 749 | } |
| 750 | } |
| 751 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 752 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 753 | free(buffer); |
| 754 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 755 | ALOGW("Unable to open %s", file.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 756 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 757 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 758 | //ALOGI("Done!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 759 | env->ReleaseLongArrayElements(outFields, sizesArray, 0); |
| 760 | } |
| 761 | |
| 762 | jintArray android_os_Process_getPids(JNIEnv* env, jobject clazz, |
| 763 | jstring file, jintArray lastArray) |
| 764 | { |
| 765 | if (file == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 766 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 767 | return NULL; |
| 768 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 769 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 770 | const char* file8 = env->GetStringUTFChars(file, NULL); |
| 771 | if (file8 == NULL) { |
| 772 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 773 | return NULL; |
| 774 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 775 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 776 | DIR* dirp = opendir(file8); |
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 | env->ReleaseStringUTFChars(file, file8); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 779 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 780 | if(dirp == NULL) { |
| 781 | return NULL; |
| 782 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 783 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 784 | jsize curCount = 0; |
| 785 | jint* curData = NULL; |
| 786 | if (lastArray != NULL) { |
| 787 | curCount = env->GetArrayLength(lastArray); |
| 788 | curData = env->GetIntArrayElements(lastArray, 0); |
| 789 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 790 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 791 | jint curPos = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 792 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 793 | struct dirent* entry; |
| 794 | while ((entry=readdir(dirp)) != NULL) { |
| 795 | const char* p = entry->d_name; |
| 796 | while (*p) { |
| 797 | if (*p < '0' || *p > '9') break; |
| 798 | p++; |
| 799 | } |
| 800 | if (*p != 0) continue; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 801 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 802 | char* end; |
| 803 | int pid = strtol(entry->d_name, &end, 10); |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 804 | //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] | 805 | if (curPos >= curCount) { |
| 806 | jsize newCount = (curCount == 0) ? 10 : (curCount*2); |
| 807 | jintArray newArray = env->NewIntArray(newCount); |
| 808 | if (newArray == NULL) { |
| 809 | closedir(dirp); |
| 810 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 811 | return NULL; |
| 812 | } |
| 813 | jint* newData = env->GetIntArrayElements(newArray, 0); |
| 814 | if (curData != NULL) { |
| 815 | memcpy(newData, curData, sizeof(jint)*curCount); |
| 816 | env->ReleaseIntArrayElements(lastArray, curData, 0); |
| 817 | } |
| 818 | lastArray = newArray; |
| 819 | curCount = newCount; |
| 820 | curData = newData; |
| 821 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 822 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 823 | curData[curPos] = pid; |
| 824 | curPos++; |
| 825 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 826 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 827 | closedir(dirp); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 828 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | if (curData != NULL && curPos > 0) { |
| 830 | qsort(curData, curPos, sizeof(jint), pid_compare); |
| 831 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 832 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 833 | while (curPos < curCount) { |
| 834 | curData[curPos] = -1; |
| 835 | curPos++; |
| 836 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 837 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 838 | if (curData != NULL) { |
| 839 | env->ReleaseIntArrayElements(lastArray, curData, 0); |
| 840 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 841 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 842 | return lastArray; |
| 843 | } |
| 844 | |
| 845 | enum { |
| 846 | PROC_TERM_MASK = 0xff, |
| 847 | PROC_ZERO_TERM = 0, |
| 848 | PROC_SPACE_TERM = ' ', |
| 849 | PROC_COMBINE = 0x100, |
| 850 | PROC_PARENS = 0x200, |
Dianne Hackborn | 13ac041 | 2013-06-25 19:34:49 -0700 | [diff] [blame] | 851 | PROC_QUOTES = 0x400, |
Dianne Hackborn | 4de5a3a | 2016-06-20 15:20:15 -0700 | [diff] [blame] | 852 | PROC_CHAR = 0x800, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 853 | PROC_OUT_STRING = 0x1000, |
| 854 | PROC_OUT_LONG = 0x2000, |
| 855 | PROC_OUT_FLOAT = 0x4000, |
| 856 | }; |
| 857 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 858 | jboolean android_os_Process_parseProcLineArray(JNIEnv* env, jobject clazz, |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 859 | char* buffer, jint startIndex, jint endIndex, jintArray format, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 860 | jobjectArray outStrings, jlongArray outLongs, jfloatArray outFloats) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 862 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 863 | const jsize NF = env->GetArrayLength(format); |
| 864 | const jsize NS = outStrings ? env->GetArrayLength(outStrings) : 0; |
| 865 | const jsize NL = outLongs ? env->GetArrayLength(outLongs) : 0; |
| 866 | const jsize NR = outFloats ? env->GetArrayLength(outFloats) : 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 867 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 868 | jint* formatData = env->GetIntArrayElements(format, 0); |
| 869 | jlong* longsData = outLongs ? |
| 870 | env->GetLongArrayElements(outLongs, 0) : NULL; |
| 871 | jfloat* floatsData = outFloats ? |
| 872 | env->GetFloatArrayElements(outFloats, 0) : NULL; |
| 873 | if (formatData == NULL || (NL > 0 && longsData == NULL) |
| 874 | || (NR > 0 && floatsData == NULL)) { |
| 875 | if (formatData != NULL) { |
| 876 | env->ReleaseIntArrayElements(format, formatData, 0); |
| 877 | } |
| 878 | if (longsData != NULL) { |
| 879 | env->ReleaseLongArrayElements(outLongs, longsData, 0); |
| 880 | } |
| 881 | if (floatsData != NULL) { |
| 882 | env->ReleaseFloatArrayElements(outFloats, floatsData, 0); |
| 883 | } |
| 884 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 885 | return JNI_FALSE; |
| 886 | } |
| 887 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 888 | jsize i = startIndex; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 889 | jsize di = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 890 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 891 | jboolean res = JNI_TRUE; |
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 | for (jsize fi=0; fi<NF; fi++) { |
Dianne Hackborn | 13ac041 | 2013-06-25 19:34:49 -0700 | [diff] [blame] | 894 | jint mode = formatData[fi]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 895 | if ((mode&PROC_PARENS) != 0) { |
| 896 | i++; |
Bernhard Rosenkränzer | 3b1e22e | 2014-11-17 22:30:56 +0100 | [diff] [blame] | 897 | } else if ((mode&PROC_QUOTES) != 0) { |
Dianne Hackborn | 13ac041 | 2013-06-25 19:34:49 -0700 | [diff] [blame] | 898 | if (buffer[i] == '"') { |
| 899 | i++; |
| 900 | } else { |
| 901 | mode &= ~PROC_QUOTES; |
| 902 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 903 | } |
| 904 | const char term = (char)(mode&PROC_TERM_MASK); |
| 905 | const jsize start = i; |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 906 | if (i >= endIndex) { |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 907 | if (kDebugProc) { |
| 908 | ALOGW("Ran off end of data @%d", i); |
| 909 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 910 | res = JNI_FALSE; |
| 911 | break; |
| 912 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 913 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 914 | jsize end = -1; |
| 915 | if ((mode&PROC_PARENS) != 0) { |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 916 | while (i < endIndex && buffer[i] != ')') { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 917 | i++; |
| 918 | } |
| 919 | end = i; |
| 920 | i++; |
Dianne Hackborn | 13ac041 | 2013-06-25 19:34:49 -0700 | [diff] [blame] | 921 | } else if ((mode&PROC_QUOTES) != 0) { |
| 922 | while (buffer[i] != '"' && i < endIndex) { |
| 923 | i++; |
| 924 | } |
| 925 | end = i; |
| 926 | i++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 927 | } |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 928 | while (i < endIndex && buffer[i] != term) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | i++; |
| 930 | } |
| 931 | if (end < 0) { |
| 932 | end = i; |
| 933 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 934 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 935 | if (i < endIndex) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | i++; |
| 937 | if ((mode&PROC_COMBINE) != 0) { |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 938 | while (i < endIndex && buffer[i] == term) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | i++; |
| 940 | } |
| 941 | } |
| 942 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 943 | |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 944 | //ALOGI("Field %" PRId32 ": %" PRId32 "-%" PRId32 " dest=%" PRId32 " mode=0x%" PRIx32 "\n", i, start, end, di, mode); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 945 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 946 | if ((mode&(PROC_OUT_FLOAT|PROC_OUT_LONG|PROC_OUT_STRING)) != 0) { |
| 947 | char c = buffer[end]; |
| 948 | buffer[end] = 0; |
| 949 | if ((mode&PROC_OUT_FLOAT) != 0 && di < NR) { |
| 950 | char* end; |
| 951 | floatsData[di] = strtof(buffer+start, &end); |
| 952 | } |
| 953 | if ((mode&PROC_OUT_LONG) != 0 && di < NL) { |
Dianne Hackborn | 4de5a3a | 2016-06-20 15:20:15 -0700 | [diff] [blame] | 954 | if ((mode&PROC_CHAR) != 0) { |
| 955 | // Caller wants single first character returned as one long. |
| 956 | longsData[di] = buffer[start]; |
| 957 | } else { |
| 958 | char* end; |
| 959 | longsData[di] = strtoll(buffer+start, &end, 10); |
| 960 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | } |
| 962 | if ((mode&PROC_OUT_STRING) != 0 && di < NS) { |
| 963 | jstring str = env->NewStringUTF(buffer+start); |
| 964 | env->SetObjectArrayElement(outStrings, di, str); |
| 965 | } |
| 966 | buffer[end] = c; |
| 967 | di++; |
| 968 | } |
| 969 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 970 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 971 | env->ReleaseIntArrayElements(format, formatData, 0); |
| 972 | if (longsData != NULL) { |
| 973 | env->ReleaseLongArrayElements(outLongs, longsData, 0); |
| 974 | } |
| 975 | if (floatsData != NULL) { |
| 976 | env->ReleaseFloatArrayElements(outFloats, floatsData, 0); |
| 977 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 978 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | return res; |
| 980 | } |
| 981 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 982 | jboolean android_os_Process_parseProcLine(JNIEnv* env, jobject clazz, |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 983 | jbyteArray buffer, jint startIndex, jint endIndex, jintArray format, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 984 | jobjectArray outStrings, jlongArray outLongs, jfloatArray outFloats) |
| 985 | { |
| 986 | jbyte* bufferArray = env->GetByteArrayElements(buffer, NULL); |
| 987 | |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 988 | jboolean result = android_os_Process_parseProcLineArray(env, clazz, |
| 989 | (char*) bufferArray, startIndex, endIndex, format, outStrings, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 990 | outLongs, outFloats); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 991 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 992 | env->ReleaseByteArrayElements(buffer, bufferArray, 0); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 993 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 994 | return result; |
| 995 | } |
| 996 | |
| 997 | jboolean android_os_Process_readProcFile(JNIEnv* env, jobject clazz, |
| 998 | jstring file, jintArray format, jobjectArray outStrings, |
| 999 | jlongArray outLongs, jfloatArray outFloats) |
| 1000 | { |
| 1001 | if (file == NULL || format == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1002 | jniThrowNullPointerException(env, NULL); |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1003 | return JNI_FALSE; |
| 1004 | } |
| 1005 | |
| 1006 | const char* file8 = env->GetStringUTFChars(file, NULL); |
| 1007 | if (file8 == NULL) { |
| 1008 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 1009 | return JNI_FALSE; |
| 1010 | } |
| 1011 | int fd = open(file8, O_RDONLY); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1012 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1013 | if (fd < 0) { |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1014 | if (kDebugProc) { |
| 1015 | ALOGW("Unable to open process file: %s\n", file8); |
| 1016 | } |
Dianne Hackborn | 306af67 | 2014-06-24 15:41:03 -0700 | [diff] [blame] | 1017 | env->ReleaseStringUTFChars(file, file8); |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1018 | return JNI_FALSE; |
| 1019 | } |
Dianne Hackborn | 306af67 | 2014-06-24 15:41:03 -0700 | [diff] [blame] | 1020 | env->ReleaseStringUTFChars(file, file8); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1021 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1022 | char buffer[256]; |
| 1023 | const int len = read(fd, buffer, sizeof(buffer)-1); |
| 1024 | close(fd); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1025 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1026 | if (len < 0) { |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1027 | if (kDebugProc) { |
| 1028 | ALOGW("Unable to open process file: %s fd=%d\n", file8, fd); |
| 1029 | } |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1030 | return JNI_FALSE; |
| 1031 | } |
| 1032 | buffer[len] = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1033 | |
| 1034 | return android_os_Process_parseProcLineArray(env, clazz, buffer, 0, len, |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1035 | format, outStrings, outLongs, outFloats); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1036 | |
Evan Millar | c64edde | 2009-04-18 12:26:32 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1039 | void android_os_Process_setApplicationObject(JNIEnv* env, jobject clazz, |
| 1040 | jobject binderObject) |
| 1041 | { |
| 1042 | if (binderObject == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1043 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1044 | return; |
| 1045 | } |
| 1046 | |
| 1047 | sp<IBinder> binder = ibinderForJavaObject(env, binderObject); |
| 1048 | } |
| 1049 | |
| 1050 | void android_os_Process_sendSignal(JNIEnv* env, jobject clazz, jint pid, jint sig) |
| 1051 | { |
| 1052 | if (pid > 0) { |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 1053 | ALOGI("Sending signal. PID: %" PRId32 " SIG: %" PRId32, pid, sig); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1054 | kill(pid, sig); |
| 1055 | } |
| 1056 | } |
| 1057 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1058 | void android_os_Process_sendSignalQuiet(JNIEnv* env, jobject clazz, jint pid, jint sig) |
| 1059 | { |
| 1060 | if (pid > 0) { |
| 1061 | kill(pid, sig); |
| 1062 | } |
| 1063 | } |
| 1064 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1065 | static jlong android_os_Process_getElapsedCpuTime(JNIEnv* env, jobject clazz) |
| 1066 | { |
| 1067 | struct timespec ts; |
| 1068 | |
| 1069 | int res = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1070 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1071 | if (res != 0) { |
| 1072 | return (jlong) 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1075 | nsecs_t when = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec; |
| 1076 | return (jlong) nanoseconds_to_milliseconds(when); |
| 1077 | } |
| 1078 | |
| 1079 | static jlong android_os_Process_getPss(JNIEnv* env, jobject clazz, jint pid) |
| 1080 | { |
| 1081 | char filename[64]; |
| 1082 | |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 1083 | snprintf(filename, sizeof(filename), "/proc/%" PRId32 "/smaps", pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1084 | |
| 1085 | FILE * file = fopen(filename, "r"); |
| 1086 | if (!file) { |
| 1087 | return (jlong) -1; |
| 1088 | } |
| 1089 | |
| 1090 | // Tally up all of the Pss from the various maps |
| 1091 | char line[256]; |
| 1092 | jlong pss = 0; |
| 1093 | while (fgets(line, sizeof(line), file)) { |
| 1094 | jlong v; |
Mark Salyzyn | c6a4101 | 2014-04-24 13:05:18 -0700 | [diff] [blame] | 1095 | if (sscanf(line, "Pss: %" SCNd64 " kB", &v) == 1) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1096 | pss += v; |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | fclose(file); |
| 1101 | |
| 1102 | // Return the Pss value in bytes, not kilobytes |
| 1103 | return pss * 1024; |
| 1104 | } |
| 1105 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 1106 | jintArray android_os_Process_getPidsForCommands(JNIEnv* env, jobject clazz, |
| 1107 | jobjectArray commandNames) |
| 1108 | { |
| 1109 | if (commandNames == NULL) { |
| 1110 | jniThrowNullPointerException(env, NULL); |
| 1111 | return NULL; |
| 1112 | } |
| 1113 | |
| 1114 | Vector<String8> commands; |
| 1115 | |
| 1116 | jsize count = env->GetArrayLength(commandNames); |
| 1117 | |
| 1118 | for (int i=0; i<count; i++) { |
| 1119 | jobject obj = env->GetObjectArrayElement(commandNames, i); |
| 1120 | if (obj != NULL) { |
| 1121 | const char* str8 = env->GetStringUTFChars((jstring)obj, NULL); |
| 1122 | if (str8 == NULL) { |
| 1123 | jniThrowNullPointerException(env, "Element in commandNames"); |
| 1124 | return NULL; |
| 1125 | } |
| 1126 | commands.add(String8(str8)); |
| 1127 | env->ReleaseStringUTFChars((jstring)obj, str8); |
| 1128 | } else { |
| 1129 | jniThrowNullPointerException(env, "Element in commandNames"); |
| 1130 | return NULL; |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | Vector<jint> pids; |
| 1135 | |
| 1136 | DIR *proc = opendir("/proc"); |
| 1137 | if (proc == NULL) { |
| 1138 | fprintf(stderr, "/proc: %s\n", strerror(errno)); |
| 1139 | return NULL; |
| 1140 | } |
| 1141 | |
| 1142 | struct dirent *d; |
| 1143 | while ((d = readdir(proc))) { |
| 1144 | int pid = atoi(d->d_name); |
| 1145 | if (pid <= 0) continue; |
| 1146 | |
| 1147 | char path[PATH_MAX]; |
| 1148 | char data[PATH_MAX]; |
| 1149 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); |
| 1150 | |
| 1151 | int fd = open(path, O_RDONLY); |
| 1152 | if (fd < 0) { |
| 1153 | continue; |
| 1154 | } |
| 1155 | const int len = read(fd, data, sizeof(data)-1); |
| 1156 | close(fd); |
| 1157 | |
| 1158 | if (len < 0) { |
| 1159 | continue; |
| 1160 | } |
| 1161 | data[len] = 0; |
| 1162 | |
| 1163 | for (int i=0; i<len; i++) { |
| 1164 | if (data[i] == ' ') { |
| 1165 | data[i] = 0; |
| 1166 | break; |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | for (size_t i=0; i<commands.size(); i++) { |
| 1171 | if (commands[i] == data) { |
| 1172 | pids.add(pid); |
| 1173 | break; |
| 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | closedir(proc); |
| 1179 | |
| 1180 | jintArray pidArray = env->NewIntArray(pids.size()); |
| 1181 | if (pidArray == NULL) { |
| 1182 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 1183 | return NULL; |
| 1184 | } |
| 1185 | |
| 1186 | if (pids.size() > 0) { |
| 1187 | env->SetIntArrayRegion(pidArray, 0, pids.size(), pids.array()); |
| 1188 | } |
| 1189 | |
| 1190 | return pidArray; |
| 1191 | } |
| 1192 | |
Colin Cross | 0769e55 | 2014-06-03 13:25:35 -0700 | [diff] [blame] | 1193 | jint android_os_Process_killProcessGroup(JNIEnv* env, jobject clazz, jint uid, jint pid) |
| 1194 | { |
| 1195 | return killProcessGroup(uid, pid, SIGKILL); |
| 1196 | } |
| 1197 | |
| 1198 | void android_os_Process_removeAllProcessGroups(JNIEnv* env, jobject clazz) |
| 1199 | { |
| 1200 | return removeAllProcessGroups(); |
| 1201 | } |
| 1202 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1203 | static const JNINativeMethod methods[] = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1204 | {"getUidForName", "(Ljava/lang/String;)I", (void*)android_os_Process_getUidForName}, |
| 1205 | {"getGidForName", "(Ljava/lang/String;)I", (void*)android_os_Process_getGidForName}, |
| 1206 | {"setThreadPriority", "(II)V", (void*)android_os_Process_setThreadPriority}, |
Glenn Kasten | 6793ac9 | 2011-07-13 12:44:12 -0700 | [diff] [blame] | 1207 | {"setThreadScheduler", "(III)V", (void*)android_os_Process_setThreadScheduler}, |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 1208 | {"setCanSelfBackground", "(Z)V", (void*)android_os_Process_setCanSelfBackground}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1209 | {"setThreadPriority", "(I)V", (void*)android_os_Process_setCallingThreadPriority}, |
| 1210 | {"getThreadPriority", "(I)I", (void*)android_os_Process_getThreadPriority}, |
Srinath Sridharan | 1b15d13 | 2016-07-19 15:16:11 -0700 | [diff] [blame] | 1211 | {"getThreadScheduler", "(I)I", (void*)android_os_Process_getThreadScheduler}, |
San Mehat | e9d376b | 2009-04-21 14:06:36 -0700 | [diff] [blame] | 1212 | {"setThreadGroup", "(II)V", (void*)android_os_Process_setThreadGroup}, |
Jeff Sharkey | 9e57c41 | 2013-01-17 14:12:41 -0800 | [diff] [blame] | 1213 | {"setProcessGroup", "(II)V", (void*)android_os_Process_setProcessGroup}, |
| 1214 | {"getProcessGroup", "(I)I", (void*)android_os_Process_getProcessGroup}, |
Martijn Coenen | cd4bdf3 | 2016-03-03 17:30:52 +0100 | [diff] [blame] | 1215 | {"getExclusiveCores", "()[I", (void*)android_os_Process_getExclusiveCores}, |
Rom Lemarchand | 5534ba9 | 2013-07-12 16:15:36 -0700 | [diff] [blame] | 1216 | {"setSwappiness", "(IZ)Z", (void*)android_os_Process_setSwappiness}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1217 | {"setArgV0", "(Ljava/lang/String;)V", (void*)android_os_Process_setArgV0}, |
| 1218 | {"setUid", "(I)I", (void*)android_os_Process_setUid}, |
| 1219 | {"setGid", "(I)I", (void*)android_os_Process_setGid}, |
| 1220 | {"sendSignal", "(II)V", (void*)android_os_Process_sendSignal}, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1221 | {"sendSignalQuiet", "(II)V", (void*)android_os_Process_sendSignalQuiet}, |
Marco Nelissen | 0bca96b | 2009-07-17 12:59:25 -0700 | [diff] [blame] | 1222 | {"getFreeMemory", "()J", (void*)android_os_Process_getFreeMemory}, |
Dianne Hackborn | 59325eb | 2012-05-09 18:45:20 -0700 | [diff] [blame] | 1223 | {"getTotalMemory", "()J", (void*)android_os_Process_getTotalMemory}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1224 | {"readProcLines", "(Ljava/lang/String;[Ljava/lang/String;[J)V", (void*)android_os_Process_readProcLines}, |
| 1225 | {"getPids", "(Ljava/lang/String;[I)[I", (void*)android_os_Process_getPids}, |
| 1226 | {"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] | 1227 | {"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] | 1228 | {"getElapsedCpuTime", "()J", (void*)android_os_Process_getElapsedCpuTime}, |
| 1229 | {"getPss", "(I)J", (void*)android_os_Process_getPss}, |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 1230 | {"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] | 1231 | //{"setApplicationObject", "(Landroid/os/IBinder;)V", (void*)android_os_Process_setApplicationObject}, |
Colin Cross | 0769e55 | 2014-06-03 13:25:35 -0700 | [diff] [blame] | 1232 | {"killProcessGroup", "(II)I", (void*)android_os_Process_killProcessGroup}, |
| 1233 | {"removeAllProcessGroups", "()V", (void*)android_os_Process_removeAllProcessGroups}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1234 | }; |
| 1235 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1236 | int register_android_os_Process(JNIEnv* env) |
| 1237 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1238 | return RegisterMethodsOrDie(env, "android/os/Process", methods, NELEM(methods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1239 | } |