util: activate directory override when used from library

The Perl bindings for libvirt use the test driver for unit tests. This
tries to load the cpu_map/index.xml file, and when run from an
uninstalled build will fail.

The problem is that virFileActivateDirOverride is called by our various
binaries like libvirtd, virsh, but is not called when a 3rd party app
uses libvirt.so

To deal with this we allow the LIBVIRT_DIR_OVERRIDE=1 env variable to be
set and make virInitialize look for this. The 'run' script will set it,
so now build using this script to run against an uninstalled tree we
will correctly resolve files to the source tree.

Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2019-08-29 11:52:08 +01:00
parent 29307fa84d
commit b1b878c512
12 changed files with 35 additions and 11 deletions

6
run.in
View File

@ -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)"

View File

@ -247,6 +247,8 @@ virGlobalInit(void)
virErrorInitialize() < 0)
goto error;
virFileActivateDirOverrideForLib();
if (getuid() != geteuid() ||
getgid() != getegid()) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",

View File

@ -1918,7 +1918,8 @@ virDirOpenQuiet;
virDirRead;
virFileAbsPath;
virFileAccessibleAs;
virFileActivateDirOverride;
virFileActivateDirOverrideForLib;
virFileActivateDirOverrideForProg;
virFileBindMountDevice;
virFileBuildPath;
virFileCanonicalizePath;

View File

@ -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"));

View File

@ -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"));

View File

@ -1034,7 +1034,7 @@ int main(int argc, char **argv) {
virUpdateSelfLastChanged(argv[0]);
virFileActivateDirOverride(argv[0]);
virFileActivateDirOverrideForProg(argv[0]);
while (1) {
int optidx = 0;

View File

@ -1427,7 +1427,7 @@ main(int argc, char **argv)
exit(EXIT_FAILURE);
}
virFileActivateDirOverride(argv[0]);
virFileActivateDirOverrideForProg(argv[0]);
/* Initialize the log system */
virLogSetFromEnv();

View File

@ -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

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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);