process_test.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <procinfo/process.h>
  17. #include <fcntl.h>
  18. #include <stdlib.h>
  19. #include <sys/types.h>
  20. #include <unistd.h>
  21. #include <chrono>
  22. #include <set>
  23. #include <thread>
  24. #include <vector>
  25. #include <gtest/gtest.h>
  26. #include <android-base/stringprintf.h>
  27. using namespace std::chrono_literals;
  28. #if !defined(__BIONIC__)
  29. #include <syscall.h>
  30. static pid_t gettid() {
  31. return syscall(__NR_gettid);
  32. }
  33. #endif
  34. TEST(process_info, process_info_smoke) {
  35. android::procinfo::ProcessInfo self;
  36. ASSERT_TRUE(android::procinfo::GetProcessInfo(gettid(), &self));
  37. ASSERT_EQ(gettid(), self.tid);
  38. ASSERT_EQ(getpid(), self.pid);
  39. ASSERT_EQ(getppid(), self.ppid);
  40. ASSERT_EQ(getuid(), self.uid);
  41. ASSERT_EQ(getgid(), self.gid);
  42. }
  43. TEST(process_info, process_info_proc_pid_fd_smoke) {
  44. android::procinfo::ProcessInfo self;
  45. int fd = open(android::base::StringPrintf("/proc/%d", gettid()).c_str(), O_DIRECTORY | O_RDONLY);
  46. ASSERT_NE(-1, fd);
  47. ASSERT_TRUE(android::procinfo::GetProcessInfoFromProcPidFd(fd, &self));
  48. // Process name is capped at 15 bytes.
  49. ASSERT_EQ("libprocinfo_tes", self.name);
  50. ASSERT_EQ(gettid(), self.tid);
  51. ASSERT_EQ(getpid(), self.pid);
  52. ASSERT_EQ(getppid(), self.ppid);
  53. ASSERT_EQ(getuid(), self.uid);
  54. ASSERT_EQ(getgid(), self.gid);
  55. close(fd);
  56. }
  57. TEST(process_info, process_tids_smoke) {
  58. pid_t main_tid = gettid();
  59. std::thread([main_tid]() {
  60. pid_t thread_tid = gettid();
  61. {
  62. std::vector<pid_t> vec;
  63. ASSERT_TRUE(android::procinfo::GetProcessTids(getpid(), &vec));
  64. ASSERT_EQ(1, std::count(vec.begin(), vec.end(), main_tid));
  65. ASSERT_EQ(1, std::count(vec.begin(), vec.end(), thread_tid));
  66. }
  67. {
  68. std::set<pid_t> set;
  69. ASSERT_TRUE(android::procinfo::GetProcessTids(getpid(), &set));
  70. ASSERT_EQ(1, std::count(set.begin(), set.end(), main_tid));
  71. ASSERT_EQ(1, std::count(set.begin(), set.end(), thread_tid));
  72. }
  73. }).join();
  74. }
  75. TEST(process_info, process_state) {
  76. int pipefd[2];
  77. ASSERT_EQ(0, pipe2(pipefd, O_CLOEXEC));
  78. pid_t forkpid = fork();
  79. ASSERT_NE(-1, forkpid);
  80. if (forkpid == 0) {
  81. close(pipefd[1]);
  82. char buf;
  83. TEMP_FAILURE_RETRY(read(pipefd[0], &buf, 1));
  84. _exit(0);
  85. }
  86. // Give the child some time to get to the read.
  87. std::this_thread::sleep_for(100ms);
  88. android::procinfo::ProcessInfo procinfo;
  89. ASSERT_TRUE(android::procinfo::GetProcessInfo(forkpid, &procinfo));
  90. ASSERT_EQ(android::procinfo::kProcessStateSleeping, procinfo.state);
  91. ASSERT_EQ(0, kill(forkpid, SIGKILL));
  92. // Give the kernel some time to kill the child.
  93. std::this_thread::sleep_for(100ms);
  94. ASSERT_TRUE(android::procinfo::GetProcessInfo(forkpid, &procinfo));
  95. ASSERT_EQ(android::procinfo::kProcessStateZombie, procinfo.state);
  96. ASSERT_EQ(forkpid, waitpid(forkpid, nullptr, 0));
  97. }