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