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