blob: 612e33a9e3d91293cde151f5108ad360aab6a4e8 [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 {
37 char * m_module;
Robert Grieblaead70b2002-07-26 15:54:20 +000038 char * m_options;
Eric Andersen3b1a7442003-12-24 20:30:45 +000039
Robert Griebl1d4ef2a2002-05-28 21:32:10 +000040 int m_isalias : 1;
41 int m_reserved : 15;
Eric Andersen3b1a7442003-12-24 20:30:45 +000042
Eric Andersen716ccb22004-01-10 11:29:31 +000043 int m_depcnt : 16;
Eric Andersen60e56f52002-04-26 06:04:01 +000044 char ** m_deparr;
Eric Andersen3b1a7442003-12-24 20:30:45 +000045
Eric Andersen60e56f52002-04-26 06:04:01 +000046 struct dep_t * m_next;
47};
48
Robert Griebl52e8d062002-05-14 23:42:08 +000049struct mod_list_t {
50 char * m_module;
Robert Grieblaead70b2002-07-26 15:54:20 +000051 char * m_options;
Eric Andersen3b1a7442003-12-24 20:30:45 +000052
Robert Griebl52e8d062002-05-14 23:42:08 +000053 struct mod_list_t * m_prev;
54 struct mod_list_t * m_next;
55};
56
57
Robert Griebl236abbf2002-05-22 23:34:35 +000058static struct dep_t *depend;
59static int autoclean, show_only, quiet, do_syslog, verbose;
Eric Andersen03d80912003-12-19 21:04:19 +000060static int k_version;
Robert Griebl52e8d062002-05-14 23:42:08 +000061
Robert Grieblaead70b2002-07-26 15:54:20 +000062int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
63{
64 char *tag, *value;
65
66 while ( isspace ( *buffer ))
Eric Andersen716ccb22004-01-10 11:29:31 +000067 buffer++;
Robert Grieblaead70b2002-07-26 15:54:20 +000068 tag = value = buffer;
69 while ( !isspace ( *value ))
Eric Andersen7e496a72004-04-06 12:06:03 +000070 if (!*value) return 0;
71 else value++;
Robert Grieblaead70b2002-07-26 15:54:20 +000072 *value++ = 0;
73 while ( isspace ( *value ))
74 value++;
Eric Andersen7e496a72004-04-06 12:06:03 +000075 if (!*value) return 0;
Robert Grieblaead70b2002-07-26 15:54:20 +000076
77 *ptag = tag;
78 *pvalue = value;
Eric Andersen3b1a7442003-12-24 20:30:45 +000079
Eric Andersen7e496a72004-04-06 12:06:03 +000080 return 1;
Robert Grieblaead70b2002-07-26 15:54:20 +000081}
Eric Andersen60e56f52002-04-26 06:04:01 +000082
Robert Grieblbc28f7a2002-06-04 19:33:58 +000083/* Jump through hoops to simulate how fgets() grabs just one line at a
84 * time... Don't use any stdio since modprobe gets called from a kernel
Eric Andersen716ccb22004-01-10 11:29:31 +000085 * thread and stdio junk can overflow the limited stack...
Robert Grieblbc28f7a2002-06-04 19:33:58 +000086 */
87static char *reads ( int fd, char *buffer, size_t len )
88{
89 int n = read ( fd, buffer, len );
Eric Andersen3b1a7442003-12-24 20:30:45 +000090
Robert Grieblbc28f7a2002-06-04 19:33:58 +000091 if ( n > 0 ) {
92 char *p;
Eric Andersen3b1a7442003-12-24 20:30:45 +000093
Robert Grieblbc28f7a2002-06-04 19:33:58 +000094 buffer [len-1] = 0;
95 p = strchr ( buffer, '\n' );
Eric Andersen3b1a7442003-12-24 20:30:45 +000096
Robert Grieblbc28f7a2002-06-04 19:33:58 +000097 if ( p ) {
98 off_t offset;
Eric Andersen3b1a7442003-12-24 20:30:45 +000099
Eric Andersen716ccb22004-01-10 11:29:31 +0000100 offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000101 lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
102
103 p[1] = 0;
104 }
105 return buffer;
106 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000107
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000108 else
109 return 0;
110}
111
Eric Andersen864b7972002-05-03 15:48:26 +0000112static struct dep_t *build_dep ( void )
Eric Andersen60e56f52002-04-26 06:04:01 +0000113{
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000114 int fd;
Eric Andersen60e56f52002-04-26 06:04:01 +0000115 struct utsname un;
Eric Andersen60e56f52002-04-26 06:04:01 +0000116 struct dep_t *first = 0;
117 struct dep_t *current = 0;
Eric Andersenc06391b2002-06-04 13:28:43 +0000118 char buffer[256];
Eric Andersen60e56f52002-04-26 06:04:01 +0000119 char *filename = buffer;
120 int continuation_line = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000121
Eric Andersen03d80912003-12-19 21:04:19 +0000122 k_version = 0;
Eric Andersen60e56f52002-04-26 06:04:01 +0000123 if ( uname ( &un ))
124 return 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000125
Robert Griebl236abbf2002-05-22 23:34:35 +0000126 // check for buffer overflow in following code
Manuel Novoa III cad53642003-03-19 09:13:01 +0000127 if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
Robert Griebl236abbf2002-05-22 23:34:35 +0000128 return 0;
Eric Andersenc06391b2002-06-04 13:28:43 +0000129 }
Eric Andersen03d80912003-12-19 21:04:19 +0000130 if (un.release[0] == '2') {
131 k_version = un.release[2] - '0';
132 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000133
Eric Andersen60e56f52002-04-26 06:04:01 +0000134 strcpy ( filename, "/lib/modules/" );
Eric Andersenc06391b2002-06-04 13:28:43 +0000135 strcat ( filename, un.release );
Eric Andersen60e56f52002-04-26 06:04:01 +0000136 strcat ( filename, "/modules.dep" );
137
Eric Andersen25ea42d2003-06-20 09:57:30 +0000138 if (( fd = open ( filename, O_RDONLY )) < 0 ) {
139
140 /* Ok, that didn't work. Fall back to looking in /lib/modules */
141 if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
142 return 0;
143 }
144 }
Eric Andersenc06391b2002-06-04 13:28:43 +0000145
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000146 while ( reads ( fd, buffer, sizeof( buffer ))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000147 int l = bb_strlen ( buffer );
Eric Andersen60e56f52002-04-26 06:04:01 +0000148 char *p = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000149
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000150 while ( isspace ( buffer [l-1] )) {
Eric Andersen60e56f52002-04-26 06:04:01 +0000151 buffer [l-1] = 0;
152 l--;
153 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000154
Eric Andersen60e56f52002-04-26 06:04:01 +0000155 if ( l == 0 ) {
156 continuation_line = 0;
157 continue;
158 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000159
Eric Andersen716ccb22004-01-10 11:29:31 +0000160 if ( !continuation_line ) {
Eric Andersen60e56f52002-04-26 06:04:01 +0000161 char *col = strchr ( buffer, ':' );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000162
Eric Andersen60e56f52002-04-26 06:04:01 +0000163 if ( col ) {
164 char *mods;
165 char *mod;
Eric Andersen864b7972002-05-03 15:48:26 +0000166 int ext = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000167
Eric Andersen60e56f52002-04-26 06:04:01 +0000168 *col = 0;
169 mods = strrchr ( buffer, '/' );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000170
Eric Andersen60e56f52002-04-26 06:04:01 +0000171 if ( !mods )
172 mods = buffer;
173 else
174 mods++;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000175
Eric Andersen03d80912003-12-19 21:04:19 +0000176#if defined(CONFIG_FEATURE_2_6_MODULES)
177 if ((k_version > 4) && ( *(col-3) == '.' ) &&
Eric Andersen3b1a7442003-12-24 20:30:45 +0000178 ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ))
Eric Andersen03d80912003-12-19 21:04:19 +0000179 ext = 3;
180 else
181#endif
Eric Andersen3b1a7442003-12-24 20:30:45 +0000182 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
183 ext = 2;
184
Eric Andersend9438372004-06-22 10:43:09 +0000185 mod = bb_xstrndup ( buffer, col - buffer );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000186
Eric Andersen60e56f52002-04-26 06:04:01 +0000187 if ( !current ) {
Eric Andersenc06391b2002-06-04 13:28:43 +0000188 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
Eric Andersen60e56f52002-04-26 06:04:01 +0000189 }
190 else {
Eric Andersenc06391b2002-06-04 13:28:43 +0000191 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
Eric Andersen60e56f52002-04-26 06:04:01 +0000192 current = current-> m_next;
193 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000194 current-> m_module = mod;
Robert Grieblaead70b2002-07-26 15:54:20 +0000195 current-> m_options = 0;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000196 current-> m_isalias = 0;
197 current-> m_depcnt = 0;
198 current-> m_deparr = 0;
199 current-> m_next = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000200
Eric Andersen864b7972002-05-03 15:48:26 +0000201 //printf ( "%s:\n", mod );
Eric Andersen716ccb22004-01-10 11:29:31 +0000202 p = col + 1;
Eric Andersen60e56f52002-04-26 06:04:01 +0000203 }
204 else
205 p = 0;
206 }
207 else
208 p = buffer;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000209
Eric Andersen9d65ab22004-01-06 00:07:17 +0000210 while ( p && *p && isblank(*p))
211 p++;
212
Eric Andersen60e56f52002-04-26 06:04:01 +0000213 if ( p && *p ) {
214 char *end = &buffer [l-1];
Eric Andersen9d65ab22004-01-06 00:07:17 +0000215 char *deps;
Eric Andersen60e56f52002-04-26 06:04:01 +0000216 char *dep;
Eric Andersen9d65ab22004-01-06 00:07:17 +0000217 char *next;
Eric Andersen864b7972002-05-03 15:48:26 +0000218 int ext = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000219
Eric Andersen60e56f52002-04-26 06:04:01 +0000220 while ( isblank ( *end ) || ( *end == '\\' ))
221 end--;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000222
Eric Andersen9d65ab22004-01-06 00:07:17 +0000223 do
224 {
225 next = strchr (p, ' ' );
226 if (next)
227 {
228 *next = 0;
229 next--;
230 }
231 else
232 next = end;
233
Eric Andersen716ccb22004-01-10 11:29:31 +0000234 deps = strrchr ( p, '/' );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000235
Eric Andersen716ccb22004-01-10 11:29:31 +0000236 if ( !deps || ( deps < p )) {
237 deps = p;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000238
Eric Andersen716ccb22004-01-10 11:29:31 +0000239 while ( isblank ( *deps ))
240 deps++;
241 }
242 else
Eric Andersen60e56f52002-04-26 06:04:01 +0000243 deps++;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000244
Eric Andersen03d80912003-12-19 21:04:19 +0000245#if defined(CONFIG_FEATURE_2_6_MODULES)
Eric Andersen9d65ab22004-01-06 00:07:17 +0000246 if ((k_version > 4) && ( *(next-2) == '.' ) && *(next-1) == 'k' &&
247 ( *next == 'o' ))
Eric Andersen716ccb22004-01-10 11:29:31 +0000248 ext = 3;
249 else
Eric Andersen03d80912003-12-19 21:04:19 +0000250#endif
Eric Andersen9d65ab22004-01-06 00:07:17 +0000251 if (( *(next-1) == '.' ) && ( *next == 'o' ))
Eric Andersen716ccb22004-01-10 11:29:31 +0000252 ext = 2;
Eric Andersenb493dec2002-07-02 19:14:23 +0000253
Eric Andersen716ccb22004-01-10 11:29:31 +0000254 /* Cope with blank lines */
Eric Andersen9d65ab22004-01-06 00:07:17 +0000255 if ((next-deps-ext+1) <= 0)
Eric Andersen716ccb22004-01-10 11:29:31 +0000256 continue;
Eric Andersen9d65ab22004-01-06 00:07:17 +0000257 dep = bb_xstrndup ( deps, next - deps - ext + 1 );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000258
Eric Andersen716ccb22004-01-10 11:29:31 +0000259 current-> m_depcnt++;
260 current-> m_deparr = (char **) xrealloc ( current-> m_deparr,
261 sizeof ( char *) * current-> m_depcnt );
262 current-> m_deparr [current-> m_depcnt - 1] = dep;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000263
Eric Andersen716ccb22004-01-10 11:29:31 +0000264 //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
265 p = next + 2;
Eric Andersen9d65ab22004-01-06 00:07:17 +0000266 } while (next < end);
Eric Andersen60e56f52002-04-26 06:04:01 +0000267 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000268
Eric Andersen60e56f52002-04-26 06:04:01 +0000269 if ( buffer [l-1] == '\\' )
270 continuation_line = 1;
271 else
272 continuation_line = 0;
273 }
Eric Andersenc06391b2002-06-04 13:28:43 +0000274 close ( fd );
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000275
276 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
277
Robert Griebl36a836d2004-07-22 00:03:39 +0000278#if defined(CONFIG_FEATURE_2_6_MODULES)
279 if (( fd = open ( "/etc/modprobe.conf", O_RDONLY )) < 0 )
280#endif
281 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
282 if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
283 return first;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000284
Eric Andersenc06391b2002-06-04 13:28:43 +0000285 continuation_line = 0;
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000286 while ( reads ( fd, buffer, sizeof( buffer ))) {
Eric Andersenc06391b2002-06-04 13:28:43 +0000287 int l;
288 char *p;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000289
Eric Andersen716ccb22004-01-10 11:29:31 +0000290 p = strchr ( buffer, '#' );
Eric Andersenc06391b2002-06-04 13:28:43 +0000291 if ( p )
292 *p = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000293
Manuel Novoa III cad53642003-03-19 09:13:01 +0000294 l = bb_strlen ( buffer );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000295
Eric Andersenc06391b2002-06-04 13:28:43 +0000296 while ( l && isspace ( buffer [l-1] )) {
297 buffer [l-1] = 0;
298 l--;
299 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000300
Eric Andersenc06391b2002-06-04 13:28:43 +0000301 if ( l == 0 ) {
302 continuation_line = 0;
303 continue;
304 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000305
Eric Andersen716ccb22004-01-10 11:29:31 +0000306 if ( !continuation_line ) {
Eric Andersenc06391b2002-06-04 13:28:43 +0000307 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
308 char *alias, *mod;
309
Robert Grieblaead70b2002-07-26 15:54:20 +0000310 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
311 // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000312
Robert Grieblaead70b2002-07-26 15:54:20 +0000313 if ( !current ) {
Eric Andersenba05e792004-04-06 12:04:14 +0000314 first = current = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
Robert Grieblaead70b2002-07-26 15:54:20 +0000315 }
316 else {
Eric Andersenba05e792004-04-06 12:04:14 +0000317 current-> m_next = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
Robert Grieblaead70b2002-07-26 15:54:20 +0000318 current = current-> m_next;
319 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000320 current-> m_module = bb_xstrdup ( alias );
Robert Grieblaead70b2002-07-26 15:54:20 +0000321 current-> m_isalias = 1;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000322
Eric Andersend298bd12004-04-06 12:05:04 +0000323 if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) {
Robert Grieblaead70b2002-07-26 15:54:20 +0000324 current-> m_depcnt = 0;
325 current-> m_deparr = 0;
326 }
327 else {
328 current-> m_depcnt = 1;
329 current-> m_deparr = xmalloc ( 1 * sizeof( char * ));
Manuel Novoa III cad53642003-03-19 09:13:01 +0000330 current-> m_deparr[0] = bb_xstrdup ( mod );
Robert Grieblaead70b2002-07-26 15:54:20 +0000331 }
Eric Andersen716ccb22004-01-10 11:29:31 +0000332 current-> m_next = 0;
Eric Andersenc06391b2002-06-04 13:28:43 +0000333 }
Eric Andersen716ccb22004-01-10 11:29:31 +0000334 }
335 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
Robert Grieblaead70b2002-07-26 15:54:20 +0000336 char *mod, *opt;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000337
Robert Grieblaead70b2002-07-26 15:54:20 +0000338 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
339 struct dep_t *dt;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000340
Robert Grieblaead70b2002-07-26 15:54:20 +0000341 for ( dt = first; dt; dt = dt-> m_next ) {
Eric Andersen716ccb22004-01-10 11:29:31 +0000342 if ( strcmp ( dt-> m_module, mod ) == 0 )
Robert Grieblaead70b2002-07-26 15:54:20 +0000343 break;
344 }
345 if ( dt ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000346 dt-> m_options = xrealloc ( dt-> m_options, bb_strlen( opt ) + 1 );
Robert Grieblaead70b2002-07-26 15:54:20 +0000347 strcpy ( dt-> m_options, opt );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000348
Robert Grieblaead70b2002-07-26 15:54:20 +0000349 // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_module, dt-> m_options );
350 }
351 }
352 }
Eric Andersenc06391b2002-06-04 13:28:43 +0000353 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000354 }
Eric Andersenc06391b2002-06-04 13:28:43 +0000355 close ( fd );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000356
Eric Andersen60e56f52002-04-26 06:04:01 +0000357 return first;
358}
359
Eric Andersend9438372004-06-22 10:43:09 +0000360/* check if /lib/modules/bar/foo.ko belongs to module foo */
361/* return 1 = found, 0 = not found */
362static int mod_strcmp ( const char *mod_path, const char *mod_name )
363{
364#if defined(CONFIG_FEATURE_2_6_MODULES)
365#define MODULE_EXTENSION ".ko"
366#define MOD_EXTENSION_LEN 3
367#else
368#define MODULE_EXTENSION ".o"
369#define MOD_EXTENSION_LEN 2
370#endif
371 if ((strstr (mod_path, mod_name) ==
372 (mod_path + strlen(mod_path) -
373 strlen(mod_name) - MOD_EXTENSION_LEN))
374 && (!strcmp(mod_path + strlen(mod_path) -
375 MOD_EXTENSION_LEN, MODULE_EXTENSION)))
376 {
377 return 1;
378 }
379 return 0;
380}
381
Glenn L McGrath350733a2003-09-08 00:32:49 +0000382/* return 1 = loaded, 0 = not loaded, -1 = can't tell */
383static int already_loaded (const char *name)
384{
385 int fd;
386 char buffer[256];
387
388 fd = open ("/proc/modules", O_RDONLY);
389 if (fd < 0)
390 return -1;
391
392 while ( reads ( fd, buffer, sizeof( buffer ))) {
393 char *p;
394
395 p = strchr (buffer, ' ');
396 if (p) {
397 *p = 0;
Eric Andersend9438372004-06-22 10:43:09 +0000398 if (mod_strcmp (name, buffer)) {
Glenn L McGrath350733a2003-09-08 00:32:49 +0000399 close (fd);
400 return 1;
401 }
402 }
403 }
404
405 close (fd);
406 return 0;
407}
Eric Andersen60e56f52002-04-26 06:04:01 +0000408
Robert Griebl236abbf2002-05-22 23:34:35 +0000409static int mod_process ( struct mod_list_t *list, int do_insert )
Eric Andersen60e56f52002-04-26 06:04:01 +0000410{
Robert Griebl52e8d062002-05-14 23:42:08 +0000411 char lcmd [256];
Eric Andersen908e3622003-06-20 09:56:37 +0000412 int rc = 1;
Eric Andersen864b7972002-05-03 15:48:26 +0000413
Robert Griebl52e8d062002-05-14 23:42:08 +0000414 while ( list ) {
Eric Andersen61b038a2003-11-14 02:49:19 +0000415 *lcmd = '\0';
Glenn L McGrath350733a2003-09-08 00:32:49 +0000416 if ( do_insert ) {
417 if (already_loaded (list->m_module) != 1)
Eric Andersen716ccb22004-01-10 11:29:31 +0000418 snprintf ( lcmd, sizeof( lcmd ) - 1, "insmod %s %s %s %s %s",
419 do_syslog ? "-s" : "", autoclean ? "-k" : "",
420 quiet ? "-q" : "", list-> m_module, list-> m_options ?
421 list-> m_options : "" );
Glenn L McGrath350733a2003-09-08 00:32:49 +0000422 } else {
423 if (already_loaded (list->m_module) != 0)
Eric Andersen716ccb22004-01-10 11:29:31 +0000424 snprintf ( lcmd, sizeof( lcmd ) - 1, "rmmod %s %s",
425 do_syslog ? "-s" : "", list-> m_module );
Glenn L McGrath350733a2003-09-08 00:32:49 +0000426 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000427
Robert Griebl52e8d062002-05-14 23:42:08 +0000428 if ( verbose )
429 printf ( "%s\n", lcmd );
Eric Andersen61b038a2003-11-14 02:49:19 +0000430 if ( !show_only && *lcmd) {
Eric Andersen908e3622003-06-20 09:56:37 +0000431 int rc2 = system ( lcmd );
432 if (do_insert) rc = rc2; /* only last module matters */
433 else if (!rc2) rc = 0; /* success if remove any mod */
434 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000435
Robert Griebl52e8d062002-05-14 23:42:08 +0000436 list = do_insert ? list-> m_prev : list-> m_next;
Eric Andersen60e56f52002-04-26 06:04:01 +0000437 }
Eric Andersen908e3622003-06-20 09:56:37 +0000438 return (show_only) ? 0 : rc;
Eric Andersen60e56f52002-04-26 06:04:01 +0000439}
440
Robert Griebl52e8d062002-05-14 23:42:08 +0000441static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
Eric Andersen60e56f52002-04-26 06:04:01 +0000442{
Robert Griebl52e8d062002-05-14 23:42:08 +0000443 struct mod_list_t *find;
Eric Andersen60e56f52002-04-26 06:04:01 +0000444 struct dep_t *dt;
Robert Grieblaead70b2002-07-26 15:54:20 +0000445 char *opt = 0;
Robert Griebl52e8d062002-05-14 23:42:08 +0000446 int lm;
447
448 // remove .o extension
Manuel Novoa III cad53642003-03-19 09:13:01 +0000449 lm = bb_strlen ( mod );
Eric Andersen03d80912003-12-19 21:04:19 +0000450
451#if defined(CONFIG_FEATURE_2_6_MODULES)
452 if ((k_version > 4) && ( mod [lm-3] == '.' ) &&
Eric Andersen3b1a7442003-12-24 20:30:45 +0000453 ( mod [lm-2] == 'k' ) && ( mod [lm-1] == 'o' ))
Eric Andersen03d80912003-12-19 21:04:19 +0000454 mod [lm-3] = 0;
455 else
456#endif
Eric Andersen3b1a7442003-12-24 20:30:45 +0000457 if (( mod [lm-2] == '.' ) && ( mod [lm-1] == 'o' ))
458 mod [lm-2] = 0;
Robert Griebl52e8d062002-05-14 23:42:08 +0000459
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000460 // check dependencies
461 for ( dt = depend; dt; dt = dt-> m_next ) {
Eric Andersend9438372004-06-22 10:43:09 +0000462 if ( mod_strcmp ( dt-> m_module, mod )) {
463 mod = dt-> m_module;
Robert Grieblaead70b2002-07-26 15:54:20 +0000464 opt = dt-> m_options;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000465 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000466 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000467 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000468
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000469 // resolve alias names
Robert Grieblaead70b2002-07-26 15:54:20 +0000470 while ( dt && dt-> m_isalias ) {
471 if ( dt-> m_depcnt == 1 ) {
472 struct dep_t *adt;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000473
Robert Grieblaead70b2002-07-26 15:54:20 +0000474 for ( adt = depend; adt; adt = adt-> m_next ) {
Robert Griebl70112da2002-07-29 20:28:38 +0000475 if ( strcmp ( adt-> m_module, dt-> m_deparr [0] ) == 0 )
Robert Grieblaead70b2002-07-26 15:54:20 +0000476 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000477 }
Robert Griebl70112da2002-07-29 20:28:38 +0000478 if ( adt ) {
Eric Andersen716ccb22004-01-10 11:29:31 +0000479 dt = adt;
Robert Griebl70112da2002-07-29 20:28:38 +0000480 mod = dt-> m_module;
481 if ( !opt )
482 opt = dt-> m_options;
483 }
Robert Grieblaead70b2002-07-26 15:54:20 +0000484 else
Robert Griebl70112da2002-07-29 20:28:38 +0000485 return;
Robert Grieblaead70b2002-07-26 15:54:20 +0000486 }
487 else
Eric Andersen716ccb22004-01-10 11:29:31 +0000488 return;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000489 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000490
Robert Griebl52e8d062002-05-14 23:42:08 +0000491 // search for duplicates
492 for ( find = *head; find; find = find-> m_next ) {
493 if ( !strcmp ( mod, find-> m_module )) {
494 // found -> dequeue it
495
496 if ( find-> m_prev )
497 find-> m_prev-> m_next = find-> m_next;
498 else
499 *head = find-> m_next;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000500
Robert Griebl52e8d062002-05-14 23:42:08 +0000501 if ( find-> m_next )
502 find-> m_next-> m_prev = find-> m_prev;
503 else
504 *tail = find-> m_prev;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000505
Robert Griebl52e8d062002-05-14 23:42:08 +0000506 break; // there can be only one duplicate
Eric Andersen716ccb22004-01-10 11:29:31 +0000507 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000508 }
509
510 if ( !find ) { // did not find a duplicate
Eric Andersen716ccb22004-01-10 11:29:31 +0000511 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
Robert Griebl52e8d062002-05-14 23:42:08 +0000512 find-> m_module = mod;
Robert Grieblaead70b2002-07-26 15:54:20 +0000513 find-> m_options = opt;
Robert Griebl52e8d062002-05-14 23:42:08 +0000514 }
515
Eric Andersen716ccb22004-01-10 11:29:31 +0000516 // enqueue at tail
Robert Griebl52e8d062002-05-14 23:42:08 +0000517 if ( *tail )
518 (*tail)-> m_next = find;
519 find-> m_prev = *tail;
520 find-> m_next = 0;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000521
Robert Griebl52e8d062002-05-14 23:42:08 +0000522 if ( !*head )
523 *head = find;
524 *tail = find;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000525
Eric Andersen716ccb22004-01-10 11:29:31 +0000526 if ( dt ) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000527 int i;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000528
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000529 for ( i = 0; i < dt-> m_depcnt; i++ )
530 check_dep ( dt-> m_deparr [i], head, tail );
531 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000532}
533
534
535
Robert Griebl236abbf2002-05-22 23:34:35 +0000536static int mod_insert ( char *mod, int argc, char **argv )
Robert Griebl52e8d062002-05-14 23:42:08 +0000537{
538 struct mod_list_t *tail = 0;
Eric Andersen716ccb22004-01-10 11:29:31 +0000539 struct mod_list_t *head = 0;
Eric Andersen908e3622003-06-20 09:56:37 +0000540 int rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000541
Robert Griebl52e8d062002-05-14 23:42:08 +0000542 // get dep list for module mod
543 check_dep ( mod, &head, &tail );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000544
Robert Griebl52e8d062002-05-14 23:42:08 +0000545 if ( head && tail ) {
Robert Grieblaead70b2002-07-26 15:54:20 +0000546 if ( argc ) {
Eric Andersen716ccb22004-01-10 11:29:31 +0000547 int i;
Robert Grieblaead70b2002-07-26 15:54:20 +0000548 int l = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000549
Robert Grieblaead70b2002-07-26 15:54:20 +0000550 // append module args
Eric Andersen716ccb22004-01-10 11:29:31 +0000551 for ( i = 0; i < argc; i++ )
Manuel Novoa III cad53642003-03-19 09:13:01 +0000552 l += ( bb_strlen ( argv [i] ) + 1 );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000553
Robert Grieblaead70b2002-07-26 15:54:20 +0000554 head-> m_options = xrealloc ( head-> m_options, l + 1 );
555 head-> m_options [0] = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000556
Robert Grieblaead70b2002-07-26 15:54:20 +0000557 for ( i = 0; i < argc; i++ ) {
558 strcat ( head-> m_options, argv [i] );
559 strcat ( head-> m_options, " " );
560 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000561 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000562
Robert Griebl52e8d062002-05-14 23:42:08 +0000563 // process tail ---> head
Eric Andersen908e3622003-06-20 09:56:37 +0000564 rc = mod_process ( tail, 1 );
Robert Griebl52e8d062002-05-14 23:42:08 +0000565 }
566 else
567 rc = 1;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000568
Robert Griebl52e8d062002-05-14 23:42:08 +0000569 return rc;
570}
571
Eric Andersen908e3622003-06-20 09:56:37 +0000572static int mod_remove ( char *mod )
Robert Griebl52e8d062002-05-14 23:42:08 +0000573{
Eric Andersen908e3622003-06-20 09:56:37 +0000574 int rc;
Eric Andersen716ccb22004-01-10 11:29:31 +0000575 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
Eric Andersen3b1a7442003-12-24 20:30:45 +0000576
Robert Griebl52e8d062002-05-14 23:42:08 +0000577 struct mod_list_t *head = 0;
578 struct mod_list_t *tail = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000579
Robert Griebl52e8d062002-05-14 23:42:08 +0000580 if ( mod )
581 check_dep ( mod, &head, &tail );
582 else // autoclean
583 head = tail = &rm_a_dummy;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000584
Robert Griebl236abbf2002-05-22 23:34:35 +0000585 if ( head && tail )
Eric Andersen908e3622003-06-20 09:56:37 +0000586 rc = mod_process ( head, 0 ); // process head ---> tail
587 else
588 rc = 1;
589 return rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000590
Robert Griebl52e8d062002-05-14 23:42:08 +0000591}
592
593
Eric Andersen60e56f52002-04-26 06:04:01 +0000594
Eric Andersen0139ca92001-07-22 23:01:03 +0000595extern int modprobe_main(int argc, char** argv)
596{
Robert Griebl236abbf2002-05-22 23:34:35 +0000597 int opt;
598 int remove_opt = 0;
Eric Andersen0139ca92001-07-22 23:01:03 +0000599
Robert Griebl236abbf2002-05-22 23:34:35 +0000600 autoclean = show_only = quiet = do_syslog = verbose = 0;
601
602 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
603 switch(opt) {
Eric Andersen3b1a7442003-12-24 20:30:45 +0000604 case 'c': // no config used
605 case 'l': // no pattern matching
606 return EXIT_SUCCESS;
607 break;
608 case 'C': // no config used
609 case 't': // no pattern matching
610 bb_error_msg_and_die("-t and -C not supported");
Robert Griebl236abbf2002-05-22 23:34:35 +0000611
Eric Andersen3b1a7442003-12-24 20:30:45 +0000612 case 'a': // ignore
613 case 'd': // ignore
614 break;
615 case 'k':
616 autoclean++;
617 break;
618 case 'n':
619 show_only++;
620 break;
621 case 'q':
622 quiet++;
623 break;
624 case 'r':
625 remove_opt++;
626 break;
627 case 's':
628 do_syslog++;
629 break;
630 case 'v':
631 verbose++;
632 break;
633 case 'V':
634 default:
635 bb_show_usage();
636 break;
Eric Andersen0139ca92001-07-22 23:01:03 +0000637 }
Eric Andersen0139ca92001-07-22 23:01:03 +0000638 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000639
Eric Andersen716ccb22004-01-10 11:29:31 +0000640 depend = build_dep ( );
Robert Griebl52e8d062002-05-14 23:42:08 +0000641
Eric Andersen716ccb22004-01-10 11:29:31 +0000642 if ( !depend )
Manuel Novoa III cad53642003-03-19 09:13:01 +0000643 bb_error_msg_and_die ( "could not parse modules.dep\n" );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000644
Eric Andersen1b064192001-07-25 07:23:38 +0000645 if (remove_opt) {
Eric Andersen908e3622003-06-20 09:56:37 +0000646 int rc = EXIT_SUCCESS;
Eric Andersen0139ca92001-07-22 23:01:03 +0000647 do {
Eric Andersen908e3622003-06-20 09:56:37 +0000648 if (mod_remove ( optind < argc ?
Eric Andersen3b1a7442003-12-24 20:30:45 +0000649 bb_xstrdup (argv [optind]) : NULL )) {
Eric Andersen908e3622003-06-20 09:56:37 +0000650 bb_error_msg ("failed to remove module %s",
Eric Andersen3b1a7442003-12-24 20:30:45 +0000651 argv [optind] );
Eric Andersen908e3622003-06-20 09:56:37 +0000652 rc = EXIT_FAILURE;
653 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000654 } while ( ++optind < argc );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000655
Eric Andersen908e3622003-06-20 09:56:37 +0000656 return rc;
Eric Andersen0139ca92001-07-22 23:01:03 +0000657 }
658
Eric Andersen716ccb22004-01-10 11:29:31 +0000659 if (optind >= argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000660 bb_error_msg_and_die ( "No module or pattern provided\n" );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000661
Eric Andersen716ccb22004-01-10 11:29:31 +0000662 if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000663 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000664
Robert Griebl3ba7cee2002-12-03 22:41:36 +0000665 return EXIT_SUCCESS;
Eric Andersen0139ca92001-07-22 23:01:03 +0000666}
667
668