1 /**
2  * Copyright (c) 2020, 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 #include "ProcPidDir.h"
18 
19 #include "UidProcStatsCollector.h"
20 
21 #include <android-base/file.h>
22 #include <android-base/result.h>
23 
24 #include <errno.h>
25 
26 namespace android {
27 namespace automotive {
28 namespace watchdog {
29 namespace testing {
30 
31 using ::android::base::Error;
32 using ::android::base::Result;
33 using ::android::base::WriteStringToFile;
34 
35 namespace {
36 
makeDir(std::string path)37 Result<void> makeDir(std::string path) {
38     if (mkdir(path.c_str(), 0700) && errno != EEXIST) {
39         return Error() << "Could not mkdir " << path << ": " << strerror(errno);
40     }
41     return {};
42 }
43 
44 }  // namespace
45 
populateProcPidDir(const std::string & procDirPath,const std::unordered_map<pid_t,std::vector<pid_t>> & pidToTids,const std::unordered_map<pid_t,std::string> & processStat,const std::unordered_map<pid_t,std::string> & processStatus,const std::unordered_map<pid_t,std::string> & processSmapsRollup,const std::unordered_map<pid_t,std::string> & processStatm,const std::unordered_map<pid_t,std::string> & threadStat,const std::unordered_map<pid_t,std::string> & threadTimeInState)46 Result<void> populateProcPidDir(const std::string& procDirPath,
47                                 const std::unordered_map<pid_t, std::vector<pid_t>>& pidToTids,
48                                 const std::unordered_map<pid_t, std::string>& processStat,
49                                 const std::unordered_map<pid_t, std::string>& processStatus,
50                                 const std::unordered_map<pid_t, std::string>& processSmapsRollup,
51                                 const std::unordered_map<pid_t, std::string>& processStatm,
52                                 const std::unordered_map<pid_t, std::string>& threadStat,
53                                 const std::unordered_map<pid_t, std::string>& threadTimeInState) {
54     for (const auto& it : pidToTids) {
55         // 1. Create /proc/PID dir.
56         const auto& pidDirRes = makeDir(StringPrintf("%s/%" PRIu32, procDirPath.c_str(), it.first));
57         if (!pidDirRes.ok()) {
58             return Error() << "Failed to create top-level per-process directory: "
59                            << pidDirRes.error();
60         }
61 
62         // 2. Create /proc/PID/stat file.
63         uint32_t pid = it.first;
64         if (processStat.find(pid) != processStat.end()) {
65             std::string path = StringPrintf((procDirPath + kStatFileFormat).c_str(), pid);
66             if (!WriteStringToFile(processStat.at(pid), path)) {
67                 return Error() << "Failed to write pid stat file " << path;
68             }
69         }
70 
71         // 3. Create /proc/PID/status file.
72         if (processStatus.find(pid) != processStatus.end()) {
73             std::string path = StringPrintf((procDirPath + kStatusFileFormat).c_str(), pid);
74             if (!WriteStringToFile(processStatus.at(pid), path)) {
75                 return Error() << "Failed to write pid status file " << path;
76             }
77         }
78 
79         // 4. Create /proc/PID/smaps_rollup file.
80         if (processSmapsRollup.find(pid) != processSmapsRollup.end()) {
81             std::string path = StringPrintf((procDirPath + kSmapsRollupFileFormat).c_str(), pid);
82             if (!WriteStringToFile(processSmapsRollup.at(pid), path)) {
83                 return Error() << "Failed to write pid smaps_rollup file " << path;
84             }
85         }
86 
87         // 5. Create /proc/PID/statm file.
88         if (processStatm.find(pid) != processStatm.end()) {
89             std::string path = StringPrintf((procDirPath + kStatmFileFormat).c_str(), pid);
90             if (!WriteStringToFile(processStatm.at(pid), path)) {
91                 return Error() << "Failed to write pid statm file " << path;
92             }
93         }
94 
95         // 6. Create /proc/PID/task dir.
96         const auto& taskDirRes = makeDir(StringPrintf((procDirPath + kTaskDirFormat).c_str(), pid));
97         if (!taskDirRes.ok()) {
98             return Error() << "Failed to create task directory: " << taskDirRes.error();
99         }
100 
101         // 7. Create /proc/PID/task/TID dirs, /proc/PID/task/TID/stat and
102         //    /proc/PID/task/TID/time_in_state files.
103         for (const auto& tid : it.second) {
104             const auto& tidDirRes = makeDir(
105                     StringPrintf((procDirPath + kTaskDirFormat + "/%" PRIu32).c_str(), pid, tid));
106             if (!tidDirRes.ok()) {
107                 return Error() << "Failed to create per-thread directory: " << tidDirRes.error();
108             }
109             if (threadStat.find(tid) != threadStat.end()) {
110                 std::string path =
111                         StringPrintf((procDirPath + kTaskDirFormat + kStatFileFormat).c_str(), pid,
112                                      tid);
113                 if (!WriteStringToFile(threadStat.at(tid), path)) {
114                     return Error() << "Failed to write thread stat file " << path;
115                 }
116             }
117             if (threadTimeInState.find(tid) != threadTimeInState.end()) {
118                 std::string path =
119                         StringPrintf((procDirPath + kTaskDirFormat + kTimeInStateFileFormat)
120                                              .c_str(),
121                                      pid, tid);
122                 if (!WriteStringToFile(threadTimeInState.at(tid), path)) {
123                     return Error() << "Failed to write thread time_in_state file " << path;
124                 }
125             }
126         }
127     }
128 
129     return {};
130 }
131 
132 }  // namespace testing
133 }  // namespace watchdog
134 }  // namespace automotive
135 }  // namespace android
136