virpcimock: Adapt to virmock.h

Instead of introducing our own wrapper for dlsym()
we can use the one provided by virmock.h.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Michal Privoznik 2016-05-13 12:32:00 +02:00
parent 9055faebd4
commit 57c484dbac
2 changed files with 50 additions and 55 deletions

View File

@ -292,4 +292,14 @@
} \ } \
} while (0) } while (0)
# define VIR_MOCK_REAL_INIT_ALT(name1, name2) \
do { \
if (!(real_ ## name1 = dlsym(RTLD_NEXT, #name1)) && \
!(real_ ## name2 = dlsym(RTLD_NEXT, #name2))) { \
fprintf(stderr, "Cannot find real '%s' or '%s' symbol\n", \
#name1, #name2); \
abort(); \
} \
} while (0)
#endif /* __VIR_MOCK_H__ */ #endif /* __VIR_MOCK_H__ */

View File

@ -21,9 +21,8 @@
#include <config.h> #include <config.h>
#ifdef __linux__ #ifdef __linux__
# include "internal.h" # include "virmock.h"
# include <stdio.h> # include <stdio.h>
# include <dlfcn.h>
# include <stdlib.h> # include <stdlib.h>
# include <unistd.h> # include <unistd.h>
# include <fcntl.h> # include <fcntl.h>
@ -35,15 +34,15 @@
# include "virfile.h" # include "virfile.h"
# include "dirname.h" # include "dirname.h"
static int (*realaccess)(const char *path, int mode); static int (*real_access)(const char *path, int mode);
static int (*reallstat)(const char *path, struct stat *sb); static int (*real_lstat)(const char *path, struct stat *sb);
static int (*real__lxstat)(int ver, const char *path, struct stat *sb); static int (*real___lxstat)(int ver, const char *path, struct stat *sb);
static int (*realstat)(const char *path, struct stat *sb); static int (*real_stat)(const char *path, struct stat *sb);
static int (*real__xstat)(int ver, const char *path, struct stat *sb); static int (*real___xstat)(int ver, const char *path, struct stat *sb);
static char *(*realcanonicalize_file_name)(const char *path); static char *(*real_canonicalize_file_name)(const char *path);
static int (*realopen)(const char *path, int flags, ...); static int (*real_open)(const char *path, int flags, ...);
static int (*realclose)(int fd); static int (*real_close)(int fd);
static DIR * (*realopendir)(const char *name); static DIR * (*real_opendir)(const char *name);
/* Don't make static, since it causes problems with clang /* Don't make static, since it causes problems with clang
* when passed as an arg to virAsprintf() * when passed as an arg to virAsprintf()
@ -181,7 +180,7 @@ make_file(const char *path,
if (virAsprintfQuiet(&filepath, "%s/%s", path, name) < 0) if (virAsprintfQuiet(&filepath, "%s/%s", path, name) < 0)
ABORT_OOM(); ABORT_OOM();
if ((fd = realopen(filepath, O_CREAT|O_WRONLY, 0666)) < 0) if ((fd = real_open(filepath, O_CREAT|O_WRONLY, 0666)) < 0)
ABORT("Unable to open: %s", filepath); ABORT("Unable to open: %s", filepath);
if (value && safewrite(fd, value, len) != len) if (value && safewrite(fd, value, len) != len)
@ -207,7 +206,7 @@ pci_read_file(const char *path,
goto cleanup; goto cleanup;
} }
if ((fd = realopen(newpath, O_RDWR)) < 0) if ((fd = real_open(newpath, O_RDWR)) < 0)
goto cleanup; goto cleanup;
bzero(buf, buf_size); bzero(buf, buf_size);
@ -222,7 +221,7 @@ pci_read_file(const char *path,
ret = 0; ret = 0;
cleanup: cleanup:
VIR_FREE(newpath); VIR_FREE(newpath);
realclose(fd); real_close(fd);
return ret; return ret;
} }
@ -354,8 +353,8 @@ pci_device_new_from_stub(const struct pciDevice *data)
/* If there is a config file for the device within virpcitestdata dir, /* If there is a config file for the device within virpcitestdata dir,
* symlink it. Otherwise create a dummy config file. */ * symlink it. Otherwise create a dummy config file. */
if ((realstat && realstat(configSrc, &sb) == 0) || if ((real_stat && real_stat(configSrc, &sb) == 0) ||
(real__xstat && real__xstat(_STAT_VER, configSrc, &sb) == 0)) { (real___xstat && real___xstat(_STAT_VER, configSrc, &sb) == 0)) {
/* On success, copy @configSrc into the destination (a copy, /* On success, copy @configSrc into the destination (a copy,
* rather than a symlink, is required since we write into the * rather than a symlink, is required since we write into the
* file, and parallel VPATH builds must not stomp on the * file, and parallel VPATH builds must not stomp on the
@ -772,30 +771,16 @@ pci_driver_handle_remove_id(const char *path)
static void static void
init_syms(void) init_syms(void)
{ {
if (realaccess) if (real_access)
return; return;
# define LOAD_SYM(name) \ VIR_MOCK_REAL_INIT(access);
do { \ VIR_MOCK_REAL_INIT_ALT(lstat, __lxstat);
if (!(real ## name = dlsym(RTLD_NEXT, #name))) \ VIR_MOCK_REAL_INIT_ALT(stat, __xstat);
ABORT("Cannot find real '%s' symbol\n", #name); \ VIR_MOCK_REAL_INIT(canonicalize_file_name);
} while (0) VIR_MOCK_REAL_INIT(open);
VIR_MOCK_REAL_INIT(close);
# define LOAD_SYM_ALT(name1, name2) \ VIR_MOCK_REAL_INIT(opendir);
do { \
if (!(real ## name1 = dlsym(RTLD_NEXT, #name1)) && \
!(real ## name2 = dlsym(RTLD_NEXT, #name2))) \
ABORT("Cannot find real '%s' or '%s' symbol\n", \
#name1, #name2); \
} while (0)
LOAD_SYM(access);
LOAD_SYM_ALT(lstat, __lxstat);
LOAD_SYM_ALT(stat, __xstat);
LOAD_SYM(canonicalize_file_name);
LOAD_SYM(open);
LOAD_SYM(close);
LOAD_SYM(opendir);
} }
static void static void
@ -865,10 +850,10 @@ access(const char *path, int mode)
char *newpath; char *newpath;
if (getrealpath(&newpath, path) < 0) if (getrealpath(&newpath, path) < 0)
return -1; return -1;
ret = realaccess(newpath, mode); ret = real_access(newpath, mode);
VIR_FREE(newpath); VIR_FREE(newpath);
} else { } else {
ret = realaccess(path, mode); ret = real_access(path, mode);
} }
return ret; return ret;
} }
@ -884,10 +869,10 @@ __lxstat(int ver, const char *path, struct stat *sb)
char *newpath; char *newpath;
if (getrealpath(&newpath, path) < 0) if (getrealpath(&newpath, path) < 0)
return -1; return -1;
ret = real__lxstat(ver, newpath, sb); ret = real___lxstat(ver, newpath, sb);
VIR_FREE(newpath); VIR_FREE(newpath);
} else { } else {
ret = real__lxstat(ver, path, sb); ret = real___lxstat(ver, path, sb);
} }
return ret; return ret;
} }
@ -903,10 +888,10 @@ lstat(const char *path, struct stat *sb)
char *newpath; char *newpath;
if (getrealpath(&newpath, path) < 0) if (getrealpath(&newpath, path) < 0)
return -1; return -1;
ret = reallstat(newpath, sb); ret = real_lstat(newpath, sb);
VIR_FREE(newpath); VIR_FREE(newpath);
} else { } else {
ret = reallstat(path, sb); ret = real_lstat(path, sb);
} }
return ret; return ret;
} }
@ -922,10 +907,10 @@ __xstat(int ver, const char *path, struct stat *sb)
char *newpath; char *newpath;
if (getrealpath(&newpath, path) < 0) if (getrealpath(&newpath, path) < 0)
return -1; return -1;
ret = real__xstat(ver, newpath, sb); ret = real___xstat(ver, newpath, sb);
VIR_FREE(newpath); VIR_FREE(newpath);
} else { } else {
ret = real__xstat(ver, path, sb); ret = real___xstat(ver, path, sb);
} }
return ret; return ret;
} }
@ -941,10 +926,10 @@ stat(const char *path, struct stat *sb)
char *newpath; char *newpath;
if (getrealpath(&newpath, path) < 0) if (getrealpath(&newpath, path) < 0)
return -1; return -1;
ret = realstat(newpath, sb); ret = real_stat(newpath, sb);
VIR_FREE(newpath); VIR_FREE(newpath);
} else { } else {
ret = realstat(path, sb); ret = real_stat(path, sb);
} }
return ret; return ret;
} }
@ -960,10 +945,10 @@ canonicalize_file_name(const char *path)
char *newpath; char *newpath;
if (getrealpath(&newpath, path) < 0) if (getrealpath(&newpath, path) < 0)
return NULL; return NULL;
ret = realcanonicalize_file_name(newpath); ret = real_canonicalize_file_name(newpath);
VIR_FREE(newpath); VIR_FREE(newpath);
} else { } else {
ret = realcanonicalize_file_name(path); ret = real_canonicalize_file_name(path);
} }
return ret; return ret;
} }
@ -986,16 +971,16 @@ open(const char *path, int flags, ...)
va_start(ap, flags); va_start(ap, flags);
mode = va_arg(ap, mode_t); mode = va_arg(ap, mode_t);
va_end(ap); va_end(ap);
ret = realopen(newpath ? newpath : path, flags, mode); ret = real_open(newpath ? newpath : path, flags, mode);
} else { } else {
ret = realopen(newpath ? newpath : path, flags); ret = real_open(newpath ? newpath : path, flags);
} }
/* Catch both: /sys/bus/pci/drivers/... and /* Catch both: /sys/bus/pci/drivers/... and
* /sys/bus/pci/device/.../driver/... */ * /sys/bus/pci/device/.../driver/... */
if (ret >= 0 && STRPREFIX(path, SYSFS_PCI_PREFIX) && if (ret >= 0 && STRPREFIX(path, SYSFS_PCI_PREFIX) &&
strstr(path, "driver") && add_fd(ret, path) < 0) { strstr(path, "driver") && add_fd(ret, path) < 0) {
realclose(ret); real_close(ret);
ret = -1; ret = -1;
} }
@ -1015,7 +1000,7 @@ opendir(const char *path)
getrealpath(&newpath, path) < 0) getrealpath(&newpath, path) < 0)
return NULL; return NULL;
ret = realopendir(newpath ? newpath : path); ret = real_opendir(newpath ? newpath : path);
VIR_FREE(newpath); VIR_FREE(newpath);
return ret; return ret;
@ -1026,7 +1011,7 @@ close(int fd)
{ {
if (remove_fd(fd) < 0) if (remove_fd(fd) < 0)
return -1; return -1;
return realclose(fd); return real_close(fd);
} }
#else #else
/* Nothing to override on non-__linux__ platforms */ /* Nothing to override on non-__linux__ platforms */