blob: 4831ae8f41c2c77985b66f70dd80e4624baa0a6a [file] [log] [blame]
Josh Gao788a35d2016-10-28 15:23:25 -07001/*
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 <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
24
25#include <string>
26
27#include <android-base/unique_fd.h>
28
29using android::base::unique_fd;
30
31namespace android {
32namespace procinfo {
33
Yabin Cui20cdf0a2018-07-24 10:52:25 -070034bool GetProcessInfo(pid_t tid, ProcessInfo* process_info, std::string* error) {
Josh Gao788a35d2016-10-28 15:23:25 -070035 char path[PATH_MAX];
36 snprintf(path, sizeof(path), "/proc/%d", tid);
37
38 unique_fd dirfd(open(path, O_DIRECTORY | O_RDONLY));
39 if (dirfd == -1) {
Yabin Cui20cdf0a2018-07-24 10:52:25 -070040 if (error != nullptr) {
41 *error = std::string("failed to open ") + path;
42 }
Josh Gao788a35d2016-10-28 15:23:25 -070043 return false;
44 }
45
Yabin Cui20cdf0a2018-07-24 10:52:25 -070046 return GetProcessInfoFromProcPidFd(dirfd.get(), process_info, error);
Josh Gao788a35d2016-10-28 15:23:25 -070047}
48
Josh Gao67887fe2021-03-01 23:02:54 -080049static ProcessState parse_state(char state) {
50 switch (state) {
Josh Gao8a1b53c2017-06-27 14:06:19 -070051 case 'R':
52 return kProcessStateRunning;
53 case 'S':
54 return kProcessStateSleeping;
55 case 'D':
56 return kProcessStateUninterruptibleWait;
57 case 'T':
58 return kProcessStateStopped;
59 case 'Z':
60 return kProcessStateZombie;
61 default:
Josh Gao8a1b53c2017-06-27 14:06:19 -070062 return kProcessStateUnknown;
63 }
64}
65
Yabin Cui20cdf0a2018-07-24 10:52:25 -070066bool GetProcessInfoFromProcPidFd(int fd, ProcessInfo* process_info, std::string* error) {
Josh Gao788a35d2016-10-28 15:23:25 -070067 int status_fd = openat(fd, "status", O_RDONLY | O_CLOEXEC);
68
69 if (status_fd == -1) {
Yabin Cui20cdf0a2018-07-24 10:52:25 -070070 if (error != nullptr) {
71 *error = "failed to open status fd in GetProcessInfoFromProcPidFd";
72 }
Josh Gao788a35d2016-10-28 15:23:25 -070073 return false;
74 }
75
76 std::unique_ptr<FILE, decltype(&fclose)> fp(fdopen(status_fd, "r"), fclose);
77 if (!fp) {
Yabin Cui20cdf0a2018-07-24 10:52:25 -070078 if (error != nullptr) {
79 *error = "failed to open status file in GetProcessInfoFromProcPidFd";
80 }
Josh Gao788a35d2016-10-28 15:23:25 -070081 close(status_fd);
82 return false;
83 }
84
85 int field_bitmap = 0;
Josh Gao67887fe2021-03-01 23:02:54 -080086 static constexpr int finished_bitmap = 63;
Josh Gao788a35d2016-10-28 15:23:25 -070087 char* line = nullptr;
88 size_t len = 0;
89
90 while (getline(&line, &len, fp.get()) != -1 && field_bitmap != finished_bitmap) {
91 char* tab = strchr(line, '\t');
92 if (tab == nullptr) {
93 continue;
94 }
95
96 size_t header_len = tab - line;
97 std::string header = std::string(line, header_len);
98 if (header == "Name:") {
99 std::string name = line + header_len + 1;
100
101 // line includes the trailing newline.
102 name.pop_back();
103 process_info->name = std::move(name);
104
105 field_bitmap |= 1;
Josh Gao67887fe2021-03-01 23:02:54 -0800106 } else if (header == "Tgid:") {
107 process_info->pid = atoi(tab + 1);
108 field_bitmap |= 2;
Josh Gao788a35d2016-10-28 15:23:25 -0700109 } else if (header == "Pid:") {
110 process_info->tid = atoi(tab + 1);
Josh Gao788a35d2016-10-28 15:23:25 -0700111 field_bitmap |= 4;
Josh Gao788a35d2016-10-28 15:23:25 -0700112 } else if (header == "TracerPid:") {
113 process_info->tracer = atoi(tab + 1);
Josh Gao67887fe2021-03-01 23:02:54 -0800114 field_bitmap |= 8;
Josh Gao788a35d2016-10-28 15:23:25 -0700115 } else if (header == "Uid:") {
116 process_info->uid = atoi(tab + 1);
Josh Gao67887fe2021-03-01 23:02:54 -0800117 field_bitmap |= 16;
Josh Gao788a35d2016-10-28 15:23:25 -0700118 } else if (header == "Gid:") {
119 process_info->gid = atoi(tab + 1);
Josh Gao67887fe2021-03-01 23:02:54 -0800120 field_bitmap |= 32;
Josh Gao788a35d2016-10-28 15:23:25 -0700121 }
122 }
123
124 free(line);
Josh Gao67887fe2021-03-01 23:02:54 -0800125 if (field_bitmap != finished_bitmap) {
126 *error = "failed to parse /proc/<pid>/status";
127 return false;
128 }
129
130 unique_fd stat_fd(openat(fd, "stat", O_RDONLY | O_CLOEXEC));
131 if (stat_fd == -1) {
132 *error = "failed to open /proc/<pid>/stat";
133 }
134
135 std::string stat;
136 if (!android::base::ReadFdToString(stat_fd, &stat)) {
137 *error = "failed to read /proc/<pid>/stat";
138 return false;
139 }
140
141 // See man 5 proc. There's no reason comm can't contain ' ' or ')',
142 // so we search backwards for the end of it.
143 const char* end_of_comm = strrchr(stat.c_str(), ')');
144
145 static constexpr const char* pattern =
146 "%c " // state
147 "%d " // ppid
148 "%*d " // pgrp
149 "%*d " // session
150 "%*d " // tty_nr
151 "%*d " // tpgid
152 "%*u " // flags
153 "%*lu " // minflt
154 "%*lu " // cminflt
155 "%*lu " // majflt
156 "%*lu " // cmajflt
157 "%*lu " // utime
158 "%*lu " // stime
159 "%*ld " // cutime
160 "%*ld " // cstime
161 "%*ld " // priority
162 "%*ld " // nice
163 "%*ld " // num_threads
164 "%*ld " // itrealvalue
165 "%llu " // starttime
166 ;
167
168 char state = '\0';
169 int ppid = 0;
170 unsigned long long start_time = 0;
171 int rc = sscanf(end_of_comm + 2, pattern, &state, &ppid, &start_time);
172 if (rc != 3) {
173 *error = "failed to parse /proc/<pid>/stat";
174 return false;
175 }
176
177 process_info->state = parse_state(state);
178 process_info->ppid = ppid;
179 process_info->starttime = start_time;
180 return true;
Josh Gao788a35d2016-10-28 15:23:25 -0700181}
182
183} /* namespace procinfo */
184} /* namespace android */