Use XDG Base Directories instead of storing in home directory

As defined in:
http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html

This offers a number of advantages:
 * Allows sharing a home directory between different machines, or
sessions (eg. using NFS)
 * Cleanly separates cache, runtime (eg. sockets), or app data from
user settings
 * Supports performing smart or selective migration of settings
between different OS versions
 * Supports reseting settings without breaking things
 * Makes it possible to clear cache data to make room when the disk
is filling up
 * Allows us to write a robust and efficient backup solution
 * Allows an admin flexibility to change where data and settings are stored
 * Dramatically reduces the complexity and incoherence of the
system for administrators
This commit is contained in:
William Jon McCann 2012-05-03 12:36:27 -04:00 committed by Daniel P. Berrange
parent a25d5cfd80
commit 32a9aac2e0
20 changed files with 247 additions and 89 deletions

View File

@ -236,6 +236,7 @@ Patches have also been contributed by:
Wido den Hollander <wido@widodh.nl> Wido den Hollander <wido@widodh.nl>
Eugen Feller <eugen.feller@inria.fr> Eugen Feller <eugen.feller@inria.fr>
Dmitry Guryanov <dguryanov@parallels.com> Dmitry Guryanov <dguryanov@parallels.com>
William Jon McCann <jmccann@redhat.com>
[....send patches to get your name here....] [....send patches to get your name here....]

View File

@ -205,16 +205,16 @@ daemonConfigFilePath(bool privileged, char **configfile)
if (!(*configfile = strdup(SYSCONFDIR "/libvirt/libvirtd.conf"))) if (!(*configfile = strdup(SYSCONFDIR "/libvirt/libvirtd.conf")))
goto no_memory; goto no_memory;
} else { } else {
char *userdir = NULL; char *configdir = NULL;
if (!(userdir = virGetUserDirectory(geteuid()))) if (!(configdir = virGetUserConfigDirectory(geteuid())))
goto error; goto error;
if (virAsprintf(configfile, "%s/.libvirt/libvirtd.conf", userdir) < 0) { if (virAsprintf(configfile, "%s/libvirtd.conf", configdir) < 0) {
VIR_FREE(userdir); VIR_FREE(configdir);
goto no_memory; goto no_memory;
} }
VIR_FREE(userdir); VIR_FREE(configdir);
} }
return 0; return 0;

View File

@ -239,17 +239,25 @@ daemonPidFilePath(bool privileged,
if (!(*pidfile = strdup(LOCALSTATEDIR "/run/libvirtd.pid"))) if (!(*pidfile = strdup(LOCALSTATEDIR "/run/libvirtd.pid")))
goto no_memory; goto no_memory;
} else { } else {
char *userdir = NULL; char *rundir = NULL;
mode_t old_umask;
if (!(userdir = virGetUserDirectory(geteuid()))) if (!(rundir = virGetUserRuntimeDirectory(geteuid())))
goto error; goto error;
if (virAsprintf(pidfile, "%s/.libvirt/libvirtd.pid", userdir) < 0) { old_umask = umask(077);
VIR_FREE(userdir); if (virFileMakePath(rundir) < 0) {
umask(old_umask);
goto error;
}
umask(old_umask);
if (virAsprintf(pidfile, "%s/libvirtd.pid", rundir) < 0) {
VIR_FREE(rundir);
goto no_memory; goto no_memory;
} }
VIR_FREE(userdir); VIR_FREE(rundir);
} }
return 0; return 0;
@ -279,17 +287,25 @@ daemonUnixSocketPaths(struct daemonConfig *config,
if (!(*rosockfile = strdup(LOCALSTATEDIR "/run/libvirt/libvirt-sock-ro"))) if (!(*rosockfile = strdup(LOCALSTATEDIR "/run/libvirt/libvirt-sock-ro")))
goto no_memory; goto no_memory;
} else { } else {
char *userdir = NULL; char *rundir = NULL;
mode_t old_umask;
if (!(userdir = virGetUserDirectory(geteuid()))) if (!(rundir = virGetUserRuntimeDirectory(geteuid())))
goto error; goto error;
if (virAsprintf(sockfile, "@%s/.libvirt/libvirt-sock", userdir) < 0) { old_umask = umask(077);
VIR_FREE(userdir); if (virFileMakePath(rundir) < 0) {
umask(old_umask);
goto error;
}
umask(old_umask);
if (virAsprintf(sockfile, "@%s/libvirt-sock", rundir) < 0) {
VIR_FREE(rundir);
goto no_memory; goto no_memory;
} }
VIR_FREE(userdir); VIR_FREE(rundir);
} }
} }
return 0; return 0;
@ -593,16 +609,25 @@ daemonSetupLogging(struct daemonConfig *config,
LOCALSTATEDIR) == -1) LOCALSTATEDIR) == -1)
goto no_memory; goto no_memory;
} else { } else {
char *userdir = virGetUserDirectory(geteuid()); char *logdir = virGetUserCacheDirectory(geteuid());
if (!userdir) mode_t old_umask;
if (!logdir)
goto error; goto error;
if (virAsprintf(&tmp, "%d:file:%s/.libvirt/libvirtd.log", old_umask = umask(077);
virLogGetDefaultPriority(), userdir) == -1) { if (virFileMakePath(logdir) < 0) {
VIR_FREE(userdir); umask(old_umask);
goto error;
}
umask(old_umask);
if (virAsprintf(&tmp, "%d:file:%s/libvirtd.log",
virLogGetDefaultPriority(), logdir) == -1) {
VIR_FREE(logdir);
goto no_memory; goto no_memory;
} }
VIR_FREE(userdir); VIR_FREE(logdir);
} }
} else { } else {
if (virAsprintf(&tmp, "%d:stderr", virLogGetDefaultPriority()) < 0) if (virAsprintf(&tmp, "%d:stderr", virLogGetDefaultPriority()) < 0)
@ -722,6 +747,76 @@ static int daemonStateInit(virNetServerPtr srv)
return 0; return 0;
} }
static int migrateProfile(void)
{
char *old_base = NULL;
char *updated = NULL;
char *home = NULL;
char *xdg_dir = NULL;
char *config_dir = NULL;
const char *config_home;
int ret = -1;
mode_t old_umask;
if (!(home = virGetUserDirectory(geteuid())))
goto cleanup;
if (virAsprintf(&old_base, "%s/.libvirt", home) < 0) {
goto cleanup;
}
/* if the new directory is there or the old one is not: do nothing */
if (!(config_dir = virGetUserConfigDirectory(geteuid())))
goto cleanup;
if (!virFileIsDir(old_base) || virFileExists(config_dir)) {
ret = 0;
goto cleanup;
}
/* test if we already attempted to migrate first */
if (virAsprintf(&updated, "%s/DEPRECATED-DIRECTORY", old_base) < 0) {
goto cleanup;
}
if (virFileExists(updated)) {
goto cleanup;
}
config_home = getenv("XDG_CONFIG_HOME");
if (config_home && config_home[0] != '\0') {
xdg_dir = strdup(config_home);
} else {
if (virAsprintf(&xdg_dir, "%s/.config", home) < 0) {
goto cleanup;
}
}
old_umask = umask(077);
if (virFileMakePath(xdg_dir) < 0) {
umask(old_umask);
goto cleanup;
}
umask(old_umask);
if (rename(old_base, config_dir) < 0) {
int fd = creat(updated, 0600);
VIR_FORCE_CLOSE(fd);
VIR_ERROR(_("Unable to migrate %s to %s"), old_base, config_dir);
goto cleanup;
}
ret = 0;
cleanup:
VIR_FREE(home);
VIR_FREE(old_base);
VIR_FREE(xdg_dir);
VIR_FREE(config_dir);
VIR_FREE(updated);
return ret;
}
/* Print command-line usage. */ /* Print command-line usage. */
static void static void
daemonUsage(const char *argv0, bool privileged) daemonUsage(const char *argv0, bool privileged)
@ -775,10 +870,10 @@ libvirt management daemon:\n"), argv0);
Default paths:\n\ Default paths:\n\
\n\ \n\
Configuration file (unless overridden by -f):\n\ Configuration file (unless overridden by -f):\n\
$HOME/.libvirt/libvirtd.conf\n\ $XDG_CONFIG_HOME/libvirt/libvirtd.conf\n\
\n\ \n\
Sockets:\n\ Sockets:\n\
$HOME/.libvirt/libvirt-sock (in UNIX abstract namespace)\n\ $XDG_RUNTIME_HOME/libvirt/libvirt-sock (in UNIX abstract namespace)\n\
\n\ \n\
TLS:\n\ TLS:\n\
CA certificate: $HOME/.pki/libvirt/cacert.pem\n\ CA certificate: $HOME/.pki/libvirt/cacert.pem\n\
@ -786,7 +881,7 @@ libvirt management daemon:\n"), argv0);
Server private key: $HOME/.pki/libvirt/serverkey.pem\n\ Server private key: $HOME/.pki/libvirt/serverkey.pem\n\
\n\ \n\
PID file:\n\ PID file:\n\
$HOME/.libvirt/libvirtd.pid\n\ $XDG_RUNTIME_HOME/libvirt/libvirtd.pid\n\
\n")); \n"));
} }
} }
@ -931,6 +1026,9 @@ int main(int argc, char **argv) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (migrateProfile() < 0)
exit(EXIT_FAILURE);
if (config->host_uuid && if (config->host_uuid &&
virSetHostUUIDStr(config->host_uuid) < 0) { virSetHostUUIDStr(config->host_uuid) < 0) {
VIR_ERROR(_("invalid host UUID: %s"), config->host_uuid); VIR_ERROR(_("invalid host UUID: %s"), config->host_uuid);
@ -977,21 +1075,19 @@ int main(int argc, char **argv) {
if (privileged) { if (privileged) {
run_dir = strdup(LOCALSTATEDIR "/run/libvirt"); run_dir = strdup(LOCALSTATEDIR "/run/libvirt");
} else { } else {
char *user_dir = virGetUserDirectory(geteuid()); run_dir = virGetUserRuntimeDirectory(geteuid());
if (!user_dir) { if (!run_dir) {
VIR_ERROR(_("Can't determine user directory")); VIR_ERROR(_("Can't determine user directory"));
goto cleanup; goto cleanup;
} }
ignore_value(virAsprintf(&run_dir, "%s/.libvirt/", user_dir));
VIR_FREE(user_dir);
} }
if (!run_dir) { if (!run_dir) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
old_umask = umask(022); old_umask = umask(077);
if (virFileMakePath(run_dir) < 0) { if (virFileMakePath(run_dir) < 0) {
char ebuf[1024]; char ebuf[1024];
VIR_ERROR(_("unable to create rundir %s: %s"), run_dir, VIR_ERROR(_("unable to create rundir %s: %s"), run_dir,

View File

@ -85,7 +85,7 @@ command line using the B<-f>|B<--config> option.
The sockets libvirtd will use when B<run as root>. The sockets libvirtd will use when B<run as root>.
=item F<$HOME/.libvirt/libvirt-sock> =item F<$XDG_RUNTIME_DIR/libvirt/libvirt-sock>
The socket libvirtd will use when run as a B<non-root> user. The socket libvirtd will use when run as a B<non-root> user.

View File

@ -25,7 +25,7 @@ for the authentication file using the following sequence:
variable.</li> variable.</li>
<li>The file path specified by the "authfile=/some/file" URI <li>The file path specified by the "authfile=/some/file" URI
query parameter</li> query parameter</li>
<li>The file $HOME/.libvirt/auth.conf</li> <li>The file $XDG_CONFIG_DIR/libvirt/auth.conf</li>
<li>The file /etc/libvirt/auth.conf</li> <li>The file /etc/libvirt/auth.conf</li>
</ol> </ol>

View File

@ -30,7 +30,7 @@ virConnectPtr conn = virConnectOpenReadOnly (<b>"test:///default"</b>);
<p> <p>
To simplify life for administrators, it is possible to setup URI aliases in a To simplify life for administrators, it is possible to setup URI aliases in a
libvirt client configuration file. The configuration file is <code>/etc/libvirt/libvirt.conf</code> libvirt client configuration file. The configuration file is <code>/etc/libvirt/libvirt.conf</code>
for the root user, or <code>$HOME/.libvirt/libvirt.conf</code> for any unprivileged user. for the root user, or <code>$XDG_CONFIG_DIR/libvirt/libvirt.conf</code> for any unprivileged user.
In this file, the following syntax can be used to setup aliases In this file, the following syntax can be used to setup aliases
</p> </p>

View File

@ -969,11 +969,11 @@ virConnectGetConfigFilePath(void)
SYSCONFDIR) < 0) SYSCONFDIR) < 0)
goto no_memory; goto no_memory;
} else { } else {
char *userdir = virGetUserDirectory(geteuid()); char *userdir = virGetUserConfigDirectory(geteuid());
if (!userdir) if (!userdir)
goto error; goto error;
if (virAsprintf(&path, "%s/.libvirt/libvirt.conf", if (virAsprintf(&path, "%s/libvirt.conf",
userdir) < 0) { userdir) < 0) {
VIR_FREE(userdir); VIR_FREE(userdir);
goto no_memory; goto no_memory;

View File

@ -1116,6 +1116,7 @@ virFileFindMountPoint;
virFileHasSuffix; virFileHasSuffix;
virFileIsExecutable; virFileIsExecutable;
virFileIsLink; virFileIsLink;
virFileIsDir;
virFileLinkPointsTo; virFileLinkPointsTo;
virFileLock; virFileLock;
virFileMakePath; virFileMakePath;
@ -1136,6 +1137,9 @@ virGetGroupID;
virGetGroupName; virGetGroupName;
virGetHostname; virGetHostname;
virGetUserDirectory; virGetUserDirectory;
virGetUserConfigDirectory;
virGetUserCacheDirectory;
virGetUserRuntimeDirectory;
virGetUserID; virGetUserID;
virGetUserName; virGetUserName;
virHexToBin; virHexToBin;

View File

@ -280,18 +280,20 @@ networkStartup(int privileged) {
if ((base = strdup (SYSCONFDIR "/libvirt")) == NULL) if ((base = strdup (SYSCONFDIR "/libvirt")) == NULL)
goto out_of_memory; goto out_of_memory;
} else { } else {
char *userdir = virGetUserDirectory(uid); char *userdir = virGetUserCacheDirectory(uid);
if (!userdir) if (!userdir)
goto error; goto error;
if (virAsprintf(&driverState->logDir, if (virAsprintf(&driverState->logDir,
"%s/.libvirt/qemu/log", userdir) == -1) { "%s/qemu/log", userdir) == -1) {
VIR_FREE(userdir); VIR_FREE(userdir);
goto out_of_memory; goto out_of_memory;
} }
VIR_FREE(userdir);
if (virAsprintf(&base, "%s/.libvirt", userdir) == -1) { userdir = virGetUserConfigDirectory(uid);
if (virAsprintf(&base, "%s", userdir) == -1) {
VIR_FREE(userdir); VIR_FREE(userdir);
goto out_of_memory; goto out_of_memory;
} }

View File

@ -87,16 +87,9 @@ nwfilterDriverStartup(int privileged) {
goto out_of_memory; goto out_of_memory;
} else { } else {
uid_t uid = geteuid(); uid_t uid = geteuid();
char *userdir = virGetUserDirectory(uid); base = virGetUserConfigDirectory(uid);
if (!base)
if (!userdir)
goto error; goto error;
if (virAsprintf(&base, "%s/.libvirt", userdir) == -1) {
VIR_FREE(userdir);
goto out_of_memory;
}
VIR_FREE(userdir);
} }
if (virAsprintf(&driverState->configDir, if (virAsprintf(&driverState->configDir,

View File

@ -520,28 +520,38 @@ qemudStartup(int privileged) {
goto out_of_memory; goto out_of_memory;
} else { } else {
uid_t uid = geteuid(); uid_t uid = geteuid();
char *userdir = virGetUserDirectory(uid); char *rundir;
if (!userdir) char *cachedir;
cachedir = virGetUserCacheDirectory(uid);
if (!cachedir)
goto error; goto error;
if (virAsprintf(&qemu_driver->logDir, if (virAsprintf(&qemu_driver->logDir,
"%s/.libvirt/qemu/log", userdir) == -1) { "%s/qemu/log", cachedir) == -1) {
VIR_FREE(userdir); VIR_FREE(cachedir);
goto out_of_memory; goto out_of_memory;
} }
if (virAsprintf(&qemu_driver->cacheDir, "%s/qemu/cache", cachedir) == -1) {
if (virAsprintf(&base, "%s/.libvirt", userdir) == -1) { VIR_FREE(cachedir);
VIR_FREE(userdir);
goto out_of_memory; goto out_of_memory;
} }
VIR_FREE(userdir); VIR_FREE(cachedir);
if (virAsprintf(&qemu_driver->stateDir, "%s/qemu/run", base) == -1) rundir = virGetUserRuntimeDirectory(uid);
if (!rundir)
goto error;
if (virAsprintf(&qemu_driver->stateDir, "%s/qemu/run", rundir) == -1) {
VIR_FREE(rundir);
goto out_of_memory; goto out_of_memory;
}
VIR_FREE(rundir);
base = virGetUserConfigDirectory(uid);
if (!base)
goto error;
if (virAsprintf(&qemu_driver->libDir, "%s/qemu/lib", base) == -1) if (virAsprintf(&qemu_driver->libDir, "%s/qemu/lib", base) == -1)
goto out_of_memory; goto out_of_memory;
if (virAsprintf(&qemu_driver->cacheDir, "%s/qemu/cache", base) == -1)
goto out_of_memory;
if (virAsprintf(&qemu_driver->saveDir, "%s/qemu/save", base) == -1) if (virAsprintf(&qemu_driver->saveDir, "%s/qemu/save", base) == -1)
goto out_of_memory; goto out_of_memory;
if (virAsprintf(&qemu_driver->snapshotDir, "%s/qemu/snapshot", base) == -1) if (virAsprintf(&qemu_driver->snapshotDir, "%s/qemu/snapshot", base) == -1)

View File

@ -578,12 +578,12 @@ doRemoteOpen (virConnectPtr conn,
case trans_unix: case trans_unix:
if (!sockname) { if (!sockname) {
if (flags & VIR_DRV_OPEN_REMOTE_USER) { if (flags & VIR_DRV_OPEN_REMOTE_USER) {
char *userdir = virGetUserDirectory(getuid()); char *userdir = virGetUserRuntimeDirectory(getuid());
if (!userdir) if (!userdir)
goto failed; goto failed;
if (virAsprintf(&sockname, "@%s" LIBVIRTD_USER_UNIX_SOCKET, userdir) < 0) { if (virAsprintf(&sockname, "@%s/" LIBVIRTD_USER_UNIX_SOCKET, userdir) < 0) {
VIR_FREE(userdir); VIR_FREE(userdir);
goto out_of_memory; goto out_of_memory;
} }

View File

@ -37,7 +37,7 @@ unsigned long remoteVersion(void);
# define LIBVIRTD_TCP_PORT "16509" # define LIBVIRTD_TCP_PORT "16509"
# define LIBVIRTD_PRIV_UNIX_SOCKET LOCALSTATEDIR "/run/libvirt/libvirt-sock" # define LIBVIRTD_PRIV_UNIX_SOCKET LOCALSTATEDIR "/run/libvirt/libvirt-sock"
# define LIBVIRTD_PRIV_UNIX_SOCKET_RO LOCALSTATEDIR "/run/libvirt/libvirt-sock-ro" # define LIBVIRTD_PRIV_UNIX_SOCKET_RO LOCALSTATEDIR "/run/libvirt/libvirt-sock-ro"
# define LIBVIRTD_USER_UNIX_SOCKET "/.libvirt/libvirt-sock" # define LIBVIRTD_USER_UNIX_SOCKET "libvirt-sock"
# define LIBVIRTD_CONFIGURATION_FILE SYSCONFDIR "/libvirtd.conf" # define LIBVIRTD_CONFIGURATION_FILE SYSCONFDIR "/libvirtd.conf"
/* Defaults for PKI directory. */ /* Defaults for PKI directory. */

View File

@ -1013,16 +1013,9 @@ secretDriverStartup(int privileged)
goto out_of_memory; goto out_of_memory;
} else { } else {
uid_t uid = geteuid(); uid_t uid = geteuid();
char *userdir = virGetUserDirectory(uid); base = virGetUserConfigDirectory(uid);
if (!base)
if (!userdir)
goto error; goto error;
if (virAsprintf(&base, "%s/.libvirt", userdir) == -1) {
VIR_FREE(userdir);
goto out_of_memory;
}
VIR_FREE(userdir);
} }
if (virAsprintf(&driverState->directory, "%s/secrets", base) == -1) if (virAsprintf(&driverState->directory, "%s/secrets", base) == -1)
goto out_of_memory; goto out_of_memory;

View File

@ -146,19 +146,12 @@ storageDriverStartup(int privileged)
goto out_of_memory; goto out_of_memory;
} else { } else {
uid_t uid = geteuid(); uid_t uid = geteuid();
char *userdir = virGetUserDirectory(uid); base = virGetUserConfigDirectory(uid);
if (!base)
if (!userdir)
goto error; goto error;
if (virAsprintf(&base, "%s/.libvirt", userdir) == -1) {
VIR_FREE(userdir);
goto out_of_memory;
}
VIR_FREE(userdir);
} }
/* Configuration paths are either ~/.libvirt/storage/... (session) or /* Configuration paths are either $USER_CONFIG_HOME/libvirt/storage/... (session) or
* /etc/libvirt/storage/... (system). * /etc/libvirt/storage/... (system).
*/ */
if (virAsprintf(&driverState->configDir, if (virAsprintf(&driverState->configDir,

View File

@ -434,12 +434,12 @@ umlStartup(int privileged)
"%s/run/libvirt/uml-guest", LOCALSTATEDIR) == -1) "%s/run/libvirt/uml-guest", LOCALSTATEDIR) == -1)
goto out_of_memory; goto out_of_memory;
} else { } else {
base = virGetUserConfigDirectory(uid);
if (!base)
goto error;
if (virAsprintf(&uml_driver->logDir, if (virAsprintf(&uml_driver->logDir,
"%s/.libvirt/uml/log", userdir) == -1) "%s/uml/log", base) == -1)
goto out_of_memory;
if (virAsprintf(&base, "%s/.libvirt", userdir) == -1)
goto out_of_memory; goto out_of_memory;
if (virAsprintf(&uml_driver->monitorDir, if (virAsprintf(&uml_driver->monitorDir,
@ -447,7 +447,7 @@ umlStartup(int privileged)
goto out_of_memory; goto out_of_memory;
} }
/* Configuration paths are either ~/.libvirt/uml/... (session) or /* Configuration paths are either $XDG_CONFIG_HOME/libvirt/uml/... (session) or
* /etc/libvirt/uml/... (system). * /etc/libvirt/uml/... (system).
*/ */
if (virAsprintf(&uml_driver->configDir, "%s/uml", base) == -1) if (virAsprintf(&uml_driver->configDir, "%s/uml", base) == -1)

View File

@ -665,6 +665,12 @@ char *virFindFileInPath(const char *file)
return fullpath; return fullpath;
} }
bool virFileIsDir(const char *path)
{
struct stat s;
return (stat (path, &s) == 0) && S_ISDIR (s.st_mode);
}
bool virFileExists(const char *path) bool virFileExists(const char *path)
{ {
return access(path, F_OK) == 0; return access(path, F_OK) == 0;
@ -2304,6 +2310,62 @@ char *virGetUserDirectory(uid_t uid)
return virGetUserEnt(uid, VIR_USER_ENT_DIRECTORY); return virGetUserEnt(uid, VIR_USER_ENT_DIRECTORY);
} }
static char *virGetXDGDirectory(uid_t uid, const char *xdgenvname, const char *xdgdefdir)
{
const char *path = NULL;
char *ret = NULL;
char *home = virGetUserEnt(uid, VIR_USER_ENT_DIRECTORY);
if (uid == getuid())
path = getenv(xdgenvname);
if (path && path[0]) {
if (virAsprintf(&ret, "%s/libvirt/", path) < 0)
goto no_memory;
} else {
if (virAsprintf(&ret, "%s/%s/libvirt/", home, xdgdefdir) < 0)
goto no_memory;
}
cleanup:
VIR_FREE(home);
return ret;
no_memory:
virReportOOMError();
goto cleanup;
}
char *virGetUserConfigDirectory(uid_t uid)
{
return virGetXDGDirectory(uid, "XDG_CONFIG_HOME", ".config");
}
char *virGetUserCacheDirectory(uid_t uid)
{
return virGetXDGDirectory(uid, "XDG_CACHE_HOME", ".cache");
}
char *virGetUserRuntimeDirectory(uid_t uid)
{
const char *path = NULL;
if (uid == getuid ())
path = getenv("XDG_RUNTIME_DIR");
if (!path || !path[0]) {
return virGetUserCacheDirectory(uid);
} else {
char *ret;
if (virAsprintf(&ret, "%s/libvirt/", path) < 0) {
virReportOOMError();
return NULL;
}
return ret;
}
}
char *virGetUserName(uid_t uid) char *virGetUserName(uid_t uid)
{ {
return virGetUserEnt(uid, VIR_USER_ENT_NAME); return virGetUserEnt(uid, VIR_USER_ENT_NAME);

View File

@ -85,6 +85,7 @@ int virFileIsLink(const char *linkpath)
char *virFindFileInPath(const char *file); char *virFindFileInPath(const char *file);
bool virFileIsDir (const char *file) ATTRIBUTE_NONNULL(1);
bool virFileExists(const char *file) ATTRIBUTE_NONNULL(1); bool virFileExists(const char *file) ATTRIBUTE_NONNULL(1);
bool virFileIsExecutable(const char *file) ATTRIBUTE_NONNULL(1); bool virFileIsExecutable(const char *file) ATTRIBUTE_NONNULL(1);
@ -228,6 +229,9 @@ char *virGetHostname(virConnectPtr conn);
int virKillProcess(pid_t pid, int sig); int virKillProcess(pid_t pid, int sig);
char *virGetUserDirectory(uid_t uid); char *virGetUserDirectory(uid_t uid);
char *virGetUserConfigDirectory(uid_t uid);
char *virGetUserCacheDirectory(uid_t uid);
char *virGetUserRuntimeDirectory(uid_t uid);
char *virGetUserName(uid_t uid); char *virGetUserName(uid_t uid);
char *virGetGroupName(gid_t gid); char *virGetGroupName(gid_t gid);
int virGetUserID(const char *name, int virGetUserID(const char *name,

View File

@ -65,10 +65,10 @@ int virAuthGetConfigFilePath(virConnectPtr conn,
} }
} }
if (!(userdir = virGetUserDirectory(geteuid()))) if (!(userdir = virGetUserConfigDirectory(geteuid())))
goto cleanup; goto cleanup;
if (virAsprintf(path, "%s/.libvirt/auth.conf", userdir) < 0) if (virAsprintf(path, "%s/auth.conf", userdir) < 0)
goto no_memory; goto no_memory;
VIR_DEBUG("Checking for readability of '%s'", *path); VIR_DEBUG("Checking for readability of '%s'", *path);

View File

@ -19864,15 +19864,15 @@ vshReadlineInit(vshControl *ctl)
/* Limit the total size of the history buffer */ /* Limit the total size of the history buffer */
stifle_history(500); stifle_history(500);
/* Prepare to read/write history from/to the ~/.virsh/history file */ /* Prepare to read/write history from/to the $XDG_CACHE_HOME/virsh/history file */
userdir = virGetUserDirectory(getuid()); userdir = virGetUserCacheDirectory(getuid());
if (userdir == NULL) { if (userdir == NULL) {
vshError(ctl, "%s", _("Could not determine home directory")); vshError(ctl, "%s", _("Could not determine home directory"));
return -1; return -1;
} }
if (virAsprintf(&ctl->historydir, "%s/.virsh", userdir) < 0) { if (virAsprintf(&ctl->historydir, "%s/virsh", userdir) < 0) {
vshError(ctl, "%s", _("Out of memory")); vshError(ctl, "%s", _("Out of memory"));
VIR_FREE(userdir); VIR_FREE(userdir);
return -1; return -1;