2 * Copyright (C) 2014 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
21 #include <sys/types.h>
29 #include "virstring.h"
32 #define USB_SYSFS "/sys/bus/usb"
33 #define FAKE_USB_SYSFS "virusbtestdata/sys_bus_usb"
35 static int (*realopen
)(const char *pathname
, int flags
, ...);
36 static DIR *(*realopendir
)(const char *name
);
38 static void init_syms(void)
43 realopen
= dlsym(RTLD_NEXT
, "open");
44 realopendir
= dlsym(RTLD_NEXT
, "opendir");
45 if (!realopen
|| !realopendir
) {
46 fprintf(stderr
, "Error getting symbols");
51 static char *get_fake_path(const char *real_path
)
56 if ((p
= STRSKIP(real_path
, USB_SYSFS
)) &&
57 virAsprintfQuiet(&path
, "%s/%s/%s", abs_srcdir
, FAKE_USB_SYSFS
, p
) < 0)
59 else if (!p
&& VIR_STRDUP_QUIET(path
, real_path
) < 0)
69 DIR *opendir(const char *name
)
76 path
= get_fake_path(name
);
78 ret
= realopendir(path
);
83 int open(const char *pathname
, int flags
, ...)
92 path
= get_fake_path(pathname
);
96 /* The mode argument is mandatory when O_CREAT is set in flags,
97 * otherwise the argument is ignored.
99 if (flags
& O_CREAT
) {
101 mode
= (mode_t
) va_arg(ap
, int);
105 ret
= realopen(path
, flags
, mode
);