Add virGetGroupName to convert from GID to group name

This commit is contained in:
Daniel P. Berrange 2011-12-16 00:55:19 +00:00
parent 8c9a29545b
commit 1fff03ef9b
3 changed files with 57 additions and 0 deletions

View File

@ -1112,6 +1112,7 @@ virFindFileInPath;
virFormatMacAddr;
virGenerateMacAddr;
virGetGroupID;
virGetGroupName;
virGetHostname;
virGetUserDirectory;
virGetUserID;

View File

@ -2187,6 +2187,56 @@ static char *virGetUserEnt(uid_t uid,
return ret;
}
static char *virGetGroupEnt(gid_t gid)
{
char *strbuf;
char *ret;
struct group grbuf;
struct group *gr = NULL;
long val = sysconf(_SC_GETGR_R_SIZE_MAX);
size_t strbuflen = val;
int rc;
/* sysconf is a hint; if it fails, fall back to a reasonable size */
if (val < 0)
strbuflen = 1024;
if (VIR_ALLOC_N(strbuf, strbuflen) < 0) {
virReportOOMError();
return NULL;
}
/*
* From the manpage (terrifying but true):
*
* ERRORS
* 0 or ENOENT or ESRCH or EBADF or EPERM or ...
* The given name or gid was not found.
*/
while ((rc = getgrgid_r(gid, &grbuf, strbuf, strbuflen, &gr)) == ERANGE) {
if (VIR_RESIZE_N(strbuf, strbuflen, strbuflen, strbuflen) < 0) {
virReportOOMError();
VIR_FREE(strbuf);
return NULL;
}
}
if (rc != 0 || gr == NULL) {
virReportSystemError(rc,
_("Failed to find group record for gid '%u'"),
(unsigned int) gid);
VIR_FREE(strbuf);
return NULL;
}
ret = strdup(gr->gr_name);
VIR_FREE(strbuf);
if (!ret)
virReportOOMError();
return ret;
}
char *virGetUserDirectory(uid_t uid)
{
return virGetUserEnt(uid, VIR_USER_ENT_DIRECTORY);
@ -2197,6 +2247,11 @@ char *virGetUserName(uid_t uid)
return virGetUserEnt(uid, VIR_USER_ENT_NAME);
}
char *virGetGroupName(gid_t gid)
{
return virGetGroupEnt(gid);
}
int virGetUserID(const char *name,
uid_t *uid)

View File

@ -236,6 +236,7 @@ int virKillProcess(pid_t pid, int sig);
char *virGetUserDirectory(uid_t uid);
char *virGetUserName(uid_t uid);
char *virGetGroupName(gid_t gid);
int virGetUserID(const char *name,
uid_t *uid) ATTRIBUTE_RETURN_CHECK;
int virGetGroupID(const char *name,