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 | f302930 | 2017-04-12 17:23:49 -0700 | [diff] [blame] | 36 | #include <cutils/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) { } |
| 48 | status_t fetchAllInformation(); |
| 49 | private: |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 50 | status_t fetchVersion(); |
| 51 | status_t fetchKernelConfigs(); |
| 52 | status_t fetchCpuInfo(); |
| 53 | status_t fetchKernelSepolicyVers(); |
Yifan Hong | f302930 | 2017-04-12 17:23:49 -0700 | [diff] [blame] | 54 | status_t fetchAvb(); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 55 | status_t parseKernelVersion(); |
| 56 | RuntimeInfo *mRuntimeInfo; |
Yifan Hong | e4959a1 | 2017-07-05 16:05:09 -0700 | [diff] [blame^] | 57 | KernelConfigParser mConfigParser; |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | // decompress /proc/config.gz and read its contents. |
| 61 | status_t RuntimeInfoFetcher::fetchKernelConfigs() { |
| 62 | gzFile f = gzopen(PROC_CONFIG, "rb"); |
| 63 | if (f == NULL) { |
| 64 | LOG(ERROR) << "Could not open /proc/config.gz: " << errno; |
| 65 | return -errno; |
| 66 | } |
| 67 | |
| 68 | char buf[BUFFER_SIZE]; |
| 69 | int len; |
| 70 | while ((len = gzread(f, buf, sizeof buf)) > 0) { |
Yifan Hong | e4959a1 | 2017-07-05 16:05:09 -0700 | [diff] [blame^] | 71 | mConfigParser.process(buf, len); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 72 | } |
| 73 | status_t err = OK; |
| 74 | if (len < 0) { |
| 75 | int errnum; |
| 76 | const char *errmsg = gzerror(f, &errnum); |
| 77 | LOG(ERROR) << "Could not read /proc/config.gz: " << errmsg; |
| 78 | err = (errnum == Z_ERRNO ? -errno : errnum); |
| 79 | } |
Yifan Hong | e4959a1 | 2017-07-05 16:05:09 -0700 | [diff] [blame^] | 80 | mConfigParser.finish(); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 81 | gzclose(f); |
Yifan Hong | e4959a1 | 2017-07-05 16:05:09 -0700 | [diff] [blame^] | 82 | mRuntimeInfo->mKernelConfigs = std::move(mConfigParser.configs()); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 83 | return err; |
| 84 | } |
| 85 | |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 86 | status_t RuntimeInfoFetcher::fetchCpuInfo() { |
| 87 | // TODO implement this; 32-bit and 64-bit has different format. |
Yifan Hong | 242eabf | 2017-04-20 14:06:26 -0700 | [diff] [blame] | 88 | std::ifstream in{"/proc/cpuinfo"}; |
| 89 | if (!in.is_open()) { |
| 90 | LOG(WARNING) << "Cannot read /proc/cpuinfo"; |
| 91 | return UNKNOWN_ERROR; |
| 92 | } |
| 93 | std::stringstream sstream; |
| 94 | sstream << in.rdbuf(); |
| 95 | mRuntimeInfo->mCpuInfo = sstream.str(); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 96 | return OK; |
| 97 | } |
| 98 | |
| 99 | status_t RuntimeInfoFetcher::fetchKernelSepolicyVers() { |
| 100 | int pv; |
| 101 | #ifdef LIBVINTF_TARGET |
| 102 | pv = security_policyvers(); |
| 103 | #else |
| 104 | pv = 0; |
| 105 | #endif |
| 106 | if (pv < 0) { |
| 107 | return pv; |
| 108 | } |
| 109 | mRuntimeInfo->mKernelSepolicyVersion = pv; |
| 110 | return OK; |
| 111 | } |
| 112 | |
| 113 | status_t RuntimeInfoFetcher::fetchVersion() { |
| 114 | struct utsname buf; |
| 115 | if (uname(&buf)) { |
| 116 | return -errno; |
| 117 | } |
| 118 | mRuntimeInfo->mOsName = buf.sysname; |
| 119 | mRuntimeInfo->mNodeName = buf.nodename; |
| 120 | mRuntimeInfo->mOsRelease = buf.release; |
| 121 | mRuntimeInfo->mOsVersion = buf.version; |
| 122 | mRuntimeInfo->mHardwareId = buf.machine; |
| 123 | |
| 124 | status_t err = parseKernelVersion(); |
| 125 | if (err != OK) { |
| 126 | LOG(ERROR) << "Could not parse kernel version from \"" |
| 127 | << mRuntimeInfo->mOsRelease << "\""; |
| 128 | } |
| 129 | return err; |
| 130 | } |
| 131 | |
| 132 | status_t RuntimeInfoFetcher::parseKernelVersion() { |
| 133 | auto pos = mRuntimeInfo->mOsRelease.find('.'); |
| 134 | if (pos == std::string::npos) { |
| 135 | return UNKNOWN_ERROR; |
| 136 | } |
| 137 | pos = mRuntimeInfo->mOsRelease.find('.', pos + 1); |
| 138 | if (pos == std::string::npos) { |
| 139 | return UNKNOWN_ERROR; |
| 140 | } |
| 141 | pos = mRuntimeInfo->mOsRelease.find_first_not_of("0123456789", pos + 1); |
| 142 | // no need to check pos == std::string::npos, because substr will handle this |
| 143 | if (!parse(mRuntimeInfo->mOsRelease.substr(0, pos), &mRuntimeInfo->mKernelVersion)) { |
| 144 | return UNKNOWN_ERROR; |
| 145 | } |
| 146 | return OK; |
| 147 | } |
| 148 | |
Yifan Hong | f302930 | 2017-04-12 17:23:49 -0700 | [diff] [blame] | 149 | status_t RuntimeInfoFetcher::fetchAvb() { |
| 150 | char prop[PROPERTY_VALUE_MAX]; |
| 151 | property_get("ro.boot.vbmeta.avb_version", prop, "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 | } |
| 155 | property_get("ro.boot.avb_version", prop, "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 | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 162 | status_t RuntimeInfoFetcher::fetchAllInformation() { |
| 163 | status_t err; |
| 164 | if ((err = fetchVersion()) != OK) { |
Yifan Hong | 3477f04 | 2017-05-08 14:02:22 -0700 | [diff] [blame] | 165 | LOG(WARNING) << "Cannot fetch or parse /proc/version: " << strerror(-err); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 166 | } |
| 167 | if ((err = fetchKernelConfigs()) != OK) { |
Yifan Hong | 3477f04 | 2017-05-08 14:02:22 -0700 | [diff] [blame] | 168 | LOG(WARNING) << "Cannot fetch or parse /proc/config.gz: " << strerror(-err); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 169 | } |
| 170 | if ((err = fetchCpuInfo()) != OK) { |
Yifan Hong | 3477f04 | 2017-05-08 14:02:22 -0700 | [diff] [blame] | 171 | LOG(WARNING) << "Cannot fetch /proc/cpuinfo: " << strerror(-err); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 172 | } |
| 173 | if ((err = fetchKernelSepolicyVers()) != OK) { |
Yifan Hong | 3477f04 | 2017-05-08 14:02:22 -0700 | [diff] [blame] | 174 | LOG(WARNING) << "Cannot fetch kernel sepolicy version: " << strerror(-err); |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 175 | } |
Yifan Hong | f302930 | 2017-04-12 17:23:49 -0700 | [diff] [blame] | 176 | if ((err = fetchAvb()) != OK) { |
Yifan Hong | 3477f04 | 2017-05-08 14:02:22 -0700 | [diff] [blame] | 177 | LOG(WARNING) << "Cannot fetch sepolicy avb version: " << strerror(-err); |
Yifan Hong | f302930 | 2017-04-12 17:23:49 -0700 | [diff] [blame] | 178 | } |
Yifan Hong | 25c1eed | 2017-04-07 13:50:24 -0700 | [diff] [blame] | 179 | return OK; |
| 180 | } |
| 181 | |
| 182 | status_t RuntimeInfo::fetchAllInformation() { |
| 183 | return RuntimeInfoFetcher(this).fetchAllInformation(); |
| 184 | } |
| 185 | |
| 186 | } // namespace vintf |
| 187 | } // namespace android |