depmod: fix tabs in help output
[module-init-tools.git] / testing.h
blob9d6090abb3a3bb13b5d7bb612de46cc1210cb484
1 #ifndef _TESTING_H
2 #define _TESTING_H
4 /* Testing code. */
5 #ifdef JUST_TESTING
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <stdarg.h>
13 #include <sys/utsname.h>
14 #include <asm/unistd.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <dirent.h>
19 #include <time.h>
21 /* We don't use all of these. */
22 static int modtest_uname(struct utsname *buf) __attribute__((unused));
23 static long modtest_create_module(const char *name, size_t size)
24 __attribute__((unused));
25 static void *modtest_fopen(const char *path, const char *mode)
26 __attribute__((unused));
27 static int modtest_open(const char *path, int flags, mode_t mode)
28 __attribute__((unused));
29 static int modtest_stat(const char *file_name, struct stat *buf)
30 __attribute__((unused));
31 static int modtest_lstat(const char *file_name, struct stat *buf)
32 __attribute__((unused));
33 static DIR *modtest_opendir(const char *name) __attribute__((unused));
34 static int modtest_system(const char *string) __attribute__((unused));
35 static int modtest_rename(const char *oldpath, const char *newpath)
36 __attribute__((unused));
37 static long modtest_init_module(void *map, unsigned long size,
38 const char *optstring) __attribute__((unused));
39 static long modtest_delete_module(const char *modname, unsigned int flags)
40 __attribute__((unused));
42 static int modtest_readlink(const char *path, char *buf, size_t bufsiz)
43 __attribute__((unused));
44 static int modtest_unlink(const char *path)
45 __attribute__((unused));
47 static int modtest_uname(struct utsname *buf)
49 char *release = NULL;
51 strcpy(buf->sysname, "Linux");
52 strcpy(buf->nodename, "fakenodename");
53 if ((release = getenv("MODTEST_UNAME")))
54 strcpy(buf->release, release);
55 else {
56 printf("MODTEST_OVERRIDE_ROOT used but MODTEST_UNAME not set.\n");
57 exit(1);
59 strcpy(buf->version, "Fakeversion");
60 strcpy(buf->machine, "fakemachine");
61 return 0;
64 static long modtest_create_module(const char *name, size_t size)
66 if (getenv("MODTEST_DO_CREATE_MODULE"))
67 return 0;
68 errno = ENOSYS;
69 return -1;
72 static const struct timespec modtest_delay = {
73 .tv_sec = 0,
74 .tv_nsec = 500 * 1000 * 1000
77 static long modtest_init_module(void *map, unsigned long size,
78 const char *optstring)
81 if (getenv("MODPROBE_WAIT")) {
82 int fd;
83 const char *file = getenv("MODPROBE_WAIT");
85 printf("Looping on %s\n", file);
86 fflush(stdout);
87 while ((fd = open(file, O_RDONLY)) < 0)
88 nanosleep(&modtest_delay, NULL);
89 close(fd);
90 printf("Removing %s\n", file);
91 unlink(file);
93 if (getenv("MODTEST_DUMP_INIT")) {
94 while (size) {
95 int ret;
96 ret = write(2, map, size);
97 if (ret < 0) exit(1);
98 size -= ret;
99 map += ret;
101 } else
102 printf("INIT_MODULE: %lu %s\n", size, optstring);
104 return 0;
107 static long modtest_delete_module(const char *modname, unsigned int flags)
109 char flagnames[100];
111 if (getenv("MODPROBE_WAIT")) {
112 int fd;
113 const char *file = getenv("MODPROBE_WAIT");
115 printf("Looping on %s\n", file);
116 fflush(stdout);
117 while ((fd = open(file, O_RDONLY)) < 0)
118 nanosleep(&modtest_delay, NULL);
119 close(fd);
120 printf("Removing %s\n", file);
121 fflush(stdout);
122 unlink(file);
124 flagnames[0] = '\0';
125 if (flags & O_EXCL)
126 strcat(flagnames, "EXCL ");
127 if (flags & O_TRUNC)
128 strcat(flagnames, "TRUNC ");
129 if (flags & O_NONBLOCK)
130 strcat(flagnames, "NONBLOCK ");
131 if (flags & ~(O_EXCL|O_TRUNC|O_NONBLOCK))
132 strcat(flagnames, "UNKNOWN ");
134 printf("DELETE_MODULE: %s %s\n", modname, flagnames);
135 return 0;
138 /* Add prefix to absolute paths; relative paths are left unchanged */
139 static const char *modtest_mapname(const char *path, char *buf, size_t buflen)
141 char *root;
143 if (path[0] != '/')
144 return path;
146 root = getenv("MODTEST_OVERRIDE_ROOT");
147 if (!root)
148 return path;
150 snprintf(buf, buflen, "%s%s", root, path);
151 return buf;
154 static void *modtest_fopen(const char *path, const char *mode)
156 char path_buf[PATH_MAX];
158 path = modtest_mapname(path, path_buf, sizeof(path_buf));
159 return fopen(path, mode);
162 static int modtest_open(const char *path, int flags, mode_t mode)
164 char path_buf[PATH_MAX];
166 path = modtest_mapname(path, path_buf, sizeof(path_buf));
167 return open(path, flags, mode);
170 static int modtest_stat(const char *path, struct stat *buf)
172 char path_buf[PATH_MAX];
174 path = modtest_mapname(path, path_buf, sizeof(path_buf));
175 return stat(path, buf);
178 static int modtest_lstat(const char *path, struct stat *buf)
180 char path_buf[PATH_MAX];
182 path = modtest_mapname(path, path_buf, sizeof(path_buf));
183 return lstat(path, buf);
186 static DIR *modtest_opendir(const char *path)
188 char path_buf[PATH_MAX];
190 path = modtest_mapname(path, path_buf, sizeof(path_buf));
191 return opendir(path);
194 static int modtest_system(const char *string)
196 if (getenv("MODTEST_DO_SYSTEM"))
197 return system(string);
198 printf("SYSTEM: %s\n", string);
199 return 0;
202 static int modtest_rename(const char *oldpath, const char *newpath)
204 char oldpath_buf[PATH_MAX];
205 char newpath_buf[PATH_MAX];
207 oldpath = modtest_mapname(oldpath, oldpath_buf, sizeof(oldpath_buf));
208 newpath = modtest_mapname(newpath, newpath_buf, sizeof(newpath_buf));
209 return rename(oldpath, newpath);
212 static int modtest_readlink(const char *path, char *buf, size_t bufsiz)
214 char path_buf[PATH_MAX];
216 path = modtest_mapname(path, path_buf, sizeof(path_buf));
217 return readlink(path, buf, bufsiz);
220 static int modtest_unlink(const char *path)
222 char path_buf[PATH_MAX];
224 path = modtest_mapname(path, path_buf, sizeof(path_buf));
225 return unlink(path);
228 #ifdef CONFIG_USE_ZLIB
229 #include <zlib.h>
230 static gzFile *modtest_gzopen(const char *path, const char *mode)
231 __attribute__((unused));
233 static gzFile *modtest_gzopen(const char *path, const char *mode)
235 char path_buf[PATH_MAX];
237 path = modtest_mapname(path, path_buf, sizeof(path_buf));
238 return gzopen(path, mode);
240 #endif
242 /* create_module call */
243 #undef create_module
244 #define create_module modtest_create_module
246 #define uname modtest_uname
247 #define delete_module modtest_delete_module
248 #define init_module modtest_init_module
249 #define open modtest_open
250 #define fopen modtest_fopen
251 #define stat(name, ptr) modtest_stat(name, ptr)
252 #define lstat(name, ptr) modtest_lstat(name, ptr)
253 #define opendir modtest_opendir
254 #define system modtest_system
255 #define rename modtest_rename
256 #define readlink modtest_readlink
257 #define unlink modtest_unlink
258 #define gzopen modtest_gzopen
260 #endif /* JUST_TESTING */
261 #endif /* _TESTING_H */