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 #pragma once
18 
19 #include <dirent.h>
20 #include <fcntl.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 
25 #include <memory>
26 #include <string>
27 #include <type_traits>
28 
29 #include <android-base/logging.h>
30 #include <android-base/parseint.h>
31 #include <android-base/unique_fd.h>
32 
33 namespace android {
34 namespace procinfo {
35 
36 #if defined(__linux__)
37 
38 enum ProcessState {
39   kProcessStateUnknown,
40   kProcessStateRunning,
41   kProcessStateSleeping,
42   kProcessStateUninterruptibleWait,
43   kProcessStateStopped,
44   kProcessStateZombie,
45 };
46 
47 struct ProcessInfo {
48   std::string name;
49   ProcessState state;
50   pid_t tid;
51   pid_t pid;
52   pid_t ppid;
53   pid_t tracer;
54   uid_t uid;
55   uid_t gid;
56 };
57 
58 // Parse the contents of /proc/<tid>/status into |process_info|.
59 bool GetProcessInfo(pid_t tid, ProcessInfo* process_info, std::string* error = nullptr);
60 
61 // Parse the contents of <fd>/status into |process_info|.
62 // |fd| should be an fd pointing at a /proc/<pid> directory.
63 bool GetProcessInfoFromProcPidFd(int fd, ProcessInfo* process_info, std::string* error = nullptr);
64 
65 // Fetch the list of threads from a given process's /proc/<pid> directory.
66 // |fd| should be an fd pointing at a /proc/<pid> directory.
67 template <typename Collection>
68 auto GetProcessTidsFromProcPidFd(int fd, Collection* out, std::string* error = nullptr) ->
69     typename std::enable_if<sizeof(typename Collection::value_type) >= sizeof(pid_t), bool>::type {
70   out->clear();
71 
72   int task_fd = openat(fd, "task", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
73   std::unique_ptr<DIR, int (*)(DIR*)> dir(fdopendir(task_fd), closedir);
74   if (!dir) {
75     if (error != nullptr) {
76       *error = "failed to open task directory";
77     }
78     return false;
79   }
80 
81   struct dirent* dent;
82   while ((dent = readdir(dir.get()))) {
83     if (strcmp(dent->d_name, ".") != 0 && strcmp(dent->d_name, "..") != 0) {
84       pid_t tid;
85       if (!android::base::ParseInt(dent->d_name, &tid, 1, std::numeric_limits<pid_t>::max())) {
86         if (error != nullptr) {
87           *error = std::string("failed to parse task id: ") + dent->d_name;
88         }
89         return false;
90       }
91 
92       out->insert(out->end(), tid);
93     }
94   }
95 
96   return true;
97 }
98 
99 template <typename Collection>
100 auto GetProcessTids(pid_t pid, Collection* out, std::string* error = nullptr) ->
101     typename std::enable_if<sizeof(typename Collection::value_type) >= sizeof(pid_t), bool>::type {
102   char task_path[PATH_MAX];
103   if (snprintf(task_path, PATH_MAX, "/proc/%d", pid) >= PATH_MAX) {
104     if (error != nullptr) {
105       *error = "task path overflow (pid = " + std::to_string(pid) + ")";
106     }
107     return false;
108   }
109 
110   android::base::unique_fd fd(open(task_path, O_DIRECTORY | O_RDONLY | O_CLOEXEC));
111   if (fd == -1) {
112     if (error != nullptr) {
113       *error = std::string("failed to open ") + task_path;
114     }
115     return false;
116   }
117 
118   return GetProcessTidsFromProcPidFd(fd.get(), out, error);
119 }
120 
121 #endif
122 
123 } /* namespace procinfo */
124 } /* namespace android */
125