Skip to content

Commit

Permalink
Merge pull request #2893 from brauner/2019-03-04/minor_tweaks
Browse files Browse the repository at this point in the history
minor tweaks
  • Loading branch information
stgraber committed Mar 5, 2019
2 parents 8690bff + f766251 commit 859ce01
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 85 deletions.
56 changes: 55 additions & 1 deletion src/lxc/confile.c
Expand Up @@ -1450,6 +1450,26 @@ static int set_config_cgroup_relative(const char *key, const char *value,
return -EINVAL;
}

static bool parse_limit_value(const char **value, rlim_t *res)
{
char *endptr = NULL;

if (strncmp(*value, "unlimited", STRLITERALLEN("unlimited")) == 0) {
*res = RLIM_INFINITY;
*value += STRLITERALLEN("unlimited");
return true;
}

errno = 0;
*res = strtoull(*value, &endptr, 10);
if (errno || !endptr)
return false;

*value = endptr;

return true;
}

static int set_config_prlimit(const char *key, const char *value,
struct lxc_conf *lxc_conf, void *data)
{
Expand Down Expand Up @@ -2081,6 +2101,40 @@ static int set_config_console_size(const char *key, const char *value,
return 0;
}

/*
* If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
* file, we expand it in the unexpanded_config, so that after a save_config we
* store the hwaddr for re-use.
* This is only called when reading the config file, not when executing a
* lxc.include.
* 'x' and 'X' are substituted in-place.
*/
static void update_hwaddr(const char *line)
{
char *p;

line += lxc_char_left_gc(line, strlen(line));
if (line[0] == '#')
return;

if (!lxc_config_net_is_hwaddr(line))
return;

/* Let config_net_hwaddr raise the error. */
p = strchr(line, '=');
if (!p)
return;
p++;

while (isblank(*p))
p++;

if (!*p)
return;

rand_complete_hwaddr(p);
}

int append_unexp_config_line(const char *line, struct lxc_conf *conf)
{
size_t linelen;
Expand Down Expand Up @@ -3014,7 +3068,7 @@ bool network_new_hwaddrs(struct lxc_conf *conf)
else
lend++;

if (!lxc_config_net_hwaddr(lstart)) {
if (!lxc_config_net_is_hwaddr(lstart)) {
lstart = lend;
continue;
}
Expand Down
96 changes: 21 additions & 75 deletions src/lxc/confile_utils.c
Expand Up @@ -548,6 +548,27 @@ int network_ifname(char *valuep, const char *value, size_t size)
return 0;
}

bool lxc_config_net_is_hwaddr(const char *line)
{
unsigned index;
char tmp[7];

if (strncmp(line, "lxc.net", 7) != 0)
return false;

if (strncmp(line, "lxc.net.hwaddr", 14) == 0)
return true;

if (strncmp(line, "lxc.network.hwaddr", 18) == 0)
return true;

if (sscanf(line, "lxc.net.%u.%6s", &index, tmp) == 2 ||
sscanf(line, "lxc.network.%u.%6s", &index, tmp) == 2)
return strncmp(tmp, "hwaddr", 6) == 0;

return false;
}

void rand_complete_hwaddr(char *hwaddr)
{
const char hex[] = "0123456789abcdef";
Expand Down Expand Up @@ -580,61 +601,6 @@ void rand_complete_hwaddr(char *hwaddr)
}
}

bool lxc_config_net_hwaddr(const char *line)
{
unsigned index;
char tmp[7];

if (strncmp(line, "lxc.net", 7) != 0)
return false;

if (strncmp(line, "lxc.net.hwaddr", 14) == 0)
return true;

if (strncmp(line, "lxc.network.hwaddr", 18) == 0)
return true;

if (sscanf(line, "lxc.net.%u.%6s", &index, tmp) == 2 ||
sscanf(line, "lxc.network.%u.%6s", &index, tmp) == 2)
return strncmp(tmp, "hwaddr", 6) == 0;

return false;
}

/*
* If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
* file, we expand it in the unexpanded_config, so that after a save_config we
* store the hwaddr for re-use.
* This is only called when reading the config file, not when executing a
* lxc.include.
* 'x' and 'X' are substituted in-place.
*/
void update_hwaddr(const char *line)
{
char *p;

line += lxc_char_left_gc(line, strlen(line));
if (line[0] == '#')
return;

if (!lxc_config_net_hwaddr(line))
return;

/* Let config_net_hwaddr raise the error. */
p = strchr(line, '=');
if (!p)
return;
p++;

while (isblank(*p))
p++;

if (!*p)
return;

rand_complete_hwaddr(p);
}

bool new_hwaddr(char *hwaddr)
{
int ret;
Expand Down Expand Up @@ -734,26 +700,6 @@ int lxc_get_conf_uint64(struct lxc_conf *c, char *retv, int inlen, uint64_t v)
return fulllen;
}

bool parse_limit_value(const char **value, rlim_t *res)
{
char *endptr = NULL;

if (strncmp(*value, "unlimited", STRLITERALLEN("unlimited")) == 0) {
*res = RLIM_INFINITY;
*value += STRLITERALLEN("unlimited");
return true;
}

errno = 0;
*res = strtoull(*value, &endptr, 10);
if (errno || !endptr)
return false;

*value = endptr;

return true;
}

static int lxc_container_name_to_pid(const char *lxcname_or_pid,
const char *lxcpath)
{
Expand Down
4 changes: 1 addition & 3 deletions src/lxc/confile_utils.h
Expand Up @@ -66,15 +66,13 @@ extern int set_config_path_item(char **conf_item, const char *value);
extern int config_ip_prefix(struct in_addr *addr);
extern int network_ifname(char *valuep, const char *value, size_t size);
extern void rand_complete_hwaddr(char *hwaddr);
extern bool lxc_config_net_hwaddr(const char *line);
extern void update_hwaddr(const char *line);
extern bool lxc_config_net_is_hwaddr(const char *line);
extern bool new_hwaddr(char *hwaddr);
extern int lxc_get_conf_str(char *retv, int inlen, const char *value);
extern int lxc_get_conf_bool(struct lxc_conf *c, char *retv, int inlen, bool v);
extern int lxc_get_conf_int(struct lxc_conf *c, char *retv, int inlen, int v);
extern int lxc_get_conf_size_t(struct lxc_conf *c, char *retv, int inlen, size_t v);
extern int lxc_get_conf_uint64(struct lxc_conf *c, char *retv, int inlen, uint64_t v);
extern bool parse_limit_value(const char **value, rlim_t *res);
extern int lxc_inherit_namespace(const char *lxcname_or_pid,
const char *lxcpath, const char *namespace);
extern int sig_parse(const char *signame);
Expand Down
12 changes: 6 additions & 6 deletions src/tests/lxc-test-utils.c
Expand Up @@ -518,18 +518,18 @@ void test_parse_byte_size_string(void)
}
}

void test_lxc_config_net_hwaddr(void)
void test_lxc_config_net_is_hwaddr(void)
{
if (!lxc_config_net_hwaddr("lxc.net.0.hwaddr = 00:16:3e:04:65:b8\n"))
if (!lxc_config_net_is_hwaddr("lxc.net.0.hwaddr = 00:16:3e:04:65:b8\n"))
exit(EXIT_FAILURE);

if (lxc_config_net_hwaddr("lxc.net"))
if (lxc_config_net_is_hwaddr("lxc.net"))
exit(EXIT_FAILURE);

if (lxc_config_net_hwaddr("lxc.net."))
if (lxc_config_net_is_hwaddr("lxc.net."))
exit(EXIT_FAILURE);

if (lxc_config_net_hwaddr("lxc.net.0."))
if (lxc_config_net_is_hwaddr("lxc.net.0."))
exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -604,7 +604,7 @@ int main(int argc, char *argv[])
test_lxc_safe_int();
test_lxc_safe_long();
test_parse_byte_size_string();
test_lxc_config_net_hwaddr();
test_lxc_config_net_is_hwaddr();
test_task_blocks_signal();

exit(EXIT_SUCCESS);
Expand Down

0 comments on commit 859ce01

Please sign in to comment.