blob: a7c6307f83adcd614ff3b2a7aab0fe9450d89680 [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
237 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
238
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 */
379 char *mods;
380 char *modpath;
381 char *mod;
382
383 /* Find the beginning of the module file name */
384 *col = 0;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000385 mods = strrchr(buffer, '/');
Rob Landleye9190962005-12-12 19:38:44 +0000386
Denis Vlasenkocf704332006-10-27 15:12:50 +0000387 if (!mods)
Rob Landleye9190962005-12-12 19:38:44 +0000388 mods = buffer; /* no path for this module */
389 else
390 mods++; /* there was a path for this module... */
391
392 /* find the path of the module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000393 modpath = strchr(buffer, '/'); /* ... and this is the path */
394 if (!modpath)
Rob Landleye9190962005-12-12 19:38:44 +0000395 modpath = buffer; /* module with no path */
396 /* find the end of the module name in the file name */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000397 if (ENABLE_FEATURE_2_6_MODULES &&
398 (k_version > 4) && (*(col-3) == '.') &&
399 (*(col-2) == 'k') && (*(col-1) == 'o'))
Rob Landleye9190962005-12-12 19:38:44 +0000400 dot = col - 3;
401 else
Denis Vlasenkocf704332006-10-27 15:12:50 +0000402 if ((*(col-2) == '.') && (*(col-1) == 'o'))
Rob Landleye9190962005-12-12 19:38:44 +0000403 dot = col - 2;
404
Denis Vlasenkocf704332006-10-27 15:12:50 +0000405 mod = xstrndup(mods, dot - mods);
Rob Landleye9190962005-12-12 19:38:44 +0000406
407 /* enqueue new module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000408 if (!current) {
409 first = current = xmalloc(sizeof(struct dep_t));
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000410 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000411 current->m_next = xmalloc(sizeof(struct dep_t));
412 current = current->m_next;
Rob Landleye9190962005-12-12 19:38:44 +0000413 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000414 current->m_name = mod;
415 current->m_path = xstrdup(modpath);
416 current->m_options = NULL;
417 current->m_isalias = 0;
418 current->m_depcnt = 0;
419 current->m_deparr = 0;
420 current->m_next = 0;
Rob Landleye9190962005-12-12 19:38:44 +0000421
422 p = col + 1;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000423 } else
Rob Landleye9190962005-12-12 19:38:44 +0000424 /* this line is not a dep description */
425 p = 0;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000426 } else
Rob Landleye9190962005-12-12 19:38:44 +0000427 /* It's a dep description continuation */
428 p = buffer;
429
Denis Vlasenkocf704332006-10-27 15:12:50 +0000430 while (p && *p && isblank(*p))
Rob Landleye9190962005-12-12 19:38:44 +0000431 p++;
432
433 /* p points to the first dependable module; if NULL, no dependable module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000434 if (p && *p) {
435 char *end = &buffer[l-1];
Rob Landleye9190962005-12-12 19:38:44 +0000436 char *deps;
437 char *dep;
438 char *next;
439 int ext = 0;
440
Denis Vlasenkocf704332006-10-27 15:12:50 +0000441 while (isblank(*end) || (*end == '\\'))
Rob Landleye9190962005-12-12 19:38:44 +0000442 end--;
443
Denis Vlasenkocf704332006-10-27 15:12:50 +0000444 do {
Rob Landleye9190962005-12-12 19:38:44 +0000445 /* search the end of the dependency */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000446 next = strchr(p, ' ');
447 if (next) {
Rob Landleye9190962005-12-12 19:38:44 +0000448 *next = 0;
449 next--;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000450 } else
Rob Landleye9190962005-12-12 19:38:44 +0000451 next = end;
452
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000453 /* find the beginning of the module file name */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000454 deps = strrchr(p, '/');
Rob Landleye9190962005-12-12 19:38:44 +0000455
Denis Vlasenkocf704332006-10-27 15:12:50 +0000456 if (!deps || (deps < p)) {
Rob Landleye9190962005-12-12 19:38:44 +0000457 deps = p;
458
Denis Vlasenkocf704332006-10-27 15:12:50 +0000459 while (isblank(*deps))
Rob Landleye9190962005-12-12 19:38:44 +0000460 deps++;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000461 } else
Rob Landleye9190962005-12-12 19:38:44 +0000462 deps++;
463
464 /* find the end of the module name in the file name */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000465 if (ENABLE_FEATURE_2_6_MODULES
466 && (k_version > 4) && (*(next-2) == '.')
467 && (*(next-1) == 'k') && (*next == 'o'))
Rob Landleye9190962005-12-12 19:38:44 +0000468 ext = 3;
469 else
Denis Vlasenkocf704332006-10-27 15:12:50 +0000470 if ((*(next-1) == '.') && (*next == 'o'))
Rob Landleye9190962005-12-12 19:38:44 +0000471 ext = 2;
472
473 /* Cope with blank lines */
474 if ((next-deps-ext+1) <= 0)
475 continue;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000476 dep = xstrndup(deps, next - deps - ext + 1);
Rob Landleye9190962005-12-12 19:38:44 +0000477
478 /* Add the new dependable module name */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000479 current->m_depcnt++;
480 current->m_deparr = xrealloc(current->m_deparr,
481 sizeof(char *) * current->m_depcnt);
482 current->m_deparr[current->m_depcnt - 1] = dep;
Rob Landleye9190962005-12-12 19:38:44 +0000483
484 p = next + 2;
485 } while (next < end);
486 }
487
488 /* is there other dependable module(s) ? */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000489 if (buffer[l-1] == '\\')
Rob Landleye9190962005-12-12 19:38:44 +0000490 continuation_line = 1;
491 else
492 continuation_line = 0;
493 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000494 close(fd);
Rob Landleye9190962005-12-12 19:38:44 +0000495
Rob Landley3b0cfb42006-07-19 21:33:42 +0000496 /*
497 * First parse system-specific options and aliases
498 * as they take precedence over the kernel ones.
Mike Frysinger4423e5b2007-02-08 07:03:44 +0000499 * >=2.6: we only care about modprobe.conf
500 * <=2.4: we care about modules.conf and conf.modules
Rob Landley3b0cfb42006-07-19 21:33:42 +0000501 */
Mike Frysinger4423e5b2007-02-08 07:03:44 +0000502 if (ENABLE_FEATURE_2_6_MODULES
503 && (fd = open("/etc/modprobe.conf", O_RDONLY)) < 0)
504 if (ENABLE_FEATURE_2_4_MODULES
505 && (fd = open("/etc/modules.conf", O_RDONLY)) < 0)
506 if (ENABLE_FEATURE_2_4_MODULES)
507 fd = open("/etc/conf.modules", O_RDONLY);
Rob Landleye9190962005-12-12 19:38:44 +0000508
Rob Landley3b0cfb42006-07-19 21:33:42 +0000509 if (fd >= 0) {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000510 include_conf(&first, &current, buffer, sizeof(buffer), fd);
Rob Landley3b0cfb42006-07-19 21:33:42 +0000511 close(fd);
Rob Landleye9190962005-12-12 19:38:44 +0000512 }
Rob Landley72615752006-04-10 16:09:52 +0000513
Rob Landley3b0cfb42006-07-19 21:33:42 +0000514 /* Only 2.6 has a modules.alias file */
515 if (ENABLE_FEATURE_2_6_MODULES) {
516 /* Parse kernel-declared aliases */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000517 filename = xasprintf("/lib/modules/%s/modules.alias", un.release);
Denis Vlasenkocf704332006-10-27 15:12:50 +0000518 fd = open(filename, O_RDONLY);
519 if (fd < 0) {
Rob Landley3b0cfb42006-07-19 21:33:42 +0000520 /* Ok, that didn't work. Fall back to looking in /lib/modules */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000521 fd = open("/lib/modules/modules.alias", O_RDONLY);
Rob Landley3b0cfb42006-07-19 21:33:42 +0000522 }
523 if (ENABLE_FEATURE_CLEAN_UP)
524 free(filename);
525
526 if (fd >= 0) {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000527 include_conf(&first, &current, buffer, sizeof(buffer), fd);
Rob Landley3b0cfb42006-07-19 21:33:42 +0000528 close(fd);
529 }
530 }
Rob Landleye9190962005-12-12 19:38:44 +0000531
532 return first;
533}
534
535/* return 1 = loaded, 0 = not loaded, -1 = can't tell */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000536static int already_loaded(const char *name)
Rob Landleye9190962005-12-12 19:38:44 +0000537{
Rob Landleyd7605602006-06-14 01:51:16 +0000538 int fd, ret = 0;
Rob Landleye9190962005-12-12 19:38:44 +0000539 char buffer[4096];
540
Denis Vlasenkocf704332006-10-27 15:12:50 +0000541 fd = open("/proc/modules", O_RDONLY);
Rob Landleye9190962005-12-12 19:38:44 +0000542 if (fd < 0)
543 return -1;
544
Denis Vlasenkocf704332006-10-27 15:12:50 +0000545 while (reads(fd, buffer, sizeof(buffer))) {
Rob Landleye9190962005-12-12 19:38:44 +0000546 char *p;
547
548 p = strchr (buffer, ' ');
549 if (p) {
Rob Landleyd7605602006-06-14 01:51:16 +0000550 const char *n;
551
552 // Truncate buffer at first space and check for matches, with
553 // the idiosyncrasy that _ and - are interchangeable because the
554 // 2.6 kernel does weird things.
555
Rob Landleye9190962005-12-12 19:38:44 +0000556 *p = 0;
Rob Landleyd7605602006-06-14 01:51:16 +0000557 for (p = buffer, n = name; ; p++, n++) {
558 if (*p != *n) {
559 if ((*p == '_' || *p == '-') && (*n == '_' || *n == '-'))
560 continue;
561 break;
562 }
563 // If we made it to the end, that's a match.
564 if (!*p) {
565 ret = 1;
566 goto done;
567 }
Rob Landleye9190962005-12-12 19:38:44 +0000568 }
569 }
570 }
Rob Landleyd7605602006-06-14 01:51:16 +0000571done:
Rob Landleye9190962005-12-12 19:38:44 +0000572 close (fd);
Mike Frysinger135cee32006-06-21 23:03:37 +0000573 return ret;
Rob Landleye9190962005-12-12 19:38:44 +0000574}
575
Denis Vlasenko322661d2007-01-29 23:43:52 +0000576static int mod_process(const struct mod_list_t *list, int do_insert)
Eric Andersen60e56f52002-04-26 06:04:01 +0000577{
Eric Andersen44b57582004-08-03 08:23:33 +0000578 int rc = 0;
Rob Landley79e1cab2005-11-15 00:08:29 +0000579 char **argv = NULL;
Rob Landleye9190962005-12-12 19:38:44 +0000580 struct mod_opt_t *opts;
581 int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */
Paul Fox8eeb6552005-08-04 18:33:36 +0000582 int argc;
Eric Andersen864b7972002-05-03 15:48:26 +0000583
Denis Vlasenkocf704332006-10-27 15:12:50 +0000584 while (list) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000585 argc = 0;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000586 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleye9190962005-12-12 19:38:44 +0000587 argc_malloc = 0;
588 /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory
589 * each time we allocate memory for argv.
590 * But it is (quite) small amounts of memory that leak each
591 * time a module is loaded, and it is reclaimed when modprobe
592 * exits anyway (even when standalone shell?).
593 * This could become a problem when loading a module with LOTS of
594 * dependencies, with LOTS of options for each dependencies, with
595 * very little memory on the target... But in that case, the module
596 * would not load because there is no more memory, so there's no
597 * problem. */
598 /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000599 argv = xmalloc(6 * sizeof(char*));
600 if (do_insert) {
601 if (already_loaded(list->m_name) != 1) {
Denis Vlasenko322661d2007-01-29 23:43:52 +0000602 argv[argc++] = (char*)"insmod";
Rob Landleyd7605602006-06-14 01:51:16 +0000603 if (ENABLE_FEATURE_2_4_MODULES) {
604 if (do_syslog)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000605 argv[argc++] = (char*)"-s";
Rob Landleyd7605602006-06-14 01:51:16 +0000606 if (autoclean)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000607 argv[argc++] = (char*)"-k";
Rob Landleyd7605602006-06-14 01:51:16 +0000608 if (quiet)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000609 argv[argc++] = (char*)"-q";
Denis Vlasenkocf704332006-10-27 15:12:50 +0000610 else if (verbose) /* verbose and quiet are mutually exclusive */
Denis Vlasenko322661d2007-01-29 23:43:52 +0000611 argv[argc++] = (char*)"-v";
Rob Landleyd7605602006-06-14 01:51:16 +0000612 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000613 argv[argc++] = list->m_path;
614 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleye9190962005-12-12 19:38:44 +0000615 argc_malloc = argc;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000616 opts = list->m_options;
617 while (opts) {
Rob Landleye9190962005-12-12 19:38:44 +0000618 /* Add one more option */
Rob Landley79e1cab2005-11-15 00:08:29 +0000619 argc++;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000620 argv = xrealloc(argv,(argc + 1)* sizeof(char*));
621 argv[argc-1] = opts->m_opt_val;
622 opts = opts->m_next;
Rob Landley79e1cab2005-11-15 00:08:29 +0000623 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000624 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000625 } else {
Eric Andersen807bd842004-08-19 18:30:31 +0000626 /* modutils uses short name for removal */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000627 if (already_loaded(list->m_name) != 0) {
Denis Vlasenko322661d2007-01-29 23:43:52 +0000628 argv[argc++] = (char*)"rmmod";
Paul Fox8eeb6552005-08-04 18:33:36 +0000629 if (do_syslog)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000630 argv[argc++] = (char*)"-s";
631 argv[argc++] = (char*)list->m_name;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000632 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleye9190962005-12-12 19:38:44 +0000633 argc_malloc = argc;
Paul Fox8eeb6552005-08-04 18:33:36 +0000634 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000635 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000636 argv[argc] = NULL;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000637
Paul Fox8eeb6552005-08-04 18:33:36 +0000638 if (argc) {
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000639 if (verbose) {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000640 printf("%s module %s\n", do_insert?"Loading":"Unloading", list->m_name);
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000641 }
642 if (!show_only) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000643 int rc2 = wait4pid(spawn(argv));
Rob Landley4b5827a2006-08-22 23:50:11 +0000644
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000645 if (do_insert) {
646 rc = rc2; /* only last module matters */
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000647 } else if (!rc2) {
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000648 rc = 0; /* success if remove any mod */
649 }
650 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000651 if (ENABLE_FEATURE_CLEAN_UP) {
Rob Landleye9190962005-12-12 19:38:44 +0000652 /* the last value in the array has index == argc, but
653 * it is the terminating NULL, so we must not free it. */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000654 while (argc_malloc < argc) {
655 free(argv[argc_malloc++]);
656 }
Rob Landley79e1cab2005-11-15 00:08:29 +0000657 }
Eric Andersen908e3622003-06-20 09:56:37 +0000658 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000659 if (ENABLE_FEATURE_CLEAN_UP) {
660 free(argv);
Rob Landleye9190962005-12-12 19:38:44 +0000661 argv = NULL;
Rob Landley79e1cab2005-11-15 00:08:29 +0000662 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000663 list = do_insert ? list->m_prev : list->m_next;
Eric Andersen60e56f52002-04-26 06:04:01 +0000664 }
Eric Andersen908e3622003-06-20 09:56:37 +0000665 return (show_only) ? 0 : rc;
Eric Andersen60e56f52002-04-26 06:04:01 +0000666}
667
Rob Landleye9190962005-12-12 19:38:44 +0000668/*
Rob Landleybf30c692006-07-20 17:36:18 +0000669 * Check the matching between a pattern and a module name.
670 * We need this as *_* is equivalent to *-*, even in pattern matching.
671 */
Denis Vlasenkoac678ec2007-04-16 22:32:04 +0000672static int check_pattern(const char* pat_src, const char* mod_src)
673{
Rob Landleybf30c692006-07-20 17:36:18 +0000674 int ret;
675
676 if (ENABLE_FEATURE_MODPROBE_FANCY_ALIAS) {
677 char* pat;
678 char* mod;
679 char* p;
680
Denis Vlasenko6398cf42007-04-11 17:04:29 +0000681 pat = xstrdup(pat_src);
682 mod = xstrdup(mod_src);
Rob Landleybf30c692006-07-20 17:36:18 +0000683
Denis Vlasenkocf704332006-10-27 15:12:50 +0000684 for (p = pat; (p = strchr(p, '-')); *p++ = '_');
685 for (p = mod; (p = strchr(p, '-')); *p++ = '_');
Rob Landleybf30c692006-07-20 17:36:18 +0000686
Denis Vlasenkocf704332006-10-27 15:12:50 +0000687 ret = fnmatch(pat, mod, 0);
Rob Landleybf30c692006-07-20 17:36:18 +0000688
689 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkoff131b92007-04-10 15:42:06 +0000690 free(pat);
691 free(mod);
Rob Landleybf30c692006-07-20 17:36:18 +0000692 }
693
694 return ret;
695 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000696 return fnmatch(pat_src, mod_src, 0);
Rob Landleybf30c692006-07-20 17:36:18 +0000697 }
698}
699
700/*
Rob Landleye9190962005-12-12 19:38:44 +0000701 * Builds the dependency list (aka stack) of a module.
702 * head: the highest module in the stack (last to insmod, first to rmmod)
703 * tail: the lowest module in the stack (first to insmod, last to rmmod)
704 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000705static void check_dep(char *mod, struct mod_list_t **head, struct mod_list_t **tail)
Eric Andersen60e56f52002-04-26 06:04:01 +0000706{
Robert Griebl52e8d062002-05-14 23:42:08 +0000707 struct mod_list_t *find;
Eric Andersen60e56f52002-04-26 06:04:01 +0000708 struct dep_t *dt;
Rob Landleye9190962005-12-12 19:38:44 +0000709 struct mod_opt_t *opt = 0;
Eric Andersen807bd842004-08-19 18:30:31 +0000710 char *path = 0;
Robert Griebl52e8d062002-05-14 23:42:08 +0000711
Bernhard Reutner-Fischer2c351a82006-06-03 19:08:49 +0000712 /* Search for the given module name amongst all dependency rules.
713 * The module name in a dependency rule can be a shell pattern,
714 * so try to match the given module name against such a pattern.
715 * Of course if the name in the dependency rule is a plain string,
716 * then we consider it a pattern, and matching will still work. */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000717 for (dt = depend; dt; dt = dt->m_next) {
718 if (check_pattern(dt->m_name, mod) == 0) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000719 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000720 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000721 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000722
Denis Vlasenkocf704332006-10-27 15:12:50 +0000723 if (!dt) {
724 bb_error_msg("module %s not found", mod);
Rob Landleye9190962005-12-12 19:38:44 +0000725 return;
726 }
727
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000728 // resolve alias names
Denis Vlasenkocf704332006-10-27 15:12:50 +0000729 while (dt->m_isalias) {
730 if (dt->m_depcnt == 1) {
Robert Grieblaead70b2002-07-26 15:54:20 +0000731 struct dep_t *adt;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000732
Denis Vlasenkocf704332006-10-27 15:12:50 +0000733 for (adt = depend; adt; adt = adt->m_next) {
734 if (check_pattern(adt->m_name, dt->m_deparr[0]) == 0)
Robert Grieblaead70b2002-07-26 15:54:20 +0000735 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000736 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000737 if (adt) {
Rob Landleye9190962005-12-12 19:38:44 +0000738 /* This is the module we are aliased to */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000739 struct mod_opt_t *opts = dt->m_options;
Rob Landleye9190962005-12-12 19:38:44 +0000740 /* Option of the alias are appended to the options of the module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000741 while (opts) {
742 adt->m_options = append_option(adt->m_options, opts->m_opt_val);
743 opts = opts->m_next;
Rob Landleye9190962005-12-12 19:38:44 +0000744 }
Eric Andersen716ccb22004-01-10 11:29:31 +0000745 dt = adt;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000746 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000747 bb_error_msg("module %s not found", mod);
Robert Griebl70112da2002-07-29 20:28:38 +0000748 return;
Rob Landleye9190962005-12-12 19:38:44 +0000749 }
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000750 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000751 bb_error_msg("bad alias %s", dt->m_name);
Eric Andersen716ccb22004-01-10 11:29:31 +0000752 return;
Rob Landleye9190962005-12-12 19:38:44 +0000753 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000754 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000755
Denis Vlasenkocf704332006-10-27 15:12:50 +0000756 mod = dt->m_name;
757 path = dt->m_path;
758 opt = dt->m_options;
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000759
Robert Griebl52e8d062002-05-14 23:42:08 +0000760 // search for duplicates
Denis Vlasenkocf704332006-10-27 15:12:50 +0000761 for (find = *head; find; find = find->m_next) {
762 if (!strcmp(mod, find->m_name)) {
763 // found ->dequeue it
Robert Griebl52e8d062002-05-14 23:42:08 +0000764
Denis Vlasenkocf704332006-10-27 15:12:50 +0000765 if (find->m_prev)
766 find->m_prev->m_next = find->m_next;
Robert Griebl52e8d062002-05-14 23:42:08 +0000767 else
Denis Vlasenkocf704332006-10-27 15:12:50 +0000768 *head = find->m_next;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000769
Denis Vlasenkocf704332006-10-27 15:12:50 +0000770 if (find->m_next)
771 find->m_next->m_prev = find->m_prev;
Robert Griebl52e8d062002-05-14 23:42:08 +0000772 else
Denis Vlasenkocf704332006-10-27 15:12:50 +0000773 *tail = find->m_prev;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000774
Robert Griebl52e8d062002-05-14 23:42:08 +0000775 break; // there can be only one duplicate
Eric Andersen716ccb22004-01-10 11:29:31 +0000776 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000777 }
778
Denis Vlasenkocf704332006-10-27 15:12:50 +0000779 if (!find) { // did not find a duplicate
780 find = xmalloc(sizeof(struct mod_list_t));
781 find->m_name = mod;
782 find->m_path = path;
783 find->m_options = opt;
Robert Griebl52e8d062002-05-14 23:42:08 +0000784 }
785
Eric Andersen716ccb22004-01-10 11:29:31 +0000786 // enqueue at tail
Denis Vlasenkocf704332006-10-27 15:12:50 +0000787 if (*tail)
788 (*tail)->m_next = find;
789 find->m_prev = *tail;
790 find->m_next = 0;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000791
Denis Vlasenkocf704332006-10-27 15:12:50 +0000792 if (!*head)
Robert Griebl52e8d062002-05-14 23:42:08 +0000793 *head = find;
794 *tail = find;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000795
Denis Vlasenkocf704332006-10-27 15:12:50 +0000796 if (dt) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000797 int i;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000798
Rob Landleye9190962005-12-12 19:38:44 +0000799 /* Add all dependable module for that new module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000800 for (i = 0; i < dt->m_depcnt; i++)
801 check_dep(dt->m_deparr[i], head, tail);
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000802 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000803}
804
Denis Vlasenkocf704332006-10-27 15:12:50 +0000805static int mod_insert(char *mod, int argc, char **argv)
Robert Griebl52e8d062002-05-14 23:42:08 +0000806{
Denis Vlasenko322661d2007-01-29 23:43:52 +0000807 struct mod_list_t *tail = NULL;
808 struct mod_list_t *head = NULL;
Eric Andersen908e3622003-06-20 09:56:37 +0000809 int rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000810
Robert Griebl52e8d062002-05-14 23:42:08 +0000811 // get dep list for module mod
Denis Vlasenkocf704332006-10-27 15:12:50 +0000812 check_dep(mod, &head, &tail);
Eric Andersen3b1a7442003-12-24 20:30:45 +0000813
Denis Vlasenko322661d2007-01-29 23:43:52 +0000814 rc = 1;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000815 if (head && tail) {
816 if (argc) {
Eric Andersen716ccb22004-01-10 11:29:31 +0000817 int i;
Robert Grieblaead70b2002-07-26 15:54:20 +0000818 // append module args
Denis Vlasenkocf704332006-10-27 15:12:50 +0000819 for (i = 0; i < argc; i++)
820 head->m_options = append_option(head->m_options, argv[i]);
Robert Griebl52e8d062002-05-14 23:42:08 +0000821 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000822
Robert Griebl52e8d062002-05-14 23:42:08 +0000823 // process tail ---> head
Denis Vlasenko322661d2007-01-29 23:43:52 +0000824 rc = mod_process(tail, 1);
825 if (rc) {
Rob Landley4b5827a2006-08-22 23:50:11 +0000826 /*
827 * In case of using udev, multiple instances of modprobe can be
828 * spawned to load the same module (think of two same usb devices,
829 * for example; or cold-plugging at boot time). Thus we shouldn't
830 * fail if the module was loaded, and not by us.
831 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000832 if (already_loaded(mod))
Rob Landley4b5827a2006-08-22 23:50:11 +0000833 rc = 0;
834 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000835 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000836 return rc;
837}
838
Denis Vlasenkocf704332006-10-27 15:12:50 +0000839static int mod_remove(char *mod)
Robert Griebl52e8d062002-05-14 23:42:08 +0000840{
Eric Andersen908e3622003-06-20 09:56:37 +0000841 int rc;
Denis Vlasenko322661d2007-01-29 23:43:52 +0000842 static const struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL };
Eric Andersen3b1a7442003-12-24 20:30:45 +0000843
Denis Vlasenko322661d2007-01-29 23:43:52 +0000844 struct mod_list_t *head = NULL;
845 struct mod_list_t *tail = NULL;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000846
Denis Vlasenkocf704332006-10-27 15:12:50 +0000847 if (mod)
848 check_dep(mod, &head, &tail);
Robert Griebl52e8d062002-05-14 23:42:08 +0000849 else // autoclean
Denis Vlasenko322661d2007-01-29 23:43:52 +0000850 head = tail = (struct mod_list_t*) &rm_a_dummy;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000851
Denis Vlasenko322661d2007-01-29 23:43:52 +0000852 rc = 1;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000853 if (head && tail)
854 rc = mod_process(head, 0); // process head ---> tail
Eric Andersen908e3622003-06-20 09:56:37 +0000855 return rc;
Robert Griebl52e8d062002-05-14 23:42:08 +0000856}
857
Denis Vlasenko06af2162007-02-03 17:28:39 +0000858int modprobe_main(int argc, char** argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000859int modprobe_main(int argc, char** argv)
Eric Andersen0139ca92001-07-22 23:01:03 +0000860{
Rob Landleye9190962005-12-12 19:38:44 +0000861 int rc = EXIT_SUCCESS;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000862 char *unused;
Eric Andersen0139ca92001-07-22 23:01:03 +0000863
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000864 opt_complementary = "?V-:q-v:v-q";
865 main_opts = getopt32(argc, argv, "acdklnqrst:vVC:",
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000866 &unused, &unused);
Denis Vlasenkocf704332006-10-27 15:12:50 +0000867 if (main_opts & (DUMP_CONF_EXIT | LIST_ALL))
868 return EXIT_SUCCESS;
869 if (main_opts & (RESTRICT_DIR | CONFIG_FILE))
870 bb_error_msg_and_die("-t and -C not supported");
Robert Griebl236abbf2002-05-22 23:34:35 +0000871
Denis Vlasenkocf704332006-10-27 15:12:50 +0000872 depend = build_dep();
Robert Griebl52e8d062002-05-14 23:42:08 +0000873
Denis Vlasenkocf704332006-10-27 15:12:50 +0000874 if (!depend)
875 bb_error_msg_and_die("cannot parse modules.dep");
Eric Andersen3b1a7442003-12-24 20:30:45 +0000876
Eric Andersen1b064192001-07-25 07:23:38 +0000877 if (remove_opt) {
Eric Andersen0139ca92001-07-22 23:01:03 +0000878 do {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000879 if (mod_remove(optind < argc ?
880 argv[optind] : NULL)) {
881 bb_error_msg("failed to remove module %s",
882 argv[optind]);
Eric Andersen908e3622003-06-20 09:56:37 +0000883 rc = EXIT_FAILURE;
884 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000885 } while (++optind < argc);
Rob Landleye9190962005-12-12 19:38:44 +0000886 } else {
887 if (optind >= argc)
Denis Vlasenkocf704332006-10-27 15:12:50 +0000888 bb_error_msg_and_die("no module or pattern provided");
Eric Andersen3b1a7442003-12-24 20:30:45 +0000889
Denis Vlasenkocf704332006-10-27 15:12:50 +0000890 if (mod_insert(argv[optind], argc - optind - 1, argv + optind + 1))
891 bb_error_msg_and_die("failed to load module %s", argv[optind]);
Eric Andersen0139ca92001-07-22 23:01:03 +0000892 }
893
Rob Landleye9190962005-12-12 19:38:44 +0000894 /* Here would be a good place to free up memory allocated during the dependencies build. */
Eric Andersen3b1a7442003-12-24 20:30:45 +0000895
Rob Landleye9190962005-12-12 19:38:44 +0000896 return rc;
Eric Andersen0139ca92001-07-22 23:01:03 +0000897}