blob: dcab493f15ee8b1f9b3557fc022beda06ac562b6 [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
Bernhard Reutner-Fischerdeda6a52006-06-03 19:35:15 +000014#include "busybox.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 */
Eric Andersen807bd842004-08-19 18:30:31 +000040 char * m_name;
41 char * m_path;
Rob Landleye9190962005-12-12 19:38:44 +000042 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));
Rob Landley72615752006-04-10 16:09:52 +0000267 }
268 else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000269 (*current)->m_next = xzalloc(sizeof(struct dep_t));
270 (*current) = (*current)->m_next;
Rob Landley72615752006-04-10 16:09:52 +0000271 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000272 (*current)->m_name = xstrdup(alias);
273 (*current)->m_isalias = 1;
Rob Landley72615752006-04-10 16:09:52 +0000274
Denis Vlasenkocf704332006-10-27 15:12:50 +0000275 if ((strcmp(mod, "off") == 0) || (strcmp(mod, "null") == 0)) {
276 (*current)->m_depcnt = 0;
277 (*current)->m_deparr = 0;
Rob Landley72615752006-04-10 16:09:52 +0000278 }
279 else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000280 (*current)->m_depcnt = 1;
281 (*current)->m_deparr = xmalloc(1 * sizeof(char *));
282 (*current)->m_deparr[0] = xstrdup(mod);
Rob Landley72615752006-04-10 16:09:52 +0000283 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000284 (*current)->m_next = 0;
Rob Landley72615752006-04-10 16:09:52 +0000285 }
286 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000287 else if ((strncmp(buffer, "options", 7) == 0) && isspace(buffer[7])) {
Rob Landley72615752006-04-10 16:09:52 +0000288 char *mod, *opt;
289
290 /* split the line in the module/alias name, and options */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000291 if (parse_tag_value(buffer + 8, &mod, &opt)) {
Rob Landley72615752006-04-10 16:09:52 +0000292 struct dep_t *dt;
293
294 /* find the corresponding module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000295 for (dt = *first; dt; dt = dt->m_next) {
296 if (strcmp(dt->m_name, mod) == 0)
Rob Landley72615752006-04-10 16:09:52 +0000297 break;
298 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000299 if (dt) {
300 if (ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS) {
Rob Landley72615752006-04-10 16:09:52 +0000301 char* new_opt = NULL;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000302 while ((opt = parse_command_string(opt, &new_opt))) {
303 dt->m_options = append_option(dt->m_options, new_opt);
Rob Landley72615752006-04-10 16:09:52 +0000304 }
305 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000306 dt->m_options = append_option(dt->m_options, opt);
Rob Landley72615752006-04-10 16:09:52 +0000307 }
308 }
309 }
310 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000311 else if ((strncmp(buffer, "include", 7) == 0) && isspace(buffer[7])) {
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000312 int fdi; char *filename;
Rob Landley72615752006-04-10 16:09:52 +0000313
Denis Vlasenkocf704332006-10-27 15:12:50 +0000314 filename = skip_whitespace(buffer + 8);
Rob Landley72615752006-04-10 16:09:52 +0000315
Denis Vlasenkocf704332006-10-27 15:12:50 +0000316 if ((fdi = open(filename, O_RDONLY)) >= 0) {
Rob Landley72615752006-04-10 16:09:52 +0000317 include_conf(first, current, buffer, buflen, fdi);
318 close(fdi);
319 }
320 }
321 }
322 }
323}
324
325/*
Bernhard Reutner-Fischere3c150b2006-05-19 11:24:28 +0000326 * This function builds a list of dependency rules from /lib/modules/`uname -r`\modules.dep.
327 * It then fills every modules and aliases with their default options, found by parsing
Rob Landleye9190962005-12-12 19:38:44 +0000328 * modprobe.conf (or modules.conf, or conf.modules).
329 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000330static struct dep_t *build_dep(void)
Rob Landleye9190962005-12-12 19:38:44 +0000331{
332 int fd;
333 struct utsname un;
334 struct dep_t *first = 0;
335 struct dep_t *current = 0;
336 char buffer[2048];
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000337 char *filename;
Rob Landleye9190962005-12-12 19:38:44 +0000338 int continuation_line = 0;
339 int k_version;
340
Denis Vlasenkocf704332006-10-27 15:12:50 +0000341 if (uname(&un))
Rob Landleye9190962005-12-12 19:38:44 +0000342 bb_error_msg_and_die("can't determine kernel version");
343
Denis Vlasenkocf704332006-10-27 15:12:50 +0000344 k_version = 0;
Rob Landleye9190962005-12-12 19:38:44 +0000345 if (un.release[0] == '2') {
346 k_version = un.release[2] - '0';
347 }
348
Denis Vlasenkocf704332006-10-27 15:12:50 +0000349 filename = xasprintf("/lib/modules/%s/modules.dep", un.release);
350 fd = open(filename, O_RDONLY);
Bernhard Reutner-Fischere3c150b2006-05-19 11:24:28 +0000351 if (ENABLE_FEATURE_CLEAN_UP)
352 free(filename);
Rob Landley3afb0702006-05-18 20:41:43 +0000353 if (fd < 0) {
Rob Landleye9190962005-12-12 19:38:44 +0000354 /* Ok, that didn't work. Fall back to looking in /lib/modules */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000355 fd = open("/lib/modules/modules.dep", O_RDONLY);
356 if (fd < 0) {
Rob Landleye9190962005-12-12 19:38:44 +0000357 return 0;
358 }
359 }
360
Denis Vlasenkocf704332006-10-27 15:12:50 +0000361 while (reads(fd, buffer, sizeof(buffer))) {
362 int l = strlen(buffer);
Rob Landleye9190962005-12-12 19:38:44 +0000363 char *p = 0;
364
Denis Vlasenkocf704332006-10-27 15:12:50 +0000365 while (l > 0 && isspace(buffer[l-1])) {
366 buffer[l-1] = 0;
Rob Landleye9190962005-12-12 19:38:44 +0000367 l--;
368 }
369
Denis Vlasenkocf704332006-10-27 15:12:50 +0000370 if (l == 0) {
Rob Landleye9190962005-12-12 19:38:44 +0000371 continuation_line = 0;
372 continue;
373 }
374
375 /* Is this a new module dep description? */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000376 if (!continuation_line) {
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000377 /* find the dep beginning */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000378 char *col = strchr(buffer, ':');
Rob Landleye9190962005-12-12 19:38:44 +0000379 char *dot = col;
380
Denis Vlasenkocf704332006-10-27 15:12:50 +0000381 if (col) {
Rob Landleye9190962005-12-12 19:38:44 +0000382 /* This line is a dep description */
383 char *mods;
384 char *modpath;
385 char *mod;
386
387 /* Find the beginning of the module file name */
388 *col = 0;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000389 mods = strrchr(buffer, '/');
Rob Landleye9190962005-12-12 19:38:44 +0000390
Denis Vlasenkocf704332006-10-27 15:12:50 +0000391 if (!mods)
Rob Landleye9190962005-12-12 19:38:44 +0000392 mods = buffer; /* no path for this module */
393 else
394 mods++; /* there was a path for this module... */
395
396 /* find the path of the module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000397 modpath = strchr(buffer, '/'); /* ... and this is the path */
398 if (!modpath)
Rob Landleye9190962005-12-12 19:38:44 +0000399 modpath = buffer; /* module with no path */
400 /* find the end of the module name in the file name */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000401 if (ENABLE_FEATURE_2_6_MODULES &&
402 (k_version > 4) && (*(col-3) == '.') &&
403 (*(col-2) == 'k') && (*(col-1) == 'o'))
Rob Landleye9190962005-12-12 19:38:44 +0000404 dot = col - 3;
405 else
Denis Vlasenkocf704332006-10-27 15:12:50 +0000406 if ((*(col-2) == '.') && (*(col-1) == 'o'))
Rob Landleye9190962005-12-12 19:38:44 +0000407 dot = col - 2;
408
Denis Vlasenkocf704332006-10-27 15:12:50 +0000409 mod = xstrndup(mods, dot - mods);
Rob Landleye9190962005-12-12 19:38:44 +0000410
411 /* enqueue new module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000412 if (!current) {
413 first = current = xmalloc(sizeof(struct dep_t));
Rob Landleye9190962005-12-12 19:38:44 +0000414 }
415 else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000416 current->m_next = xmalloc(sizeof(struct dep_t));
417 current = current->m_next;
Rob Landleye9190962005-12-12 19:38:44 +0000418 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000419 current->m_name = mod;
420 current->m_path = xstrdup(modpath);
421 current->m_options = NULL;
422 current->m_isalias = 0;
423 current->m_depcnt = 0;
424 current->m_deparr = 0;
425 current->m_next = 0;
Rob Landleye9190962005-12-12 19:38:44 +0000426
427 p = col + 1;
428 }
429 else
430 /* this line is not a dep description */
431 p = 0;
432 }
433 else
434 /* It's a dep description continuation */
435 p = buffer;
436
Denis Vlasenkocf704332006-10-27 15:12:50 +0000437 while (p && *p && isblank(*p))
Rob Landleye9190962005-12-12 19:38:44 +0000438 p++;
439
440 /* p points to the first dependable module; if NULL, no dependable module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000441 if (p && *p) {
442 char *end = &buffer[l-1];
Rob Landleye9190962005-12-12 19:38:44 +0000443 char *deps;
444 char *dep;
445 char *next;
446 int ext = 0;
447
Denis Vlasenkocf704332006-10-27 15:12:50 +0000448 while (isblank(*end) || (*end == '\\'))
Rob Landleye9190962005-12-12 19:38:44 +0000449 end--;
450
Denis Vlasenkocf704332006-10-27 15:12:50 +0000451 do {
Rob Landleye9190962005-12-12 19:38:44 +0000452 /* search the end of the dependency */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000453 next = strchr(p, ' ');
454 if (next) {
Rob Landleye9190962005-12-12 19:38:44 +0000455 *next = 0;
456 next--;
457 }
458 else
459 next = end;
460
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000461 /* find the beginning of the module file name */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000462 deps = strrchr(p, '/');
Rob Landleye9190962005-12-12 19:38:44 +0000463
Denis Vlasenkocf704332006-10-27 15:12:50 +0000464 if (!deps || (deps < p)) {
Rob Landleye9190962005-12-12 19:38:44 +0000465 deps = p;
466
Denis Vlasenkocf704332006-10-27 15:12:50 +0000467 while (isblank(*deps))
Rob Landleye9190962005-12-12 19:38:44 +0000468 deps++;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000469 } else
Rob Landleye9190962005-12-12 19:38:44 +0000470 deps++;
471
472 /* find the end of the module name in the file name */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000473 if (ENABLE_FEATURE_2_6_MODULES
474 && (k_version > 4) && (*(next-2) == '.')
475 && (*(next-1) == 'k') && (*next == 'o'))
Rob Landleye9190962005-12-12 19:38:44 +0000476 ext = 3;
477 else
Denis Vlasenkocf704332006-10-27 15:12:50 +0000478 if ((*(next-1) == '.') && (*next == 'o'))
Rob Landleye9190962005-12-12 19:38:44 +0000479 ext = 2;
480
481 /* Cope with blank lines */
482 if ((next-deps-ext+1) <= 0)
483 continue;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000484 dep = xstrndup(deps, next - deps - ext + 1);
Rob Landleye9190962005-12-12 19:38:44 +0000485
486 /* Add the new dependable module name */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000487 current->m_depcnt++;
488 current->m_deparr = xrealloc(current->m_deparr,
489 sizeof(char *) * current->m_depcnt);
490 current->m_deparr[current->m_depcnt - 1] = dep;
Rob Landleye9190962005-12-12 19:38:44 +0000491
492 p = next + 2;
493 } while (next < end);
494 }
495
496 /* is there other dependable module(s) ? */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000497 if (buffer[l-1] == '\\')
Rob Landleye9190962005-12-12 19:38:44 +0000498 continuation_line = 1;
499 else
500 continuation_line = 0;
501 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000502 close(fd);
Rob Landleye9190962005-12-12 19:38:44 +0000503
Rob Landley3b0cfb42006-07-19 21:33:42 +0000504 /*
505 * First parse system-specific options and aliases
506 * as they take precedence over the kernel ones.
507 */
Rob Landleyae50c6d2005-12-15 07:42:13 +0000508 if (!ENABLE_FEATURE_2_6_MODULES
Denis Vlasenkocf704332006-10-27 15:12:50 +0000509 || (fd = open("/etc/modprobe.conf", O_RDONLY)) < 0)
510 if ((fd = open("/etc/modules.conf", O_RDONLY)) < 0)
511 fd = open("/etc/conf.modules", O_RDONLY);
Rob Landleye9190962005-12-12 19:38:44 +0000512
Rob Landley3b0cfb42006-07-19 21:33:42 +0000513 if (fd >= 0) {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000514 include_conf(&first, &current, buffer, sizeof(buffer), fd);
Rob Landley3b0cfb42006-07-19 21:33:42 +0000515 close(fd);
Rob Landleye9190962005-12-12 19:38:44 +0000516 }
Rob Landley72615752006-04-10 16:09:52 +0000517
Rob Landley3b0cfb42006-07-19 21:33:42 +0000518 /* Only 2.6 has a modules.alias file */
519 if (ENABLE_FEATURE_2_6_MODULES) {
520 /* Parse kernel-declared aliases */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000521 filename = xasprintf("/lib/modules/%s/modules.alias", un.release);
Denis Vlasenkocf704332006-10-27 15:12:50 +0000522 fd = open(filename, O_RDONLY);
523 if (fd < 0) {
Rob Landley3b0cfb42006-07-19 21:33:42 +0000524 /* Ok, that didn't work. Fall back to looking in /lib/modules */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000525 fd = open("/lib/modules/modules.alias", O_RDONLY);
Rob Landley3b0cfb42006-07-19 21:33:42 +0000526 }
527 if (ENABLE_FEATURE_CLEAN_UP)
528 free(filename);
529
530 if (fd >= 0) {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000531 include_conf(&first, &current, buffer, sizeof(buffer), fd);
Rob Landley3b0cfb42006-07-19 21:33:42 +0000532 close(fd);
533 }
534 }
Rob Landleye9190962005-12-12 19:38:44 +0000535
536 return first;
537}
538
539/* return 1 = loaded, 0 = not loaded, -1 = can't tell */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000540static int already_loaded(const char *name)
Rob Landleye9190962005-12-12 19:38:44 +0000541{
Rob Landleyd7605602006-06-14 01:51:16 +0000542 int fd, ret = 0;
Rob Landleye9190962005-12-12 19:38:44 +0000543 char buffer[4096];
544
Denis Vlasenkocf704332006-10-27 15:12:50 +0000545 fd = open("/proc/modules", O_RDONLY);
Rob Landleye9190962005-12-12 19:38:44 +0000546 if (fd < 0)
547 return -1;
548
Denis Vlasenkocf704332006-10-27 15:12:50 +0000549 while (reads(fd, buffer, sizeof(buffer))) {
Rob Landleye9190962005-12-12 19:38:44 +0000550 char *p;
551
552 p = strchr (buffer, ' ');
553 if (p) {
Rob Landleyd7605602006-06-14 01:51:16 +0000554 const char *n;
555
556 // Truncate buffer at first space and check for matches, with
557 // the idiosyncrasy that _ and - are interchangeable because the
558 // 2.6 kernel does weird things.
559
Rob Landleye9190962005-12-12 19:38:44 +0000560 *p = 0;
Rob Landleyd7605602006-06-14 01:51:16 +0000561 for (p = buffer, n = name; ; p++, n++) {
562 if (*p != *n) {
563 if ((*p == '_' || *p == '-') && (*n == '_' || *n == '-'))
564 continue;
565 break;
566 }
567 // If we made it to the end, that's a match.
568 if (!*p) {
569 ret = 1;
570 goto done;
571 }
Rob Landleye9190962005-12-12 19:38:44 +0000572 }
573 }
574 }
Rob Landleyd7605602006-06-14 01:51:16 +0000575done:
Rob Landleye9190962005-12-12 19:38:44 +0000576 close (fd);
Mike Frysinger135cee32006-06-21 23:03:37 +0000577 return ret;
Rob Landleye9190962005-12-12 19:38:44 +0000578}
579
Denis Vlasenkocf704332006-10-27 15:12:50 +0000580static int mod_process(struct mod_list_t *list, int do_insert)
Eric Andersen60e56f52002-04-26 06:04:01 +0000581{
Eric Andersen44b57582004-08-03 08:23:33 +0000582 int rc = 0;
Rob Landley79e1cab2005-11-15 00:08:29 +0000583 char **argv = NULL;
Rob Landleye9190962005-12-12 19:38:44 +0000584 struct mod_opt_t *opts;
585 int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */
Paul Fox8eeb6552005-08-04 18:33:36 +0000586 int argc;
Eric Andersen864b7972002-05-03 15:48:26 +0000587
Denis Vlasenkocf704332006-10-27 15:12:50 +0000588 while (list) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000589 argc = 0;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000590 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleye9190962005-12-12 19:38:44 +0000591 argc_malloc = 0;
592 /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory
593 * each time we allocate memory for argv.
594 * But it is (quite) small amounts of memory that leak each
595 * time a module is loaded, and it is reclaimed when modprobe
596 * exits anyway (even when standalone shell?).
597 * This could become a problem when loading a module with LOTS of
598 * dependencies, with LOTS of options for each dependencies, with
599 * very little memory on the target... But in that case, the module
600 * would not load because there is no more memory, so there's no
601 * problem. */
602 /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000603 argv = xmalloc(6 * sizeof(char*));
604 if (do_insert) {
605 if (already_loaded(list->m_name) != 1) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000606 argv[argc++] = "insmod";
Rob Landleyd7605602006-06-14 01:51:16 +0000607 if (ENABLE_FEATURE_2_4_MODULES) {
608 if (do_syslog)
609 argv[argc++] = "-s";
610 if (autoclean)
611 argv[argc++] = "-k";
612 if (quiet)
613 argv[argc++] = "-q";
Denis Vlasenkocf704332006-10-27 15:12:50 +0000614 else if (verbose) /* verbose and quiet are mutually exclusive */
Rob Landleyd7605602006-06-14 01:51:16 +0000615 argv[argc++] = "-v";
616 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000617 argv[argc++] = list->m_path;
618 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleye9190962005-12-12 19:38:44 +0000619 argc_malloc = argc;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000620 opts = list->m_options;
621 while (opts) {
Rob Landleye9190962005-12-12 19:38:44 +0000622 /* Add one more option */
Rob Landley79e1cab2005-11-15 00:08:29 +0000623 argc++;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000624 argv = xrealloc(argv,(argc + 1)* sizeof(char*));
625 argv[argc-1] = opts->m_opt_val;
626 opts = opts->m_next;
Rob Landley79e1cab2005-11-15 00:08:29 +0000627 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000628 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000629 } else {
Eric Andersen807bd842004-08-19 18:30:31 +0000630 /* modutils uses short name for removal */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000631 if (already_loaded(list->m_name) != 0) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000632 argv[argc++] = "rmmod";
633 if (do_syslog)
634 argv[argc++] = "-s";
635 argv[argc++] = list->m_name;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000636 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleye9190962005-12-12 19:38:44 +0000637 argc_malloc = argc;
Paul Fox8eeb6552005-08-04 18:33:36 +0000638 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000639 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000640 argv[argc] = NULL;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000641
Paul Fox8eeb6552005-08-04 18:33:36 +0000642 if (argc) {
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000643 if (verbose) {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000644 printf("%s module %s\n", do_insert?"Loading":"Unloading", list->m_name);
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000645 }
646 if (!show_only) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000647 int rc2 = wait4pid(spawn(argv));
Rob Landley4b5827a2006-08-22 23:50:11 +0000648
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000649 if (do_insert) {
650 rc = rc2; /* only last module matters */
651 }
652 else if (!rc2) {
653 rc = 0; /* success if remove any mod */
654 }
655 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000656 if (ENABLE_FEATURE_CLEAN_UP) {
Rob Landleye9190962005-12-12 19:38:44 +0000657 /* the last value in the array has index == argc, but
658 * it is the terminating NULL, so we must not free it. */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000659 while (argc_malloc < argc) {
660 free(argv[argc_malloc++]);
661 }
Rob Landley79e1cab2005-11-15 00:08:29 +0000662 }
Eric Andersen908e3622003-06-20 09:56:37 +0000663 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000664 if (ENABLE_FEATURE_CLEAN_UP) {
665 free(argv);
Rob Landleye9190962005-12-12 19:38:44 +0000666 argv = NULL;
Rob Landley79e1cab2005-11-15 00:08:29 +0000667 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000668 list = do_insert ? list->m_prev : list->m_next;
Eric Andersen60e56f52002-04-26 06:04:01 +0000669 }
Eric Andersen908e3622003-06-20 09:56:37 +0000670 return (show_only) ? 0 : rc;
Eric Andersen60e56f52002-04-26 06:04:01 +0000671}
672
Rob Landleye9190962005-12-12 19:38:44 +0000673/*
Rob Landleybf30c692006-07-20 17:36:18 +0000674 * Check the matching between a pattern and a module name.
675 * We need this as *_* is equivalent to *-*, even in pattern matching.
676 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000677static int check_pattern(const char* pat_src, const char* mod_src) {
Rob Landleybf30c692006-07-20 17:36:18 +0000678 int ret;
679
680 if (ENABLE_FEATURE_MODPROBE_FANCY_ALIAS) {
681 char* pat;
682 char* mod;
683 char* p;
684
Rob Landleyd921b2e2006-08-03 15:41:12 +0000685 pat = xstrdup (pat_src);
686 mod = xstrdup (mod_src);
Rob Landleybf30c692006-07-20 17:36:18 +0000687
Denis Vlasenkocf704332006-10-27 15:12:50 +0000688 for (p = pat; (p = strchr(p, '-')); *p++ = '_');
689 for (p = mod; (p = strchr(p, '-')); *p++ = '_');
Rob Landleybf30c692006-07-20 17:36:18 +0000690
Denis Vlasenkocf704332006-10-27 15:12:50 +0000691 ret = fnmatch(pat, mod, 0);
Rob Landleybf30c692006-07-20 17:36:18 +0000692
693 if (ENABLE_FEATURE_CLEAN_UP) {
694 free (pat);
695 free (mod);
696 }
697
698 return ret;
699 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000700 return fnmatch(pat_src, mod_src, 0);
Rob Landleybf30c692006-07-20 17:36:18 +0000701 }
702}
703
704/*
Rob Landleye9190962005-12-12 19:38:44 +0000705 * Builds the dependency list (aka stack) of a module.
706 * head: the highest module in the stack (last to insmod, first to rmmod)
707 * tail: the lowest module in the stack (first to insmod, last to rmmod)
708 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000709static void check_dep(char *mod, struct mod_list_t **head, struct mod_list_t **tail)
Eric Andersen60e56f52002-04-26 06:04:01 +0000710{
Robert Griebl52e8d062002-05-14 23:42:08 +0000711 struct mod_list_t *find;
Eric Andersen60e56f52002-04-26 06:04:01 +0000712 struct dep_t *dt;
Rob Landleye9190962005-12-12 19:38:44 +0000713 struct mod_opt_t *opt = 0;
Eric Andersen807bd842004-08-19 18:30:31 +0000714 char *path = 0;
Robert Griebl52e8d062002-05-14 23:42:08 +0000715
Bernhard Reutner-Fischer2c351a82006-06-03 19:08:49 +0000716 /* Search for the given module name amongst all dependency rules.
717 * The module name in a dependency rule can be a shell pattern,
718 * so try to match the given module name against such a pattern.
719 * Of course if the name in the dependency rule is a plain string,
720 * then we consider it a pattern, and matching will still work. */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000721 for (dt = depend; dt; dt = dt->m_next) {
722 if (check_pattern(dt->m_name, mod) == 0) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000723 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000724 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000725 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000726
Denis Vlasenkocf704332006-10-27 15:12:50 +0000727 if (!dt) {
728 bb_error_msg("module %s not found", mod);
Rob Landleye9190962005-12-12 19:38:44 +0000729 return;
730 }
731
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000732 // resolve alias names
Denis Vlasenkocf704332006-10-27 15:12:50 +0000733 while (dt->m_isalias) {
734 if (dt->m_depcnt == 1) {
Robert Grieblaead70b2002-07-26 15:54:20 +0000735 struct dep_t *adt;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000736
Denis Vlasenkocf704332006-10-27 15:12:50 +0000737 for (adt = depend; adt; adt = adt->m_next) {
738 if (check_pattern(adt->m_name, dt->m_deparr[0]) == 0)
Robert Grieblaead70b2002-07-26 15:54:20 +0000739 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000740 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000741 if (adt) {
Rob Landleye9190962005-12-12 19:38:44 +0000742 /* This is the module we are aliased to */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000743 struct mod_opt_t *opts = dt->m_options;
Rob Landleye9190962005-12-12 19:38:44 +0000744 /* Option of the alias are appended to the options of the module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000745 while (opts) {
746 adt->m_options = append_option(adt->m_options, opts->m_opt_val);
747 opts = opts->m_next;
Rob Landleye9190962005-12-12 19:38:44 +0000748 }
Eric Andersen716ccb22004-01-10 11:29:31 +0000749 dt = adt;
Robert Griebl70112da2002-07-29 20:28:38 +0000750 }
Rob Landleye9190962005-12-12 19:38:44 +0000751 else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000752 bb_error_msg("module %s not found", mod);
Robert Griebl70112da2002-07-29 20:28:38 +0000753 return;
Rob Landleye9190962005-12-12 19:38:44 +0000754 }
Robert Grieblaead70b2002-07-26 15:54:20 +0000755 }
Rob Landleye9190962005-12-12 19:38:44 +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{
813 struct mod_list_t *tail = 0;
Eric Andersen716ccb22004-01-10 11:29:31 +0000814 struct mod_list_t *head = 0;
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 Vlasenkocf704332006-10-27 15:12:50 +0000820 if (head && tail) {
821 if (argc) {
Eric Andersen716ccb22004-01-10 11:29:31 +0000822 int i;
Robert Grieblaead70b2002-07-26 15:54:20 +0000823 // append module args
Denis Vlasenkocf704332006-10-27 15:12:50 +0000824 for (i = 0; i < argc; i++)
825 head->m_options = append_option(head->m_options, argv[i]);
Robert Griebl52e8d062002-05-14 23:42:08 +0000826 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000827
Robert Griebl52e8d062002-05-14 23:42:08 +0000828 // process tail ---> head
Denis Vlasenkocf704332006-10-27 15:12:50 +0000829 if ((rc = mod_process(tail, 1)) != 0) {
Rob Landley4b5827a2006-08-22 23:50:11 +0000830 /*
831 * In case of using udev, multiple instances of modprobe can be
832 * spawned to load the same module (think of two same usb devices,
833 * for example; or cold-plugging at boot time). Thus we shouldn't
834 * fail if the module was loaded, and not by us.
835 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000836 if (already_loaded(mod))
Rob Landley4b5827a2006-08-22 23:50:11 +0000837 rc = 0;
838 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000839 }
840 else
841 rc = 1;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000842
Robert Griebl52e8d062002-05-14 23:42:08 +0000843 return rc;
844}
845
Denis Vlasenkocf704332006-10-27 15:12:50 +0000846static int mod_remove(char *mod)
Robert Griebl52e8d062002-05-14 23:42:08 +0000847{
Eric Andersen908e3622003-06-20 09:56:37 +0000848 int rc;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000849 static struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL };
Eric Andersen3b1a7442003-12-24 20:30:45 +0000850
Robert Griebl52e8d062002-05-14 23:42:08 +0000851 struct mod_list_t *head = 0;
852 struct mod_list_t *tail = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000853
Denis Vlasenkocf704332006-10-27 15:12:50 +0000854 if (mod)
855 check_dep(mod, &head, &tail);
Robert Griebl52e8d062002-05-14 23:42:08 +0000856 else // autoclean
857 head = tail = &rm_a_dummy;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000858
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 else
862 rc = 1;
863 return rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000864
Robert Griebl52e8d062002-05-14 23:42:08 +0000865}
866
Rob Landleydfba7412006-03-06 20:47:33 +0000867int modprobe_main(int argc, char** argv)
Eric Andersen0139ca92001-07-22 23:01:03 +0000868{
Rob Landleye9190962005-12-12 19:38:44 +0000869 int rc = EXIT_SUCCESS;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000870 char *unused;
Eric Andersen0139ca92001-07-22 23:01:03 +0000871
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000872 opt_complementary = "?V-:q-v:v-q";
873 main_opts = getopt32(argc, argv, "acdklnqrst:vVC:",
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000874 &unused, &unused);
Denis Vlasenkocf704332006-10-27 15:12:50 +0000875 if (main_opts & (DUMP_CONF_EXIT | LIST_ALL))
876 return EXIT_SUCCESS;
877 if (main_opts & (RESTRICT_DIR | CONFIG_FILE))
878 bb_error_msg_and_die("-t and -C not supported");
Robert Griebl236abbf2002-05-22 23:34:35 +0000879
Denis Vlasenkocf704332006-10-27 15:12:50 +0000880 depend = build_dep();
Robert Griebl52e8d062002-05-14 23:42:08 +0000881
Denis Vlasenkocf704332006-10-27 15:12:50 +0000882 if (!depend)
883 bb_error_msg_and_die("cannot parse modules.dep");
Eric Andersen3b1a7442003-12-24 20:30:45 +0000884
Eric Andersen1b064192001-07-25 07:23:38 +0000885 if (remove_opt) {
Eric Andersen0139ca92001-07-22 23:01:03 +0000886 do {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000887 if (mod_remove(optind < argc ?
888 argv[optind] : NULL)) {
889 bb_error_msg("failed to remove module %s",
890 argv[optind]);
Eric Andersen908e3622003-06-20 09:56:37 +0000891 rc = EXIT_FAILURE;
892 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000893 } while (++optind < argc);
Rob Landleye9190962005-12-12 19:38:44 +0000894 } else {
895 if (optind >= argc)
Denis Vlasenkocf704332006-10-27 15:12:50 +0000896 bb_error_msg_and_die("no module or pattern provided");
Eric Andersen3b1a7442003-12-24 20:30:45 +0000897
Denis Vlasenkocf704332006-10-27 15:12:50 +0000898 if (mod_insert(argv[optind], argc - optind - 1, argv + optind + 1))
899 bb_error_msg_and_die("failed to load module %s", argv[optind]);
Eric Andersen0139ca92001-07-22 23:01:03 +0000900 }
901
Rob Landleye9190962005-12-12 19:38:44 +0000902 /* Here would be a good place to free up memory allocated during the dependencies build. */
Eric Andersen3b1a7442003-12-24 20:30:45 +0000903
Rob Landleye9190962005-12-12 19:38:44 +0000904 return rc;
Eric Andersen0139ca92001-07-22 23:01:03 +0000905}