cpio: testsuite for http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=466771
[oweals/busybox.git] / modutils / modprobe.c
index 60dc926656ff43b914f33ff64bf216d3df76099a..1a4f5d4d42c60f404a4a316ef46919cf1c9ff373 100644 (file)
@@ -24,18 +24,18 @@ struct mod_opt_t {  /* one-way list of options to pass to a module */
 
 struct dep_t { /* one-way list of dependency rules */
        /* a dependency rule */
-       char *  m_name;                         /* the module name*/
-       char *  m_path;                         /* the module file path */
-       struct mod_opt_t *  m_options;          /* the module options */
+       char *  m_name;                     /* the module name*/
+       char *  m_path;                     /* the module file path */
+       struct mod_opt_t *  m_options;      /* the module options */
 
-       int     m_isalias  : 1;                 /* the module is an alias */
-       int     m_isblacklisted : 1;
-       int     m_reserved : 14;                /* stuffin' */
+       unsigned int m_isalias      :1;     /* the module is an alias */
+       unsigned int m_isblacklisted:1;     /* the module is blacklisted */
+       unsigned int m_reserved     :14;    /* stuffin' */
 
-       int     m_depcnt   : 16;                /* the number of dependable module(s) */
-       char ** m_deparr;                       /* the list of dependable module(s) */
+       unsigned int m_depcnt       :16;    /* the number of dependable module(s) */
+       char ** m_deparr;                   /* the list of dependable module(s) */
 
-       struct dep_t * m_next;                  /* the next dependency rule */
+       struct dep_t * m_next;              /* the next dependency rule */
 };
 
 struct mod_list_t {    /* two-way list of modules to process */
@@ -232,10 +232,10 @@ static char *parse_command_string(char *src, char **dst)
 #endif /* ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
 
 static int is_conf_command(char *buffer, const char *command)
-{ 
+{
        int len = strlen(command);
        return ((strstr(buffer, command) == buffer) &&
-                       isspace(buffer[len])); 
+                       isspace(buffer[len]));
 }
 
 /*
@@ -325,14 +325,14 @@ static void include_conf(struct dep_t **first, struct dep_t **current, char *buf
                                include_conf(first, current, buffer, buflen, fdi);
                                close(fdi);
                        }
-               } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST && 
+               } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST &&
                                (is_conf_command(buffer, "blacklist"))) {
                        char *mod;
                        struct dep_t *dt;
 
                        mod = skip_whitespace(buffer + 10);
                        for (dt = *first; dt; dt = dt->m_next) {
-                               if (dt->m_isalias && strcmp(dt->m_deparr[0], mod) == 0)
+                               if (strcmp(dt->m_name, mod) == 0)
                                        break;
                        }
                        if (dt)
@@ -364,15 +364,15 @@ static struct dep_t *build_dep(void)
                k_version = un.release[2] - '0';
        }
 
-       filename = xasprintf("/lib/modules/%s/modules.dep", un.release);
+       filename = xasprintf(CONFIG_DEFAULT_MODULES_DIR"/%s/"CONFIG_DEFAULT_DEPMOD_FILE, un.release);
        fd = open(filename, O_RDONLY);
        if (ENABLE_FEATURE_CLEAN_UP)
                free(filename);
        if (fd < 0) {
                /* Ok, that didn't work.  Fall back to looking in /lib/modules */
-               fd = open("/lib/modules/modules.dep", O_RDONLY);
+               fd = open(CONFIG_DEFAULT_MODULES_DIR"/"CONFIG_DEFAULT_DEPMOD_FILE, O_RDONLY);
                if (fd < 0) {
-                       bb_error_msg_and_die("cannot parse modules.dep");
+                       bb_error_msg_and_die("cannot parse " CONFIG_DEFAULT_DEPMOD_FILE);
                }
        }
 
@@ -443,11 +443,8 @@ static struct dep_t *build_dep(void)
                        /* It's a dep description continuation */
                        p = line_buffer;
 
-               while (p && *p && isblank(*p))
-                       p++;
-
                /* p points to the first dependable module; if NULL, no dependable module */
-               if (p && *p) {
+               if (p && (p = skip_whitespace(p))[0] != '\0') {
                        char *end = &line_buffer[l-1];
                        const char *deps;
                        char *dep;
@@ -468,10 +465,8 @@ static struct dep_t *build_dep(void)
 
                                /* find the beginning of the module file name */
                                deps = bb_basename(p);
-                               if (deps == p) {
-                                       while (isblank(*deps))
-                                               deps++;
-                               }
+                               if (deps == p)
+                                       deps = skip_whitespace(deps);
 
                                /* find the end of the module name in the file name */
                                if (ENABLE_FEATURE_2_6_MODULES
@@ -487,10 +482,8 @@ static struct dep_t *build_dep(void)
                                dep = xstrndup(deps, next - deps - ext + 1);
 
                                /* Add the new dependable module name */
-                               current->m_depcnt++;
-                               current->m_deparr = xrealloc(current->m_deparr,
-                                               sizeof(char *) * current->m_depcnt);
-                               current->m_deparr[current->m_depcnt - 1] = dep;
+                               current->m_deparr = xrealloc_vector(current->m_deparr, 2, current->m_depcnt);
+                               current->m_deparr[current->m_depcnt++] = dep;
 
                                p = next + 2;
                        } while (next < end);
@@ -522,11 +515,11 @@ static struct dep_t *build_dep(void)
        /* Only 2.6 has a modules.alias file */
        if (ENABLE_FEATURE_2_6_MODULES) {
                /* Parse kernel-declared module aliases */
-               filename = xasprintf("/lib/modules/%s/modules.alias", un.release);
+               filename = xasprintf(CONFIG_DEFAULT_MODULES_DIR"/%s/modules.alias", un.release);
                fd = open(filename, O_RDONLY);
                if (fd < 0) {
                        /* Ok, that didn't work.  Fall back to looking in /lib/modules */
-                       fd = open("/lib/modules/modules.alias", O_RDONLY);
+                       fd = open(CONFIG_DEFAULT_MODULES_DIR"/modules.alias", O_RDONLY);
                }
                if (ENABLE_FEATURE_CLEAN_UP)
                        free(filename);
@@ -537,11 +530,11 @@ static struct dep_t *build_dep(void)
                }
 
                /* Parse kernel-declared symbol aliases */
-               filename = xasprintf("/lib/modules/%s/modules.symbols", un.release);
+               filename = xasprintf(CONFIG_DEFAULT_MODULES_DIR"/%s/modules.symbols", un.release);
                fd = open(filename, O_RDONLY);
                if (fd < 0) {
                        /* Ok, that didn't work.  Fall back to looking in /lib/modules */
-                       fd = open("/lib/modules/modules.symbols", O_RDONLY);
+                       fd = open(CONFIG_DEFAULT_MODULES_DIR"/modules.symbols", O_RDONLY);
                }
                if (ENABLE_FEATURE_CLEAN_UP)
                        free(filename);
@@ -748,12 +741,13 @@ static void check_dep(char *mod, struct mod_list_t **head, struct mod_list_t **t
 
        // resolve alias names
        while (dt->m_isalias) {
-               if (dt->m_depcnt == 1 && !(ENABLE_FEATURE_MODPROBE_BLACKLIST &&
-                               dt->m_isblacklisted)) {
+               if (dt->m_depcnt == 1) {
                        struct dep_t *adt;
 
                        for (adt = depend; adt; adt = adt->m_next) {
-                               if (check_pattern(adt->m_name, dt->m_deparr[0]) == 0)
+                               if (check_pattern(adt->m_name, dt->m_deparr[0]) == 0 &&
+                                               !(ENABLE_FEATURE_MODPROBE_BLACKLIST &&
+                                                       adt->m_isblacklisted))
                                        break;
                        }
                        if (adt) {
@@ -894,13 +888,13 @@ int modprobe_main(int argc, char **argv)
        depend = build_dep();
 
        if (!depend)
-               bb_error_msg_and_die("cannot parse modules.dep");
+               bb_error_msg_and_die("cannot parse "CONFIG_DEFAULT_DEPMOD_FILE);
 
        if (remove_opt) {
                do {
                        /* argv[optind] can be NULL here */
                        if (mod_remove(argv[optind])) {
-                               bb_error_msg("failed to remove module %s",
+                               bb_error_msg("failed to %s module %s", "remove",
                                                argv[optind]);
                                rc = EXIT_FAILURE;
                        }
@@ -910,7 +904,7 @@ int modprobe_main(int argc, char **argv)
                        bb_error_msg_and_die("no module or pattern provided");
 
                if (mod_insert(argv[optind], argc - optind - 1, argv + optind + 1))
-                       bb_error_msg_and_die("failed to load module %s", argv[optind]);
+                       bb_error_msg_and_die("failed to %s module %s", "load", argv[optind]);
        }
 
        /* Here would be a good place to free up memory allocated during the dependencies build. */