-
Notifications
You must be signed in to change notification settings - Fork 856
Expand file tree
/
Copy pathRecConfigParse.cc
More file actions
285 lines (244 loc) · 8.48 KB
/
RecConfigParse.cc
File metadata and controls
285 lines (244 loc) · 8.48 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/** @file
Parse the records.config configuration file.
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
*/
#include "tscore/ink_platform.h"
#include "tscore/ink_memory.h"
#include "tscore/TextBuffer.h"
#include "tscore/Tokenizer.h"
#include "tscore/ink_defs.h"
#include "tscore/ink_string.h"
#include "tscore/runroot.h"
#include "P_RecFile.h"
#include "P_RecUtils.h"
#include "P_RecMessage.h"
#include "P_RecCore.h"
#include "tscore/Layout.h"
#include "records/RecYAMLDefs.h"
#include "records/RecYAMLDecoder.h"
const char *g_rec_config_fpath = nullptr;
std::unordered_set<std::string> g_rec_config_contents_ht;
ink_mutex g_rec_config_lock;
//-------------------------------------------------------------------------
// RecConfigFileInit
//-------------------------------------------------------------------------
void
RecConfigFileInit()
{
ink_mutex_init(&g_rec_config_lock);
}
//-------------------------------------------------------------------------
// RecFileImport_Xmalloc
//-------------------------------------------------------------------------
static int
RecFileImport_Xmalloc(const char *file, char **file_buf, int *file_size)
{
int err = REC_ERR_FAIL;
RecHandle h_file;
int bytes_read;
if (file && file_buf && file_size) {
*file_buf = nullptr;
*file_size = 0;
if ((h_file = RecFileOpenR(file)) != REC_HANDLE_INVALID) {
*file_size = RecFileGetSize(h_file);
*file_buf = static_cast<char *>(ats_malloc(*file_size + 1));
if (RecFileRead(h_file, *file_buf, *file_size, &bytes_read) != REC_ERR_FAIL && bytes_read == *file_size) {
(*file_buf)[*file_size] = '\0';
err = REC_ERR_OKAY;
} else {
ats_free(*file_buf);
*file_buf = nullptr;
*file_size = 0;
}
RecFileClose(h_file);
}
}
return err;
}
//-------------------------------------------------------------------------
// RecConfigOverrideFromRunroot
//-------------------------------------------------------------------------
bool
RecConfigOverrideFromRunroot(const char *name)
{
if (!get_runroot().empty()) {
if (!strcmp(name, "proxy.config.bin_path") || !strcmp(name, "proxy.config.local_state_dir") ||
!strcmp(name, "proxy.config.log.logfile_dir") || !strcmp(name, "proxy.config.plugin.plugin_dir")) {
return true;
}
}
return false;
}
//-------------------------------------------------------------------------
// RecConfigOverrideFromEnvironment
//-------------------------------------------------------------------------
const char *
RecConfigOverrideFromEnvironment(const char *name, const char *value)
{
ats_scoped_str envname(ats_strdup(name));
const char *envval = nullptr;
// Munge foo.bar.config into FOO_BAR_CONFIG.
for (char *c = envname; *c != '\0'; ++c) {
switch (*c) {
case '.':
*c = '_';
break;
default:
*c = ParseRules::ink_toupper(*c);
break;
}
}
envval = getenv(envname.get());
if (envval) {
return envval;
} else if (RecConfigOverrideFromRunroot(name)) {
return value;
}
return value;
}
//-------------------------------------------------------------------------
// RecParseConfigFile
//-------------------------------------------------------------------------
int
RecConfigFileParse(const char *path, RecConfigEntryCallback handler)
{
char *fbuf;
int fsize;
const char *line;
int line_num;
char *rec_type_str, *name_str, *data_type_str, *data_str;
const char *value_str;
RecT rec_type;
RecDataT data_type;
Tokenizer line_tok("\r\n");
tok_iter_state line_tok_state;
RecDebug(DL_Note, "Reading '%s'", path);
// watch out, we're altering our g_rec_config_xxx structures
ink_mutex_acquire(&g_rec_config_lock);
if (RecFileImport_Xmalloc(path, &fbuf, &fsize) == REC_ERR_FAIL) {
RecLog(DL_Warning, "Could not import '%s'", path);
ink_mutex_release(&g_rec_config_lock);
return REC_ERR_FAIL;
}
line_tok.Initialize(fbuf, SHARE_TOKS | ALLOW_EMPTY_TOKS);
line = line_tok.iterFirst(&line_tok_state);
line_num = 1;
while (line) {
char *lc = ats_strdup(line);
char *lt = lc;
char *ln;
while (isspace(*lt)) {
lt++;
}
rec_type_str = strtok_r(lt, " \t", &ln);
// check for blank lines and comments
if ((!rec_type_str) || (rec_type_str && (*rec_type_str == '#'))) {
goto L_done;
}
name_str = strtok_r(nullptr, " \t", &ln);
data_type_str = strtok_r(nullptr, " \t", &ln);
// extract the string data (a little bit tricker since it can have spaces)
if (ln) {
// 'ln' will point to either the next token or a bunch of spaces
// if the user didn't supply a value (e.g. 'STRING '). First
// scan past all of the spaces. If we hit a '\0', then we
// know we didn't have a valid value. If not, set 'data_str' to
// the start of the token and scan until we find the end. Once
// the end is found, back-peddle to remove any trailing spaces.
while (isspace(*ln)) {
ln++;
}
if (*ln == '\0') {
data_str = nullptr;
} else {
data_str = ln;
while (*ln != '\0') {
ln++;
}
ln--;
while (isspace(*ln) && (ln > data_str)) {
ln--;
}
ln++;
*ln = '\0';
}
} else {
data_str = nullptr;
}
// check for errors
if (!(rec_type_str && name_str && data_type_str && data_str)) {
RecLog(DL_Warning, "Could not parse line at '%s:%d' -- skipping line: '%s'", path, line_num, line);
goto L_done;
}
// record type
if (strcmp(rec_type_str, "CONFIG") == 0) {
rec_type = RECT_CONFIG;
} else if (strcmp(rec_type_str, "PROCESS") == 0) {
rec_type = RECT_PROCESS;
} else if (strcmp(rec_type_str, "NODE") == 0) {
rec_type = RECT_NODE;
} else if (strcmp(rec_type_str, "LOCAL") == 0) {
rec_type = RECT_LOCAL;
} else {
RecLog(DL_Warning, "Unknown record type '%s' at '%s:%d' -- skipping line", rec_type_str, path, line_num);
goto L_done;
}
// data_type
if (strcmp(data_type_str, "INT") == 0) {
data_type = RECD_INT;
} else if (strcmp(data_type_str, "FLOAT") == 0) {
data_type = RECD_FLOAT;
} else if (strcmp(data_type_str, "STRING") == 0) {
data_type = RECD_STRING;
} else if (strcmp(data_type_str, "COUNTER") == 0) {
data_type = RECD_COUNTER;
} else {
RecLog(DL_Warning, "Unknown data type '%s' at '%s:%d' -- skipping line", data_type_str, path, line_num);
goto L_done;
}
// OK, we parsed the record, send it to the handler ...
value_str = RecConfigOverrideFromEnvironment(name_str, data_str);
handler(rec_type, data_type, name_str, value_str, value_str == data_str ? REC_SOURCE_EXPLICIT : REC_SOURCE_ENV);
// update our g_rec_config_contents_xxx
g_rec_config_contents_ht.emplace(name_str);
L_done:
line = line_tok.iterNext(&line_tok_state);
line_num++;
ats_free(lc);
}
ink_mutex_release(&g_rec_config_lock);
ats_free(fbuf);
return REC_ERR_OKAY;
}
swoc::Errata
RecYAMLConfigFileParse(const char *path, RecYAMLNodeHandler handler)
{
swoc::Errata status;
try {
auto nodes = YAML::LoadAllFromFile(path);
// We will parse each doc from the stream, subsequently config node will overwrite
// the value for previously loaded records. This would work similar to the old
// records.config which a later CONFIG variable would overwrite a previous one.
for (auto const &root : nodes) {
if (auto &&ret = ParseRecordsFromYAML(root, handler); !ret.empty()) {
status.note(ret);
}
}
} catch (std::exception const &ex) {
status.note(ERRATA_ERROR, "Error parsing {}. {}", path, ex.what());
}
return status;
}