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