]>
git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/path.c
f87f5c52d8faf9391d3ac78d4a8225f71732a58c
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, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/types.h>
33 #include "testsuite.h"
36 static const char *rootpath
;
37 static size_t rootpathlen
;
39 static inline bool need_trap(const char *path
)
41 return path
!= NULL
&& path
[0] == '/';
44 static const char *trap_path(const char *path
, char buf
[PATH_MAX
* 2])
53 if (len
+ rootpathlen
> PATH_MAX
* 2) {
58 memcpy(buf
, rootpath
, rootpathlen
);
59 strcpy(buf
+ rootpathlen
, path
);
63 static bool get_rootpath(const char *f
)
68 rootpath
= getenv(S_TC_ROOTFS
);
69 if (rootpath
== NULL
) {
70 ERR("TRAP %s(): missing export %s?\n", f
, S_TC_ROOTFS
);
75 rootpathlen
= strlen(rootpath
);
80 static void *get_libc_func(const char *f
)
84 if (nextlib
== NULL
) {
88 nextlib
= dlopen("libc.so.6", RTLD_LAZY
);
92 fp
= dlsym(nextlib
, f
);
98 /* wrapper template for a function with one "const char* path" argument */
99 #define WRAP_1ARG(rettype, failret, name) \
100 TS_EXPORT rettype name(const char *path) \
103 char buf[PATH_MAX * 2]; \
104 static rettype (*_fn)(const char*); \
106 if (!get_rootpath(__func__)) \
108 _fn = get_libc_func(#name); \
109 p = trap_path(path, buf); \
115 /* wrapper template for a function with "const char* path" and another argument */
116 #define WRAP_2ARGS(rettype, failret, name, arg2t) \
117 TS_EXPORT rettype name(const char *path, arg2t arg2) \
120 char buf[PATH_MAX * 2]; \
121 static rettype (*_fn)(const char*, arg2t arg2); \
123 if (!get_rootpath(__func__)) \
125 _fn = get_libc_func(#name); \
126 p = trap_path(path, buf); \
129 return (*_fn)(p, arg2); \
132 /* wrapper template for open family */
133 #define WRAP_OPEN(suffix) \
134 TS_EXPORT int open ## suffix (const char *path, int flags, ...) \
137 char buf[PATH_MAX * 2]; \
138 static int (*_fn)(const char *path, int flags, ...); \
140 if (!get_rootpath(__func__)) \
142 _fn = get_libc_func("open" #suffix); \
143 p = trap_path(path, buf); \
147 if (flags & O_CREAT) { \
151 va_start(ap, flags); \
152 mode = va_arg(ap, mode_t); \
154 return _fn(p, flags, mode); \
157 return _fn(p, flags); \
160 /* wrapper template for __xstat family */
161 #define WRAP_VERSTAT(prefix, suffix) \
162 TS_EXPORT int prefix ## stat ## suffix (int ver, \
164 struct stat ## suffix *st) \
167 char buf[PATH_MAX * 2]; \
168 static int (*_fn)(int ver, const char *path, \
169 struct stat ## suffix *); \
170 _fn = get_libc_func(#prefix "stat" #suffix); \
172 if (!get_rootpath(__func__)) \
174 p = trap_path(path, buf); \
178 return _fn(ver, p, st); \
181 WRAP_1ARG(DIR*, NULL
, opendir
);
183 WRAP_2ARGS(FILE*, NULL
, fopen
, const char*);
184 WRAP_2ARGS(int, -1, mkdir
, mode_t
);
185 WRAP_2ARGS(int, -1, access
, int);
186 WRAP_2ARGS(int, -1, stat
, struct stat
*);
187 WRAP_2ARGS(int, -1, lstat
, struct stat
*);
188 #ifndef _FILE_OFFSET_BITS
189 WRAP_2ARGS(int, -1, stat64
, struct stat64
*);
190 WRAP_2ARGS(int, -1, lstat64
, struct stat64
*);
199 #ifndef _FILE_OFFSET_BITS
200 WRAP_VERSTAT(__x
,64);
201 WRAP_VERSTAT(__lx
,64);