[lxc-devel] [PATCH 20/21] lxc_unshare: Replace getpw*_r by getpw*

Stéphane Graber stgraber at ubuntu.com
Fri Jan 4 19:29:45 UTC 2013


On 01/03/2013 02:51 PM, Serge Hallyn wrote:
> Quoting Stéphane Graber (stgraber at ubuntu.com):
>> Bionic and maybe some other libc implementations lack the _r nss functions.
>> This replaces our current getpwnam_r and getpwuid_r calls by getpwnam and
>> getpwuid.
>>
>> Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
>> ---
>>  src/lxc/lxc_unshare.c | 15 ++++++++-------
>>  1 file changed, 8 insertions(+), 7 deletions(-)
>>
>> diff --git a/src/lxc/lxc_unshare.c b/src/lxc/lxc_unshare.c
>> index 3a848b2..224d8d0 100644
>> --- a/src/lxc/lxc_unshare.c
>> +++ b/src/lxc/lxc_unshare.c
>> @@ -54,12 +54,9 @@ void usage(char *cmd)
>>  
>>  static uid_t lookup_user(const char *optarg)
>>  {
>> -	int bufflen = sysconf(_SC_GETPW_R_SIZE_MAX);
>> -	char buff[bufflen];
>>  	char name[sysconf(_SC_LOGIN_NAME_MAX)];
>>  	uid_t uid = -1;
>> -	struct passwd pwent;
>> -	struct passwd *pent;
>> +	struct passwd *pwent = NULL;
>>  
>>  	if (!optarg || (optarg[0] == '\0'))
>>  		return uid;
>> @@ -69,17 +66,21 @@ static uid_t lookup_user(const char *optarg)
>>  		if (sscanf(optarg, "%s", name) < 1)
>>  			return uid;
>>  
>> -		if (getpwnam_r(name, &pwent, buff, bufflen, &pent) || !pent) {
>> +		pwent = getpwnam(name);
>> +		if (!pwent) {
>>  			ERROR("invalid username %s", name);
>>  			return uid;
>>  		}
>> -		uid = pent->pw_uid;
>> +		uid = pwent->pw_uid;
>> +		free(pwent);
> 
> You can't free the result of getpwnam.

Oops, fixed. Thanks.

>>  	} else {
>> -		if (getpwuid_r(uid, &pwent, buff, bufflen, &pent) || !pent) {
>> +		pwent = getpwuid(uid);
>> +		if (!pwent) {
>>  			ERROR("invalid uid %d", uid);
>>  			uid = -1;
>>  			return uid;
>>  		}
>> +		free(pwent);
>>  	}
>>  	return uid;
>>  }
>> -- 
>> 1.8.0
>>
>>
>> ------------------------------------------------------------------------------
>> Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
>> MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
>> with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
>> MVPs and experts. ON SALE this month only -- learn more at:
>> http://p.sf.net/sfu/learnmore_122712
>> _______________________________________________
>> Lxc-devel mailing list
>> Lxc-devel at lists.sourceforge.net
>> https://lists.sourceforge.net/lists/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: 899 bytes
Desc: OpenPGP digital signature
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20130104/33ca58e4/attachment.pgp>


More information about the lxc-devel mailing list