blob: b8268b3abd069d82ef044fe4a7c5084b687dc4cc [file] [log] [blame]
Eric Andersen0139ca92001-07-22 23:01:03 +00001/* vi: set sw=4 ts=4: */
2/*
Robert Grieblaead70b2002-07-26 15:54:20 +00003 * Modprobe written from scratch for BusyBox
Eric Andersen60e56f52002-04-26 06:04:01 +00004 *
Robert Griebl6859d762002-08-05 02:57:12 +00005 * Copyright (c) 2002 by Robert Griebl, griebl@gmx.de
Eric Andersen908e3622003-06-20 09:56:37 +00006 * Copyright (c) 2003 by Andrew Dennison, andrew.dennison@motec.com.au
Robert Griebl6859d762002-08-05 02:57:12 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22*/
Eric Andersen0139ca92001-07-22 23:01:03 +000023
Robert Griebl52e8d062002-05-14 23:42:08 +000024#include <sys/utsname.h>
Eric Andersen0139ca92001-07-22 23:01:03 +000025#include <getopt.h>
26#include <stdlib.h>
27#include <unistd.h>
28#include <syslog.h>
29#include <string.h>
Eric Andersen60e56f52002-04-26 06:04:01 +000030#include <ctype.h>
Eric Andersenc06391b2002-06-04 13:28:43 +000031#include <fcntl.h>
Eric Andersen0139ca92001-07-22 23:01:03 +000032#include "busybox.h"
33
Eric Andersen0139ca92001-07-22 23:01:03 +000034
Eric Andersen60e56f52002-04-26 06:04:01 +000035
36struct dep_t {
Eric Andersen807bd842004-08-19 18:30:31 +000037 char * m_name;
38 char * m_path;
Robert Grieblaead70b2002-07-26 15:54:20 +000039 char * m_options;
Eric Andersen3b1a7442003-12-24 20:30:45 +000040
Robert Griebl1d4ef2a2002-05-28 21:32:10 +000041 int m_isalias : 1;
42 int m_reserved : 15;
Eric Andersen3b1a7442003-12-24 20:30:45 +000043
Eric Andersen716ccb22004-01-10 11:29:31 +000044 int m_depcnt : 16;
Eric Andersen60e56f52002-04-26 06:04:01 +000045 char ** m_deparr;
Eric Andersen3b1a7442003-12-24 20:30:45 +000046
Eric Andersen60e56f52002-04-26 06:04:01 +000047 struct dep_t * m_next;
48};
49
Robert Griebl52e8d062002-05-14 23:42:08 +000050struct mod_list_t {
Eric Andersen807bd842004-08-19 18:30:31 +000051 char * m_name;
52 char * m_path;
Robert Grieblaead70b2002-07-26 15:54:20 +000053 char * m_options;
Eric Andersen3b1a7442003-12-24 20:30:45 +000054
Robert Griebl52e8d062002-05-14 23:42:08 +000055 struct mod_list_t * m_prev;
56 struct mod_list_t * m_next;
57};
58
59
Robert Griebl236abbf2002-05-22 23:34:35 +000060static struct dep_t *depend;
61static int autoclean, show_only, quiet, do_syslog, verbose;
Eric Andersen03d80912003-12-19 21:04:19 +000062static int k_version;
Robert Griebl52e8d062002-05-14 23:42:08 +000063
Eric Andersen14f5c8d2005-04-16 19:39:00 +000064static int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
Robert Grieblaead70b2002-07-26 15:54:20 +000065{
66 char *tag, *value;
67
68 while ( isspace ( *buffer ))
Eric Andersen716ccb22004-01-10 11:29:31 +000069 buffer++;
Robert Grieblaead70b2002-07-26 15:54:20 +000070 tag = value = buffer;
71 while ( !isspace ( *value ))
Eric Andersen7e496a72004-04-06 12:06:03 +000072 if (!*value) return 0;
73 else value++;
Robert Grieblaead70b2002-07-26 15:54:20 +000074 *value++ = 0;
75 while ( isspace ( *value ))
76 value++;
Eric Andersen7e496a72004-04-06 12:06:03 +000077 if (!*value) return 0;
Robert Grieblaead70b2002-07-26 15:54:20 +000078
79 *ptag = tag;
80 *pvalue = value;
Eric Andersen3b1a7442003-12-24 20:30:45 +000081
Eric Andersen7e496a72004-04-06 12:06:03 +000082 return 1;
Robert Grieblaead70b2002-07-26 15:54:20 +000083}
Eric Andersen60e56f52002-04-26 06:04:01 +000084
Robert Grieblbc28f7a2002-06-04 19:33:58 +000085/* Jump through hoops to simulate how fgets() grabs just one line at a
86 * time... Don't use any stdio since modprobe gets called from a kernel
Eric Andersen716ccb22004-01-10 11:29:31 +000087 * thread and stdio junk can overflow the limited stack...
Robert Grieblbc28f7a2002-06-04 19:33:58 +000088 */
89static char *reads ( int fd, char *buffer, size_t len )
90{
91 int n = read ( fd, buffer, len );
Eric Andersen3b1a7442003-12-24 20:30:45 +000092
Robert Grieblbc28f7a2002-06-04 19:33:58 +000093 if ( n > 0 ) {
94 char *p;
Eric Andersen3b1a7442003-12-24 20:30:45 +000095
Robert Grieblbc28f7a2002-06-04 19:33:58 +000096 buffer [len-1] = 0;
97 p = strchr ( buffer, '\n' );
Eric Andersen3b1a7442003-12-24 20:30:45 +000098
Robert Grieblbc28f7a2002-06-04 19:33:58 +000099 if ( p ) {
100 off_t offset;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000101
Eric Andersen716ccb22004-01-10 11:29:31 +0000102 offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000103 lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
104
105 p[1] = 0;
106 }
107 return buffer;
108 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000109
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000110 else
111 return 0;
112}
113
Eric Andersen864b7972002-05-03 15:48:26 +0000114static struct dep_t *build_dep ( void )
Eric Andersen60e56f52002-04-26 06:04:01 +0000115{
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000116 int fd;
Eric Andersen60e56f52002-04-26 06:04:01 +0000117 struct utsname un;
Eric Andersen60e56f52002-04-26 06:04:01 +0000118 struct dep_t *first = 0;
119 struct dep_t *current = 0;
Eric Andersenbeaef162004-08-14 20:57:33 +0000120 char buffer[2048];
Eric Andersen60e56f52002-04-26 06:04:01 +0000121 char *filename = buffer;
122 int continuation_line = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000123
Eric Andersen03d80912003-12-19 21:04:19 +0000124 k_version = 0;
Eric Andersen60e56f52002-04-26 06:04:01 +0000125 if ( uname ( &un ))
126 return 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000127
Robert Griebl236abbf2002-05-22 23:34:35 +0000128 // check for buffer overflow in following code
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
Robert Griebl236abbf2002-05-22 23:34:35 +0000130 return 0;
Eric Andersenc06391b2002-06-04 13:28:43 +0000131 }
Eric Andersen03d80912003-12-19 21:04:19 +0000132 if (un.release[0] == '2') {
133 k_version = un.release[2] - '0';
134 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000135
Eric Andersen60e56f52002-04-26 06:04:01 +0000136 strcpy ( filename, "/lib/modules/" );
Eric Andersenc06391b2002-06-04 13:28:43 +0000137 strcat ( filename, un.release );
Eric Andersen60e56f52002-04-26 06:04:01 +0000138 strcat ( filename, "/modules.dep" );
139
Eric Andersen25ea42d2003-06-20 09:57:30 +0000140 if (( fd = open ( filename, O_RDONLY )) < 0 ) {
141
142 /* Ok, that didn't work. Fall back to looking in /lib/modules */
143 if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
144 return 0;
145 }
146 }
Eric Andersenc06391b2002-06-04 13:28:43 +0000147
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000148 while ( reads ( fd, buffer, sizeof( buffer ))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000149 int l = bb_strlen ( buffer );
Eric Andersen60e56f52002-04-26 06:04:01 +0000150 char *p = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000151
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000152 while ( isspace ( buffer [l-1] )) {
Eric Andersen60e56f52002-04-26 06:04:01 +0000153 buffer [l-1] = 0;
154 l--;
155 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000156
Eric Andersen60e56f52002-04-26 06:04:01 +0000157 if ( l == 0 ) {
158 continuation_line = 0;
159 continue;
160 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000161
Eric Andersen716ccb22004-01-10 11:29:31 +0000162 if ( !continuation_line ) {
Eric Andersen60e56f52002-04-26 06:04:01 +0000163 char *col = strchr ( buffer, ':' );
Eric Andersen807bd842004-08-19 18:30:31 +0000164 char *dot = col;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000165
Eric Andersen60e56f52002-04-26 06:04:01 +0000166 if ( col ) {
167 char *mods;
Eric Andersen807bd842004-08-19 18:30:31 +0000168 char *modpath;
Eric Andersen60e56f52002-04-26 06:04:01 +0000169 char *mod;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000170
Eric Andersen60e56f52002-04-26 06:04:01 +0000171 *col = 0;
172 mods = strrchr ( buffer, '/' );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000173
Eric Andersen60e56f52002-04-26 06:04:01 +0000174 if ( !mods )
175 mods = buffer;
176 else
177 mods++;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000178
Eric Andersen807bd842004-08-19 18:30:31 +0000179 modpath = strchr ( buffer, '/' );
180 if ( !modpath )
181 modpath = buffer;
Eric Andersen03d80912003-12-19 21:04:19 +0000182#if defined(CONFIG_FEATURE_2_6_MODULES)
183 if ((k_version > 4) && ( *(col-3) == '.' ) &&
Eric Andersen3b1a7442003-12-24 20:30:45 +0000184 ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ))
Eric Andersen807bd842004-08-19 18:30:31 +0000185 dot = col - 3;
Eric Andersen03d80912003-12-19 21:04:19 +0000186 else
187#endif
Eric Andersen3b1a7442003-12-24 20:30:45 +0000188 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
Eric Andersen807bd842004-08-19 18:30:31 +0000189 dot = col - 2;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000190
Eric Andersen807bd842004-08-19 18:30:31 +0000191 mod = bb_xstrndup ( mods, dot - mods );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000192
Eric Andersen60e56f52002-04-26 06:04:01 +0000193 if ( !current ) {
Eric Andersenc06391b2002-06-04 13:28:43 +0000194 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
Eric Andersen60e56f52002-04-26 06:04:01 +0000195 }
196 else {
Eric Andersenc06391b2002-06-04 13:28:43 +0000197 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
Eric Andersen60e56f52002-04-26 06:04:01 +0000198 current = current-> m_next;
199 }
Eric Andersen807bd842004-08-19 18:30:31 +0000200 current-> m_name = mod;
201 current-> m_path = bb_xstrdup(modpath);
Robert Grieblaead70b2002-07-26 15:54:20 +0000202 current-> m_options = 0;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000203 current-> m_isalias = 0;
204 current-> m_depcnt = 0;
205 current-> m_deparr = 0;
206 current-> m_next = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000207
Eric Andersen864b7972002-05-03 15:48:26 +0000208 //printf ( "%s:\n", mod );
Eric Andersen716ccb22004-01-10 11:29:31 +0000209 p = col + 1;
Eric Andersen60e56f52002-04-26 06:04:01 +0000210 }
211 else
212 p = 0;
213 }
214 else
215 p = buffer;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000216
Eric Andersen9d65ab22004-01-06 00:07:17 +0000217 while ( p && *p && isblank(*p))
218 p++;
219
Eric Andersen60e56f52002-04-26 06:04:01 +0000220 if ( p && *p ) {
221 char *end = &buffer [l-1];
Eric Andersen9d65ab22004-01-06 00:07:17 +0000222 char *deps;
Eric Andersen60e56f52002-04-26 06:04:01 +0000223 char *dep;
Eric Andersen9d65ab22004-01-06 00:07:17 +0000224 char *next;
Eric Andersen864b7972002-05-03 15:48:26 +0000225 int ext = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000226
Eric Andersen60e56f52002-04-26 06:04:01 +0000227 while ( isblank ( *end ) || ( *end == '\\' ))
228 end--;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000229
Eric Andersen9d65ab22004-01-06 00:07:17 +0000230 do
231 {
232 next = strchr (p, ' ' );
233 if (next)
234 {
235 *next = 0;
236 next--;
237 }
238 else
239 next = end;
240
Eric Andersen716ccb22004-01-10 11:29:31 +0000241 deps = strrchr ( p, '/' );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000242
Eric Andersen716ccb22004-01-10 11:29:31 +0000243 if ( !deps || ( deps < p )) {
244 deps = p;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000245
Eric Andersen716ccb22004-01-10 11:29:31 +0000246 while ( isblank ( *deps ))
247 deps++;
248 }
249 else
Eric Andersen60e56f52002-04-26 06:04:01 +0000250 deps++;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000251
Eric Andersen03d80912003-12-19 21:04:19 +0000252#if defined(CONFIG_FEATURE_2_6_MODULES)
Eric Andersen9d65ab22004-01-06 00:07:17 +0000253 if ((k_version > 4) && ( *(next-2) == '.' ) && *(next-1) == 'k' &&
254 ( *next == 'o' ))
Eric Andersen716ccb22004-01-10 11:29:31 +0000255 ext = 3;
256 else
Eric Andersen03d80912003-12-19 21:04:19 +0000257#endif
Eric Andersen9d65ab22004-01-06 00:07:17 +0000258 if (( *(next-1) == '.' ) && ( *next == 'o' ))
Eric Andersen716ccb22004-01-10 11:29:31 +0000259 ext = 2;
Eric Andersenb493dec2002-07-02 19:14:23 +0000260
Eric Andersen716ccb22004-01-10 11:29:31 +0000261 /* Cope with blank lines */
Eric Andersen9d65ab22004-01-06 00:07:17 +0000262 if ((next-deps-ext+1) <= 0)
Eric Andersen716ccb22004-01-10 11:29:31 +0000263 continue;
Eric Andersen9d65ab22004-01-06 00:07:17 +0000264 dep = bb_xstrndup ( deps, next - deps - ext + 1 );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000265
Eric Andersen716ccb22004-01-10 11:29:31 +0000266 current-> m_depcnt++;
267 current-> m_deparr = (char **) xrealloc ( current-> m_deparr,
268 sizeof ( char *) * current-> m_depcnt );
269 current-> m_deparr [current-> m_depcnt - 1] = dep;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000270
Eric Andersen716ccb22004-01-10 11:29:31 +0000271 //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
272 p = next + 2;
Eric Andersen9d65ab22004-01-06 00:07:17 +0000273 } while (next < end);
Eric Andersen60e56f52002-04-26 06:04:01 +0000274 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000275
Eric Andersen60e56f52002-04-26 06:04:01 +0000276 if ( buffer [l-1] == '\\' )
277 continuation_line = 1;
278 else
279 continuation_line = 0;
280 }
Eric Andersenc06391b2002-06-04 13:28:43 +0000281 close ( fd );
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000282
283 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
284
Robert Griebl36a836d2004-07-22 00:03:39 +0000285#if defined(CONFIG_FEATURE_2_6_MODULES)
286 if (( fd = open ( "/etc/modprobe.conf", O_RDONLY )) < 0 )
287#endif
288 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
289 if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
290 return first;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000291
Eric Andersenc06391b2002-06-04 13:28:43 +0000292 continuation_line = 0;
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000293 while ( reads ( fd, buffer, sizeof( buffer ))) {
Eric Andersenc06391b2002-06-04 13:28:43 +0000294 int l;
295 char *p;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000296
Eric Andersen716ccb22004-01-10 11:29:31 +0000297 p = strchr ( buffer, '#' );
Eric Andersenc06391b2002-06-04 13:28:43 +0000298 if ( p )
299 *p = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000300
Manuel Novoa III cad53642003-03-19 09:13:01 +0000301 l = bb_strlen ( buffer );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000302
Eric Andersenc06391b2002-06-04 13:28:43 +0000303 while ( l && isspace ( buffer [l-1] )) {
304 buffer [l-1] = 0;
305 l--;
306 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000307
Eric Andersenc06391b2002-06-04 13:28:43 +0000308 if ( l == 0 ) {
309 continuation_line = 0;
310 continue;
311 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000312
Eric Andersen716ccb22004-01-10 11:29:31 +0000313 if ( !continuation_line ) {
Eric Andersenc06391b2002-06-04 13:28:43 +0000314 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
315 char *alias, *mod;
316
Robert Grieblaead70b2002-07-26 15:54:20 +0000317 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
318 // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000319
Robert Grieblaead70b2002-07-26 15:54:20 +0000320 if ( !current ) {
Eric Andersenba05e792004-04-06 12:04:14 +0000321 first = current = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
Robert Grieblaead70b2002-07-26 15:54:20 +0000322 }
323 else {
Eric Andersenba05e792004-04-06 12:04:14 +0000324 current-> m_next = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
Robert Grieblaead70b2002-07-26 15:54:20 +0000325 current = current-> m_next;
326 }
Eric Andersen807bd842004-08-19 18:30:31 +0000327 current-> m_name = bb_xstrdup ( alias );
Robert Grieblaead70b2002-07-26 15:54:20 +0000328 current-> m_isalias = 1;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000329
Eric Andersend298bd12004-04-06 12:05:04 +0000330 if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) {
Robert Grieblaead70b2002-07-26 15:54:20 +0000331 current-> m_depcnt = 0;
332 current-> m_deparr = 0;
333 }
334 else {
335 current-> m_depcnt = 1;
336 current-> m_deparr = xmalloc ( 1 * sizeof( char * ));
Manuel Novoa III cad53642003-03-19 09:13:01 +0000337 current-> m_deparr[0] = bb_xstrdup ( mod );
Robert Grieblaead70b2002-07-26 15:54:20 +0000338 }
Eric Andersen716ccb22004-01-10 11:29:31 +0000339 current-> m_next = 0;
Eric Andersenc06391b2002-06-04 13:28:43 +0000340 }
Eric Andersen716ccb22004-01-10 11:29:31 +0000341 }
342 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
Robert Grieblaead70b2002-07-26 15:54:20 +0000343 char *mod, *opt;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000344
Robert Grieblaead70b2002-07-26 15:54:20 +0000345 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
346 struct dep_t *dt;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000347
Robert Grieblaead70b2002-07-26 15:54:20 +0000348 for ( dt = first; dt; dt = dt-> m_next ) {
Eric Andersen807bd842004-08-19 18:30:31 +0000349 if ( strcmp ( dt-> m_name, mod ) == 0 )
Robert Grieblaead70b2002-07-26 15:54:20 +0000350 break;
351 }
352 if ( dt ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000353 dt-> m_options = xrealloc ( dt-> m_options, bb_strlen( opt ) + 1 );
Robert Grieblaead70b2002-07-26 15:54:20 +0000354 strcpy ( dt-> m_options, opt );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000355
Eric Andersen807bd842004-08-19 18:30:31 +0000356 // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_name, dt-> m_options );
Robert Grieblaead70b2002-07-26 15:54:20 +0000357 }
358 }
359 }
Eric Andersenc06391b2002-06-04 13:28:43 +0000360 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000361 }
Eric Andersenc06391b2002-06-04 13:28:43 +0000362 close ( fd );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000363
Eric Andersen60e56f52002-04-26 06:04:01 +0000364 return first;
365}
366
Glenn L McGrath350733a2003-09-08 00:32:49 +0000367/* return 1 = loaded, 0 = not loaded, -1 = can't tell */
368static int already_loaded (const char *name)
369{
370 int fd;
Eric Andersene3a792c2004-08-16 10:22:34 +0000371 char buffer[4096];
Glenn L McGrath350733a2003-09-08 00:32:49 +0000372
373 fd = open ("/proc/modules", O_RDONLY);
374 if (fd < 0)
375 return -1;
376
377 while ( reads ( fd, buffer, sizeof( buffer ))) {
378 char *p;
379
380 p = strchr (buffer, ' ');
381 if (p) {
382 *p = 0;
Eric Andersen807bd842004-08-19 18:30:31 +0000383 if (strcmp (name, buffer) == 0) {
Glenn L McGrath350733a2003-09-08 00:32:49 +0000384 close (fd);
385 return 1;
386 }
387 }
388 }
389
390 close (fd);
391 return 0;
392}
Eric Andersen60e56f52002-04-26 06:04:01 +0000393
Robert Griebl236abbf2002-05-22 23:34:35 +0000394static int mod_process ( struct mod_list_t *list, int do_insert )
Eric Andersen60e56f52002-04-26 06:04:01 +0000395{
Eric Andersene3a792c2004-08-16 10:22:34 +0000396 char lcmd [4096];
Eric Andersen44b57582004-08-03 08:23:33 +0000397 int rc = 0;
Eric Andersen864b7972002-05-03 15:48:26 +0000398
Robert Griebl52e8d062002-05-14 23:42:08 +0000399 while ( list ) {
Eric Andersen61b038a2003-11-14 02:49:19 +0000400 *lcmd = '\0';
Glenn L McGrath350733a2003-09-08 00:32:49 +0000401 if ( do_insert ) {
Eric Andersen807bd842004-08-19 18:30:31 +0000402 if (already_loaded (list->m_name) != 1)
Eric Andersen716ccb22004-01-10 11:29:31 +0000403 snprintf ( lcmd, sizeof( lcmd ) - 1, "insmod %s %s %s %s %s",
404 do_syslog ? "-s" : "", autoclean ? "-k" : "",
Eric Andersen807bd842004-08-19 18:30:31 +0000405 quiet ? "-q" : "", list-> m_path, list-> m_options ?
Eric Andersen716ccb22004-01-10 11:29:31 +0000406 list-> m_options : "" );
Glenn L McGrath350733a2003-09-08 00:32:49 +0000407 } else {
Eric Andersen807bd842004-08-19 18:30:31 +0000408 /* modutils uses short name for removal */
409 if (already_loaded (list->m_name) != 0)
Eric Andersen716ccb22004-01-10 11:29:31 +0000410 snprintf ( lcmd, sizeof( lcmd ) - 1, "rmmod %s %s",
Eric Andersen807bd842004-08-19 18:30:31 +0000411 do_syslog ? "-s" : "", list-> m_name );
Glenn L McGrath350733a2003-09-08 00:32:49 +0000412 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000413
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000414 if (*lcmd) {
415 if (verbose) {
416 printf("%s\n", lcmd);
417 }
418 if (!show_only) {
419 int rc2 = system(lcmd);
420 if (do_insert) {
421 rc = rc2; /* only last module matters */
422 }
423 else if (!rc2) {
424 rc = 0; /* success if remove any mod */
425 }
426 }
Eric Andersen908e3622003-06-20 09:56:37 +0000427 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000428 list = do_insert ? list-> m_prev : list-> m_next;
Eric Andersen60e56f52002-04-26 06:04:01 +0000429 }
Eric Andersen908e3622003-06-20 09:56:37 +0000430 return (show_only) ? 0 : rc;
Eric Andersen60e56f52002-04-26 06:04:01 +0000431}
432
Robert Griebl52e8d062002-05-14 23:42:08 +0000433static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
Eric Andersen60e56f52002-04-26 06:04:01 +0000434{
Robert Griebl52e8d062002-05-14 23:42:08 +0000435 struct mod_list_t *find;
Eric Andersen60e56f52002-04-26 06:04:01 +0000436 struct dep_t *dt;
Robert Grieblaead70b2002-07-26 15:54:20 +0000437 char *opt = 0;
Eric Andersen807bd842004-08-19 18:30:31 +0000438 char *path = 0;
Robert Griebl52e8d062002-05-14 23:42:08 +0000439
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000440 // check dependencies
441 for ( dt = depend; dt; dt = dt-> m_next ) {
Eric Andersen807bd842004-08-19 18:30:31 +0000442 if ( strcmp ( dt-> m_name, mod ) == 0) {
443 mod = dt-> m_name;
444 path = dt-> m_path;
Robert Grieblaead70b2002-07-26 15:54:20 +0000445 opt = dt-> m_options;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000446 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000447 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000448 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000449
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000450 // resolve alias names
Robert Grieblaead70b2002-07-26 15:54:20 +0000451 while ( dt && dt-> m_isalias ) {
452 if ( dt-> m_depcnt == 1 ) {
453 struct dep_t *adt;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000454
Robert Grieblaead70b2002-07-26 15:54:20 +0000455 for ( adt = depend; adt; adt = adt-> m_next ) {
Eric Andersen807bd842004-08-19 18:30:31 +0000456 if ( strcmp ( adt-> m_name, dt-> m_deparr [0] ) == 0 )
Robert Grieblaead70b2002-07-26 15:54:20 +0000457 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000458 }
Robert Griebl70112da2002-07-29 20:28:38 +0000459 if ( adt ) {
Eric Andersen716ccb22004-01-10 11:29:31 +0000460 dt = adt;
Eric Andersen807bd842004-08-19 18:30:31 +0000461 mod = dt-> m_name;
462 path = dt-> m_path;
Robert Griebl70112da2002-07-29 20:28:38 +0000463 if ( !opt )
464 opt = dt-> m_options;
465 }
Robert Grieblaead70b2002-07-26 15:54:20 +0000466 else
Robert Griebl70112da2002-07-29 20:28:38 +0000467 return;
Robert Grieblaead70b2002-07-26 15:54:20 +0000468 }
469 else
Eric Andersen716ccb22004-01-10 11:29:31 +0000470 return;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000471 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000472
Glenn L McGrathbdf63192004-09-24 09:18:55 +0000473 if ( !path ) {
474 bb_error_msg ("module %s not found.", mod);
475 return;
476 }
477
Robert Griebl52e8d062002-05-14 23:42:08 +0000478 // search for duplicates
479 for ( find = *head; find; find = find-> m_next ) {
Eric Andersen807bd842004-08-19 18:30:31 +0000480 if ( !strcmp ( mod, find-> m_name )) {
Robert Griebl52e8d062002-05-14 23:42:08 +0000481 // found -> dequeue it
482
483 if ( find-> m_prev )
484 find-> m_prev-> m_next = find-> m_next;
485 else
486 *head = find-> m_next;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000487
Robert Griebl52e8d062002-05-14 23:42:08 +0000488 if ( find-> m_next )
489 find-> m_next-> m_prev = find-> m_prev;
490 else
491 *tail = find-> m_prev;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000492
Robert Griebl52e8d062002-05-14 23:42:08 +0000493 break; // there can be only one duplicate
Eric Andersen716ccb22004-01-10 11:29:31 +0000494 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000495 }
496
497 if ( !find ) { // did not find a duplicate
Eric Andersen716ccb22004-01-10 11:29:31 +0000498 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
Eric Andersen807bd842004-08-19 18:30:31 +0000499 find-> m_name = mod;
500 find-> m_path = path;
Robert Grieblaead70b2002-07-26 15:54:20 +0000501 find-> m_options = opt;
Robert Griebl52e8d062002-05-14 23:42:08 +0000502 }
503
Eric Andersen716ccb22004-01-10 11:29:31 +0000504 // enqueue at tail
Robert Griebl52e8d062002-05-14 23:42:08 +0000505 if ( *tail )
506 (*tail)-> m_next = find;
507 find-> m_prev = *tail;
508 find-> m_next = 0;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000509
Robert Griebl52e8d062002-05-14 23:42:08 +0000510 if ( !*head )
511 *head = find;
512 *tail = find;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000513
Eric Andersen716ccb22004-01-10 11:29:31 +0000514 if ( dt ) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000515 int i;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000516
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000517 for ( i = 0; i < dt-> m_depcnt; i++ )
518 check_dep ( dt-> m_deparr [i], head, tail );
519 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000520}
521
522
523
Robert Griebl236abbf2002-05-22 23:34:35 +0000524static int mod_insert ( char *mod, int argc, char **argv )
Robert Griebl52e8d062002-05-14 23:42:08 +0000525{
526 struct mod_list_t *tail = 0;
Eric Andersen716ccb22004-01-10 11:29:31 +0000527 struct mod_list_t *head = 0;
Eric Andersen908e3622003-06-20 09:56:37 +0000528 int rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000529
Robert Griebl52e8d062002-05-14 23:42:08 +0000530 // get dep list for module mod
531 check_dep ( mod, &head, &tail );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000532
Robert Griebl52e8d062002-05-14 23:42:08 +0000533 if ( head && tail ) {
Eric Andersene3a792c2004-08-16 10:22:34 +0000534#if defined(CONFIG_FEATURE_2_6_MODULES)
Robert Grieblaead70b2002-07-26 15:54:20 +0000535 if ( argc ) {
Eric Andersen716ccb22004-01-10 11:29:31 +0000536 int i;
Robert Grieblaead70b2002-07-26 15:54:20 +0000537 int l = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000538
Robert Grieblaead70b2002-07-26 15:54:20 +0000539 // append module args
Eric Andersen716ccb22004-01-10 11:29:31 +0000540 for ( i = 0; i < argc; i++ )
Manuel Novoa III cad53642003-03-19 09:13:01 +0000541 l += ( bb_strlen ( argv [i] ) + 1 );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000542
Robert Grieblaead70b2002-07-26 15:54:20 +0000543 head-> m_options = xrealloc ( head-> m_options, l + 1 );
544 head-> m_options [0] = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000545
Robert Grieblaead70b2002-07-26 15:54:20 +0000546 for ( i = 0; i < argc; i++ ) {
547 strcat ( head-> m_options, argv [i] );
548 strcat ( head-> m_options, " " );
549 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000550 }
Eric Andersene3a792c2004-08-16 10:22:34 +0000551#endif
Eric Andersen3b1a7442003-12-24 20:30:45 +0000552
Robert Griebl52e8d062002-05-14 23:42:08 +0000553 // process tail ---> head
Eric Andersen908e3622003-06-20 09:56:37 +0000554 rc = mod_process ( tail, 1 );
Robert Griebl52e8d062002-05-14 23:42:08 +0000555 }
556 else
557 rc = 1;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000558
Robert Griebl52e8d062002-05-14 23:42:08 +0000559 return rc;
560}
561
Eric Andersen908e3622003-06-20 09:56:37 +0000562static int mod_remove ( char *mod )
Robert Griebl52e8d062002-05-14 23:42:08 +0000563{
Eric Andersen908e3622003-06-20 09:56:37 +0000564 int rc;
Eric Andersen716ccb22004-01-10 11:29:31 +0000565 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
Eric Andersen3b1a7442003-12-24 20:30:45 +0000566
Robert Griebl52e8d062002-05-14 23:42:08 +0000567 struct mod_list_t *head = 0;
568 struct mod_list_t *tail = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000569
Robert Griebl52e8d062002-05-14 23:42:08 +0000570 if ( mod )
571 check_dep ( mod, &head, &tail );
572 else // autoclean
573 head = tail = &rm_a_dummy;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000574
Robert Griebl236abbf2002-05-22 23:34:35 +0000575 if ( head && tail )
Eric Andersen908e3622003-06-20 09:56:37 +0000576 rc = mod_process ( head, 0 ); // process head ---> tail
577 else
578 rc = 1;
579 return rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000580
Robert Griebl52e8d062002-05-14 23:42:08 +0000581}
582
Eric Andersen0139ca92001-07-22 23:01:03 +0000583extern int modprobe_main(int argc, char** argv)
584{
Robert Griebl236abbf2002-05-22 23:34:35 +0000585 int opt;
586 int remove_opt = 0;
Eric Andersen0139ca92001-07-22 23:01:03 +0000587
Robert Griebl236abbf2002-05-22 23:34:35 +0000588 autoclean = show_only = quiet = do_syslog = verbose = 0;
589
590 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
591 switch(opt) {
Eric Andersen3b1a7442003-12-24 20:30:45 +0000592 case 'c': // no config used
593 case 'l': // no pattern matching
594 return EXIT_SUCCESS;
595 break;
596 case 'C': // no config used
597 case 't': // no pattern matching
598 bb_error_msg_and_die("-t and -C not supported");
Robert Griebl236abbf2002-05-22 23:34:35 +0000599
Eric Andersen3b1a7442003-12-24 20:30:45 +0000600 case 'a': // ignore
601 case 'd': // ignore
602 break;
603 case 'k':
604 autoclean++;
605 break;
606 case 'n':
607 show_only++;
608 break;
609 case 'q':
610 quiet++;
611 break;
612 case 'r':
613 remove_opt++;
614 break;
615 case 's':
616 do_syslog++;
617 break;
618 case 'v':
619 verbose++;
620 break;
621 case 'V':
622 default:
623 bb_show_usage();
624 break;
Eric Andersen0139ca92001-07-22 23:01:03 +0000625 }
Eric Andersen0139ca92001-07-22 23:01:03 +0000626 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000627
Eric Andersen716ccb22004-01-10 11:29:31 +0000628 depend = build_dep ( );
Robert Griebl52e8d062002-05-14 23:42:08 +0000629
Eric Andersen716ccb22004-01-10 11:29:31 +0000630 if ( !depend )
Manuel Novoa III cad53642003-03-19 09:13:01 +0000631 bb_error_msg_and_die ( "could not parse modules.dep\n" );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000632
Eric Andersen1b064192001-07-25 07:23:38 +0000633 if (remove_opt) {
Eric Andersen908e3622003-06-20 09:56:37 +0000634 int rc = EXIT_SUCCESS;
Eric Andersen0139ca92001-07-22 23:01:03 +0000635 do {
Eric Andersen908e3622003-06-20 09:56:37 +0000636 if (mod_remove ( optind < argc ?
Eric Andersen3b1a7442003-12-24 20:30:45 +0000637 bb_xstrdup (argv [optind]) : NULL )) {
Eric Andersen908e3622003-06-20 09:56:37 +0000638 bb_error_msg ("failed to remove module %s",
Eric Andersen3b1a7442003-12-24 20:30:45 +0000639 argv [optind] );
Eric Andersen908e3622003-06-20 09:56:37 +0000640 rc = EXIT_FAILURE;
641 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000642 } while ( ++optind < argc );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000643
Eric Andersen908e3622003-06-20 09:56:37 +0000644 return rc;
Eric Andersen0139ca92001-07-22 23:01:03 +0000645 }
646
Eric Andersen716ccb22004-01-10 11:29:31 +0000647 if (optind >= argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000648 bb_error_msg_and_die ( "No module or pattern provided\n" );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000649
Eric Andersen716ccb22004-01-10 11:29:31 +0000650 if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000651 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000652
Robert Griebl3ba7cee2002-12-03 22:41:36 +0000653 return EXIT_SUCCESS;
Eric Andersen0139ca92001-07-22 23:01:03 +0000654}