summaryrefslogtreecommitdiffstats
path: root/libprocinfo/process_test.cpp
diff options
context:
space:
mode:
authorJosh Gao <jmgao@google.com>2017-06-27 14:06:19 -0700
committerJosh Gao <jmgao@google.com>2017-06-27 15:06:27 -0700
commit9cb2e2eb8cefcfca5133d01f29ab29005e1f2a46 (patch)
treeae94fdbeda8f092eafb93313ecc0d2a29d8f947c /libprocinfo/process_test.cpp
parent3e8d923276f0c872dc5dfd19a53d462196298267 (diff)
downloadsystem_core-9cb2e2eb8cefcfca5133d01f29ab29005e1f2a46.tar.gz
system_core-9cb2e2eb8cefcfca5133d01f29ab29005e1f2a46.tar.bz2
system_core-9cb2e2eb8cefcfca5133d01f29ab29005e1f2a46.zip
libprocinfo: add support for parsing process state.
Bug: http://b/63008395 Test: libprocinfo_test32 Change-Id: I20a337bb5075bcdb325d2b48e174b0b5ef896261
Diffstat (limited to 'libprocinfo/process_test.cpp')
-rw-r--r--libprocinfo/process_test.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/libprocinfo/process_test.cpp b/libprocinfo/process_test.cpp
index 5ffd2365b..9da927899 100644
--- a/libprocinfo/process_test.cpp
+++ b/libprocinfo/process_test.cpp
@@ -21,6 +21,7 @@
#include <sys/types.h>
#include <unistd.h>
+#include <chrono>
#include <set>
#include <thread>
#include <vector>
@@ -29,6 +30,8 @@
#include <android-base/stringprintf.h>
+using namespace std::chrono_literals;
+
#if !defined(__BIONIC__)
#include <syscall.h>
static pid_t gettid() {
@@ -82,3 +85,34 @@ TEST(process_info, process_tids_smoke) {
}
}).join();
}
+
+TEST(process_info, process_state) {
+ int pipefd[2];
+ ASSERT_EQ(0, pipe2(pipefd, O_CLOEXEC));
+ pid_t forkpid = fork();
+
+ ASSERT_NE(-1, forkpid);
+ if (forkpid == 0) {
+ close(pipefd[1]);
+ char buf;
+ TEMP_FAILURE_RETRY(read(pipefd[0], &buf, 1));
+ _exit(0);
+ }
+
+ // Give the child some time to get to the read.
+ std::this_thread::sleep_for(100ms);
+
+ android::procinfo::ProcessInfo procinfo;
+ ASSERT_TRUE(android::procinfo::GetProcessInfo(forkpid, &procinfo));
+ ASSERT_EQ(android::procinfo::kProcessStateSleeping, procinfo.state);
+
+ ASSERT_EQ(0, kill(forkpid, SIGKILL));
+
+ // Give the kernel some time to kill the child.
+ std::this_thread::sleep_for(100ms);
+
+ ASSERT_TRUE(android::procinfo::GetProcessInfo(forkpid, &procinfo));
+ ASSERT_EQ(android::procinfo::kProcessStateZombie, procinfo.state);
+
+ ASSERT_EQ(forkpid, waitpid(forkpid, nullptr, 0));
+}