13 #include <sys/utsname.h>
14 #include <asm/unistd.h>
15 #include <sys/types.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
)
46 strcpy(buf
->sysname
, "Linux");
47 strcpy(buf
->nodename
, "fakenodename");
48 if ((release
= getenv("MODTEST_UNAME")))
49 strcpy(buf
->release
, release
);
51 printf("MODTEST_OVERRIDE used but MODTEST_UNAME not set.\n");
54 strcpy(buf
->version
, "Fakeversion");
55 strcpy(buf
->machine
, "fakemachine");
59 static long modtest_create_module(const char *name
, size_t size
)
61 if (getenv("MODTEST_DO_CREATE_MODULE"))
67 static long modtest_init_module(void *map
, unsigned long size
,
68 const char *optstring
)
70 if (getenv("MODPROBE_WAIT")) {
72 const char *file
= getenv("MODPROBE_WAIT");
74 printf("Looping on %s\n", file
);
76 while ((fd
= open(file
, O_RDONLY
)) < 0)
79 printf("Removing %s\n", 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"));
89 if (getenv("MODTEST_DUMP_INIT")) {
92 ret
= write(2, map
, size
);
98 printf("INIT_MODULE: %lu %s\n", size
, optstring
);
103 static long modtest_delete_module(const char *modname
, unsigned int flags
)
107 if (getenv("MODPROBE_WAIT")) {
109 const char *file
= getenv("MODPROBE_WAIT");
111 printf("Looping on %s\n", file
);
113 while ((fd
= open(file
, O_RDONLY
)) < 0)
116 printf("Removing %s\n", file
);
122 strcat(flagnames
, "EXCL ");
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
);
134 static const char *modtest_mapname(const char *path
)
141 sprintf(envname
, "MODTEST_OVERRIDE%u", i
);
142 name
= getenv(envname
);
145 if (strcmp(path
, name
) == 0) {
146 sprintf(envname
, "MODTEST_OVERRIDE_WITH%u", i
);
147 return getenv(envname
);
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
);
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
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
);
207 /* create_module call */
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 */