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 
17 #include <procinfo/process.h>
18 
19 #include <fcntl.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 
24 #include <algorithm>
25 #include <chrono>
26 #include <set>
27 #include <thread>
28 #include <vector>
29 
30 #include <gtest/gtest.h>
31 
32 #include <android-base/file.h>
33 #include <android-base/logging.h>
34 #include <android-base/stringprintf.h>
35 #include <android-base/unique_fd.h>
36 
37 using namespace std::chrono_literals;
38 
39 #if defined(__GLIBC__)
40 #include <syscall.h>
gettid()41 static pid_t gettid() {
42   return syscall(__NR_gettid);
43 }
44 #endif
45 
TEST(process_info,process_info_smoke)46 TEST(process_info, process_info_smoke) {
47   android::procinfo::ProcessInfo self;
48   ASSERT_TRUE(android::procinfo::GetProcessInfo(gettid(), &self));
49   ASSERT_EQ(gettid(), self.tid);
50   ASSERT_EQ(getpid(), self.pid);
51   ASSERT_EQ(getppid(), self.ppid);
52   ASSERT_EQ(getuid(), self.uid);
53   ASSERT_EQ(getgid(), self.gid);
54 }
55 
TEST(process_info,process_info_proc_pid_fd_smoke)56 TEST(process_info, process_info_proc_pid_fd_smoke) {
57   android::procinfo::ProcessInfo self;
58   int fd = open(android::base::StringPrintf("/proc/%d", gettid()).c_str(), O_DIRECTORY | O_RDONLY);
59   ASSERT_NE(-1, fd);
60   ASSERT_TRUE(android::procinfo::GetProcessInfoFromProcPidFd(fd, gettid(), &self));
61 
62   // Process name is capped at 15 bytes.
63   ASSERT_EQ("libprocinfo_tes", self.name);
64   ASSERT_EQ(gettid(), self.tid);
65   ASSERT_EQ(getpid(), self.pid);
66   ASSERT_EQ(getppid(), self.ppid);
67   ASSERT_EQ(getuid(), self.uid);
68   ASSERT_EQ(getgid(), self.gid);
69   close(fd);
70 }
71 
TEST(process_info,process_tids_smoke)72 TEST(process_info, process_tids_smoke) {
73   pid_t main_tid = gettid();
74   std::thread([main_tid]() {
75     pid_t thread_tid = gettid();
76 
77     {
78       std::vector<pid_t> vec;
79       ASSERT_TRUE(android::procinfo::GetProcessTids(getpid(), &vec));
80       ASSERT_EQ(1, std::count(vec.begin(), vec.end(), main_tid));
81       ASSERT_EQ(1, std::count(vec.begin(), vec.end(), thread_tid));
82     }
83 
84     {
85       std::set<pid_t> set;
86       ASSERT_TRUE(android::procinfo::GetProcessTids(getpid(), &set));
87       ASSERT_EQ(1, std::count(set.begin(), set.end(), main_tid));
88       ASSERT_EQ(1, std::count(set.begin(), set.end(), thread_tid));
89     }
90   }).join();
91 }
92 
TEST(process_info,process_state)93 TEST(process_info, process_state) {
94   int pipefd[2];
95   ASSERT_EQ(0, pipe2(pipefd, O_CLOEXEC));
96   pid_t forkpid = fork();
97 
98   ASSERT_NE(-1, forkpid);
99   if (forkpid == 0) {
100     close(pipefd[1]);
101     char buf;
102     TEMP_FAILURE_RETRY(read(pipefd[0], &buf, 1));
103     _exit(0);
104   }
105 
106 
107   // Give the child some time to get to the read.
108   android::procinfo::ProcessInfo procinfo;
109   for (int loop = 0; loop < 50 && procinfo.state != android::procinfo::kProcessStateSleeping; loop++) {
110    std::this_thread::sleep_for(100ms);
111    ASSERT_TRUE(android::procinfo::GetProcessInfo(forkpid, &procinfo));
112   }
113   ASSERT_EQ(android::procinfo::kProcessStateSleeping, procinfo.state);
114 
115   ASSERT_EQ(0, kill(forkpid, SIGKILL));
116 
117   // Give the kernel some time to kill the child.
118   for (int loop = 0; loop < 50 && procinfo.state != android::procinfo::kProcessStateZombie; loop++) {
119     std::this_thread::sleep_for(100ms);
120     ASSERT_TRUE(android::procinfo::GetProcessInfo(forkpid, &procinfo));
121   }
122   ASSERT_EQ(android::procinfo::kProcessStateZombie, procinfo.state);
123 
124   ASSERT_EQ(forkpid, waitpid(forkpid, nullptr, 0));
125 }
126 
read_uptime_secs()127 static uint64_t read_uptime_secs() {
128   std::string uptime;
129   if (!android::base::ReadFileToString("/proc/uptime", &uptime)) {
130     PLOG(FATAL) << "failed to read /proc/uptime";
131   }
132   return strtoll(uptime.c_str(), nullptr, 10);
133 }
134 
TEST(process_info,process_start_time)135 TEST(process_info, process_start_time) {
136   uint64_t start = read_uptime_secs();
137   int pipefd[2];
138   ASSERT_EQ(0, pipe2(pipefd, O_CLOEXEC));
139 
140   std::this_thread::sleep_for(1000ms);
141 
142   pid_t forkpid = fork();
143 
144   ASSERT_NE(-1, forkpid);
145   if (forkpid == 0) {
146     close(pipefd[1]);
147     char buf;
148     TEMP_FAILURE_RETRY(read(pipefd[0], &buf, 1));
149     _exit(0);
150   }
151 
152   std::this_thread::sleep_for(1000ms);
153 
154   uint64_t end = read_uptime_secs();
155 
156   android::procinfo::ProcessInfo procinfo;
157   ASSERT_TRUE(android::procinfo::GetProcessInfo(forkpid, &procinfo));
158 
159   // starttime is measured in clock ticks: uptime is in seconds:
160   uint64_t process_start = procinfo.starttime / sysconf(_SC_CLK_TCK);
161   ASSERT_LE(start, process_start);
162   ASSERT_LE(process_start, end);
163 
164   ASSERT_EQ(0, kill(forkpid, SIGKILL));
165   ASSERT_EQ(forkpid, waitpid(forkpid, nullptr, 0));
166 }
167 
TEST(process_info,GetProcessInfoFromProcPidFd_set_error)168 TEST(process_info, GetProcessInfoFromProcPidFd_set_error) {
169   TemporaryDir tmp_dir;
170 
171   android::base::unique_fd dirfd(open(tmp_dir.path, O_DIRECTORY | O_RDONLY));
172   android::procinfo::ProcessInfo procinfo;
173   std::string error;
174 
175   // failed to open status file error
176   // No segfault if not given error string.
177   ASSERT_FALSE(android::procinfo::GetProcessInfoFromProcPidFd(dirfd.get(), 0, &procinfo));
178   // Set error when given error string.
179   ASSERT_FALSE(android::procinfo::GetProcessInfoFromProcPidFd(dirfd.get(), 0, &procinfo, &error));
180   ASSERT_EQ(error, "failed to open /proc/0/status in GetProcessInfoFromProcPidFd: No such file or directory");
181 
182   // failed to parse status file error
183   std::string status_file = std::string(tmp_dir.path) + "/status";
184   ASSERT_TRUE(android::base::WriteStringToFile("invalid data", status_file));
185   ASSERT_FALSE(android::procinfo::GetProcessInfoFromProcPidFd(dirfd.get(), 0, &procinfo));
186   ASSERT_FALSE(android::procinfo::GetProcessInfoFromProcPidFd(dirfd.get(), 0, &procinfo, &error));
187   ASSERT_EQ(error, "failed to parse /proc/0/status");
188 
189   // Give the "/status" file valid contents.
190   ASSERT_TRUE(android::base::WriteStringToFile(
191       "Name:\tsh\nTgid:\t0\nPid:\t0\nTracerPid:\t0\nUid:\t0\nGid:\t0\n", status_file));
192 
193   // failed to open stat file error
194   ASSERT_FALSE(android::procinfo::GetProcessInfoFromProcPidFd(dirfd.get(), 0, &procinfo));
195   ASSERT_FALSE(android::procinfo::GetProcessInfoFromProcPidFd(dirfd.get(), 0, &procinfo, &error));
196   ASSERT_EQ(error, "failed to open /proc/0/stat: No such file or directory");
197 
198   // failed to parse stat file error
199   std::string stat_file = std::string(tmp_dir.path) + "/stat";
200   ASSERT_TRUE(android::base::WriteStringToFile("2027 (sh) invalid data", stat_file));
201   ASSERT_FALSE(android::procinfo::GetProcessInfoFromProcPidFd(dirfd.get(), 0, &procinfo));
202   ASSERT_FALSE(android::procinfo::GetProcessInfoFromProcPidFd(dirfd.get(), 0, &procinfo, &error));
203   ASSERT_EQ(error, "failed to parse /proc/0/stat");
204 }
205