]> git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/path.c
c1ae4986b865ac314b2830f21265915fff95634b
[thirdparty/kmod.git] / testsuite / path.c
1 /*
2 * Copyright (C) 2012-2013 ProFUSION embedded systems
3 *
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.
8 *
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.
13 *
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/>.
16 */
17
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
21
22 #include <assert.h>
23 #include <dirent.h>
24 #include <dlfcn.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35
36 #include <shared/util.h>
37
38 #include "testsuite.h"
39
40 static void *nextlib;
41 static const char *rootpath;
42 static size_t rootpathlen;
43
44 static inline bool need_trap(const char *path)
45 {
46 return path != NULL && path[0] == '/'
47 && !strstartswith(path, ABS_TOP_BUILDDIR);
48 }
49
50 static const char *trap_path(const char *path, char buf[PATH_MAX * 2])
51 {
52 size_t len;
53
54 if (!need_trap(path))
55 return path;
56
57 len = strlen(path);
58
59 if (len + rootpathlen > PATH_MAX * 2) {
60 errno = ENAMETOOLONG;
61 return NULL;
62 }
63
64 memcpy(buf, rootpath, rootpathlen);
65 strcpy(buf + rootpathlen, path);
66 return buf;
67 }
68
69 static bool get_rootpath(const char *f)
70 {
71 if (rootpath != NULL)
72 return true;
73
74 rootpath = getenv(S_TC_ROOTFS);
75 if (rootpath == NULL) {
76 ERR("TRAP %s(): missing export %s?\n", f, S_TC_ROOTFS);
77 errno = ENOENT;
78 return false;
79 }
80
81 rootpathlen = strlen(rootpath);
82
83 return true;
84 }
85
86 static void *get_libc_func(const char *f)
87 {
88 void *fp;
89
90 if (nextlib == NULL) {
91 #ifdef RTLD_NEXT
92 nextlib = RTLD_NEXT;
93 #else
94 nextlib = dlopen("libc.so.6", RTLD_LAZY);
95 #endif
96 }
97
98 fp = dlsym(nextlib, f);
99 assert(fp);
100
101 return fp;
102 }
103
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) \
107 { \
108 const char *p; \
109 char buf[PATH_MAX * 2]; \
110 static rettype (*_fn)(const char*); \
111 \
112 if (!get_rootpath(__func__)) \
113 return failret; \
114 _fn = get_libc_func(#name); \
115 p = trap_path(path, buf); \
116 if (p == NULL) \
117 return failret; \
118 return (*_fn)(p); \
119 }
120
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) \
124 { \
125 const char *p; \
126 char buf[PATH_MAX * 2]; \
127 static rettype (*_fn)(const char*, arg2t arg2); \
128 \
129 if (!get_rootpath(__func__)) \
130 return failret; \
131 _fn = get_libc_func(#name); \
132 p = trap_path(path, buf); \
133 if (p == NULL) \
134 return failret; \
135 return (*_fn)(p, arg2); \
136 }
137
138 /* wrapper template for open family */
139 #define WRAP_OPEN(suffix) \
140 TS_EXPORT int open ## suffix (const char *path, int flags, ...) \
141 { \
142 const char *p; \
143 char buf[PATH_MAX * 2]; \
144 static int (*_fn)(const char *path, int flags, ...); \
145 \
146 if (!get_rootpath(__func__)) \
147 return -1; \
148 _fn = get_libc_func("open" #suffix); \
149 p = trap_path(path, buf); \
150 if (p == NULL) \
151 return -1; \
152 \
153 if (flags & O_CREAT) { \
154 mode_t mode; \
155 va_list ap; \
156 \
157 va_start(ap, flags); \
158 mode = va_arg(ap, mode_t); \
159 va_end(ap); \
160 return _fn(p, flags, mode); \
161 } \
162 \
163 return _fn(p, flags); \
164 }
165
166 /* wrapper template for __xstat family */
167 #define WRAP_VERSTAT(prefix, suffix) \
168 TS_EXPORT int prefix ## stat ## suffix (int ver, \
169 const char *path, \
170 struct stat ## suffix *st) \
171 { \
172 const char *p; \
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); \
177 \
178 if (!get_rootpath(__func__)) \
179 return -1; \
180 p = trap_path(path, buf); \
181 if (p == NULL) \
182 return -1; \
183 \
184 return _fn(ver, p, st); \
185 }
186
187 WRAP_1ARG(DIR*, NULL, opendir);
188 WRAP_1ARG(int, -1, chdir);
189
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*);
198 WRAP_OPEN(64);
199
200 WRAP_OPEN();
201
202 #ifdef HAVE___XSTAT
203 WRAP_VERSTAT(__x,);
204 WRAP_VERSTAT(__lx,);
205 WRAP_VERSTAT(__x,64);
206 WRAP_VERSTAT(__lx,64);
207 #endif