1 /*
2 * Copyright (C) 2018 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 <procpartition/procpartition.h>
18
19 #include <android-base/file.h>
20
21 namespace android {
22 namespace procpartition {
23
operator <<(std::ostream & os,Partition p)24 std::ostream& operator<<(std::ostream& os, Partition p) {
25 switch (p) {
26 case Partition::SYSTEM: return os << "system";
27 case Partition::SYSTEM_EXT: return os << "system_ext";
28 case Partition::PRODUCT: return os << "product";
29 case Partition::VENDOR: return os << "vendor";
30 case Partition::ODM: return os << "odm";
31 case Partition::UNKNOWN: // fallthrough
32 default:
33 return os << "(unknown)";
34 }
35 }
36
getExe(pid_t pid)37 std::string getExe(pid_t pid) {
38 std::string exe;
39 std::string real;
40 if (!android::base::Readlink("/proc/" + std::to_string(pid) + "/exe", &exe)) {
41 return "";
42 }
43 if (!android::base::Realpath(exe, &real)) {
44 return "";
45 }
46 return real;
47 }
48
getCmdline(pid_t pid)49 std::string getCmdline(pid_t pid) {
50 std::string content;
51 if (!android::base::ReadFileToString("/proc/" + std::to_string(pid) + "/cmdline", &content,
52 false /* follow symlinks */)) {
53 return "";
54 }
55 return std::string{content.c_str()};
56 }
57
parsePartition(const std::string & s)58 Partition parsePartition(const std::string& s) {
59 if (s == "system") {
60 return Partition::SYSTEM;
61 }
62 if (s == "system_ext") {
63 return Partition::SYSTEM_EXT;
64 }
65 if (s == "product") {
66 return Partition::PRODUCT;
67 }
68 if (s == "vendor") {
69 return Partition::VENDOR;
70 }
71 if (s == "odm") {
72 return Partition::ODM;
73 }
74 return Partition::UNKNOWN;
75 }
76
getPartitionFromRealpath(const std::string & path)77 Partition getPartitionFromRealpath(const std::string& path) {
78 if (path == "/system/bin/app_process64" ||
79 path == "/system/bin/app_process32") {
80
81 return Partition::UNKNOWN; // cannot determine
82 }
83 size_t backslash = path.find_first_of('/', 1);
84 std::string partition = (backslash != std::string::npos) ? path.substr(1, backslash - 1) : path;
85
86 return parsePartition(partition);
87 }
88
getPartitionFromCmdline(pid_t pid)89 Partition getPartitionFromCmdline(pid_t pid) {
90 const auto& cmdline = getCmdline(pid);
91 if (cmdline == "system_server") {
92 return Partition::SYSTEM;
93 }
94 if (cmdline.empty() || cmdline.front() != '/') {
95 return Partition::UNKNOWN;
96 }
97 return getPartitionFromRealpath(cmdline);
98 }
99
getPartitionFromExe(pid_t pid)100 Partition getPartitionFromExe(pid_t pid) {
101 const auto& real = getExe(pid);
102 if (real.empty() || real.front() != '/') {
103 return Partition::UNKNOWN;
104 }
105 return getPartitionFromRealpath(real);
106 }
107
108
getPartition(pid_t pid)109 Partition getPartition(pid_t pid) {
110 Partition partition = getPartitionFromExe(pid);
111 if (partition == Partition::UNKNOWN) {
112 partition = getPartitionFromCmdline(pid);
113 }
114 return partition;
115 }
116
117 } // namespace procpartition
118 } // namespace android
119