summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYifan Hong2017-07-07 15:43:46 -0500
committerandroid-build-merger2017-07-07 15:43:46 -0500
commitfc4bd1daa623784b660e29859985c5910f1d9706 (patch)
tree3a6c16b9bbe27eee785d6d2e1a7a7a4c27f387f0
parent76452a7c6687fc40c8223a8963f76c027f38833c (diff)
parent4217163a5104c8986cae68312eebb57c5ab48075 (diff)
downloadplatform-system-libvintf-fc4bd1daa623784b660e29859985c5910f1d9706.tar.gz
platform-system-libvintf-fc4bd1daa623784b660e29859985c5910f1d9706.tar.xz
platform-system-libvintf-fc4bd1daa623784b660e29859985c5910f1d9706.zip
Use KernelConfigParser to parse /proc/config.gz am: e4959a119a am: edaee52696
am: 4217163a51 Change-Id: I44e20bd1c3d289fbfda924e61944128e950a2439
-rw-r--r--RuntimeInfo-target.cpp63
1 files changed, 5 insertions, 58 deletions
diff --git a/RuntimeInfo-target.cpp b/RuntimeInfo-target.cpp
index 95c0ae0..9518a0c 100644
--- a/RuntimeInfo-target.cpp
+++ b/RuntimeInfo-target.cpp
@@ -21,6 +21,7 @@
21#include "RuntimeInfo.h" 21#include "RuntimeInfo.h"
22 22
23#include "CompatibilityMatrix.h" 23#include "CompatibilityMatrix.h"
24#include "KernelConfigParser.h"
24#include "parse_string.h" 25#include "parse_string.h"
25 26
26#include <dirent.h> 27#include <dirent.h>
@@ -42,27 +43,10 @@
42namespace android { 43namespace android {
43namespace vintf { 44namespace vintf {
44 45
45static void removeTrailingComments(std::string *s) {
46 size_t sharpPos = s->find('#');
47 if (sharpPos != std::string::npos) {
48 s->erase(sharpPos);
49 }
50}
51static void trim(std::string *s) {
52 auto l = s->begin();
53 for (; l != s->end() && std::isspace(*l); ++l);
54 s->erase(s->begin(), l);
55 auto r = s->rbegin();
56 for (; r != s->rend() && std::isspace(*r); ++r);
57 s->erase(r.base(), s->end());
58}
59
60struct RuntimeInfoFetcher { 46struct RuntimeInfoFetcher {
61 RuntimeInfoFetcher(RuntimeInfo *ki) : mRuntimeInfo(ki) { } 47 RuntimeInfoFetcher(RuntimeInfo *ki) : mRuntimeInfo(ki) { }
62 status_t fetchAllInformation(); 48 status_t fetchAllInformation();
63private: 49private:
64 void streamConfig(const char *buf, size_t len);
65 void parseConfig(std::string *s);
66 status_t fetchVersion(); 50 status_t fetchVersion();
67 status_t fetchKernelConfigs(); 51 status_t fetchKernelConfigs();
68 status_t fetchCpuInfo(); 52 status_t fetchCpuInfo();
@@ -70,7 +54,7 @@ private:
70 status_t fetchAvb(); 54 status_t fetchAvb();
71 status_t parseKernelVersion(); 55 status_t parseKernelVersion();
72 RuntimeInfo *mRuntimeInfo; 56 RuntimeInfo *mRuntimeInfo;
73 std::string mRemaining; 57 KernelConfigParser mConfigParser;
74}; 58};
75 59
76// decompress /proc/config.gz and read its contents. 60// decompress /proc/config.gz and read its contents.
@@ -84,7 +68,7 @@ status_t RuntimeInfoFetcher::fetchKernelConfigs() {
84 char buf[BUFFER_SIZE]; 68 char buf[BUFFER_SIZE];
85 int len; 69 int len;
86 while ((len = gzread(f, buf, sizeof buf)) > 0) { 70 while ((len = gzread(f, buf, sizeof buf)) > 0) {
87 streamConfig(buf, len); 71 mConfigParser.process(buf, len);
88 } 72 }
89 status_t err = OK; 73 status_t err = OK;
90 if (len < 0) { 74 if (len < 0) {
@@ -93,49 +77,12 @@ status_t RuntimeInfoFetcher::fetchKernelConfigs() {
93 LOG(ERROR) << "Could not read /proc/config.gz: " << errmsg; 77 LOG(ERROR) << "Could not read /proc/config.gz: " << errmsg;
94 err = (errnum == Z_ERRNO ? -errno : errnum); 78 err = (errnum == Z_ERRNO ? -errno : errnum);
95 } 79 }
96 80 mConfigParser.finish();
97 // stream a "\n" to end the stream to finish the last line.
98 streamConfig("\n", 1 /* sizeof "\n" */);
99
100 gzclose(f); 81 gzclose(f);
82 mRuntimeInfo->mKernelConfigs = std::move(mConfigParser.configs());
101 return err; 83 return err;
102} 84}
103 85
104void RuntimeInfoFetcher::parseConfig(std::string *s) {
105 removeTrailingComments(s);
106 trim(s);
107 if (s->empty()) {
108 return;
109 }
110 size_t equalPos = s->find('=');
111 if (equalPos == std::string::npos) {
112 LOG(WARNING) << "Unrecognized line in /proc/config.gz: " << *s;
113 return;
114 }
115 std::string key = s->substr(0, equalPos);
116 std::string value = s->substr(equalPos + 1);
117 if (!mRuntimeInfo->mKernelConfigs.emplace(std::move(key), std::move(value)).second) {
118 LOG(WARNING) << "Duplicated key in /proc/config.gz: " << s->substr(0, equalPos);
119 return;
120 }
121}
122
123void RuntimeInfoFetcher::streamConfig(const char *buf, size_t len) {
124 const char *begin = buf;
125 const char *end = buf;
126 const char *stop = buf + len;
127 while (end < stop) {
128 if (*end == '\n') {
129 mRemaining.insert(mRemaining.size(), begin, end - begin);
130 parseConfig(&mRemaining);
131 mRemaining.clear();
132 begin = end + 1;
133 }
134 end++;
135 }
136 mRemaining.insert(mRemaining.size(), begin, end - begin);
137}
138
139status_t RuntimeInfoFetcher::fetchCpuInfo() { 86status_t RuntimeInfoFetcher::fetchCpuInfo() {
140 // TODO implement this; 32-bit and 64-bit has different format. 87 // TODO implement this; 32-bit and 64-bit has different format.
141 std::ifstream in{"/proc/cpuinfo"}; 88 std::ifstream in{"/proc/cpuinfo"};