]>
git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/path.c
c1ae4986b865ac314b2830f21265915fff95634b
2 * Copyright (C) 2012-2013 ProFUSION embedded systems
4 * This program 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 program 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 <http://www.gnu.org/licenses/>.
18 /* We unset _FILE_OFFSET_BITS here so we can override both stat and stat64 on
19 * 32-bit architectures and forward each to the right libc function */
20 #undef _FILE_OFFSET_BITS
34 #include <sys/types.h>
36 #include <shared/util.h>
38 #include "testsuite.h"
41 static const char *rootpath
;
42 static size_t rootpathlen
;
44 static inline bool need_trap(const char *path
)
46 return path
!= NULL
&& path
[0] == '/'
47 && !strstartswith(path
, ABS_TOP_BUILDDIR
);
50 static const char *trap_path(const char *path
, char buf
[PATH_MAX
* 2])
59 if (len
+ rootpathlen
> PATH_MAX
* 2) {
64 memcpy(buf
, rootpath
, rootpathlen
);
65 strcpy(buf
+ rootpathlen
, path
);
69 static bool get_rootpath(const char *f
)
74 rootpath
= getenv(S_TC_ROOTFS
);
75 if (rootpath
== NULL
) {
76 ERR("TRAP %s(): missing export %s?\n", f
, S_TC_ROOTFS
);
81 rootpathlen
= strlen(rootpath
);
86 static void *get_libc_func(const char *f
)
90 if (nextlib
== NULL
) {
94 nextlib
= dlopen("libc.so.6", RTLD_LAZY
);
98 fp
= dlsym(nextlib
, f
);
104 /* wrapper template for a function with one "const char* path" argument */
105 #define WRAP_1ARG(rettype, failret, name) \
106 TS_EXPORT rettype name(const char *path) \
109 char buf[PATH_MAX * 2]; \
110 static rettype (*_fn)(const char*); \
112 if (!get_rootpath(__func__)) \
114 _fn = get_libc_func(#name); \
115 p = trap_path(path, buf); \
121 /* wrapper template for a function with "const char* path" and another argument */
122 #define WRAP_2ARGS(rettype, failret, name, arg2t) \
123 TS_EXPORT rettype name(const char *path, arg2t arg2) \
126 char buf[PATH_MAX * 2]; \
127 static rettype (*_fn)(const char*, arg2t arg2); \
129 if (!get_rootpath(__func__)) \
131 _fn = get_libc_func(#name); \
132 p = trap_path(path, buf); \
135 return (*_fn)(p, arg2); \
138 /* wrapper template for open family */
139 #define WRAP_OPEN(suffix) \
140 TS_EXPORT int open ## suffix (const char *path, int flags, ...) \
143 char buf[PATH_MAX * 2]; \
144 static int (*_fn)(const char *path, int flags, ...); \
146 if (!get_rootpath(__func__)) \
148 _fn = get_libc_func("open" #suffix); \
149 p = trap_path(path, buf); \
153 if (flags & O_CREAT) { \
157 va_start(ap, flags); \
158 mode = va_arg(ap, mode_t); \
160 return _fn(p, flags, mode); \
163 return _fn(p, flags); \
166 /* wrapper template for __xstat family */
167 #define WRAP_VERSTAT(prefix, suffix) \
168 TS_EXPORT int prefix ## stat ## suffix (int ver, \
170 struct stat ## suffix *st) \
173 char buf[PATH_MAX * 2]; \
174 static int (*_fn)(int ver, const char *path, \
175 struct stat ## suffix *); \
176 _fn = get_libc_func(#prefix "stat" #suffix); \
178 if (!get_rootpath(__func__)) \
180 p = trap_path(path, buf); \
184 return _fn(ver, p, st); \
187 WRAP_1ARG(DIR*, NULL
, opendir
);
188 WRAP_1ARG(int, -1, chdir
);
190 WRAP_2ARGS(FILE*, NULL
, fopen
, const char*);
191 WRAP_2ARGS(FILE*, NULL
, fopen64
, const char*);
192 WRAP_2ARGS(int, -1, mkdir
, mode_t
);
193 WRAP_2ARGS(int, -1, access
, int);
194 WRAP_2ARGS(int, -1, stat
, struct stat
*);
195 WRAP_2ARGS(int, -1, lstat
, struct stat
*);
196 WRAP_2ARGS(int, -1, stat64
, struct stat64
*);
197 WRAP_2ARGS(int, -1, lstat64
, struct stat64
*);
205 WRAP_VERSTAT(__x
,64);
206 WRAP_VERSTAT(__lx
,64);