diff --git a/run.in b/run.in index 9a1c6ea11a..8574f81463 100644 --- a/run.in +++ b/run.in @@ -60,6 +60,12 @@ else fi export PKG_CONFIG_PATH +# Ensure that any 3rd party apps using libvirt.so from the build tree get +# files resolved to the build/source tree too. Typically useful for language +# bindings running tests against non-installed libvirt. +LIBVIRT_DIR_OVERRIDE=1 +export LIBVIRT_DIR_OVERRIDE + # This is a cheap way to find some use-after-free and uninitialized # read problems when using glibc. random_val="$(awk 'BEGIN{srand(); print 1+int(255*rand())}' < /dev/null)" diff --git a/src/libvirt.c b/src/libvirt.c index 19bc05638f..15917cba6f 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -247,6 +247,8 @@ virGlobalInit(void) virErrorInitialize() < 0) goto error; + virFileActivateDirOverrideForLib(); + if (getuid() != geteuid() || getgid() != getegid()) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 0ee2014beb..918f81470b 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1918,7 +1918,8 @@ virDirOpenQuiet; virDirRead; virFileAbsPath; virFileAccessibleAs; -virFileActivateDirOverride; +virFileActivateDirOverrideForLib; +virFileActivateDirOverrideForProg; virFileBindMountDevice; virFileBuildPath; virFileCanonicalizePath; diff --git a/src/locking/lock_daemon.c b/src/locking/lock_daemon.c index ac242bf65c..edb61d74c0 100644 --- a/src/locking/lock_daemon.c +++ b/src/locking/lock_daemon.c @@ -1188,7 +1188,7 @@ int main(int argc, char **argv) { } } - virFileActivateDirOverride(argv[0]); + virFileActivateDirOverrideForProg(argv[0]); if (!(config = virLockDaemonConfigNew(privileged))) { VIR_ERROR(_("Can't create initial configuration")); diff --git a/src/logging/log_daemon.c b/src/logging/log_daemon.c index f74e900aea..85c58aaa7b 100644 --- a/src/logging/log_daemon.c +++ b/src/logging/log_daemon.c @@ -960,7 +960,7 @@ int main(int argc, char **argv) { } } - virFileActivateDirOverride(argv[0]); + virFileActivateDirOverrideForProg(argv[0]); if (!(config = virLogDaemonConfigNew(privileged))) { VIR_ERROR(_("Can't create initial configuration")); diff --git a/src/remote/remote_daemon.c b/src/remote/remote_daemon.c index 5d64397062..966f3da2d0 100644 --- a/src/remote/remote_daemon.c +++ b/src/remote/remote_daemon.c @@ -1034,7 +1034,7 @@ int main(int argc, char **argv) { virUpdateSelfLastChanged(argv[0]); - virFileActivateDirOverride(argv[0]); + virFileActivateDirOverrideForProg(argv[0]); while (1) { int optidx = 0; diff --git a/src/security/virt-aa-helper.c b/src/security/virt-aa-helper.c index 3f9d775acc..326cfaf52a 100644 --- a/src/security/virt-aa-helper.c +++ b/src/security/virt-aa-helper.c @@ -1427,7 +1427,7 @@ main(int argc, char **argv) exit(EXIT_FAILURE); } - virFileActivateDirOverride(argv[0]); + virFileActivateDirOverrideForProg(argv[0]); /* Initialize the log system */ virLogSetFromEnv(); diff --git a/src/util/virfile.c b/src/util/virfile.c index dbfe74e24f..bb844c64e5 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -1774,7 +1774,7 @@ virFileFindResource(const char *filename, /** - * virFileActivateDirOverride: + * virFileActivateDirOverrideForProg: * @argv0: argv[0] of the calling program * * Look at @argv0 and try to detect if running from @@ -1782,7 +1782,7 @@ virFileFindResource(const char *filename, * on the binary name, or '/.libs/' in the path */ void -virFileActivateDirOverride(const char *argv0) +virFileActivateDirOverrideForProg(const char *argv0) { char *file = strrchr(argv0, '/'); if (!file || file[1] == '\0') @@ -1796,6 +1796,20 @@ virFileActivateDirOverride(const char *argv0) } +/** + * virFileActivateDirOverrideForLib: + * + * Look for LIBVIRT_DIR_OVERRIDE env var to see if we should find files from + * the build/src tree instead of install tree. + */ +void +virFileActivateDirOverrideForLib(void) +{ + if (getenv("LIBVIRT_DIR_OVERRIDE") != NULL) + useDirOverride = true; +} + + /** * virFileLength: * @path: full path of the file diff --git a/src/util/virfile.h b/src/util/virfile.h index a8a5dd8074..2b037b00aa 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -193,8 +193,9 @@ char *virFileFindResourceFull(const char *filename, const char *installdir, const char *envname) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(5); -void virFileActivateDirOverride(const char *argv0) +void virFileActivateDirOverrideForProg(const char *argv0) ATTRIBUTE_NONNULL(1); +void virFileActivateDirOverrideForLib(void); off_t virFileLength(const char *path, int fd) ATTRIBUTE_NONNULL(1); bool virFileIsDir (const char *file) ATTRIBUTE_NONNULL(1); diff --git a/tests/testutils.c b/tests/testutils.c index 8b2c51044e..003893dc44 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -992,7 +992,7 @@ int virTestMain(int argc, setenv("VIR_TEST_MOCK_PROGNAME", progname, 1); - virFileActivateDirOverride(argv[0]); + virFileActivateDirOverrideForProg(argv[0]); if (virTestSetEnvPath() < 0) return EXIT_AM_HARDFAIL; diff --git a/tools/virsh.c b/tools/virsh.c index e76d242ec8..ec20f35a77 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -897,7 +897,7 @@ main(int argc, char **argv) return EXIT_FAILURE; } - virFileActivateDirOverride(argv[0]); + virFileActivateDirOverrideForProg(argv[0]); if (!vshInit(ctl, cmdGroups, NULL)) exit(EXIT_FAILURE); diff --git a/tools/virt-admin.c b/tools/virt-admin.c index 4ff006ffcb..e549ec1f83 100644 --- a/tools/virt-admin.c +++ b/tools/virt-admin.c @@ -1552,7 +1552,7 @@ main(int argc, char **argv) return EXIT_FAILURE; } - virFileActivateDirOverride(argv[0]); + virFileActivateDirOverrideForProg(argv[0]); if (!vshInit(ctl, cmdGroups, NULL)) exit(EXIT_FAILURE);