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