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 <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
29 using android::base::unique_fd;
30
31 namespace android {
32 namespace procinfo {
33
GetProcessInfo(pid_t tid,ProcessInfo * process_info,std::string * error)34 bool GetProcessInfo(pid_t tid, ProcessInfo* process_info, std::string* error) {
35 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) {
40 if (error != nullptr) {
41 *error = std::string("failed to open ") + path;
42 }
43 return false;
44 }
45
46 return GetProcessInfoFromProcPidFd(dirfd.get(), process_info, error);
47 }
48
parse_state(const char * state)49 static 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:
62 LOG(ERROR) << "unknown process state: " << *state;
63 return kProcessStateUnknown;
64 }
65 }
66
GetProcessInfoFromProcPidFd(int fd,ProcessInfo * process_info,std::string * error)67 bool GetProcessInfoFromProcPidFd(int fd, ProcessInfo* process_info, std::string* error) {
68 int status_fd = openat(fd, "status", O_RDONLY | O_CLOEXEC);
69
70 if (status_fd == -1) {
71 if (error != nullptr) {
72 *error = "failed to open status fd in GetProcessInfoFromProcPidFd";
73 }
74 return false;
75 }
76
77 std::unique_ptr<FILE, decltype(&fclose)> fp(fdopen(status_fd, "r"), fclose);
78 if (!fp) {
79 if (error != nullptr) {
80 *error = "failed to open status file in GetProcessInfoFromProcPidFd";
81 }
82 close(status_fd);
83 return false;
84 }
85
86 int field_bitmap = 0;
87 static constexpr int finished_bitmap = 255;
88 char* line = nullptr;
89 size_t len = 0;
90
91 while (getline(&line, &len, fp.get()) != -1 && field_bitmap != finished_bitmap) {
92 char* tab = strchr(line, '\t');
93 if (tab == nullptr) {
94 continue;
95 }
96
97 size_t header_len = tab - line;
98 std::string header = std::string(line, header_len);
99 if (header == "Name:") {
100 std::string name = line + header_len + 1;
101
102 // line includes the trailing newline.
103 name.pop_back();
104 process_info->name = std::move(name);
105
106 field_bitmap |= 1;
107 } else if (header == "Pid:") {
108 process_info->tid = atoi(tab + 1);
109 field_bitmap |= 2;
110 } else if (header == "Tgid:") {
111 process_info->pid = atoi(tab + 1);
112 field_bitmap |= 4;
113 } else if (header == "PPid:") {
114 process_info->ppid = atoi(tab + 1);
115 field_bitmap |= 8;
116 } else if (header == "TracerPid:") {
117 process_info->tracer = atoi(tab + 1);
118 field_bitmap |= 16;
119 } else if (header == "Uid:") {
120 process_info->uid = atoi(tab + 1);
121 field_bitmap |= 32;
122 } else if (header == "Gid:") {
123 process_info->gid = atoi(tab + 1);
124 field_bitmap |= 64;
125 } else if (header == "State:") {
126 process_info->state = parse_state(tab + 1);
127 field_bitmap |= 128;
128 }
129 }
130
131 free(line);
132 return field_bitmap == finished_bitmap;
133 }
134
135 } /* namespace procinfo */
136 } /* namespace android */
137