]> git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/path.c
f87f5c52d8faf9391d3ac78d4a8225f71732a58c
[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, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <assert.h>
20 #include <errno.h>
21 #include <dirent.h>
22 #include <fcntl.h>
23 #include <dlfcn.h>
24 #include <limits.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32
33 #include "testsuite.h"
34
35 static void *nextlib;
36 static const char *rootpath;
37 static size_t rootpathlen;
38
39 static inline bool need_trap(const char *path)
40 {
41 return path != NULL && path[0] == '/';
42 }
43
44 static const char *trap_path(const char *path, char buf[PATH_MAX * 2])
45 {
46 size_t len;
47
48 if (!need_trap(path))
49 return path;
50
51 len = strlen(path);
52
53 if (len + rootpathlen > PATH_MAX * 2) {
54 errno = ENAMETOOLONG;
55 return NULL;
56 }
57
58 memcpy(buf, rootpath, rootpathlen);
59 strcpy(buf + rootpathlen, path);
60 return buf;
61 }
62
63 static bool get_rootpath(const char *f)
64 {
65 if (rootpath != NULL)
66 return true;
67
68 rootpath = getenv(S_TC_ROOTFS);
69 if (rootpath == NULL) {
70 ERR("TRAP %s(): missing export %s?\n", f, S_TC_ROOTFS);
71 errno = ENOENT;
72 return false;
73 }
74
75 rootpathlen = strlen(rootpath);
76
77 return true;
78 }
79
80 static void *get_libc_func(const char *f)
81 {
82 void *fp;
83
84 if (nextlib == NULL) {
85 #ifdef RTLD_NEXT
86 nextlib = RTLD_NEXT;
87 #else
88 nextlib = dlopen("libc.so.6", RTLD_LAZY);
89 #endif
90 }
91
92 fp = dlsym(nextlib, f);
93 assert(fp);
94
95 return fp;
96 }
97
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) \
101 { \
102 const char *p; \
103 char buf[PATH_MAX * 2]; \
104 static rettype (*_fn)(const char*); \
105 \
106 if (!get_rootpath(__func__)) \
107 return failret; \
108 _fn = get_libc_func(#name); \
109 p = trap_path(path, buf); \
110 if (p == NULL) \
111 return failret; \
112 return (*_fn)(p); \
113 }
114
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) \
118 { \
119 const char *p; \
120 char buf[PATH_MAX * 2]; \
121 static rettype (*_fn)(const char*, arg2t arg2); \
122 \
123 if (!get_rootpath(__func__)) \
124 return failret; \
125 _fn = get_libc_func(#name); \
126 p = trap_path(path, buf); \
127 if (p == NULL) \
128 return failret; \
129 return (*_fn)(p, arg2); \
130 }
131
132 /* wrapper template for open family */
133 #define WRAP_OPEN(suffix) \
134 TS_EXPORT int open ## suffix (const char *path, int flags, ...) \
135 { \
136 const char *p; \
137 char buf[PATH_MAX * 2]; \
138 static int (*_fn)(const char *path, int flags, ...); \
139 \
140 if (!get_rootpath(__func__)) \
141 return -1; \
142 _fn = get_libc_func("open" #suffix); \
143 p = trap_path(path, buf); \
144 if (p == NULL) \
145 return -1; \
146 \
147 if (flags & O_CREAT) { \
148 mode_t mode; \
149 va_list ap; \
150 \
151 va_start(ap, flags); \
152 mode = va_arg(ap, mode_t); \
153 va_end(ap); \
154 return _fn(p, flags, mode); \
155 } \
156 \
157 return _fn(p, flags); \
158 }
159
160 /* wrapper template for __xstat family */
161 #define WRAP_VERSTAT(prefix, suffix) \
162 TS_EXPORT int prefix ## stat ## suffix (int ver, \
163 const char *path, \
164 struct stat ## suffix *st) \
165 { \
166 const char *p; \
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); \
171 \
172 if (!get_rootpath(__func__)) \
173 return -1; \
174 p = trap_path(path, buf); \
175 if (p == NULL) \
176 return -1; \
177 \
178 return _fn(ver, p, st); \
179 }
180
181 WRAP_1ARG(DIR*, NULL, opendir);
182
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*);
191 WRAP_OPEN(64);
192 #endif
193
194 WRAP_OPEN();
195
196 #ifdef HAVE___XSTAT
197 WRAP_VERSTAT(__x,);
198 WRAP_VERSTAT(__lx,);
199 #ifndef _FILE_OFFSET_BITS
200 WRAP_VERSTAT(__x,64);
201 WRAP_VERSTAT(__lx,64);
202 #endif
203 #endif