libbb: introduce and use strcpy_and_process_escape_sequences
[oweals/busybox.git] / modutils / modprobe-small.c
index f28c42558f4c8002bdf47a1dcc7f1e313029dcfe..ec3ddfb8f4369f683c9ba4db5020a78a8b28c0a1 100644 (file)
@@ -3,14 +3,19 @@
  * 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.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  */
 
-#include "libbb.h"
-#include "unarchive.h"
+//applet:IF_MODPROBE_SMALL(APPLET(modprobe, _BB_DIR_SBIN, _BB_SUID_DROP))
+//applet:IF_MODPROBE_SMALL(APPLET_ODDNAME(depmod, modprobe, _BB_DIR_SBIN, _BB_SUID_DROP, modprobe))
+//applet:IF_MODPROBE_SMALL(APPLET_ODDNAME(insmod, modprobe, _BB_DIR_SBIN, _BB_SUID_DROP, modprobe))
+//applet:IF_MODPROBE_SMALL(APPLET_ODDNAME(lsmod, modprobe, _BB_DIR_SBIN, _BB_SUID_DROP, modprobe))
+//applet:IF_MODPROBE_SMALL(APPLET_ODDNAME(rmmod, modprobe, _BB_DIR_SBIN, _BB_SUID_DROP, modprobe))
 
+#include "libbb.h"
+/* After libbb.h, since it needs sys/types.h on some systems */
 #include <sys/utsname.h> /* uname() */
 #include <fnmatch.h>
 
@@ -19,10 +24,13 @@ extern int delete_module(const char *module, unsigned flags);
 extern int query_module(const char *name, int which, void *buf, size_t bufsize, size_t *ret);
 
 
-#define dbg1_error_msg(...) ((void)0)
-#define dbg2_error_msg(...) ((void)0)
-//#define dbg1_error_msg(...) bb_error_msg(__VA_ARGS__)
-//#define dbg2_error_msg(...) bb_error_msg(__VA_ARGS__)
+#if 1
+# define dbg1_error_msg(...) ((void)0)
+# define dbg2_error_msg(...) ((void)0)
+#else
+# define dbg1_error_msg(...) bb_error_msg(__VA_ARGS__)
+# define dbg2_error_msg(...) bb_error_msg(__VA_ARGS__)
+#endif
 
 #define DEPFILE_BB CONFIG_DEFAULT_DEPMOD_FILE".bb"
 
@@ -45,11 +53,13 @@ struct globals {
        char *module_load_options;
        smallint dep_bb_seen;
        smallint wrote_dep_bb_ok;
-       int module_count;
+       unsigned module_count;
        int module_found_idx;
-       int stringbuf_idx;
-       char stringbuf[32 * 1024]; /* some modules have lots of stuff */
+       unsigned stringbuf_idx;
+       unsigned stringbuf_size;
+       char *stringbuf; /* some modules have lots of stuff */
        /* for example, drivers/media/video/saa7134/saa7134.ko */
+       /* therefore having a fixed biggish buffer is not wise */
 };
 #define G (*ptr_to_globals)
 #define modinfo             (G.modinfo            )
@@ -59,16 +69,29 @@ struct globals {
 #define module_found_idx    (G.module_found_idx   )
 #define module_load_options (G.module_load_options)
 #define stringbuf_idx       (G.stringbuf_idx      )
+#define stringbuf_size      (G.stringbuf_size     )
 #define stringbuf           (G.stringbuf          )
 #define INIT_G() do { \
        SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
 } while (0)
 
+static void append(const char *s)
+{
+       unsigned len = strlen(s);
+       if (stringbuf_idx + len + 15 > stringbuf_size) {
+               stringbuf_size = stringbuf_idx + len + 127;
+               dbg2_error_msg("grow stringbuf to %u", stringbuf_size);
+               stringbuf = xrealloc(stringbuf, stringbuf_size);
+       }
+       memcpy(stringbuf + stringbuf_idx, s, len);
+       stringbuf_idx += len;
+}
 
 static void appendc(char c)
 {
-       if (stringbuf_idx < sizeof(stringbuf))
-               stringbuf[stringbuf_idx++] = c;
+       /* We appendc() only after append(), + 15 trick in append()
+        * makes it unnecessary to check for overflow here */
+       stringbuf[stringbuf_idx++] = c;
 }
 
 static void bksp(void)
@@ -77,15 +100,6 @@ static void bksp(void)
                stringbuf_idx--;
 }
 
-static void append(const char *s)
-{
-       size_t len = strlen(s);
-       if (stringbuf_idx + len < sizeof(stringbuf)) {
-               memcpy(stringbuf + stringbuf_idx, s, len);
-               stringbuf_idx += len;
-       }
-}
-
 static void reset_stringbuf(void)
 {
        stringbuf_idx = 0;
@@ -93,7 +107,7 @@ static void reset_stringbuf(void)
 
 static char* copy_stringbuf(void)
 {
-       char *copy = xmalloc(stringbuf_idx);
+       char *copy = xzalloc(stringbuf_idx + 1); /* terminating NUL */
        return memcpy(copy, stringbuf, stringbuf_idx);
 }
 
@@ -101,7 +115,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);
@@ -142,40 +156,6 @@ static char* str_2_list(const char *str)
        return dst;
 }
 
-#if ENABLE_FEATURE_MODPROBE_SMALL_ZIPPED
-static char *xmalloc_open_zipped_read_close(const char *fname, size_t *sizep)
-{
-       size_t len;
-       char *image;
-       char *suffix;
-
-       int fd = open_or_warn(fname, O_RDONLY);
-       if (fd < 0)
-               return NULL;
-
-       suffix = strrchr(fname, '.');
-       if (suffix) {
-               if (strcmp(suffix, ".gz") == 0)
-                       fd = open_transformer(fd, unpack_gz_stream, "gunzip");
-               else if (strcmp(suffix, ".bz2") == 0)
-                       fd = open_transformer(fd, unpack_bz2_stream, "bunzip2");
-       }
-
-       len = (sizep) ? *sizep : 64 * 1024 * 1024;
-       image = xmalloc_read(fd, &len);
-       if (!image)
-               bb_perror_msg("read error from '%s'", fname);
-       close(fd);
-
-       if (sizep)
-               *sizep = len;
-       return image;
-}
-# 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)
 {
@@ -202,7 +182,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);
@@ -224,7 +204,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" */
@@ -251,8 +231,8 @@ static void parse_module(module_info *info, const char *pathname)
                pos = (ptr - module_image);
        }
        bksp(); /* remove last ' ' */
-       appendc('\0');
        info->aliases = copy_stringbuf();
+       replace(info->aliases, '-', '_');
 
        /* "dependency1 depandency2" */
        reset_stringbuf();
@@ -263,7 +243,6 @@ static void parse_module(module_info *info, const char *pathname)
                dbg2_error_msg("dep:'%s'", ptr);
                append(ptr);
        }
-       appendc('\0');
        info->deps = copy_stringbuf();
 
        free(module_image);
@@ -299,10 +278,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);
@@ -328,7 +306,7 @@ static FAST_FUNC int fileAction(const char *pathname,
 static int load_dep_bb(void)
 {
        char *line;
-       FILE *fp = fopen(DEPFILE_BB, "r");
+       FILE *fp = fopen_for_read(DEPFILE_BB);
 
        if (!fp)
                return 0;
@@ -350,6 +328,7 @@ static int load_dep_bb(void)
 
        while ((line = xmalloc_fgetline(fp)) != NULL) {
                char* space;
+               char* linebuf;
                int cur;
 
                if (!line[0]) {
@@ -359,13 +338,13 @@ 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';
                modinfo[cur].aliases = space;
-               modinfo[cur].deps = xmalloc_fgetline(fp) ? : xzalloc(1);
+               linebuf = xmalloc_fgetline(fp);
+               modinfo[cur].deps = linebuf ? linebuf : xzalloc(1);
                if (modinfo[cur].deps[0]) {
                        /* deps are not "", so next line must be empty */
                        line = xmalloc_fgetline(fp);
@@ -414,11 +393,7 @@ static void write_out_dep_bb(int fd)
        FILE *fp;
 
        /* We want good error reporting. fdprintf is not good enough. */
-       fp = fdopen(fd, "w");
-       if (!fp) {
-               close(fd);
-               goto err;
-       }
+       fp = xfdopen_for_write(fd);
        i = 0;
        while (modinfo[i].pathname) {
                fprintf(fp, "%s%s%s\n" "%s%s\n",
@@ -436,7 +411,7 @@ static void write_out_dep_bb(int fd)
 
        if (rename(DEPFILE_BB".new", DEPFILE_BB) != 0) {
  err:
-               bb_perror_msg("can't create %s", DEPFILE_BB);
+               bb_perror_msg("can't create '%s'", DEPFILE_BB);
                unlink(DEPFILE_BB".new");
        } else {
  ok:
@@ -481,8 +456,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? */
@@ -514,23 +491,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("/proc/modules", "r");
-       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
@@ -605,12 +578,19 @@ static void process_module(char *name, const char *cmdline_options)
                info = find_alias(name);
        }
 
+// Problem here: there can be more than one module
+// for the given alias. For example,
+// "pci:v00008086d00007010sv00000000sd00000000bc01sc01i80" matches
+// ata_piix because it has an alias "pci:v00008086d00007010sv*sd*bc*sc*i*"
+// and ata_generic, it has an alias "alias=pci:v*d*sv*sd*bc01sc01i*"
+// Standard modprobe would load them both.
+// In this code, find_alias() returns only the first matching module.
+
        /* rmmod? unload it by name */
        if (is_rmmod) {
-               if (delete_module(name, O_NONBLOCK | O_EXCL) != 0
-                && !(option_mask32 & OPT_q)
-               ) {
-                       bb_perror_msg("remove '%s'", name);
+               if (delete_module(name, O_NONBLOCK | O_EXCL) != 0) {
+                       if (!(option_mask32 & OPT_q))
+                               bb_perror_msg("remove '%s'", name);
                        goto ret;
                }
                /* N.B. we do not stop here -
@@ -622,9 +602,9 @@ static void process_module(char *name, const char *cmdline_options)
 
        if (!info) {
                /* both dirscan and find_alias found nothing */
-               if (applet_name[0] != 'd') /* it wasn't depmod */
+               if (!is_rmmod && applet_name[0] != 'd') /* it wasn't rmmod or depmod */
                        bb_error_msg("module '%s' not found", name);
-//TODO: _and_die()?
+//TODO: _and_die()? or should we continue (un)loading modules listed on cmdline?
                goto ret;
        }
 
@@ -638,19 +618,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:
@@ -675,7 +659,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).
 
@@ -691,33 +675,88 @@ depmod -[aA] [-n -e -v -q -V -r -u]
       [-b basedirectory] [forced_version]
 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.
+depmod will output a dependency 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
 */
 
+//usage:#if ENABLE_MODPROBE_SMALL
+
+//usage:#define depmod_trivial_usage NOUSAGE_STR
+//usage:#define depmod_full_usage ""
+
+//usage:#define lsmod_trivial_usage
+//usage:       ""
+//usage:#define lsmod_full_usage "\n\n"
+//usage:       "List the currently loaded kernel modules"
+
+//usage:#define insmod_trivial_usage
+//usage:       IF_FEATURE_2_4_MODULES("[OPTIONS] MODULE ")
+//usage:       IF_NOT_FEATURE_2_4_MODULES("FILE ")
+//usage:       "[SYMBOL=VALUE]..."
+//usage:#define insmod_full_usage "\n\n"
+//usage:       "Load the specified kernel modules into the kernel"
+//usage:       IF_FEATURE_2_4_MODULES( "\n"
+//usage:     "\nOptions:"
+//usage:     "\n       -f      Force module to load into the wrong kernel version"
+//usage:     "\n       -k      Make module autoclean-able"
+//usage:     "\n       -v      Verbose"
+//usage:     "\n       -q      Quiet"
+//usage:     "\n       -L      Lock: prevent simultaneous loads"
+//usage:       IF_FEATURE_INSMOD_LOAD_MAP(
+//usage:     "\n       -m      Output load map to stdout"
+//usage:       )
+//usage:     "\n       -x      Don't export externs"
+//usage:       )
+
+//usage:#define rmmod_trivial_usage
+//usage:       "[-wfa] [MODULE]..."
+//usage:#define rmmod_full_usage "\n\n"
+//usage:       "Unload kernel modules\n"
+//usage:     "\nOptions:"
+//usage:     "\n       -w      Wait until the module is no longer used"
+//usage:     "\n       -f      Force unload"
+//usage:     "\n       -a      Remove all unused modules (recursively)"
+//usage:
+//usage:#define rmmod_example_usage
+//usage:       "$ rmmod tulip\n"
+
+//usage:#define modprobe_trivial_usage
+//usage:       "[-qfwrsv] MODULE [symbol=value]..."
+//usage:#define modprobe_full_usage "\n\n"
+//usage:       "Options:"
+//usage:     "\n       -r      Remove MODULE (stacks) or do autoclean"
+//usage:     "\n       -q      Quiet"
+//usage:     "\n       -v      Verbose"
+//usage:     "\n       -f      Force"
+//usage:     "\n       -w      Wait for unload"
+//usage:     "\n       -s      Report via syslog instead of stderr"
+
+//usage:#endif
+
 int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 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) {
-               xprint_and_close_file(xfopen("/proc/modules", "r"));
+               xprint_and_close_file(xfopen_for_read("/proc/modules"));
                return EXIT_SUCCESS;
        }
 
@@ -726,8 +765,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? */
@@ -774,8 +815,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.
@@ -796,17 +839,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);
+               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;
 }