[lxc-devel] [PATCH] use non-thread-safe getpwuid and getpwgid for android

Stéphane Graber stgraber at ubuntu.com
Thu Jul 31 19:58:12 UTC 2014


On Thu, Jul 31, 2014 at 07:54:27PM +0000, Serge Hallyn wrote:
> We only call it (so far) after doing a fork(), so this is fine.  If we
> ever need such a thing from threaded context, we'll simply need to write
> our own version for android.
> 
> Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>

Acked-by: Stéphane Graber <stgraber at ubuntu.com>

> ---
>  src/lxc/conf.c | 47 +++++++++++------------------------------------
>  1 file changed, 11 insertions(+), 36 deletions(-)
> 
> diff --git a/src/lxc/conf.c b/src/lxc/conf.c
> index 6900139..473d076 100644
> --- a/src/lxc/conf.c
> +++ b/src/lxc/conf.c
> @@ -4674,56 +4674,31 @@ err:
>  	return -1;
>  }
>  
> +/* not thread-safe, do not use from api without first forking */
>  static char* getuname(void)
>  {
> -	struct passwd pwd, *result;
> -	char *buf, *ret = NULL;
> -	size_t bufsize;
> -	int s;
> +	struct passwd *result;
>  
> -	bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
> -	if (bufsize == -1)
> -		bufsize = 16384;
> -
> -	buf = malloc(bufsize);
> -	if (!buf)
> +	result = getpwuid(geteuid());
> +	if (!result)
>  		return NULL;
>  
> -	s = getpwuid_r(geteuid(), &pwd, buf, bufsize, &result);
> -	if (s || result == NULL)
> -		goto out;
> -
> -	ret = strdup(pwd.pw_name);
> -out:
> -	free(buf);
> -	return ret;
> +	return strdup(result->pw_name);
>  }
>  
> +/* not thread-safe, do not use from api without first forking */
>  static char *getgname(void)
>  {
> -	struct group grp, *result;
> -	char *buf, *ret = NULL;
> -	size_t bufsize;
> -	int s;
> +	struct group *result;
>  
> -	bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
> -	if (bufsize == -1)
> -		bufsize = 16384;
> -
> -	buf = malloc(bufsize);
> -	if (!buf)
> +	result = getgrgid(getegid());
> +	if (!result)
>  		return NULL;
>  
> -	s = getgrgid_r(geteuid(), &grp, buf, bufsize, &result);
> -	if (s || result == NULL)
> -		goto out;
> -
> -	ret = strdup(grp.gr_name);
> -out:
> -	free(buf);
> -	return ret;
> +	return strdup(result->gr_name);
>  }
>  
> +/* not thread-safe, do not use from api without first forking */
>  void suggest_default_idmap(void)
>  {
>  	FILE *f;
> -- 
> 2.0.1
> 
> _______________________________________________
> lxc-devel mailing list
> lxc-devel at lists.linuxcontainers.org
> http://lists.linuxcontainers.org/listinfo/lxc-devel

-- 
Stéphane Graber
Ubuntu developer
http://www.ubuntu.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20140731/177cfc36/attachment.sig>


More information about the lxc-devel mailing list