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