1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "incident_helper"
#include <android/util/ProtoOutputStream.h>
#include "frameworks/base/core/proto/android/os/system_properties.proto.h"
#include "ih_util.h"
#include "SystemPropertiesParser.h"
using namespace android::os;
const string LINE_DELIMITER = "]: [";
// system properties' names sometimes are not valid proto field names, make the names valid.
static string convertToFieldName(const string& name) {
int len = (int)name.length();
char cstr[len + 1];
strcpy(cstr, name.c_str());
for (int i = 0; i < len; i++) {
if (!isValidChar(cstr[i])) {
cstr[i] = '_';
}
}
return string(cstr);
}
status_t
SystemPropertiesParser::Parse(const int in, const int out) const
{
Reader reader(in);
string line;
string name; // the name of the property
string value; // the string value of the property
ProtoOutputStream proto;
vector<pair<string, string>> extras;
Table sysPropTable(SystemPropertiesProto::_FIELD_NAMES,
SystemPropertiesProto::_FIELD_IDS,
SystemPropertiesProto::_FIELD_COUNT);
Message sysProp(&sysPropTable);
Table aacDrcTable(SystemPropertiesProto::AacDrc::_FIELD_NAMES,
SystemPropertiesProto::AacDrc::_FIELD_IDS,
SystemPropertiesProto::AacDrc::_FIELD_COUNT);
Message aacDrc(&aacDrcTable);
sysProp.addSubMessage(SystemPropertiesProto::AAC_DRC, &aacDrc);
Table aaudioTable(SystemPropertiesProto::Aaudio::_FIELD_NAMES,
SystemPropertiesProto::Aaudio::_FIELD_IDS,
SystemPropertiesProto::Aaudio::_FIELD_COUNT);
Message aaudio(&aaudioTable);
sysProp.addSubMessage(SystemPropertiesProto::AAUDIO, &aaudio);
Table cameraTable(SystemPropertiesProto::Camera::_FIELD_NAMES,
SystemPropertiesProto::Camera::_FIELD_IDS,
SystemPropertiesProto::Camera::_FIELD_COUNT);
Message camera(&cameraTable);
sysProp.addSubMessage(SystemPropertiesProto::CAMERA, &camera);
Table dalvikVmTable(SystemPropertiesProto::DalvikVm::_FIELD_NAMES,
SystemPropertiesProto::DalvikVm::_FIELD_IDS,
SystemPropertiesProto::DalvikVm::_FIELD_COUNT);
Message dalvikVm(&dalvikVmTable);
sysProp.addSubMessage(SystemPropertiesProto::DALVIK_VM, &dalvikVm);
Table initSvcTable(SystemPropertiesProto::InitSvc::_FIELD_NAMES,
SystemPropertiesProto::InitSvc::_FIELD_IDS,
SystemPropertiesProto::InitSvc::_FIELD_COUNT);
initSvcTable.addEnumNameToValue("running", SystemPropertiesProto::InitSvc::STATUS_RUNNING);
initSvcTable.addEnumNameToValue("stopped", SystemPropertiesProto::InitSvc::STATUS_STOPPED);
Message initSvc(&initSvcTable);
sysProp.addSubMessage(SystemPropertiesProto::INIT_SVC, &initSvc);
Table logTable(SystemPropertiesProto::Log::_FIELD_NAMES,
SystemPropertiesProto::Log::_FIELD_IDS,
SystemPropertiesProto::Log::_FIELD_COUNT);
Message logMsg(&logTable);
sysProp.addSubMessage(SystemPropertiesProto::LOG, &logMsg);
Table persistTable(SystemPropertiesProto::Persist::_FIELD_NAMES,
SystemPropertiesProto::Persist::_FIELD_IDS,
SystemPropertiesProto::Persist::_FIELD_COUNT);
Message persist(&persistTable);
sysProp.addSubMessage(SystemPropertiesProto::PERSIST, &persist);
Table pmDexoptTable(SystemPropertiesProto::PmDexopt::_FIELD_NAMES,
SystemPropertiesProto::PmDexopt::_FIELD_IDS,
SystemPropertiesProto::PmDexopt::_FIELD_COUNT);
Message pmDexopt(&pmDexoptTable);
sysProp.addSubMessage(SystemPropertiesProto::PM_DEXOPT, &pmDexopt);
Table roTable(SystemPropertiesProto::Ro::_FIELD_NAMES,
SystemPropertiesProto::Ro::_FIELD_IDS,
SystemPropertiesProto::Ro::_FIELD_COUNT);
Message ro(&roTable);
Table bootTable(SystemPropertiesProto::Ro::Boot::_FIELD_NAMES,
SystemPropertiesProto::Ro::Boot::_FIELD_IDS,
SystemPropertiesProto::Ro::Boot::_FIELD_COUNT);
Message boot(&bootTable);
ro.addSubMessage(SystemPropertiesProto::Ro::BOOT, &boot);
Table bootimageTable(SystemPropertiesProto::Ro::BootImage::_FIELD_NAMES,
SystemPropertiesProto::Ro::BootImage::_FIELD_IDS,
SystemPropertiesProto::Ro::BootImage::_FIELD_COUNT);
Message bootimage(&bootimageTable);
ro.addSubMessage(SystemPropertiesProto::Ro::BOOTIMAGE, &bootimage);
Table buildTable(SystemPropertiesProto::Ro::Build::_FIELD_NAMES,
SystemPropertiesProto::Ro::Build::_FIELD_IDS,
SystemPropertiesProto::Ro::Build::_FIELD_COUNT);
Message build(&buildTable);
Table versionTable(SystemPropertiesProto::Ro::Build::Version::_FIELD_NAMES,
SystemPropertiesProto::Ro::Build::Version::_FIELD_IDS,
SystemPropertiesProto::Ro::Build::Version::_FIELD_COUNT);
Message version(&versionTable);
build.addSubMessage(SystemPropertiesProto::Ro::Build::VERSION, &version);
ro.addSubMessage(SystemPropertiesProto::Ro::BUILD, &build);
Table configTable(SystemPropertiesProto::Ro::Config::_FIELD_NAMES,
SystemPropertiesProto::Ro::Config::_FIELD_IDS,
SystemPropertiesProto::Ro::Config::_FIELD_COUNT);
Message config(&configTable);
ro.addSubMessage(SystemPropertiesProto::Ro::CONFIG, &config);
Table hardwareTable(SystemPropertiesProto::Ro::Hardware::_FIELD_NAMES,
SystemPropertiesProto::Ro::Hardware::_FIELD_IDS,
SystemPropertiesProto::Ro::Hardware::_FIELD_COUNT);
Message hardware(&hardwareTable);
ro.addSubMessage(SystemPropertiesProto::Ro::HARDWARE, &hardware);
Table productTable(SystemPropertiesProto::Ro::Product::_FIELD_NAMES,
SystemPropertiesProto::Ro::Product::_FIELD_IDS,
SystemPropertiesProto::Ro::Product::_FIELD_COUNT);
Message product(&productTable);
Table pVendorTable(SystemPropertiesProto::Ro::Product::Vendor::_FIELD_NAMES,
SystemPropertiesProto::Ro::Product::Vendor::_FIELD_IDS,
SystemPropertiesProto::Ro::Product::Vendor::_FIELD_COUNT);
Message pVendor(&pVendorTable);
product.addSubMessage(SystemPropertiesProto::Ro::Product::VENDOR, &pVendor);
ro.addSubMessage(SystemPropertiesProto::Ro::PRODUCT, &product);
Table telephonyTable(SystemPropertiesProto::Ro::Telephony::_FIELD_NAMES,
SystemPropertiesProto::Ro::Telephony::_FIELD_IDS,
SystemPropertiesProto::Ro::Telephony::_FIELD_COUNT);
Message telephony(&telephonyTable);
ro.addSubMessage(SystemPropertiesProto::Ro::TELEPHONY, &telephony);
Table vendorTable(SystemPropertiesProto::Ro::Vendor::_FIELD_NAMES,
SystemPropertiesProto::Ro::Vendor::_FIELD_IDS,
SystemPropertiesProto::Ro::Vendor::_FIELD_COUNT);
Message vendor(&vendorTable);
ro.addSubMessage(SystemPropertiesProto::Ro::VENDOR, &vendor);
sysProp.addSubMessage(SystemPropertiesProto::RO, &ro);
Table sysTable(SystemPropertiesProto::Sys::_FIELD_NAMES,
SystemPropertiesProto::Sys::_FIELD_IDS,
SystemPropertiesProto::Sys::_FIELD_COUNT);
Message sys(&sysTable);
Table usbTable(SystemPropertiesProto::Sys::Usb::_FIELD_NAMES,
SystemPropertiesProto::Sys::Usb::_FIELD_IDS,
SystemPropertiesProto::Sys::Usb::_FIELD_COUNT);
Message usb(&usbTable);
sys.addSubMessage(SystemPropertiesProto::Sys::USB, &usb);
sysProp.addSubMessage(SystemPropertiesProto::SYS, &sys);
// parse line by line
while (reader.readLine(&line)) {
if (line.empty()) continue;
line = line.substr(1, line.size() - 2); // trim []
size_t index = line.find(LINE_DELIMITER); // split by "]: ["
if (index == string::npos) {
fprintf(stderr, "Bad Line %s\n", line.c_str());
continue;
}
name = line.substr(0, index);
value = trim(line.substr(index + 4), DEFAULT_WHITESPACE);
if (value.empty()) continue;
// if the property name couldn't be found in proto definition or the value has mistype,
// add to extra properties with its name and value
if (!sysProp.insertField(&proto, convertToFieldName(name), value)) {
extras.push_back(make_pair(name, value));
}
}
// end session for the last write.
sysProp.endSession(&proto);
for (auto it = extras.begin(); it != extras.end(); it++) {
long long token = proto.start(SystemPropertiesProto::EXTRA_PROPERTIES);
proto.write(SystemPropertiesProto::Property::NAME, it->first);
proto.write(SystemPropertiesProto::Property::VALUE, it->second);
proto.end(token);
}
if (!reader.ok(&line)) {
fprintf(stderr, "Bad read from fd %d: %s\n", in, line.c_str());
return -1;
}
if (!proto.flush(out)) {
fprintf(stderr, "[%s]Error writing proto back\n", this->name.string());
return -1;
}
fprintf(stderr, "[%s]Proto size: %zu bytes\n", this->name.string(), proto.size());
return NO_ERROR;
}
|