Patch from Robert Griebl <griebl@gmx.de> to support modprobe -r properly,

merged in with the latest and greatest.
This commit is contained in:
Eric Andersen 2002-05-03 15:48:26 +00:00
parent 26920c6c94
commit 864b79791a

View File

@ -34,7 +34,7 @@ struct dep_t {
}; };
static struct dep_t *build_dep ( ) static struct dep_t *build_dep ( void )
{ {
struct utsname un; struct utsname un;
FILE *f; FILE *f;
@ -55,7 +55,7 @@ static struct dep_t *build_dep ( )
return 0; return 0;
while ( fgets ( buffer, sizeof( buffer), f )) { while ( fgets ( buffer, sizeof( buffer), f )) {
int l = strlen ( buffer ); int l = xstrlen ( buffer );
char *p = 0; char *p = 0;
if ( buffer [l-1] == '\n' ) { if ( buffer [l-1] == '\n' ) {
@ -74,6 +74,7 @@ static struct dep_t *build_dep ( )
if ( col ) { if ( col ) {
char *mods; char *mods;
char *mod; char *mod;
int ext = 0;
*col = 0; *col = 0;
mods = strrchr ( buffer, '/' ); mods = strrchr ( buffer, '/' );
@ -83,9 +84,10 @@ static struct dep_t *build_dep ( )
else else
mods++; mods++;
mod = (char *) malloc ( col - mods + 1 ); if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
strncpy ( mod, mods, col - mods ); ext = 2;
mod [col - mods] = 0;
mod = xstrndup ( mods, col - mods - ext );
if ( !current ) { if ( !current ) {
first = current = (struct dep_t *) malloc ( sizeof ( struct dep_t )); first = current = (struct dep_t *) malloc ( sizeof ( struct dep_t ));
@ -99,7 +101,7 @@ static struct dep_t *build_dep ( )
current-> m_deparr = 0; current-> m_deparr = 0;
current-> m_next = 0; current-> m_next = 0;
/* printf ( "%s:\n", mod ); */ //printf ( "%s:\n", mod );
p = col + 1; p = col + 1;
} }
@ -113,6 +115,7 @@ static struct dep_t *build_dep ( )
char *end = &buffer [l-1]; char *end = &buffer [l-1];
char *deps = strrchr ( end, '/' ); char *deps = strrchr ( end, '/' );
char *dep; char *dep;
int ext = 0;
while ( isblank ( *end ) || ( *end == '\\' )) while ( isblank ( *end ) || ( *end == '\\' ))
end--; end--;
@ -128,15 +131,16 @@ static struct dep_t *build_dep ( )
else else
deps++; deps++;
dep = (char *) malloc ( end - deps + 2 ); if (( *(end-1) == '.' ) && ( *end == 'o' ))
strncpy ( dep, deps, end - deps + 1 ); ext = 2;
dep [end - deps + 1] = 0;
dep = xstrndup ( deps, end - deps - ext + 1 );
current-> m_depcnt++; current-> m_depcnt++;
current-> m_deparr = (char **) realloc ( current-> m_deparr, sizeof ( char *) * current-> m_depcnt ); current-> m_deparr = (char **) xrealloc ( current-> m_deparr, sizeof ( char *) * current-> m_depcnt );
current-> m_deparr [current-> m_depcnt - 1] = dep; current-> m_deparr [current-> m_depcnt - 1] = dep;
/* printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] ); */ //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
} }
if ( buffer [l-1] == '\\' ) if ( buffer [l-1] == '\\' )
@ -152,26 +156,33 @@ static struct dep_t *build_dep ( )
static struct dep_t *find_dep ( struct dep_t *dt, char *mod ) static struct dep_t *find_dep ( struct dep_t *dt, char *mod )
{ {
int lm = strlen ( mod ); int lm = xstrlen ( mod );
int hasext = 0; int extpos = 0;
if (( mod [lm-2] == '.' ) && ( mod [lm-1] == 'o' )) if (( mod [lm-2] == '.' ) && ( mod [lm-1] == 'o' ))
hasext = 1; extpos = 2;
if ( extpos > 0 )
mod [lm - extpos] = 0;
while ( dt ) { while ( dt ) {
if ( hasext && !strcmp ( dt-> m_module, mod )) if ( !strcmp ( dt-> m_module, mod ))
break;
else if ( !hasext && !strncmp ( dt-> m_module, mod, strlen ( dt-> m_module ) - 2 ))
break; break;
dt = dt-> m_next; dt = dt-> m_next;
} }
if ( extpos > 0 )
mod [lm - extpos] = '.';
return dt; return dt;
} }
#define MODPROBE_EXECUTE 0x1
#define MODPROBE_INSERT 0x2
#define MODPROBE_REMOVE 0x4
static void check_dep ( char *mod, int do_syslog, static void check_dep ( char *mod, int do_syslog,
int show_only, int verbose, int recursing ) int show_only, int verbose, int flags )
{ {
static struct dep_t *depend = (struct dep_t *) -1; static struct dep_t *depend = (struct dep_t *) -1;
struct dep_t *dt; struct dep_t *dt;
@ -179,25 +190,30 @@ static void check_dep ( char *mod, int do_syslog,
if ( depend == (struct dep_t *) -1 ) if ( depend == (struct dep_t *) -1 )
depend = build_dep ( ); depend = build_dep ( );
/* printf ( "CHECK: %s (%p)\n", mod, depend ); */
if (( dt = find_dep ( depend, mod ))) { if (( dt = find_dep ( depend, mod ))) {
int i; int i;
for ( i = 0; i < dt-> m_depcnt; i++ ) for ( i = 0; i < dt-> m_depcnt; i++ )
check_dep ( dt-> m_deparr [i], do_syslog, check_dep ( dt-> m_deparr [i], do_syslog,
show_only, verbose, 1); show_only, verbose, flags|MODPROBE_EXECUTE);
} }
if ( recursing ) { if ( flags & MODPROBE_EXECUTE ) {
char lcmd [256]; char lcmd [256];
if ( flags & MODPROBE_INSERT ) {
snprintf(lcmd, sizeof(lcmd)-1, "insmod %s -q -k %s 2>/dev/null", snprintf(lcmd, sizeof(lcmd)-1, "insmod %s -q -k %s 2>/dev/null",
do_syslog ? "-s" : "", mod ); do_syslog ? "-s" : "", mod );
if (show_only || verbose) }
if ( flags & MODPROBE_REMOVE ) {
snprintf(lcmd, sizeof(lcmd)-1, "insmod %s -q -k %s 2>/dev/null",
do_syslog ? "-s" : "", mod );
}
if ( flags & (MODPROBE_REMOVE|MODPROBE_INSERT) ) {
if (verbose)
printf("%s\n", lcmd); printf("%s\n", lcmd);
if (!show_only) if (!show_only)
system ( lcmd ); system ( lcmd );
} }
}
} }
#endif #endif
@ -273,10 +289,15 @@ extern int modprobe_main(int argc, char** argv)
optind < argc ? argv[optind] : ""); optind < argc ? argv[optind] : "");
if (do_syslog) if (do_syslog)
syslog(LOG_INFO, "%s", cmd); syslog(LOG_INFO, "%s", cmd);
if (show_only || verbose) if (verbose)
printf("%s\n", cmd); printf("%s\n", cmd);
if (!show_only) if (!show_only)
rc = system(cmd); rc = system(cmd);
#ifdef CONFIG_MODPROBE_DEPEND
if ( optind < argc )
check_dep ( argv [optind], do_syslog, show_only, verbose, MODPROBE_REMOVE);
#endif
} while (++optind < argc); } while (++optind < argc);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
@ -292,7 +313,7 @@ extern int modprobe_main(int argc, char** argv)
autoclean ? "-k" : ""); autoclean ? "-k" : "");
#ifdef CONFIG_MODPROBE_DEPEND #ifdef CONFIG_MODPROBE_DEPEND
check_dep ( argv [optind], do_syslog, show_only, verbose, 0); check_dep ( argv [optind], do_syslog, show_only, verbose, MODPROBE_INSERT);
#endif #endif
while (optind < argc) { while (optind < argc) {
@ -302,7 +323,7 @@ extern int modprobe_main(int argc, char** argv)
} }
if (do_syslog) if (do_syslog)
syslog(LOG_INFO, "%s", cmd); syslog(LOG_INFO, "%s", cmd);
if (show_only || verbose) if (verbose)
printf("%s\n", cmd); printf("%s\n", cmd);
if (!show_only) if (!show_only)
rc = system(cmd); rc = system(cmd);