From 3db41a6cf0eaf58401b13b0c2de0d576dc3b3c71 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Mon, 4 Mar 2019 20:26:33 +0100 Subject: [PATCH 1/3] confile_utils: lxc_config_net_is_hwaddr() Signed-off-by: Christian Brauner --- src/lxc/confile.c | 2 +- src/lxc/confile_utils.c | 44 +++++++++++++++++++------------------- src/lxc/confile_utils.h | 2 +- src/tests/lxc-test-utils.c | 12 +++++------ 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/lxc/confile.c b/src/lxc/confile.c index 120e9cddf..2c420cf8b 100644 --- a/src/lxc/confile.c +++ b/src/lxc/confile.c @@ -3014,7 +3014,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; } diff --git a/src/lxc/confile_utils.c b/src/lxc/confile_utils.c index 04926854d..fd6c28a0b 100644 --- a/src/lxc/confile_utils.c +++ b/src/lxc/confile_utils.c @@ -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"; @@ -580,27 +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 @@ -617,7 +617,7 @@ void update_hwaddr(const char *line) if (line[0] == '#') return; - if (!lxc_config_net_hwaddr(line)) + if (!lxc_config_net_is_hwaddr(line)) return; /* Let config_net_hwaddr raise the error. */ diff --git a/src/lxc/confile_utils.h b/src/lxc/confile_utils.h index e381cbe64..2c3948b8f 100644 --- a/src/lxc/confile_utils.h +++ b/src/lxc/confile_utils.h @@ -66,7 +66,7 @@ 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 bool lxc_config_net_is_hwaddr(const char *line); extern void update_hwaddr(const char *line); extern bool new_hwaddr(char *hwaddr); extern int lxc_get_conf_str(char *retv, int inlen, const char *value); diff --git a/src/tests/lxc-test-utils.c b/src/tests/lxc-test-utils.c index 4f7950ce8..841198d9e 100644 --- a/src/tests/lxc-test-utils.c +++ b/src/tests/lxc-test-utils.c @@ -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); } @@ -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); From 5648fc191c92e7bb1a8427e3e27d3bdb66807ed3 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Mon, 4 Mar 2019 20:30:41 +0100 Subject: [PATCH 2/3] confile_utils: make update_hwaddr() static Signed-off-by: Christian Brauner --- src/lxc/confile.c | 34 ++++++++++++++++++++++++++++++++++ src/lxc/confile_utils.c | 35 ----------------------------------- src/lxc/confile_utils.h | 1 - 3 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/lxc/confile.c b/src/lxc/confile.c index 2c420cf8b..a61add754 100644 --- a/src/lxc/confile.c +++ b/src/lxc/confile.c @@ -2081,6 +2081,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; diff --git a/src/lxc/confile_utils.c b/src/lxc/confile_utils.c index fd6c28a0b..72db49a08 100644 --- a/src/lxc/confile_utils.c +++ b/src/lxc/confile_utils.c @@ -601,40 +601,6 @@ void rand_complete_hwaddr(char *hwaddr) } } -/* - * 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_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); -} - bool new_hwaddr(char *hwaddr) { int ret; @@ -703,7 +669,6 @@ int lxc_get_conf_int(struct lxc_conf *c, char *retv, int inlen, int v) return fulllen; } - int lxc_get_conf_size_t(struct lxc_conf *c, char *retv, int inlen, size_t v) { int len; diff --git a/src/lxc/confile_utils.h b/src/lxc/confile_utils.h index 2c3948b8f..b097edc0f 100644 --- a/src/lxc/confile_utils.h +++ b/src/lxc/confile_utils.h @@ -67,7 +67,6 @@ 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_is_hwaddr(const char *line); -extern void update_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); From f766251429bf2e06edcbfd0c10f2d86b1fbe416c Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Mon, 4 Mar 2019 20:31:52 +0100 Subject: [PATCH 3/3] confile: make parse_limit_value() static Signed-off-by: Christian Brauner --- src/lxc/confile.c | 20 ++++++++++++++++++++ src/lxc/confile_utils.c | 21 +-------------------- src/lxc/confile_utils.h | 1 - 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/lxc/confile.c b/src/lxc/confile.c index a61add754..8f94635d1 100644 --- a/src/lxc/confile.c +++ b/src/lxc/confile.c @@ -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) { diff --git a/src/lxc/confile_utils.c b/src/lxc/confile_utils.c index 72db49a08..50777c448 100644 --- a/src/lxc/confile_utils.c +++ b/src/lxc/confile_utils.c @@ -669,6 +669,7 @@ int lxc_get_conf_int(struct lxc_conf *c, char *retv, int inlen, int v) return fulllen; } + int lxc_get_conf_size_t(struct lxc_conf *c, char *retv, int inlen, size_t v) { int len; @@ -699,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) { diff --git a/src/lxc/confile_utils.h b/src/lxc/confile_utils.h index b097edc0f..5a3bcc914 100644 --- a/src/lxc/confile_utils.h +++ b/src/lxc/confile_utils.h @@ -73,7 +73,6 @@ 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);