2014-02-26 09:25:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2024-03-13 16:35:15 +00:00
|
|
|
#include "virmock.h"
|
2014-02-26 09:25:30 +00:00
|
|
|
|
|
|
|
#define USB_SYSFS "/sys/bus/usb"
|
|
|
|
#define FAKE_USB_SYSFS "virusbtestdata/sys_bus_usb"
|
|
|
|
|
2024-03-13 16:35:15 +00:00
|
|
|
static int (*real_open)(const char *pathname, int flags, ...);
|
2024-03-13 16:25:35 +00:00
|
|
|
#if WITH___OPEN_2
|
|
|
|
static int (*real___open_2)(const char *path, int flags);
|
|
|
|
#endif
|
2024-03-13 16:35:15 +00:00
|
|
|
static DIR *(*real_opendir)(const char *name);
|
2014-02-26 09:25:30 +00:00
|
|
|
|
|
|
|
static void init_syms(void)
|
|
|
|
{
|
2024-03-13 16:35:15 +00:00
|
|
|
if (real_open)
|
2014-02-26 09:25:30 +00:00
|
|
|
return;
|
|
|
|
|
2024-03-13 16:35:15 +00:00
|
|
|
VIR_MOCK_REAL_INIT(open);
|
2024-03-13 16:25:35 +00:00
|
|
|
#if WITH___OPEN_2
|
|
|
|
VIR_MOCK_REAL_INIT(__open_2);
|
|
|
|
#endif
|
2024-03-13 16:35:15 +00:00
|
|
|
VIR_MOCK_REAL_INIT(opendir);
|
2014-02-26 09:25:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *get_fake_path(const char *real_path)
|
|
|
|
{
|
|
|
|
const char *p = NULL;
|
|
|
|
char *path = NULL;
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
if ((p = STRSKIP(real_path, USB_SYSFS)))
|
|
|
|
path = g_strdup_printf("%s/%s/%s", abs_srcdir, FAKE_USB_SYSFS, p);
|
2019-10-20 11:49:46 +00:00
|
|
|
else if (!p)
|
|
|
|
path = g_strdup(real_path);
|
2014-02-26 09:25:30 +00:00
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
DIR *opendir(const char *name)
|
|
|
|
{
|
2021-09-04 20:37:31 +00:00
|
|
|
g_autofree char *path = NULL;
|
2014-02-26 09:25:30 +00:00
|
|
|
|
|
|
|
init_syms();
|
|
|
|
|
|
|
|
path = get_fake_path(name);
|
|
|
|
|
2024-03-13 16:35:15 +00:00
|
|
|
return real_opendir(path);
|
2014-02-26 09:25:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int open(const char *pathname, int flags, ...)
|
|
|
|
{
|
2021-09-04 20:37:31 +00:00
|
|
|
g_autofree char *path = NULL;
|
2014-02-26 09:25:30 +00:00
|
|
|
int ret;
|
2017-09-25 18:27:07 +00:00
|
|
|
va_list ap;
|
|
|
|
mode_t mode = 0;
|
2014-02-26 09:25:30 +00:00
|
|
|
|
|
|
|
init_syms();
|
|
|
|
|
|
|
|
path = get_fake_path(pathname);
|
|
|
|
if (!path)
|
|
|
|
return -1;
|
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);
|
|
|
|
}
|
|
|
|
|
2024-03-13 16:35:15 +00:00
|
|
|
ret = real_open(path, flags, mode);
|
2014-02-26 09:25:30 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2024-03-13 16:25:35 +00:00
|
|
|
|
|
|
|
#if WITH___OPEN_2
|
|
|
|
int
|
|
|
|
__open_2(const char *pathname, int flags)
|
|
|
|
{
|
|
|
|
g_autofree char *path = NULL;
|
|
|
|
|
|
|
|
init_syms();
|
|
|
|
|
|
|
|
path = get_fake_path(pathname);
|
|
|
|
if (!path)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return real_open(path, flags);
|
|
|
|
}
|
|
|
|
#endif
|