diff options
Diffstat (limited to 'libprocinfo/include/procinfo/process.h')
-rw-r--r-- | libprocinfo/include/procinfo/process.h | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/libprocinfo/include/procinfo/process.h b/libprocinfo/include/procinfo/process.h new file mode 100644 index 000000000..fb140ff3d --- /dev/null +++ b/libprocinfo/include/procinfo/process.h | |||
@@ -0,0 +1,106 @@ | |||
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 | struct ProcessInfo { | ||
39 | std::string name; | ||
40 | pid_t tid; | ||
41 | pid_t pid; | ||
42 | pid_t ppid; | ||
43 | pid_t tracer; | ||
44 | uid_t uid; | ||
45 | uid_t gid; | ||
46 | }; | ||
47 | |||
48 | // Parse the contents of /proc/<tid>/status into |process_info|. | ||
49 | bool GetProcessInfo(pid_t tid, ProcessInfo* process_info); | ||
50 | |||
51 | // Parse the contents of <fd>/status into |process_info|. | ||
52 | // |fd| should be an fd pointing at a /proc/<pid> directory. | ||
53 | bool GetProcessInfoFromProcPidFd(int fd, ProcessInfo* process_info); | ||
54 | |||
55 | // Fetch the list of threads from a given process's /proc/<pid> directory. | ||
56 | // |fd| should be an fd pointing at a /proc/<pid> directory. | ||
57 | template <typename Collection> | ||
58 | auto GetProcessTidsFromProcPidFd(int fd, Collection* out) -> | ||
59 | typename std::enable_if<sizeof(typename Collection::value_type) >= sizeof(pid_t), bool>::type { | ||
60 | out->clear(); | ||
61 | |||
62 | int task_fd = openat(fd, "task", O_DIRECTORY | O_RDONLY | O_CLOEXEC); | ||
63 | std::unique_ptr<DIR, int (*)(DIR*)> dir(fdopendir(task_fd), closedir); | ||
64 | if (!dir) { | ||
65 | PLOG(ERROR) << "failed to open task directory"; | ||
66 | return false; | ||
67 | } | ||
68 | |||
69 | struct dirent* dent; | ||
70 | while ((dent = readdir(dir.get()))) { | ||
71 | if (strcmp(dent->d_name, ".") != 0 && strcmp(dent->d_name, "..") != 0) { | ||
72 | pid_t tid; | ||
73 | if (!android::base::ParseInt(dent->d_name, &tid, 1, std::numeric_limits<pid_t>::max())) { | ||
74 | LOG(ERROR) << "failed to parse task id: " << dent->d_name; | ||
75 | return false; | ||
76 | } | ||
77 | |||
78 | out->insert(out->end(), tid); | ||
79 | } | ||
80 | } | ||
81 | |||
82 | return true; | ||
83 | } | ||
84 | |||
85 | template <typename Collection> | ||
86 | auto GetProcessTids(pid_t pid, Collection* out) -> | ||
87 | typename std::enable_if<sizeof(typename Collection::value_type) >= sizeof(pid_t), bool>::type { | ||
88 | char task_path[PATH_MAX]; | ||
89 | if (snprintf(task_path, PATH_MAX, "/proc/%d", pid) >= PATH_MAX) { | ||
90 | LOG(ERROR) << "task path overflow (pid = " << pid << ")"; | ||
91 | return false; | ||
92 | } | ||
93 | |||
94 | android::base::unique_fd fd(open(task_path, O_DIRECTORY | O_RDONLY | O_CLOEXEC)); | ||
95 | if (fd == -1) { | ||
96 | PLOG(ERROR) << "failed to open " << task_path; | ||
97 | return false; | ||
98 | } | ||
99 | |||
100 | return GetProcessTidsFromProcPidFd(fd.get(), out); | ||
101 | } | ||
102 | |||
103 | #endif | ||
104 | |||
105 | } /* namespace procinfo */ | ||
106 | } /* namespace android */ | ||