[libvirt] [PATCH][take2][1/2] add virGetUIDByUsername() and virGetGIDByGroupname()

Guido Günther agx at sigxcpu.org
Thu Apr 16 15:07:36 UTC 2009


Hi Ryota,
On Thu, Apr 16, 2009 at 11:24:10PM +0900, Ryota Ozaki wrote:
> +#ifdef HAVE_GETPWNAM_R
> +int virGetUIDByUsername(virConnectPtr conn, char *name)
> +{
> +    struct passwd pwbuf;
> +    struct passwd *pw = NULL;
> +    char *strbuf;
> +    size_t strbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);

> +
> +    if (VIR_ALLOC_N(strbuf, strbuflen) < 0) {
> +        virReportOOMError(conn);
> +        return -1;
> +    }
We should take the same safety measures as in remoteReadConfig file
here.

> +    if (getpwnam_r(name, &pwbuf, strbuf, strbuflen, &pw) != 0 || pw == NULL) {
> +        virReportSystemError(conn, errno,
> +                             _("Failed to find user record for name '%s'"),
> +                             name);
> +        VIR_FREE(strbuf);
> +        return -1;
> +    }
> +    VIR_FREE(strbuf);
> +    return pw->pw_uid;
> +}
> +#endif
> +
> +
> +#ifdef HAVE_GETGRNAM_R
> +int virGetGIDByGroupname(virConnectPtr conn, char *name)
> +{
> +    struct group grbuf;
> +    struct group *gr = NULL;
> +    char *strbuf;
> +    size_t strbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
> +
> +    if (VIR_ALLOC_N(strbuf, strbuflen) < 0) {
> +        virReportOOMError(conn);
> +        return -1;
> +    }
same here.
Cheers,
 -- Guido




More information about the libvir-list mailing list