13 #include <sys/utsname.h>
14 #include <asm/unistd.h>
15 #include <sys/types.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
));
45 static int modtest_uname(struct utsname
*buf
)
49 strcpy(buf
->sysname
, "Linux");
50 strcpy(buf
->nodename
, "fakenodename");
51 if ((release
= getenv("MODTEST_UNAME")))
52 strcpy(buf
->release
, release
);
54 printf("MODTEST_OVERRIDE_ROOT used but MODTEST_UNAME not set.\n");
57 strcpy(buf
->version
, "Fakeversion");
58 strcpy(buf
->machine
, "fakemachine");
62 static long modtest_create_module(const char *name
, size_t size
)
64 if (getenv("MODTEST_DO_CREATE_MODULE"))
70 static long modtest_init_module(void *map
, unsigned long size
,
71 const char *optstring
)
73 const struct timespec delay
= {
75 .tv_nsec
= 500 * 1000 * 1000
78 if (getenv("MODPROBE_WAIT")) {
80 const char *file
= getenv("MODPROBE_WAIT");
82 printf("Looping on %s\n", file
);
84 while ((fd
= open(file
, O_RDONLY
)) < 0)
85 nanosleep(&delay
, NULL
);
87 printf("Removing %s\n", file
);
90 if (getenv("MODTEST_DUMP_INIT")) {
93 ret
= write(2, map
, size
);
99 printf("INIT_MODULE: %lu %s\n", size
, optstring
);
104 static long modtest_delete_module(const char *modname
, unsigned int flags
)
108 if (getenv("MODPROBE_WAIT")) {
110 const char *file
= getenv("MODPROBE_WAIT");
112 printf("Looping on %s\n", file
);
114 while ((fd
= open(file
, O_RDONLY
)) < 0)
117 printf("Removing %s\n", file
);
123 strcat(flagnames
, "EXCL ");
125 strcat(flagnames
, "TRUNC ");
126 if (flags
& O_NONBLOCK
)
127 strcat(flagnames
, "NONBLOCK ");
128 if (flags
& ~(O_EXCL
|O_TRUNC
|O_NONBLOCK
))
129 strcat(flagnames
, "UNKNOWN ");
131 printf("DELETE_MODULE: %s %s\n", modname
, flagnames
);
135 /* Add prefix to absolute paths; relative paths are left unchanged */
136 static const char *modtest_mapname(const char *path
, char *buf
, size_t buflen
)
143 root
= getenv("MODTEST_OVERRIDE_ROOT");
147 snprintf(buf
, buflen
, "%s%s", root
, path
);
151 static void *modtest_fopen(const char *path
, const char *mode
)
153 char path_buf
[PATH_MAX
];
155 path
= modtest_mapname(path
, path_buf
, sizeof(path_buf
));
156 return fopen(path
, mode
);
159 static int modtest_open(const char *path
, int flags
, mode_t mode
)
161 char path_buf
[PATH_MAX
];
163 path
= modtest_mapname(path
, path_buf
, sizeof(path_buf
));
164 return open(path
, flags
, mode
);
167 static int modtest_stat(const char *path
, struct stat
*buf
)
169 char path_buf
[PATH_MAX
];
171 path
= modtest_mapname(path
, path_buf
, sizeof(path_buf
));
172 return stat(path
, buf
);
175 static int modtest_lstat(const char *path
, struct stat
*buf
)
177 char path_buf
[PATH_MAX
];
179 path
= modtest_mapname(path
, path_buf
, sizeof(path_buf
));
180 return lstat(path
, buf
);
183 static DIR *modtest_opendir(const char *path
)
185 char path_buf
[PATH_MAX
];
187 path
= modtest_mapname(path
, path_buf
, sizeof(path_buf
));
188 return opendir(path
);
191 static int modtest_system(const char *string
)
193 if (getenv("MODTEST_DO_SYSTEM"))
194 return system(string
);
195 printf("SYSTEM: %s\n", string
);
199 static int modtest_rename(const char *oldpath
, const char *newpath
)
201 char oldpath_buf
[PATH_MAX
];
202 char newpath_buf
[PATH_MAX
];
204 oldpath
= modtest_mapname(oldpath
, oldpath_buf
, sizeof(oldpath_buf
));
205 newpath
= modtest_mapname(newpath
, newpath_buf
, sizeof(newpath_buf
));
206 return rename(oldpath
, newpath
);
209 static int modtest_readlink(const char *path
, char *buf
, size_t bufsiz
)
211 char path_buf
[PATH_MAX
];
213 path
= modtest_mapname(path
, path_buf
, sizeof(path_buf
));
214 return readlink(path
, buf
, bufsiz
);
217 #ifdef CONFIG_USE_ZLIB
219 static gzFile
*modtest_gzopen(const char *path
, const char *mode
)
220 __attribute__((unused
));
222 static gzFile
*modtest_gzopen(const char *path
, const char *mode
)
224 char path_buf
[PATH_MAX
];
226 path
= modtest_mapname(path
, path_buf
, sizeof(path_buf
));
227 return gzopen(path
, mode
);
231 /* create_module call */
233 #define create_module modtest_create_module
235 #define uname modtest_uname
236 #define delete_module modtest_delete_module
237 #define init_module modtest_init_module
238 #define open modtest_open
239 #define fopen modtest_fopen
240 #define stat(name, ptr) modtest_stat(name, ptr)
241 #define lstat(name, ptr) modtest_lstat(name, ptr)
242 #define opendir modtest_opendir
243 #define system modtest_system
244 #define rename modtest_rename
245 #define readlink modtest_readlink
246 #define gzopen modtest_gzopen
248 #endif /* JUST_TESTING */
249 #endif /* _TESTING_H */