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