Changeset View
Standalone View
usr.sbin/bhyve/config.c
- This file was added.
Property | Old Value | New Value |
---|---|---|
svn:eol-style | null | native \ No newline at end of property |
svn:keywords | null | FreeBSD=%H \ No newline at end of property |
svn:mime-type | null | text/plain \ No newline at end of property |
/*- | |||||
* Copyright (c) 2015 Marcelo Araujo <araujo@FreeBSD.org> | |||||
* All rights reserved. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in the | |||||
* documentation and/or other materials provided with the distribution. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
* | |||||
*/ | |||||
#include <sys/cdefs.h> | |||||
#include <sys/types.h> | |||||
#include <err.h> | |||||
#include <errno.h> | |||||
#include <sysexits.h> | |||||
#include <string.h> | |||||
#include <stdio.h> | |||||
#include <string.h> | |||||
#include <ucl.h> | |||||
#include <unistd.h> | |||||
#include "config.h" | |||||
struct config_entry cinit = { | |||||
NULL, | |||||
NULL, | |||||
0, | |||||
NULL, | |||||
NULL, | |||||
NULL, | |||||
NULL, | |||||
NULL, | |||||
}; | |||||
/* | |||||
* Check if the config file exist. | |||||
*/ | |||||
int | |||||
load_config(const char *fname) | |||||
{ | |||||
FILE *file; | |||||
if ((file = fopen(fname, "r"))) { | |||||
fclose(file); | |||||
return 0; | |||||
} | |||||
errx(EX_USAGE, "Config file does not exist: %s", fname); | |||||
return 1; | |||||
} | |||||
/* | |||||
* Check if the basic variables are setting in | |||||
* the config file. | |||||
*/ | |||||
int | |||||
check_config(struct config_entry *config) | |||||
{ | |||||
if (!config->vmname) | |||||
errx(EX_USAGE, "name not defined"); | |||||
else if (!config->memsize) | |||||
errx(EX_USAGE, "memory not defined"); | |||||
else if (!config->vcpus) | |||||
errx(EX_USAGE, "vcpus not defined"); | |||||
else if (!config->bootdisk) | |||||
errx(EX_USAGE, "no boot disk found"); | |||||
else if (!config->iface) | |||||
errx(EX_USAGE, "iface not defined"); | |||||
allanjude: might there be cases where a user doesn't want any network interfaces? | |||||
else if (!config->console) | |||||
errx(EX_USAGE, "console not defined"); | |||||
return (0); | |||||
} | |||||
/* | |||||
* Parse the config file. | |||||
*/ | |||||
struct config_entry | |||||
parse_conf(char *config_file) | |||||
{ | |||||
struct config_entry *c = NULL; | |||||
const char *config = config_file; | |||||
struct ucl_parser *p = NULL; | |||||
ucl_object_t *obj = NULL; | |||||
const ucl_object_t *cur; | |||||
ucl_object_iter_t it = NULL; | |||||
const char *key, *value; | |||||
if (load_config(config) == 1) { | |||||
errx(EXIT_FAILURE, "No config file found: %s", config); | |||||
exit (1); | |||||
} | |||||
p = ucl_parser_new(0); | |||||
if (p == NULL) | |||||
errx(1, "Cant allocate p"); | |||||
if (!ucl_parser_add_file(p, config)) { | |||||
if (errno != ENOENT) | |||||
errx(EXIT_FAILURE, "No config file found " | |||||
"file %s: %s", config, ucl_parser_get_error(p)); | |||||
ucl_parser_free(p); | |||||
} | |||||
obj = ucl_parser_get_object(p); | |||||
if (obj->type != UCL_OBJECT) | |||||
warnx("Invalid configuration format.\n"); | |||||
c = malloc(sizeof(struct config_entry)); | |||||
c = &cinit; | |||||
while ((cur = ucl_iterate_object(obj, &it, true))) { | |||||
allanjudeUnsubmitted Not Done Inline Actionsshould probably use the new 'safe' iterator instead. allanjude: should probably use the new 'safe' iterator instead. | |||||
key = ucl_object_key(cur); | |||||
value = ucl_object_tostring_forced(cur); | |||||
if (strcasecmp(key, "name") == 0) | |||||
c->vmname = strdup(value); | |||||
else if (strcasecmp(key, "memory") == 0) | |||||
c->memsize = strdup(value); | |||||
else if (strcasecmp(key, "vcpus") == 0) | |||||
Not Done Inline Actionsnot sure if it matters to anyone, but using ucl_object_toint() here, would allow the user to use UCL's unit expansion, (k, kb, m, mb) instead of the internal call to expand_number that is used later in bhyve. UCL supports both power of 10 and power of 2 units, although for memory, bhyve will always 'do the right thing' which might be better, except in the case where the user specifies memory = 1024mb. However, with the goal of converting all config files to UCL, standardizing on using 'mb' etc may have value. allanjude: not sure if it matters to anyone, but using ucl_object_toint() here, would allow the user to… | |||||
Not Done Inline Actionsbhyve has an internal function vm_parse_memsize() that parses the memory option, I didn't bother too much to use ucl_object_toint(), but I have no problem to use it. araujo: bhyve has an internal function vm_parse_memsize() that parses the memory option, I didn't… | |||||
c->vcpus = atoi(strdup(value)); | |||||
allanjudeUnsubmitted Not Done Inline Actionsdid you consider ucl_object_toint ? allanjude: did you consider ucl_object_toint ? | |||||
araujoAuthorUnsubmitted Not Done Inline ActionsHi, I forgot to reply this one: Yes, I did consider to use it, but in the end to simplify code, I choose to use atoi(3). araujo: Hi, I forgot to reply this one: Yes, I did consider to use it, but in the end to simplify code… | |||||
else if (strcasecmp(key, "bootdisk") == 0) | |||||
Not Done Inline ActionsThe atoi() function has been deprecated by strtol() and should not be used in new code. You might want to use strtonum() here, as it also includes an upper and lower bounds check, so you can prevent the user from trying to pass negative or insanely high numbers, so something like: c->vcpus = strtonum(strdup(value), 1, MAXCPU, &errstr); errx(1, "number of cpus is %s: %s", errstr, value); allanjude: The atoi() function has been deprecated by strtol() and should not be
used in new… | |||||
Not Done Inline ActionsMake sense, I will make the changes. araujo: Make sense, I will make the changes. | |||||
c->bootdisk = strdup(value); | |||||
else if (strcasecmp(key, "iface") == 0) | |||||
c->iface = strdup(value); | |||||
else if (strcasecmp(key, "console") == 0) | |||||
c->console = strdup(value); | |||||
else if (strcasecmp(key, "disk") == 0) | |||||
c->vdisk = strdup(value); | |||||
else if (strcasecmp(key, "isofile") == 0) | |||||
c->isofile = strdup(value); | |||||
} | |||||
check_config(c); | |||||
if (p != NULL) | |||||
ucl_parser_free(p); | |||||
if (obj != NULL) | |||||
ucl_object_unref(obj); | |||||
return *c; | |||||
} |
might there be cases where a user doesn't want any network interfaces?