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_DUMP_INIT")) {
85 ret
= write(2, map
, size
);
91 printf("INIT_MODULE: %lu %s\n", size
, optstring
);
96 static long modtest_delete_module(const char *modname
, unsigned int flags
)
100 if (getenv("MODPROBE_WAIT")) {
102 const char *file
= getenv("MODPROBE_WAIT");
104 printf("Looping on %s\n", file
);
106 while ((fd
= open(file
, O_RDONLY
)) < 0)
109 printf("Removing %s\n", file
);
115 strcat(flagnames
, "EXCL ");
117 strcat(flagnames
, "TRUNC ");
118 if (flags
& O_NONBLOCK
)
119 strcat(flagnames
, "NONBLOCK ");
120 if (flags
& ~(O_EXCL
|O_TRUNC
|O_NONBLOCK
))
121 strcat(flagnames
, "UNKNOWN ");
123 printf("DELETE_MODULE: %s %s\n", modname
, flagnames
);
127 static const char *modtest_mapname(const char *path
)
134 sprintf(envname
, "MODTEST_OVERRIDE%u", i
);
135 name
= getenv(envname
);
139 if (strcmp(path
, name
) == 0) {
140 sprintf(envname
, "MODTEST_OVERRIDE_WITH%u", i
);
141 return getenv(envname
);
147 static void *modtest_fopen(const char *path
, const char *mode
)
149 return fopen(modtest_mapname(path
), mode
);
152 static int modtest_open(const char *path
, int flags
, mode_t mode
)
154 return open(modtest_mapname(path
), flags
, mode
);
157 static int modtest_stat(const char *file_name
, struct stat
*buf
)
159 return stat(modtest_mapname(file_name
), buf
);
162 static int modtest_lstat(const char *file_name
, struct stat
*buf
)
164 return lstat(modtest_mapname(file_name
), buf
);
167 static DIR *modtest_opendir(const char *name
)
169 return opendir(modtest_mapname(name
));
172 static int modtest_system(const char *string
)
174 if (getenv("MODTEST_DO_SYSTEM"))
175 return system(string
);
176 printf("SYSTEM: %s\n", string
);
180 static int modtest_rename(const char *oldpath
, const char *newpath
)
182 return rename(modtest_mapname(oldpath
), modtest_mapname(newpath
));
185 static int modtest_readlink(const char *path
, char *buf
, size_t bufsiz
)
187 return readlink(modtest_mapname(path
), buf
, bufsiz
);
190 #ifdef CONFIG_USE_ZLIB
192 static gzFile
*modtest_gzopen(const char *filename
, const char *mode
)
193 __attribute__((unused
));
195 static gzFile
*modtest_gzopen(const char *filename
, const char *mode
)
197 return gzopen(modtest_mapname(filename
), mode
);
201 /* create_module call */
203 #define create_module modtest_create_module
205 #define uname modtest_uname
206 #define delete_module modtest_delete_module
207 #define init_module modtest_init_module
208 #define open modtest_open
209 #define fopen modtest_fopen
210 #define stat(name, ptr) modtest_stat(name, ptr)
211 #define lstat(name, ptr) modtest_lstat(name, ptr)
212 #define opendir modtest_opendir
213 #define system modtest_system
214 #define rename modtest_rename
215 #define readlink modtest_readlink
216 #define gzopen modtest_gzopen
218 #endif /* JUST_TESTING */
219 #endif /* _TESTING_H */