[libvirt PATCH 06/32] util: use getgrouplist() directly instead of mgetgroups

Pavel Hrdina phrdina at redhat.com
Mon Jan 27 11:44:01 UTC 2020


On Thu, Jan 23, 2020 at 11:42:59AM +0000, Daniel P. Berrangé wrote:
> The mgetgroups function is a GNULIB custom wrapper around
> getgrouplist(). This implements a simplified version of
> that code directly.
> 
> Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
> ---
>  src/internal.h     |  4 ++++
>  src/util/virutil.c | 34 ++++++++++++++++++++++++++++------
>  2 files changed, 32 insertions(+), 6 deletions(-)
> 
> diff --git a/src/util/virutil.c b/src/util/virutil.c
> index 87ca16c088..4bc0aef35f 100644
> --- a/src/util/virutil.c
> +++ b/src/util/virutil.c
> @@ -1000,11 +1004,27 @@ virGetGroupList(uid_t uid, gid_t gid, gid_t **list)
>      /* invalid users have no supplementary groups */
>      if (uid != (uid_t)-1 &&
>          virGetUserEnt(uid, &user, &primary, NULL, NULL, true) >= 0) {
> -        if ((ret = mgetgroups(user, primary, list)) < 0) {
> -            virReportSystemError(errno,
> -                                 _("cannot get group list for '%s'"), user);
> -            ret = -1;
> -            goto cleanup;
> +        int nallocgrps = 10;
> +        gid_t *grps = g_new(gid_t, nallocgrps);

This is never used anywhere except in the while loop.  

> +        while (1) {
> +            int nprevallocgrps = nallocgrps;
> +            int rv;
> +
> +            rv = getgrouplist(user, primary, grps, &nallocgrps);
> +
> +            /* Some systems (like Darwin) have a bug where they
> +               never increase max_n_groups.  */
> +            if (rv < 0 && nprevallocgrps == nallocgrps)
> +                nallocgrps *= 2;
> +
> +            /* either shrinks to actual size, or enlarges tonew size */

s/tonew/to new/

> +            grps = g_renew(gid_t, grps, nallocgrps);
> +
> +            if (rv >= 0) {
> +                ret = rv;
> +                break;

Based on the GNULIB implementation here we should set *list = grps.

With that fixed:

Reviewed-by: Pavel Hrdina <phrdina at redhat.com>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20200127/80800fc7/attachment-0001.sig>


More information about the libvir-list mailing list