android_util_Process.cpp revision 403475a894de8b898c1e0bafdeae2e2f7dd19951
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 <dirent.h> 36#include <fcntl.h> 37#include <grp.h> 38#include <pwd.h> 39#include <signal.h> 40#include <unistd.h> 41 42#define POLICY_DEBUG 0 43#define GUARD_THREAD_PRIORITY 0 44 45using namespace android; 46 47#if GUARD_THREAD_PRIORITY 48Mutex gKeyCreateMutex; 49static pthread_key_t gBgKey = -1; 50#endif 51 52static void signalExceptionForPriorityError(JNIEnv* env, int err) 53{ 54 switch (err) { 55 case EINVAL: 56 jniThrowException(env, "java/lang/IllegalArgumentException", NULL); 57 break; 58 case ESRCH: 59 jniThrowException(env, "java/lang/IllegalArgumentException", "Given thread does not exist"); 60 break; 61 case EPERM: 62 jniThrowException(env, "java/lang/SecurityException", "No permission to modify given thread"); 63 break; 64 case EACCES: 65 jniThrowException(env, "java/lang/SecurityException", "No permission to set to given priority"); 66 break; 67 default: 68 jniThrowException(env, "java/lang/RuntimeException", "Unknown error"); 69 break; 70 } 71} 72 73static void signalExceptionForGroupError(JNIEnv* env, int err) 74{ 75 switch (err) { 76 case EINVAL: 77 jniThrowException(env, "java/lang/IllegalArgumentException", NULL); 78 break; 79 case ESRCH: 80 jniThrowException(env, "java/lang/IllegalArgumentException", "Given thread does not exist"); 81 break; 82 case EPERM: 83 jniThrowException(env, "java/lang/SecurityException", "No permission to modify given thread"); 84 break; 85 case EACCES: 86 jniThrowException(env, "java/lang/SecurityException", "No permission to set to given group"); 87 break; 88 default: 89 jniThrowException(env, "java/lang/RuntimeException", "Unknown error"); 90 break; 91 } 92} 93 94jint android_os_Process_myPid(JNIEnv* env, jobject clazz) 95{ 96 return getpid(); 97} 98 99jint android_os_Process_myUid(JNIEnv* env, jobject clazz) 100{ 101 return getuid(); 102} 103 104jint android_os_Process_myTid(JNIEnv* env, jobject clazz) 105{ 106 return androidGetTid(); 107} 108 109jint android_os_Process_getUidForName(JNIEnv* env, jobject clazz, jstring name) 110{ 111 if (name == NULL) { 112 jniThrowNullPointerException(env, NULL); 113 return -1; 114 } 115 116 const jchar* str16 = env->GetStringCritical(name, 0); 117 String8 name8; 118 if (str16) { 119 name8 = String8(str16, env->GetStringLength(name)); 120 env->ReleaseStringCritical(name, str16); 121 } 122 123 const size_t N = name8.size(); 124 if (N > 0) { 125 const char* str = name8.string(); 126 for (size_t i=0; i<N; i++) { 127 if (str[i] < '0' || str[i] > '9') { 128 struct passwd* pwd = getpwnam(str); 129 if (pwd == NULL) { 130 return -1; 131 } 132 return pwd->pw_uid; 133 } 134 } 135 return atoi(str); 136 } 137 return -1; 138} 139 140jint android_os_Process_getGidForName(JNIEnv* env, jobject clazz, jstring name) 141{ 142 if (name == NULL) { 143 jniThrowNullPointerException(env, NULL); 144 return -1; 145 } 146 147 const jchar* str16 = env->GetStringCritical(name, 0); 148 String8 name8; 149 if (str16) { 150 name8 = String8(str16, env->GetStringLength(name)); 151 env->ReleaseStringCritical(name, str16); 152 } 153 154 const size_t N = name8.size(); 155 if (N > 0) { 156 const char* str = name8.string(); 157 for (size_t i=0; i<N; i++) { 158 if (str[i] < '0' || str[i] > '9') { 159 struct group* grp = getgrnam(str); 160 if (grp == NULL) { 161 return -1; 162 } 163 return grp->gr_gid; 164 } 165 } 166 return atoi(str); 167 } 168 return -1; 169} 170 171void android_os_Process_setThreadGroup(JNIEnv* env, jobject clazz, int pid, jint grp) 172{ 173 int res = androidSetThreadSchedulingGroup(pid, grp); 174 if (res != NO_ERROR) { 175 signalExceptionForGroupError(env, res == BAD_VALUE ? EINVAL : errno); 176 return; 177 } 178} 179 180void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jint grp) 181{ 182 DIR *d; 183 FILE *fp; 184 char proc_path[255]; 185 struct dirent *de; 186 187 if (grp > ANDROID_TGROUP_MAX || grp < 0) { 188 signalExceptionForGroupError(env, EINVAL); 189 return; 190 } 191 192#if POLICY_DEBUG 193 char cmdline[32]; 194 int fd; 195 196 strcpy(cmdline, "unknown"); 197 198 sprintf(proc_path, "/proc/%d/cmdline", pid); 199 fd = open(proc_path, O_RDONLY); 200 if (fd >= 0) { 201 int rc = read(fd, cmdline, sizeof(cmdline)-1); 202 cmdline[rc] = 0; 203 close(fd); 204 } 205 206 if (grp == ANDROID_TGROUP_BG_NONINTERACT) { 207 ALOGD("setProcessGroup: vvv pid %d (%s)", pid, cmdline); 208 } else { 209 ALOGD("setProcessGroup: ^^^ pid %d (%s)", pid, cmdline); 210 } 211#endif 212 sprintf(proc_path, "/proc/%d/task", pid); 213 if (!(d = opendir(proc_path))) { 214 // If the process exited on us, don't generate an exception 215 if (errno != ENOENT) 216 signalExceptionForGroupError(env, errno); 217 return; 218 } 219 220 while ((de = readdir(d))) { 221 int t_pid; 222 int t_pri; 223 224 if (de->d_name[0] == '.') 225 continue; 226 t_pid = atoi(de->d_name); 227 228 if (!t_pid) { 229 ALOGE("Error getting pid for '%s'\n", de->d_name); 230 continue; 231 } 232 233 t_pri = getpriority(PRIO_PROCESS, t_pid); 234 235 if (grp == ANDROID_TGROUP_DEFAULT && 236 t_pri >= ANDROID_PRIORITY_BACKGROUND) { 237 // This task wants to stay at background 238 continue; 239 } 240 241 if (androidSetThreadSchedulingGroup(t_pid, grp) != NO_ERROR) { 242 signalExceptionForGroupError(env, errno); 243 break; 244 } 245 } 246 closedir(d); 247} 248 249static void android_os_Process_setCanSelfBackground(JNIEnv* env, jobject clazz, jboolean bgOk) { 250 // Establishes the calling thread as illegal to put into the background. 251 // Typically used only for the system process's main looper. 252#if GUARD_THREAD_PRIORITY 253 ALOGV("Process.setCanSelfBackground(%d) : tid=%d", bgOk, androidGetTid()); 254 { 255 Mutex::Autolock _l(gKeyCreateMutex); 256 if (gBgKey == -1) { 257 pthread_key_create(&gBgKey, NULL); 258 } 259 } 260 261 // inverted: not-okay, we set a sentinel value 262 pthread_setspecific(gBgKey, (void*)(bgOk ? 0 : 0xbaad)); 263#endif 264} 265 266void android_os_Process_setThreadScheduler(JNIEnv* env, jclass clazz, 267 jint tid, jint policy, jint pri) 268{ 269#ifdef HAVE_SCHED_SETSCHEDULER 270 struct sched_param param; 271 param.sched_priority = pri; 272 int rc = sched_setscheduler(tid, policy, ¶m); 273 if (rc) { 274 signalExceptionForPriorityError(env, errno); 275 } 276#else 277 signalExceptionForPriorityError(env, ENOSYS); 278#endif 279} 280 281void android_os_Process_setThreadPriority(JNIEnv* env, jobject clazz, 282 jint pid, jint pri) 283{ 284#if GUARD_THREAD_PRIORITY 285 // if we're putting the current thread into the background, check the TLS 286 // to make sure this thread isn't guarded. If it is, raise an exception. 287 if (pri >= ANDROID_PRIORITY_BACKGROUND) { 288 if (pid == androidGetTid()) { 289 void* bgOk = pthread_getspecific(gBgKey); 290 if (bgOk == ((void*)0xbaad)) { 291 ALOGE("Thread marked fg-only put self in background!"); 292 jniThrowException(env, "java/lang/SecurityException", "May not put this thread into background"); 293 return; 294 } 295 } 296 } 297#endif 298 299 int rc = androidSetThreadPriority(pid, pri); 300 if (rc != 0) { 301 if (rc == INVALID_OPERATION) { 302 signalExceptionForPriorityError(env, errno); 303 } else { 304 signalExceptionForGroupError(env, errno); 305 } 306 } 307 308 //ALOGI("Setting priority of %d: %d, getpriority returns %d\n", 309 // pid, pri, getpriority(PRIO_PROCESS, pid)); 310} 311 312void android_os_Process_setCallingThreadPriority(JNIEnv* env, jobject clazz, 313 jint pri) 314{ 315 jint tid = android_os_Process_myTid(env, clazz); 316 android_os_Process_setThreadPriority(env, clazz, tid, pri); 317} 318 319jint android_os_Process_getThreadPriority(JNIEnv* env, jobject clazz, 320 jint pid) 321{ 322 errno = 0; 323 jint pri = getpriority(PRIO_PROCESS, pid); 324 if (errno != 0) { 325 signalExceptionForPriorityError(env, errno); 326 } 327 //ALOGI("Returning priority of %d: %d\n", pid, pri); 328 return pri; 329} 330 331jboolean android_os_Process_setOomAdj(JNIEnv* env, jobject clazz, 332 jint pid, jint adj) 333{ 334#ifdef HAVE_OOM_ADJ 335 char text[64]; 336 sprintf(text, "/proc/%d/oom_adj", pid); 337 int fd = open(text, O_WRONLY); 338 if (fd >= 0) { 339 sprintf(text, "%d", adj); 340 write(fd, text, strlen(text)); 341 close(fd); 342 } 343 return true; 344#endif 345 return false; 346} 347 348void android_os_Process_setArgV0(JNIEnv* env, jobject clazz, jstring name) 349{ 350 if (name == NULL) { 351 jniThrowNullPointerException(env, NULL); 352 return; 353 } 354 355 const jchar* str = env->GetStringCritical(name, 0); 356 String8 name8; 357 if (str) { 358 name8 = String8(str, env->GetStringLength(name)); 359 env->ReleaseStringCritical(name, str); 360 } 361 362 if (name8.size() > 0) { 363 ProcessState::self()->setArgV0(name8.string()); 364 } 365} 366 367jint android_os_Process_setUid(JNIEnv* env, jobject clazz, jint uid) 368{ 369 return setuid(uid) == 0 ? 0 : errno; 370} 371 372jint android_os_Process_setGid(JNIEnv* env, jobject clazz, jint uid) 373{ 374 return setgid(uid) == 0 ? 0 : errno; 375} 376 377static int pid_compare(const void* v1, const void* v2) 378{ 379 //ALOGI("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 ALOGW("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 ALOGW("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:"), 0 }; 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 //ALOGI("getMemInfo: %p %p", reqFields, outFields); 438 439 if (fileStr == NULL || reqFields == NULL || outFields == NULL) { 440 jniThrowNullPointerException(env, 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 //ALOGI("String at %d: %p = %s", i, obj, str8); 465 if (str8 == NULL) { 466 jniThrowNullPointerException(env, "Element in reqFields"); 467 return; 468 } 469 fields.add(String8(str8)); 470 env->ReleaseStringUTFChars((jstring)obj, str8); 471 } else { 472 jniThrowNullPointerException(env, "Element in reqFields"); 473 return; 474 } 475 } 476 477 jlong* sizesArray = env->GetLongArrayElements(outFields, 0); 478 if (sizesArray == NULL) { 479 return; 480 } 481 482 //ALOGI("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 ALOGW("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 //ALOGI("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 //ALOGI("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 ALOGW("Unable to open %s", file.string()); 539 } 540 541 //ALOGI("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 jniThrowNullPointerException(env, 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 //ALOGI("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 //ALOGI("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 jniThrowNullPointerException(env, 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 //ALOGW("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 //ALOGW("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 jniThrowNullPointerException(env, 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 ALOGI("Sending signal. PID: %d SIG: %d", pid, sig); 810 kill(pid, sig); 811 } 812} 813 814void android_os_Process_sendSignalQuiet(JNIEnv* env, jobject clazz, jint pid, jint sig) 815{ 816 if (pid > 0) { 817 kill(pid, sig); 818 } 819} 820 821static jlong android_os_Process_getElapsedCpuTime(JNIEnv* env, jobject clazz) 822{ 823 struct timespec ts; 824 825 int res = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); 826 827 if (res != 0) { 828 return (jlong) 0; 829 } 830 831 nsecs_t when = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec; 832 return (jlong) nanoseconds_to_milliseconds(when); 833} 834 835static jlong android_os_Process_getPss(JNIEnv* env, jobject clazz, jint pid) 836{ 837 char filename[64]; 838 839 snprintf(filename, sizeof(filename), "/proc/%d/smaps", pid); 840 841 FILE * file = fopen(filename, "r"); 842 if (!file) { 843 return (jlong) -1; 844 } 845 846 // Tally up all of the Pss from the various maps 847 char line[256]; 848 jlong pss = 0; 849 while (fgets(line, sizeof(line), file)) { 850 jlong v; 851 if (sscanf(line, "Pss: %lld kB", &v) == 1) { 852 pss += v; 853 } 854 } 855 856 fclose(file); 857 858 // Return the Pss value in bytes, not kilobytes 859 return pss * 1024; 860} 861 862static const JNINativeMethod methods[] = { 863 {"myPid", "()I", (void*)android_os_Process_myPid}, 864 {"myTid", "()I", (void*)android_os_Process_myTid}, 865 {"myUid", "()I", (void*)android_os_Process_myUid}, 866 {"getUidForName", "(Ljava/lang/String;)I", (void*)android_os_Process_getUidForName}, 867 {"getGidForName", "(Ljava/lang/String;)I", (void*)android_os_Process_getGidForName}, 868 {"setThreadPriority", "(II)V", (void*)android_os_Process_setThreadPriority}, 869 {"setThreadScheduler", "(III)V", (void*)android_os_Process_setThreadScheduler}, 870 {"setCanSelfBackground", "(Z)V", (void*)android_os_Process_setCanSelfBackground}, 871 {"setThreadPriority", "(I)V", (void*)android_os_Process_setCallingThreadPriority}, 872 {"getThreadPriority", "(I)I", (void*)android_os_Process_getThreadPriority}, 873 {"setThreadGroup", "(II)V", (void*)android_os_Process_setThreadGroup}, 874 {"setProcessGroup", "(II)V", (void*)android_os_Process_setProcessGroup}, 875 {"setOomAdj", "(II)Z", (void*)android_os_Process_setOomAdj}, 876 {"setArgV0", "(Ljava/lang/String;)V", (void*)android_os_Process_setArgV0}, 877 {"setUid", "(I)I", (void*)android_os_Process_setUid}, 878 {"setGid", "(I)I", (void*)android_os_Process_setGid}, 879 {"sendSignal", "(II)V", (void*)android_os_Process_sendSignal}, 880 {"sendSignalQuiet", "(II)V", (void*)android_os_Process_sendSignalQuiet}, 881 {"getFreeMemory", "()J", (void*)android_os_Process_getFreeMemory}, 882 {"readProcLines", "(Ljava/lang/String;[Ljava/lang/String;[J)V", (void*)android_os_Process_readProcLines}, 883 {"getPids", "(Ljava/lang/String;[I)[I", (void*)android_os_Process_getPids}, 884 {"readProcFile", "(Ljava/lang/String;[I[Ljava/lang/String;[J[F)Z", (void*)android_os_Process_readProcFile}, 885 {"parseProcLine", "([BII[I[Ljava/lang/String;[J[F)Z", (void*)android_os_Process_parseProcLine}, 886 {"getElapsedCpuTime", "()J", (void*)android_os_Process_getElapsedCpuTime}, 887 {"getPss", "(I)J", (void*)android_os_Process_getPss}, 888 //{"setApplicationObject", "(Landroid/os/IBinder;)V", (void*)android_os_Process_setApplicationObject}, 889}; 890 891const char* const kProcessPathName = "android/os/Process"; 892 893int register_android_os_Process(JNIEnv* env) 894{ 895 return AndroidRuntime::registerNativeMethods( 896 env, kProcessPathName, 897 methods, NELEM(methods)); 898} 899