Patch from Denis Vlasenko, tweak error messages.
[oweals/busybox.git] / modutils / modprobe.c
index 32a37ce1711226742284cf82a5d2c8dd93f070a6..93e5102939789dc9089de318fdec4d3f21f30bb2 100644 (file)
@@ -11,6 +11,7 @@
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
 */
 
+#include "busybox.h"
 #include <sys/utsname.h>
 #include <sys/types.h>
 #include <sys/wait.h>
@@ -21,7 +22,7 @@
 #include <string.h>
 #include <ctype.h>
 #include <fcntl.h>
-#include "busybox.h"
+#include <fnmatch.h>
 
 struct mod_opt_t {     /* one-way list of options to pass to a module */
        char *  m_opt_val;
@@ -283,7 +284,7 @@ static void include_conf ( struct dep_t **first, struct dep_t **current, char *b
                if ( p )
                        *p = 0;
 
-               l = bb_strlen ( buffer );
+               l = strlen ( buffer );
 
                while ( l && isspace ( buffer [l-1] )) {
                        buffer [l-1] = 0;
@@ -364,8 +365,8 @@ static void include_conf ( struct dep_t **first, struct dep_t **current, char *b
 }
 
 /*
- * This function builds a list of dependency rules from /lib/modules/`uname -r\modules.dep.
- * It then fills every modules and aliases with their  default options, found by parsing
+ * This function builds a list of dependency rules from /lib/modules/`uname -r`\modules.dep.
+ * It then fills every modules and aliases with their default options, found by parsing
  * modprobe.conf (or modules.conf, or conf.modules).
  */
 static struct dep_t *build_dep ( void )
@@ -388,18 +389,18 @@ static struct dep_t *build_dep ( void )
        }
 
        filename = bb_xasprintf("/lib/modules/%s/modules.dep", un.release );
-
-       if (( fd = open ( filename, O_RDONLY )) < 0 ) {
-
+       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 */
                if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
                        return 0;
                }
        }
-       free(filename);
 
        while ( reads ( fd, buffer, sizeof( buffer ))) {
-               int l = bb_strlen ( buffer );
+               int l = strlen ( buffer );
                char *p = 0;
 
                while ( l > 0 && isspace ( buffer [l-1] )) {
@@ -553,16 +554,16 @@ static struct dep_t *build_dep ( void )
        include_conf (&first, &current, buffer, sizeof(buffer), fd);
        close(fd);
 
-       filename = bb_xasprintf("/lib/modules/%s/modules.alias", un.release );
-
-       if (( fd = open ( filename, O_RDONLY )) < 0 ) {
-
+       filename = bb_xasprintf("/lib/modules/%s/modules.alias", 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 */
                if (( fd = open ( "/lib/modules/modules.alias", O_RDONLY )) < 0 ) {
                        return first;
                }
        }
-       free(filename);
 
        include_conf (&first, &current, buffer, sizeof(buffer), fd);
        close(fd);
@@ -721,9 +722,13 @@ static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t *
        struct mod_opt_t *opt = 0;
        char *path = 0;
 
-       // check dependencies
+       /* Search for the given module name amongst all dependency rules.
+        * The module name in a dependency rule can be a shell pattern,
+        * so try to match the given module name against such a pattern.
+        * Of course if the name in the dependency rule is a plain string,
+        * then we consider it a pattern, and matching will still work. */
        for ( dt = depend; dt; dt = dt-> m_next ) {
-               if ( strcmp ( dt-> m_name, mod ) == 0) {
+               if ( fnmatch ( dt-> m_name, mod, 0 ) == 0) {
                        break;
                }
        }
@@ -875,7 +880,7 @@ int modprobe_main(int argc, char** argv)
        depend = build_dep ( );
 
        if ( !depend )
-               bb_error_msg_and_die ( "could not parse modules.dep\n" );
+               bb_error_msg_and_die ( "could not parse modules.dep" );
 
        if (remove_opt) {
                do {
@@ -888,7 +893,7 @@ int modprobe_main(int argc, char** argv)
                } while ( ++optind < argc );
        } else {
                if (optind >= argc)
-                       bb_error_msg_and_die ( "No module or pattern provided\n" );
+                       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] );