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