]>
git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/path.c
2 * Copyright (C) 2012 ProFUSION embedded systems
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include <sys/types.h>
32 #include "testsuite.h"
35 static const char *rootpath
;
36 static size_t rootpathlen
;
38 static inline bool need_trap(const char *path
)
40 return path
!= NULL
&& path
[0] == '/';
43 static const char *trap_path(const char *path
, char buf
[PATH_MAX
* 2])
52 if (len
+ rootpathlen
> PATH_MAX
* 2) {
57 memcpy(buf
, rootpath
, rootpathlen
);
58 strcpy(buf
+ rootpathlen
, path
);
62 static bool get_rootpath(const char *f
)
67 rootpath
= getenv(S_TC_ROOTFS
);
68 if (rootpath
== NULL
) {
69 ERR("TRAP %s(): missing export %s?\n", f
, S_TC_ROOTFS
);
74 rootpathlen
= strlen(rootpath
);
79 static void *get_libc_func(const char *f
)
83 if (nextlib
== NULL
) {
87 nextlib
= dlopen("libc.so.6", RTLD_LAZY
);
91 fp
= dlsym(nextlib
, f
);
97 TS_EXPORT
FILE *fopen(const char *path
, const char *mode
)
100 char buf
[PATH_MAX
* 2];
101 static int (*_fopen
)(const char *path
, const char *mode
);
103 if (!get_rootpath(__func__
))
106 _fopen
= get_libc_func("fopen");
108 p
= trap_path(path
, buf
);
112 return (void *) (long) (*_fopen
)(p
, mode
);
115 TS_EXPORT
int open(const char *path
, int flags
, ...)
118 char buf
[PATH_MAX
* 2];
119 static int (*_open
)(const char *path
, int flags
, ...);
121 if (!get_rootpath(__func__
))
124 _open
= get_libc_func("open");
125 p
= trap_path(path
, buf
);
129 if (flags
& O_CREAT
) {
134 mode
= va_arg(ap
, mode_t
);
136 _open(p
, flags
, mode
);
139 return _open(p
, flags
);
142 TS_EXPORT
int stat(const char *path
, struct stat
*st
)
145 char buf
[PATH_MAX
* 2];
146 static int (*_stat
)(const char *path
, struct stat
*buf
);
148 if (!get_rootpath(__func__
))
151 _stat
= get_libc_func("stat");
153 p
= trap_path(path
, buf
);
161 TS_EXPORT
int __xstat(int ver
, const char *path
, struct stat
*st
)
164 char buf
[PATH_MAX
* 2];
165 static int (*_xstat
)(int __ver
, const char *__filename
,
166 struct stat
*__stat_buf
);
168 if (!get_rootpath(__func__
))
171 _xstat
= get_libc_func("__xstat");
173 p
= trap_path(path
, buf
);
177 return _xstat(ver
, p
, st
);
181 TS_EXPORT
int access(const char *path
, int mode
)
184 char buf
[PATH_MAX
* 2];
185 static int (*_access
)(const char *path
, int mode
);
187 if (!get_rootpath(__func__
))
190 _access
= get_libc_func("access");
192 p
= trap_path(path
, buf
);
196 return _access(p
, mode
);
199 TS_EXPORT
DIR *opendir(const char *path
)
202 char buf
[PATH_MAX
* 2];
203 static int (*_opendir
)(const char *path
);
205 if (!get_rootpath(__func__
))
208 _opendir
= get_libc_func("opendir");
210 p
= trap_path(path
, buf
);
214 return (void *)(long)(*_opendir
)(p
);