07cbb6fc78996b28125ebf6013fdf3c16abaae7b
[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
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         
77         return bb_strlen( tag ) && bb_strlen( value );
78 }
79
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
109 static struct dep_t *build_dep ( void )
110 {
111         int fd;
112         struct utsname un;
113         struct dep_t *first = 0;
114         struct dep_t *current = 0;
115         char buffer[256];
116         char *filename = buffer;
117         int continuation_line = 0;
118         
119         if ( uname ( &un ))
120                 return 0;
121                 
122         // check for buffer overflow in following code
123         if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
124                 return 0;
125         }
126                                 
127         strcpy ( filename, "/lib/modules/" );
128         strcat ( filename, un.release );
129         strcat ( filename, "/modules.dep" );
130
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         }
138
139         while ( reads ( fd, buffer, sizeof( buffer ))) {
140                 int l = bb_strlen ( buffer );
141                 char *p = 0;
142                 
143                 while ( isspace ( buffer [l-1] )) {
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;
159                                 int ext = 0;
160                                 
161                                 *col = 0;
162                                 mods = strrchr ( buffer, '/' );
163                                 
164                                 if ( !mods )
165                                         mods = buffer;
166                                 else
167                                         mods++;
168                                         
169                                 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
170                                         ext = 2;
171                                 
172                                 mod = bb_xstrndup ( mods, col - mods - ext );
173                                         
174                                 if ( !current ) {
175                                         first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
176                                 }
177                                 else {
178                                         current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
179                                         current = current-> m_next;
180                                 }
181                                 current-> m_module  = mod;
182                                 current-> m_options = 0;
183                                 current-> m_isalias = 0;
184                                 current-> m_depcnt  = 0;
185                                 current-> m_deparr  = 0;
186                                 current-> m_next    = 0;
187                                                 
188                                 //printf ( "%s:\n", mod );
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;
202                         int ext = 0;
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                         
218                         if (( *(end-1) == '.' ) && ( *end == 'o' ))
219                                 ext = 2;
220
221                         /* Cope with blank lines */
222                         if ((end-deps-ext+1) <= 0)
223                                 continue;
224                         
225                         dep = bb_xstrndup ( deps, end - deps - ext + 1 );
226                         
227                         current-> m_depcnt++;
228                         current-> m_deparr = (char **) xrealloc ( current-> m_deparr, sizeof ( char *) * current-> m_depcnt );
229                         current-> m_deparr [current-> m_depcnt - 1] = dep;              
230                         
231                         //printf ( "    %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
232                 }
233         
234                 if ( buffer [l-1] == '\\' )
235                         continuation_line = 1;
236                 else
237                         continuation_line = 0;
238         }
239         close ( fd );
240
241         // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
242
243         if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
244                 if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
245                         return first;
246         
247         continuation_line = 0;
248         while ( reads ( fd, buffer, sizeof( buffer ))) {
249                 int l;
250                 char *p;
251                 
252                 p = strchr ( buffer, '#' );     
253                 if ( p )
254                         *p = 0;
255                         
256                 l = bb_strlen ( buffer );
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
272                                 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
273                                         // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod );
274                                 
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                                         }
282                                         current-> m_module  = bb_xstrdup ( alias );
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 * ));
292                                                 current-> m_deparr[0] = bb_xstrdup ( mod );
293                                         }
294                                         current-> m_next    = 0;                                        
295                                 }
296                         }                               
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 ) {
308                                                 dt-> m_options = xrealloc ( dt-> m_options, bb_strlen( opt ) + 1 );
309                                                 strcpy ( dt-> m_options, opt );
310                                                 
311                                                 // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_module, dt-> m_options );
312                                         }
313                                 }
314                         }
315                 }
316         }
317         close ( fd );
318         
319         return first;
320 }
321
322 /* return 1 = loaded, 0 = not loaded, -1 = can't tell */
323 static 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 }
348
349 static int mod_process ( struct mod_list_t *list, int do_insert )
350 {
351         char lcmd [256];
352         int rc = 1;
353
354         while ( list ) {
355                 if ( do_insert ) {
356                         if (already_loaded (list->m_module) != 1)
357                                 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 : "" );
358                 } else {
359                         if (already_loaded (list->m_module) != 0)
360                                 snprintf ( lcmd, sizeof( lcmd ) - 1, "rmmod %s %s", do_syslog ? "-s" : "", list-> m_module );
361                 }
362                 
363                 if ( verbose )
364                         printf ( "%s\n", lcmd );
365                 if ( !show_only ) {
366                         int rc2 = system ( lcmd );
367                         if (do_insert) rc = rc2; /* only last module matters */
368                         else if (!rc2) rc = 0; /* success if remove any mod */
369                 }
370                         
371                 list = do_insert ? list-> m_prev : list-> m_next;
372         }
373         return (show_only) ? 0 : rc;
374 }
375
376 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
377 {
378         struct mod_list_t *find;
379         struct dep_t *dt;
380         char *opt = 0;
381         int lm;
382
383         // remove .o extension
384         lm = bb_strlen ( mod );
385         if (( mod [lm-2] == '.' ) && ( mod [lm-1] == 'o' ))
386                 mod [lm-2] = 0;
387
388         // check dependencies
389         for ( dt = depend; dt; dt = dt-> m_next ) {
390                 if ( strcmp ( dt-> m_module, mod ) == 0 ) {
391                         opt = dt-> m_options;
392                         break;
393                 }
394         }
395         
396         // resolve alias names
397         while ( dt && dt-> m_isalias ) {
398                 if ( dt-> m_depcnt == 1 ) {
399                         struct dep_t *adt;
400                 
401                         for ( adt = depend; adt; adt = adt-> m_next ) {
402                                 if ( strcmp ( adt-> m_module, dt-> m_deparr [0] ) == 0 )
403                                         break;
404                         }
405                         if ( adt ) {
406                                 dt = adt;                       
407                                 mod = dt-> m_module;
408                                 if ( !opt )
409                                         opt = dt-> m_options;
410                         }
411                         else
412                                 return;
413                 }
414                 else
415                         return;                 
416         }
417         
418         // search for duplicates
419         for ( find = *head; find; find = find-> m_next ) {
420                 if ( !strcmp ( mod, find-> m_module )) {
421                         // found -> dequeue it
422
423                         if ( find-> m_prev )
424                                 find-> m_prev-> m_next = find-> m_next;
425                         else
426                                 *head = find-> m_next;
427                                         
428                         if ( find-> m_next )
429                                 find-> m_next-> m_prev = find-> m_prev;
430                         else
431                                 *tail = find-> m_prev;
432                                         
433                         break; // there can be only one duplicate
434                 }                               
435         }
436
437         if ( !find ) { // did not find a duplicate
438                 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));              
439                 find-> m_module = mod;
440                 find-> m_options = opt;
441         }
442
443         // enqueue at tail      
444         if ( *tail )
445                 (*tail)-> m_next = find;
446         find-> m_prev   = *tail;
447         find-> m_next   = 0;
448
449         if ( !*head )
450                 *head = find;
451         *tail = find;
452                 
453         if ( dt ) {     
454                 int i;
455                 
456                 for ( i = 0; i < dt-> m_depcnt; i++ )
457                         check_dep ( dt-> m_deparr [i], head, tail );
458         }
459 }
460
461
462
463 static int mod_insert ( char *mod, int argc, char **argv )
464 {
465         struct mod_list_t *tail = 0;
466         struct mod_list_t *head = 0;    
467         int rc;
468         
469         // get dep list for module mod
470         check_dep ( mod, &head, &tail );
471         
472         if ( head && tail ) {
473                 if ( argc ) {
474                         int i;          
475                         int l = 0;
476         
477                         // append module args
478                         for ( i = 0; i < argc; i++ ) 
479                                 l += ( bb_strlen ( argv [i] ) + 1 );
480                 
481                         head-> m_options = xrealloc ( head-> m_options, l + 1 );
482                         head-> m_options [0] = 0;
483                 
484                         for ( i = 0; i < argc; i++ ) {
485                                 strcat ( head-> m_options, argv [i] );
486                                 strcat ( head-> m_options, " " );
487                         }
488                 }
489                 
490                 // process tail ---> head
491                 rc = mod_process ( tail, 1 );
492         }
493         else
494                 rc = 1;
495         
496         return rc;
497 }
498
499 static int mod_remove ( char *mod )
500 {
501         int rc;
502         static struct mod_list_t rm_a_dummy = { "-a", 0, 0 }; 
503         
504         struct mod_list_t *head = 0;
505         struct mod_list_t *tail = 0;
506         
507         if ( mod )
508                 check_dep ( mod, &head, &tail );
509         else  // autoclean
510                 head = tail = &rm_a_dummy;
511         
512         if ( head && tail )
513                 rc = mod_process ( head, 0 );  // process head ---> tail
514         else
515                 rc = 1;
516         return rc;
517         
518 }
519
520
521
522 extern int modprobe_main(int argc, char** argv)
523 {
524         int     opt;
525         int remove_opt = 0;
526
527         autoclean = show_only = quiet = do_syslog = verbose = 0;
528
529         while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
530                 switch(opt) {
531                 case 'c': // no config used
532                 case 'l': // no pattern matching
533                         return EXIT_SUCCESS;
534                         break;
535                 case 'C': // no config used
536                 case 't': // no pattern matching
537                         bb_error_msg_and_die("-t and -C not supported");
538
539                 case 'a': // ignore
540                 case 'd': // ignore
541                         break;
542                 case 'k':
543                         autoclean++;
544                         break;
545                 case 'n':
546                         show_only++;
547                         break;
548                 case 'q':
549                         quiet++;
550                         break;
551                 case 'r':
552                         remove_opt++;
553                         break;
554                 case 's':
555                         do_syslog++;
556                         break;
557                 case 'v':
558                         verbose++;
559                         break;
560                 case 'V':
561                 default:
562                         bb_show_usage();
563                         break;
564                 }
565         }
566         
567         depend = build_dep ( ); 
568
569         if ( !depend ) 
570                 bb_error_msg_and_die ( "could not parse modules.dep\n" );
571         
572         if (remove_opt) {
573                 int rc = EXIT_SUCCESS;
574                 do {
575                         if (mod_remove ( optind < argc ?
576                                          bb_xstrdup (argv [optind]) : NULL )) {
577                                 bb_error_msg ("failed to remove module %s",
578                                         argv [optind] );
579                                 rc = EXIT_FAILURE;
580                         }
581                 } while ( ++optind < argc );
582                 
583                 return rc;
584         }
585
586         if (optind >= argc) 
587                 bb_error_msg_and_die ( "No module or pattern provided\n" );
588         
589         if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 )) 
590                 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );
591         
592         return EXIT_SUCCESS;
593 }
594
595