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 |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 7 | * Copyright (c) 2005 by Jim Bauer, jfbauer@nfr.com |
Robert Griebl | 6859d76 | 2002-08-05 02:57:12 +0000 | [diff] [blame] | 8 | * |
| 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 Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 24 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 25 | #include <sys/utsname.h> |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #include <sys/wait.h> |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 28 | #include <getopt.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <unistd.h> |
| 31 | #include <syslog.h> |
| 32 | #include <string.h> |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 33 | #include <ctype.h> |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 34 | #include <fcntl.h> |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 35 | #include "busybox.h" |
| 36 | |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 37 | |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 38 | |
| 39 | struct dep_t { |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 40 | char * m_name; |
| 41 | char * m_path; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 42 | char * m_options; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 43 | |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 44 | int m_isalias : 1; |
| 45 | int m_reserved : 15; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 46 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 47 | int m_depcnt : 16; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 48 | char ** m_deparr; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 49 | |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 50 | struct dep_t * m_next; |
| 51 | }; |
| 52 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 53 | struct mod_list_t { |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 54 | char * m_name; |
| 55 | char * m_path; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 56 | char * m_options; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 57 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 58 | struct mod_list_t * m_prev; |
| 59 | struct mod_list_t * m_next; |
| 60 | }; |
| 61 | |
| 62 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 63 | static struct dep_t *depend; |
| 64 | static int autoclean, show_only, quiet, do_syslog, verbose; |
Eric Andersen | 03d8091 | 2003-12-19 21:04:19 +0000 | [diff] [blame] | 65 | static int k_version; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 66 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 67 | static int parse_tag_value ( char *buffer, char **ptag, char **pvalue ) |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 68 | { |
| 69 | char *tag, *value; |
| 70 | |
| 71 | while ( isspace ( *buffer )) |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 72 | buffer++; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 73 | tag = value = buffer; |
| 74 | while ( !isspace ( *value )) |
Eric Andersen | 7e496a7 | 2004-04-06 12:06:03 +0000 | [diff] [blame] | 75 | if (!*value) return 0; |
| 76 | else value++; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 77 | *value++ = 0; |
| 78 | while ( isspace ( *value )) |
| 79 | value++; |
Eric Andersen | 7e496a7 | 2004-04-06 12:06:03 +0000 | [diff] [blame] | 80 | if (!*value) return 0; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 81 | |
| 82 | *ptag = tag; |
| 83 | *pvalue = value; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 84 | |
Eric Andersen | 7e496a7 | 2004-04-06 12:06:03 +0000 | [diff] [blame] | 85 | return 1; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 86 | } |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 87 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 88 | /* 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 Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 90 | * thread and stdio junk can overflow the limited stack... |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 91 | */ |
| 92 | static char *reads ( int fd, char *buffer, size_t len ) |
| 93 | { |
| 94 | int n = read ( fd, buffer, len ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 95 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 96 | if ( n > 0 ) { |
| 97 | char *p; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 98 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 99 | buffer [len-1] = 0; |
| 100 | p = strchr ( buffer, '\n' ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 101 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 102 | if ( p ) { |
| 103 | off_t offset; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 104 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 105 | offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 106 | 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 Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 112 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 113 | else |
| 114 | return 0; |
| 115 | } |
| 116 | |
Eric Andersen | 864b797 | 2002-05-03 15:48:26 +0000 | [diff] [blame] | 117 | static struct dep_t *build_dep ( void ) |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 118 | { |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 119 | int fd; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 120 | struct utsname un; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 121 | struct dep_t *first = 0; |
| 122 | struct dep_t *current = 0; |
Eric Andersen | beaef16 | 2004-08-14 20:57:33 +0000 | [diff] [blame] | 123 | char buffer[2048]; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 124 | char *filename = buffer; |
| 125 | int continuation_line = 0; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 126 | |
Eric Andersen | 03d8091 | 2003-12-19 21:04:19 +0000 | [diff] [blame] | 127 | k_version = 0; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 128 | if ( uname ( &un )) |
| 129 | return 0; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 130 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 131 | // check for buffer overflow in following code |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 132 | if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) { |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 133 | return 0; |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 134 | } |
Eric Andersen | 03d8091 | 2003-12-19 21:04:19 +0000 | [diff] [blame] | 135 | if (un.release[0] == '2') { |
| 136 | k_version = un.release[2] - '0'; |
| 137 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 138 | |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 139 | strcpy ( filename, "/lib/modules/" ); |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 140 | strcat ( filename, un.release ); |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 141 | strcat ( filename, "/modules.dep" ); |
| 142 | |
Eric Andersen | 25ea42d | 2003-06-20 09:57:30 +0000 | [diff] [blame] | 143 | 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 Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 150 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 151 | while ( reads ( fd, buffer, sizeof( buffer ))) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 152 | int l = bb_strlen ( buffer ); |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 153 | char *p = 0; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 154 | |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 155 | while ( isspace ( buffer [l-1] )) { |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 156 | buffer [l-1] = 0; |
| 157 | l--; |
| 158 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 159 | |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 160 | if ( l == 0 ) { |
| 161 | continuation_line = 0; |
| 162 | continue; |
| 163 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 164 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 165 | if ( !continuation_line ) { |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 166 | char *col = strchr ( buffer, ':' ); |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 167 | char *dot = col; |
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 ( col ) { |
| 170 | char *mods; |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 171 | char *modpath; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 172 | char *mod; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 173 | |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 174 | *col = 0; |
| 175 | mods = strrchr ( buffer, '/' ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 176 | |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 177 | if ( !mods ) |
| 178 | mods = buffer; |
| 179 | else |
| 180 | mods++; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 181 | |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 182 | modpath = strchr ( buffer, '/' ); |
| 183 | if ( !modpath ) |
| 184 | modpath = buffer; |
Eric Andersen | 03d8091 | 2003-12-19 21:04:19 +0000 | [diff] [blame] | 185 | #if defined(CONFIG_FEATURE_2_6_MODULES) |
| 186 | if ((k_version > 4) && ( *(col-3) == '.' ) && |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 187 | ( *(col-2) == 'k' ) && ( *(col-1) == 'o' )) |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 188 | dot = col - 3; |
Eric Andersen | 03d8091 | 2003-12-19 21:04:19 +0000 | [diff] [blame] | 189 | else |
| 190 | #endif |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 191 | if (( *(col-2) == '.' ) && ( *(col-1) == 'o' )) |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 192 | dot = col - 2; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 193 | |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 194 | mod = bb_xstrndup ( mods, dot - mods ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 195 | |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 196 | if ( !current ) { |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 197 | first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t )); |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 198 | } |
| 199 | else { |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 200 | current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t )); |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 201 | current = current-> m_next; |
| 202 | } |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 203 | current-> m_name = mod; |
| 204 | current-> m_path = bb_xstrdup(modpath); |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 205 | current-> m_options = 0; |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 206 | current-> m_isalias = 0; |
| 207 | current-> m_depcnt = 0; |
| 208 | current-> m_deparr = 0; |
| 209 | current-> m_next = 0; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 210 | |
Eric Andersen | 864b797 | 2002-05-03 15:48:26 +0000 | [diff] [blame] | 211 | //printf ( "%s:\n", mod ); |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 212 | p = col + 1; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 213 | } |
| 214 | else |
| 215 | p = 0; |
| 216 | } |
| 217 | else |
| 218 | p = buffer; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 219 | |
Eric Andersen | 9d65ab2 | 2004-01-06 00:07:17 +0000 | [diff] [blame] | 220 | while ( p && *p && isblank(*p)) |
| 221 | p++; |
| 222 | |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 223 | if ( p && *p ) { |
| 224 | char *end = &buffer [l-1]; |
Eric Andersen | 9d65ab2 | 2004-01-06 00:07:17 +0000 | [diff] [blame] | 225 | char *deps; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 226 | char *dep; |
Eric Andersen | 9d65ab2 | 2004-01-06 00:07:17 +0000 | [diff] [blame] | 227 | char *next; |
Eric Andersen | 864b797 | 2002-05-03 15:48:26 +0000 | [diff] [blame] | 228 | int ext = 0; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 229 | |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 230 | while ( isblank ( *end ) || ( *end == '\\' )) |
| 231 | end--; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 232 | |
Eric Andersen | 9d65ab2 | 2004-01-06 00:07:17 +0000 | [diff] [blame] | 233 | do |
| 234 | { |
| 235 | next = strchr (p, ' ' ); |
| 236 | if (next) |
| 237 | { |
| 238 | *next = 0; |
| 239 | next--; |
| 240 | } |
| 241 | else |
| 242 | next = end; |
| 243 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 244 | deps = strrchr ( p, '/' ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 245 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 246 | if ( !deps || ( deps < p )) { |
| 247 | deps = p; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 248 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 249 | while ( isblank ( *deps )) |
| 250 | deps++; |
| 251 | } |
| 252 | else |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 253 | deps++; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 254 | |
Eric Andersen | 03d8091 | 2003-12-19 21:04:19 +0000 | [diff] [blame] | 255 | #if defined(CONFIG_FEATURE_2_6_MODULES) |
Eric Andersen | 9d65ab2 | 2004-01-06 00:07:17 +0000 | [diff] [blame] | 256 | if ((k_version > 4) && ( *(next-2) == '.' ) && *(next-1) == 'k' && |
| 257 | ( *next == 'o' )) |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 258 | ext = 3; |
| 259 | else |
Eric Andersen | 03d8091 | 2003-12-19 21:04:19 +0000 | [diff] [blame] | 260 | #endif |
Eric Andersen | 9d65ab2 | 2004-01-06 00:07:17 +0000 | [diff] [blame] | 261 | if (( *(next-1) == '.' ) && ( *next == 'o' )) |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 262 | ext = 2; |
Eric Andersen | b493dec | 2002-07-02 19:14:23 +0000 | [diff] [blame] | 263 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 264 | /* Cope with blank lines */ |
Eric Andersen | 9d65ab2 | 2004-01-06 00:07:17 +0000 | [diff] [blame] | 265 | if ((next-deps-ext+1) <= 0) |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 266 | continue; |
Eric Andersen | 9d65ab2 | 2004-01-06 00:07:17 +0000 | [diff] [blame] | 267 | dep = bb_xstrndup ( deps, next - deps - ext + 1 ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 268 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 269 | 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 Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 273 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 274 | //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] ); |
| 275 | p = next + 2; |
Eric Andersen | 9d65ab2 | 2004-01-06 00:07:17 +0000 | [diff] [blame] | 276 | } while (next < end); |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 277 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 278 | |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 279 | if ( buffer [l-1] == '\\' ) |
| 280 | continuation_line = 1; |
| 281 | else |
| 282 | continuation_line = 0; |
| 283 | } |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 284 | close ( fd ); |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 285 | |
| 286 | // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) ! |
| 287 | |
Robert Griebl | 36a836d | 2004-07-22 00:03:39 +0000 | [diff] [blame] | 288 | #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 Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 294 | |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 295 | continuation_line = 0; |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 296 | while ( reads ( fd, buffer, sizeof( buffer ))) { |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 297 | int l; |
| 298 | char *p; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 299 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 300 | p = strchr ( buffer, '#' ); |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 301 | if ( p ) |
| 302 | *p = 0; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 303 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 304 | l = bb_strlen ( buffer ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 305 | |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 306 | while ( l && isspace ( buffer [l-1] )) { |
| 307 | buffer [l-1] = 0; |
| 308 | l--; |
| 309 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 310 | |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 311 | if ( l == 0 ) { |
| 312 | continuation_line = 0; |
| 313 | continue; |
| 314 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 315 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 316 | if ( !continuation_line ) { |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 317 | if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) { |
| 318 | char *alias, *mod; |
| 319 | |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 320 | if ( parse_tag_value ( buffer + 6, &alias, &mod )) { |
| 321 | // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 322 | |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 323 | if ( !current ) { |
Eric Andersen | ba05e79 | 2004-04-06 12:04:14 +0000 | [diff] [blame] | 324 | first = current = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t )); |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 325 | } |
| 326 | else { |
Eric Andersen | ba05e79 | 2004-04-06 12:04:14 +0000 | [diff] [blame] | 327 | current-> m_next = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t )); |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 328 | current = current-> m_next; |
| 329 | } |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 330 | current-> m_name = bb_xstrdup ( alias ); |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 331 | current-> m_isalias = 1; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 332 | |
Eric Andersen | d298bd1 | 2004-04-06 12:05:04 +0000 | [diff] [blame] | 333 | if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) { |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 334 | 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 | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 340 | current-> m_deparr[0] = bb_xstrdup ( mod ); |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 341 | } |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 342 | current-> m_next = 0; |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 343 | } |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 344 | } |
| 345 | else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) { |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 346 | char *mod, *opt; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 347 | |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 348 | if ( parse_tag_value ( buffer + 8, &mod, &opt )) { |
| 349 | struct dep_t *dt; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 350 | |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 351 | for ( dt = first; dt; dt = dt-> m_next ) { |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 352 | if ( strcmp ( dt-> m_name, mod ) == 0 ) |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 353 | break; |
| 354 | } |
| 355 | if ( dt ) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 356 | dt-> m_options = xrealloc ( dt-> m_options, bb_strlen( opt ) + 1 ); |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 357 | strcpy ( dt-> m_options, opt ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 358 | |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 359 | // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_name, dt-> m_options ); |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | } |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 363 | } |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 364 | } |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 365 | close ( fd ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 366 | |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 367 | return first; |
| 368 | } |
| 369 | |
Glenn L McGrath | 350733a | 2003-09-08 00:32:49 +0000 | [diff] [blame] | 370 | /* return 1 = loaded, 0 = not loaded, -1 = can't tell */ |
| 371 | static int already_loaded (const char *name) |
| 372 | { |
| 373 | int fd; |
Eric Andersen | e3a792c | 2004-08-16 10:22:34 +0000 | [diff] [blame] | 374 | char buffer[4096]; |
Glenn L McGrath | 350733a | 2003-09-08 00:32:49 +0000 | [diff] [blame] | 375 | |
| 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 Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 386 | if (strcmp (name, buffer) == 0) { |
Glenn L McGrath | 350733a | 2003-09-08 00:32:49 +0000 | [diff] [blame] | 387 | close (fd); |
| 388 | return 1; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | close (fd); |
| 394 | return 0; |
| 395 | } |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 396 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 397 | static int mod_process ( struct mod_list_t *list, int do_insert ) |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 398 | { |
Eric Andersen | 44b5758 | 2004-08-03 08:23:33 +0000 | [diff] [blame] | 399 | int rc = 0; |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 400 | char *argv[10]; |
| 401 | int argc; |
Eric Andersen | 864b797 | 2002-05-03 15:48:26 +0000 | [diff] [blame] | 402 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 403 | while ( list ) { |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 404 | argc = 0; |
Glenn L McGrath | 350733a | 2003-09-08 00:32:49 +0000 | [diff] [blame] | 405 | if ( do_insert ) { |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 406 | 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 McGrath | 350733a | 2003-09-08 00:32:49 +0000 | [diff] [blame] | 418 | } else { |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 419 | /* modutils uses short name for removal */ |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 420 | 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 McGrath | 350733a | 2003-09-08 00:32:49 +0000 | [diff] [blame] | 426 | } |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 427 | argv[argc] = NULL; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 428 | |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 429 | if (argc) { |
Glenn L McGrath | fcf4732 | 2004-08-11 05:56:30 +0000 | [diff] [blame] | 430 | if (verbose) { |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 431 | int i; |
| 432 | for (i=0; i<argc; i++) |
| 433 | printf("%s ", argv[i]); |
| 434 | printf("\n"); |
Glenn L McGrath | fcf4732 | 2004-08-11 05:56:30 +0000 | [diff] [blame] | 435 | } |
| 436 | if (!show_only) { |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 437 | 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 McGrath | fcf4732 | 2004-08-11 05:56:30 +0000 | [diff] [blame] | 458 | 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 Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 465 | } |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 466 | list = do_insert ? list-> m_prev : list-> m_next; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 467 | } |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 468 | return (show_only) ? 0 : rc; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 471 | 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] | 472 | { |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 473 | struct mod_list_t *find; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 474 | struct dep_t *dt; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 475 | char *opt = 0; |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 476 | char *path = 0; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 477 | |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 478 | // check dependencies |
| 479 | for ( dt = depend; dt; dt = dt-> m_next ) { |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 480 | if ( strcmp ( dt-> m_name, mod ) == 0) { |
| 481 | mod = dt-> m_name; |
| 482 | path = dt-> m_path; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 483 | opt = dt-> m_options; |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 484 | break; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 485 | } |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 486 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 487 | |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 488 | // resolve alias names |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 489 | while ( dt && dt-> m_isalias ) { |
| 490 | if ( dt-> m_depcnt == 1 ) { |
| 491 | struct dep_t *adt; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 492 | |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 493 | for ( adt = depend; adt; adt = adt-> m_next ) { |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 494 | if ( strcmp ( adt-> m_name, dt-> m_deparr [0] ) == 0 ) |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 495 | break; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 496 | } |
Robert Griebl | 70112da | 2002-07-29 20:28:38 +0000 | [diff] [blame] | 497 | if ( adt ) { |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 498 | dt = adt; |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 499 | mod = dt-> m_name; |
| 500 | path = dt-> m_path; |
Robert Griebl | 70112da | 2002-07-29 20:28:38 +0000 | [diff] [blame] | 501 | if ( !opt ) |
| 502 | opt = dt-> m_options; |
| 503 | } |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 504 | else |
Robert Griebl | 70112da | 2002-07-29 20:28:38 +0000 | [diff] [blame] | 505 | return; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 506 | } |
| 507 | else |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 508 | return; |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 509 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 510 | |
Glenn L McGrath | bdf6319 | 2004-09-24 09:18:55 +0000 | [diff] [blame] | 511 | if ( !path ) { |
| 512 | bb_error_msg ("module %s not found.", mod); |
| 513 | return; |
| 514 | } |
| 515 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 516 | // search for duplicates |
| 517 | for ( find = *head; find; find = find-> m_next ) { |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 518 | if ( !strcmp ( mod, find-> m_name )) { |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 519 | // 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 Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 525 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 526 | if ( find-> m_next ) |
| 527 | find-> m_next-> m_prev = find-> m_prev; |
| 528 | else |
| 529 | *tail = find-> m_prev; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 530 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 531 | break; // there can be only one duplicate |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 532 | } |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | if ( !find ) { // did not find a duplicate |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 536 | find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t)); |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 537 | find-> m_name = mod; |
| 538 | find-> m_path = path; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 539 | find-> m_options = opt; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 542 | // enqueue at tail |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 543 | if ( *tail ) |
| 544 | (*tail)-> m_next = find; |
| 545 | find-> m_prev = *tail; |
| 546 | find-> m_next = 0; |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 547 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 548 | if ( !*head ) |
| 549 | *head = find; |
| 550 | *tail = find; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 551 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 552 | if ( dt ) { |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 553 | int i; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 554 | |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 555 | for ( i = 0; i < dt-> m_depcnt; i++ ) |
| 556 | check_dep ( dt-> m_deparr [i], head, tail ); |
| 557 | } |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | |
| 561 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 562 | static int mod_insert ( char *mod, int argc, char **argv ) |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 563 | { |
| 564 | struct mod_list_t *tail = 0; |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 565 | struct mod_list_t *head = 0; |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 566 | int rc; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 567 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 568 | // get dep list for module mod |
| 569 | check_dep ( mod, &head, &tail ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 570 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 571 | if ( head && tail ) { |
Eric Andersen | e3a792c | 2004-08-16 10:22:34 +0000 | [diff] [blame] | 572 | #if defined(CONFIG_FEATURE_2_6_MODULES) |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 573 | if ( argc ) { |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 574 | int i; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 575 | int l = 0; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 576 | |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 577 | // append module args |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 578 | for ( i = 0; i < argc; i++ ) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 579 | l += ( bb_strlen ( argv [i] ) + 1 ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 580 | |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 581 | head-> m_options = xrealloc ( head-> m_options, l + 1 ); |
| 582 | head-> m_options [0] = 0; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 583 | |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 584 | for ( i = 0; i < argc; i++ ) { |
| 585 | strcat ( head-> m_options, argv [i] ); |
| 586 | strcat ( head-> m_options, " " ); |
| 587 | } |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 588 | } |
Eric Andersen | e3a792c | 2004-08-16 10:22:34 +0000 | [diff] [blame] | 589 | #endif |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 590 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 591 | // process tail ---> head |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 592 | rc = mod_process ( tail, 1 ); |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 593 | } |
| 594 | else |
| 595 | rc = 1; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 596 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 597 | return rc; |
| 598 | } |
| 599 | |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 600 | static int mod_remove ( char *mod ) |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 601 | { |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 602 | int rc; |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 603 | static struct mod_list_t rm_a_dummy = { "-a", 0, 0 }; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 604 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 605 | struct mod_list_t *head = 0; |
| 606 | struct mod_list_t *tail = 0; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 607 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 608 | if ( mod ) |
| 609 | check_dep ( mod, &head, &tail ); |
| 610 | else // autoclean |
| 611 | head = tail = &rm_a_dummy; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 612 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 613 | if ( head && tail ) |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 614 | rc = mod_process ( head, 0 ); // process head ---> tail |
| 615 | else |
| 616 | rc = 1; |
| 617 | return rc; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 618 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 621 | extern int modprobe_main(int argc, char** argv) |
| 622 | { |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 623 | int opt; |
| 624 | int remove_opt = 0; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 625 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 626 | autoclean = show_only = quiet = do_syslog = verbose = 0; |
| 627 | |
| 628 | while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) { |
| 629 | switch(opt) { |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 630 | 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 Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 637 | |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 638 | 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 Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 663 | } |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 664 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 665 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 666 | depend = build_dep ( ); |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 667 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 668 | if ( !depend ) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 669 | bb_error_msg_and_die ( "could not parse modules.dep\n" ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 670 | |
Eric Andersen | 1b06419 | 2001-07-25 07:23:38 +0000 | [diff] [blame] | 671 | if (remove_opt) { |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 672 | int rc = EXIT_SUCCESS; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 673 | do { |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 674 | if (mod_remove ( optind < argc ? |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 675 | bb_xstrdup (argv [optind]) : NULL )) { |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 676 | bb_error_msg ("failed to remove module %s", |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 677 | argv [optind] ); |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 678 | rc = EXIT_FAILURE; |
| 679 | } |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 680 | } while ( ++optind < argc ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 681 | |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 682 | return rc; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 685 | if (optind >= argc) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 686 | bb_error_msg_and_die ( "No module or pattern provided\n" ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 687 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 688 | 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] | 689 | bb_error_msg_and_die ( "failed to load module %s", argv [optind] ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 690 | |
Robert Griebl | 3ba7cee | 2002-12-03 22:41:36 +0000 | [diff] [blame] | 691 | return EXIT_SUCCESS; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 692 | } |