blob: 95c0ae0873ce79d5123c733bee30f3e216cdb4e0 [file] [log] [blame]
Yifan Hong25c1eed2017-04-07 13:50:24 -07001/*
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
Yifan Hong5075f452017-04-20 14:00:24 -070026#include <dirent.h>
Yifan Hong25c1eed2017-04-07 13:50:24 -070027#include <errno.h>
28#include <sys/utsname.h>
29#include <unistd.h>
30
Yifan Hong242eabf2017-04-20 14:06:26 -070031#include <fstream>
32#include <iostream>
33#include <sstream>
34
Yifan Hongf3029302017-04-12 17:23:49 -070035#include <cutils/properties.h>
Yifan Hong25c1eed2017-04-07 13:50:24 -070036#include <selinux/selinux.h>
37#include <zlib.h>
38
39#define PROC_CONFIG "/proc/config.gz"
40#define BUFFER_SIZE sysconf(_SC_PAGESIZE)
41
42namespace android {
43namespace vintf {
44
45static void removeTrailingComments(std::string *s) {
46 size_t sharpPos = s->find('#');
47 if (sharpPos != std::string::npos) {
48 s->erase(sharpPos);
49 }
50}
51static 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
60struct RuntimeInfoFetcher {
61 RuntimeInfoFetcher(RuntimeInfo *ki) : mRuntimeInfo(ki) { }
62 status_t fetchAllInformation();
63private:
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();
Yifan Hongf3029302017-04-12 17:23:49 -070070 status_t fetchAvb();
Yifan Hong25c1eed2017-04-07 13:50:24 -070071 status_t parseKernelVersion();
72 RuntimeInfo *mRuntimeInfo;
73 std::string mRemaining;
74};
75
76// decompress /proc/config.gz and read its contents.
77status_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
104void 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
123void 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
139status_t RuntimeInfoFetcher::fetchCpuInfo() {
140 // TODO implement this; 32-bit and 64-bit has different format.
Yifan Hong242eabf2017-04-20 14:06:26 -0700141 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();
Yifan Hong25c1eed2017-04-07 13:50:24 -0700149 return OK;
150}
151
152status_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
166status_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
185status_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
Yifan Hongf3029302017-04-12 17:23:49 -0700202status_t RuntimeInfoFetcher::fetchAvb() {
203 char prop[PROPERTY_VALUE_MAX];
204 property_get("ro.boot.vbmeta.avb_version", prop, "0.0");
Yifan Hong881a9e452017-04-27 19:31:13 -0700205 if (!parse(prop, &mRuntimeInfo->mBootVbmetaAvbVersion)) {
Yifan Hongf3029302017-04-12 17:23:49 -0700206 return UNKNOWN_ERROR;
207 }
208 property_get("ro.boot.avb_version", prop, "0.0");
Yifan Hong881a9e452017-04-27 19:31:13 -0700209 if (!parse(prop, &mRuntimeInfo->mBootAvbVersion)) {
Yifan Hongf3029302017-04-12 17:23:49 -0700210 return UNKNOWN_ERROR;
211 }
212 return OK;
213}
214
Yifan Hong25c1eed2017-04-07 13:50:24 -0700215status_t RuntimeInfoFetcher::fetchAllInformation() {
216 status_t err;
217 if ((err = fetchVersion()) != OK) {
Yifan Hong3477f042017-05-08 14:02:22 -0700218 LOG(WARNING) << "Cannot fetch or parse /proc/version: " << strerror(-err);
Yifan Hong25c1eed2017-04-07 13:50:24 -0700219 }
220 if ((err = fetchKernelConfigs()) != OK) {
Yifan Hong3477f042017-05-08 14:02:22 -0700221 LOG(WARNING) << "Cannot fetch or parse /proc/config.gz: " << strerror(-err);
Yifan Hong25c1eed2017-04-07 13:50:24 -0700222 }
223 if ((err = fetchCpuInfo()) != OK) {
Yifan Hong3477f042017-05-08 14:02:22 -0700224 LOG(WARNING) << "Cannot fetch /proc/cpuinfo: " << strerror(-err);
Yifan Hong25c1eed2017-04-07 13:50:24 -0700225 }
226 if ((err = fetchKernelSepolicyVers()) != OK) {
Yifan Hong3477f042017-05-08 14:02:22 -0700227 LOG(WARNING) << "Cannot fetch kernel sepolicy version: " << strerror(-err);
Yifan Hong25c1eed2017-04-07 13:50:24 -0700228 }
Yifan Hongf3029302017-04-12 17:23:49 -0700229 if ((err = fetchAvb()) != OK) {
Yifan Hong3477f042017-05-08 14:02:22 -0700230 LOG(WARNING) << "Cannot fetch sepolicy avb version: " << strerror(-err);
Yifan Hongf3029302017-04-12 17:23:49 -0700231 }
Yifan Hong25c1eed2017-04-07 13:50:24 -0700232 return OK;
233}
234
235status_t RuntimeInfo::fetchAllInformation() {
236 return RuntimeInfoFetcher(this).fetchAllInformation();
237}
238
239} // namespace vintf
240} // namespace android