1*288bf522SAndroid Build Coastguard Worker // Copyright (C) 2015 The Android Open Source Project
2*288bf522SAndroid Build Coastguard Worker //
3*288bf522SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*288bf522SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*288bf522SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*288bf522SAndroid Build Coastguard Worker //
7*288bf522SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0
8*288bf522SAndroid Build Coastguard Worker //
9*288bf522SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*288bf522SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*288bf522SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*288bf522SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*288bf522SAndroid Build Coastguard Worker // limitations under the License.
14*288bf522SAndroid Build Coastguard Worker
15*288bf522SAndroid Build Coastguard Worker #include <dirent.h>
16*288bf522SAndroid Build Coastguard Worker #include <errno.h>
17*288bf522SAndroid Build Coastguard Worker #include <stdlib.h>
18*288bf522SAndroid Build Coastguard Worker #include <string.h>
19*288bf522SAndroid Build Coastguard Worker #include <sys/types.h>
20*288bf522SAndroid Build Coastguard Worker
21*288bf522SAndroid Build Coastguard Worker #include <map>
22*288bf522SAndroid Build Coastguard Worker #include <memory>
23*288bf522SAndroid Build Coastguard Worker #include <string>
24*288bf522SAndroid Build Coastguard Worker #include <vector>
25*288bf522SAndroid Build Coastguard Worker
26*288bf522SAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
27*288bf522SAndroid Build Coastguard Worker
28*288bf522SAndroid Build Coastguard Worker #include "tasklist.h"
29*288bf522SAndroid Build Coastguard Worker
30*288bf522SAndroid Build Coastguard Worker template <typename Func>
ScanPidsInDir(const std::string & name,Func f)31*288bf522SAndroid Build Coastguard Worker static bool ScanPidsInDir(const std::string& name, Func f) {
32*288bf522SAndroid Build Coastguard Worker std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(name.c_str()), closedir);
33*288bf522SAndroid Build Coastguard Worker if (!dir) {
34*288bf522SAndroid Build Coastguard Worker return false;
35*288bf522SAndroid Build Coastguard Worker }
36*288bf522SAndroid Build Coastguard Worker
37*288bf522SAndroid Build Coastguard Worker dirent* entry;
38*288bf522SAndroid Build Coastguard Worker while ((entry = readdir(dir.get())) != nullptr) {
39*288bf522SAndroid Build Coastguard Worker if (isdigit(entry->d_name[0])) {
40*288bf522SAndroid Build Coastguard Worker pid_t pid = atoi(entry->d_name);
41*288bf522SAndroid Build Coastguard Worker f(pid);
42*288bf522SAndroid Build Coastguard Worker }
43*288bf522SAndroid Build Coastguard Worker }
44*288bf522SAndroid Build Coastguard Worker
45*288bf522SAndroid Build Coastguard Worker return true;
46*288bf522SAndroid Build Coastguard Worker }
47*288bf522SAndroid Build Coastguard Worker
Scan(std::map<pid_t,std::vector<pid_t>> & tgid_map)48*288bf522SAndroid Build Coastguard Worker bool TaskList::Scan(std::map<pid_t, std::vector<pid_t>>& tgid_map) {
49*288bf522SAndroid Build Coastguard Worker tgid_map.clear();
50*288bf522SAndroid Build Coastguard Worker
51*288bf522SAndroid Build Coastguard Worker return ScanPidsInDir("/proc", [&tgid_map](pid_t tgid) {
52*288bf522SAndroid Build Coastguard Worker std::vector<pid_t> pid_list;
53*288bf522SAndroid Build Coastguard Worker if (ScanPid(tgid, pid_list)) {
54*288bf522SAndroid Build Coastguard Worker tgid_map.insert({tgid, pid_list});
55*288bf522SAndroid Build Coastguard Worker }
56*288bf522SAndroid Build Coastguard Worker });
57*288bf522SAndroid Build Coastguard Worker }
58*288bf522SAndroid Build Coastguard Worker
ScanPid(pid_t tgid,std::vector<pid_t> & pid_list)59*288bf522SAndroid Build Coastguard Worker bool TaskList::ScanPid(pid_t tgid, std::vector<pid_t>& pid_list) {
60*288bf522SAndroid Build Coastguard Worker std::string filename = android::base::StringPrintf("/proc/%d/task", tgid);
61*288bf522SAndroid Build Coastguard Worker
62*288bf522SAndroid Build Coastguard Worker return ScanPidsInDir(filename, [&pid_list](pid_t pid) { pid_list.push_back(pid); });
63*288bf522SAndroid Build Coastguard Worker }
64