virconf.c 26.6 KB
Newer Older
1 2
/*
 * virconf.c: parser for a subset of the Python encoded Xen configuration files
D
Daniel Veillard 已提交
3
 *
4
 * Copyright (C) 2006-2013 Red Hat, Inc.
D
Daniel Veillard 已提交
5
 *
O
Osier Yang 已提交
6 7 8 9 10 11 12 13 14 15 16
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
D
Daniel Veillard 已提交
19 20 21 22
 *
 * Daniel Veillard <veillard@redhat.com>
 */

23
#include <config.h>
24

D
Daniel Veillard 已提交
25 26 27 28 29 30 31
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

32
#include "virerror.h"
33
#include "virbuffer.h"
34
#include "virconf.h"
35
#include "virutil.h"
J
Jim Meyering 已提交
36
#include "c-ctype.h"
37
#include "virlog.h"
38
#include "viralloc.h"
E
Eric Blake 已提交
39
#include "virfile.h"
40
#include "virstring.h"
D
Daniel Veillard 已提交
41

42 43
#define VIR_FROM_THIS VIR_FROM_CONF

D
Daniel Veillard 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/************************************************************************
 *									*
 *	Structures and macros used by the mini parser			*
 *									*
 ************************************************************************/

typedef struct _virConfParserCtxt virConfParserCtxt;
typedef virConfParserCtxt *virConfParserCtxtPtr;

struct _virConfParserCtxt {
    const char* filename;
    const char* base;
    const char* cur;
    const char *end;
    int line;

    virConfPtr conf;
};

#define CUR (*ctxt->cur)
#define NEXT if (ctxt->cur < ctxt->end) ctxt->cur++;
#define IS_EOL(c) (((c) == '\n') || ((c) == '\r'))
J
Jim Meyering 已提交
66 67 68 69

#define SKIP_BLANKS_AND_EOL                                             \
  do { while ((ctxt->cur < ctxt->end) && (c_isblank(CUR) || IS_EOL(CUR))) { \
         if (CUR == '\n') ctxt->line++;	                                \
70
         ctxt->cur++; } } while (0)
J
Jim Meyering 已提交
71 72 73
#define SKIP_BLANKS                                                     \
  do { while ((ctxt->cur < ctxt->end) && (c_isblank(CUR)))              \
          ctxt->cur++; } while (0)
D
Daniel Veillard 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

/************************************************************************
 *									*
 *		Structures used by configuration data			*
 *									*
 ************************************************************************/

typedef struct _virConfEntry virConfEntry;
typedef virConfEntry *virConfEntryPtr;

struct _virConfEntry {
    virConfEntryPtr next;
    char* name;
    char* comment;
    virConfValuePtr value;
};

struct _virConf {
    const char* filename;
93
    unsigned int flags;
D
Daniel Veillard 已提交
94 95 96 97 98
    virConfEntryPtr entries;
};

/**
 * virConfError:
99
 * @ctxt: the parser context if available or NULL
D
Daniel Veillard 已提交
100 101 102 103 104
 * @error: the error number
 * @info: extra information string
 *
 * Handle an error at the xend daemon interface
 */
105 106
#define virConfError(ctxt, error, info) \
    virConfErrorHelper(__FILE__, __FUNCTION__, __LINE__, ctxt, error, info)
D
Daniel Veillard 已提交
107
static void
108 109 110
virConfErrorHelper(const char *file, const char *func, size_t line,
                   virConfParserCtxtPtr ctxt,
                   virErrorNumber error, const char *info)
D
Daniel Veillard 已提交
111 112 113 114
{
    if (error == VIR_ERR_OK)
        return;

115 116
    /* Construct the string 'filename:line: info' if we have that. */
    if (ctxt && ctxt->filename) {
117 118
        virReportErrorHelper(VIR_FROM_CONF, error, file, func, line,
                             _("%s:%d: %s"), ctxt->filename, ctxt->line, info);
119
    } else {
120 121
        virReportErrorHelper(VIR_FROM_CONF, error, file, func, line,
                             "%s", info);
122
    }
D
Daniel Veillard 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
}


/************************************************************************
 *									*
 *		Structures allocations and deallocations		*
 *									*
 ************************************************************************/

/**
 * virConfFreeList:
 * @list: the list to free
 *
 * Free a list
 */
static void
virConfFreeList(virConfValuePtr list)
{
    virConfValuePtr next;

    while (list != NULL) {
        next = list->next;
145 146 147
        list->next = NULL;
        virConfFreeValue(list);
        list = next;
D
Daniel Veillard 已提交
148 149 150 151 152 153 154 155 156
    }
}

/**
 * virConfFreeValue:
 * @val: the value to free
 *
 * Free a value
 */
157
void
D
Daniel P. Berrange 已提交
158
virConfFreeValue(virConfValuePtr val)
D
Daniel Veillard 已提交
159 160 161
{
    if (val == NULL)
        return;
162 163
    if (val->type == VIR_CONF_STRING &&
        val->str != NULL)
164
        VIR_FREE(val->str);
165 166
    if (val->type == VIR_CONF_LIST &&
        val->list != NULL)
D
Daniel Veillard 已提交
167
        virConfFreeList(val->list);
168
    VIR_FREE(val);
D
Daniel Veillard 已提交
169 170
}

171
virConfPtr
D
Daniel P. Berrange 已提交
172
virConfNew(void)
D
Daniel Veillard 已提交
173 174 175
{
    virConfPtr ret;

176
    if (VIR_ALLOC(ret) < 0)
177
        return NULL;
178
    ret->filename = NULL;
179
    ret->flags = 0;
180

181
    return ret;
182
}
D
Daniel Veillard 已提交
183

184 185 186
/**
 * virConfCreate:
 * @filename: the name to report errors
187
 * @flags: combination of virConfFlag(s)
188 189 190 191 192 193
 *
 * Create a configuration internal structure
 *
 * Returns a pointer or NULL in case of error.
 */
static virConfPtr
194
virConfCreate(const char *filename, unsigned int flags)
195 196
{
    virConfPtr ret = virConfNew();
197
    if (ret) {
198
        ret->filename = filename;
199 200
        ret->flags = flags;
    }
201
    return ret;
D
Daniel Veillard 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
}

/**
 * virConfAddEntry:
 * @conf: the conf structure
 * @name: name of the entry or NULL for comment
 * @value: the value if any
 * @comm: extra comment for that entry if any
 *
 * add one entry to the conf, the parameters are included in the conf
 * if successful and freed on virConfFree()
 *
 * Returns a pointer to the entry or NULL in case of failure
 */
static virConfEntryPtr
virConfAddEntry(virConfPtr conf, char *name, virConfValuePtr value, char *comm)
{
    virConfEntryPtr ret, prev;

    if (conf == NULL)
222
        return NULL;
D
Daniel Veillard 已提交
223
    if ((comm == NULL) && (name == NULL))
224
        return NULL;
225

226
    if (VIR_ALLOC(ret) < 0)
227
        return NULL;
228

D
Daniel Veillard 已提交
229 230 231 232 233 234 235 236
    ret->name = name;
    ret->value = value;
    ret->comment = comm;

    if (conf->entries == NULL) {
        conf->entries = ret;
    } else {
        prev = conf->entries;
237 238 239
        while (prev->next != NULL)
            prev = prev->next;
        prev->next = ret;
D
Daniel Veillard 已提交
240
    }
241
    return ret;
D
Daniel Veillard 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
}

/************************************************************************
 *									*
 *			Serialization					*
 *									*
 ************************************************************************/

/**
 * virConfSaveValue:
 * @buf: output buffer
 * @val: a value
 *
 * Serialize the value to the buffer
 *
 * Returns 0 in case of success, -1 in case of error.
 */
static int
virConfSaveValue(virBufferPtr buf, virConfValuePtr val)
{
    if (val == NULL)
263
        return -1;
D
Daniel Veillard 已提交
264 265
    switch (val->type) {
        case VIR_CONF_NONE:
266
            return -1;
267
        case VIR_CONF_LONG:
268
            virBufferAsprintf(buf, "%ld", val->l);
269 270 271
            break;
        case VIR_CONF_STRING:
            if (strchr(val->str, '\n') != NULL) {
272
                virBufferAsprintf(buf, "\"\"\"%s\"\"\"", val->str);
273
            } else if (strchr(val->str, '"') == NULL) {
274
                virBufferAsprintf(buf, "\"%s\"", val->str);
275
            } else if (strchr(val->str, '\'') == NULL) {
276
                virBufferAsprintf(buf, "'%s'", val->str);
277
            } else {
278
                virBufferAsprintf(buf, "\"\"\"%s\"\"\"", val->str);
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
            }
            break;
        case VIR_CONF_LIST: {
            virConfValuePtr cur;

            cur = val->list;
            virBufferAddLit(buf, "[ ");
            if (cur != NULL) {
                virConfSaveValue(buf, cur);
                cur = cur->next;
                while (cur != NULL) {
                    virBufferAddLit(buf, ", ");
                    virConfSaveValue(buf, cur);
                    cur = cur->next;
                }
            }
            virBufferAddLit(buf, " ]");
            break;
        }
        default:
299
            return -1;
D
Daniel Veillard 已提交
300
    }
301
    return 0;
D
Daniel Veillard 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
}

/**
 * virConfSaveEntry:
 * @buf: output buffer
 * @cur: a conf entry
 *
 * Serialize the entry to the buffer
 *
 * Returns 0 in case of success, -1 in case of error.
 */
static int
virConfSaveEntry(virBufferPtr buf, virConfEntryPtr cur)
{
    if (cur->name != NULL) {
        virBufferAdd(buf, cur->name, -1);
318 319 320 321 322 323
        virBufferAddLit(buf, " = ");
        virConfSaveValue(buf, cur->value);
        if (cur->comment != NULL) {
            virBufferAddLit(buf, " #");
            virBufferAdd(buf, cur->comment, -1);
        }
D
Daniel Veillard 已提交
324
    } else if (cur->comment != NULL) {
325 326
        virBufferAddLit(buf, "#");
        virBufferAdd(buf, cur->comment, -1);
D
Daniel Veillard 已提交
327
    }
328
    virBufferAddLit(buf, "\n");
329
    return 0;
D
Daniel Veillard 已提交
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
}

/************************************************************************
 *									*
 *			The parser core					*
 *									*
 ************************************************************************/

/**
 * virConfParseLong:
 * @ctxt: the parsing context
 * @val: the result
 *
 * Parse one long int value
 *
 * Returns 0 in case of success and -1 in case of error
 */
static int
virConfParseLong(virConfParserCtxtPtr ctxt, long *val)
{
    long l = 0;
    int neg = 0;

    if (CUR == '-') {
        neg = 1;
355
        NEXT;
D
Daniel Veillard 已提交
356 357 358
    } else if (CUR == '+') {
        NEXT;
    }
J
Jim Meyering 已提交
359
    if ((ctxt->cur >= ctxt->end) || (!c_isdigit(CUR))) {
360
        virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("unterminated number"));
361
        return -1;
D
Daniel Veillard 已提交
362
    }
J
Jim Meyering 已提交
363
    while ((ctxt->cur < ctxt->end) && (c_isdigit(CUR))) {
D
Daniel Veillard 已提交
364
        l = l * 10 + (CUR - '0');
365
        NEXT;
D
Daniel Veillard 已提交
366
    }
367 368
    if (neg)
        l = -l;
D
Daniel Veillard 已提交
369
    *val = l;
370
    return 0;
D
Daniel Veillard 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
}

/**
 * virConfParseString:
 * @ctxt: the parsing context
 *
 * Parse one string
 *
 * Returns a pointer to the string or NULL in case of error
 */
static char *
virConfParseString(virConfParserCtxtPtr ctxt)
{
    const char *base;
    char *ret = NULL;

    if (CUR == '\'') {
        NEXT;
389 390 391 392
        base = ctxt->cur;
        while ((ctxt->cur < ctxt->end) && (CUR != '\'') && (!IS_EOL(CUR)))
            NEXT;
        if (CUR != '\'') {
393
            virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("unterminated string"));
394
            return NULL;
395
        }
396
        if (VIR_STRNDUP(ret, base, ctxt->cur - base) < 0)
397
            return NULL;
398
        NEXT;
399 400 401
    } else if ((ctxt->cur + 6 < ctxt->end) &&
               (STRPREFIX(ctxt->cur, "\"\"\""))) {
        /* String starts with python-style triple quotes """ */
402 403
        ctxt->cur += 3;
        base = ctxt->cur;
404

405
        /* Find the ending triple quotes */
406
        while ((ctxt->cur + 2 < ctxt->end) &&
407
               !(STRPREFIX(ctxt->cur, "\"\"\""))) {
408 409 410
            if (CUR == '\n')
                ctxt->line++;
            NEXT;
411
        }
412 413

        if (!STRPREFIX(ctxt->cur, "\"\"\"")) {
414
            virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("unterminated string"));
415
            return NULL;
416
        }
417
        if (VIR_STRNDUP(ret, base, ctxt->cur - base) < 0)
418
            return NULL;
419
        ctxt->cur += 3;
D
Daniel Veillard 已提交
420 421
    } else if (CUR == '"') {
        NEXT;
422 423 424 425
        base = ctxt->cur;
        while ((ctxt->cur < ctxt->end) && (CUR != '"') && (!IS_EOL(CUR)))
            NEXT;
        if (CUR != '"') {
426
            virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("unterminated string"));
427
            return NULL;
428
        }
429
        if (VIR_STRNDUP(ret, base, ctxt->cur - base) < 0)
430
            return NULL;
431
        NEXT;
432 433 434 435 436 437 438 439 440 441
    } else if (ctxt->conf->flags & VIR_CONF_FLAG_LXC_FORMAT) {
        base = ctxt->cur;
        /* LXC config format doesn't support comments after the value */
        while ((ctxt->cur < ctxt->end) && (!IS_EOL(CUR)))
            NEXT;
        /* Reverse to exclude the trailing blanks from the value */
        while ((ctxt->cur > base) && (c_isblank(CUR)))
            ctxt->cur--;
        if (VIR_STRNDUP(ret, base, ctxt->cur - base) < 0)
            return NULL;
D
Daniel Veillard 已提交
442
    }
443
    return ret;
D
Daniel Veillard 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
}

/**
 * virConfParseValue:
 * @ctxt: the parsing context
 *
 * Parse one value
 *
 * Returns a pointer to the value or NULL in case of error
 */
static virConfValuePtr
virConfParseValue(virConfParserCtxtPtr ctxt)
{
    virConfValuePtr ret, lst = NULL, tmp, prev;
    virConfType type = VIR_CONF_NONE;
    char *str = NULL;
    long  l = 0;

J
Jim Meyering 已提交
462
    SKIP_BLANKS;
D
Daniel Veillard 已提交
463
    if (ctxt->cur >= ctxt->end) {
464
        virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("expecting a value"));
465
        return NULL;
D
Daniel Veillard 已提交
466
    }
467 468
    if ((CUR == '"') || (CUR == '\'') ||
         (ctxt->conf->flags & VIR_CONF_FLAG_LXC_FORMAT)) {
D
Daniel Veillard 已提交
469 470
        type = VIR_CONF_STRING;
        str = virConfParseString(ctxt);
471
        if (str == NULL)
472
            return NULL;
D
Daniel Veillard 已提交
473
    } else if (CUR == '[') {
474 475 476
        if (ctxt->conf->flags & VIR_CONF_FLAG_VMX_FORMAT) {
            virConfError(ctxt, VIR_ERR_CONF_SYNTAX,
                         _("lists not allowed in VMX format"));
477
            return NULL;
478
        }
D
Daniel Veillard 已提交
479 480
        type = VIR_CONF_LIST;
        NEXT;
J
Jim Meyering 已提交
481
        SKIP_BLANKS_AND_EOL;
482
        if ((ctxt->cur < ctxt->end) && (CUR != ']')) {
483
            if ((lst = virConfParseValue(ctxt)) == NULL)
484
                return NULL;
J
Jim Meyering 已提交
485
            SKIP_BLANKS_AND_EOL;
486 487
        }
        while ((ctxt->cur < ctxt->end) && (CUR != ']')) {
488 489 490 491 492

            /* Tell Clang that when execution reaches this point
               "lst" is guaranteed to be non-NULL.  This stops it
               from issuing an invalid NULL-dereference warning about
               "prev = lst; while (prev->next..." below.  */
493
            sa_assert(lst);
494

495
            if (CUR != ',') {
496 497
                virConfError(ctxt, VIR_ERR_CONF_SYNTAX,
                             _("expecting a separator in list"));
498
                virConfFreeList(lst);
499
                return NULL;
500 501
            }
            NEXT;
J
Jim Meyering 已提交
502
            SKIP_BLANKS_AND_EOL;
503 504 505 506 507 508
            if (CUR == ']') {
                break;
            }
            tmp = virConfParseValue(ctxt);
            if (tmp == NULL) {
                virConfFreeList(lst);
509
                return NULL;
510 511 512 513
            }
            prev = lst;
            while (prev->next != NULL) prev = prev->next;
            prev->next = tmp;
J
Jim Meyering 已提交
514
            SKIP_BLANKS_AND_EOL;
515 516 517 518
        }
        if (CUR == ']') {
            NEXT;
        } else {
519 520
            virConfError(ctxt, VIR_ERR_CONF_SYNTAX,
                         _("list is not closed with ]"));
521
            virConfFreeList(lst);
522
            return NULL;
523
        }
J
Jim Meyering 已提交
524
    } else if (c_isdigit(CUR) || (CUR == '-') || (CUR == '+')) {
525 526 527
        if (ctxt->conf->flags & VIR_CONF_FLAG_VMX_FORMAT) {
            virConfError(ctxt, VIR_ERR_CONF_SYNTAX,
                         _("numbers not allowed in VMX format"));
528
            return NULL;
529
        }
D
Daniel Veillard 已提交
530
        if (virConfParseLong(ctxt, &l) < 0) {
531
            return NULL;
532
        }
D
Daniel Veillard 已提交
533 534
        type = VIR_CONF_LONG;
    } else {
535
        virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("expecting a value"));
536
        return NULL;
D
Daniel Veillard 已提交
537
    }
538 539 540
    if (VIR_ALLOC(ret) < 0) {
        virConfFreeList(lst);
        VIR_FREE(str);
541
        return NULL;
D
Daniel Veillard 已提交
542 543 544 545 546
    }
    ret->type = type;
    ret->l = l;
    ret->str = str;
    ret->list = lst;
547
    return ret;
D
Daniel Veillard 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
}

/**
 * virConfParseName:
 * @ctxt: the parsing context
 *
 * Parse one name
 *
 * Returns a copy of the new string, NULL in case of error
 */
static char *
virConfParseName(virConfParserCtxtPtr ctxt)
{
    const char *base;
    char *ret;

J
Jim Meyering 已提交
564
    SKIP_BLANKS;
D
Daniel Veillard 已提交
565 566
    base = ctxt->cur;
    /* TODO: probably need encoding support and UTF-8 parsing ! */
567 568
    if (!c_isalpha(CUR) &&
        !((ctxt->conf->flags & VIR_CONF_FLAG_VMX_FORMAT) && (CUR == '.'))) {
569
        virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("expecting a name"));
570
        return NULL;
D
Daniel Veillard 已提交
571
    }
572 573
    while ((ctxt->cur < ctxt->end) &&
           (c_isalnum(CUR) || (CUR == '_') ||
574
            ((ctxt->conf->flags & VIR_CONF_FLAG_VMX_FORMAT) &&
575 576 577
             ((CUR == ':') || (CUR == '.') || (CUR == '-'))) ||
            ((ctxt->conf->flags & VIR_CONF_FLAG_LXC_FORMAT) &&
             (CUR == '.'))))
D
Daniel Veillard 已提交
578
        NEXT;
579
    if (VIR_STRNDUP(ret, base, ctxt->cur - base) < 0)
580 581
        return NULL;
    return ret;
D
Daniel Veillard 已提交
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
}

/**
 * virConfParseComment:
 * @ctxt: the parsing context
 *
 * Parse one standalone comment in the configuration file
 *
 * Returns 0 in case of success and -1 in case of error
 */
static int
virConfParseComment(virConfParserCtxtPtr ctxt)
{
    const char *base;
    char *comm;

    if (CUR != '#')
599
        return -1;
D
Daniel Veillard 已提交
600 601 602
    NEXT;
    base = ctxt->cur;
    while ((ctxt->cur < ctxt->end) && (!IS_EOL(CUR))) NEXT;
603
    if (VIR_STRNDUP(comm, base, ctxt->cur - base) < 0)
604
        return -1;
605 606 607 608
    if (virConfAddEntry(ctxt->conf, NULL, NULL, comm) == NULL) {
        VIR_FREE(comm);
        return -1;
    }
609
    return 0;
D
Daniel Veillard 已提交
610 611 612 613 614 615 616 617 618 619 620 621 622
}

/**
 * virConfParseSeparator:
 * @ctxt: the parsing context
 *
 * Parse one separator between statement if not at the end.
 *
 * Returns 0 in case of success and -1 in case of error
 */
static int
virConfParseSeparator(virConfParserCtxtPtr ctxt)
{
J
Jim Meyering 已提交
623
    SKIP_BLANKS;
D
Daniel Veillard 已提交
624
    if (ctxt->cur >= ctxt->end)
625
        return 0;
D
Daniel Veillard 已提交
626
    if (IS_EOL(CUR)) {
J
Jim Meyering 已提交
627
        SKIP_BLANKS_AND_EOL;
D
Daniel Veillard 已提交
628
    } else if (CUR == ';') {
629
        NEXT;
J
Jim Meyering 已提交
630
        SKIP_BLANKS_AND_EOL;
D
Daniel Veillard 已提交
631
    } else {
632
        virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("expecting a separator"));
633
        return -1;
D
Daniel Veillard 已提交
634
    }
635
    return 0;
D
Daniel Veillard 已提交
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
}

/**
 * virConfParseStatement:
 * @ctxt: the parsing context
 *
 * Parse one statement in the conf file
 *
 * Returns 0 in case of success and -1 in case of error
 */
static int
virConfParseStatement(virConfParserCtxtPtr ctxt)
{
    const char *base;
    char *name;
    virConfValuePtr value;
    char *comm = NULL;

J
Jim Meyering 已提交
654
    SKIP_BLANKS_AND_EOL;
D
Daniel Veillard 已提交
655
    if (CUR == '#') {
P
Phil Petty 已提交
656
        return virConfParseComment(ctxt);
D
Daniel Veillard 已提交
657 658 659
    }
    name = virConfParseName(ctxt);
    if (name == NULL)
P
Phil Petty 已提交
660
        return -1;
J
Jim Meyering 已提交
661
    SKIP_BLANKS;
D
Daniel Veillard 已提交
662
    if (CUR != '=') {
663
        virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("expecting an assignment"));
P
Phil Petty 已提交
664 665
        VIR_FREE(name);
        return -1;
D
Daniel Veillard 已提交
666 667
    }
    NEXT;
J
Jim Meyering 已提交
668
    SKIP_BLANKS;
D
Daniel Veillard 已提交
669 670
    value = virConfParseValue(ctxt);
    if (value == NULL) {
671
        VIR_FREE(name);
P
Phil Petty 已提交
672
        return -1;
D
Daniel Veillard 已提交
673
    }
J
Jim Meyering 已提交
674
    SKIP_BLANKS;
D
Daniel Veillard 已提交
675
    if (CUR == '#') {
676 677 678
        NEXT;
        base = ctxt->cur;
        while ((ctxt->cur < ctxt->end) && (!IS_EOL(CUR))) NEXT;
679
        if (VIR_STRNDUP(comm, base, ctxt->cur - base) < 0) {
680
            VIR_FREE(name);
681
            virConfFreeValue(value);
P
Phil Petty 已提交
682
            return -1;
683
        }
D
Daniel Veillard 已提交
684 685
    }
    if (virConfAddEntry(ctxt->conf, name, value, comm) == NULL) {
686
        VIR_FREE(name);
687
        virConfFreeValue(value);
688
        VIR_FREE(comm);
P
Phil Petty 已提交
689
        return -1;
D
Daniel Veillard 已提交
690
    }
P
Phil Petty 已提交
691
    return 0;
D
Daniel Veillard 已提交
692 693 694 695 696 697 698
}

/**
 * virConfParse:
 * @filename: the name to report errors
 * @content: the configuration content in memory
 * @len: the length in bytes
699
 * @flags: combination of virConfFlag(s)
D
Daniel Veillard 已提交
700 701 702 703
 *
 * Parse the subset of the Python language needed to handle simple
 * Xen configuration files.
 *
E
Eric Blake 已提交
704
 * Returns a handle to lookup settings or NULL if it failed to
D
Daniel Veillard 已提交
705 706 707
 *         read or parse the file, use virConfFree() to free the data.
 */
static virConfPtr
708 709
virConfParse(const char *filename, const char *content, int len,
             unsigned int flags) {
D
Daniel Veillard 已提交
710 711 712 713 714 715 716
    virConfParserCtxt ctxt;

    ctxt.filename = filename;
    ctxt.base = ctxt.cur = content;
    ctxt.end = content + len - 1;
    ctxt.line = 1;

717
    ctxt.conf = virConfCreate(filename, flags);
D
Daniel Veillard 已提交
718
    if (ctxt.conf == NULL)
719
        return NULL;
D
Daniel Veillard 已提交
720 721 722

    while (ctxt.cur < ctxt.end) {
        if (virConfParseStatement(&ctxt) < 0)
723 724 725
            goto error;
        if (virConfParseSeparator(&ctxt) < 0)
            goto error;
D
Daniel Veillard 已提交
726 727
    }

728
    return ctxt.conf;
D
Daniel Veillard 已提交
729 730 731

error:
    virConfFree(ctxt.conf);
732
    return NULL;
D
Daniel Veillard 已提交
733 734 735 736 737 738 739 740
}

/************************************************************************
 *									*
 *			The module entry points				*
 *									*
 ************************************************************************/

741 742 743
/* 10 MB limit on config file size as a sanity check */
#define MAX_CONFIG_FILE_SIZE (1024*1024*10)

D
Daniel Veillard 已提交
744
/**
D
Daniel P. Berrange 已提交
745
 * virConfReadFile:
D
Daniel Veillard 已提交
746
 * @filename: the path to the configuration file.
747
 * @flags: combination of virConfFlag(s)
D
Daniel Veillard 已提交
748 749 750
 *
 * Reads a configuration file.
 *
E
Eric Blake 已提交
751
 * Returns a handle to lookup settings or NULL if it failed to
D
Daniel Veillard 已提交
752 753 754
 *         read or parse the file, use virConfFree() to free the data.
 */
virConfPtr
755
virConfReadFile(const char *filename, unsigned int flags)
D
Daniel Veillard 已提交
756
{
757
    char *content;
D
Daniel Veillard 已提交
758
    int len;
759
    virConfPtr conf;
D
Daniel Veillard 已提交
760

761 762
    VIR_DEBUG("filename=%s", NULLSTR(filename));

D
Daniel Veillard 已提交
763
    if (filename == NULL) {
764
        virConfError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
765
        return NULL;
D
Daniel Veillard 已提交
766
    }
767 768 769

    if ((len = virFileReadAll(filename, MAX_CONFIG_FILE_SIZE, &content)) < 0) {
        return NULL;
D
Daniel Veillard 已提交
770
    }
771

772
    conf = virConfParse(filename, content, len, flags);
773

774
    VIR_FREE(content);
775 776

    return conf;
D
Daniel Veillard 已提交
777 778 779
}

/**
D
Daniel P. Berrange 已提交
780
 * virConfReadMem:
D
Daniel Veillard 已提交
781
 * @memory: pointer to the content of the configuration file
D
Daniel Veillard 已提交
782
 * @len: length in byte
783
 * @flags: combination of virConfFlag(s)
D
Daniel Veillard 已提交
784 785 786 787
 *
 * Reads a configuration file loaded in memory. The string can be
 * zero terminated in which case @len can be 0
 *
E
Eric Blake 已提交
788
 * Returns a handle to lookup settings or NULL if it failed to
D
Daniel Veillard 已提交
789 790 791
 *         parse the content, use virConfFree() to free the data.
 */
virConfPtr
792
virConfReadMem(const char *memory, int len, unsigned int flags)
D
Daniel Veillard 已提交
793 794
{
    if ((memory == NULL) || (len < 0)) {
795
        virConfError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
796
        return NULL;
D
Daniel Veillard 已提交
797 798 799 800
    }
    if (len == 0)
        len = strlen(memory);

801
    return virConfParse("memory conf", memory, len, flags);
D
Daniel Veillard 已提交
802 803 804
}

/**
D
Daniel P. Berrange 已提交
805
 * virConfFree:
D
Daniel Veillard 已提交
806 807 808 809 810 811 812
 * @conf: a configuration file handle
 *
 * Frees all data associated to the handle
 *
 * Returns 0 in case of success, -1 in case of error.
 */
int
D
Daniel P. Berrange 已提交
813
virConfFree(virConfPtr conf)
D
Daniel Veillard 已提交
814
{
815
    virConfEntryPtr tmp;
816 817
    if (conf == NULL)
        return 0;
818 819 820 821

    tmp = conf->entries;
    while (tmp) {
        virConfEntryPtr next;
822
        VIR_FREE(tmp->name);
823
        virConfFreeValue(tmp->value);
824
        VIR_FREE(tmp->comment);
825
        next = tmp->next;
826
        VIR_FREE(tmp);
827
        tmp = next;
D
Daniel Veillard 已提交
828
    }
829
    VIR_FREE(conf);
830
    return 0;
D
Daniel Veillard 已提交
831 832 833
}

/**
D
Daniel P. Berrange 已提交
834
 * virConfGetValue:
D
Daniel Veillard 已提交
835 836 837 838 839
 * @conf: a configuration file handle
 * @entry: the name of the entry
 *
 * Lookup the value associated to this entry in the configuration file
 *
840
 * Returns a pointer to the value or NULL if the lookup failed, the data
D
Daniel Veillard 已提交
841 842 843
 *         associated will be freed when virConfFree() is called
 */
virConfValuePtr
D
Daniel P. Berrange 已提交
844
virConfGetValue(virConfPtr conf, const char *setting)
D
Daniel Veillard 已提交
845
{
846 847
    virConfEntryPtr cur;

848 849 850
    if (conf == NULL)
        return NULL;

851 852
    cur = conf->entries;
    while (cur != NULL) {
853 854 855 856
        if ((cur->name != NULL) &&
            ((conf->flags & VIR_CONF_FLAG_VMX_FORMAT &&
              STRCASEEQ(cur->name, setting)) ||
             STREQ(cur->name, setting)))
857
            return cur->value;
858 859
        cur = cur->next;
    }
860
    return NULL;
D
Daniel Veillard 已提交
861 862
}

863
/**
D
Daniel P. Berrange 已提交
864
 * virConfSetValue:
865 866 867 868 869 870 871 872 873 874 875
 * @conf: a configuration file handle
 * @entry: the name of the entry
 * @value: the new configuration value
 *
 * Set (or replace) the value associated to this entry in the configuration
 * file. The passed in 'value' will be owned by the conf object upon return
 * of this method, even in case of error. It should not be referenced again
 * by the caller.
 *
 * Returns 0 on success, or -1 on failure.
 */
876
int
877 878 879
virConfSetValue(virConfPtr conf,
                const char *setting,
                virConfValuePtr value)
880
{
881 882
    virConfEntryPtr cur, prev = NULL;

883 884 885
    if (value && value->type == VIR_CONF_STRING && value->str == NULL)
        return -1;

886 887
    cur = conf->entries;
    while (cur != NULL) {
888
        if ((cur->name != NULL) && (STREQ(cur->name, setting))) {
889 890 891 892 893
            break;
        }
        prev = cur;
        cur = cur->next;
    }
894

895
    if (!cur) {
896
        if (VIR_ALLOC(cur) < 0) {
897
            virConfFreeValue(value);
898
            return -1;
899 900
        }
        cur->comment = NULL;
901
        if (VIR_STRDUP(cur->name, setting) < 0) {
902
            virConfFreeValue(value);
903
            VIR_FREE(cur);
904
            return -1;
905 906 907
        }
        cur->value = value;
        if (prev) {
908
            cur->next = prev->next;
909 910
            prev->next = cur;
        } else {
911
            cur->next = conf->entries;
912 913 914
            conf->entries = cur;
        }
    } else {
915
        virConfFreeValue(cur->value);
916 917
        cur->value = value;
    }
918
    return 0;
919 920
}

921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
/**
 * virConfWalk:
 * @conf: a configuration file handle
 * @callback: the function to call to process each entry
 * @data: obscure data passed to callback
 *
 * Walk over all entries of the configuration file and run the callback
 * for each with entry name, value and the obscure data.
 *
 * Returns 0 on success, or -1 on failure.
 */
int virConfWalk(virConfPtr conf,
                 virConfWalkCallback callback,
                 void *opaque)
{
    virConfEntryPtr cur;

    if (!conf)
        return 0;

    cur = conf->entries;
    while (cur != NULL) {
        if (cur->name && cur->value &&
                callback(cur->name, cur->value, opaque) < 0)
            return -1;
        cur = cur->next;
    }
    return 0;
}
950

D
Daniel Veillard 已提交
951
/**
D
Daniel P. Berrange 已提交
952
 * virConfWriteFile:
D
Daniel Veillard 已提交
953 954 955 956 957 958 959 960
 * @filename: the path to the configuration file.
 * @conf: the conf
 *
 * Writes a configuration file back to a file.
 *
 * Returns the number of bytes written or -1 in case of error.
 */
int
D
Daniel P. Berrange 已提交
961
virConfWriteFile(const char *filename, virConfPtr conf)
D
Daniel Veillard 已提交
962
{
963
    virBuffer buf = VIR_BUFFER_INITIALIZER;
D
Daniel Veillard 已提交
964 965 966
    virConfEntryPtr cur;
    int ret;
    int fd;
967 968
    char *content;
    unsigned int use;
D
Daniel Veillard 已提交
969 970

    if (conf == NULL)
971
        return -1;
D
Daniel Veillard 已提交
972 973 974

    cur = conf->entries;
    while (cur != NULL) {
975
        virConfSaveEntry(&buf, cur);
976
        cur = cur->next;
D
Daniel Veillard 已提交
977
    }
978

979
    if (virBufferError(&buf)) {
980
        virBufferFreeAndReset(&buf);
981
        virReportOOMError();
982 983 984
        return -1;
    }

985
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
D
Daniel Veillard 已提交
986
    if (fd < 0) {
987
        virBufferFreeAndReset(&buf);
988
        virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to open file"));
989
        return -1;
D
Daniel Veillard 已提交
990 991
    }

992 993 994
    use = virBufferUse(&buf);
    content = virBufferContentAndReset(&buf);
    ret = safewrite(fd, content, use);
995
    VIR_FREE(content);
996
    VIR_FORCE_CLOSE(fd);
997
    if (ret != (int)use) {
998
        virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to save content"));
999
        return -1;
D
Daniel Veillard 已提交
1000
    }
1001 1002

    return ret;
D
Daniel Veillard 已提交
1003 1004 1005
}

/**
D
Daniel P. Berrange 已提交
1006
 * virConfWriteMem:
D
Daniel Veillard 已提交
1007
 * @memory: pointer to the memory to store the config file
1008
 * @len: pointer to the length in bytes of the store, on output the size
D
Daniel Veillard 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
 * @conf: the conf
 *
 * Writes a configuration file back to a memory area. @len is an IN/OUT
 * parameter, it indicates the size available in bytes, and on output the
 * size required for the configuration file (even if the call fails due to
 * insufficient space).
 *
 * Returns the number of bytes written or -1 in case of error.
 */
int
D
Daniel P. Berrange 已提交
1019
virConfWriteMem(char *memory, int *len, virConfPtr conf)
D
Daniel Veillard 已提交
1020
{
1021
    virBuffer buf = VIR_BUFFER_INITIALIZER;
D
Daniel Veillard 已提交
1022
    virConfEntryPtr cur;
1023 1024
    char *content;
    unsigned int use;
D
Daniel Veillard 已提交
1025 1026

    if ((memory == NULL) || (len == NULL) || (*len <= 0) || (conf == NULL))
1027
        return -1;
D
Daniel Veillard 已提交
1028 1029 1030

    cur = conf->entries;
    while (cur != NULL) {
1031
        virConfSaveEntry(&buf, cur);
1032
        cur = cur->next;
D
Daniel Veillard 已提交
1033
    }
1034

1035
    if (virBufferError(&buf)) {
1036
        virBufferFreeAndReset(&buf);
1037
        virReportOOMError();
1038
        return -1;
D
Daniel Veillard 已提交
1039
    }
1040 1041 1042 1043 1044 1045

    use = virBufferUse(&buf);
    content = virBufferContentAndReset(&buf);

    if ((int)use >= *len) {
        *len = (int)use;
1046
        VIR_FREE(content);
1047 1048 1049
        return -1;
    }
    memcpy(memory, content, use);
1050
    VIR_FREE(content);
1051 1052
    *len = use;
    return use;
D
Daniel Veillard 已提交
1053
}