From: Christian Brauner Date: Tue, 4 May 2021 11:38:52 +0000 (+0200) Subject: conf: rework lxc_config_parse_arch() X-Git-Tag: lxc-5.0.0~178^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7c43fa56e70c65607f63dec8ff5a9682a3091ab2;p=thirdparty%2Flxc.git conf: rework lxc_config_parse_arch() Fix architecture parsing. So far we couldn't really differ between "want default architecture" and "failed to parse requested architecture" because the -1 return value means both. Fix this by using the return value only to indicate success or failure and return the parsed personality in a return argument. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/attach.c b/src/lxc/attach.c index 08bcb449e..929a6fea3 100644 --- a/src/lxc/attach.c +++ b/src/lxc/attach.c @@ -200,6 +200,7 @@ static int get_personality(const char *name, const char *lxcpath, signed long *personality) { __do_free char *p = NULL; + int ret; signed long per; p = lxc_cmd_get_config_item(name, "lxc.arch", lxcpath); @@ -208,9 +209,9 @@ static int get_personality(const char *name, const char *lxcpath, return 0; } - per = lxc_config_parse_arch(p); - if (per == LXC_ARCH_UNCHANGED) - return ret_errno(EINVAL); + ret = lxc_config_parse_arch(p, &per); + if (ret < 0) + return syserror("Failed to parse personality"); *personality = per; return 0; diff --git a/src/lxc/confile.c b/src/lxc/confile.c index 02d63dfe4..d895568fb 100644 --- a/src/lxc/confile.c +++ b/src/lxc/confile.c @@ -1388,14 +1388,14 @@ static int set_config_hooks_version(const char *key, const char *value, static int set_config_personality(const char *key, const char *value, struct lxc_conf *lxc_conf, void *data) { + int ret; signed long personality; - personality = lxc_config_parse_arch(value); - if (personality >= 0) - lxc_conf->personality = personality; - else - WARN("Unsupported personality \"%s\"", value); + ret = lxc_config_parse_arch(value, &personality); + if (ret < 0) + return syserror("Unsupported personality \"%s\"", value); + lxc_conf->personality = personality; return 0; } @@ -3209,7 +3209,7 @@ void lxc_config_define_free(struct lxc_list *defines) } } -signed long lxc_config_parse_arch(const char *arch) +int lxc_config_parse_arch(const char *arch, signed long *persona) { static struct per_name { char *name; @@ -3242,13 +3242,16 @@ signed long lxc_config_parse_arch(const char *arch) { "s390x", PER_LINUX }, { "x86_64", PER_LINUX }, }; - size_t len = sizeof(pername) / sizeof(pername[0]); - for (int i = 0; i < len; i++) - if (strequal(pername[i].name, arch)) - return pername[i].per; + for (int i = 0; i < ARRAY_SIZE(pername); i++) { + if (!strequal(pername[i].name, arch)) + continue; + + *persona = pername[i].per; + return 0; + } - return LXC_ARCH_UNCHANGED; + return ret_errno(EINVAL); } int lxc_fill_elevated_privileges(char *flaglist, int *flags) diff --git a/src/lxc/confile.h b/src/lxc/confile.h index c49dc8722..96c589189 100644 --- a/src/lxc/confile.h +++ b/src/lxc/confile.h @@ -84,11 +84,10 @@ __hidden extern void lxc_config_define_free(struct lxc_list *defines); #define LXC_ARCH_UNCHANGED 0xffffffffL /* - * Parse personality of the container. Returns LXC_ARCH_UNCHANGED if the - * personality is not know. - * (Used during attach.) + * Parse personality of the container. Returns 0 if personality is valid, + * negative errno otherwise. */ -__hidden extern signed long lxc_config_parse_arch(const char *arch); +__hidden extern int lxc_config_parse_arch(const char *arch, signed long *persona); __hidden extern int lxc_fill_elevated_privileges(char *flaglist, int *flags); diff --git a/src/lxc/tools/lxc_attach.c b/src/lxc/tools/lxc_attach.c index efced97b1..bce2e1cae 100644 --- a/src/lxc/tools/lxc_attach.c +++ b/src/lxc/tools/lxc_attach.c @@ -154,8 +154,8 @@ static int my_parser(struct lxc_arguments *args, int c, char *arg) break; case 'R': remount_sys_proc = 1; break; case 'a': - new_personality = lxc_config_parse_arch(arg); - if (new_personality < 0) { + ret = lxc_config_parse_arch(arg, &new_personality); + if (ret < 0) { ERROR("Invalid architecture specified: %s", arg); return -1; }