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