util: add virGetUserDirectoryByUID

This function is needed for virt-login-shell.  Also modify virGirUserDirectory
to use the new function, to simplify the code.

Signed-off-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Dan Walsh 2013-07-30 15:55:44 -04:00 committed by Eric Blake
parent 02033b633a
commit fbd7682706
3 changed files with 20 additions and 4 deletions

View File

@ -2044,6 +2044,7 @@ virGetUnprivSGIOSysfsPath;
virGetUserCacheDirectory;
virGetUserConfigDirectory;
virGetUserDirectory;
virGetUserDirectoryByUID;
virGetUserID;
virGetUserName;
virGetUserRuntimeDirectory;

View File

@ -647,6 +647,14 @@ cleanup:
return result;
}
char *
virGetUserDirectory(void)
{
return virGetUserDirectoryByUID(geteuid());
}
#ifdef HAVE_GETPWUID_R
/* Look up fields from the user database for the given user. On
* error, set errno, report the error, and return -1. */
@ -750,13 +758,16 @@ static char *virGetGroupEnt(gid_t gid)
return ret;
}
char *virGetUserDirectory(void)
char *
virGetUserDirectoryByUID(uid_t uid)
{
char *ret;
virGetUserEnt(geteuid(), NULL, NULL, &ret);
virGetUserEnt(uid, NULL, NULL, &ret);
return ret;
}
static char *virGetXDGDirectory(const char *xdgenvname, const char *xdgdefdir)
{
const char *path = getenv(xdgenvname);
@ -1092,8 +1103,11 @@ virGetWin32DirectoryRoot(char **path)
char *
virGetUserDirectory(void)
virGetUserDirectoryByUID(uid_t uid ATTRIBUTE_UNUSED)
{
/* Since Windows lacks setuid binaries, and since we already fake
* geteuid(), we can safely assume that this is only called when
* querying about the current user */
const char *dir;
char *ret;
@ -1177,7 +1191,7 @@ virGetUserRuntimeDirectory(void)
# else /* !HAVE_GETPWUID_R && !WIN32 */
char *
virGetUserDirectory(void)
virGetUserDirectoryByUID(uid_t uid ATTRIBUTE_UNUSED)
{
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("virGetUserDirectory is not available"));

View File

@ -111,6 +111,7 @@ static inline int getgid (void) { return 0; }
char *virGetHostname(void);
char *virGetUserDirectory(void);
char *virGetUserDirectoryByUID(uid_t uid);
char *virGetUserConfigDirectory(void);
char *virGetUserCacheDirectory(void);
char *virGetUserRuntimeDirectory(void);