blob: 2efd49c37606e11c965fd39993692d1451b0393b [file] [log] [blame]
Josh Gao911d7292016-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 Cui5f036ab2018-07-24 10:52:25 -070034bool GetProcessInfo(pid_t tid, ProcessInfo* process_info, std::string* error) {
Josh Gao911d7292016-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 Cui5f036ab2018-07-24 10:52:25 -070040 if (error != nullptr) {
41 *error = std::string("failed to open ") + path;
42 }
Josh Gao911d7292016-10-28 15:23:25 -070043 return false;
44 }
45
Yabin Cui5f036ab2018-07-24 10:52:25 -070046 return GetProcessInfoFromProcPidFd(dirfd.get(), process_info, error);
Josh Gao911d7292016-10-28 15:23:25 -070047}
48
Josh Gao9cb2e2e2017-06-27 14:06:19 -070049static ProcessState parse_state(const char* state) {
50 switch (*state) {
51 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 Gao9cb2e2e2017-06-27 14:06:19 -070062 return kProcessStateUnknown;
63 }
64}
65
Yabin Cui5f036ab2018-07-24 10:52:25 -070066bool GetProcessInfoFromProcPidFd(int fd, ProcessInfo* process_info, std::string* error) {
Josh Gao911d7292016-10-28 15:23:25 -070067 int status_fd = openat(fd, "status", O_RDONLY | O_CLOEXEC);
68
69 if (status_fd == -1) {
Yabin Cui5f036ab2018-07-24 10:52:25 -070070 if (error != nullptr) {
71 *error = "failed to open status fd in GetProcessInfoFromProcPidFd";
72 }
Josh Gao911d7292016-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 Cui5f036ab2018-07-24 10:52:25 -070078 if (error != nullptr) {
79 *error = "failed to open status file in GetProcessInfoFromProcPidFd";
80 }
Josh Gao911d7292016-10-28 15:23:25 -070081 close(status_fd);
82 return false;
83 }
84
85 int field_bitmap = 0;
Josh Gao9cb2e2e2017-06-27 14:06:19 -070086 static constexpr int finished_bitmap = 255;
Josh Gao911d7292016-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;
106 } else if (header == "Pid:") {
107 process_info->tid = atoi(tab + 1);
108 field_bitmap |= 2;
109 } else if (header == "Tgid:") {
110 process_info->pid = atoi(tab + 1);
111 field_bitmap |= 4;
112 } else if (header == "PPid:") {
113 process_info->ppid = atoi(tab + 1);
114 field_bitmap |= 8;
115 } else if (header == "TracerPid:") {
116 process_info->tracer = atoi(tab + 1);
117 field_bitmap |= 16;
118 } else if (header == "Uid:") {
119 process_info->uid = atoi(tab + 1);
120 field_bitmap |= 32;
121 } else if (header == "Gid:") {
122 process_info->gid = atoi(tab + 1);
123 field_bitmap |= 64;
Josh Gao9cb2e2e2017-06-27 14:06:19 -0700124 } else if (header == "State:") {
125 process_info->state = parse_state(tab + 1);
126 field_bitmap |= 128;
Josh Gao911d7292016-10-28 15:23:25 -0700127 }
128 }
129
130 free(line);
131 return field_bitmap == finished_bitmap;
132}
133
134} /* namespace procinfo */
135} /* namespace android */