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 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
/* Copyright (C) 2009 Intel Corporation
Simple config file parser
mcelog is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; version
2.
mcelog is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should find a copy of v2 of the GNU General Public License somewhere
on your Linux system; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Author: Andi Kleen
*/
#define _GNU_SOURCE 1
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include "memutil.h"
#include "mcelog.h"
#include "config.h"
#include "leaky-bucket.h"
#include "trigger.h"
#ifdef TEST
#define Eprintf printf
#define Wprintf printf
#define xalloc(x) calloc(x,1)
#endif
/* ISSUES:
doesn't detect misspelled options (this would require a major revamp!)
doesn't merge/detect duplicated headers */
#define SHASH 11
struct opt {
struct opt *next;
char *name;
char *val;
};
struct header {
struct header *next;
char *name;
struct opt *opts[SHASH];
struct opt *optslast[SHASH];
};
static struct header *hlist;
/* djb hash */
static unsigned hash(const char *str)
{
const unsigned char *s;
unsigned hash = 5381;
for (s = (const unsigned char *)str; *s; s++)
hash = (hash * 32) + hash + *s;
return hash % SHASH;
}
static struct header *new_header(struct header *prevh, char *name)
{
struct header *h = xalloc(sizeof(struct header));
h->name = xstrdup(name);
if (prevh)
prevh->next = h;
else
hlist = h;
return h;
}
static int empty(char *s)
{
while (isspace(*s))
++s;
return *s == 0;
}
static void noreturn parse_error(int line, char *msg)
{
Eprintf("config file line %d: %s\n", line, msg);
exit(1);
}
static void nothing(char *s, int line)
{
if (!empty(s) != 0)
parse_error(line, "left over characters at end of line");
}
static void unparseable(char *desc, const char *header, const char *name)
{
char *field;
if (!strcmp(header, "global")) {
asprintf(&field, "%s", name);
} else {
asprintf(&field, "[%s] %s", header, name);
}
Eprintf("%s config option `%s' unparseable\n", desc, field);
free(field);
exit(1);
}
/* Remove leading/trailing white space */
static char *strstrip(char *s)
{
char *p;
while (isspace(*s))
s++;
p = s + strlen(s) - 1;
if (p <= s)
return s;
while (isspace(*p) && p >= s)
*p-- = 0;
return s;
}
int parse_config_file(const char *fn)
{
FILE *f;
char *line = NULL;
size_t linelen = 0;
char *name;
char *val;
struct opt *opt;
struct header *hdr;
int lineno = 1;
unsigned h;
f = fopen(fn, "r");
if (!f)
return -1;
hdr = NULL;
while (getline(&line, &linelen, f) > 0) {
char *s = strchr(line, '#');
if (s)
*s = 0;
s = strstrip(line);
if (*s == '[') {
char *p = strchr(s, ']');
if (p == NULL)
parse_error(lineno, "Header without ending ]");
nothing(p + 1, lineno);
*p = 0;
hdr = new_header(hdr, s + 1);
} else if ((val = strchr(line, '=')) != NULL) {
*val++ = 0;
name = strstrip(s);
val = strstrip(val);
opt = xalloc(sizeof(struct opt));
opt->name = xstrdup(name);
opt->val = xstrdup(val);
h = hash(name);
if (!hdr)
hdr = new_header(hdr, "global");
//printf("[%s] \"%s\" = \"%s\"\n", hdr->name, name, val);
if (hdr->optslast[h] == NULL)
hdr->opts[h] = opt;
else
hdr->optslast[h]->next = opt;
hdr->optslast[h] = opt;
} else if (!empty(s)) {
parse_error(lineno, "config file line not field nor header");
}
lineno++;
}
fclose(f);
free(line);
return 0;
}
char *config_string(const char *header, const char *name)
{
struct header *hdr;
unsigned h = hash(name);
for (hdr = hlist; hdr; hdr = hdr->next) {
if (!strcmp(hdr->name, header)) {
struct opt *o;
for (o = hdr->opts[h]; o; o = o->next) {
if (!strcmp(o->name, name))
return o->val;
}
}
}
if (strcmp(header, "global"))
return config_string("global", name);
return NULL;
}
int config_number(const char *header, const char *name, char *fmt, void *val)
{
char *str = config_string(header, name);
if (str == NULL)
return -1;
if (sscanf(str, fmt, val) != 1) {
unparseable("numerical", header, name);
return -1;
}
return 0;
}
int config_choice(const char *header, const char *name, const struct config_choice *c)
{
char *str = config_string(header, name);
if (!str)
return -1;
for (; c->name; c++) {
if (!strcasecmp(str, c->name))
return c->val;
}
unparseable("choice", header, name);
return -1;
}
int config_bool(const char *header, const char *name)
{
static const struct config_choice bool_choices[] = {
{ "yes", 1 }, { "true", 1 }, { "1", 1 }, { "on", 1 },
{ "no", 0 }, { "false", 0 }, { "0", 0 }, { "off", 0 },
{}
};
return config_choice(header, name, bool_choices);
}
static char *match_arg(char **av, char *arg)
{
int len = strlen(arg);
if (!strncmp(*av, arg, len)) {
if ((*av)[len] == '=') {
return len + 1 + *av;
} else {
if (av[1] == NULL)
usage();
return av[1];
}
}
return NULL;
}
/* Look for the config file argument before parsing the other
options because we want to read the config file first so
that command line options can conveniently override it. */
const char *config_file(char **av, const char *deffn)
{
char *arg;
while (*++av) {
if (!strcmp(*av, "--"))
break;
if ((arg = match_arg(av, "--conf")) != NULL)
return arg;
}
return deffn;
}
/* Use getopt_long struct option array to process config file */
void config_options(struct option *opts, int (*func)(int))
{
for (; opts->name; opts++) {
if (!opts->has_arg) {
if (config_bool("global", opts->name) != 1)
continue;
if (opts->flag) {
*(opts->flag) = opts->val;
continue;
}
} else {
char *s = config_string("global", opts->name);
if (s == NULL)
continue;
optarg = s;
}
func(opts->val);
}
}
int config_trigger(const char *header, const char *base, struct bucket_conf *bc)
{
char *s;
char *name;
int n;
asprintf(&name, "%s-threshold", base);
s = config_string(header, name);
if (s) {
if (bucket_conf_init(bc, s) < 0) {
unparseable("trigger", header, name);
return -1;
}
}
free(name);
asprintf(&name, "%s-trigger", base);
s = config_string(header, name);
if (s) {
/* no $PATH */
if (trigger_check(s) != 0) {
SYSERRprintf("Trigger `%s' not executable\n", s);
exit(1);
}
bc->trigger = s;
}
free(name);
bc->log = 0;
asprintf(&name, "%s-log", base);
n = config_bool(header, name);
if (n >= 0)
bc->log = n;
free(name);
return 0;
}
void config_cred(char *header, char *base, struct config_cred *cred)
{
char *s;
char *name;
asprintf(&name, "%s-user", base);
if ((s = config_string(header, name)) != NULL) {
struct passwd *pw;
if (!strcmp(s, "*"))
cred->uid = -1U;
else if ((pw = getpwnam(s)) == NULL)
Eprintf("Unknown user `%s' in %s:%s config entry\n",
s, header, name);
else
cred->uid = pw->pw_uid;
}
free(name);
asprintf(&name, "%s-group", base);
if ((s = config_string(header, name)) != NULL) {
struct group *gr;
if (!strcmp(s, "*"))
cred->gid = -1U;
else if ((gr = getgrnam(s)) == NULL)
Eprintf("Unknown group `%s' in %s:%s config entry\n",
header, name, s);
else
cred->gid = gr->gr_gid;
}
free(name);
}
#ifdef TEST
int main(int ac, char **av)
{
if (!av[1])
printf("need config file\n"), exit(1);
if (parse_config_file(av[1]) < 0)
printf("cannot parse config file\n"), exit(1);
char *type;
char *header;
char *name;
int n;
while (scanf("%as %as %as", &type, &header, &name) == 3) {
switch (type[0]) {
case 'n':
if (config_number(header, name, "%d", &n) < 0)
printf("Cannot parse number %s %s\n", header, name);
else
printf("res %d\n", n);
break;
case 's':
printf("res %s\n", config_string(header, name));
break;
case 'b':
printf("res %d\n", config_bool(header, name));
break;
default:
printf("unknown type %s\n", type);
break;
}
free(type);
free(header);
free(name);
}
return 0;
}
#endif
|