Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 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" |
Yifan Hong | e4959a1 | 2017-07-05 16:05:09 -0700 | [diff] [blame] | 24 | #include "KernelConfigParser.h" |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 25 | #include "parse_string.h" |
| 26 | |
Yifan Hong | 5075f45 | 2017-04-20 14:00:24 -0700 | [diff] [blame] | 27 | #include <dirent.h> |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 28 | #include <errno.h> |
| 29 | #include <sys/utsname.h> |
| 30 | #include <unistd.h> |
| 31 | |
Yifan Hong | 242eabf | 2017-04-20 14:06:26 -0700 | [diff] [blame] | 32 | #include <fstream> |
| 33 | #include <iostream> |
| 34 | #include <sstream> |
| 35 | |
Yifan Hong | 4641979 | 2018-02-28 14:41:38 -0800 | [diff] [blame^] | 36 | #include <android-base/properties.h> |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 37 | #include <selinux/selinux.h> |
| 38 | #include <zlib.h> |
| 39 | |
| 40 | #define PROC_CONFIG "/proc/config.gz" |
| 41 | #define BUFFER_SIZE sysconf(_SC_PAGESIZE) |
| 42 | |
| 43 | namespace android { |
| 44 | namespace vintf { |
| 45 | |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 46 | struct RuntimeInfoFetcher { |
| 47 | RuntimeInfoFetcher(RuntimeInfo *ki) : mRuntimeInfo(ki) { } |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 48 | status_t fetchAllInformation(RuntimeInfo::FetchFlags flags); |
| 49 | |
| 50 | private: |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 51 | status_t fetchVersion(); |
| 52 | status_t fetchKernelConfigs(); |
| 53 | status_t fetchCpuInfo(); |
| 54 | status_t fetchKernelSepolicyVers(); |
Yifan Hong | f302930 | 2017-04-12 17:23:49 -0700 | [diff] [blame] | 55 | status_t fetchAvb(); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 56 | status_t parseKernelVersion(); |
| 57 | RuntimeInfo *mRuntimeInfo; |
Yifan Hong | e4959a1 | 2017-07-05 16:05:09 -0700 | [diff] [blame] | 58 | KernelConfigParser mConfigParser; |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | // decompress /proc/config.gz and read its contents. |
| 62 | status_t RuntimeInfoFetcher::fetchKernelConfigs() { |
| 63 | gzFile f = gzopen(PROC_CONFIG, "rb"); |
| 64 | if (f == NULL) { |
| 65 | LOG(ERROR) << "Could not open /proc/config.gz: " << errno; |
| 66 | return -errno; |
| 67 | } |
| 68 | |
| 69 | char buf[BUFFER_SIZE]; |
| 70 | int len; |
| 71 | while ((len = gzread(f, buf, sizeof buf)) > 0) { |
Yifan Hong | e4959a1 | 2017-07-05 16:05:09 -0700 | [diff] [blame] | 72 | mConfigParser.process(buf, len); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 73 | } |
| 74 | status_t err = OK; |
| 75 | if (len < 0) { |
| 76 | int errnum; |
| 77 | const char *errmsg = gzerror(f, &errnum); |
| 78 | LOG(ERROR) << "Could not read /proc/config.gz: " << errmsg; |
| 79 | err = (errnum == Z_ERRNO ? -errno : errnum); |
| 80 | } |
Yifan Hong | e4959a1 | 2017-07-05 16:05:09 -0700 | [diff] [blame] | 81 | mConfigParser.finish(); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 82 | gzclose(f); |
Yifan Hong | e4959a1 | 2017-07-05 16:05:09 -0700 | [diff] [blame] | 83 | mRuntimeInfo->mKernelConfigs = std::move(mConfigParser.configs()); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 84 | return err; |
| 85 | } |
| 86 | |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 87 | status_t RuntimeInfoFetcher::fetchCpuInfo() { |
| 88 | // TODO implement this; 32-bit and 64-bit has different format. |
Yifan Hong | 242eabf | 2017-04-20 14:06:26 -0700 | [diff] [blame] | 89 | std::ifstream in{"/proc/cpuinfo"}; |
| 90 | if (!in.is_open()) { |
| 91 | LOG(WARNING) << "Cannot read /proc/cpuinfo"; |
| 92 | return UNKNOWN_ERROR; |
| 93 | } |
| 94 | std::stringstream sstream; |
| 95 | sstream << in.rdbuf(); |
| 96 | mRuntimeInfo->mCpuInfo = sstream.str(); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 97 | return OK; |
| 98 | } |
| 99 | |
| 100 | status_t RuntimeInfoFetcher::fetchKernelSepolicyVers() { |
| 101 | int pv; |
| 102 | #ifdef LIBVINTF_TARGET |
| 103 | pv = security_policyvers(); |
| 104 | #else |
| 105 | pv = 0; |
| 106 | #endif |
| 107 | if (pv < 0) { |
| 108 | return pv; |
| 109 | } |
| 110 | mRuntimeInfo->mKernelSepolicyVersion = pv; |
| 111 | return OK; |
| 112 | } |
| 113 | |
| 114 | status_t RuntimeInfoFetcher::fetchVersion() { |
| 115 | struct utsname buf; |
| 116 | if (uname(&buf)) { |
| 117 | return -errno; |
| 118 | } |
| 119 | mRuntimeInfo->mOsName = buf.sysname; |
| 120 | mRuntimeInfo->mNodeName = buf.nodename; |
| 121 | mRuntimeInfo->mOsRelease = buf.release; |
| 122 | mRuntimeInfo->mOsVersion = buf.version; |
| 123 | mRuntimeInfo->mHardwareId = buf.machine; |
| 124 | |
| 125 | status_t err = parseKernelVersion(); |
| 126 | if (err != OK) { |
| 127 | LOG(ERROR) << "Could not parse kernel version from \"" |
| 128 | << mRuntimeInfo->mOsRelease << "\""; |
| 129 | } |
| 130 | return err; |
| 131 | } |
| 132 | |
| 133 | status_t RuntimeInfoFetcher::parseKernelVersion() { |
| 134 | auto pos = mRuntimeInfo->mOsRelease.find('.'); |
| 135 | if (pos == std::string::npos) { |
| 136 | return UNKNOWN_ERROR; |
| 137 | } |
| 138 | pos = mRuntimeInfo->mOsRelease.find('.', pos + 1); |
| 139 | if (pos == std::string::npos) { |
| 140 | return UNKNOWN_ERROR; |
| 141 | } |
| 142 | pos = mRuntimeInfo->mOsRelease.find_first_not_of("0123456789", pos + 1); |
| 143 | // no need to check pos == std::string::npos, because substr will handle this |
| 144 | if (!parse(mRuntimeInfo->mOsRelease.substr(0, pos), &mRuntimeInfo->mKernelVersion)) { |
| 145 | return UNKNOWN_ERROR; |
| 146 | } |
| 147 | return OK; |
| 148 | } |
| 149 | |
Yifan Hong | f302930 | 2017-04-12 17:23:49 -0700 | [diff] [blame] | 150 | status_t RuntimeInfoFetcher::fetchAvb() { |
Yifan Hong | 4641979 | 2018-02-28 14:41:38 -0800 | [diff] [blame^] | 151 | std::string prop = android::base::GetProperty("ro.boot.vbmeta.avb_version", "0.0"); |
Yifan Hong | 881a9e45 | 2017-04-27 19:31:13 -0700 | [diff] [blame] | 152 | if (!parse(prop, &mRuntimeInfo->mBootVbmetaAvbVersion)) { |
Yifan Hong | f302930 | 2017-04-12 17:23:49 -0700 | [diff] [blame] | 153 | return UNKNOWN_ERROR; |
| 154 | } |
Yifan Hong | 4641979 | 2018-02-28 14:41:38 -0800 | [diff] [blame^] | 155 | prop = android::base::GetProperty("ro.boot.avb_version", "0.0"); |
Yifan Hong | 881a9e45 | 2017-04-27 19:31:13 -0700 | [diff] [blame] | 156 | if (!parse(prop, &mRuntimeInfo->mBootAvbVersion)) { |
Yifan Hong | f302930 | 2017-04-12 17:23:49 -0700 | [diff] [blame] | 157 | return UNKNOWN_ERROR; |
| 158 | } |
| 159 | return OK; |
| 160 | } |
| 161 | |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 162 | status_t RuntimeInfoFetcher::fetchAllInformation(RuntimeInfo::FetchFlags flags) { |
| 163 | |
| 164 | using F = RuntimeInfo::FetchFlag; |
| 165 | using RF = RuntimeInfoFetcher; |
| 166 | using FetchFunction = status_t(RF::*)(); |
| 167 | const static std::vector<std::tuple<F, FetchFunction, std::string>> gFetchFunctions({ |
| 168 | // flag fetch function description |
| 169 | {F::CPU_VERSION, &RF::fetchVersion, "/proc/version"}, |
| 170 | {F::CONFIG_GZ, &RF::fetchKernelConfigs, "/proc/config.gz"}, |
| 171 | {F::CPU_INFO, &RF::fetchCpuInfo, "/proc/cpuinfo"}, |
| 172 | {F::POLICYVERS, &RF::fetchKernelSepolicyVers, "kernel sepolicy version"}, |
| 173 | {F::AVB, &RF::fetchAvb, "avb version"}, |
| 174 | }); |
| 175 | |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 176 | status_t err; |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 177 | for (const auto& tuple : gFetchFunctions) |
| 178 | if ((flags & std::get<0>(tuple)) && (err = (*this.*std::get<1>(tuple))()) != OK) |
| 179 | LOG(WARNING) << "Cannot fetch or parse " << std::get<2>(tuple) << ": " |
| 180 | << strerror(-err); |
| 181 | |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 182 | return OK; |
| 183 | } |
| 184 | |
Yifan Hong | 1fb004e | 2017-09-26 15:04:44 -0700 | [diff] [blame] | 185 | status_t RuntimeInfo::fetchAllInformation(RuntimeInfo::FetchFlags flags) { |
| 186 | return RuntimeInfoFetcher(this).fetchAllInformation(flags); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | } // namespace vintf |
| 190 | } // namespace android |