Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 3 | * Modprobe written from scratch for BusyBox |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 4 | * |
Robert Griebl | 6859d76 | 2002-08-05 02:57:12 +0000 | [diff] [blame] | 5 | * Copyright (c) 2002 by Robert Griebl, griebl@gmx.de |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 6 | * Copyright (c) 2003 by Andrew Dennison, andrew.dennison@motec.com.au |
Robert Griebl | 6859d76 | 2002-08-05 02:57:12 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 23 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 24 | #include <sys/utsname.h> |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 25 | #include <getopt.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <unistd.h> |
| 28 | #include <syslog.h> |
| 29 | #include <string.h> |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 30 | #include <ctype.h> |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 31 | #include <fcntl.h> |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 32 | #include "busybox.h" |
| 33 | |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 34 | |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 35 | |
| 36 | struct dep_t { |
| 37 | char * m_module; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 38 | char * m_options; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 39 | |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 40 | int m_isalias : 1; |
| 41 | int m_reserved : 15; |
| 42 | |
| 43 | int m_depcnt : 16; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 44 | char ** m_deparr; |
| 45 | |
| 46 | struct dep_t * m_next; |
| 47 | }; |
| 48 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 49 | struct mod_list_t { |
| 50 | char * m_module; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 51 | char * m_options; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 52 | |
| 53 | struct mod_list_t * m_prev; |
| 54 | struct mod_list_t * m_next; |
| 55 | }; |
| 56 | |
| 57 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 58 | static struct dep_t *depend; |
| 59 | static int autoclean, show_only, quiet, do_syslog, verbose; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 60 | |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 61 | int parse_tag_value ( char *buffer, char **ptag, char **pvalue ) |
| 62 | { |
| 63 | char *tag, *value; |
| 64 | |
| 65 | while ( isspace ( *buffer )) |
| 66 | buffer++; |
| 67 | tag = value = buffer; |
| 68 | while ( !isspace ( *value )) |
| 69 | value++; |
| 70 | *value++ = 0; |
| 71 | while ( isspace ( *value )) |
| 72 | value++; |
| 73 | |
| 74 | *ptag = tag; |
| 75 | *pvalue = value; |
| 76 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 77 | return bb_strlen( tag ) && bb_strlen( value ); |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 78 | } |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 79 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 80 | /* Jump through hoops to simulate how fgets() grabs just one line at a |
| 81 | * time... Don't use any stdio since modprobe gets called from a kernel |
| 82 | * thread and stdio junk can overflow the limited stack... |
| 83 | */ |
| 84 | static char *reads ( int fd, char *buffer, size_t len ) |
| 85 | { |
| 86 | int n = read ( fd, buffer, len ); |
| 87 | |
| 88 | if ( n > 0 ) { |
| 89 | char *p; |
| 90 | |
| 91 | buffer [len-1] = 0; |
| 92 | p = strchr ( buffer, '\n' ); |
| 93 | |
| 94 | if ( p ) { |
| 95 | off_t offset; |
| 96 | |
| 97 | offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset |
| 98 | lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n |
| 99 | |
| 100 | p[1] = 0; |
| 101 | } |
| 102 | return buffer; |
| 103 | } |
| 104 | |
| 105 | else |
| 106 | return 0; |
| 107 | } |
| 108 | |
Eric Andersen | 864b797 | 2002-05-03 15:48:26 +0000 | [diff] [blame] | 109 | static struct dep_t *build_dep ( void ) |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 110 | { |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 111 | int fd; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 112 | struct utsname un; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 113 | struct dep_t *first = 0; |
| 114 | struct dep_t *current = 0; |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 115 | char buffer[256]; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 116 | char *filename = buffer; |
| 117 | int continuation_line = 0; |
| 118 | |
| 119 | if ( uname ( &un )) |
| 120 | return 0; |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 121 | |
| 122 | // check for buffer overflow in following code |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 123 | if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) { |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 124 | return 0; |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 125 | } |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 126 | |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 127 | strcpy ( filename, "/lib/modules/" ); |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 128 | strcat ( filename, un.release ); |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 129 | strcat ( filename, "/modules.dep" ); |
| 130 | |
Eric Andersen | 25ea42d | 2003-06-20 09:57:30 +0000 | [diff] [blame] | 131 | if (( fd = open ( filename, O_RDONLY )) < 0 ) { |
| 132 | |
| 133 | /* Ok, that didn't work. Fall back to looking in /lib/modules */ |
| 134 | if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) { |
| 135 | return 0; |
| 136 | } |
| 137 | } |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 138 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 139 | while ( reads ( fd, buffer, sizeof( buffer ))) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 140 | int l = bb_strlen ( buffer ); |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 141 | char *p = 0; |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 142 | |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 143 | while ( isspace ( buffer [l-1] )) { |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 144 | buffer [l-1] = 0; |
| 145 | l--; |
| 146 | } |
| 147 | |
| 148 | if ( l == 0 ) { |
| 149 | continuation_line = 0; |
| 150 | continue; |
| 151 | } |
| 152 | |
| 153 | if ( !continuation_line ) { |
| 154 | char *col = strchr ( buffer, ':' ); |
| 155 | |
| 156 | if ( col ) { |
| 157 | char *mods; |
| 158 | char *mod; |
Eric Andersen | 864b797 | 2002-05-03 15:48:26 +0000 | [diff] [blame] | 159 | int ext = 0; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 160 | |
| 161 | *col = 0; |
| 162 | mods = strrchr ( buffer, '/' ); |
| 163 | |
| 164 | if ( !mods ) |
| 165 | mods = buffer; |
| 166 | else |
| 167 | mods++; |
| 168 | |
Eric Andersen | 864b797 | 2002-05-03 15:48:26 +0000 | [diff] [blame] | 169 | if (( *(col-2) == '.' ) && ( *(col-1) == 'o' )) |
| 170 | ext = 2; |
| 171 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 172 | mod = bb_xstrndup ( mods, col - mods - ext ); |
Eric Andersen | 864b797 | 2002-05-03 15:48:26 +0000 | [diff] [blame] | 173 | |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 174 | if ( !current ) { |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 175 | first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t )); |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 176 | } |
| 177 | else { |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 178 | current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t )); |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 179 | current = current-> m_next; |
| 180 | } |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 181 | current-> m_module = mod; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 182 | current-> m_options = 0; |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 183 | current-> m_isalias = 0; |
| 184 | current-> m_depcnt = 0; |
| 185 | current-> m_deparr = 0; |
| 186 | current-> m_next = 0; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 187 | |
Eric Andersen | 864b797 | 2002-05-03 15:48:26 +0000 | [diff] [blame] | 188 | //printf ( "%s:\n", mod ); |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 189 | |
| 190 | p = col + 1; |
| 191 | } |
| 192 | else |
| 193 | p = 0; |
| 194 | } |
| 195 | else |
| 196 | p = buffer; |
| 197 | |
| 198 | if ( p && *p ) { |
| 199 | char *end = &buffer [l-1]; |
| 200 | char *deps = strrchr ( end, '/' ); |
| 201 | char *dep; |
Eric Andersen | 864b797 | 2002-05-03 15:48:26 +0000 | [diff] [blame] | 202 | int ext = 0; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 203 | |
| 204 | while ( isblank ( *end ) || ( *end == '\\' )) |
| 205 | end--; |
| 206 | |
| 207 | deps = strrchr ( p, '/' ); |
| 208 | |
| 209 | if ( !deps || ( deps < p )) { |
| 210 | deps = p; |
| 211 | |
| 212 | while ( isblank ( *deps )) |
| 213 | deps++; |
| 214 | } |
| 215 | else |
| 216 | deps++; |
| 217 | |
Eric Andersen | 864b797 | 2002-05-03 15:48:26 +0000 | [diff] [blame] | 218 | if (( *(end-1) == '.' ) && ( *end == 'o' )) |
| 219 | ext = 2; |
Eric Andersen | b493dec | 2002-07-02 19:14:23 +0000 | [diff] [blame] | 220 | |
| 221 | /* Cope with blank lines */ |
| 222 | if ((end-deps-ext+1) <= 0) |
| 223 | continue; |
Eric Andersen | 864b797 | 2002-05-03 15:48:26 +0000 | [diff] [blame] | 224 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 225 | dep = bb_xstrndup ( deps, end - deps - ext + 1 ); |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 226 | |
| 227 | current-> m_depcnt++; |
Eric Andersen | 864b797 | 2002-05-03 15:48:26 +0000 | [diff] [blame] | 228 | current-> m_deparr = (char **) xrealloc ( current-> m_deparr, sizeof ( char *) * current-> m_depcnt ); |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 229 | current-> m_deparr [current-> m_depcnt - 1] = dep; |
| 230 | |
Eric Andersen | 864b797 | 2002-05-03 15:48:26 +0000 | [diff] [blame] | 231 | //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] ); |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | if ( buffer [l-1] == '\\' ) |
| 235 | continuation_line = 1; |
| 236 | else |
| 237 | continuation_line = 0; |
| 238 | } |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 239 | close ( fd ); |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 240 | |
| 241 | // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) ! |
| 242 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 243 | if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 ) |
| 244 | if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 ) |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 245 | return first; |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 246 | |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 247 | continuation_line = 0; |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 248 | while ( reads ( fd, buffer, sizeof( buffer ))) { |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 249 | int l; |
| 250 | char *p; |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 251 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 252 | p = strchr ( buffer, '#' ); |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 253 | if ( p ) |
| 254 | *p = 0; |
| 255 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 256 | l = bb_strlen ( buffer ); |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 257 | |
| 258 | while ( l && isspace ( buffer [l-1] )) { |
| 259 | buffer [l-1] = 0; |
| 260 | l--; |
| 261 | } |
| 262 | |
| 263 | if ( l == 0 ) { |
| 264 | continuation_line = 0; |
| 265 | continue; |
| 266 | } |
| 267 | |
| 268 | if ( !continuation_line ) { |
| 269 | if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) { |
| 270 | char *alias, *mod; |
| 271 | |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 272 | if ( parse_tag_value ( buffer + 6, &alias, &mod )) { |
| 273 | // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod ); |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 274 | |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 275 | if ( !current ) { |
| 276 | first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t )); |
| 277 | } |
| 278 | else { |
| 279 | current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t )); |
| 280 | current = current-> m_next; |
| 281 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 282 | current-> m_module = bb_xstrdup ( alias ); |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 283 | current-> m_isalias = 1; |
| 284 | |
| 285 | if (( strcmp ( alias, "off" ) == 0 ) || ( strcmp ( alias, "null" ) == 0 )) { |
| 286 | current-> m_depcnt = 0; |
| 287 | current-> m_deparr = 0; |
| 288 | } |
| 289 | else { |
| 290 | current-> m_depcnt = 1; |
| 291 | current-> m_deparr = xmalloc ( 1 * sizeof( char * )); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 292 | current-> m_deparr[0] = bb_xstrdup ( mod ); |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 293 | } |
| 294 | current-> m_next = 0; |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 295 | } |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 296 | } |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 297 | else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) { |
| 298 | char *mod, *opt; |
| 299 | |
| 300 | if ( parse_tag_value ( buffer + 8, &mod, &opt )) { |
| 301 | struct dep_t *dt; |
| 302 | |
| 303 | for ( dt = first; dt; dt = dt-> m_next ) { |
| 304 | if ( strcmp ( dt-> m_module, mod ) == 0 ) |
| 305 | break; |
| 306 | } |
| 307 | if ( dt ) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 308 | dt-> m_options = xrealloc ( dt-> m_options, bb_strlen( opt ) + 1 ); |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 309 | strcpy ( dt-> m_options, opt ); |
| 310 | |
| 311 | // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_module, dt-> m_options ); |
| 312 | } |
| 313 | } |
| 314 | } |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 315 | } |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 316 | } |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 317 | close ( fd ); |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 318 | |
| 319 | return first; |
| 320 | } |
| 321 | |
| 322 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 323 | static int mod_process ( struct mod_list_t *list, int do_insert ) |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 324 | { |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 325 | char lcmd [256]; |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 326 | int rc = 1; |
Eric Andersen | 864b797 | 2002-05-03 15:48:26 +0000 | [diff] [blame] | 327 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 328 | while ( list ) { |
| 329 | if ( do_insert ) |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 330 | snprintf ( lcmd, sizeof( lcmd ) - 1, "insmod %s %s %s %s %s 2>/dev/null", do_syslog ? "-s" : "", autoclean ? "-k" : "", quiet ? "-q" : "", list-> m_module, list-> m_options ? list-> m_options : "" ); |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 331 | else |
| 332 | snprintf ( lcmd, sizeof( lcmd ) - 1, "rmmod %s %s 2>/dev/null", do_syslog ? "-s" : "", list-> m_module ); |
| 333 | |
| 334 | if ( verbose ) |
| 335 | printf ( "%s\n", lcmd ); |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 336 | if ( !show_only ) { |
| 337 | int rc2 = system ( lcmd ); |
| 338 | if (do_insert) rc = rc2; /* only last module matters */ |
| 339 | else if (!rc2) rc = 0; /* success if remove any mod */ |
| 340 | } |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 341 | |
| 342 | list = do_insert ? list-> m_prev : list-> m_next; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 343 | } |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 344 | return (show_only) ? 0 : rc; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 347 | static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail ) |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 348 | { |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 349 | struct mod_list_t *find; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 350 | struct dep_t *dt; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 351 | char *opt = 0; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 352 | int lm; |
| 353 | |
| 354 | // remove .o extension |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 355 | lm = bb_strlen ( mod ); |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 356 | if (( mod [lm-2] == '.' ) && ( mod [lm-1] == 'o' )) |
| 357 | mod [lm-2] = 0; |
| 358 | |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 359 | // check dependencies |
| 360 | for ( dt = depend; dt; dt = dt-> m_next ) { |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 361 | if ( strcmp ( dt-> m_module, mod ) == 0 ) { |
| 362 | opt = dt-> m_options; |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 363 | break; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 364 | } |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 365 | } |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 366 | |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 367 | // resolve alias names |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 368 | while ( dt && dt-> m_isalias ) { |
| 369 | if ( dt-> m_depcnt == 1 ) { |
| 370 | struct dep_t *adt; |
| 371 | |
| 372 | for ( adt = depend; adt; adt = adt-> m_next ) { |
Robert Griebl | 70112da | 2002-07-29 20:28:38 +0000 | [diff] [blame] | 373 | if ( strcmp ( adt-> m_module, dt-> m_deparr [0] ) == 0 ) |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 374 | break; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 375 | } |
Robert Griebl | 70112da | 2002-07-29 20:28:38 +0000 | [diff] [blame] | 376 | if ( adt ) { |
| 377 | dt = adt; |
| 378 | mod = dt-> m_module; |
| 379 | if ( !opt ) |
| 380 | opt = dt-> m_options; |
| 381 | } |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 382 | else |
Robert Griebl | 70112da | 2002-07-29 20:28:38 +0000 | [diff] [blame] | 383 | return; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 384 | } |
| 385 | else |
| 386 | return; |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 389 | // search for duplicates |
| 390 | for ( find = *head; find; find = find-> m_next ) { |
| 391 | if ( !strcmp ( mod, find-> m_module )) { |
| 392 | // found -> dequeue it |
| 393 | |
| 394 | if ( find-> m_prev ) |
| 395 | find-> m_prev-> m_next = find-> m_next; |
| 396 | else |
| 397 | *head = find-> m_next; |
| 398 | |
| 399 | if ( find-> m_next ) |
| 400 | find-> m_next-> m_prev = find-> m_prev; |
| 401 | else |
| 402 | *tail = find-> m_prev; |
| 403 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 404 | break; // there can be only one duplicate |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | if ( !find ) { // did not find a duplicate |
| 409 | find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t)); |
| 410 | find-> m_module = mod; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 411 | find-> m_options = opt; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | // enqueue at tail |
| 415 | if ( *tail ) |
| 416 | (*tail)-> m_next = find; |
| 417 | find-> m_prev = *tail; |
| 418 | find-> m_next = 0; |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 419 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 420 | if ( !*head ) |
| 421 | *head = find; |
| 422 | *tail = find; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 423 | |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 424 | if ( dt ) { |
| 425 | int i; |
| 426 | |
| 427 | for ( i = 0; i < dt-> m_depcnt; i++ ) |
| 428 | check_dep ( dt-> m_deparr [i], head, tail ); |
| 429 | } |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | |
| 433 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 434 | static int mod_insert ( char *mod, int argc, char **argv ) |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 435 | { |
| 436 | struct mod_list_t *tail = 0; |
| 437 | struct mod_list_t *head = 0; |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 438 | int rc; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 439 | |
| 440 | // get dep list for module mod |
| 441 | check_dep ( mod, &head, &tail ); |
| 442 | |
| 443 | if ( head && tail ) { |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 444 | if ( argc ) { |
| 445 | int i; |
| 446 | int l = 0; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 447 | |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 448 | // append module args |
| 449 | for ( i = 0; i < argc; i++ ) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 450 | l += ( bb_strlen ( argv [i] ) + 1 ); |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 451 | |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 452 | head-> m_options = xrealloc ( head-> m_options, l + 1 ); |
| 453 | head-> m_options [0] = 0; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 454 | |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 455 | for ( i = 0; i < argc; i++ ) { |
| 456 | strcat ( head-> m_options, argv [i] ); |
| 457 | strcat ( head-> m_options, " " ); |
| 458 | } |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 459 | } |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 460 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 461 | // process tail ---> head |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 462 | rc = mod_process ( tail, 1 ); |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 463 | } |
| 464 | else |
| 465 | rc = 1; |
| 466 | |
| 467 | return rc; |
| 468 | } |
| 469 | |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 470 | static int mod_remove ( char *mod ) |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 471 | { |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 472 | int rc; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 473 | static struct mod_list_t rm_a_dummy = { "-a", 0, 0 }; |
| 474 | |
| 475 | struct mod_list_t *head = 0; |
| 476 | struct mod_list_t *tail = 0; |
| 477 | |
| 478 | if ( mod ) |
| 479 | check_dep ( mod, &head, &tail ); |
| 480 | else // autoclean |
| 481 | head = tail = &rm_a_dummy; |
| 482 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 483 | if ( head && tail ) |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 484 | rc = mod_process ( head, 0 ); // process head ---> tail |
| 485 | else |
| 486 | rc = 1; |
| 487 | return rc; |
| 488 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 492 | |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 493 | extern int modprobe_main(int argc, char** argv) |
| 494 | { |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 495 | int opt; |
| 496 | int remove_opt = 0; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 497 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 498 | autoclean = show_only = quiet = do_syslog = verbose = 0; |
| 499 | |
| 500 | while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) { |
| 501 | switch(opt) { |
| 502 | case 'c': // no config used |
| 503 | case 'l': // no pattern matching |
| 504 | return EXIT_SUCCESS; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 505 | break; |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 506 | case 'C': // no config used |
| 507 | case 't': // no pattern matching |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 508 | bb_error_msg_and_die("-t and -C not supported"); |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 509 | |
| 510 | case 'a': // ignore |
| 511 | case 'd': // ignore |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 512 | break; |
| 513 | case 'k': |
| 514 | autoclean++; |
| 515 | break; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 516 | case 'n': |
| 517 | show_only++; |
| 518 | break; |
| 519 | case 'q': |
| 520 | quiet++; |
| 521 | break; |
| 522 | case 'r': |
Eric Andersen | 1b06419 | 2001-07-25 07:23:38 +0000 | [diff] [blame] | 523 | remove_opt++; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 524 | break; |
| 525 | case 's': |
| 526 | do_syslog++; |
| 527 | break; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 528 | case 'v': |
| 529 | verbose++; |
| 530 | break; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 531 | case 'V': |
| 532 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 533 | bb_show_usage(); |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 534 | break; |
| 535 | } |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 536 | } |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 537 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 538 | depend = build_dep ( ); |
| 539 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 540 | if ( !depend ) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 541 | bb_error_msg_and_die ( "could not parse modules.dep\n" ); |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 542 | |
Eric Andersen | 1b06419 | 2001-07-25 07:23:38 +0000 | [diff] [blame] | 543 | if (remove_opt) { |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 544 | int rc = EXIT_SUCCESS; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 545 | do { |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 546 | if (mod_remove ( optind < argc ? |
| 547 | bb_xstrdup (argv [optind]) : NULL )) { |
| 548 | bb_error_msg ("failed to remove module %s", |
| 549 | argv [optind] ); |
| 550 | rc = EXIT_FAILURE; |
| 551 | } |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 552 | } while ( ++optind < argc ); |
| 553 | |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 554 | return rc; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 557 | if (optind >= argc) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 558 | bb_error_msg_and_die ( "No module or pattern provided\n" ); |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 559 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 560 | if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 )) |
| 561 | bb_error_msg_and_die ( "failed to load module %s", argv [optind] ); |
Robert Griebl | 3ba7cee | 2002-12-03 22:41:36 +0000 | [diff] [blame] | 562 | |
| 563 | return EXIT_SUCCESS; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | |