Remove uid param from directory lookup APIs

Remove the uid param from virGetUserConfigDirectory,
virGetUserCacheDirectory, virGetUserRuntimeDirectory,
and virGetUserDirectory

These functions were universally called with the
results of getuid() or geteuid(). To make it practical
to port to Win32, remove the uid parameter and hardcode
geteuid()

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2012-05-24 13:29:42 +01:00
parent 83fe3d22e2
commit 517368a377
16 changed files with 41 additions and 54 deletions

View File

@ -207,7 +207,7 @@ daemonConfigFilePath(bool privileged, char **configfile)
} else {
char *configdir = NULL;
if (!(configdir = virGetUserConfigDirectory(geteuid())))
if (!(configdir = virGetUserConfigDirectory()))
goto error;
if (virAsprintf(configfile, "%s/libvirtd.conf", configdir) < 0) {

View File

@ -242,7 +242,7 @@ daemonPidFilePath(bool privileged,
char *rundir = NULL;
mode_t old_umask;
if (!(rundir = virGetUserRuntimeDirectory(geteuid())))
if (!(rundir = virGetUserRuntimeDirectory()))
goto error;
old_umask = umask(077);
@ -290,7 +290,7 @@ daemonUnixSocketPaths(struct daemonConfig *config,
char *rundir = NULL;
mode_t old_umask;
if (!(rundir = virGetUserRuntimeDirectory(geteuid())))
if (!(rundir = virGetUserRuntimeDirectory()))
goto error;
old_umask = umask(077);
@ -634,7 +634,7 @@ daemonSetupLogging(struct daemonConfig *config,
LOCALSTATEDIR) == -1)
goto no_memory;
} else {
char *logdir = virGetUserCacheDirectory(geteuid());
char *logdir = virGetUserCacheDirectory();
mode_t old_umask;
if (!logdir)
@ -783,7 +783,7 @@ static int migrateProfile(void)
int ret = -1;
mode_t old_umask;
if (!(home = virGetUserDirectory(geteuid())))
if (!(home = virGetUserDirectory()))
goto cleanup;
if (virAsprintf(&old_base, "%s/.libvirt", home) < 0) {
@ -791,7 +791,7 @@ static int migrateProfile(void)
}
/* if the new directory is there or the old one is not: do nothing */
if (!(config_dir = virGetUserConfigDirectory(geteuid())))
if (!(config_dir = virGetUserConfigDirectory()))
goto cleanup;
if (!virFileIsDir(old_base) || virFileExists(config_dir)) {
@ -1112,7 +1112,7 @@ int main(int argc, char **argv) {
if (privileged) {
run_dir = strdup(LOCALSTATEDIR "/run/libvirt");
} else {
run_dir = virGetUserRuntimeDirectory(geteuid());
run_dir = virGetUserRuntimeDirectory();
if (!run_dir) {
VIR_ERROR(_("Can't determine user directory"));

View File

@ -926,7 +926,7 @@ virConnectGetConfigFilePath(void)
SYSCONFDIR) < 0)
goto no_memory;
} else {
char *userdir = virGetUserConfigDirectory(geteuid());
char *userdir = virGetUserConfigDirectory();
if (!userdir)
goto error;

View File

@ -260,7 +260,6 @@ networkAutostartConfigs(struct network_driver *driver) {
*/
static int
networkStartup(int privileged) {
uid_t uid = geteuid();
char *base = NULL;
if (VIR_ALLOC(driverState) < 0)
@ -280,7 +279,7 @@ networkStartup(int privileged) {
if ((base = strdup (SYSCONFDIR "/libvirt")) == NULL)
goto out_of_memory;
} else {
char *userdir = virGetUserCacheDirectory(uid);
char *userdir = virGetUserCacheDirectory();
if (!userdir)
goto error;
@ -292,7 +291,7 @@ networkStartup(int privileged) {
}
VIR_FREE(userdir);
userdir = virGetUserConfigDirectory(uid);
userdir = virGetUserConfigDirectory();
if (virAsprintf(&base, "%s", userdir) == -1) {
VIR_FREE(userdir);
goto out_of_memory;

View File

@ -86,8 +86,7 @@ nwfilterDriverStartup(int privileged) {
if ((base = strdup (SYSCONFDIR "/libvirt")) == NULL)
goto out_of_memory;
} else {
uid_t uid = geteuid();
base = virGetUserConfigDirectory(uid);
base = virGetUserConfigDirectory();
if (!base)
goto error;
}

View File

@ -977,7 +977,7 @@ openSSHSession(virConnectPtr conn, virConnectAuthPtr auth,
int ret;
char *pubkey = NULL;
char *pvtkey = NULL;
char *userhome = virGetUserDirectory(geteuid());
char *userhome = virGetUserDirectory();
struct stat pvt_stat, pub_stat;
if (userhome == NULL)

View File

@ -523,11 +523,10 @@ qemudStartup(int privileged) {
"%s/lib/libvirt/qemu/dump", LOCALSTATEDIR) == -1)
goto out_of_memory;
} else {
uid_t uid = geteuid();
char *rundir;
char *cachedir;
cachedir = virGetUserCacheDirectory(uid);
cachedir = virGetUserCacheDirectory();
if (!cachedir)
goto error;
@ -542,7 +541,7 @@ qemudStartup(int privileged) {
}
VIR_FREE(cachedir);
rundir = virGetUserRuntimeDirectory(uid);
rundir = virGetUserRuntimeDirectory();
if (!rundir)
goto error;
if (virAsprintf(&qemu_driver->stateDir, "%s/qemu/run", rundir) == -1) {
@ -551,7 +550,7 @@ qemudStartup(int privileged) {
}
VIR_FREE(rundir);
base = virGetUserConfigDirectory(uid);
base = virGetUserConfigDirectory();
if (!base)
goto error;
if (virAsprintf(&qemu_driver->libDir, "%s/qemu/lib", base) == -1)

View File

@ -578,7 +578,7 @@ doRemoteOpen (virConnectPtr conn,
case trans_unix:
if (!sockname) {
if (flags & VIR_DRV_OPEN_REMOTE_USER) {
char *userdir = virGetUserRuntimeDirectory(getuid());
char *userdir = virGetUserRuntimeDirectory();
if (!userdir)
goto failed;

View File

@ -774,7 +774,7 @@ static int virNetTLSContextLocateCredentials(const char *pkipath,
/* Check to see if $HOME/.pki contains at least one of the
* files and if so, use that
*/
userdir = virGetUserDirectory(getuid());
userdir = virGetUserDirectory();
if (!userdir)
goto out_of_memory;

View File

@ -1012,8 +1012,7 @@ secretDriverStartup(int privileged)
if (base == NULL)
goto out_of_memory;
} else {
uid_t uid = geteuid();
base = virGetUserConfigDirectory(uid);
base = virGetUserConfigDirectory();
if (!base)
goto error;
}

View File

@ -145,8 +145,7 @@ storageDriverStartup(int privileged)
if ((base = strdup (SYSCONFDIR "/libvirt")) == NULL)
goto out_of_memory;
} else {
uid_t uid = geteuid();
base = virGetUserConfigDirectory(uid);
base = virGetUserConfigDirectory();
if (!base)
goto error;
}

View File

@ -392,7 +392,6 @@ cleanup:
static int
umlStartup(int privileged)
{
uid_t uid = geteuid();
char *base = NULL;
char *userdir = NULL;
@ -418,7 +417,7 @@ umlStartup(int privileged)
if (!uml_driver->domainEventState)
goto error;
userdir = virGetUserDirectory(uid);
userdir = virGetUserDirectory();
if (!userdir)
goto error;
@ -434,7 +433,7 @@ umlStartup(int privileged)
"%s/run/libvirt/uml-guest", LOCALSTATEDIR) == -1)
goto out_of_memory;
} else {
base = virGetUserConfigDirectory(uid);
base = virGetUserConfigDirectory();
if (!base)
goto error;

View File

@ -2305,19 +2305,16 @@ static char *virGetGroupEnt(gid_t gid)
return ret;
}
char *virGetUserDirectory(uid_t uid)
char *virGetUserDirectory(void)
{
return virGetUserEnt(uid, VIR_USER_ENT_DIRECTORY);
return virGetUserEnt(geteuid(), VIR_USER_ENT_DIRECTORY);
}
static char *virGetXDGDirectory(uid_t uid, const char *xdgenvname, const char *xdgdefdir)
static char *virGetXDGDirectory(const char *xdgenvname, const char *xdgdefdir)
{
const char *path = NULL;
const char *path = getenv(xdgenvname);
char *ret = NULL;
char *home = virGetUserEnt(uid, VIR_USER_ENT_DIRECTORY);
if (uid == getuid())
path = getenv(xdgenvname);
char *home = virGetUserEnt(geteuid(), VIR_USER_ENT_DIRECTORY);
if (path && path[0]) {
if (virAsprintf(&ret, "%s/libvirt", path) < 0)
@ -2335,25 +2332,22 @@ static char *virGetXDGDirectory(uid_t uid, const char *xdgenvname, const char *x
goto cleanup;
}
char *virGetUserConfigDirectory(uid_t uid)
char *virGetUserConfigDirectory(void)
{
return virGetXDGDirectory(uid, "XDG_CONFIG_HOME", ".config");
return virGetXDGDirectory("XDG_CONFIG_HOME", ".config");
}
char *virGetUserCacheDirectory(uid_t uid)
char *virGetUserCacheDirectory(void)
{
return virGetXDGDirectory(uid, "XDG_CACHE_HOME", ".cache");
return virGetXDGDirectory("XDG_CACHE_HOME", ".cache");
}
char *virGetUserRuntimeDirectory(uid_t uid)
char *virGetUserRuntimeDirectory(void)
{
const char *path = NULL;
if (uid == getuid ())
path = getenv("XDG_RUNTIME_DIR");
const char *path = getenv("XDG_RUNTIME_DIR");
if (!path || !path[0]) {
return virGetUserCacheDirectory(uid);
return virGetUserCacheDirectory();
} else {
char *ret;
@ -2551,7 +2545,7 @@ error:
#else /* HAVE_GETPWUID_R */
char *
virGetUserDirectory(uid_t uid ATTRIBUTE_UNUSED)
virGetUserDirectory(void)
{
virUtilError(VIR_ERR_INTERNAL_ERROR,
"%s", _("virGetUserDirectory is not available"));

View File

@ -228,10 +228,10 @@ char *virGetHostname(virConnectPtr conn);
int virKillProcess(pid_t pid, int sig);
char *virGetUserDirectory(uid_t uid);
char *virGetUserConfigDirectory(uid_t uid);
char *virGetUserCacheDirectory(uid_t uid);
char *virGetUserRuntimeDirectory(uid_t uid);
char *virGetUserDirectory(void);
char *virGetUserConfigDirectory(void);
char *virGetUserCacheDirectory(void);
char *virGetUserRuntimeDirectory(void);
char *virGetUserName(uid_t uid);
char *virGetGroupName(gid_t gid);
int virGetUserID(const char *name,

View File

@ -65,7 +65,7 @@ int virAuthGetConfigFilePath(virConnectPtr conn,
}
}
if (!(userdir = virGetUserConfigDirectory(geteuid())))
if (!(userdir = virGetUserConfigDirectory()))
goto cleanup;
if (virAsprintf(path, "%s/auth.conf", userdir) < 0)

View File

@ -15674,8 +15674,7 @@ cmdCd(vshControl *ctl, const vshCmd *cmd)
}
if (vshCommandOptString(cmd, "dir", &dir) <= 0) {
uid_t uid = geteuid();
dir = dir_malloced = virGetUserDirectory(uid);
dir = dir_malloced = virGetUserDirectory();
}
if (!dir)
dir = "/";
@ -19872,7 +19871,7 @@ vshReadlineInit(vshControl *ctl)
stifle_history(500);
/* Prepare to read/write history from/to the $XDG_CACHE_HOME/virsh/history file */
userdir = virGetUserCacheDirectory(getuid());
userdir = virGetUserCacheDirectory();
if (userdir == NULL) {
vshError(ctl, "%s", _("Could not determine home directory"));