Minimize overhead of getlines_wrapped in modprobe and depmod
[mit.git] / testing.h
blobbe6b31866f5537b0317ee3142437f8813bdc469e
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 <dirent.h>
18 /* We don't use all of these. */
19 static int modtest_uname(struct utsname *buf) __attribute__((unused));
20 static long modtest_create_module(const char *name, size_t size)
21 __attribute__((unused));
22 static void *modtest_fopen(const char *path, const char *mode)
23 __attribute__((unused));
24 static int modtest_open(const char *path, int flags, mode_t mode)
25 __attribute__((unused));
26 static int modtest_stat(const char *file_name, struct stat *buf)
27 __attribute__((unused));
28 static int modtest_lstat(const char *file_name, struct stat *buf)
29 __attribute__((unused));
30 static DIR *modtest_opendir(const char *name) __attribute__((unused));
31 static int modtest_system(const char *string) __attribute__((unused));
32 static int modtest_rename(const char *oldpath, const char *newpath)
33 __attribute__((unused));
34 static long modtest_init_module(void *map, unsigned long size,
35 const char *optstring) __attribute__((unused));
36 static long modtest_delete_module(const char *modname, unsigned int flags)
37 __attribute__((unused));
39 static int modtest_readlink(const char *path, char *buf, size_t bufsiz)
40 __attribute__((unused));
42 static int modtest_uname(struct utsname *buf)
44 char *release = NULL;
46 strcpy(buf->sysname, "Linux");
47 strcpy(buf->nodename, "fakenodename");
48 if ((release = getenv("MODTEST_UNAME")))
49 strcpy(buf->release, release);
50 else {
51 printf("MODTEST_OVERRIDE used but MODTEST_UNAME not set.\n");
52 exit(1);
54 strcpy(buf->version, "Fakeversion");
55 strcpy(buf->machine, "fakemachine");
56 return 0;
59 static long modtest_create_module(const char *name, size_t size)
61 if (getenv("MODTEST_DO_CREATE_MODULE"))
62 return 0;
63 errno = ENOSYS;
64 return -1;
67 static long modtest_init_module(void *map, unsigned long size,
68 const char *optstring)
70 if (getenv("MODPROBE_WAIT")) {
71 int fd;
72 const char *file = getenv("MODPROBE_WAIT");
74 printf("Looping on %s\n", file);
75 fflush(stdout);
76 while ((fd = open(file, O_RDONLY)) < 0)
77 sleep(1);
78 close(fd);
79 printf("Removing %s\n", file);
80 unlink(file);
82 if (getenv("MODTEST_INSERT_PROC")) {
83 int fd = modtest_open("/proc/modules", O_APPEND|O_WRONLY, 0);
84 write(fd, getenv("MODPROBE_MODULE"), strlen(getenv("MODPROBE_MODULE")));
85 write(fd, " 1000 1 -\n", strlen(" 1000 1 -\n"));
86 close(fd);
87 return 0;
89 if (getenv("MODTEST_DUMP_INIT")) {
90 while (size) {
91 int ret;
92 ret = write(2, map, size);
93 if (ret < 0) exit(1);
94 size -= ret;
95 map += ret;
97 } else
98 printf("INIT_MODULE: %lu %s\n", size, optstring);
100 return 0;
103 static long modtest_delete_module(const char *modname, unsigned int flags)
105 char flagnames[100];
107 if (getenv("MODPROBE_WAIT")) {
108 int fd;
109 const char *file = getenv("MODPROBE_WAIT");
111 printf("Looping on %s\n", file);
112 fflush(stdout);
113 while ((fd = open(file, O_RDONLY)) < 0)
114 sleep(1);
115 close(fd);
116 printf("Removing %s\n", file);
117 fflush(stdout);
118 unlink(file);
120 flagnames[0] = '\0';
121 if (flags & O_EXCL)
122 strcat(flagnames, "EXCL ");
123 if (flags & O_TRUNC)
124 strcat(flagnames, "TRUNC ");
125 if (flags & O_NONBLOCK)
126 strcat(flagnames, "NONBLOCK ");
127 if (flags & ~(O_EXCL|O_TRUNC|O_NONBLOCK))
128 strcat(flagnames, "UNKNOWN ");
130 printf("DELETE_MODULE: %s %s\n", modname, flagnames);
131 return 0;
134 static const char *modtest_mapname(const char *path)
136 unsigned int i;
137 char envname[64];
139 for (i = 0; ; i++) {
140 char *name;
141 sprintf(envname, "MODTEST_OVERRIDE%u", i);
142 name = getenv(envname);
143 if (!name)
144 break;
145 if (strcmp(path, name) == 0) {
146 sprintf(envname, "MODTEST_OVERRIDE_WITH%u", i);
147 return getenv(envname);
150 return path;
153 static void *modtest_fopen(const char *path, const char *mode)
155 return fopen(modtest_mapname(path), mode);
158 static int modtest_open(const char *path, int flags, mode_t mode)
160 return open(modtest_mapname(path), flags, mode);
163 static int modtest_stat(const char *file_name, struct stat *buf)
165 return stat(modtest_mapname(file_name), buf);
168 static int modtest_lstat(const char *file_name, struct stat *buf)
170 return lstat(modtest_mapname(file_name), buf);
173 static DIR *modtest_opendir(const char *name)
175 return opendir(modtest_mapname(name));
178 static int modtest_system(const char *string)
180 if (getenv("MODTEST_DO_SYSTEM"))
181 return system(string);
182 printf("SYSTEM: %s\n", string);
183 return 0;
186 static int modtest_rename(const char *oldpath, const char *newpath)
188 return rename(modtest_mapname(oldpath), modtest_mapname(newpath));
191 static int modtest_readlink(const char *path, char *buf, size_t bufsiz)
193 return readlink(modtest_mapname(path), buf, bufsiz);
196 #ifdef CONFIG_USE_ZLIB
197 #include <zlib.h>
198 static gzFile *modtest_gzopen(const char *filename, const char *mode)
199 __attribute__((unused));
201 static gzFile *modtest_gzopen(const char *filename, const char *mode)
203 return gzopen(modtest_mapname(filename), mode);
205 #endif
207 /* create_module call */
208 #undef create_module
209 #define create_module modtest_create_module
211 #define uname modtest_uname
212 #define delete_module modtest_delete_module
213 #define init_module modtest_init_module
214 #define open modtest_open
215 #define fopen modtest_fopen
216 #define stat(name, ptr) modtest_stat(name, ptr)
217 #define lstat(name, ptr) modtest_lstat(name, ptr)
218 #define opendir modtest_opendir
219 #define system modtest_system
220 #define rename modtest_rename
221 #define readlink modtest_readlink
222 #define gzopen modtest_gzopen
224 #endif /* JUST_TESTING */
225 #endif /* _TESTING_H */