*
* Copyright (C) 2008 by Timo Teras <timo.teras@iki.fi>
*
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
#include "modutils.h"
return modname;
}
-char * FAST_FUNC parse_cmdline_module_options(char **argv)
+char* FAST_FUNC parse_cmdline_module_options(char **argv, int quote_spaces)
{
char *options;
int optlen;
options = xzalloc(1);
optlen = 0;
while (*++argv) {
- options = xrealloc(options, optlen + 2 + strlen(*argv) + 2);
- /* Spaces handled by "" pairs, but no way of escaping quotes */
- optlen += sprintf(options + optlen, (strchr(*argv, ' ') ? "\"%s\" " : "%s "), *argv);
+ const char *fmt;
+ const char *var;
+ const char *val;
+
+ var = *argv;
+ options = xrealloc(options, optlen + 2 + strlen(var) + 2);
+ fmt = "%.*s%s ";
+ val = strchrnul(var, '=');
+ if (quote_spaces) {
+ /*
+ * modprobe (module-init-tools version 3.11.1) compat:
+ * quote only value:
+ * var="val with spaces", not "var=val with spaces"
+ * (note: var *name* is not checked for spaces!)
+ */
+ if (*val) { /* has var=val format. skip '=' */
+ val++;
+ if (strchr(val, ' '))
+ fmt = "%.*s\"%s\" ";
+ }
+ }
+ optlen += sprintf(options + optlen, fmt, (int)(val - var), var, val);
}
+ /* Remove trailing space. Disabled */
+ /* if (optlen != 0) options[optlen-1] = '\0'; */
return options;
}
+#if ENABLE_FEATURE_INSMOD_TRY_MMAP
+void* FAST_FUNC try_to_mmap_module(const char *filename, size_t *image_size_p)
+{
+ /* We have user reports of failure to load 3MB module
+ * on a 16MB RAM machine. Apparently even a transient
+ * memory spike to 6MB during module load
+ * is too big for that system. */
+ void *image;
+ struct stat st;
+ int fd;
+
+ fd = xopen(filename, O_RDONLY);
+ fstat(fd, &st);
+ image = NULL;
+ /* st.st_size is off_t, we can't just pass it to mmap */
+ if (st.st_size <= *image_size_p) {
+ size_t image_size = st.st_size;
+ image = mmap(NULL, image_size, PROT_READ, MAP_PRIVATE, fd, 0);
+ if (image == MAP_FAILED) {
+ image = NULL;
+ } else if (*(uint32_t*)image != SWAP_BE32(0x7f454C46)) {
+ /* No ELF signature. Compressed module? */
+ munmap(image, image_size);
+ image = NULL;
+ } else {
+ /* Success. Report the size */
+ *image_size_p = image_size;
+ }
+ }
+ close(fd);
+ return image;
+}
+#endif
+
/* Return:
* 0 on success,
* -errno on open/read error,
*/
int FAST_FUNC bb_init_module(const char *filename, const char *options)
{
- size_t len;
+ size_t image_size;
char *image;
int rc;
+ bool mmaped;
if (!options)
options = "";
return bb_init_module_24(filename, options);
#endif
- /* Use the 2.6 way */
- len = INT_MAX - 4095;
- errno = ENOMEM; /* may be changed by e.g. open errors below */
- image = xmalloc_open_zipped_read_close(filename, &len);
- if (!image)
- return -errno;
+ image_size = INT_MAX - 4095;
+ mmaped = 0;
+ image = try_to_mmap_module(filename, &image_size);
+ if (image) {
+ mmaped = 1;
+ } else {
+ errno = ENOMEM; /* may be changed by e.g. open errors below */
+ image = xmalloc_open_zipped_read_close(filename, &image_size);
+ if (!image)
+ return -errno;
+ }
errno = 0;
- init_module(image, len, options);
+ init_module(image, image_size, options);
rc = errno;
- free(image);
+ if (mmaped)
+ munmap(image, image_size);
+ else
+ free(image);
return rc;
}