2017-04-03 12:29:45 +00:00
|
|
|
/*
|
|
|
|
* virfilewrapper.c: Wrapper for universal file access
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2017-05-11 10:42:14 +00:00
|
|
|
#ifndef WIN32
|
2017-04-03 12:29:45 +00:00
|
|
|
|
2017-05-11 10:42:14 +00:00
|
|
|
# include <fcntl.h>
|
|
|
|
|
|
|
|
# include "viralloc.h"
|
|
|
|
# include "virfile.h"
|
|
|
|
# include "virfilewrapper.h"
|
|
|
|
# include "virmock.h"
|
|
|
|
# include "virstring.h"
|
2017-04-03 12:29:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Mapping for prefix overrides */
|
|
|
|
static size_t noverrides;
|
|
|
|
static const char **overrides;
|
|
|
|
|
|
|
|
/* nprefixes == noverrides, but two variables make it easier to use
|
|
|
|
* VIR_*_ELEMENT macros */
|
|
|
|
static size_t nprefixes;
|
|
|
|
static const char **prefixes;
|
|
|
|
|
|
|
|
/* TODO: callbacks */
|
|
|
|
static int (*real_open)(const char *path, int flags, ...);
|
|
|
|
static FILE *(*real_fopen)(const char *path, const char *mode);
|
|
|
|
static int (*real_access)(const char *path, int mode);
|
|
|
|
static int (*real_mkdir)(const char *path, mode_t mode);
|
|
|
|
static DIR *(*real_opendir)(const char *path);
|
2019-09-23 10:44:38 +00:00
|
|
|
static int (*real_execv)(const char *path, char *const argv[]);
|
|
|
|
static int (*real_execve)(const char *path, char *const argv[], char *const envp[]);
|
2017-04-03 12:29:45 +00:00
|
|
|
|
|
|
|
static void init_syms(void)
|
|
|
|
{
|
|
|
|
if (real_fopen)
|
|
|
|
return;
|
|
|
|
|
|
|
|
VIR_MOCK_REAL_INIT(fopen);
|
|
|
|
VIR_MOCK_REAL_INIT(access);
|
|
|
|
VIR_MOCK_REAL_INIT(mkdir);
|
|
|
|
VIR_MOCK_REAL_INIT(open);
|
|
|
|
VIR_MOCK_REAL_INIT(opendir);
|
2019-09-23 10:44:38 +00:00
|
|
|
VIR_MOCK_REAL_INIT(execv);
|
|
|
|
VIR_MOCK_REAL_INIT(execve);
|
2017-04-03 12:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-13 08:45:25 +00:00
|
|
|
void
|
2017-04-03 12:29:45 +00:00
|
|
|
virFileWrapperAddPrefix(const char *prefix,
|
2017-07-25 09:43:33 +00:00
|
|
|
const char *override)
|
2017-04-03 12:29:45 +00:00
|
|
|
{
|
|
|
|
/* Both parameters are mandatory */
|
2019-03-13 08:45:25 +00:00
|
|
|
if (!prefix || !override) {
|
|
|
|
fprintf(stderr, "Attempt to add invalid path override\n");
|
|
|
|
abort();
|
|
|
|
}
|
2017-04-03 12:29:45 +00:00
|
|
|
|
|
|
|
init_syms();
|
|
|
|
|
|
|
|
if (VIR_APPEND_ELEMENT_QUIET(prefixes, nprefixes, prefix) < 0 ||
|
|
|
|
VIR_APPEND_ELEMENT_QUIET(overrides, noverrides, override) < 0) {
|
|
|
|
VIR_FREE(prefixes);
|
|
|
|
VIR_FREE(overrides);
|
2019-03-13 08:45:25 +00:00
|
|
|
fprintf(stderr, "Unable to add path override for '%s'\n", prefix);
|
|
|
|
abort();
|
2017-04-03 12:29:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
virFileWrapperRemovePrefix(const char *prefix)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < noverrides; i++) {
|
|
|
|
if (STREQ(prefixes[i], prefix))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == noverrides)
|
|
|
|
return;
|
|
|
|
|
|
|
|
VIR_DELETE_ELEMENT(overrides, i, noverrides);
|
|
|
|
VIR_DELETE_ELEMENT(prefixes, i, nprefixes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
virFileWrapperClearPrefixes(void)
|
|
|
|
{
|
|
|
|
nprefixes = 0;
|
|
|
|
noverrides = 0;
|
|
|
|
|
|
|
|
VIR_FREE(prefixes);
|
|
|
|
VIR_FREE(overrides);
|
|
|
|
}
|
|
|
|
|
tests: fix mocking of stat() / lstat() functions
Quite a few of the tests have a need to mock the stat() / lstat()
functions and they are taking somewhat different & inconsistent
approaches none of which are actually fully correct. This is shown
by fact that 'make check' fails on 32-bit hosts. Investigation
revealed that the code was calling into the native C library impl,
not getting intercepted by our mocks.
The POSIX stat() function might resolve to any number of different
symbols in the C library.
The may be an additional stat64() function exposed by the headers
too.
On 64-bit hosts the stat & stat64 functions are identical, always
refering to the 64-bit ABI.
On 32-bit hosts they refer to the 32-bit & 64-bit ABIs respectively.
Libvirt uses _FILE_OFFSET_BITS=64 on 32-bit hosts, which causes the
C library to transparently rewrite stat() calls to be stat64() calls.
Libvirt will never see the 32-bit ABI from the traditional stat()
call. We cannot assume this rewriting is done using a macro. It might
be, but on GLibC it is done with a magic __asm__ statement to apply
the rewrite at link time instead of at preprocessing.
In GLibC there may be two additional functions exposed by the headers,
__xstat() and __xstat64(). When these exist, stat() and stat64() are
transparently rewritten to call __xstat() and __xstat64() respectively.
The former symbols will not actally exist in the library at all, only
the header. The leading "__" indicates the symbols are a private impl
detail of the C library that applications should not care about.
Unfortunately, because we are trying to mock replace the C library,
we need to know about this internal impl detail.
With all this in mind the list of functions we have to mock will depend
on several factors
- If _FILE_OFFSET_BITS is set, then we are on a 32-bit host, and we
only need to mock stat64 and __xstat64. The other stat / __xstat
functions exist, but we'll never call them so they can be ignored
for mocking.
- If _FILE_OFFSET_BITS is not set, then we are on a 64-bit host and
we should mock stat, stat64, __xstat & __xstat64. Either may be
called by app code.
- If __xstat & __xstat64 exist, then stat & stat64 will not exist
as symbols in the library, so the latter should not be mocked.
The same all applies to lstat()
These rules are complex enough that we don't want to duplicate them
across every mock file, so this centralizes all the logic in a helper
file virmockstathelper.c that should be #included when needed. The
code merely need to provide a filename rewriting callback called
virMockStatRedirect(). Optionally VIR_MOCK_STAT_HOOK can be defined
as a macro if further processing is needed inline.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2019-04-01 16:47:25 +00:00
|
|
|
# include "virmockstathelpers.c"
|
|
|
|
|
|
|
|
int
|
|
|
|
virMockStatRedirect(const char *path, char **newpath)
|
2017-04-03 12:29:45 +00:00
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < noverrides; i++) {
|
|
|
|
const char *tmp = STRSKIP(path, prefixes[i]);
|
|
|
|
|
|
|
|
if (!tmp)
|
|
|
|
continue;
|
|
|
|
|
tests: fix mocking of stat() / lstat() functions
Quite a few of the tests have a need to mock the stat() / lstat()
functions and they are taking somewhat different & inconsistent
approaches none of which are actually fully correct. This is shown
by fact that 'make check' fails on 32-bit hosts. Investigation
revealed that the code was calling into the native C library impl,
not getting intercepted by our mocks.
The POSIX stat() function might resolve to any number of different
symbols in the C library.
The may be an additional stat64() function exposed by the headers
too.
On 64-bit hosts the stat & stat64 functions are identical, always
refering to the 64-bit ABI.
On 32-bit hosts they refer to the 32-bit & 64-bit ABIs respectively.
Libvirt uses _FILE_OFFSET_BITS=64 on 32-bit hosts, which causes the
C library to transparently rewrite stat() calls to be stat64() calls.
Libvirt will never see the 32-bit ABI from the traditional stat()
call. We cannot assume this rewriting is done using a macro. It might
be, but on GLibC it is done with a magic __asm__ statement to apply
the rewrite at link time instead of at preprocessing.
In GLibC there may be two additional functions exposed by the headers,
__xstat() and __xstat64(). When these exist, stat() and stat64() are
transparently rewritten to call __xstat() and __xstat64() respectively.
The former symbols will not actally exist in the library at all, only
the header. The leading "__" indicates the symbols are a private impl
detail of the C library that applications should not care about.
Unfortunately, because we are trying to mock replace the C library,
we need to know about this internal impl detail.
With all this in mind the list of functions we have to mock will depend
on several factors
- If _FILE_OFFSET_BITS is set, then we are on a 32-bit host, and we
only need to mock stat64 and __xstat64. The other stat / __xstat
functions exist, but we'll never call them so they can be ignored
for mocking.
- If _FILE_OFFSET_BITS is not set, then we are on a 64-bit host and
we should mock stat, stat64, __xstat & __xstat64. Either may be
called by app code.
- If __xstat & __xstat64 exist, then stat & stat64 will not exist
as symbols in the library, so the latter should not be mocked.
The same all applies to lstat()
These rules are complex enough that we don't want to duplicate them
across every mock file, so this centralizes all the logic in a helper
file virmockstathelper.c that should be #included when needed. The
code merely need to provide a filename rewriting callback called
virMockStatRedirect(). Optionally VIR_MOCK_STAT_HOOK can be defined
as a macro if further processing is needed inline.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2019-04-01 16:47:25 +00:00
|
|
|
if (virAsprintfQuiet(newpath, "%s%s", overrides[i], tmp) < 0)
|
|
|
|
return -1;
|
2017-04-03 12:29:45 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
tests: fix mocking of stat() / lstat() functions
Quite a few of the tests have a need to mock the stat() / lstat()
functions and they are taking somewhat different & inconsistent
approaches none of which are actually fully correct. This is shown
by fact that 'make check' fails on 32-bit hosts. Investigation
revealed that the code was calling into the native C library impl,
not getting intercepted by our mocks.
The POSIX stat() function might resolve to any number of different
symbols in the C library.
The may be an additional stat64() function exposed by the headers
too.
On 64-bit hosts the stat & stat64 functions are identical, always
refering to the 64-bit ABI.
On 32-bit hosts they refer to the 32-bit & 64-bit ABIs respectively.
Libvirt uses _FILE_OFFSET_BITS=64 on 32-bit hosts, which causes the
C library to transparently rewrite stat() calls to be stat64() calls.
Libvirt will never see the 32-bit ABI from the traditional stat()
call. We cannot assume this rewriting is done using a macro. It might
be, but on GLibC it is done with a magic __asm__ statement to apply
the rewrite at link time instead of at preprocessing.
In GLibC there may be two additional functions exposed by the headers,
__xstat() and __xstat64(). When these exist, stat() and stat64() are
transparently rewritten to call __xstat() and __xstat64() respectively.
The former symbols will not actally exist in the library at all, only
the header. The leading "__" indicates the symbols are a private impl
detail of the C library that applications should not care about.
Unfortunately, because we are trying to mock replace the C library,
we need to know about this internal impl detail.
With all this in mind the list of functions we have to mock will depend
on several factors
- If _FILE_OFFSET_BITS is set, then we are on a 32-bit host, and we
only need to mock stat64 and __xstat64. The other stat / __xstat
functions exist, but we'll never call them so they can be ignored
for mocking.
- If _FILE_OFFSET_BITS is not set, then we are on a 64-bit host and
we should mock stat, stat64, __xstat & __xstat64. Either may be
called by app code.
- If __xstat & __xstat64 exist, then stat & stat64 will not exist
as symbols in the library, so the latter should not be mocked.
The same all applies to lstat()
These rules are complex enough that we don't want to duplicate them
across every mock file, so this centralizes all the logic in a helper
file virmockstathelper.c that should be #included when needed. The
code merely need to provide a filename rewriting callback called
virMockStatRedirect(). Optionally VIR_MOCK_STAT_HOOK can be defined
as a macro if further processing is needed inline.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2019-04-01 16:47:25 +00:00
|
|
|
return 0;
|
2017-04-03 12:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
# define PATH_OVERRIDE(newpath, path) \
|
|
|
|
do { \
|
|
|
|
init_syms(); \
|
|
|
|
\
|
tests: fix mocking of stat() / lstat() functions
Quite a few of the tests have a need to mock the stat() / lstat()
functions and they are taking somewhat different & inconsistent
approaches none of which are actually fully correct. This is shown
by fact that 'make check' fails on 32-bit hosts. Investigation
revealed that the code was calling into the native C library impl,
not getting intercepted by our mocks.
The POSIX stat() function might resolve to any number of different
symbols in the C library.
The may be an additional stat64() function exposed by the headers
too.
On 64-bit hosts the stat & stat64 functions are identical, always
refering to the 64-bit ABI.
On 32-bit hosts they refer to the 32-bit & 64-bit ABIs respectively.
Libvirt uses _FILE_OFFSET_BITS=64 on 32-bit hosts, which causes the
C library to transparently rewrite stat() calls to be stat64() calls.
Libvirt will never see the 32-bit ABI from the traditional stat()
call. We cannot assume this rewriting is done using a macro. It might
be, but on GLibC it is done with a magic __asm__ statement to apply
the rewrite at link time instead of at preprocessing.
In GLibC there may be two additional functions exposed by the headers,
__xstat() and __xstat64(). When these exist, stat() and stat64() are
transparently rewritten to call __xstat() and __xstat64() respectively.
The former symbols will not actally exist in the library at all, only
the header. The leading "__" indicates the symbols are a private impl
detail of the C library that applications should not care about.
Unfortunately, because we are trying to mock replace the C library,
we need to know about this internal impl detail.
With all this in mind the list of functions we have to mock will depend
on several factors
- If _FILE_OFFSET_BITS is set, then we are on a 32-bit host, and we
only need to mock stat64 and __xstat64. The other stat / __xstat
functions exist, but we'll never call them so they can be ignored
for mocking.
- If _FILE_OFFSET_BITS is not set, then we are on a 64-bit host and
we should mock stat, stat64, __xstat & __xstat64. Either may be
called by app code.
- If __xstat & __xstat64 exist, then stat & stat64 will not exist
as symbols in the library, so the latter should not be mocked.
The same all applies to lstat()
These rules are complex enough that we don't want to duplicate them
across every mock file, so this centralizes all the logic in a helper
file virmockstathelper.c that should be #included when needed. The
code merely need to provide a filename rewriting callback called
virMockStatRedirect(). Optionally VIR_MOCK_STAT_HOOK can be defined
as a macro if further processing is needed inline.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2019-04-01 16:47:25 +00:00
|
|
|
if (virMockStatRedirect(path, &newpath) < 0) \
|
2017-11-03 12:09:47 +00:00
|
|
|
abort(); \
|
2017-04-03 12:29:45 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
|
|
FILE *fopen(const char *path, const char *mode)
|
|
|
|
{
|
2019-03-13 08:54:45 +00:00
|
|
|
VIR_AUTOFREE(char *) newpath = NULL;
|
2017-04-03 12:29:45 +00:00
|
|
|
|
|
|
|
PATH_OVERRIDE(newpath, path);
|
|
|
|
|
tests: fix mocking of stat() / lstat() functions
Quite a few of the tests have a need to mock the stat() / lstat()
functions and they are taking somewhat different & inconsistent
approaches none of which are actually fully correct. This is shown
by fact that 'make check' fails on 32-bit hosts. Investigation
revealed that the code was calling into the native C library impl,
not getting intercepted by our mocks.
The POSIX stat() function might resolve to any number of different
symbols in the C library.
The may be an additional stat64() function exposed by the headers
too.
On 64-bit hosts the stat & stat64 functions are identical, always
refering to the 64-bit ABI.
On 32-bit hosts they refer to the 32-bit & 64-bit ABIs respectively.
Libvirt uses _FILE_OFFSET_BITS=64 on 32-bit hosts, which causes the
C library to transparently rewrite stat() calls to be stat64() calls.
Libvirt will never see the 32-bit ABI from the traditional stat()
call. We cannot assume this rewriting is done using a macro. It might
be, but on GLibC it is done with a magic __asm__ statement to apply
the rewrite at link time instead of at preprocessing.
In GLibC there may be two additional functions exposed by the headers,
__xstat() and __xstat64(). When these exist, stat() and stat64() are
transparently rewritten to call __xstat() and __xstat64() respectively.
The former symbols will not actally exist in the library at all, only
the header. The leading "__" indicates the symbols are a private impl
detail of the C library that applications should not care about.
Unfortunately, because we are trying to mock replace the C library,
we need to know about this internal impl detail.
With all this in mind the list of functions we have to mock will depend
on several factors
- If _FILE_OFFSET_BITS is set, then we are on a 32-bit host, and we
only need to mock stat64 and __xstat64. The other stat / __xstat
functions exist, but we'll never call them so they can be ignored
for mocking.
- If _FILE_OFFSET_BITS is not set, then we are on a 64-bit host and
we should mock stat, stat64, __xstat & __xstat64. Either may be
called by app code.
- If __xstat & __xstat64 exist, then stat & stat64 will not exist
as symbols in the library, so the latter should not be mocked.
The same all applies to lstat()
These rules are complex enough that we don't want to duplicate them
across every mock file, so this centralizes all the logic in a helper
file virmockstathelper.c that should be #included when needed. The
code merely need to provide a filename rewriting callback called
virMockStatRedirect(). Optionally VIR_MOCK_STAT_HOOK can be defined
as a macro if further processing is needed inline.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2019-04-01 16:47:25 +00:00
|
|
|
return real_fopen(newpath ? newpath : path, mode);
|
2017-04-03 12:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int access(const char *path, int mode)
|
|
|
|
{
|
2019-03-13 08:54:45 +00:00
|
|
|
VIR_AUTOFREE(char *) newpath = NULL;
|
2017-04-03 12:29:45 +00:00
|
|
|
|
|
|
|
PATH_OVERRIDE(newpath, path);
|
|
|
|
|
tests: fix mocking of stat() / lstat() functions
Quite a few of the tests have a need to mock the stat() / lstat()
functions and they are taking somewhat different & inconsistent
approaches none of which are actually fully correct. This is shown
by fact that 'make check' fails on 32-bit hosts. Investigation
revealed that the code was calling into the native C library impl,
not getting intercepted by our mocks.
The POSIX stat() function might resolve to any number of different
symbols in the C library.
The may be an additional stat64() function exposed by the headers
too.
On 64-bit hosts the stat & stat64 functions are identical, always
refering to the 64-bit ABI.
On 32-bit hosts they refer to the 32-bit & 64-bit ABIs respectively.
Libvirt uses _FILE_OFFSET_BITS=64 on 32-bit hosts, which causes the
C library to transparently rewrite stat() calls to be stat64() calls.
Libvirt will never see the 32-bit ABI from the traditional stat()
call. We cannot assume this rewriting is done using a macro. It might
be, but on GLibC it is done with a magic __asm__ statement to apply
the rewrite at link time instead of at preprocessing.
In GLibC there may be two additional functions exposed by the headers,
__xstat() and __xstat64(). When these exist, stat() and stat64() are
transparently rewritten to call __xstat() and __xstat64() respectively.
The former symbols will not actally exist in the library at all, only
the header. The leading "__" indicates the symbols are a private impl
detail of the C library that applications should not care about.
Unfortunately, because we are trying to mock replace the C library,
we need to know about this internal impl detail.
With all this in mind the list of functions we have to mock will depend
on several factors
- If _FILE_OFFSET_BITS is set, then we are on a 32-bit host, and we
only need to mock stat64 and __xstat64. The other stat / __xstat
functions exist, but we'll never call them so they can be ignored
for mocking.
- If _FILE_OFFSET_BITS is not set, then we are on a 64-bit host and
we should mock stat, stat64, __xstat & __xstat64. Either may be
called by app code.
- If __xstat & __xstat64 exist, then stat & stat64 will not exist
as symbols in the library, so the latter should not be mocked.
The same all applies to lstat()
These rules are complex enough that we don't want to duplicate them
across every mock file, so this centralizes all the logic in a helper
file virmockstathelper.c that should be #included when needed. The
code merely need to provide a filename rewriting callback called
virMockStatRedirect(). Optionally VIR_MOCK_STAT_HOOK can be defined
as a macro if further processing is needed inline.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2019-04-01 16:47:25 +00:00
|
|
|
return real_access(newpath ? newpath : path, mode);
|
2017-04-03 12:29:45 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 16:13:22 +00:00
|
|
|
# ifdef __APPLE__
|
|
|
|
int _open(const char *path, int flags, ...) __asm("_open");
|
|
|
|
int _open(const char *path, int flags, ...)
|
|
|
|
# else
|
2017-04-03 12:29:45 +00:00
|
|
|
int open(const char *path, int flags, ...)
|
2019-08-21 16:13:22 +00:00
|
|
|
# endif
|
2017-04-03 12:29:45 +00:00
|
|
|
{
|
2019-03-13 08:54:45 +00:00
|
|
|
VIR_AUTOFREE(char *) newpath = NULL;
|
2017-09-25 18:27:07 +00:00
|
|
|
va_list ap;
|
|
|
|
mode_t mode = 0;
|
2017-04-03 12:29:45 +00:00
|
|
|
|
|
|
|
PATH_OVERRIDE(newpath, path);
|
|
|
|
|
2017-09-25 18:27:07 +00:00
|
|
|
/* The mode argument is mandatory when O_CREAT is set in flags,
|
|
|
|
* otherwise the argument is ignored.
|
|
|
|
*/
|
|
|
|
if (flags & O_CREAT) {
|
|
|
|
va_start(ap, flags);
|
2017-10-05 07:06:03 +00:00
|
|
|
mode = (mode_t) va_arg(ap, int);
|
2017-09-25 18:27:07 +00:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
tests: fix mocking of stat() / lstat() functions
Quite a few of the tests have a need to mock the stat() / lstat()
functions and they are taking somewhat different & inconsistent
approaches none of which are actually fully correct. This is shown
by fact that 'make check' fails on 32-bit hosts. Investigation
revealed that the code was calling into the native C library impl,
not getting intercepted by our mocks.
The POSIX stat() function might resolve to any number of different
symbols in the C library.
The may be an additional stat64() function exposed by the headers
too.
On 64-bit hosts the stat & stat64 functions are identical, always
refering to the 64-bit ABI.
On 32-bit hosts they refer to the 32-bit & 64-bit ABIs respectively.
Libvirt uses _FILE_OFFSET_BITS=64 on 32-bit hosts, which causes the
C library to transparently rewrite stat() calls to be stat64() calls.
Libvirt will never see the 32-bit ABI from the traditional stat()
call. We cannot assume this rewriting is done using a macro. It might
be, but on GLibC it is done with a magic __asm__ statement to apply
the rewrite at link time instead of at preprocessing.
In GLibC there may be two additional functions exposed by the headers,
__xstat() and __xstat64(). When these exist, stat() and stat64() are
transparently rewritten to call __xstat() and __xstat64() respectively.
The former symbols will not actally exist in the library at all, only
the header. The leading "__" indicates the symbols are a private impl
detail of the C library that applications should not care about.
Unfortunately, because we are trying to mock replace the C library,
we need to know about this internal impl detail.
With all this in mind the list of functions we have to mock will depend
on several factors
- If _FILE_OFFSET_BITS is set, then we are on a 32-bit host, and we
only need to mock stat64 and __xstat64. The other stat / __xstat
functions exist, but we'll never call them so they can be ignored
for mocking.
- If _FILE_OFFSET_BITS is not set, then we are on a 64-bit host and
we should mock stat, stat64, __xstat & __xstat64. Either may be
called by app code.
- If __xstat & __xstat64 exist, then stat & stat64 will not exist
as symbols in the library, so the latter should not be mocked.
The same all applies to lstat()
These rules are complex enough that we don't want to duplicate them
across every mock file, so this centralizes all the logic in a helper
file virmockstathelper.c that should be #included when needed. The
code merely need to provide a filename rewriting callback called
virMockStatRedirect(). Optionally VIR_MOCK_STAT_HOOK can be defined
as a macro if further processing is needed inline.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2019-04-01 16:47:25 +00:00
|
|
|
return real_open(newpath ? newpath : path, flags, mode);
|
2017-04-03 12:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DIR *opendir(const char *path)
|
|
|
|
{
|
2019-03-13 08:54:45 +00:00
|
|
|
VIR_AUTOFREE(char *) newpath = NULL;
|
2017-04-03 12:29:45 +00:00
|
|
|
|
|
|
|
PATH_OVERRIDE(newpath, path);
|
|
|
|
|
tests: fix mocking of stat() / lstat() functions
Quite a few of the tests have a need to mock the stat() / lstat()
functions and they are taking somewhat different & inconsistent
approaches none of which are actually fully correct. This is shown
by fact that 'make check' fails on 32-bit hosts. Investigation
revealed that the code was calling into the native C library impl,
not getting intercepted by our mocks.
The POSIX stat() function might resolve to any number of different
symbols in the C library.
The may be an additional stat64() function exposed by the headers
too.
On 64-bit hosts the stat & stat64 functions are identical, always
refering to the 64-bit ABI.
On 32-bit hosts they refer to the 32-bit & 64-bit ABIs respectively.
Libvirt uses _FILE_OFFSET_BITS=64 on 32-bit hosts, which causes the
C library to transparently rewrite stat() calls to be stat64() calls.
Libvirt will never see the 32-bit ABI from the traditional stat()
call. We cannot assume this rewriting is done using a macro. It might
be, but on GLibC it is done with a magic __asm__ statement to apply
the rewrite at link time instead of at preprocessing.
In GLibC there may be two additional functions exposed by the headers,
__xstat() and __xstat64(). When these exist, stat() and stat64() are
transparently rewritten to call __xstat() and __xstat64() respectively.
The former symbols will not actally exist in the library at all, only
the header. The leading "__" indicates the symbols are a private impl
detail of the C library that applications should not care about.
Unfortunately, because we are trying to mock replace the C library,
we need to know about this internal impl detail.
With all this in mind the list of functions we have to mock will depend
on several factors
- If _FILE_OFFSET_BITS is set, then we are on a 32-bit host, and we
only need to mock stat64 and __xstat64. The other stat / __xstat
functions exist, but we'll never call them so they can be ignored
for mocking.
- If _FILE_OFFSET_BITS is not set, then we are on a 64-bit host and
we should mock stat, stat64, __xstat & __xstat64. Either may be
called by app code.
- If __xstat & __xstat64 exist, then stat & stat64 will not exist
as symbols in the library, so the latter should not be mocked.
The same all applies to lstat()
These rules are complex enough that we don't want to duplicate them
across every mock file, so this centralizes all the logic in a helper
file virmockstathelper.c that should be #included when needed. The
code merely need to provide a filename rewriting callback called
virMockStatRedirect(). Optionally VIR_MOCK_STAT_HOOK can be defined
as a macro if further processing is needed inline.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2019-04-01 16:47:25 +00:00
|
|
|
return real_opendir(newpath ? newpath : path);
|
2017-04-03 12:29:45 +00:00
|
|
|
}
|
tests: fix mocking of stat() / lstat() functions
Quite a few of the tests have a need to mock the stat() / lstat()
functions and they are taking somewhat different & inconsistent
approaches none of which are actually fully correct. This is shown
by fact that 'make check' fails on 32-bit hosts. Investigation
revealed that the code was calling into the native C library impl,
not getting intercepted by our mocks.
The POSIX stat() function might resolve to any number of different
symbols in the C library.
The may be an additional stat64() function exposed by the headers
too.
On 64-bit hosts the stat & stat64 functions are identical, always
refering to the 64-bit ABI.
On 32-bit hosts they refer to the 32-bit & 64-bit ABIs respectively.
Libvirt uses _FILE_OFFSET_BITS=64 on 32-bit hosts, which causes the
C library to transparently rewrite stat() calls to be stat64() calls.
Libvirt will never see the 32-bit ABI from the traditional stat()
call. We cannot assume this rewriting is done using a macro. It might
be, but on GLibC it is done with a magic __asm__ statement to apply
the rewrite at link time instead of at preprocessing.
In GLibC there may be two additional functions exposed by the headers,
__xstat() and __xstat64(). When these exist, stat() and stat64() are
transparently rewritten to call __xstat() and __xstat64() respectively.
The former symbols will not actally exist in the library at all, only
the header. The leading "__" indicates the symbols are a private impl
detail of the C library that applications should not care about.
Unfortunately, because we are trying to mock replace the C library,
we need to know about this internal impl detail.
With all this in mind the list of functions we have to mock will depend
on several factors
- If _FILE_OFFSET_BITS is set, then we are on a 32-bit host, and we
only need to mock stat64 and __xstat64. The other stat / __xstat
functions exist, but we'll never call them so they can be ignored
for mocking.
- If _FILE_OFFSET_BITS is not set, then we are on a 64-bit host and
we should mock stat, stat64, __xstat & __xstat64. Either may be
called by app code.
- If __xstat & __xstat64 exist, then stat & stat64 will not exist
as symbols in the library, so the latter should not be mocked.
The same all applies to lstat()
These rules are complex enough that we don't want to duplicate them
across every mock file, so this centralizes all the logic in a helper
file virmockstathelper.c that should be #included when needed. The
code merely need to provide a filename rewriting callback called
virMockStatRedirect(). Optionally VIR_MOCK_STAT_HOOK can be defined
as a macro if further processing is needed inline.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2019-04-01 16:47:25 +00:00
|
|
|
|
2019-09-23 10:44:38 +00:00
|
|
|
int execv(const char *path, char *const argv[])
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) newpath = NULL;
|
|
|
|
|
|
|
|
PATH_OVERRIDE(newpath, path);
|
|
|
|
|
|
|
|
return real_execv(newpath ? newpath : path, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
int execve(const char *path, char *const argv[], char *const envp[])
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) newpath = NULL;
|
|
|
|
|
|
|
|
PATH_OVERRIDE(newpath, path);
|
|
|
|
|
|
|
|
return real_execve(newpath ? newpath : path, argv, envp);
|
|
|
|
}
|
|
|
|
|
2017-05-11 10:42:14 +00:00
|
|
|
#endif
|