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