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