tftp: do not risk invoking Sorcerer's Apprentice syndrome
[oweals/busybox.git] / modutils / modprobe-small.c
index f75dae8a3bc4c7190fe3ae35e60a95227939992c..3fd7bf5adaec76df5b1f1a9b57a1c4045e8a067e 100644 (file)
@@ -3,7 +3,7 @@
  * simplified modprobe
  *
  * Copyright (c) 2008 Vladimir Dronnikov
- * Copyright (c) 2008 Bernhard Fischer (initial depmod code)
+ * Copyright (c) 2008 Bernhard Reutner-Fischer (initial depmod code)
  *
  * Licensed under GPLv2, see file LICENSE in this tarball for details.
  */
@@ -100,7 +100,7 @@ static char* find_keyword(char *ptr, size_t len, const char *word)
 {
        int wlen;
 
-       if (!ptr) /* happens if read_module cannot read it */
+       if (!ptr) /* happens if xmalloc_open_zipped_read_close cannot read it */
                return NULL;
 
        wlen = strlen(word);
@@ -141,12 +141,6 @@ static char* str_2_list(const char *str)
        return dst;
 }
 
-#if ENABLE_FEATURE_MODPROBE_SMALL_ZIPPED
-# define read_module xmalloc_open_zipped_read_close
-#else
-# define read_module xmalloc_open_read_close
-#endif
-
 /* We use error numbers in a loose translation... */
 static const char *moderror(int err)
 {
@@ -173,7 +167,7 @@ static int load_module(const char *fname, const char *options)
        char *module_image;
        dbg1_error_msg("load_module('%s','%s')", fname, options);
 
-       module_image = read_module(fname, &len);
+       module_image = xmalloc_open_zipped_read_close(fname, &len);
        r = (!module_image || init_module(module_image, len, options ? options : "") != 0);
        free(module_image);
        dbg1_error_msg("load_module:%d", r);
@@ -195,7 +189,7 @@ static void parse_module(module_info *info, const char *pathname)
 
        /* Read (possibly compressed) module */
        len = 64 * 1024 * 1024; /* 64 Mb at most */
-       module_image = read_module(pathname, &len);
+       module_image = xmalloc_open_zipped_read_close(pathname, &len);
 //TODO: optimize redundant module body reads
 
        /* "alias1 symbol:sym1 alias2 symbol:sym2" */
@@ -270,10 +264,9 @@ static FAST_FUNC int fileAction(const char *pathname,
 
        cur = module_count++;
        modinfo = xrealloc_vector(modinfo, 12, cur);
-//TODO: use zeroing version of xrealloc_vector?
        modinfo[cur].pathname = xstrdup(pathname);
-       modinfo[cur].aliases = NULL;
-       modinfo[cur+1].pathname = NULL;
+       /*modinfo[cur].aliases = NULL; - xrealloc_vector did it */
+       /*modinfo[cur+1].pathname = NULL;*/
 
        if (!pathname_matches_modname(fname, modname_to_match)) {
                dbg1_error_msg("'%s' module name doesn't match", pathname);
@@ -330,8 +323,7 @@ static int load_dep_bb(void)
                space = strchrnul(line, ' ');
                cur = module_count++;
                modinfo = xrealloc_vector(modinfo, 12, cur);
-//TODO: use zeroing version of xrealloc_vector?
-               modinfo[cur+1].pathname = NULL;
+               /*modinfo[cur+1].pathname = NULL; - xrealloc_vector did it */
                modinfo[cur].pathname = line; /* we take ownership of malloced block here */
                if (*space)
                        *space++ = '\0';
@@ -452,8 +444,10 @@ static module_info* find_alias(const char *alias)
                if (!modinfo[i].aliases) {
                        parse_module(&modinfo[i], modinfo[i].pathname);
                }
-               if (result)
+               if (result) {
+                       i++;
                        continue;
+               }
                /* "alias1 symbol:sym1 alias2 symbol:sym2" */
                desc = str_2_list(modinfo[i].aliases);
                /* Does matching substring exist? */
@@ -485,23 +479,19 @@ static module_info* find_alias(const char *alias)
 }
 
 #if ENABLE_FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED
+// TODO: open only once, invent config_rewind()
 static int already_loaded(const char *name)
 {
        int ret = 0;
-       int len = strlen(name);
-       char *line;
-       FILE* modules;
-
-       modules = xfopen_for_read("/proc/modules");
-       while ((line = xmalloc_fgets(modules)) != NULL) {
-               if (strncmp(line, name, len) == 0 && line[len] == ' ') {
-                       free(line);
+       char *s;
+       parser_t *parser = config_open2("/proc/modules", xfopen_for_read);
+       while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) {
+               if (strcmp(s, name) == 0) {
                        ret = 1;
                        break;
                }
-               free(line);
        }
-       fclose(modules);
+       config_close(parser);
        return ret;
 }
 #else
@@ -609,19 +599,23 @@ static void process_module(char *name, const char *cmdline_options)
        }
        free(deps);
 
-       /* insmod -> load it */
+       /* modprobe -> load it */
        if (!is_rmmod) {
-               errno = 0;
-               if (load_module(info->pathname, options) != 0) {
-                       if (EEXIST != errno) {
-                               bb_error_msg("'%s': %s",
+               if (!options || strstr(options, "blacklist") == NULL) {
+                       errno = 0;
+                       if (load_module(info->pathname, options) != 0) {
+                               if (EEXIST != errno) {
+                                       bb_error_msg("'%s': %s",
                                                info->pathname,
                                                moderror(errno));
-                       } else {
-                               dbg1_error_msg("'%s': %s",
+                               } else {
+                                       dbg1_error_msg("'%s': %s",
                                                info->pathname,
                                                moderror(errno));
+                               }
                        }
+               } else {
+                       dbg1_error_msg("'%s': blacklisted", info->pathname);
                }
        }
  ret:
@@ -646,7 +640,7 @@ Usage: rmmod [-fhswvV] modulename ...
  -s (or --syslog) says use syslog, not stderr
  -v (or --verbose) enables more messages
  -V (or --version) prints the version code
- -w (or --wait) begins module removal even if it is used
+ -w (or --wait) begins module removal even if it is used
     and will stop new users from accessing the module (so it
     should eventually fall to zero).
 
@@ -664,19 +658,20 @@ depmod [-n -e -v -q -r -u] [-F kernelsyms] module1.ko module2.ko ...
 If no arguments (except options) are given, "depmod -a" is assumed.
 depmod will output a dependancy list suitable for the modprobe utility.
 Options:
-    -a, --all            Probe all modules
-    -A, --quick          Only does the work if there's a new module
-    -n, --show           Write the dependency file on stdout only
-    -e, --errsyms        Report not supplied symbols
-    -V, --version        Print the release version
-    -v, --verbose        Enable verbose mode
-    -h, --help           Print this usage message
+    -a, --all           Probe all modules
+    -A, --quick         Only does the work if there's a new module
+    -n, --show          Write the dependency file on stdout only
+    -e, --errsyms       Report not supplied symbols
+    -V, --version       Print the release version
+    -v, --verbose       Enable verbose mode
+    -h, --help          Print this usage message
 The following options are useful for people managing distributions:
     -b basedirectory
-        --basedir basedirectory    Use an image of a module tree.
+    --basedir basedirectory
+                        Use an image of a module tree
     -F kernelsyms
-        --filesyms kernelsyms      Use the file instead of the
-                                   current kernel symbols.
+    --filesyms kernelsyms
+                        Use the file instead of the current kernel symbols
 */
 
 int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -684,7 +679,7 @@ int modprobe_main(int argc UNUSED_PARAM, char **argv)
 {
        struct utsname uts;
        char applet0 = applet_name[0];
-       USE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(char *options;)
+       IF_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(char *options;)
 
        /* are we lsmod? -> just dump /proc/modules */
        if ('l' == applet0) {
@@ -697,8 +692,10 @@ int modprobe_main(int argc UNUSED_PARAM, char **argv)
        /* Prevent ugly corner cases with no modules at all */
        modinfo = xzalloc(sizeof(modinfo[0]));
 
-       /* Goto modules directory */
-       xchdir(CONFIG_DEFAULT_MODULES_DIR);
+       if ('i' != applet0) { /* not insmod */
+               /* Goto modules directory */
+               xchdir(CONFIG_DEFAULT_MODULES_DIR);
+       }
        uname(&uts); /* never fails */
 
        /* depmod? */
@@ -745,8 +742,10 @@ int modprobe_main(int argc UNUSED_PARAM, char **argv)
                option_mask32 |= OPT_r;
        }
 
-       /* Goto $VERSION directory */
-       xchdir(uts.release);
+       if ('i' != applet0) { /* not insmod */
+               /* Goto $VERSION directory */
+               xchdir(uts.release);
+       }
 
 #if ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
        /* If not rmmod, parse possible module options given on command line.
@@ -767,17 +766,32 @@ int modprobe_main(int argc UNUSED_PARAM, char **argv)
                argv[1] = NULL;
 #endif
 
+       if ('i' == applet0) { /* insmod */
+               size_t len;
+               void *map;
+
+               len = MAXINT(ssize_t);
+               map = xmalloc_xopen_read_close(*argv, &len);
+               if (init_module(map, len,
+                       IF_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(options ? options : "")
+                       IF_NOT_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE("")
+                               ) != 0)
+                       bb_error_msg_and_die("can't insert '%s': %s",
+                                       *argv, moderror(errno));
+               return 0;
+       }
+
        /* Try to load modprobe.dep.bb */
        load_dep_bb();
 
        /* Load/remove modules.
-        * Only rmmod loops here, insmod/modprobe has only argv[0] */
+        * Only rmmod loops here, modprobe has only argv[0] */
        do {
                process_module(*argv++, options);
        } while (*argv);
 
        if (ENABLE_FEATURE_CLEAN_UP) {
-               USE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(free(options);)
+               IF_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(free(options);)
        }
        return EXIT_SUCCESS;
 }