blob: c5e9d2327b268c41a3778ed3853943164c0eb53e [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>
Yifan Hong23502692020-04-06 18:46:20 -070020#include <android-base/strings.h>
Yifan Hong25c1eed2017-04-07 13:50:24 -070021
22#include "RuntimeInfo.h"
23
24#include "CompatibilityMatrix.h"
Yifan Honge4959a12017-07-05 16:05:09 -070025#include "KernelConfigParser.h"
Yifan Hong25c1eed2017-04-07 13:50:24 -070026#include "parse_string.h"
27
Yifan Hong5075f452017-04-20 14:00:24 -070028#include <dirent.h>
Yifan Hong25c1eed2017-04-07 13:50:24 -070029#include <errno.h>
30#include <sys/utsname.h>
31#include <unistd.h>
32
Yifan Hong242eabf2017-04-20 14:06:26 -070033#include <fstream>
34#include <iostream>
35#include <sstream>
36
Yifan Honge621fb12018-02-28 14:41:38 -080037#include <android-base/properties.h>
Yifan Hong25c1eed2017-04-07 13:50:24 -070038#include <selinux/selinux.h>
39#include <zlib.h>
40
41#define PROC_CONFIG "/proc/config.gz"
42#define BUFFER_SIZE sysconf(_SC_PAGESIZE)
43
Yifan Hong23502692020-04-06 18:46:20 -070044static constexpr char kMainline[] = "-mainline-";
45
Yifan Hong25c1eed2017-04-07 13:50:24 -070046namespace android {
47namespace vintf {
48
Yifan Hong25c1eed2017-04-07 13:50:24 -070049struct RuntimeInfoFetcher {
50 RuntimeInfoFetcher(RuntimeInfo *ki) : mRuntimeInfo(ki) { }
Yifan Hong1fb004e2017-09-26 15:04:44 -070051 status_t fetchAllInformation(RuntimeInfo::FetchFlags flags);
52
53 private:
Yifan Hong5a5dbef2020-11-10 17:31:47 -080054 status_t fetchVersion(RuntimeInfo::FetchFlags flags);
55 status_t fetchKernelConfigs(RuntimeInfo::FetchFlags flags);
56 status_t fetchCpuInfo(RuntimeInfo::FetchFlags flags);
57 status_t fetchKernelSepolicyVers(RuntimeInfo::FetchFlags flags);
58 status_t fetchAvb(RuntimeInfo::FetchFlags flags);
Yifan Hong25c1eed2017-04-07 13:50:24 -070059 status_t parseKernelVersion();
60 RuntimeInfo *mRuntimeInfo;
Yifan Honge4959a12017-07-05 16:05:09 -070061 KernelConfigParser mConfigParser;
Yifan Hong25c1eed2017-04-07 13:50:24 -070062};
63
64// decompress /proc/config.gz and read its contents.
Yifan Hong5a5dbef2020-11-10 17:31:47 -080065status_t RuntimeInfoFetcher::fetchKernelConfigs(RuntimeInfo::FetchFlags) {
Yifan Hong25c1eed2017-04-07 13:50:24 -070066 gzFile f = gzopen(PROC_CONFIG, "rb");
67 if (f == NULL) {
68 LOG(ERROR) << "Could not open /proc/config.gz: " << errno;
69 return -errno;
70 }
71
72 char buf[BUFFER_SIZE];
73 int len;
74 while ((len = gzread(f, buf, sizeof buf)) > 0) {
Yifan Honge4959a12017-07-05 16:05:09 -070075 mConfigParser.process(buf, len);
Yifan Hong25c1eed2017-04-07 13:50:24 -070076 }
77 status_t err = OK;
78 if (len < 0) {
79 int errnum;
80 const char *errmsg = gzerror(f, &errnum);
81 LOG(ERROR) << "Could not read /proc/config.gz: " << errmsg;
82 err = (errnum == Z_ERRNO ? -errno : errnum);
83 }
Yifan Honge4959a12017-07-05 16:05:09 -070084 mConfigParser.finish();
Yifan Hong25c1eed2017-04-07 13:50:24 -070085 gzclose(f);
Yifan Hong90ed9972018-12-03 15:08:34 -080086 mRuntimeInfo->mKernel.mConfigs = std::move(mConfigParser.configs());
Yifan Hong25c1eed2017-04-07 13:50:24 -070087 return err;
88}
89
Yifan Hong5a5dbef2020-11-10 17:31:47 -080090status_t RuntimeInfoFetcher::fetchCpuInfo(RuntimeInfo::FetchFlags) {
Yifan Hong25c1eed2017-04-07 13:50:24 -070091 // TODO implement this; 32-bit and 64-bit has different format.
Yifan Hong242eabf2017-04-20 14:06:26 -070092 std::ifstream in{"/proc/cpuinfo"};
93 if (!in.is_open()) {
94 LOG(WARNING) << "Cannot read /proc/cpuinfo";
95 return UNKNOWN_ERROR;
96 }
97 std::stringstream sstream;
98 sstream << in.rdbuf();
99 mRuntimeInfo->mCpuInfo = sstream.str();
Yifan Hong25c1eed2017-04-07 13:50:24 -0700100 return OK;
101}
102
Yifan Hong5a5dbef2020-11-10 17:31:47 -0800103status_t RuntimeInfoFetcher::fetchKernelSepolicyVers(RuntimeInfo::FetchFlags) {
Yifan Hong25c1eed2017-04-07 13:50:24 -0700104 int pv;
105#ifdef LIBVINTF_TARGET
106 pv = security_policyvers();
107#else
108 pv = 0;
109#endif
110 if (pv < 0) {
111 return pv;
112 }
113 mRuntimeInfo->mKernelSepolicyVersion = pv;
114 return OK;
115}
116
Yifan Hong5a5dbef2020-11-10 17:31:47 -0800117status_t RuntimeInfoFetcher::fetchVersion(RuntimeInfo::FetchFlags) {
Yifan Hong25c1eed2017-04-07 13:50:24 -0700118 struct utsname buf;
119 if (uname(&buf)) {
120 return -errno;
121 }
122 mRuntimeInfo->mOsName = buf.sysname;
123 mRuntimeInfo->mNodeName = buf.nodename;
124 mRuntimeInfo->mOsRelease = buf.release;
125 mRuntimeInfo->mOsVersion = buf.version;
126 mRuntimeInfo->mHardwareId = buf.machine;
127
Yifan Hong5a5dbef2020-11-10 17:31:47 -0800128 mRuntimeInfo->mIsMainline = mRuntimeInfo->mOsRelease.find(kMainline) != std::string::npos;
129
Yifan Hong25c1eed2017-04-07 13:50:24 -0700130 status_t err = parseKernelVersion();
131 if (err != OK) {
132 LOG(ERROR) << "Could not parse kernel version from \""
133 << mRuntimeInfo->mOsRelease << "\"";
134 }
135 return err;
136}
137
138status_t RuntimeInfoFetcher::parseKernelVersion() {
139 auto pos = mRuntimeInfo->mOsRelease.find('.');
140 if (pos == std::string::npos) {
141 return UNKNOWN_ERROR;
142 }
143 pos = mRuntimeInfo->mOsRelease.find('.', pos + 1);
144 if (pos == std::string::npos) {
145 return UNKNOWN_ERROR;
146 }
147 pos = mRuntimeInfo->mOsRelease.find_first_not_of("0123456789", pos + 1);
148 // no need to check pos == std::string::npos, because substr will handle this
Yifan Hong90ed9972018-12-03 15:08:34 -0800149 if (!parse(mRuntimeInfo->mOsRelease.substr(0, pos), &mRuntimeInfo->mKernel.mVersion)) {
Yifan Hong25c1eed2017-04-07 13:50:24 -0700150 return UNKNOWN_ERROR;
151 }
152 return OK;
153}
154
Yifan Hong5a5dbef2020-11-10 17:31:47 -0800155status_t RuntimeInfoFetcher::fetchAvb(RuntimeInfo::FetchFlags) {
Yifan Honge621fb12018-02-28 14:41:38 -0800156 std::string prop = android::base::GetProperty("ro.boot.vbmeta.avb_version", "0.0");
Yifan Hong881a9e452017-04-27 19:31:13 -0700157 if (!parse(prop, &mRuntimeInfo->mBootVbmetaAvbVersion)) {
Yifan Hongf3029302017-04-12 17:23:49 -0700158 return UNKNOWN_ERROR;
159 }
Yifan Honge621fb12018-02-28 14:41:38 -0800160 prop = android::base::GetProperty("ro.boot.avb_version", "0.0");
Yifan Hong881a9e452017-04-27 19:31:13 -0700161 if (!parse(prop, &mRuntimeInfo->mBootAvbVersion)) {
Yifan Hongf3029302017-04-12 17:23:49 -0700162 return UNKNOWN_ERROR;
163 }
164 return OK;
165}
166
Yifan Hong5a5dbef2020-11-10 17:31:47 -0800167struct FetchFunction {
168 RuntimeInfo::FetchFlags flags;
169 std::function<status_t(RuntimeInfoFetcher*, RuntimeInfo::FetchFlags)> fetch;
170 std::string description;
171};
Yifan Hong1fb004e2017-09-26 15:04:44 -0700172
Yifan Hong5a5dbef2020-11-10 17:31:47 -0800173status_t RuntimeInfoFetcher::fetchAllInformation(RuntimeInfo::FetchFlags flags) {
Yifan Hong1fb004e2017-09-26 15:04:44 -0700174 using F = RuntimeInfo::FetchFlag;
175 using RF = RuntimeInfoFetcher;
Yifan Hong5a5dbef2020-11-10 17:31:47 -0800176 // clang-format off
177 const static std::vector<FetchFunction> gFetchFunctions({
178 {F::CPU_VERSION, &RF::fetchVersion, "/proc/version"},
179 {F::CONFIG_GZ, &RF::fetchKernelConfigs, "/proc/config.gz"},
180 {F::CPU_INFO, &RF::fetchCpuInfo, "/proc/cpuinfo"},
181 {F::POLICYVERS, &RF::fetchKernelSepolicyVers, "kernel sepolicy version"},
182 {F::AVB, &RF::fetchAvb, "avb version"},
Yifan Hong1fb004e2017-09-26 15:04:44 -0700183 });
Yifan Hong5a5dbef2020-11-10 17:31:47 -0800184 // clang-format on
Yifan Hong1fb004e2017-09-26 15:04:44 -0700185
Yifan Hong25c1eed2017-04-07 13:50:24 -0700186 status_t err;
Yifan Hong5a5dbef2020-11-10 17:31:47 -0800187 for (const auto& fetchFunction : gFetchFunctions)
188 if ((flags & fetchFunction.flags) && (err = (fetchFunction.fetch(this, flags)) != OK))
189 LOG(WARNING) << "Cannot fetch or parse " << fetchFunction.description << ": "
Yifan Hong1fb004e2017-09-26 15:04:44 -0700190 << strerror(-err);
191
Yifan Hong25c1eed2017-04-07 13:50:24 -0700192 return OK;
193}
194
Yifan Hong1fb004e2017-09-26 15:04:44 -0700195status_t RuntimeInfo::fetchAllInformation(RuntimeInfo::FetchFlags flags) {
196 return RuntimeInfoFetcher(this).fetchAllInformation(flags);
Yifan Hong25c1eed2017-04-07 13:50:24 -0700197}
198
199} // namespace vintf
200} // namespace android