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