]>
git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/path.c
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/>.
30 #include <sys/types.h>
32 #include <shared/util.h>
34 #include "testsuite.h"
37 static const char *rootpath
;
38 static size_t rootpathlen
;
40 static inline bool need_trap(const char *path
)
42 return path
!= NULL
&& path
[0] == '/'
43 && !strstartswith(path
, ABS_TOP_BUILDDIR
);
46 static const char *trap_path(const char *path
, char buf
[PATH_MAX
* 2])
55 if (len
+ rootpathlen
> PATH_MAX
* 2) {
60 memcpy(buf
, rootpath
, rootpathlen
);
61 strcpy(buf
+ rootpathlen
, path
);
65 static bool get_rootpath(const char *f
)
70 rootpath
= getenv(S_TC_ROOTFS
);
71 if (rootpath
== NULL
) {
72 ERR("TRAP %s(): missing export %s?\n", f
, S_TC_ROOTFS
);
77 rootpathlen
= strlen(rootpath
);
82 static void *get_libc_func(const char *f
)
86 if (nextlib
== NULL
) {
90 nextlib
= dlopen("libc.so.6", RTLD_LAZY
);
94 fp
= dlsym(nextlib
, f
);
100 /* wrapper template for a function with one "const char* path" argument */
101 #define WRAP_1ARG(rettype, failret, name) \
102 TS_EXPORT rettype name(const char *path) \
105 char buf[PATH_MAX * 2]; \
106 static rettype (*_fn)(const char*); \
108 if (!get_rootpath(__func__)) \
110 _fn = get_libc_func(#name); \
111 p = trap_path(path, buf); \
117 /* wrapper template for a function with "const char* path" and another argument */
118 #define WRAP_2ARGS(rettype, failret, name, arg2t) \
119 TS_EXPORT rettype name(const char *path, arg2t arg2) \
122 char buf[PATH_MAX * 2]; \
123 static rettype (*_fn)(const char*, arg2t arg2); \
125 if (!get_rootpath(__func__)) \
127 _fn = get_libc_func(#name); \
128 p = trap_path(path, buf); \
131 return (*_fn)(p, arg2); \
134 /* wrapper template for open family */
135 #define WRAP_OPEN(suffix) \
136 TS_EXPORT int open ## suffix (const char *path, int flags, ...) \
139 char buf[PATH_MAX * 2]; \
140 static int (*_fn)(const char *path, int flags, ...); \
142 if (!get_rootpath(__func__)) \
144 _fn = get_libc_func("open" #suffix); \
145 p = trap_path(path, buf); \
149 if (flags & O_CREAT) { \
153 va_start(ap, flags); \
154 mode = va_arg(ap, mode_t); \
156 return _fn(p, flags, mode); \
159 return _fn(p, flags); \
162 /* wrapper template for __xstat family */
163 #define WRAP_VERSTAT(prefix, suffix) \
164 TS_EXPORT int prefix ## stat ## suffix (int ver, \
166 struct stat ## suffix *st) \
169 char buf[PATH_MAX * 2]; \
170 static int (*_fn)(int ver, const char *path, \
171 struct stat ## suffix *); \
172 _fn = get_libc_func(#prefix "stat" #suffix); \
174 if (!get_rootpath(__func__)) \
176 p = trap_path(path, buf); \
180 return _fn(ver, p, st); \
183 WRAP_1ARG(DIR*, NULL
, opendir
);
185 WRAP_2ARGS(FILE*, NULL
, fopen
, const char*);
186 WRAP_2ARGS(int, -1, mkdir
, mode_t
);
187 WRAP_2ARGS(int, -1, access
, int);
188 WRAP_2ARGS(int, -1, stat
, struct stat
*);
189 WRAP_2ARGS(int, -1, lstat
, struct stat
*);
190 #ifndef _FILE_OFFSET_BITS
191 WRAP_2ARGS(int, -1, stat64
, struct stat64
*);
192 WRAP_2ARGS(int, -1, lstat64
, struct stat64
*);
201 #ifndef _FILE_OFFSET_BITS
202 WRAP_VERSTAT(__x
,64);
203 WRAP_VERSTAT(__lx
,64);