forked from open-iscsi/tcmu-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibtcmu_config.c
346 lines (294 loc) · 7.26 KB
/
libtcmu_config.c
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* Copyright 2016-2017 China Mobile, Inc.
*
* 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 _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <stdbool.h>
#include "libtcmu_config.h"
#include "libtcmu_log.h"
/*
* System config for TCMU, for now there are only 3 option types supported:
* 1, The "int type" option, for example:
* log_level = 2
*
* 2, The "string type" option, for example:
* tcmu_str = "Tom" --> Tom
* or
* tcmu_str = 'Tom' --> Tom
* or
* tcmu_str = 'Tom is a "boy"' ---> Tom is a "boy"
* or
* tcmu_str = "'T' is short for Tom" --> 'T' is short for Tom
*
* 3, The "boolean type" option, for example:
* tcmu_bool
*
* ========================
* How to add new options ?
*
* Using "log_level" as an example:
* Firstly, add log_level member in:
* struct tcmu_config {
* int log_level;
* };
*
* Secondly, add the following in "tcmu_conf_option tcmu_options[] = {}":
* {
* .key = "log_level", // from struct tcmu_config {}
* .type = TCMU_OPT_INT, // int type
* {
* .opt_int = 2, // default value is 2
* },
* },
*
* Thirdly, add the following option in "tcmu.conf" file as default value,
* and this default value will overwrite the value in '.opt_int' above:
* log_level = 2
*
* Then the new options could be configured through /etc/tcmu/tcmu.conf.
*
* The last, you should add your own set method in:
* static void tcmu_conf_set_options(struct tcmu_config *cfg)
* {
* TCMU_PARSE_CFG_INT(cfg, log_level);
* TCMU_CONF_CHECK_LOG_LEVEL(log_level);
* }
*
* Note: For now, if the options have been changed in config file, the
* tcmu-runner, consumer and tcmu-synthesizer daemons should be restarted.
* And the dynamic reloading feature will be added later.
*/
static struct tcmu_conf_option tcmu_options[] = {
{
.key = "log_level",
.type = TCMU_OPT_INT,
{
.opt_int = 2,
},
},
/* Add new options here */
{
.key = NULL,
.type = TCMU_OPT_NONE,
{},
},
};
static int tcmu_get_option_index(const char *key)
{
int i = 0;
while (tcmu_options[i].key != NULL) {
if (!strcmp(tcmu_options[i].key, key))
return i;
i++;
}
return -1;
}
#define TCMU_PARSE_CFG_INT(cfg, key) \
do { \
int ind = tcmu_get_option_index(#key); \
if (ind >= 0) { \
cfg->key = tcmu_options[ind].opt_int; \
} \
} while (0)
#define TCMU_PARSE_CFG_BOOL(cfg, key) \
do { \
int ind = tcmu_get_option_index(#key); \
if (ind >= 0) { \
cfg->key = tcmu_options[ind].opt_bool; \
} \
} while (0)
#define TCMU_PARSE_CFG_STR(cfg, key) \
do { \
int ind = tcmu_get_option_index(#key); \
if (ind >= 0) { \
cfg->key = strdup(tcmu_options[ind].opt_str); } \
} while (0);
#define TCMU_FREE_CFG_STR(cfg, key) \
do { \
cfg->key = NULL; \
free(tcmu_options[i]->opt_str); \
} while (0);
#define TCMU_CONF_CHECK_LOG_LEVEL(key) \
do { \
int ind = tcmu_get_option_index(#key); \
if (tcmu_options[ind].opt_int > TCMU_CONF_LOG_LEVEL_MAX) { \
tcmu_options[ind].opt_int = TCMU_CONF_LOG_LEVEL_MAX; \
} else if (tcmu_options[ind].opt_int < TCMU_CONF_LOG_LEVEL_MIN) { \
tcmu_options[ind].opt_int = TCMU_CONF_LOG_LEVEL_MIN; \
} \
} while (0);
static void tcmu_conf_set_options(struct tcmu_config *cfg)
{
/* set log_level option */
TCMU_PARSE_CFG_INT(cfg, log_level);
TCMU_CONF_CHECK_LOG_LEVEL(log_level);
/* add your new config options */
}
struct tcmu_config *tcmu_config_new(void)
{
struct tcmu_config *cfg;
cfg = calloc(1, sizeof(*cfg));
if (cfg == NULL) {
tcmu_err("Alloc TCMU config failed!\n");
return NULL;
}
return cfg;
}
void tcmu_config_destroy(struct tcmu_config *cfg)
{
/* TCMU_FREE_CFG_STR(cfg, "__STR__"); */
free(cfg);
}
#define TCMU_MAX_CFG_FILE_SIZE (2 * 1024 * 1024)
static int tcmu_read_config(int fd, char *buf, int count)
{
ssize_t len;
int save = errno;
do {
len = read(fd, buf, count);
} while (errno == EAGAIN);
errno = save;
return len;
}
/* end of line */
#define __EOL(c) (((c) == '\n') || ((c) == '\r'))
#define TCMU_TO_LINE_END(x, y) \
do { while ((x) < (y) && !__EOL(*(x))) { \
(x)++; } \
} while (0);
/* skip blank lines */
#define TCMU_SKIP_BLANK_LINES(x, y) \
do { while ((x) < (y) && (isblank(*(x)) || __EOL(*(x)))) { \
(x)++; } \
} while (0);
/* skip comment line with '#' */
#define TCMU_SKIP_COMMENT_LINE(x, y) \
do { while ((x) < (y) && !__EOL(*x)) { \
(x)++; } \
(x)++; \
} while (0);
/* skip comment lines with '#' */
#define TCMU_SKIP_COMMENT_LINES(x, y) \
do { while ((x) < (y) && *(x) == '#') { \
TCMU_SKIP_COMMENT_LINE((x), (y)); } \
} while (0);
#define MAX_KEY_LEN 64
#define MAX_VAL_STR_LEN 256
static void tcmu_parse_option(char **cur, const char *end)
{
char *p = *cur, *q = *cur, *r, *s;
int ind;
while (isblank(*p))
p++;
TCMU_TO_LINE_END(q, end);
*q = '\0';
*cur = q + 1;
/* parse the boolean type option */
s = r = strchr(p, '=');
if (!r) {
/* boolean type option at file end or line end */
r = p;
while (!isblank(*r) && r < q)
r++;
*r = '\0';
ind = tcmu_get_option_index(p);
if (ind < 0)
return;
tcmu_options[ind].opt_bool = true;
return;
}
while (isblank(*r) || *r == '=')
r--;
r++;
*r = '\0';
ind = tcmu_get_option_index(p);
if (ind < 0)
return;
/* parse the int/string type options */
switch (tcmu_options[ind].type) {
case TCMU_OPT_INT:
while (!isdigit(*s))
s++;
r = s;
while (isdigit(*r))
r++;
*r= '\0';
tcmu_options[ind].opt_int = atoi(s);
break;
case TCMU_OPT_STR:
s++;
while (isblank(*s))
s++;
/* skip first " or ' if exist */
if (*s == '"' || *s == '\'')
s++;
r = q - 1;
while (isblank(*r))
r--;
/* skip last " or ' if exist */
if (*r == '"' || *r == '\'')
*r = '\0';
tcmu_options[ind].opt_str = strdup(s);
break;
default:
break;
}
}
static void tcmu_parse_options(struct tcmu_config *cfg, char *buf, int len)
{
char *cur = buf, *end = buf + len;
while (cur < end) {
/* skip blanks lines */
TCMU_SKIP_BLANK_LINES(cur, end);
/* skip comments with '#' */
TCMU_SKIP_COMMENT_LINES(cur, end);
if (cur >= end)
break;
if (!isalpha(*cur))
continue;
/* parse the options from config file to tcmu_options[] */
tcmu_parse_option(&cur, end);
}
/* parse the options from tcmu_options[] to struct tcmu_config */
tcmu_conf_set_options(cfg);
}
int tcmu_load_config(struct tcmu_config *cfg, const char *path)
{
char buf[TCMU_MAX_CFG_FILE_SIZE];
int fd, len;
if (!path)
path = "/etc/tcmu/tcmu.conf"; /* the default config file */
fd = open(path, O_RDONLY);
if (fd < 0) {
tcmu_err("Failed to open file '%s'\n", path);
return -1;
}
len = tcmu_read_config(fd, buf, TCMU_MAX_CFG_FILE_SIZE);
if (len < 0) {
tcmu_err("Failed to read file '%s'\n", path);
return -1;
}
close(fd);
buf[len] = '\0';
tcmu_parse_options(cfg, buf, len);
return 0;
}