RuntimeInfo-target.cpp 5.7 KB

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