1 /*
2 * Copyright (C) 2017 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
18 #define LOG_TAG "libvintf"
19 #include <android-base/logging.h>
20
21 #include "RuntimeInfo.h"
22
23 #include "CompatibilityMatrix.h"
24 #include "parse_string.h"
25
26 #include <dirent.h>
27 #include <errno.h>
28 #include <sys/utsname.h>
29 #include <unistd.h>
30
31 #include <fstream>
32 #include <iostream>
33 #include <sstream>
34
35 #include <cutils/properties.h>
36 #include <selinux/selinux.h>
37 #include <zlib.h>
38
39 #define PROC_CONFIG "/proc/config.gz"
40 #define BUFFER_SIZE sysconf(_SC_PAGESIZE)
41
42 namespace android {
43 namespace vintf {
44
removeTrailingComments(std::string * s)45 static void removeTrailingComments(std::string *s) {
46 size_t sharpPos = s->find('#');
47 if (sharpPos != std::string::npos) {
48 s->erase(sharpPos);
49 }
50 }
trim(std::string * s)51 static void trim(std::string *s) {
52 auto l = s->begin();
53 for (; l != s->end() && std::isspace(*l); ++l);
54 s->erase(s->begin(), l);
55 auto r = s->rbegin();
56 for (; r != s->rend() && std::isspace(*r); ++r);
57 s->erase(r.base(), s->end());
58 }
59
60 struct RuntimeInfoFetcher {
RuntimeInfoFetcherandroid::vintf::RuntimeInfoFetcher61 RuntimeInfoFetcher(RuntimeInfo *ki) : mRuntimeInfo(ki) { }
62 status_t fetchAllInformation();
63 private:
64 void streamConfig(const char *buf, size_t len);
65 void parseConfig(std::string *s);
66 status_t fetchVersion();
67 status_t fetchKernelConfigs();
68 status_t fetchCpuInfo();
69 status_t fetchKernelSepolicyVers();
70 status_t fetchAvb();
71 status_t parseKernelVersion();
72 RuntimeInfo *mRuntimeInfo;
73 std::string mRemaining;
74 };
75
76 // decompress /proc/config.gz and read its contents.
fetchKernelConfigs()77 status_t RuntimeInfoFetcher::fetchKernelConfigs() {
78 gzFile f = gzopen(PROC_CONFIG, "rb");
79 if (f == NULL) {
80 LOG(ERROR) << "Could not open /proc/config.gz: " << errno;
81 return -errno;
82 }
83
84 char buf[BUFFER_SIZE];
85 int len;
86 while ((len = gzread(f, buf, sizeof buf)) > 0) {
87 streamConfig(buf, len);
88 }
89 status_t err = OK;
90 if (len < 0) {
91 int errnum;
92 const char *errmsg = gzerror(f, &errnum);
93 LOG(ERROR) << "Could not read /proc/config.gz: " << errmsg;
94 err = (errnum == Z_ERRNO ? -errno : errnum);
95 }
96
97 // stream a "\n" to end the stream to finish the last line.
98 streamConfig("\n", 1 /* sizeof "\n" */);
99
100 gzclose(f);
101 return err;
102 }
103
parseConfig(std::string * s)104 void RuntimeInfoFetcher::parseConfig(std::string *s) {
105 removeTrailingComments(s);
106 trim(s);
107 if (s->empty()) {
108 return;
109 }
110 size_t equalPos = s->find('=');
111 if (equalPos == std::string::npos) {
112 LOG(WARNING) << "Unrecognized line in /proc/config.gz: " << *s;
113 return;
114 }
115 std::string key = s->substr(0, equalPos);
116 std::string value = s->substr(equalPos + 1);
117 if (!mRuntimeInfo->mKernelConfigs.emplace(std::move(key), std::move(value)).second) {
118 LOG(WARNING) << "Duplicated key in /proc/config.gz: " << s->substr(0, equalPos);
119 return;
120 }
121 }
122
streamConfig(const char * buf,size_t len)123 void RuntimeInfoFetcher::streamConfig(const char *buf, size_t len) {
124 const char *begin = buf;
125 const char *end = buf;
126 const char *stop = buf + len;
127 while (end < stop) {
128 if (*end == '\n') {
129 mRemaining.insert(mRemaining.size(), begin, end - begin);
130 parseConfig(&mRemaining);
131 mRemaining.clear();
132 begin = end + 1;
133 }
134 end++;
135 }
136 mRemaining.insert(mRemaining.size(), begin, end - begin);
137 }
138
fetchCpuInfo()139 status_t RuntimeInfoFetcher::fetchCpuInfo() {
140 // TODO implement this; 32-bit and 64-bit has different format.
141 std::ifstream in{"/proc/cpuinfo"};
142 if (!in.is_open()) {
143 LOG(WARNING) << "Cannot read /proc/cpuinfo";
144 return UNKNOWN_ERROR;
145 }
146 std::stringstream sstream;
147 sstream << in.rdbuf();
148 mRuntimeInfo->mCpuInfo = sstream.str();
149 return OK;
150 }
151
fetchKernelSepolicyVers()152 status_t RuntimeInfoFetcher::fetchKernelSepolicyVers() {
153 int pv;
154 #ifdef LIBVINTF_TARGET
155 pv = security_policyvers();
156 #else
157 pv = 0;
158 #endif
159 if (pv < 0) {
160 return pv;
161 }
162 mRuntimeInfo->mKernelSepolicyVersion = pv;
163 return OK;
164 }
165
fetchVersion()166 status_t RuntimeInfoFetcher::fetchVersion() {
167 struct utsname buf;
168 if (uname(&buf)) {
169 return -errno;
170 }
171 mRuntimeInfo->mOsName = buf.sysname;
172 mRuntimeInfo->mNodeName = buf.nodename;
173 mRuntimeInfo->mOsRelease = buf.release;
174 mRuntimeInfo->mOsVersion = buf.version;
175 mRuntimeInfo->mHardwareId = buf.machine;
176
177 status_t err = parseKernelVersion();
178 if (err != OK) {
179 LOG(ERROR) << "Could not parse kernel version from \""
180 << mRuntimeInfo->mOsRelease << "\"";
181 }
182 return err;
183 }
184
parseKernelVersion()185 status_t RuntimeInfoFetcher::parseKernelVersion() {
186 auto pos = mRuntimeInfo->mOsRelease.find('.');
187 if (pos == std::string::npos) {
188 return UNKNOWN_ERROR;
189 }
190 pos = mRuntimeInfo->mOsRelease.find('.', pos + 1);
191 if (pos == std::string::npos) {
192 return UNKNOWN_ERROR;
193 }
194 pos = mRuntimeInfo->mOsRelease.find_first_not_of("0123456789", pos + 1);
195 // no need to check pos == std::string::npos, because substr will handle this
196 if (!parse(mRuntimeInfo->mOsRelease.substr(0, pos), &mRuntimeInfo->mKernelVersion)) {
197 return UNKNOWN_ERROR;
198 }
199 return OK;
200 }
201
fetchAvb()202 status_t RuntimeInfoFetcher::fetchAvb() {
203 char prop[PROPERTY_VALUE_MAX];
204 property_get("ro.boot.vbmeta.avb_version", prop, "0.0");
205 if (!parse(prop, &mRuntimeInfo->mBootVbmetaAvbVersion)) {
206 return UNKNOWN_ERROR;
207 }
208 property_get("ro.boot.avb_version", prop, "0.0");
209 if (!parse(prop, &mRuntimeInfo->mBootAvbVersion)) {
210 return UNKNOWN_ERROR;
211 }
212 return OK;
213 }
214
fetchAllInformation()215 status_t RuntimeInfoFetcher::fetchAllInformation() {
216 status_t err;
217 if ((err = fetchVersion()) != OK) {
218 LOG(WARNING) << "Cannot fetch or parse /proc/version: " << strerror(-err);
219 }
220 if ((err = fetchKernelConfigs()) != OK) {
221 LOG(WARNING) << "Cannot fetch or parse /proc/config.gz: " << strerror(-err);
222 }
223 if ((err = fetchCpuInfo()) != OK) {
224 LOG(WARNING) << "Cannot fetch /proc/cpuinfo: " << strerror(-err);
225 }
226 if ((err = fetchKernelSepolicyVers()) != OK) {
227 LOG(WARNING) << "Cannot fetch kernel sepolicy version: " << strerror(-err);
228 }
229 if ((err = fetchAvb()) != OK) {
230 LOG(WARNING) << "Cannot fetch sepolicy avb version: " << strerror(-err);
231 }
232 return OK;
233 }
234
fetchAllInformation()235 status_t RuntimeInfo::fetchAllInformation() {
236 return RuntimeInfoFetcher(this).fetchAllInformation();
237 }
238
239 } // namespace vintf
240 } // namespace android
241