blob: 36a3c35359b703eba3815eae7968bf55ee2faa3b [file] [log] [blame]
Eric Andersen0139ca92001-07-22 23:01:03 +00001/* vi: set sw=4 ts=4: */
2/*
Robert Grieblaead70b2002-07-26 15:54:20 +00003 * Modprobe written from scratch for BusyBox
Eric Andersen60e56f52002-04-26 06:04:01 +00004 *
Robert Griebl6859d762002-08-05 02:57:12 +00005 * Copyright (c) 2002 by Robert Griebl, griebl@gmx.de
Eric Andersen908e3622003-06-20 09:56:37 +00006 * Copyright (c) 2003 by Andrew Dennison, andrew.dennison@motec.com.au
Paul Fox8eeb6552005-08-04 18:33:36 +00007 * Copyright (c) 2005 by Jim Bauer, jfbauer@nfr.com
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +00008 *
Rob Landleye9190962005-12-12 19:38:44 +00009 * Portions Copyright (c) 2005 by Yann E. MORIN, yann.morin.1998@anciens.enib.fr
Robert Griebl6859d762002-08-05 02:57:12 +000010 *
Rob Landley79e1cab2005-11-15 00:08:29 +000011 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Robert Griebl6859d762002-08-05 02:57:12 +000012*/
Eric Andersen0139ca92001-07-22 23:01:03 +000013
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000014#include "libbb.h"
Robert Griebl52e8d062002-05-14 23:42:08 +000015#include <sys/utsname.h>
Bernhard Reutner-Fischer2c351a82006-06-03 19:08:49 +000016#include <fnmatch.h>
Eric Andersen0139ca92001-07-22 23:01:03 +000017
Rob Landleye9190962005-12-12 19:38:44 +000018struct mod_opt_t { /* one-way list of options to pass to a module */
19 char * m_opt_val;
20 struct mod_opt_t * m_next;
Eric Andersen60e56f52002-04-26 06:04:01 +000021};
22
Rob Landleye9190962005-12-12 19:38:44 +000023struct dep_t { /* one-way list of dependency rules */
24 /* a dependency rule */
Denis Vlasenkocf704332006-10-27 15:12:50 +000025 char * m_name; /* the module name*/
26 char * m_path; /* the module file path */
27 struct mod_opt_t * m_options; /* the module options */
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +000028
Denis Vlasenkocf704332006-10-27 15:12:50 +000029 int m_isalias : 1; /* the module is an alias */
30 int m_reserved : 15; /* stuffin' */
Rob Landleye9190962005-12-12 19:38:44 +000031
Denis Vlasenkocf704332006-10-27 15:12:50 +000032 int m_depcnt : 16; /* the number of dependable module(s) */
33 char ** m_deparr; /* the list of dependable module(s) */
Rob Landleye9190962005-12-12 19:38:44 +000034
Denis Vlasenkocf704332006-10-27 15:12:50 +000035 struct dep_t * m_next; /* the next dependency rule */
Rob Landleye9190962005-12-12 19:38:44 +000036};
37
38struct mod_list_t { /* two-way list of modules to process */
39 /* a module description */
Denis Vlasenko322661d2007-01-29 23:43:52 +000040 const char * m_name;
41 char * m_path;
42 struct mod_opt_t * m_options;
Eric Andersen3b1a7442003-12-24 20:30:45 +000043
Robert Griebl52e8d062002-05-14 23:42:08 +000044 struct mod_list_t * m_prev;
45 struct mod_list_t * m_next;
46};
47
48
Robert Griebl236abbf2002-05-22 23:34:35 +000049static struct dep_t *depend;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +000050
51#define main_options "acdklnqrst:vVC:"
52#define INSERT_ALL 1 /* a */
53#define DUMP_CONF_EXIT 2 /* c */
54#define D_OPT_IGNORED 4 /* d */
55#define AUTOCLEAN_FLG 8 /* k */
56#define LIST_ALL 16 /* l */
57#define SHOW_ONLY 32 /* n */
58#define QUIET 64 /* q */
59#define REMOVE_OPT 128 /* r */
60#define DO_SYSLOG 256 /* s */
61#define RESTRICT_DIR 512 /* t */
62#define VERBOSE 1024 /* v */
63#define VERSION_ONLY 2048 /* V */
64#define CONFIG_FILE 4096 /* C */
65
66#define autoclean (main_opts & AUTOCLEAN_FLG)
67#define show_only (main_opts & SHOW_ONLY)
68#define quiet (main_opts & QUIET)
69#define remove_opt (main_opts & REMOVE_OPT)
70#define do_syslog (main_opts & DO_SYSLOG)
71#define verbose (main_opts & VERBOSE)
72
73static int main_opts;
Robert Griebl52e8d062002-05-14 23:42:08 +000074
Denis Vlasenkocf704332006-10-27 15:12:50 +000075static int parse_tag_value(char *buffer, char **ptag, char **pvalue)
Robert Grieblaead70b2002-07-26 15:54:20 +000076{
77 char *tag, *value;
78
Denis Vlasenkocf704332006-10-27 15:12:50 +000079 buffer = skip_whitespace(buffer);
Robert Grieblaead70b2002-07-26 15:54:20 +000080 tag = value = buffer;
Denis Vlasenkocf704332006-10-27 15:12:50 +000081 while (!isspace(*value))
Eric Andersen7e496a72004-04-06 12:06:03 +000082 if (!*value) return 0;
83 else value++;
Robert Grieblaead70b2002-07-26 15:54:20 +000084 *value++ = 0;
Denis Vlasenkocf704332006-10-27 15:12:50 +000085 value = skip_whitespace(value);
Eric Andersen7e496a72004-04-06 12:06:03 +000086 if (!*value) return 0;
Robert Grieblaead70b2002-07-26 15:54:20 +000087
88 *ptag = tag;
89 *pvalue = value;
Eric Andersen3b1a7442003-12-24 20:30:45 +000090
Eric Andersen7e496a72004-04-06 12:06:03 +000091 return 1;
Robert Grieblaead70b2002-07-26 15:54:20 +000092}
Eric Andersen60e56f52002-04-26 06:04:01 +000093
Rob Landleye9190962005-12-12 19:38:44 +000094/*
95 * This function appends an option to a list
96 */
Denis Vlasenkocf704332006-10-27 15:12:50 +000097static struct mod_opt_t *append_option(struct mod_opt_t *opt_list, char *opt)
Eric Andersen60e56f52002-04-26 06:04:01 +000098{
Rob Landleye9190962005-12-12 19:38:44 +000099 struct mod_opt_t *ol = opt_list;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000100
Denis Vlasenkocf704332006-10-27 15:12:50 +0000101 if (ol) {
102 while (ol->m_next) {
103 ol = ol->m_next;
Rob Landleye9190962005-12-12 19:38:44 +0000104 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000105 ol->m_next = xmalloc(sizeof(struct mod_opt_t));
106 ol = ol->m_next;
Rob Landleye9190962005-12-12 19:38:44 +0000107 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000108 ol = opt_list = xmalloc(sizeof(struct mod_opt_t));
Eric Andersen03d80912003-12-19 21:04:19 +0000109 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000110
Denis Vlasenkocf704332006-10-27 15:12:50 +0000111 ol->m_opt_val = xstrdup(opt);
112 ol->m_next = NULL;
Eric Andersen60e56f52002-04-26 06:04:01 +0000113
Rob Landleye9190962005-12-12 19:38:44 +0000114 return opt_list;
Eric Andersen60e56f52002-04-26 06:04:01 +0000115}
116
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000117#if ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS
Denis Vlasenkocf704332006-10-27 15:12:50 +0000118/* static char* parse_command_string(char* src, char **dst);
Rob Landley79e1cab2005-11-15 00:08:29 +0000119 * src: pointer to string containing argument
120 * dst: pointer to where to store the parsed argument
121 * return value: the pointer to the first char after the parsed argument,
122 * NULL if there was no argument parsed (only trailing spaces).
Rob Landleyd921b2e2006-08-03 15:41:12 +0000123 * Note that memory is allocated with xstrdup when a new argument was
Rob Landley79e1cab2005-11-15 00:08:29 +0000124 * parsed. Don't forget to free it!
125 */
126#define ARG_EMPTY 0x00
127#define ARG_IN_DQUOTES 0x01
128#define ARG_IN_SQUOTES 0x02
Denis Vlasenkocf704332006-10-27 15:12:50 +0000129static char *parse_command_string(char *src, char **dst)
Rob Landley79e1cab2005-11-15 00:08:29 +0000130{
131 int opt_status = ARG_EMPTY;
132 char* tmp_str;
133
134 /* Dumb you, I have nothing to do... */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000135 if (src == NULL) return src;
Rob Landley79e1cab2005-11-15 00:08:29 +0000136
137 /* Skip leading spaces */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000138 while (*src == ' ') {
Rob Landley79e1cab2005-11-15 00:08:29 +0000139 src++;
140 }
141 /* Is the end of string reached? */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000142 if (*src == '\0') {
Rob Landley79e1cab2005-11-15 00:08:29 +0000143 return NULL;
144 }
145 /* Reached the start of an argument
Rob Landleye9190962005-12-12 19:38:44 +0000146 * By the way, we duplicate a little too much
147 * here but what is too much is freed later. */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000148 *dst = tmp_str = xstrdup(src);
Rob Landley79e1cab2005-11-15 00:08:29 +0000149 /* Get to the end of that argument */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000150 while (*tmp_str != '\0'
151 && (*tmp_str != ' ' || (opt_status & (ARG_IN_DQUOTES | ARG_IN_SQUOTES)))
152 ) {
153 switch (*tmp_str) {
154 case '\'':
155 if (opt_status & ARG_IN_DQUOTES) {
156 /* Already in double quotes, keep current char as is */
157 } else {
158 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
159 memmove(tmp_str, tmp_str + 1, strlen(tmp_str));
160 /* mark me: we enter or leave single quotes */
161 opt_status ^= ARG_IN_SQUOTES;
162 /* Back one char, as we need to re-scan the new char there. */
163 tmp_str--;
164 }
Rob Landley79e1cab2005-11-15 00:08:29 +0000165 break;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000166 case '"':
167 if (opt_status & ARG_IN_SQUOTES) {
168 /* Already in single quotes, keep current char as is */
169 } else {
170 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
171 memmove(tmp_str, tmp_str + 1, strlen(tmp_str));
172 /* mark me: we enter or leave double quotes */
173 opt_status ^= ARG_IN_DQUOTES;
174 /* Back one char, as we need to re-scan the new char there. */
175 tmp_str--;
176 }
Rob Landley79e1cab2005-11-15 00:08:29 +0000177 break;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000178 case '\\':
179 if (opt_status & ARG_IN_SQUOTES) {
180 /* Between single quotes: keep as is. */
181 } else {
182 switch (*(tmp_str+1)) {
183 case 'a':
184 case 'b':
185 case 't':
186 case 'n':
187 case 'v':
188 case 'f':
189 case 'r':
190 case '0':
191 /* We escaped a special character. For now, keep
192 * both the back-slash and the following char. */
193 tmp_str++; src++;
194 break;
195 default:
196 /* We escaped a space or a single or double quote,
197 * or a back-slash, or a non-escapable char. Remove
198 * the '\' and keep the new current char as is. */
199 memmove(tmp_str, tmp_str + 1, strlen(tmp_str));
200 break;
Rob Landley79e1cab2005-11-15 00:08:29 +0000201 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000202 }
Rob Landley79e1cab2005-11-15 00:08:29 +0000203 break;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000204 /* Any other char that is special shall appear here.
205 * Example: $ starts a variable
206 case '$':
207 do_variable_expansion();
208 break;
209 * */
210 default:
211 /* any other char is kept as is. */
212 break;
Rob Landley79e1cab2005-11-15 00:08:29 +0000213 }
214 tmp_str++; /* Go to next char */
215 src++; /* Go to next char to find the end of the argument. */
216 }
217 /* End of string, but still no ending quote */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000218 if (opt_status & (ARG_IN_DQUOTES | ARG_IN_SQUOTES)) {
219 bb_error_msg_and_die("unterminated (single or double) quote in options list: %s", src);
Rob Landley79e1cab2005-11-15 00:08:29 +0000220 }
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000221 *tmp_str++ = '\0';
Denis Vlasenkocf704332006-10-27 15:12:50 +0000222 *dst = xrealloc(*dst, (tmp_str - *dst));
Rob Landley79e1cab2005-11-15 00:08:29 +0000223 return src;
224}
Rob Landleyae50c6d2005-12-15 07:42:13 +0000225#else
226#define parse_command_string(src, dst) (0)
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000227#endif /* ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
Rob Landley79e1cab2005-11-15 00:08:29 +0000228
Rob Landleye9190962005-12-12 19:38:44 +0000229/*
Rob Landley72615752006-04-10 16:09:52 +0000230 * This function reads aliases and default module options from a configuration file
231 * (/etc/modprobe.conf syntax). It supports includes (only files, no directories).
232 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000233static void include_conf(struct dep_t **first, struct dep_t **current, char *buffer, int buflen, int fd)
Rob Landley72615752006-04-10 16:09:52 +0000234{
235 int continuation_line = 0;
236
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000237 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias)!
Rob Landley72615752006-04-10 16:09:52 +0000238
Denis Vlasenkocf704332006-10-27 15:12:50 +0000239 while (reads(fd, buffer, buflen)) {
Rob Landley72615752006-04-10 16:09:52 +0000240 int l;
241 char *p;
242
Denis Vlasenkocf704332006-10-27 15:12:50 +0000243 p = strchr(buffer, '#');
244 if (p)
Rob Landley72615752006-04-10 16:09:52 +0000245 *p = 0;
246
Denis Vlasenkocf704332006-10-27 15:12:50 +0000247 l = strlen(buffer);
Rob Landley72615752006-04-10 16:09:52 +0000248
Denis Vlasenkocf704332006-10-27 15:12:50 +0000249 while (l && isspace(buffer[l-1])) {
250 buffer[l-1] = 0;
Rob Landley72615752006-04-10 16:09:52 +0000251 l--;
252 }
253
Denis Vlasenkocf704332006-10-27 15:12:50 +0000254 if (l == 0) {
Rob Landley72615752006-04-10 16:09:52 +0000255 continuation_line = 0;
256 continue;
257 }
258
Denis Vlasenkocf704332006-10-27 15:12:50 +0000259 if (!continuation_line) {
260 if ((strncmp(buffer, "alias", 5) == 0) && isspace(buffer[5])) {
Rob Landley72615752006-04-10 16:09:52 +0000261 char *alias, *mod;
262
Denis Vlasenkocf704332006-10-27 15:12:50 +0000263 if (parse_tag_value(buffer + 6, &alias, &mod)) {
Rob Landley72615752006-04-10 16:09:52 +0000264 /* handle alias as a module dependent on the aliased module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000265 if (!*current) {
266 (*first) = (*current) = xzalloc(sizeof(struct dep_t));
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000267 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000268 (*current)->m_next = xzalloc(sizeof(struct dep_t));
269 (*current) = (*current)->m_next;
Rob Landley72615752006-04-10 16:09:52 +0000270 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000271 (*current)->m_name = xstrdup(alias);
272 (*current)->m_isalias = 1;
Rob Landley72615752006-04-10 16:09:52 +0000273
Denis Vlasenkocf704332006-10-27 15:12:50 +0000274 if ((strcmp(mod, "off") == 0) || (strcmp(mod, "null") == 0)) {
275 (*current)->m_depcnt = 0;
276 (*current)->m_deparr = 0;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000277 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000278 (*current)->m_depcnt = 1;
279 (*current)->m_deparr = xmalloc(1 * sizeof(char *));
280 (*current)->m_deparr[0] = xstrdup(mod);
Rob Landley72615752006-04-10 16:09:52 +0000281 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000282 (*current)->m_next = 0;
Rob Landley72615752006-04-10 16:09:52 +0000283 }
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000284 } else if ((strncmp(buffer, "options", 7) == 0) && isspace(buffer[7])) {
Rob Landley72615752006-04-10 16:09:52 +0000285 char *mod, *opt;
286
287 /* split the line in the module/alias name, and options */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000288 if (parse_tag_value(buffer + 8, &mod, &opt)) {
Rob Landley72615752006-04-10 16:09:52 +0000289 struct dep_t *dt;
290
291 /* find the corresponding module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000292 for (dt = *first; dt; dt = dt->m_next) {
293 if (strcmp(dt->m_name, mod) == 0)
Rob Landley72615752006-04-10 16:09:52 +0000294 break;
295 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000296 if (dt) {
297 if (ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS) {
Rob Landley72615752006-04-10 16:09:52 +0000298 char* new_opt = NULL;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000299 while ((opt = parse_command_string(opt, &new_opt))) {
300 dt->m_options = append_option(dt->m_options, new_opt);
Rob Landley72615752006-04-10 16:09:52 +0000301 }
302 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000303 dt->m_options = append_option(dt->m_options, opt);
Rob Landley72615752006-04-10 16:09:52 +0000304 }
305 }
306 }
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000307 } else if ((strncmp(buffer, "include", 7) == 0) && isspace(buffer[7])) {
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000308 int fdi; char *filename;
Rob Landley72615752006-04-10 16:09:52 +0000309
Denis Vlasenkocf704332006-10-27 15:12:50 +0000310 filename = skip_whitespace(buffer + 8);
Rob Landley72615752006-04-10 16:09:52 +0000311
Denis Vlasenkocf704332006-10-27 15:12:50 +0000312 if ((fdi = open(filename, O_RDONLY)) >= 0) {
Rob Landley72615752006-04-10 16:09:52 +0000313 include_conf(first, current, buffer, buflen, fdi);
314 close(fdi);
315 }
316 }
317 }
318 }
319}
320
321/*
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000322 * This function builds a list of dependency rules from /lib/modules/`uname -r`/modules.dep.
Bernhard Reutner-Fischere3c150b2006-05-19 11:24:28 +0000323 * It then fills every modules and aliases with their default options, found by parsing
Rob Landleye9190962005-12-12 19:38:44 +0000324 * modprobe.conf (or modules.conf, or conf.modules).
325 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000326static struct dep_t *build_dep(void)
Rob Landleye9190962005-12-12 19:38:44 +0000327{
328 int fd;
329 struct utsname un;
330 struct dep_t *first = 0;
331 struct dep_t *current = 0;
332 char buffer[2048];
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000333 char *filename;
Rob Landleye9190962005-12-12 19:38:44 +0000334 int continuation_line = 0;
335 int k_version;
336
Denis Vlasenkocf704332006-10-27 15:12:50 +0000337 if (uname(&un))
Rob Landleye9190962005-12-12 19:38:44 +0000338 bb_error_msg_and_die("can't determine kernel version");
339
Denis Vlasenkocf704332006-10-27 15:12:50 +0000340 k_version = 0;
Rob Landleye9190962005-12-12 19:38:44 +0000341 if (un.release[0] == '2') {
342 k_version = un.release[2] - '0';
343 }
344
Denis Vlasenkocf704332006-10-27 15:12:50 +0000345 filename = xasprintf("/lib/modules/%s/modules.dep", un.release);
346 fd = open(filename, O_RDONLY);
Bernhard Reutner-Fischere3c150b2006-05-19 11:24:28 +0000347 if (ENABLE_FEATURE_CLEAN_UP)
348 free(filename);
Rob Landley3afb0702006-05-18 20:41:43 +0000349 if (fd < 0) {
Rob Landleye9190962005-12-12 19:38:44 +0000350 /* Ok, that didn't work. Fall back to looking in /lib/modules */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000351 fd = open("/lib/modules/modules.dep", O_RDONLY);
352 if (fd < 0) {
Rob Landleye9190962005-12-12 19:38:44 +0000353 return 0;
354 }
355 }
356
Denis Vlasenkocf704332006-10-27 15:12:50 +0000357 while (reads(fd, buffer, sizeof(buffer))) {
358 int l = strlen(buffer);
Rob Landleye9190962005-12-12 19:38:44 +0000359 char *p = 0;
360
Denis Vlasenkocf704332006-10-27 15:12:50 +0000361 while (l > 0 && isspace(buffer[l-1])) {
362 buffer[l-1] = 0;
Rob Landleye9190962005-12-12 19:38:44 +0000363 l--;
364 }
365
Denis Vlasenkocf704332006-10-27 15:12:50 +0000366 if (l == 0) {
Rob Landleye9190962005-12-12 19:38:44 +0000367 continuation_line = 0;
368 continue;
369 }
370
371 /* Is this a new module dep description? */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000372 if (!continuation_line) {
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000373 /* find the dep beginning */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000374 char *col = strchr(buffer, ':');
Rob Landleye9190962005-12-12 19:38:44 +0000375 char *dot = col;
376
Denis Vlasenkocf704332006-10-27 15:12:50 +0000377 if (col) {
Rob Landleye9190962005-12-12 19:38:44 +0000378 /* This line is a dep description */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000379 const char *mods;
Rob Landleye9190962005-12-12 19:38:44 +0000380 char *modpath;
381 char *mod;
382
383 /* Find the beginning of the module file name */
384 *col = 0;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000385 mods = bb_basename(buffer);
Rob Landleye9190962005-12-12 19:38:44 +0000386
387 /* find the path of the module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000388 modpath = strchr(buffer, '/'); /* ... and this is the path */
389 if (!modpath)
Rob Landleye9190962005-12-12 19:38:44 +0000390 modpath = buffer; /* module with no path */
391 /* find the end of the module name in the file name */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000392 if (ENABLE_FEATURE_2_6_MODULES &&
393 (k_version > 4) && (*(col-3) == '.') &&
394 (*(col-2) == 'k') && (*(col-1) == 'o'))
Rob Landleye9190962005-12-12 19:38:44 +0000395 dot = col - 3;
396 else
Denis Vlasenkocf704332006-10-27 15:12:50 +0000397 if ((*(col-2) == '.') && (*(col-1) == 'o'))
Rob Landleye9190962005-12-12 19:38:44 +0000398 dot = col - 2;
399
Denis Vlasenkocf704332006-10-27 15:12:50 +0000400 mod = xstrndup(mods, dot - mods);
Rob Landleye9190962005-12-12 19:38:44 +0000401
402 /* enqueue new module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000403 if (!current) {
404 first = current = xmalloc(sizeof(struct dep_t));
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000405 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000406 current->m_next = xmalloc(sizeof(struct dep_t));
407 current = current->m_next;
Rob Landleye9190962005-12-12 19:38:44 +0000408 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000409 current->m_name = mod;
410 current->m_path = xstrdup(modpath);
411 current->m_options = NULL;
412 current->m_isalias = 0;
413 current->m_depcnt = 0;
414 current->m_deparr = 0;
415 current->m_next = 0;
Rob Landleye9190962005-12-12 19:38:44 +0000416
417 p = col + 1;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000418 } else
Rob Landleye9190962005-12-12 19:38:44 +0000419 /* this line is not a dep description */
420 p = 0;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000421 } else
Rob Landleye9190962005-12-12 19:38:44 +0000422 /* It's a dep description continuation */
423 p = buffer;
424
Denis Vlasenkocf704332006-10-27 15:12:50 +0000425 while (p && *p && isblank(*p))
Rob Landleye9190962005-12-12 19:38:44 +0000426 p++;
427
428 /* p points to the first dependable module; if NULL, no dependable module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000429 if (p && *p) {
430 char *end = &buffer[l-1];
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000431 const char *deps;
Rob Landleye9190962005-12-12 19:38:44 +0000432 char *dep;
433 char *next;
434 int ext = 0;
435
Denis Vlasenkocf704332006-10-27 15:12:50 +0000436 while (isblank(*end) || (*end == '\\'))
Rob Landleye9190962005-12-12 19:38:44 +0000437 end--;
438
Denis Vlasenkocf704332006-10-27 15:12:50 +0000439 do {
Rob Landleye9190962005-12-12 19:38:44 +0000440 /* search the end of the dependency */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000441 next = strchr(p, ' ');
442 if (next) {
Rob Landleye9190962005-12-12 19:38:44 +0000443 *next = 0;
444 next--;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000445 } else
Rob Landleye9190962005-12-12 19:38:44 +0000446 next = end;
447
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000448 /* find the beginning of the module file name */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000449 deps = bb_basename(p);
450 if (deps == p) {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000451 while (isblank(*deps))
Rob Landleye9190962005-12-12 19:38:44 +0000452 deps++;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000453 }
Rob Landleye9190962005-12-12 19:38:44 +0000454
455 /* find the end of the module name in the file name */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000456 if (ENABLE_FEATURE_2_6_MODULES
457 && (k_version > 4) && (*(next-2) == '.')
458 && (*(next-1) == 'k') && (*next == 'o'))
Rob Landleye9190962005-12-12 19:38:44 +0000459 ext = 3;
460 else
Denis Vlasenkocf704332006-10-27 15:12:50 +0000461 if ((*(next-1) == '.') && (*next == 'o'))
Rob Landleye9190962005-12-12 19:38:44 +0000462 ext = 2;
463
464 /* Cope with blank lines */
465 if ((next-deps-ext+1) <= 0)
466 continue;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000467 dep = xstrndup(deps, next - deps - ext + 1);
Rob Landleye9190962005-12-12 19:38:44 +0000468
469 /* Add the new dependable module name */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000470 current->m_depcnt++;
471 current->m_deparr = xrealloc(current->m_deparr,
472 sizeof(char *) * current->m_depcnt);
473 current->m_deparr[current->m_depcnt - 1] = dep;
Rob Landleye9190962005-12-12 19:38:44 +0000474
475 p = next + 2;
476 } while (next < end);
477 }
478
479 /* is there other dependable module(s) ? */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000480 if (buffer[l-1] == '\\')
Rob Landleye9190962005-12-12 19:38:44 +0000481 continuation_line = 1;
482 else
483 continuation_line = 0;
484 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000485 close(fd);
Rob Landleye9190962005-12-12 19:38:44 +0000486
Rob Landley3b0cfb42006-07-19 21:33:42 +0000487 /*
488 * First parse system-specific options and aliases
489 * as they take precedence over the kernel ones.
Mike Frysinger4423e5b2007-02-08 07:03:44 +0000490 * >=2.6: we only care about modprobe.conf
491 * <=2.4: we care about modules.conf and conf.modules
Rob Landley3b0cfb42006-07-19 21:33:42 +0000492 */
Mike Frysinger4423e5b2007-02-08 07:03:44 +0000493 if (ENABLE_FEATURE_2_6_MODULES
494 && (fd = open("/etc/modprobe.conf", O_RDONLY)) < 0)
495 if (ENABLE_FEATURE_2_4_MODULES
496 && (fd = open("/etc/modules.conf", O_RDONLY)) < 0)
497 if (ENABLE_FEATURE_2_4_MODULES)
498 fd = open("/etc/conf.modules", O_RDONLY);
Rob Landleye9190962005-12-12 19:38:44 +0000499
Rob Landley3b0cfb42006-07-19 21:33:42 +0000500 if (fd >= 0) {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000501 include_conf(&first, &current, buffer, sizeof(buffer), fd);
Rob Landley3b0cfb42006-07-19 21:33:42 +0000502 close(fd);
Rob Landleye9190962005-12-12 19:38:44 +0000503 }
Rob Landley72615752006-04-10 16:09:52 +0000504
Rob Landley3b0cfb42006-07-19 21:33:42 +0000505 /* Only 2.6 has a modules.alias file */
506 if (ENABLE_FEATURE_2_6_MODULES) {
Denis Vlasenkof8483052007-08-16 10:40:06 +0000507 /* Parse kernel-declared module aliases */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000508 filename = xasprintf("/lib/modules/%s/modules.alias", un.release);
Denis Vlasenkocf704332006-10-27 15:12:50 +0000509 fd = open(filename, O_RDONLY);
510 if (fd < 0) {
Rob Landley3b0cfb42006-07-19 21:33:42 +0000511 /* Ok, that didn't work. Fall back to looking in /lib/modules */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000512 fd = open("/lib/modules/modules.alias", O_RDONLY);
Rob Landley3b0cfb42006-07-19 21:33:42 +0000513 }
514 if (ENABLE_FEATURE_CLEAN_UP)
515 free(filename);
516
517 if (fd >= 0) {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000518 include_conf(&first, &current, buffer, sizeof(buffer), fd);
Rob Landley3b0cfb42006-07-19 21:33:42 +0000519 close(fd);
520 }
Denis Vlasenkof8483052007-08-16 10:40:06 +0000521
522 /* Parse kernel-declared symbol aliases */
523 filename = xasprintf("/lib/modules/%s/modules.symbols", un.release);
524 fd = open(filename, O_RDONLY);
525 if (fd < 0) {
526 /* Ok, that didn't work. Fall back to looking in /lib/modules */
527 fd = open("/lib/modules/modules.symbols", O_RDONLY);
528 }
529 if (ENABLE_FEATURE_CLEAN_UP)
530 free(filename);
531
532 if (fd >= 0) {
533 include_conf(&first, &current, buffer, sizeof(buffer), fd);
534 close(fd);
535 }
Rob Landley3b0cfb42006-07-19 21:33:42 +0000536 }
Rob Landleye9190962005-12-12 19:38:44 +0000537
538 return first;
539}
540
541/* return 1 = loaded, 0 = not loaded, -1 = can't tell */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000542static int already_loaded(const char *name)
Rob Landleye9190962005-12-12 19:38:44 +0000543{
Rob Landleyd7605602006-06-14 01:51:16 +0000544 int fd, ret = 0;
Rob Landleye9190962005-12-12 19:38:44 +0000545 char buffer[4096];
546
Denis Vlasenkocf704332006-10-27 15:12:50 +0000547 fd = open("/proc/modules", O_RDONLY);
Rob Landleye9190962005-12-12 19:38:44 +0000548 if (fd < 0)
549 return -1;
550
Denis Vlasenkocf704332006-10-27 15:12:50 +0000551 while (reads(fd, buffer, sizeof(buffer))) {
Rob Landleye9190962005-12-12 19:38:44 +0000552 char *p;
553
554 p = strchr (buffer, ' ');
555 if (p) {
Rob Landleyd7605602006-06-14 01:51:16 +0000556 const char *n;
557
558 // Truncate buffer at first space and check for matches, with
559 // the idiosyncrasy that _ and - are interchangeable because the
560 // 2.6 kernel does weird things.
561
Rob Landleye9190962005-12-12 19:38:44 +0000562 *p = 0;
Rob Landleyd7605602006-06-14 01:51:16 +0000563 for (p = buffer, n = name; ; p++, n++) {
564 if (*p != *n) {
565 if ((*p == '_' || *p == '-') && (*n == '_' || *n == '-'))
566 continue;
567 break;
568 }
569 // If we made it to the end, that's a match.
570 if (!*p) {
571 ret = 1;
572 goto done;
573 }
Rob Landleye9190962005-12-12 19:38:44 +0000574 }
575 }
576 }
Rob Landleyd7605602006-06-14 01:51:16 +0000577done:
Rob Landleye9190962005-12-12 19:38:44 +0000578 close (fd);
Mike Frysinger135cee32006-06-21 23:03:37 +0000579 return ret;
Rob Landleye9190962005-12-12 19:38:44 +0000580}
581
Denis Vlasenko322661d2007-01-29 23:43:52 +0000582static int mod_process(const struct mod_list_t *list, int do_insert)
Eric Andersen60e56f52002-04-26 06:04:01 +0000583{
Eric Andersen44b57582004-08-03 08:23:33 +0000584 int rc = 0;
Rob Landley79e1cab2005-11-15 00:08:29 +0000585 char **argv = NULL;
Rob Landleye9190962005-12-12 19:38:44 +0000586 struct mod_opt_t *opts;
587 int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */
Paul Fox8eeb6552005-08-04 18:33:36 +0000588 int argc;
Eric Andersen864b7972002-05-03 15:48:26 +0000589
Denis Vlasenkocf704332006-10-27 15:12:50 +0000590 while (list) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000591 argc = 0;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000592 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleye9190962005-12-12 19:38:44 +0000593 argc_malloc = 0;
594 /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory
595 * each time we allocate memory for argv.
596 * But it is (quite) small amounts of memory that leak each
597 * time a module is loaded, and it is reclaimed when modprobe
598 * exits anyway (even when standalone shell?).
599 * This could become a problem when loading a module with LOTS of
600 * dependencies, with LOTS of options for each dependencies, with
601 * very little memory on the target... But in that case, the module
602 * would not load because there is no more memory, so there's no
603 * problem. */
604 /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000605 argv = xmalloc(6 * sizeof(char*));
606 if (do_insert) {
607 if (already_loaded(list->m_name) != 1) {
Denis Vlasenko322661d2007-01-29 23:43:52 +0000608 argv[argc++] = (char*)"insmod";
Rob Landleyd7605602006-06-14 01:51:16 +0000609 if (ENABLE_FEATURE_2_4_MODULES) {
610 if (do_syslog)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000611 argv[argc++] = (char*)"-s";
Rob Landleyd7605602006-06-14 01:51:16 +0000612 if (autoclean)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000613 argv[argc++] = (char*)"-k";
Rob Landleyd7605602006-06-14 01:51:16 +0000614 if (quiet)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000615 argv[argc++] = (char*)"-q";
Denis Vlasenkocf704332006-10-27 15:12:50 +0000616 else if (verbose) /* verbose and quiet are mutually exclusive */
Denis Vlasenko322661d2007-01-29 23:43:52 +0000617 argv[argc++] = (char*)"-v";
Rob Landleyd7605602006-06-14 01:51:16 +0000618 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000619 argv[argc++] = list->m_path;
620 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleye9190962005-12-12 19:38:44 +0000621 argc_malloc = argc;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000622 opts = list->m_options;
623 while (opts) {
Rob Landleye9190962005-12-12 19:38:44 +0000624 /* Add one more option */
Rob Landley79e1cab2005-11-15 00:08:29 +0000625 argc++;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000626 argv = xrealloc(argv,(argc + 1)* sizeof(char*));
627 argv[argc-1] = opts->m_opt_val;
628 opts = opts->m_next;
Rob Landley79e1cab2005-11-15 00:08:29 +0000629 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000630 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000631 } else {
Eric Andersen807bd842004-08-19 18:30:31 +0000632 /* modutils uses short name for removal */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000633 if (already_loaded(list->m_name) != 0) {
Denis Vlasenko322661d2007-01-29 23:43:52 +0000634 argv[argc++] = (char*)"rmmod";
Paul Fox8eeb6552005-08-04 18:33:36 +0000635 if (do_syslog)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000636 argv[argc++] = (char*)"-s";
637 argv[argc++] = (char*)list->m_name;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000638 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleye9190962005-12-12 19:38:44 +0000639 argc_malloc = argc;
Paul Fox8eeb6552005-08-04 18:33:36 +0000640 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000641 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000642 argv[argc] = NULL;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000643
Paul Fox8eeb6552005-08-04 18:33:36 +0000644 if (argc) {
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000645 if (verbose) {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000646 printf("%s module %s\n", do_insert?"Loading":"Unloading", list->m_name);
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000647 }
648 if (!show_only) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000649 int rc2 = wait4pid(spawn(argv));
Rob Landley4b5827a2006-08-22 23:50:11 +0000650
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000651 if (do_insert) {
652 rc = rc2; /* only last module matters */
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000653 } else if (!rc2) {
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000654 rc = 0; /* success if remove any mod */
655 }
656 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000657 if (ENABLE_FEATURE_CLEAN_UP) {
Rob Landleye9190962005-12-12 19:38:44 +0000658 /* the last value in the array has index == argc, but
659 * it is the terminating NULL, so we must not free it. */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000660 while (argc_malloc < argc) {
661 free(argv[argc_malloc++]);
662 }
Rob Landley79e1cab2005-11-15 00:08:29 +0000663 }
Eric Andersen908e3622003-06-20 09:56:37 +0000664 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000665 if (ENABLE_FEATURE_CLEAN_UP) {
666 free(argv);
Rob Landleye9190962005-12-12 19:38:44 +0000667 argv = NULL;
Rob Landley79e1cab2005-11-15 00:08:29 +0000668 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000669 list = do_insert ? list->m_prev : list->m_next;
Eric Andersen60e56f52002-04-26 06:04:01 +0000670 }
Eric Andersen908e3622003-06-20 09:56:37 +0000671 return (show_only) ? 0 : rc;
Eric Andersen60e56f52002-04-26 06:04:01 +0000672}
673
Rob Landleye9190962005-12-12 19:38:44 +0000674/*
Rob Landleybf30c692006-07-20 17:36:18 +0000675 * Check the matching between a pattern and a module name.
676 * We need this as *_* is equivalent to *-*, even in pattern matching.
677 */
Denis Vlasenkoac678ec2007-04-16 22:32:04 +0000678static int check_pattern(const char* pat_src, const char* mod_src)
679{
Rob Landleybf30c692006-07-20 17:36:18 +0000680 int ret;
681
682 if (ENABLE_FEATURE_MODPROBE_FANCY_ALIAS) {
683 char* pat;
684 char* mod;
685 char* p;
686
Denis Vlasenko6398cf42007-04-11 17:04:29 +0000687 pat = xstrdup(pat_src);
688 mod = xstrdup(mod_src);
Rob Landleybf30c692006-07-20 17:36:18 +0000689
Denis Vlasenkocf704332006-10-27 15:12:50 +0000690 for (p = pat; (p = strchr(p, '-')); *p++ = '_');
691 for (p = mod; (p = strchr(p, '-')); *p++ = '_');
Rob Landleybf30c692006-07-20 17:36:18 +0000692
Denis Vlasenkocf704332006-10-27 15:12:50 +0000693 ret = fnmatch(pat, mod, 0);
Rob Landleybf30c692006-07-20 17:36:18 +0000694
695 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkoff131b92007-04-10 15:42:06 +0000696 free(pat);
697 free(mod);
Rob Landleybf30c692006-07-20 17:36:18 +0000698 }
699
700 return ret;
701 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000702 return fnmatch(pat_src, mod_src, 0);
Rob Landleybf30c692006-07-20 17:36:18 +0000703 }
704}
705
706/*
Rob Landleye9190962005-12-12 19:38:44 +0000707 * Builds the dependency list (aka stack) of a module.
708 * head: the highest module in the stack (last to insmod, first to rmmod)
709 * tail: the lowest module in the stack (first to insmod, last to rmmod)
710 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000711static void check_dep(char *mod, struct mod_list_t **head, struct mod_list_t **tail)
Eric Andersen60e56f52002-04-26 06:04:01 +0000712{
Robert Griebl52e8d062002-05-14 23:42:08 +0000713 struct mod_list_t *find;
Eric Andersen60e56f52002-04-26 06:04:01 +0000714 struct dep_t *dt;
Rob Landleye9190962005-12-12 19:38:44 +0000715 struct mod_opt_t *opt = 0;
Eric Andersen807bd842004-08-19 18:30:31 +0000716 char *path = 0;
Robert Griebl52e8d062002-05-14 23:42:08 +0000717
Bernhard Reutner-Fischer2c351a82006-06-03 19:08:49 +0000718 /* Search for the given module name amongst all dependency rules.
719 * The module name in a dependency rule can be a shell pattern,
720 * so try to match the given module name against such a pattern.
721 * Of course if the name in the dependency rule is a plain string,
722 * then we consider it a pattern, and matching will still work. */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000723 for (dt = depend; dt; dt = dt->m_next) {
724 if (check_pattern(dt->m_name, mod) == 0) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000725 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000726 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000727 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000728
Denis Vlasenkocf704332006-10-27 15:12:50 +0000729 if (!dt) {
730 bb_error_msg("module %s not found", mod);
Rob Landleye9190962005-12-12 19:38:44 +0000731 return;
732 }
733
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000734 // resolve alias names
Denis Vlasenkocf704332006-10-27 15:12:50 +0000735 while (dt->m_isalias) {
736 if (dt->m_depcnt == 1) {
Robert Grieblaead70b2002-07-26 15:54:20 +0000737 struct dep_t *adt;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000738
Denis Vlasenkocf704332006-10-27 15:12:50 +0000739 for (adt = depend; adt; adt = adt->m_next) {
740 if (check_pattern(adt->m_name, dt->m_deparr[0]) == 0)
Robert Grieblaead70b2002-07-26 15:54:20 +0000741 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000742 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000743 if (adt) {
Rob Landleye9190962005-12-12 19:38:44 +0000744 /* This is the module we are aliased to */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000745 struct mod_opt_t *opts = dt->m_options;
Rob Landleye9190962005-12-12 19:38:44 +0000746 /* Option of the alias are appended to the options of the module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000747 while (opts) {
748 adt->m_options = append_option(adt->m_options, opts->m_opt_val);
749 opts = opts->m_next;
Rob Landleye9190962005-12-12 19:38:44 +0000750 }
Eric Andersen716ccb22004-01-10 11:29:31 +0000751 dt = adt;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000752 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000753 bb_error_msg("module %s not found", mod);
Robert Griebl70112da2002-07-29 20:28:38 +0000754 return;
Rob Landleye9190962005-12-12 19:38:44 +0000755 }
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000756 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000757 bb_error_msg("bad alias %s", dt->m_name);
Eric Andersen716ccb22004-01-10 11:29:31 +0000758 return;
Rob Landleye9190962005-12-12 19:38:44 +0000759 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000760 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000761
Denis Vlasenkocf704332006-10-27 15:12:50 +0000762 mod = dt->m_name;
763 path = dt->m_path;
764 opt = dt->m_options;
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000765
Robert Griebl52e8d062002-05-14 23:42:08 +0000766 // search for duplicates
Denis Vlasenkocf704332006-10-27 15:12:50 +0000767 for (find = *head; find; find = find->m_next) {
768 if (!strcmp(mod, find->m_name)) {
769 // found ->dequeue it
Robert Griebl52e8d062002-05-14 23:42:08 +0000770
Denis Vlasenkocf704332006-10-27 15:12:50 +0000771 if (find->m_prev)
772 find->m_prev->m_next = find->m_next;
Robert Griebl52e8d062002-05-14 23:42:08 +0000773 else
Denis Vlasenkocf704332006-10-27 15:12:50 +0000774 *head = find->m_next;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000775
Denis Vlasenkocf704332006-10-27 15:12:50 +0000776 if (find->m_next)
777 find->m_next->m_prev = find->m_prev;
Robert Griebl52e8d062002-05-14 23:42:08 +0000778 else
Denis Vlasenkocf704332006-10-27 15:12:50 +0000779 *tail = find->m_prev;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000780
Robert Griebl52e8d062002-05-14 23:42:08 +0000781 break; // there can be only one duplicate
Eric Andersen716ccb22004-01-10 11:29:31 +0000782 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000783 }
784
Denis Vlasenkocf704332006-10-27 15:12:50 +0000785 if (!find) { // did not find a duplicate
786 find = xmalloc(sizeof(struct mod_list_t));
787 find->m_name = mod;
788 find->m_path = path;
789 find->m_options = opt;
Robert Griebl52e8d062002-05-14 23:42:08 +0000790 }
791
Eric Andersen716ccb22004-01-10 11:29:31 +0000792 // enqueue at tail
Denis Vlasenkocf704332006-10-27 15:12:50 +0000793 if (*tail)
794 (*tail)->m_next = find;
795 find->m_prev = *tail;
796 find->m_next = 0;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000797
Denis Vlasenkocf704332006-10-27 15:12:50 +0000798 if (!*head)
Robert Griebl52e8d062002-05-14 23:42:08 +0000799 *head = find;
800 *tail = find;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000801
Denis Vlasenkocf704332006-10-27 15:12:50 +0000802 if (dt) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000803 int i;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000804
Rob Landleye9190962005-12-12 19:38:44 +0000805 /* Add all dependable module for that new module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000806 for (i = 0; i < dt->m_depcnt; i++)
807 check_dep(dt->m_deparr[i], head, tail);
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000808 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000809}
810
Denis Vlasenkocf704332006-10-27 15:12:50 +0000811static int mod_insert(char *mod, int argc, char **argv)
Robert Griebl52e8d062002-05-14 23:42:08 +0000812{
Denis Vlasenko322661d2007-01-29 23:43:52 +0000813 struct mod_list_t *tail = NULL;
814 struct mod_list_t *head = NULL;
Eric Andersen908e3622003-06-20 09:56:37 +0000815 int rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000816
Robert Griebl52e8d062002-05-14 23:42:08 +0000817 // get dep list for module mod
Denis Vlasenkocf704332006-10-27 15:12:50 +0000818 check_dep(mod, &head, &tail);
Eric Andersen3b1a7442003-12-24 20:30:45 +0000819
Denis Vlasenko322661d2007-01-29 23:43:52 +0000820 rc = 1;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000821 if (head && tail) {
822 if (argc) {
Eric Andersen716ccb22004-01-10 11:29:31 +0000823 int i;
Robert Grieblaead70b2002-07-26 15:54:20 +0000824 // append module args
Denis Vlasenkocf704332006-10-27 15:12:50 +0000825 for (i = 0; i < argc; i++)
826 head->m_options = append_option(head->m_options, argv[i]);
Robert Griebl52e8d062002-05-14 23:42:08 +0000827 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000828
Robert Griebl52e8d062002-05-14 23:42:08 +0000829 // process tail ---> head
Denis Vlasenko322661d2007-01-29 23:43:52 +0000830 rc = mod_process(tail, 1);
831 if (rc) {
Rob Landley4b5827a2006-08-22 23:50:11 +0000832 /*
833 * In case of using udev, multiple instances of modprobe can be
834 * spawned to load the same module (think of two same usb devices,
835 * for example; or cold-plugging at boot time). Thus we shouldn't
836 * fail if the module was loaded, and not by us.
837 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000838 if (already_loaded(mod))
Rob Landley4b5827a2006-08-22 23:50:11 +0000839 rc = 0;
840 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000841 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000842 return rc;
843}
844
Denis Vlasenkocf704332006-10-27 15:12:50 +0000845static int mod_remove(char *mod)
Robert Griebl52e8d062002-05-14 23:42:08 +0000846{
Eric Andersen908e3622003-06-20 09:56:37 +0000847 int rc;
Denis Vlasenko322661d2007-01-29 23:43:52 +0000848 static const struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL };
Eric Andersen3b1a7442003-12-24 20:30:45 +0000849
Denis Vlasenko322661d2007-01-29 23:43:52 +0000850 struct mod_list_t *head = NULL;
851 struct mod_list_t *tail = NULL;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000852
Denis Vlasenkocf704332006-10-27 15:12:50 +0000853 if (mod)
854 check_dep(mod, &head, &tail);
Robert Griebl52e8d062002-05-14 23:42:08 +0000855 else // autoclean
Denis Vlasenko322661d2007-01-29 23:43:52 +0000856 head = tail = (struct mod_list_t*) &rm_a_dummy;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000857
Denis Vlasenko322661d2007-01-29 23:43:52 +0000858 rc = 1;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000859 if (head && tail)
860 rc = mod_process(head, 0); // process head ---> tail
Eric Andersen908e3622003-06-20 09:56:37 +0000861 return rc;
Robert Griebl52e8d062002-05-14 23:42:08 +0000862}
863
Denis Vlasenko06af2162007-02-03 17:28:39 +0000864int modprobe_main(int argc, char** argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000865int modprobe_main(int argc, char** argv)
Eric Andersen0139ca92001-07-22 23:01:03 +0000866{
Rob Landleye9190962005-12-12 19:38:44 +0000867 int rc = EXIT_SUCCESS;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000868 char *unused;
Eric Andersen0139ca92001-07-22 23:01:03 +0000869
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000870 opt_complementary = "?V-:q-v:v-q";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000871 main_opts = getopt32(argv, "acdklnqrst:vVC:",
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000872 &unused, &unused);
Denis Vlasenkocf704332006-10-27 15:12:50 +0000873 if (main_opts & (DUMP_CONF_EXIT | LIST_ALL))
874 return EXIT_SUCCESS;
875 if (main_opts & (RESTRICT_DIR | CONFIG_FILE))
876 bb_error_msg_and_die("-t and -C not supported");
Robert Griebl236abbf2002-05-22 23:34:35 +0000877
Denis Vlasenkocf704332006-10-27 15:12:50 +0000878 depend = build_dep();
Robert Griebl52e8d062002-05-14 23:42:08 +0000879
Denis Vlasenkocf704332006-10-27 15:12:50 +0000880 if (!depend)
881 bb_error_msg_and_die("cannot parse modules.dep");
Eric Andersen3b1a7442003-12-24 20:30:45 +0000882
Eric Andersen1b064192001-07-25 07:23:38 +0000883 if (remove_opt) {
Eric Andersen0139ca92001-07-22 23:01:03 +0000884 do {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000885 if (mod_remove(optind < argc ?
886 argv[optind] : NULL)) {
887 bb_error_msg("failed to remove module %s",
888 argv[optind]);
Eric Andersen908e3622003-06-20 09:56:37 +0000889 rc = EXIT_FAILURE;
890 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000891 } while (++optind < argc);
Rob Landleye9190962005-12-12 19:38:44 +0000892 } else {
893 if (optind >= argc)
Denis Vlasenkocf704332006-10-27 15:12:50 +0000894 bb_error_msg_and_die("no module or pattern provided");
Eric Andersen3b1a7442003-12-24 20:30:45 +0000895
Denis Vlasenkocf704332006-10-27 15:12:50 +0000896 if (mod_insert(argv[optind], argc - optind - 1, argv + optind + 1))
897 bb_error_msg_and_die("failed to load module %s", argv[optind]);
Eric Andersen0139ca92001-07-22 23:01:03 +0000898 }
899
Rob Landleye9190962005-12-12 19:38:44 +0000900 /* Here would be a good place to free up memory allocated during the dependencies build. */
Eric Andersen3b1a7442003-12-24 20:30:45 +0000901
Rob Landleye9190962005-12-12 19:38:44 +0000902 return rc;
Eric Andersen0139ca92001-07-22 23:01:03 +0000903}