Merge "Don't clear the output array if it failed to open the proc status file"
This commit is contained in:
@@ -644,6 +644,12 @@ static jlong android_os_Process_getTotalMemory(JNIEnv* env, jobject clazz)
|
|||||||
return si.totalram;
|
return si.totalram;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The outFields array is initialized to -1 to allow the caller to identify
|
||||||
|
* when the status file (and therefore the process) they specified is invalid.
|
||||||
|
* This array should not be overwritten or cleared before we know that the
|
||||||
|
* status file can be read.
|
||||||
|
*/
|
||||||
void android_os_Process_readProcLines(JNIEnv* env, jobject clazz, jstring fileStr,
|
void android_os_Process_readProcLines(JNIEnv* env, jobject clazz, jstring fileStr,
|
||||||
jobjectArray reqFields, jlongArray outFields)
|
jobjectArray reqFields, jlongArray outFields)
|
||||||
{
|
{
|
||||||
@@ -692,14 +698,14 @@ void android_os_Process_readProcLines(JNIEnv* env, jobject clazz, jstring fileSt
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//ALOGI("Clearing %" PRId32 " sizes", count);
|
|
||||||
for (i=0; i<count; i++) {
|
|
||||||
sizesArray[i] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int fd = open(file.string(), O_RDONLY | O_CLOEXEC);
|
int fd = open(file.string(), O_RDONLY | O_CLOEXEC);
|
||||||
|
|
||||||
if (fd >= 0) {
|
if (fd >= 0) {
|
||||||
|
//ALOGI("Clearing %" PRId32 " sizes", count);
|
||||||
|
for (i=0; i<count; i++) {
|
||||||
|
sizesArray[i] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
const size_t BUFFER_SIZE = 4096;
|
const size_t BUFFER_SIZE = 4096;
|
||||||
char* buffer = (char*)malloc(BUFFER_SIZE);
|
char* buffer = (char*)malloc(BUFFER_SIZE);
|
||||||
int len = read(fd, buffer, BUFFER_SIZE-1);
|
int len = read(fd, buffer, BUFFER_SIZE-1);
|
||||||
|
|||||||
@@ -17,13 +17,12 @@
|
|||||||
|
|
||||||
package android.os;
|
package android.os;
|
||||||
|
|
||||||
import androidx.test.filters.MediumTest;
|
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
public class ProcessTest extends TestCase {
|
public class ProcessTest extends TestCase {
|
||||||
|
|
||||||
@MediumTest
|
private static final int BAD_PID = 0;
|
||||||
|
|
||||||
public void testProcessGetUidFromName() throws Exception {
|
public void testProcessGetUidFromName() throws Exception {
|
||||||
assertEquals(android.os.Process.SYSTEM_UID, Process.getUidForName("system"));
|
assertEquals(android.os.Process.SYSTEM_UID, Process.getUidForName("system"));
|
||||||
assertEquals(Process.BLUETOOTH_UID, Process.getUidForName("bluetooth"));
|
assertEquals(Process.BLUETOOTH_UID, Process.getUidForName("bluetooth"));
|
||||||
@@ -35,7 +34,6 @@ public class ProcessTest extends TestCase {
|
|||||||
Process.getUidForName("u3_a100"));
|
Process.getUidForName("u3_a100"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@MediumTest
|
|
||||||
public void testProcessGetUidFromNameFailure() throws Exception {
|
public void testProcessGetUidFromNameFailure() throws Exception {
|
||||||
// Failure cases
|
// Failure cases
|
||||||
assertEquals(-1, Process.getUidForName("u2a_foo"));
|
assertEquals(-1, Process.getUidForName("u2a_foo"));
|
||||||
@@ -47,4 +45,29 @@ public class ProcessTest extends TestCase {
|
|||||||
assertEquals(-1, Process.getUidForName("u2jhsajhfkjhsafkhskafhkashfkjashfkjhaskjfdhakj3"));
|
assertEquals(-1, Process.getUidForName("u2jhsajhfkjhsafkhskafhkashfkjashfkjhaskjfdhakj3"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests getUidForPid() by ensuring that it returns the correct value when the process queried
|
||||||
|
* doesn't exist.
|
||||||
|
*/
|
||||||
|
public void testGetUidForPidInvalidPid() {
|
||||||
|
assertEquals(-1, Process.getUidForPid(BAD_PID));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests getParentPid() by ensuring that it returns the correct value when the process queried
|
||||||
|
* doesn't exist.
|
||||||
|
*/
|
||||||
|
public void testGetParentPidInvalidPid() {
|
||||||
|
assertEquals(-1, Process.getParentPid(BAD_PID));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests getThreadGroupLeader() by ensuring that it returns the correct value when the
|
||||||
|
* thread queried doesn't exist.
|
||||||
|
*/
|
||||||
|
public void testGetThreadGroupLeaderInvalidTid() {
|
||||||
|
// This function takes a TID instead of a PID but BAD_PID should also be a bad TID.
|
||||||
|
assertEquals(-1, Process.getThreadGroupLeader(BAD_PID));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user