blob: 8d739ef3c08eb7765267c50921dbc47a112345c9 [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
Paul Fox8eeb6552005-08-04 18:33:36 +00007 * Copyright (c) 2005 by Jim Bauer, jfbauer@nfr.com
Robert Griebl6859d762002-08-05 02:57:12 +00008 *
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*/
Eric Andersen0139ca92001-07-22 23:01:03 +000024
Robert Griebl52e8d062002-05-14 23:42:08 +000025#include <sys/utsname.h>
Paul Fox8eeb6552005-08-04 18:33:36 +000026#include <sys/types.h>
27#include <sys/wait.h>
Eric Andersen0139ca92001-07-22 23:01:03 +000028#include <getopt.h>
29#include <stdlib.h>
30#include <unistd.h>
31#include <syslog.h>
32#include <string.h>
Eric Andersen60e56f52002-04-26 06:04:01 +000033#include <ctype.h>
Eric Andersenc06391b2002-06-04 13:28:43 +000034#include <fcntl.h>
Eric Andersen0139ca92001-07-22 23:01:03 +000035#include "busybox.h"
36
Eric Andersen0139ca92001-07-22 23:01:03 +000037
Eric Andersen60e56f52002-04-26 06:04:01 +000038
39struct dep_t {
Eric Andersen807bd842004-08-19 18:30:31 +000040 char * m_name;
41 char * m_path;
Robert Grieblaead70b2002-07-26 15:54:20 +000042 char * m_options;
Eric Andersen3b1a7442003-12-24 20:30:45 +000043
Robert Griebl1d4ef2a2002-05-28 21:32:10 +000044 int m_isalias : 1;
45 int m_reserved : 15;
Eric Andersen3b1a7442003-12-24 20:30:45 +000046
Eric Andersen716ccb22004-01-10 11:29:31 +000047 int m_depcnt : 16;
Eric Andersen60e56f52002-04-26 06:04:01 +000048 char ** m_deparr;
Eric Andersen3b1a7442003-12-24 20:30:45 +000049
Eric Andersen60e56f52002-04-26 06:04:01 +000050 struct dep_t * m_next;
51};
52
Robert Griebl52e8d062002-05-14 23:42:08 +000053struct mod_list_t {
Eric Andersen807bd842004-08-19 18:30:31 +000054 char * m_name;
55 char * m_path;
Robert Grieblaead70b2002-07-26 15:54:20 +000056 char * m_options;
Eric Andersen3b1a7442003-12-24 20:30:45 +000057
Robert Griebl52e8d062002-05-14 23:42:08 +000058 struct mod_list_t * m_prev;
59 struct mod_list_t * m_next;
60};
61
62
Robert Griebl236abbf2002-05-22 23:34:35 +000063static struct dep_t *depend;
64static int autoclean, show_only, quiet, do_syslog, verbose;
Eric Andersen03d80912003-12-19 21:04:19 +000065static int k_version;
Robert Griebl52e8d062002-05-14 23:42:08 +000066
Eric Andersen14f5c8d2005-04-16 19:39:00 +000067static int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
Robert Grieblaead70b2002-07-26 15:54:20 +000068{
69 char *tag, *value;
70
71 while ( isspace ( *buffer ))
Eric Andersen716ccb22004-01-10 11:29:31 +000072 buffer++;
Robert Grieblaead70b2002-07-26 15:54:20 +000073 tag = value = buffer;
74 while ( !isspace ( *value ))
Eric Andersen7e496a72004-04-06 12:06:03 +000075 if (!*value) return 0;
76 else value++;
Robert Grieblaead70b2002-07-26 15:54:20 +000077 *value++ = 0;
78 while ( isspace ( *value ))
79 value++;
Eric Andersen7e496a72004-04-06 12:06:03 +000080 if (!*value) return 0;
Robert Grieblaead70b2002-07-26 15:54:20 +000081
82 *ptag = tag;
83 *pvalue = value;
Eric Andersen3b1a7442003-12-24 20:30:45 +000084
Eric Andersen7e496a72004-04-06 12:06:03 +000085 return 1;
Robert Grieblaead70b2002-07-26 15:54:20 +000086}
Eric Andersen60e56f52002-04-26 06:04:01 +000087
Robert Grieblbc28f7a2002-06-04 19:33:58 +000088/* 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
Eric Andersen716ccb22004-01-10 11:29:31 +000090 * thread and stdio junk can overflow the limited stack...
Robert Grieblbc28f7a2002-06-04 19:33:58 +000091 */
92static char *reads ( int fd, char *buffer, size_t len )
93{
94 int n = read ( fd, buffer, len );
Eric Andersen3b1a7442003-12-24 20:30:45 +000095
Robert Grieblbc28f7a2002-06-04 19:33:58 +000096 if ( n > 0 ) {
97 char *p;
Eric Andersen3b1a7442003-12-24 20:30:45 +000098
Robert Grieblbc28f7a2002-06-04 19:33:58 +000099 buffer [len-1] = 0;
100 p = strchr ( buffer, '\n' );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000101
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000102 if ( p ) {
103 off_t offset;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000104
Eric Andersen716ccb22004-01-10 11:29:31 +0000105 offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000106 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 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000112
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000113 else
114 return 0;
115}
116
Eric Andersen864b7972002-05-03 15:48:26 +0000117static struct dep_t *build_dep ( void )
Eric Andersen60e56f52002-04-26 06:04:01 +0000118{
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000119 int fd;
Eric Andersen60e56f52002-04-26 06:04:01 +0000120 struct utsname un;
Eric Andersen60e56f52002-04-26 06:04:01 +0000121 struct dep_t *first = 0;
122 struct dep_t *current = 0;
Eric Andersenbeaef162004-08-14 20:57:33 +0000123 char buffer[2048];
Eric Andersen60e56f52002-04-26 06:04:01 +0000124 char *filename = buffer;
125 int continuation_line = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000126
Eric Andersen03d80912003-12-19 21:04:19 +0000127 k_version = 0;
Eric Andersen60e56f52002-04-26 06:04:01 +0000128 if ( uname ( &un ))
129 return 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000130
Robert Griebl236abbf2002-05-22 23:34:35 +0000131 // check for buffer overflow in following code
Manuel Novoa III cad53642003-03-19 09:13:01 +0000132 if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
Robert Griebl236abbf2002-05-22 23:34:35 +0000133 return 0;
Eric Andersenc06391b2002-06-04 13:28:43 +0000134 }
Eric Andersen03d80912003-12-19 21:04:19 +0000135 if (un.release[0] == '2') {
136 k_version = un.release[2] - '0';
137 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000138
Eric Andersen60e56f52002-04-26 06:04:01 +0000139 strcpy ( filename, "/lib/modules/" );
Eric Andersenc06391b2002-06-04 13:28:43 +0000140 strcat ( filename, un.release );
Eric Andersen60e56f52002-04-26 06:04:01 +0000141 strcat ( filename, "/modules.dep" );
142
Eric Andersen25ea42d2003-06-20 09:57:30 +0000143 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 }
Eric Andersenc06391b2002-06-04 13:28:43 +0000150
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000151 while ( reads ( fd, buffer, sizeof( buffer ))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000152 int l = bb_strlen ( buffer );
Eric Andersen60e56f52002-04-26 06:04:01 +0000153 char *p = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000154
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000155 while ( isspace ( buffer [l-1] )) {
Eric Andersen60e56f52002-04-26 06:04:01 +0000156 buffer [l-1] = 0;
157 l--;
158 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000159
Eric Andersen60e56f52002-04-26 06:04:01 +0000160 if ( l == 0 ) {
161 continuation_line = 0;
162 continue;
163 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000164
Eric Andersen716ccb22004-01-10 11:29:31 +0000165 if ( !continuation_line ) {
Eric Andersen60e56f52002-04-26 06:04:01 +0000166 char *col = strchr ( buffer, ':' );
Eric Andersen807bd842004-08-19 18:30:31 +0000167 char *dot = col;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000168
Eric Andersen60e56f52002-04-26 06:04:01 +0000169 if ( col ) {
170 char *mods;
Eric Andersen807bd842004-08-19 18:30:31 +0000171 char *modpath;
Eric Andersen60e56f52002-04-26 06:04:01 +0000172 char *mod;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000173
Eric Andersen60e56f52002-04-26 06:04:01 +0000174 *col = 0;
175 mods = strrchr ( buffer, '/' );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000176
Eric Andersen60e56f52002-04-26 06:04:01 +0000177 if ( !mods )
178 mods = buffer;
179 else
180 mods++;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000181
Eric Andersen807bd842004-08-19 18:30:31 +0000182 modpath = strchr ( buffer, '/' );
183 if ( !modpath )
184 modpath = buffer;
Eric Andersen03d80912003-12-19 21:04:19 +0000185#if defined(CONFIG_FEATURE_2_6_MODULES)
186 if ((k_version > 4) && ( *(col-3) == '.' ) &&
Eric Andersen3b1a7442003-12-24 20:30:45 +0000187 ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ))
Eric Andersen807bd842004-08-19 18:30:31 +0000188 dot = col - 3;
Eric Andersen03d80912003-12-19 21:04:19 +0000189 else
190#endif
Eric Andersen3b1a7442003-12-24 20:30:45 +0000191 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
Eric Andersen807bd842004-08-19 18:30:31 +0000192 dot = col - 2;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000193
Eric Andersen807bd842004-08-19 18:30:31 +0000194 mod = bb_xstrndup ( mods, dot - mods );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000195
Eric Andersen60e56f52002-04-26 06:04:01 +0000196 if ( !current ) {
Eric Andersenc06391b2002-06-04 13:28:43 +0000197 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
Eric Andersen60e56f52002-04-26 06:04:01 +0000198 }
199 else {
Eric Andersenc06391b2002-06-04 13:28:43 +0000200 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
Eric Andersen60e56f52002-04-26 06:04:01 +0000201 current = current-> m_next;
202 }
Eric Andersen807bd842004-08-19 18:30:31 +0000203 current-> m_name = mod;
204 current-> m_path = bb_xstrdup(modpath);
Robert Grieblaead70b2002-07-26 15:54:20 +0000205 current-> m_options = 0;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000206 current-> m_isalias = 0;
207 current-> m_depcnt = 0;
208 current-> m_deparr = 0;
209 current-> m_next = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000210
Eric Andersen864b7972002-05-03 15:48:26 +0000211 //printf ( "%s:\n", mod );
Eric Andersen716ccb22004-01-10 11:29:31 +0000212 p = col + 1;
Eric Andersen60e56f52002-04-26 06:04:01 +0000213 }
214 else
215 p = 0;
216 }
217 else
218 p = buffer;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000219
Eric Andersen9d65ab22004-01-06 00:07:17 +0000220 while ( p && *p && isblank(*p))
221 p++;
222
Eric Andersen60e56f52002-04-26 06:04:01 +0000223 if ( p && *p ) {
224 char *end = &buffer [l-1];
Eric Andersen9d65ab22004-01-06 00:07:17 +0000225 char *deps;
Eric Andersen60e56f52002-04-26 06:04:01 +0000226 char *dep;
Eric Andersen9d65ab22004-01-06 00:07:17 +0000227 char *next;
Eric Andersen864b7972002-05-03 15:48:26 +0000228 int ext = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000229
Eric Andersen60e56f52002-04-26 06:04:01 +0000230 while ( isblank ( *end ) || ( *end == '\\' ))
231 end--;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000232
Eric Andersen9d65ab22004-01-06 00:07:17 +0000233 do
234 {
235 next = strchr (p, ' ' );
236 if (next)
237 {
238 *next = 0;
239 next--;
240 }
241 else
242 next = end;
243
Eric Andersen716ccb22004-01-10 11:29:31 +0000244 deps = strrchr ( p, '/' );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000245
Eric Andersen716ccb22004-01-10 11:29:31 +0000246 if ( !deps || ( deps < p )) {
247 deps = p;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000248
Eric Andersen716ccb22004-01-10 11:29:31 +0000249 while ( isblank ( *deps ))
250 deps++;
251 }
252 else
Eric Andersen60e56f52002-04-26 06:04:01 +0000253 deps++;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000254
Eric Andersen03d80912003-12-19 21:04:19 +0000255#if defined(CONFIG_FEATURE_2_6_MODULES)
Eric Andersen9d65ab22004-01-06 00:07:17 +0000256 if ((k_version > 4) && ( *(next-2) == '.' ) && *(next-1) == 'k' &&
257 ( *next == 'o' ))
Eric Andersen716ccb22004-01-10 11:29:31 +0000258 ext = 3;
259 else
Eric Andersen03d80912003-12-19 21:04:19 +0000260#endif
Eric Andersen9d65ab22004-01-06 00:07:17 +0000261 if (( *(next-1) == '.' ) && ( *next == 'o' ))
Eric Andersen716ccb22004-01-10 11:29:31 +0000262 ext = 2;
Eric Andersenb493dec2002-07-02 19:14:23 +0000263
Eric Andersen716ccb22004-01-10 11:29:31 +0000264 /* Cope with blank lines */
Eric Andersen9d65ab22004-01-06 00:07:17 +0000265 if ((next-deps-ext+1) <= 0)
Eric Andersen716ccb22004-01-10 11:29:31 +0000266 continue;
Eric Andersen9d65ab22004-01-06 00:07:17 +0000267 dep = bb_xstrndup ( deps, next - deps - ext + 1 );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000268
Eric Andersen716ccb22004-01-10 11:29:31 +0000269 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;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000273
Eric Andersen716ccb22004-01-10 11:29:31 +0000274 //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
275 p = next + 2;
Eric Andersen9d65ab22004-01-06 00:07:17 +0000276 } while (next < end);
Eric Andersen60e56f52002-04-26 06:04:01 +0000277 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000278
Eric Andersen60e56f52002-04-26 06:04:01 +0000279 if ( buffer [l-1] == '\\' )
280 continuation_line = 1;
281 else
282 continuation_line = 0;
283 }
Eric Andersenc06391b2002-06-04 13:28:43 +0000284 close ( fd );
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000285
286 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
287
Robert Griebl36a836d2004-07-22 00:03:39 +0000288#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;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000294
Eric Andersenc06391b2002-06-04 13:28:43 +0000295 continuation_line = 0;
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000296 while ( reads ( fd, buffer, sizeof( buffer ))) {
Eric Andersenc06391b2002-06-04 13:28:43 +0000297 int l;
298 char *p;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000299
Eric Andersen716ccb22004-01-10 11:29:31 +0000300 p = strchr ( buffer, '#' );
Eric Andersenc06391b2002-06-04 13:28:43 +0000301 if ( p )
302 *p = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000303
Manuel Novoa III cad53642003-03-19 09:13:01 +0000304 l = bb_strlen ( buffer );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000305
Eric Andersenc06391b2002-06-04 13:28:43 +0000306 while ( l && isspace ( buffer [l-1] )) {
307 buffer [l-1] = 0;
308 l--;
309 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000310
Eric Andersenc06391b2002-06-04 13:28:43 +0000311 if ( l == 0 ) {
312 continuation_line = 0;
313 continue;
314 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000315
Eric Andersen716ccb22004-01-10 11:29:31 +0000316 if ( !continuation_line ) {
Eric Andersenc06391b2002-06-04 13:28:43 +0000317 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
318 char *alias, *mod;
319
Robert Grieblaead70b2002-07-26 15:54:20 +0000320 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
321 // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000322
Robert Grieblaead70b2002-07-26 15:54:20 +0000323 if ( !current ) {
Eric Andersenba05e792004-04-06 12:04:14 +0000324 first = current = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
Robert Grieblaead70b2002-07-26 15:54:20 +0000325 }
326 else {
Eric Andersenba05e792004-04-06 12:04:14 +0000327 current-> m_next = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
Robert Grieblaead70b2002-07-26 15:54:20 +0000328 current = current-> m_next;
329 }
Eric Andersen807bd842004-08-19 18:30:31 +0000330 current-> m_name = bb_xstrdup ( alias );
Robert Grieblaead70b2002-07-26 15:54:20 +0000331 current-> m_isalias = 1;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000332
Eric Andersend298bd12004-04-06 12:05:04 +0000333 if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) {
Robert Grieblaead70b2002-07-26 15:54:20 +0000334 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 * ));
Manuel Novoa III cad53642003-03-19 09:13:01 +0000340 current-> m_deparr[0] = bb_xstrdup ( mod );
Robert Grieblaead70b2002-07-26 15:54:20 +0000341 }
Eric Andersen716ccb22004-01-10 11:29:31 +0000342 current-> m_next = 0;
Eric Andersenc06391b2002-06-04 13:28:43 +0000343 }
Eric Andersen716ccb22004-01-10 11:29:31 +0000344 }
345 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
Robert Grieblaead70b2002-07-26 15:54:20 +0000346 char *mod, *opt;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000347
Robert Grieblaead70b2002-07-26 15:54:20 +0000348 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
349 struct dep_t *dt;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000350
Robert Grieblaead70b2002-07-26 15:54:20 +0000351 for ( dt = first; dt; dt = dt-> m_next ) {
Eric Andersen807bd842004-08-19 18:30:31 +0000352 if ( strcmp ( dt-> m_name, mod ) == 0 )
Robert Grieblaead70b2002-07-26 15:54:20 +0000353 break;
354 }
355 if ( dt ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000356 dt-> m_options = xrealloc ( dt-> m_options, bb_strlen( opt ) + 1 );
Robert Grieblaead70b2002-07-26 15:54:20 +0000357 strcpy ( dt-> m_options, opt );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000358
Eric Andersen807bd842004-08-19 18:30:31 +0000359 // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_name, dt-> m_options );
Robert Grieblaead70b2002-07-26 15:54:20 +0000360 }
361 }
362 }
Eric Andersenc06391b2002-06-04 13:28:43 +0000363 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000364 }
Eric Andersenc06391b2002-06-04 13:28:43 +0000365 close ( fd );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000366
Eric Andersen60e56f52002-04-26 06:04:01 +0000367 return first;
368}
369
Glenn L McGrath350733a2003-09-08 00:32:49 +0000370/* return 1 = loaded, 0 = not loaded, -1 = can't tell */
371static int already_loaded (const char *name)
372{
373 int fd;
Eric Andersene3a792c2004-08-16 10:22:34 +0000374 char buffer[4096];
Glenn L McGrath350733a2003-09-08 00:32:49 +0000375
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;
Eric Andersen807bd842004-08-19 18:30:31 +0000386 if (strcmp (name, buffer) == 0) {
Glenn L McGrath350733a2003-09-08 00:32:49 +0000387 close (fd);
388 return 1;
389 }
390 }
391 }
392
393 close (fd);
394 return 0;
395}
Eric Andersen60e56f52002-04-26 06:04:01 +0000396
Robert Griebl236abbf2002-05-22 23:34:35 +0000397static int mod_process ( struct mod_list_t *list, int do_insert )
Eric Andersen60e56f52002-04-26 06:04:01 +0000398{
Eric Andersen44b57582004-08-03 08:23:33 +0000399 int rc = 0;
Paul Fox8eeb6552005-08-04 18:33:36 +0000400 char *argv[10];
401 int argc;
Eric Andersen864b7972002-05-03 15:48:26 +0000402
Robert Griebl52e8d062002-05-14 23:42:08 +0000403 while ( list ) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000404 argc = 0;
Glenn L McGrath350733a2003-09-08 00:32:49 +0000405 if ( do_insert ) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000406 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 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000418 } else {
Eric Andersen807bd842004-08-19 18:30:31 +0000419 /* modutils uses short name for removal */
Paul Fox8eeb6552005-08-04 18:33:36 +0000420 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 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000426 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000427 argv[argc] = NULL;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000428
Paul Fox8eeb6552005-08-04 18:33:36 +0000429 if (argc) {
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000430 if (verbose) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000431 int i;
432 for (i=0; i<argc; i++)
433 printf("%s ", argv[i]);
434 printf("\n");
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000435 }
436 if (!show_only) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000437 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 }
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000458 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 }
Eric Andersen908e3622003-06-20 09:56:37 +0000465 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000466 list = do_insert ? list-> m_prev : list-> m_next;
Eric Andersen60e56f52002-04-26 06:04:01 +0000467 }
Eric Andersen908e3622003-06-20 09:56:37 +0000468 return (show_only) ? 0 : rc;
Eric Andersen60e56f52002-04-26 06:04:01 +0000469}
470
Robert Griebl52e8d062002-05-14 23:42:08 +0000471static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
Eric Andersen60e56f52002-04-26 06:04:01 +0000472{
Robert Griebl52e8d062002-05-14 23:42:08 +0000473 struct mod_list_t *find;
Eric Andersen60e56f52002-04-26 06:04:01 +0000474 struct dep_t *dt;
Robert Grieblaead70b2002-07-26 15:54:20 +0000475 char *opt = 0;
Eric Andersen807bd842004-08-19 18:30:31 +0000476 char *path = 0;
Robert Griebl52e8d062002-05-14 23:42:08 +0000477
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000478 // check dependencies
479 for ( dt = depend; dt; dt = dt-> m_next ) {
Eric Andersen807bd842004-08-19 18:30:31 +0000480 if ( strcmp ( dt-> m_name, mod ) == 0) {
481 mod = dt-> m_name;
482 path = dt-> m_path;
Robert Grieblaead70b2002-07-26 15:54:20 +0000483 opt = dt-> m_options;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000484 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000485 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000486 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000487
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000488 // resolve alias names
Robert Grieblaead70b2002-07-26 15:54:20 +0000489 while ( dt && dt-> m_isalias ) {
490 if ( dt-> m_depcnt == 1 ) {
491 struct dep_t *adt;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000492
Robert Grieblaead70b2002-07-26 15:54:20 +0000493 for ( adt = depend; adt; adt = adt-> m_next ) {
Eric Andersen807bd842004-08-19 18:30:31 +0000494 if ( strcmp ( adt-> m_name, dt-> m_deparr [0] ) == 0 )
Robert Grieblaead70b2002-07-26 15:54:20 +0000495 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000496 }
Robert Griebl70112da2002-07-29 20:28:38 +0000497 if ( adt ) {
Eric Andersen716ccb22004-01-10 11:29:31 +0000498 dt = adt;
Eric Andersen807bd842004-08-19 18:30:31 +0000499 mod = dt-> m_name;
500 path = dt-> m_path;
Robert Griebl70112da2002-07-29 20:28:38 +0000501 if ( !opt )
502 opt = dt-> m_options;
503 }
Robert Grieblaead70b2002-07-26 15:54:20 +0000504 else
Robert Griebl70112da2002-07-29 20:28:38 +0000505 return;
Robert Grieblaead70b2002-07-26 15:54:20 +0000506 }
507 else
Eric Andersen716ccb22004-01-10 11:29:31 +0000508 return;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000509 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000510
Glenn L McGrathbdf63192004-09-24 09:18:55 +0000511 if ( !path ) {
512 bb_error_msg ("module %s not found.", mod);
513 return;
514 }
515
Robert Griebl52e8d062002-05-14 23:42:08 +0000516 // search for duplicates
517 for ( find = *head; find; find = find-> m_next ) {
Eric Andersen807bd842004-08-19 18:30:31 +0000518 if ( !strcmp ( mod, find-> m_name )) {
Robert Griebl52e8d062002-05-14 23:42:08 +0000519 // 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;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000525
Robert Griebl52e8d062002-05-14 23:42:08 +0000526 if ( find-> m_next )
527 find-> m_next-> m_prev = find-> m_prev;
528 else
529 *tail = find-> m_prev;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000530
Robert Griebl52e8d062002-05-14 23:42:08 +0000531 break; // there can be only one duplicate
Eric Andersen716ccb22004-01-10 11:29:31 +0000532 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000533 }
534
535 if ( !find ) { // did not find a duplicate
Eric Andersen716ccb22004-01-10 11:29:31 +0000536 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
Eric Andersen807bd842004-08-19 18:30:31 +0000537 find-> m_name = mod;
538 find-> m_path = path;
Robert Grieblaead70b2002-07-26 15:54:20 +0000539 find-> m_options = opt;
Robert Griebl52e8d062002-05-14 23:42:08 +0000540 }
541
Eric Andersen716ccb22004-01-10 11:29:31 +0000542 // enqueue at tail
Robert Griebl52e8d062002-05-14 23:42:08 +0000543 if ( *tail )
544 (*tail)-> m_next = find;
545 find-> m_prev = *tail;
546 find-> m_next = 0;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000547
Robert Griebl52e8d062002-05-14 23:42:08 +0000548 if ( !*head )
549 *head = find;
550 *tail = find;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000551
Eric Andersen716ccb22004-01-10 11:29:31 +0000552 if ( dt ) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000553 int i;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000554
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000555 for ( i = 0; i < dt-> m_depcnt; i++ )
556 check_dep ( dt-> m_deparr [i], head, tail );
557 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000558}
559
560
561
Robert Griebl236abbf2002-05-22 23:34:35 +0000562static int mod_insert ( char *mod, int argc, char **argv )
Robert Griebl52e8d062002-05-14 23:42:08 +0000563{
564 struct mod_list_t *tail = 0;
Eric Andersen716ccb22004-01-10 11:29:31 +0000565 struct mod_list_t *head = 0;
Eric Andersen908e3622003-06-20 09:56:37 +0000566 int rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000567
Robert Griebl52e8d062002-05-14 23:42:08 +0000568 // get dep list for module mod
569 check_dep ( mod, &head, &tail );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000570
Robert Griebl52e8d062002-05-14 23:42:08 +0000571 if ( head && tail ) {
Eric Andersene3a792c2004-08-16 10:22:34 +0000572#if defined(CONFIG_FEATURE_2_6_MODULES)
Robert Grieblaead70b2002-07-26 15:54:20 +0000573 if ( argc ) {
Eric Andersen716ccb22004-01-10 11:29:31 +0000574 int i;
Robert Grieblaead70b2002-07-26 15:54:20 +0000575 int l = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000576
Robert Grieblaead70b2002-07-26 15:54:20 +0000577 // append module args
Eric Andersen716ccb22004-01-10 11:29:31 +0000578 for ( i = 0; i < argc; i++ )
Manuel Novoa III cad53642003-03-19 09:13:01 +0000579 l += ( bb_strlen ( argv [i] ) + 1 );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000580
Robert Grieblaead70b2002-07-26 15:54:20 +0000581 head-> m_options = xrealloc ( head-> m_options, l + 1 );
582 head-> m_options [0] = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000583
Robert Grieblaead70b2002-07-26 15:54:20 +0000584 for ( i = 0; i < argc; i++ ) {
585 strcat ( head-> m_options, argv [i] );
586 strcat ( head-> m_options, " " );
587 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000588 }
Eric Andersene3a792c2004-08-16 10:22:34 +0000589#endif
Eric Andersen3b1a7442003-12-24 20:30:45 +0000590
Robert Griebl52e8d062002-05-14 23:42:08 +0000591 // process tail ---> head
Eric Andersen908e3622003-06-20 09:56:37 +0000592 rc = mod_process ( tail, 1 );
Robert Griebl52e8d062002-05-14 23:42:08 +0000593 }
594 else
595 rc = 1;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000596
Robert Griebl52e8d062002-05-14 23:42:08 +0000597 return rc;
598}
599
Eric Andersen908e3622003-06-20 09:56:37 +0000600static int mod_remove ( char *mod )
Robert Griebl52e8d062002-05-14 23:42:08 +0000601{
Eric Andersen908e3622003-06-20 09:56:37 +0000602 int rc;
Eric Andersen716ccb22004-01-10 11:29:31 +0000603 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
Eric Andersen3b1a7442003-12-24 20:30:45 +0000604
Robert Griebl52e8d062002-05-14 23:42:08 +0000605 struct mod_list_t *head = 0;
606 struct mod_list_t *tail = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000607
Robert Griebl52e8d062002-05-14 23:42:08 +0000608 if ( mod )
609 check_dep ( mod, &head, &tail );
610 else // autoclean
611 head = tail = &rm_a_dummy;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000612
Robert Griebl236abbf2002-05-22 23:34:35 +0000613 if ( head && tail )
Eric Andersen908e3622003-06-20 09:56:37 +0000614 rc = mod_process ( head, 0 ); // process head ---> tail
615 else
616 rc = 1;
617 return rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000618
Robert Griebl52e8d062002-05-14 23:42:08 +0000619}
620
Eric Andersen0139ca92001-07-22 23:01:03 +0000621extern int modprobe_main(int argc, char** argv)
622{
Robert Griebl236abbf2002-05-22 23:34:35 +0000623 int opt;
624 int remove_opt = 0;
Eric Andersen0139ca92001-07-22 23:01:03 +0000625
Robert Griebl236abbf2002-05-22 23:34:35 +0000626 autoclean = show_only = quiet = do_syslog = verbose = 0;
627
628 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
629 switch(opt) {
Eric Andersen3b1a7442003-12-24 20:30:45 +0000630 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");
Robert Griebl236abbf2002-05-22 23:34:35 +0000637
Eric Andersen3b1a7442003-12-24 20:30:45 +0000638 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;
Eric Andersen0139ca92001-07-22 23:01:03 +0000663 }
Eric Andersen0139ca92001-07-22 23:01:03 +0000664 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000665
Eric Andersen716ccb22004-01-10 11:29:31 +0000666 depend = build_dep ( );
Robert Griebl52e8d062002-05-14 23:42:08 +0000667
Eric Andersen716ccb22004-01-10 11:29:31 +0000668 if ( !depend )
Manuel Novoa III cad53642003-03-19 09:13:01 +0000669 bb_error_msg_and_die ( "could not parse modules.dep\n" );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000670
Eric Andersen1b064192001-07-25 07:23:38 +0000671 if (remove_opt) {
Eric Andersen908e3622003-06-20 09:56:37 +0000672 int rc = EXIT_SUCCESS;
Eric Andersen0139ca92001-07-22 23:01:03 +0000673 do {
Eric Andersen908e3622003-06-20 09:56:37 +0000674 if (mod_remove ( optind < argc ?
Eric Andersen3b1a7442003-12-24 20:30:45 +0000675 bb_xstrdup (argv [optind]) : NULL )) {
Eric Andersen908e3622003-06-20 09:56:37 +0000676 bb_error_msg ("failed to remove module %s",
Eric Andersen3b1a7442003-12-24 20:30:45 +0000677 argv [optind] );
Eric Andersen908e3622003-06-20 09:56:37 +0000678 rc = EXIT_FAILURE;
679 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000680 } while ( ++optind < argc );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000681
Eric Andersen908e3622003-06-20 09:56:37 +0000682 return rc;
Eric Andersen0139ca92001-07-22 23:01:03 +0000683 }
684
Eric Andersen716ccb22004-01-10 11:29:31 +0000685 if (optind >= argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000686 bb_error_msg_and_die ( "No module or pattern provided\n" );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000687
Eric Andersen716ccb22004-01-10 11:29:31 +0000688 if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000689 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000690
Robert Griebl3ba7cee2002-12-03 22:41:36 +0000691 return EXIT_SUCCESS;
Eric Andersen0139ca92001-07-22 23:01:03 +0000692}