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 33namespace android { 34namespace procinfo { 35 36#if defined(__linux__) 37 38enum ProcessState { 39 kProcessStateUnknown, 40 kProcessStateRunning, 41 kProcessStateSleeping, 42 kProcessStateUninterruptibleWait, 43 kProcessStateStopped, 44 kProcessStateZombie, 45}; 46 47struct 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|. 59bool GetProcessInfo(pid_t tid, ProcessInfo* process_info); 60 61// Parse the contents of <fd>/status into |process_info|. 62// |fd| should be an fd pointing at a /proc/<pid> directory. 63bool GetProcessInfoFromProcPidFd(int fd, ProcessInfo* process_info); 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. 67template <typename Collection> 68auto GetProcessTidsFromProcPidFd(int fd, Collection* out) -> 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 PLOG(ERROR) << "failed to open task directory"; 76 return false; 77 } 78 79 struct dirent* dent; 80 while ((dent = readdir(dir.get()))) { 81 if (strcmp(dent->d_name, ".") != 0 && strcmp(dent->d_name, "..") != 0) { 82 pid_t tid; 83 if (!android::base::ParseInt(dent->d_name, &tid, 1, std::numeric_limits<pid_t>::max())) { 84 LOG(ERROR) << "failed to parse task id: " << dent->d_name; 85 return false; 86 } 87 88 out->insert(out->end(), tid); 89 } 90 } 91 92 return true; 93} 94 95template <typename Collection> 96auto GetProcessTids(pid_t pid, Collection* out) -> 97 typename std::enable_if<sizeof(typename Collection::value_type) >= sizeof(pid_t), bool>::type { 98 char task_path[PATH_MAX]; 99 if (snprintf(task_path, PATH_MAX, "/proc/%d", pid) >= PATH_MAX) { 100 LOG(ERROR) << "task path overflow (pid = " << pid << ")"; 101 return false; 102 } 103 104 android::base::unique_fd fd(open(task_path, O_DIRECTORY | O_RDONLY | O_CLOEXEC)); 105 if (fd == -1) { 106 PLOG(ERROR) << "failed to open " << task_path; 107 return false; 108 } 109 110 return GetProcessTidsFromProcPidFd(fd.get(), out); 111} 112 113#endif 114 115} /* namespace procinfo */ 116} /* namespace android */ 117