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