blob: d48f36ed196f28d20038dd94711b6429ce511c3d [file] [log] [blame]
Eric Andersen0139ca92001-07-22 23:01:03 +00001/* vi: set sw=4 ts=4: */
2/*
Robert Grieblaead70b2002-07-26 15:54:20 +00003 * Modprobe written from scratch for BusyBox
Eric Andersen60e56f52002-04-26 06:04:01 +00004 *
Robert Griebl6859d762002-08-05 02:57:12 +00005 * Copyright (c) 2002 by Robert Griebl, griebl@gmx.de
Eric Andersen908e3622003-06-20 09:56:37 +00006 * Copyright (c) 2003 by Andrew Dennison, andrew.dennison@motec.com.au
Robert Griebl6859d762002-08-05 02:57:12 +00007 *
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 Andersen0139ca92001-07-22 23:01:03 +000023
Robert Griebl52e8d062002-05-14 23:42:08 +000024#include <sys/utsname.h>
Eric Andersen0139ca92001-07-22 23:01:03 +000025#include <getopt.h>
26#include <stdlib.h>
27#include <unistd.h>
28#include <syslog.h>
29#include <string.h>
Eric Andersen60e56f52002-04-26 06:04:01 +000030#include <ctype.h>
Eric Andersenc06391b2002-06-04 13:28:43 +000031#include <fcntl.h>
Eric Andersen0139ca92001-07-22 23:01:03 +000032#include "busybox.h"
33
Eric Andersen0139ca92001-07-22 23:01:03 +000034
Eric Andersen60e56f52002-04-26 06:04:01 +000035
36struct dep_t {
37 char * m_module;
Robert Grieblaead70b2002-07-26 15:54:20 +000038 char * m_options;
Robert Griebl52e8d062002-05-14 23:42:08 +000039
Robert Griebl1d4ef2a2002-05-28 21:32:10 +000040 int m_isalias : 1;
41 int m_reserved : 15;
42
43 int m_depcnt : 16;
Eric Andersen60e56f52002-04-26 06:04:01 +000044 char ** m_deparr;
45
46 struct dep_t * m_next;
47};
48
Robert Griebl52e8d062002-05-14 23:42:08 +000049struct mod_list_t {
50 char * m_module;
Robert Grieblaead70b2002-07-26 15:54:20 +000051 char * m_options;
Robert Griebl52e8d062002-05-14 23:42:08 +000052
53 struct mod_list_t * m_prev;
54 struct mod_list_t * m_next;
55};
56
57
Robert Griebl236abbf2002-05-22 23:34:35 +000058static struct dep_t *depend;
59static int autoclean, show_only, quiet, do_syslog, verbose;
Robert Griebl52e8d062002-05-14 23:42:08 +000060
Robert Grieblaead70b2002-07-26 15:54:20 +000061int 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 cad53642003-03-19 09:13:01 +000077 return bb_strlen( tag ) && bb_strlen( value );
Robert Grieblaead70b2002-07-26 15:54:20 +000078}
Eric Andersen60e56f52002-04-26 06:04:01 +000079
Robert Grieblbc28f7a2002-06-04 19:33:58 +000080/* 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 */
84static 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 Andersen864b7972002-05-03 15:48:26 +0000109static struct dep_t *build_dep ( void )
Eric Andersen60e56f52002-04-26 06:04:01 +0000110{
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000111 int fd;
Eric Andersen60e56f52002-04-26 06:04:01 +0000112 struct utsname un;
Eric Andersen60e56f52002-04-26 06:04:01 +0000113 struct dep_t *first = 0;
114 struct dep_t *current = 0;
Eric Andersenc06391b2002-06-04 13:28:43 +0000115 char buffer[256];
Eric Andersen60e56f52002-04-26 06:04:01 +0000116 char *filename = buffer;
117 int continuation_line = 0;
118
119 if ( uname ( &un ))
120 return 0;
Robert Griebl236abbf2002-05-22 23:34:35 +0000121
122 // check for buffer overflow in following code
Manuel Novoa III cad53642003-03-19 09:13:01 +0000123 if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
Robert Griebl236abbf2002-05-22 23:34:35 +0000124 return 0;
Eric Andersenc06391b2002-06-04 13:28:43 +0000125 }
Robert Griebl236abbf2002-05-22 23:34:35 +0000126
Eric Andersen60e56f52002-04-26 06:04:01 +0000127 strcpy ( filename, "/lib/modules/" );
Eric Andersenc06391b2002-06-04 13:28:43 +0000128 strcat ( filename, un.release );
Eric Andersen60e56f52002-04-26 06:04:01 +0000129 strcat ( filename, "/modules.dep" );
130
Eric Andersen25ea42d2003-06-20 09:57:30 +0000131 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 Andersenc06391b2002-06-04 13:28:43 +0000138
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000139 while ( reads ( fd, buffer, sizeof( buffer ))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140 int l = bb_strlen ( buffer );
Eric Andersen60e56f52002-04-26 06:04:01 +0000141 char *p = 0;
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000142
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000143 while ( isspace ( buffer [l-1] )) {
Eric Andersen60e56f52002-04-26 06:04:01 +0000144 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 Andersen864b7972002-05-03 15:48:26 +0000159 int ext = 0;
Eric Andersen60e56f52002-04-26 06:04:01 +0000160
161 *col = 0;
162 mods = strrchr ( buffer, '/' );
163
164 if ( !mods )
165 mods = buffer;
166 else
167 mods++;
168
Eric Andersen864b7972002-05-03 15:48:26 +0000169 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
170 ext = 2;
171
Manuel Novoa III cad53642003-03-19 09:13:01 +0000172 mod = bb_xstrndup ( mods, col - mods - ext );
Eric Andersen864b7972002-05-03 15:48:26 +0000173
Eric Andersen60e56f52002-04-26 06:04:01 +0000174 if ( !current ) {
Eric Andersenc06391b2002-06-04 13:28:43 +0000175 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
Eric Andersen60e56f52002-04-26 06:04:01 +0000176 }
177 else {
Eric Andersenc06391b2002-06-04 13:28:43 +0000178 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
Eric Andersen60e56f52002-04-26 06:04:01 +0000179 current = current-> m_next;
180 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000181 current-> m_module = mod;
Robert Grieblaead70b2002-07-26 15:54:20 +0000182 current-> m_options = 0;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000183 current-> m_isalias = 0;
184 current-> m_depcnt = 0;
185 current-> m_deparr = 0;
186 current-> m_next = 0;
Eric Andersen60e56f52002-04-26 06:04:01 +0000187
Eric Andersen864b7972002-05-03 15:48:26 +0000188 //printf ( "%s:\n", mod );
Eric Andersen60e56f52002-04-26 06:04:01 +0000189
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 Andersen864b7972002-05-03 15:48:26 +0000202 int ext = 0;
Eric Andersen60e56f52002-04-26 06:04:01 +0000203
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 Andersen864b7972002-05-03 15:48:26 +0000218 if (( *(end-1) == '.' ) && ( *end == 'o' ))
219 ext = 2;
Eric Andersenb493dec2002-07-02 19:14:23 +0000220
221 /* Cope with blank lines */
222 if ((end-deps-ext+1) <= 0)
223 continue;
Eric Andersen864b7972002-05-03 15:48:26 +0000224
Manuel Novoa III cad53642003-03-19 09:13:01 +0000225 dep = bb_xstrndup ( deps, end - deps - ext + 1 );
Eric Andersen60e56f52002-04-26 06:04:01 +0000226
227 current-> m_depcnt++;
Eric Andersen864b7972002-05-03 15:48:26 +0000228 current-> m_deparr = (char **) xrealloc ( current-> m_deparr, sizeof ( char *) * current-> m_depcnt );
Eric Andersen60e56f52002-04-26 06:04:01 +0000229 current-> m_deparr [current-> m_depcnt - 1] = dep;
230
Eric Andersen864b7972002-05-03 15:48:26 +0000231 //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
Eric Andersen60e56f52002-04-26 06:04:01 +0000232 }
233
234 if ( buffer [l-1] == '\\' )
235 continuation_line = 1;
236 else
237 continuation_line = 0;
238 }
Eric Andersenc06391b2002-06-04 13:28:43 +0000239 close ( fd );
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000240
241 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
242
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000243 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
244 if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
Eric Andersenc06391b2002-06-04 13:28:43 +0000245 return first;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000246
Eric Andersenc06391b2002-06-04 13:28:43 +0000247 continuation_line = 0;
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000248 while ( reads ( fd, buffer, sizeof( buffer ))) {
Eric Andersenc06391b2002-06-04 13:28:43 +0000249 int l;
250 char *p;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000251
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000252 p = strchr ( buffer, '#' );
Eric Andersenc06391b2002-06-04 13:28:43 +0000253 if ( p )
254 *p = 0;
255
Manuel Novoa III cad53642003-03-19 09:13:01 +0000256 l = bb_strlen ( buffer );
Eric Andersenc06391b2002-06-04 13:28:43 +0000257
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 Grieblaead70b2002-07-26 15:54:20 +0000272 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
273 // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod );
Eric Andersenc06391b2002-06-04 13:28:43 +0000274
Robert Grieblaead70b2002-07-26 15:54:20 +0000275 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 cad53642003-03-19 09:13:01 +0000282 current-> m_module = bb_xstrdup ( alias );
Robert Grieblaead70b2002-07-26 15:54:20 +0000283 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 cad53642003-03-19 09:13:01 +0000292 current-> m_deparr[0] = bb_xstrdup ( mod );
Robert Grieblaead70b2002-07-26 15:54:20 +0000293 }
294 current-> m_next = 0;
Eric Andersenc06391b2002-06-04 13:28:43 +0000295 }
Eric Andersenc06391b2002-06-04 13:28:43 +0000296 }
Robert Grieblaead70b2002-07-26 15:54:20 +0000297 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 cad53642003-03-19 09:13:01 +0000308 dt-> m_options = xrealloc ( dt-> m_options, bb_strlen( opt ) + 1 );
Robert Grieblaead70b2002-07-26 15:54:20 +0000309 strcpy ( dt-> m_options, opt );
310
311 // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_module, dt-> m_options );
312 }
313 }
314 }
Eric Andersenc06391b2002-06-04 13:28:43 +0000315 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000316 }
Eric Andersenc06391b2002-06-04 13:28:43 +0000317 close ( fd );
Eric Andersen60e56f52002-04-26 06:04:01 +0000318
319 return first;
320}
321
Glenn L McGrath350733a2003-09-08 00:32:49 +0000322/* return 1 = loaded, 0 = not loaded, -1 = can't tell */
323static int already_loaded (const char *name)
324{
325 int fd;
326 char buffer[256];
327
328 fd = open ("/proc/modules", O_RDONLY);
329 if (fd < 0)
330 return -1;
331
332 while ( reads ( fd, buffer, sizeof( buffer ))) {
333 char *p;
334
335 p = strchr (buffer, ' ');
336 if (p) {
337 *p = 0;
338 if (strcmp (name, buffer) == 0) {
339 close (fd);
340 return 1;
341 }
342 }
343 }
344
345 close (fd);
346 return 0;
347}
Eric Andersen60e56f52002-04-26 06:04:01 +0000348
Robert Griebl236abbf2002-05-22 23:34:35 +0000349static int mod_process ( struct mod_list_t *list, int do_insert )
Eric Andersen60e56f52002-04-26 06:04:01 +0000350{
Robert Griebl52e8d062002-05-14 23:42:08 +0000351 char lcmd [256];
Eric Andersen908e3622003-06-20 09:56:37 +0000352 int rc = 1;
Eric Andersen864b7972002-05-03 15:48:26 +0000353
Robert Griebl52e8d062002-05-14 23:42:08 +0000354 while ( list ) {
Eric Andersen61b038a2003-11-14 02:49:19 +0000355 *lcmd = '\0';
Glenn L McGrath350733a2003-09-08 00:32:49 +0000356 if ( do_insert ) {
357 if (already_loaded (list->m_module) != 1)
358 snprintf ( lcmd, sizeof( lcmd ) - 1, "insmod %s %s %s %s %s", do_syslog ? "-s" : "", autoclean ? "-k" : "", quiet ? "-q" : "", list-> m_module, list-> m_options ? list-> m_options : "" );
359 } else {
360 if (already_loaded (list->m_module) != 0)
361 snprintf ( lcmd, sizeof( lcmd ) - 1, "rmmod %s %s", do_syslog ? "-s" : "", list-> m_module );
362 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000363
364 if ( verbose )
365 printf ( "%s\n", lcmd );
Eric Andersen61b038a2003-11-14 02:49:19 +0000366 if ( !show_only && *lcmd) {
Eric Andersen908e3622003-06-20 09:56:37 +0000367 int rc2 = system ( lcmd );
368 if (do_insert) rc = rc2; /* only last module matters */
369 else if (!rc2) rc = 0; /* success if remove any mod */
370 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000371
372 list = do_insert ? list-> m_prev : list-> m_next;
Eric Andersen60e56f52002-04-26 06:04:01 +0000373 }
Eric Andersen908e3622003-06-20 09:56:37 +0000374 return (show_only) ? 0 : rc;
Eric Andersen60e56f52002-04-26 06:04:01 +0000375}
376
Robert Griebl52e8d062002-05-14 23:42:08 +0000377static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
Eric Andersen60e56f52002-04-26 06:04:01 +0000378{
Robert Griebl52e8d062002-05-14 23:42:08 +0000379 struct mod_list_t *find;
Eric Andersen60e56f52002-04-26 06:04:01 +0000380 struct dep_t *dt;
Robert Grieblaead70b2002-07-26 15:54:20 +0000381 char *opt = 0;
Robert Griebl52e8d062002-05-14 23:42:08 +0000382 int lm;
383
384 // remove .o extension
Manuel Novoa III cad53642003-03-19 09:13:01 +0000385 lm = bb_strlen ( mod );
Robert Griebl52e8d062002-05-14 23:42:08 +0000386 if (( mod [lm-2] == '.' ) && ( mod [lm-1] == 'o' ))
387 mod [lm-2] = 0;
388
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000389 // check dependencies
390 for ( dt = depend; dt; dt = dt-> m_next ) {
Robert Grieblaead70b2002-07-26 15:54:20 +0000391 if ( strcmp ( dt-> m_module, mod ) == 0 ) {
392 opt = dt-> m_options;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000393 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000394 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000395 }
Robert Grieblaead70b2002-07-26 15:54:20 +0000396
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000397 // resolve alias names
Robert Grieblaead70b2002-07-26 15:54:20 +0000398 while ( dt && dt-> m_isalias ) {
399 if ( dt-> m_depcnt == 1 ) {
400 struct dep_t *adt;
401
402 for ( adt = depend; adt; adt = adt-> m_next ) {
Robert Griebl70112da2002-07-29 20:28:38 +0000403 if ( strcmp ( adt-> m_module, dt-> m_deparr [0] ) == 0 )
Robert Grieblaead70b2002-07-26 15:54:20 +0000404 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000405 }
Robert Griebl70112da2002-07-29 20:28:38 +0000406 if ( adt ) {
407 dt = adt;
408 mod = dt-> m_module;
409 if ( !opt )
410 opt = dt-> m_options;
411 }
Robert Grieblaead70b2002-07-26 15:54:20 +0000412 else
Robert Griebl70112da2002-07-29 20:28:38 +0000413 return;
Robert Grieblaead70b2002-07-26 15:54:20 +0000414 }
415 else
416 return;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000417 }
418
Robert Griebl52e8d062002-05-14 23:42:08 +0000419 // search for duplicates
420 for ( find = *head; find; find = find-> m_next ) {
421 if ( !strcmp ( mod, find-> m_module )) {
422 // found -> dequeue it
423
424 if ( find-> m_prev )
425 find-> m_prev-> m_next = find-> m_next;
426 else
427 *head = find-> m_next;
428
429 if ( find-> m_next )
430 find-> m_next-> m_prev = find-> m_prev;
431 else
432 *tail = find-> m_prev;
433
Robert Griebl52e8d062002-05-14 23:42:08 +0000434 break; // there can be only one duplicate
435 }
436 }
437
438 if ( !find ) { // did not find a duplicate
439 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
440 find-> m_module = mod;
Robert Grieblaead70b2002-07-26 15:54:20 +0000441 find-> m_options = opt;
Robert Griebl52e8d062002-05-14 23:42:08 +0000442 }
443
444 // enqueue at tail
445 if ( *tail )
446 (*tail)-> m_next = find;
447 find-> m_prev = *tail;
448 find-> m_next = 0;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000449
Robert Griebl52e8d062002-05-14 23:42:08 +0000450 if ( !*head )
451 *head = find;
452 *tail = find;
Eric Andersen60e56f52002-04-26 06:04:01 +0000453
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000454 if ( dt ) {
455 int i;
456
457 for ( i = 0; i < dt-> m_depcnt; i++ )
458 check_dep ( dt-> m_deparr [i], head, tail );
459 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000460}
461
462
463
Robert Griebl236abbf2002-05-22 23:34:35 +0000464static int mod_insert ( char *mod, int argc, char **argv )
Robert Griebl52e8d062002-05-14 23:42:08 +0000465{
466 struct mod_list_t *tail = 0;
467 struct mod_list_t *head = 0;
Eric Andersen908e3622003-06-20 09:56:37 +0000468 int rc;
Robert Griebl52e8d062002-05-14 23:42:08 +0000469
470 // get dep list for module mod
471 check_dep ( mod, &head, &tail );
472
473 if ( head && tail ) {
Robert Grieblaead70b2002-07-26 15:54:20 +0000474 if ( argc ) {
475 int i;
476 int l = 0;
Robert Griebl52e8d062002-05-14 23:42:08 +0000477
Robert Grieblaead70b2002-07-26 15:54:20 +0000478 // append module args
479 for ( i = 0; i < argc; i++ )
Manuel Novoa III cad53642003-03-19 09:13:01 +0000480 l += ( bb_strlen ( argv [i] ) + 1 );
Robert Griebl236abbf2002-05-22 23:34:35 +0000481
Robert Grieblaead70b2002-07-26 15:54:20 +0000482 head-> m_options = xrealloc ( head-> m_options, l + 1 );
483 head-> m_options [0] = 0;
Robert Griebl52e8d062002-05-14 23:42:08 +0000484
Robert Grieblaead70b2002-07-26 15:54:20 +0000485 for ( i = 0; i < argc; i++ ) {
486 strcat ( head-> m_options, argv [i] );
487 strcat ( head-> m_options, " " );
488 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000489 }
Robert Grieblaead70b2002-07-26 15:54:20 +0000490
Robert Griebl52e8d062002-05-14 23:42:08 +0000491 // process tail ---> head
Eric Andersen908e3622003-06-20 09:56:37 +0000492 rc = mod_process ( tail, 1 );
Robert Griebl52e8d062002-05-14 23:42:08 +0000493 }
494 else
495 rc = 1;
496
497 return rc;
498}
499
Eric Andersen908e3622003-06-20 09:56:37 +0000500static int mod_remove ( char *mod )
Robert Griebl52e8d062002-05-14 23:42:08 +0000501{
Eric Andersen908e3622003-06-20 09:56:37 +0000502 int rc;
Robert Griebl52e8d062002-05-14 23:42:08 +0000503 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
504
505 struct mod_list_t *head = 0;
506 struct mod_list_t *tail = 0;
507
508 if ( mod )
509 check_dep ( mod, &head, &tail );
510 else // autoclean
511 head = tail = &rm_a_dummy;
512
Robert Griebl236abbf2002-05-22 23:34:35 +0000513 if ( head && tail )
Eric Andersen908e3622003-06-20 09:56:37 +0000514 rc = mod_process ( head, 0 ); // process head ---> tail
515 else
516 rc = 1;
517 return rc;
518
Robert Griebl52e8d062002-05-14 23:42:08 +0000519}
520
521
Eric Andersen60e56f52002-04-26 06:04:01 +0000522
Eric Andersen0139ca92001-07-22 23:01:03 +0000523extern int modprobe_main(int argc, char** argv)
524{
Robert Griebl236abbf2002-05-22 23:34:35 +0000525 int opt;
526 int remove_opt = 0;
Eric Andersen0139ca92001-07-22 23:01:03 +0000527
Robert Griebl236abbf2002-05-22 23:34:35 +0000528 autoclean = show_only = quiet = do_syslog = verbose = 0;
529
530 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
531 switch(opt) {
532 case 'c': // no config used
533 case 'l': // no pattern matching
534 return EXIT_SUCCESS;
Eric Andersen0139ca92001-07-22 23:01:03 +0000535 break;
Robert Griebl236abbf2002-05-22 23:34:35 +0000536 case 'C': // no config used
537 case 't': // no pattern matching
Manuel Novoa III cad53642003-03-19 09:13:01 +0000538 bb_error_msg_and_die("-t and -C not supported");
Robert Griebl236abbf2002-05-22 23:34:35 +0000539
540 case 'a': // ignore
541 case 'd': // ignore
Eric Andersen0139ca92001-07-22 23:01:03 +0000542 break;
543 case 'k':
544 autoclean++;
545 break;
Eric Andersen0139ca92001-07-22 23:01:03 +0000546 case 'n':
547 show_only++;
548 break;
549 case 'q':
550 quiet++;
551 break;
552 case 'r':
Eric Andersen1b064192001-07-25 07:23:38 +0000553 remove_opt++;
Eric Andersen0139ca92001-07-22 23:01:03 +0000554 break;
555 case 's':
556 do_syslog++;
557 break;
Eric Andersen0139ca92001-07-22 23:01:03 +0000558 case 'v':
559 verbose++;
560 break;
Eric Andersen0139ca92001-07-22 23:01:03 +0000561 case 'V':
562 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000563 bb_show_usage();
Eric Andersen0139ca92001-07-22 23:01:03 +0000564 break;
565 }
Eric Andersen0139ca92001-07-22 23:01:03 +0000566 }
Eric Andersen0139ca92001-07-22 23:01:03 +0000567
Robert Griebl52e8d062002-05-14 23:42:08 +0000568 depend = build_dep ( );
569
Robert Griebl236abbf2002-05-22 23:34:35 +0000570 if ( !depend )
Manuel Novoa III cad53642003-03-19 09:13:01 +0000571 bb_error_msg_and_die ( "could not parse modules.dep\n" );
Robert Griebl52e8d062002-05-14 23:42:08 +0000572
Eric Andersen1b064192001-07-25 07:23:38 +0000573 if (remove_opt) {
Eric Andersen908e3622003-06-20 09:56:37 +0000574 int rc = EXIT_SUCCESS;
Eric Andersen0139ca92001-07-22 23:01:03 +0000575 do {
Eric Andersen908e3622003-06-20 09:56:37 +0000576 if (mod_remove ( optind < argc ?
577 bb_xstrdup (argv [optind]) : NULL )) {
578 bb_error_msg ("failed to remove module %s",
579 argv [optind] );
580 rc = EXIT_FAILURE;
581 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000582 } while ( ++optind < argc );
583
Eric Andersen908e3622003-06-20 09:56:37 +0000584 return rc;
Eric Andersen0139ca92001-07-22 23:01:03 +0000585 }
586
Robert Griebl236abbf2002-05-22 23:34:35 +0000587 if (optind >= argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000588 bb_error_msg_and_die ( "No module or pattern provided\n" );
Robert Griebl52e8d062002-05-14 23:42:08 +0000589
Manuel Novoa III cad53642003-03-19 09:13:01 +0000590 if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ))
591 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );
Robert Griebl3ba7cee2002-12-03 22:41:36 +0000592
593 return EXIT_SUCCESS;
Eric Andersen0139ca92001-07-22 23:01:03 +0000594}
595
596