Commit d0386d66 by Christian Seiler Committed by Stéphane Graber

global config: Unify parsing, add additional checks

Instead of duplicating the code for parsing the global config file for each option, write one main function, lxc_global_config_value, that does the parsing for an arbitrary option name and just call that function from the existing ones. Signed-off-by: 's avatarChristian Seiler <christian@iwakd.de> Signed-off-by: 's avatarSerge Hallyn <serge.hallyn@ubuntu.com>
parent 3fc7e0a5
...@@ -198,7 +198,7 @@ extern int mkdir_p(const char *dir, mode_t mode) ...@@ -198,7 +198,7 @@ extern int mkdir_p(const char *dir, mode_t mode)
return 0; return 0;
} }
static char *copypath(char *p) static char *copy_global_config_value(char *p)
{ {
int len = strlen(p); int len = strlen(p);
char *retbuf; char *retbuf;
...@@ -216,116 +216,97 @@ static char *copypath(char *p) ...@@ -216,116 +216,97 @@ static char *copypath(char *p)
return retbuf; return retbuf;
} }
char *default_lxcpath;
#define DEFAULT_VG "lxc" #define DEFAULT_VG "lxc"
char *default_lvmvg;
#define DEFAULT_ZFSROOT "lxc" #define DEFAULT_ZFSROOT "lxc"
char *default_zfsroot;
const char *default_lvm_vg(void) const char *lxc_global_config_value(const char *option_name)
{ {
char buf[1024], *p; static const char *options[][2] = {
FILE *fin; { "lvm_vg", DEFAULT_VG },
{ "zfsroot", DEFAULT_ZFSROOT },
if (default_lvmvg) { "lxcpath", LXCPATH },
return default_lvmvg; { NULL, NULL },
};
static const char *values[sizeof(options) / sizeof(options[0])] = { 0 };
const char *(*ptr)[2];
size_t i;
char buf[1024], *p, *p2;
FILE *fin = NULL;
for (i = 0, ptr = options; (*ptr)[0]; ptr++, i++) {
if (!strcmp(option_name, (*ptr)[0]))
break;
}
if (!(*ptr)[0]) {
errno = EINVAL;
return NULL;
}
if (values[i])
return values[i];
fin = fopen(LXC_GLOBAL_CONF, "r"); fin = fopen(LXC_GLOBAL_CONF, "r");
if (fin) { if (fin) {
while (fgets(buf, 1024, fin)) { while (fgets(buf, 1024, fin)) {
if (buf[0] == '#') if (buf[0] == '#')
continue; continue;
p = strstr(buf, "lvm_vg"); p = strstr(buf, option_name);
if (!p) if (!p)
continue; continue;
/* see if there was just white space in front
* of the option name
*/
for (p2 = buf; p2 < p; p2++) {
if (*p2 != ' ' && *p2 != '\t')
break;
}
if (p2 < p)
continue;
p = strchr(p, '='); p = strchr(p, '=');
if (!p) if (!p)
continue; continue;
/* see if there was just white space after
* the option name
*/
for (p2 += strlen(option_name); p2 < p; p2++) {
if (*p2 != ' ' && *p2 != '\t')
break;
}
if (p2 < p)
continue;
p++; p++;
while (*p && (*p == ' ' || *p == '\t')) p++; while (*p && (*p == ' ' || *p == '\t')) p++;
if (!*p) if (!*p)
continue; continue;
default_lvmvg = copypath(p); values[i] = copy_global_config_value(p);
goto out; goto out;
} }
} }
default_lvmvg = DEFAULT_VG; /* could not find value, use default */
values[i] = (*ptr)[1];
/* special case: if default value is NULL,
* and there is no config, don't view that
* as an error... */
if (!values[i])
errno = 0;
out: out:
if (fin) if (fin)
fclose(fin); fclose(fin);
return default_lvmvg; return values[i];
} }
const char *default_zfs_root(void) const char *default_lvm_vg(void)
{ {
char buf[1024], *p; return lxc_global_config_value("lvm_vg");
FILE *fin; }
if (default_zfsroot)
return default_zfsroot;
fin = fopen(LXC_GLOBAL_CONF, "r");
if (fin) {
while (fgets(buf, 1024, fin)) {
if (buf[0] == '#')
continue;
p = strstr(buf, "zfsroot");
if (!p)
continue;
p = strchr(p, '=');
if (!p)
continue;
p++;
while (*p && (*p == ' ' || *p == '\t')) p++;
if (!*p)
continue;
default_zfsroot = copypath(p);
goto out;
}
}
default_zfsroot = DEFAULT_ZFSROOT;
out: const char *default_zfs_root(void)
if (fin) {
fclose(fin); return lxc_global_config_value("zfsroot");
return default_zfsroot;
} }
const char *default_lxc_path(void) const char *default_lxc_path(void)
{ {
char buf[1024], *p; return lxc_global_config_value("lxcpath");
FILE *fin;
if (default_lxcpath)
return default_lxcpath;
fin = fopen(LXC_GLOBAL_CONF, "r");
if (fin) {
while (fgets(buf, 1024, fin)) {
if (buf[0] == '#')
continue;
p = strstr(buf, "lxcpath");
if (!p)
continue;
p = strchr(p, '=');
if (!p)
continue;
p++;
while (*p && (*p == ' ' || *p == '\t')) p++;
if (!*p)
continue;
default_lxcpath = copypath(p);
goto out;
}
}
/* we couldn't open the file, or didn't find a lxcpath
* entry there. Return @LXCPATH@ */
default_lxcpath = LXCPATH;
out:
if (fin)
fclose(fin);
return default_lxcpath;
} }
int wait_for_pid(pid_t pid) int wait_for_pid(pid_t pid)
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include <errno.h> #include <errno.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
...@@ -36,9 +37,10 @@ extern int lxc_setup_fs(void); ...@@ -36,9 +37,10 @@ extern int lxc_setup_fs(void);
extern int get_u16(unsigned short *val, const char *arg, int base); extern int get_u16(unsigned short *val, const char *arg, int base);
extern int mkdir_p(const char *dir, mode_t mode); extern int mkdir_p(const char *dir, mode_t mode);
/* /*
* Return a newly allocated buffer containing the default container * Return a buffer containing the default container path.
* path. Caller must free this buffer. * Caller must NOT free this buffer, since it may be static.
*/ */
extern const char *lxc_global_config_value(const char *option_name);
extern const char *default_lxc_path(void); extern const char *default_lxc_path(void);
extern const char *default_zfs_root(void); extern const char *default_zfs_root(void);
extern const char *default_lvm_vg(void); extern const char *default_lvm_vg(void);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment