blob: bc2dbd12848151d9ba4e81f2f095f5443d87e1ab [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
Denis Vlasenkob68979a2007-11-02 23:31:10 +000018#define line_buffer bb_common_bufsiz1
19
Rob Landleye9190962005-12-12 19:38:44 +000020struct mod_opt_t { /* one-way list of options to pass to a module */
21 char * m_opt_val;
22 struct mod_opt_t * m_next;
Eric Andersen60e56f52002-04-26 06:04:01 +000023};
24
Rob Landleye9190962005-12-12 19:38:44 +000025struct dep_t { /* one-way list of dependency rules */
26 /* a dependency rule */
Denis Vlasenkocf704332006-10-27 15:12:50 +000027 char * m_name; /* the module name*/
28 char * m_path; /* the module file path */
29 struct mod_opt_t * m_options; /* the module options */
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +000030
Denis Vlasenkocf704332006-10-27 15:12:50 +000031 int m_isalias : 1; /* the module is an alias */
32 int m_reserved : 15; /* stuffin' */
Rob Landleye9190962005-12-12 19:38:44 +000033
Denis Vlasenkocf704332006-10-27 15:12:50 +000034 int m_depcnt : 16; /* the number of dependable module(s) */
35 char ** m_deparr; /* the list of dependable module(s) */
Rob Landleye9190962005-12-12 19:38:44 +000036
Denis Vlasenkocf704332006-10-27 15:12:50 +000037 struct dep_t * m_next; /* the next dependency rule */
Rob Landleye9190962005-12-12 19:38:44 +000038};
39
40struct mod_list_t { /* two-way list of modules to process */
41 /* a module description */
Denis Vlasenko322661d2007-01-29 23:43:52 +000042 const char * m_name;
43 char * m_path;
44 struct mod_opt_t * m_options;
Eric Andersen3b1a7442003-12-24 20:30:45 +000045
Robert Griebl52e8d062002-05-14 23:42:08 +000046 struct mod_list_t * m_prev;
47 struct mod_list_t * m_next;
48};
49
50
Robert Griebl236abbf2002-05-22 23:34:35 +000051static struct dep_t *depend;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +000052
Denis Vlasenkob68979a2007-11-02 23:31:10 +000053#define MAIN_OPT_STR "acdklnqrst:vVC:"
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +000054#define INSERT_ALL 1 /* a */
55#define DUMP_CONF_EXIT 2 /* c */
56#define D_OPT_IGNORED 4 /* d */
57#define AUTOCLEAN_FLG 8 /* k */
58#define LIST_ALL 16 /* l */
59#define SHOW_ONLY 32 /* n */
60#define QUIET 64 /* q */
61#define REMOVE_OPT 128 /* r */
62#define DO_SYSLOG 256 /* s */
63#define RESTRICT_DIR 512 /* t */
64#define VERBOSE 1024 /* v */
65#define VERSION_ONLY 2048 /* V */
66#define CONFIG_FILE 4096 /* C */
67
Denis Vlasenkob68979a2007-11-02 23:31:10 +000068#define autoclean (option_mask32 & AUTOCLEAN_FLG)
69#define show_only (option_mask32 & SHOW_ONLY)
70#define quiet (option_mask32 & QUIET)
71#define remove_opt (option_mask32 & REMOVE_OPT)
72#define do_syslog (option_mask32 & DO_SYSLOG)
73#define verbose (option_mask32 & VERBOSE)
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 Vlasenkob68979a2007-11-02 23:31:10 +000081 while (!isspace(*value)) {
82 if (!*value)
83 return 0;
84 value++;
85 }
86 *value++ = '\0';
Denis Vlasenkocf704332006-10-27 15:12:50 +000087 value = skip_whitespace(value);
Denis Vlasenkob68979a2007-11-02 23:31:10 +000088 if (!*value)
89 return 0;
Robert Grieblaead70b2002-07-26 15:54:20 +000090
91 *ptag = tag;
92 *pvalue = value;
Eric Andersen3b1a7442003-12-24 20:30:45 +000093
Eric Andersen7e496a72004-04-06 12:06:03 +000094 return 1;
Robert Grieblaead70b2002-07-26 15:54:20 +000095}
Eric Andersen60e56f52002-04-26 06:04:01 +000096
Rob Landleye9190962005-12-12 19:38:44 +000097/*
98 * This function appends an option to a list
99 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000100static struct mod_opt_t *append_option(struct mod_opt_t *opt_list, char *opt)
Eric Andersen60e56f52002-04-26 06:04:01 +0000101{
Rob Landleye9190962005-12-12 19:38:44 +0000102 struct mod_opt_t *ol = opt_list;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000103
Denis Vlasenkocf704332006-10-27 15:12:50 +0000104 if (ol) {
105 while (ol->m_next) {
106 ol = ol->m_next;
Rob Landleye9190962005-12-12 19:38:44 +0000107 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000108 ol->m_next = xzalloc(sizeof(struct mod_opt_t));
Denis Vlasenkocf704332006-10-27 15:12:50 +0000109 ol = ol->m_next;
Rob Landleye9190962005-12-12 19:38:44 +0000110 } else {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000111 ol = opt_list = xzalloc(sizeof(struct mod_opt_t));
Eric Andersen03d80912003-12-19 21:04:19 +0000112 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000113
Denis Vlasenkocf704332006-10-27 15:12:50 +0000114 ol->m_opt_val = xstrdup(opt);
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000115 /*ol->m_next = NULL; - done by xzalloc*/
Eric Andersen60e56f52002-04-26 06:04:01 +0000116
Rob Landleye9190962005-12-12 19:38:44 +0000117 return opt_list;
Eric Andersen60e56f52002-04-26 06:04:01 +0000118}
119
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000120#if ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS
Denis Vlasenkocf704332006-10-27 15:12:50 +0000121/* static char* parse_command_string(char* src, char **dst);
Rob Landley79e1cab2005-11-15 00:08:29 +0000122 * src: pointer to string containing argument
123 * dst: pointer to where to store the parsed argument
124 * return value: the pointer to the first char after the parsed argument,
125 * NULL if there was no argument parsed (only trailing spaces).
Rob Landleyd921b2e2006-08-03 15:41:12 +0000126 * Note that memory is allocated with xstrdup when a new argument was
Rob Landley79e1cab2005-11-15 00:08:29 +0000127 * parsed. Don't forget to free it!
128 */
129#define ARG_EMPTY 0x00
130#define ARG_IN_DQUOTES 0x01
131#define ARG_IN_SQUOTES 0x02
Denis Vlasenkocf704332006-10-27 15:12:50 +0000132static char *parse_command_string(char *src, char **dst)
Rob Landley79e1cab2005-11-15 00:08:29 +0000133{
134 int opt_status = ARG_EMPTY;
135 char* tmp_str;
136
137 /* Dumb you, I have nothing to do... */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000138 if (src == NULL) return src;
Rob Landley79e1cab2005-11-15 00:08:29 +0000139
140 /* Skip leading spaces */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000141 while (*src == ' ') {
Rob Landley79e1cab2005-11-15 00:08:29 +0000142 src++;
143 }
144 /* Is the end of string reached? */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000145 if (*src == '\0') {
Rob Landley79e1cab2005-11-15 00:08:29 +0000146 return NULL;
147 }
148 /* Reached the start of an argument
Rob Landleye9190962005-12-12 19:38:44 +0000149 * By the way, we duplicate a little too much
150 * here but what is too much is freed later. */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000151 *dst = tmp_str = xstrdup(src);
Rob Landley79e1cab2005-11-15 00:08:29 +0000152 /* Get to the end of that argument */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000153 while (*tmp_str != '\0'
154 && (*tmp_str != ' ' || (opt_status & (ARG_IN_DQUOTES | ARG_IN_SQUOTES)))
155 ) {
156 switch (*tmp_str) {
157 case '\'':
158 if (opt_status & ARG_IN_DQUOTES) {
159 /* Already in double quotes, keep current char as is */
160 } else {
161 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
162 memmove(tmp_str, tmp_str + 1, strlen(tmp_str));
163 /* mark me: we enter or leave single quotes */
164 opt_status ^= ARG_IN_SQUOTES;
165 /* Back one char, as we need to re-scan the new char there. */
166 tmp_str--;
167 }
Rob Landley79e1cab2005-11-15 00:08:29 +0000168 break;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000169 case '"':
170 if (opt_status & ARG_IN_SQUOTES) {
171 /* Already in single quotes, keep current char as is */
172 } else {
173 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
174 memmove(tmp_str, tmp_str + 1, strlen(tmp_str));
175 /* mark me: we enter or leave double quotes */
176 opt_status ^= ARG_IN_DQUOTES;
177 /* Back one char, as we need to re-scan the new char there. */
178 tmp_str--;
179 }
Rob Landley79e1cab2005-11-15 00:08:29 +0000180 break;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000181 case '\\':
182 if (opt_status & ARG_IN_SQUOTES) {
183 /* Between single quotes: keep as is. */
184 } else {
185 switch (*(tmp_str+1)) {
186 case 'a':
187 case 'b':
188 case 't':
189 case 'n':
190 case 'v':
191 case 'f':
192 case 'r':
193 case '0':
194 /* We escaped a special character. For now, keep
195 * both the back-slash and the following char. */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000196 tmp_str++;
197 src++;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000198 break;
199 default:
200 /* We escaped a space or a single or double quote,
201 * or a back-slash, or a non-escapable char. Remove
202 * the '\' and keep the new current char as is. */
203 memmove(tmp_str, tmp_str + 1, strlen(tmp_str));
204 break;
Rob Landley79e1cab2005-11-15 00:08:29 +0000205 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000206 }
Rob Landley79e1cab2005-11-15 00:08:29 +0000207 break;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000208 /* Any other char that is special shall appear here.
209 * Example: $ starts a variable
210 case '$':
211 do_variable_expansion();
212 break;
213 * */
214 default:
215 /* any other char is kept as is. */
216 break;
Rob Landley79e1cab2005-11-15 00:08:29 +0000217 }
218 tmp_str++; /* Go to next char */
219 src++; /* Go to next char to find the end of the argument. */
220 }
221 /* End of string, but still no ending quote */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000222 if (opt_status & (ARG_IN_DQUOTES | ARG_IN_SQUOTES)) {
223 bb_error_msg_and_die("unterminated (single or double) quote in options list: %s", src);
Rob Landley79e1cab2005-11-15 00:08:29 +0000224 }
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000225 *tmp_str++ = '\0';
Denis Vlasenkocf704332006-10-27 15:12:50 +0000226 *dst = xrealloc(*dst, (tmp_str - *dst));
Rob Landley79e1cab2005-11-15 00:08:29 +0000227 return src;
228}
Rob Landleyae50c6d2005-12-15 07:42:13 +0000229#else
230#define parse_command_string(src, dst) (0)
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000231#endif /* ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
Rob Landley79e1cab2005-11-15 00:08:29 +0000232
Rob Landleye9190962005-12-12 19:38:44 +0000233/*
Rob Landley72615752006-04-10 16:09:52 +0000234 * This function reads aliases and default module options from a configuration file
235 * (/etc/modprobe.conf syntax). It supports includes (only files, no directories).
236 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000237static void include_conf(struct dep_t **first, struct dep_t **current, char *buffer, int buflen, int fd)
Rob Landley72615752006-04-10 16:09:52 +0000238{
239 int continuation_line = 0;
240
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000241 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias)!
Rob Landley72615752006-04-10 16:09:52 +0000242
Denis Vlasenkocf704332006-10-27 15:12:50 +0000243 while (reads(fd, buffer, buflen)) {
Rob Landley72615752006-04-10 16:09:52 +0000244 int l;
245 char *p;
246
Denis Vlasenkocf704332006-10-27 15:12:50 +0000247 p = strchr(buffer, '#');
248 if (p)
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000249 *p = '\0';
Rob Landley72615752006-04-10 16:09:52 +0000250
Denis Vlasenkocf704332006-10-27 15:12:50 +0000251 l = strlen(buffer);
Rob Landley72615752006-04-10 16:09:52 +0000252
Denis Vlasenkocf704332006-10-27 15:12:50 +0000253 while (l && isspace(buffer[l-1])) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000254 buffer[l-1] = '\0';
Rob Landley72615752006-04-10 16:09:52 +0000255 l--;
256 }
257
Denis Vlasenkocf704332006-10-27 15:12:50 +0000258 if (l == 0) {
Rob Landley72615752006-04-10 16:09:52 +0000259 continuation_line = 0;
260 continue;
261 }
262
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000263 if (continuation_line)
264 continue;
Rob Landley72615752006-04-10 16:09:52 +0000265
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000266 if ((strncmp(buffer, "alias", 5) == 0) && isspace(buffer[5])) {
267 char *alias, *mod;
Rob Landley72615752006-04-10 16:09:52 +0000268
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000269 if (parse_tag_value(buffer + 6, &alias, &mod)) {
270 /* handle alias as a module dependent on the aliased module */
271 if (!*current) {
272 (*first) = (*current) = xzalloc(sizeof(struct dep_t));
273 } else {
274 (*current)->m_next = xzalloc(sizeof(struct dep_t));
275 (*current) = (*current)->m_next;
Rob Landley72615752006-04-10 16:09:52 +0000276 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000277 (*current)->m_name = xstrdup(alias);
278 (*current)->m_isalias = 1;
Rob Landley72615752006-04-10 16:09:52 +0000279
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000280 if ((strcmp(mod, "off") == 0) || (strcmp(mod, "null") == 0)) {
281 /*(*current)->m_depcnt = 0; - done by xzalloc */
282 /*(*current)->m_deparr = 0;*/
283 } else {
284 (*current)->m_depcnt = 1;
285 (*current)->m_deparr = xmalloc(sizeof(char *));
286 (*current)->m_deparr[0] = xstrdup(mod);
287 }
288 /*(*current)->m_next = NULL; - done by xzalloc */
289 }
290 } else if ((strncmp(buffer, "options", 7) == 0) && isspace(buffer[7])) {
291 char *mod, *opt;
Rob Landley72615752006-04-10 16:09:52 +0000292
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000293 /* split the line in the module/alias name, and options */
294 if (parse_tag_value(buffer + 8, &mod, &opt)) {
295 struct dep_t *dt;
296
297 /* find the corresponding module */
298 for (dt = *first; dt; dt = dt->m_next) {
299 if (strcmp(dt->m_name, mod) == 0)
300 break;
301 }
302 if (dt) {
303 if (ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS) {
304 char* new_opt = NULL;
305 while ((opt = parse_command_string(opt, &new_opt))) {
306 dt->m_options = append_option(dt->m_options, new_opt);
Rob Landley72615752006-04-10 16:09:52 +0000307 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000308 } else {
309 dt->m_options = append_option(dt->m_options, opt);
Rob Landley72615752006-04-10 16:09:52 +0000310 }
311 }
Rob Landley72615752006-04-10 16:09:52 +0000312 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000313 } else if ((strncmp(buffer, "include", 7) == 0) && isspace(buffer[7])) {
314 int fdi;
315 char *filename;
316
317 filename = skip_whitespace(buffer + 8);
318 fdi = open(filename, O_RDONLY);
319 if (fdi >= 0) {
320 include_conf(first, current, buffer, buflen, fdi);
321 close(fdi);
322 }
Rob Landley72615752006-04-10 16:09:52 +0000323 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000324 } /* while (reads(...)) */
Rob Landley72615752006-04-10 16:09:52 +0000325}
326
327/*
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000328 * This function builds a list of dependency rules from /lib/modules/`uname -r`/modules.dep.
Bernhard Reutner-Fischere3c150b2006-05-19 11:24:28 +0000329 * It then fills every modules and aliases with their default options, found by parsing
Rob Landleye9190962005-12-12 19:38:44 +0000330 * modprobe.conf (or modules.conf, or conf.modules).
331 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000332static struct dep_t *build_dep(void)
Rob Landleye9190962005-12-12 19:38:44 +0000333{
334 int fd;
335 struct utsname un;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000336 struct dep_t *first = NULL;
337 struct dep_t *current = NULL;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000338 char *filename;
Rob Landleye9190962005-12-12 19:38:44 +0000339 int continuation_line = 0;
340 int k_version;
341
Denis Vlasenkocf704332006-10-27 15:12:50 +0000342 if (uname(&un))
Rob Landleye9190962005-12-12 19:38:44 +0000343 bb_error_msg_and_die("can't determine kernel version");
344
Denis Vlasenkocf704332006-10-27 15:12:50 +0000345 k_version = 0;
Rob Landleye9190962005-12-12 19:38:44 +0000346 if (un.release[0] == '2') {
347 k_version = un.release[2] - '0';
348 }
349
Denis Vlasenkocf704332006-10-27 15:12:50 +0000350 filename = xasprintf("/lib/modules/%s/modules.dep", un.release);
351 fd = open(filename, O_RDONLY);
Bernhard Reutner-Fischere3c150b2006-05-19 11:24:28 +0000352 if (ENABLE_FEATURE_CLEAN_UP)
353 free(filename);
Rob Landley3afb0702006-05-18 20:41:43 +0000354 if (fd < 0) {
Rob Landleye9190962005-12-12 19:38:44 +0000355 /* Ok, that didn't work. Fall back to looking in /lib/modules */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000356 fd = open("/lib/modules/modules.dep", O_RDONLY);
357 if (fd < 0) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000358 bb_error_msg_and_die("cannot parse modules.dep");
Rob Landleye9190962005-12-12 19:38:44 +0000359 }
360 }
361
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000362 while (reads(fd, line_buffer, sizeof(line_buffer))) {
363 int l = strlen(line_buffer);
Rob Landleye9190962005-12-12 19:38:44 +0000364 char *p = 0;
365
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000366 while (l > 0 && isspace(line_buffer[l-1])) {
367 line_buffer[l-1] = '\0';
Rob Landleye9190962005-12-12 19:38:44 +0000368 l--;
369 }
370
Denis Vlasenkocf704332006-10-27 15:12:50 +0000371 if (l == 0) {
Rob Landleye9190962005-12-12 19:38:44 +0000372 continuation_line = 0;
373 continue;
374 }
375
376 /* Is this a new module dep description? */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000377 if (!continuation_line) {
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000378 /* find the dep beginning */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000379 char *col = strchr(line_buffer, ':');
Rob Landleye9190962005-12-12 19:38:44 +0000380 char *dot = col;
381
Denis Vlasenkocf704332006-10-27 15:12:50 +0000382 if (col) {
Rob Landleye9190962005-12-12 19:38:44 +0000383 /* This line is a dep description */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000384 const char *mods;
Rob Landleye9190962005-12-12 19:38:44 +0000385 char *modpath;
386 char *mod;
387
388 /* Find the beginning of the module file name */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000389 *col = '\0';
390 mods = bb_basename(line_buffer);
Rob Landleye9190962005-12-12 19:38:44 +0000391
392 /* find the path of the module */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000393 modpath = strchr(line_buffer, '/'); /* ... and this is the path */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000394 if (!modpath)
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000395 modpath = line_buffer; /* module with no path */
Rob Landleye9190962005-12-12 19:38:44 +0000396 /* 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 &&
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000398 (k_version > 4) && (col[-3] == '.') &&
399 (col[-2] == 'k') && (col[-1] == 'o'))
Rob Landleye9190962005-12-12 19:38:44 +0000400 dot = col - 3;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000401 else if ((col[-2] == '.') && (col[-1] == 'o'))
402 dot = col - 2;
Rob Landleye9190962005-12-12 19:38:44 +0000403
Denis Vlasenkocf704332006-10-27 15:12:50 +0000404 mod = xstrndup(mods, dot - mods);
Rob Landleye9190962005-12-12 19:38:44 +0000405
406 /* enqueue new module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000407 if (!current) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000408 first = current = xzalloc(sizeof(struct dep_t));
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000409 } else {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000410 current->m_next = xzalloc(sizeof(struct dep_t));
Denis Vlasenkocf704332006-10-27 15:12:50 +0000411 current = current->m_next;
Rob Landleye9190962005-12-12 19:38:44 +0000412 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000413 current->m_name = mod;
414 current->m_path = xstrdup(modpath);
415 /*current->m_options = NULL; - xzalloc did it*/
416 /*current->m_isalias = 0;*/
417 /*current->m_depcnt = 0;*/
418 /*current->m_deparr = 0;*/
419 /*current->m_next = 0;*/
Rob Landleye9190962005-12-12 19:38:44 +0000420
421 p = col + 1;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000422 } else
Rob Landleye9190962005-12-12 19:38:44 +0000423 /* this line is not a dep description */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000424 p = NULL;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000425 } else
Rob Landleye9190962005-12-12 19:38:44 +0000426 /* It's a dep description continuation */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000427 p = line_buffer;
Rob Landleye9190962005-12-12 19:38:44 +0000428
Denis Vlasenkocf704332006-10-27 15:12:50 +0000429 while (p && *p && isblank(*p))
Rob Landleye9190962005-12-12 19:38:44 +0000430 p++;
431
432 /* p points to the first dependable module; if NULL, no dependable module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000433 if (p && *p) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000434 char *end = &line_buffer[l-1];
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000435 const char *deps;
Rob Landleye9190962005-12-12 19:38:44 +0000436 char *dep;
437 char *next;
438 int ext = 0;
439
Denis Vlasenkocf704332006-10-27 15:12:50 +0000440 while (isblank(*end) || (*end == '\\'))
Rob Landleye9190962005-12-12 19:38:44 +0000441 end--;
442
Denis Vlasenkocf704332006-10-27 15:12:50 +0000443 do {
Rob Landleye9190962005-12-12 19:38:44 +0000444 /* search the end of the dependency */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000445 next = strchr(p, ' ');
446 if (next) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000447 *next = '\0';
Rob Landleye9190962005-12-12 19:38:44 +0000448 next--;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000449 } else
Rob Landleye9190962005-12-12 19:38:44 +0000450 next = end;
451
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000452 /* find the beginning of the module file name */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000453 deps = bb_basename(p);
454 if (deps == p) {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000455 while (isblank(*deps))
Rob Landleye9190962005-12-12 19:38:44 +0000456 deps++;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000457 }
Rob Landleye9190962005-12-12 19:38:44 +0000458
459 /* find the end of the module name in the file name */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000460 if (ENABLE_FEATURE_2_6_MODULES
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000461 && (k_version > 4) && (next[-2] == '.')
462 && (next[-1] == 'k') && (next[0] == 'o'))
Rob Landleye9190962005-12-12 19:38:44 +0000463 ext = 3;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000464 else if ((next[-1] == '.') && (next[0] == 'o'))
465 ext = 2;
Rob Landleye9190962005-12-12 19:38:44 +0000466
467 /* Cope with blank lines */
468 if ((next-deps-ext+1) <= 0)
469 continue;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000470 dep = xstrndup(deps, next - deps - ext + 1);
Rob Landleye9190962005-12-12 19:38:44 +0000471
472 /* Add the new dependable module name */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000473 current->m_depcnt++;
474 current->m_deparr = xrealloc(current->m_deparr,
475 sizeof(char *) * current->m_depcnt);
476 current->m_deparr[current->m_depcnt - 1] = dep;
Rob Landleye9190962005-12-12 19:38:44 +0000477
478 p = next + 2;
479 } while (next < end);
480 }
481
482 /* is there other dependable module(s) ? */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000483 continuation_line = (line_buffer[l-1] == '\\');
484 } /* while (reads(...)) */
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 Vlasenkob68979a2007-11-02 23:31:10 +0000501 include_conf(&first, &current, line_buffer, sizeof(line_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 Vlasenkob68979a2007-11-02 23:31:10 +0000518 include_conf(&first, &current, line_buffer, sizeof(line_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) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000533 include_conf(&first, &current, line_buffer, sizeof(line_buffer), fd);
Denis Vlasenkof8483052007-08-16 10:40:06 +0000534 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
Denis Vlasenkocf704332006-10-27 15:12:50 +0000546 fd = open("/proc/modules", O_RDONLY);
Rob Landleye9190962005-12-12 19:38:44 +0000547 if (fd < 0)
548 return -1;
549
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000550 while (reads(fd, line_buffer, sizeof(line_buffer))) {
Rob Landleye9190962005-12-12 19:38:44 +0000551 char *p;
552
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000553 p = strchr(line_buffer, ' ');
Rob Landleye9190962005-12-12 19:38:44 +0000554 if (p) {
Rob Landleyd7605602006-06-14 01:51:16 +0000555 const char *n;
556
557 // Truncate buffer at first space and check for matches, with
558 // the idiosyncrasy that _ and - are interchangeable because the
559 // 2.6 kernel does weird things.
560
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000561 *p = '\0';
562 for (p = line_buffer, n = name; ; p++, n++) {
Rob Landleyd7605602006-06-14 01:51:16 +0000563 if (*p != *n) {
564 if ((*p == '_' || *p == '-') && (*n == '_' || *n == '-'))
565 continue;
566 break;
567 }
568 // If we made it to the end, that's a match.
569 if (!*p) {
570 ret = 1;
571 goto done;
572 }
Rob Landleye9190962005-12-12 19:38:44 +0000573 }
574 }
575 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000576 done:
577 close(fd);
Mike Frysinger135cee32006-06-21 23:03:37 +0000578 return ret;
Rob Landleye9190962005-12-12 19:38:44 +0000579}
580
Denis Vlasenko322661d2007-01-29 23:43:52 +0000581static int mod_process(const struct mod_list_t *list, int do_insert)
Eric Andersen60e56f52002-04-26 06:04:01 +0000582{
Eric Andersen44b57582004-08-03 08:23:33 +0000583 int rc = 0;
Rob Landley79e1cab2005-11-15 00:08:29 +0000584 char **argv = NULL;
Rob Landleye9190962005-12-12 19:38:44 +0000585 struct mod_opt_t *opts;
586 int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */
Paul Fox8eeb6552005-08-04 18:33:36 +0000587 int argc;
Eric Andersen864b7972002-05-03 15:48:26 +0000588
Denis Vlasenkocf704332006-10-27 15:12:50 +0000589 while (list) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000590 argc = 0;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000591 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleye9190962005-12-12 19:38:44 +0000592 argc_malloc = 0;
593 /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory
594 * each time we allocate memory for argv.
595 * But it is (quite) small amounts of memory that leak each
596 * time a module is loaded, and it is reclaimed when modprobe
597 * exits anyway (even when standalone shell?).
598 * This could become a problem when loading a module with LOTS of
599 * dependencies, with LOTS of options for each dependencies, with
600 * very little memory on the target... But in that case, the module
601 * would not load because there is no more memory, so there's no
602 * problem. */
603 /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000604 argv = xmalloc(6 * sizeof(char*));
605 if (do_insert) {
606 if (already_loaded(list->m_name) != 1) {
Denis Vlasenko322661d2007-01-29 23:43:52 +0000607 argv[argc++] = (char*)"insmod";
Rob Landleyd7605602006-06-14 01:51:16 +0000608 if (ENABLE_FEATURE_2_4_MODULES) {
609 if (do_syslog)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000610 argv[argc++] = (char*)"-s";
Rob Landleyd7605602006-06-14 01:51:16 +0000611 if (autoclean)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000612 argv[argc++] = (char*)"-k";
Rob Landleyd7605602006-06-14 01:51:16 +0000613 if (quiet)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000614 argv[argc++] = (char*)"-q";
Denis Vlasenkocf704332006-10-27 15:12:50 +0000615 else if (verbose) /* verbose and quiet are mutually exclusive */
Denis Vlasenko322661d2007-01-29 23:43:52 +0000616 argv[argc++] = (char*)"-v";
Rob Landleyd7605602006-06-14 01:51:16 +0000617 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000618 argv[argc++] = list->m_path;
619 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleye9190962005-12-12 19:38:44 +0000620 argc_malloc = argc;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000621 opts = list->m_options;
622 while (opts) {
Rob Landleye9190962005-12-12 19:38:44 +0000623 /* Add one more option */
Rob Landley79e1cab2005-11-15 00:08:29 +0000624 argc++;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000625 argv = xrealloc(argv, (argc + 1) * sizeof(char*));
Denis Vlasenkocf704332006-10-27 15:12:50 +0000626 argv[argc-1] = opts->m_opt_val;
627 opts = opts->m_next;
Rob Landley79e1cab2005-11-15 00:08:29 +0000628 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000629 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000630 } else {
Eric Andersen807bd842004-08-19 18:30:31 +0000631 /* modutils uses short name for removal */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000632 if (already_loaded(list->m_name) != 0) {
Denis Vlasenko322661d2007-01-29 23:43:52 +0000633 argv[argc++] = (char*)"rmmod";
Paul Fox8eeb6552005-08-04 18:33:36 +0000634 if (do_syslog)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000635 argv[argc++] = (char*)"-s";
636 argv[argc++] = (char*)list->m_name;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000637 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleye9190962005-12-12 19:38:44 +0000638 argc_malloc = argc;
Paul Fox8eeb6552005-08-04 18:33:36 +0000639 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000640 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000641 argv[argc] = NULL;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000642
Paul Fox8eeb6552005-08-04 18:33:36 +0000643 if (argc) {
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000644 if (verbose) {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000645 printf("%s module %s\n", do_insert?"Loading":"Unloading", list->m_name);
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000646 }
647 if (!show_only) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000648 int rc2 = wait4pid(spawn(argv));
Rob Landley4b5827a2006-08-22 23:50:11 +0000649
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000650 if (do_insert) {
651 rc = rc2; /* only last module matters */
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000652 } else if (!rc2) {
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000653 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 Vlasenkoac678ec2007-04-16 22:32:04 +0000677static int check_pattern(const char* pat_src, const char* mod_src)
678{
Rob Landleybf30c692006-07-20 17:36:18 +0000679 int ret;
680
681 if (ENABLE_FEATURE_MODPROBE_FANCY_ALIAS) {
682 char* pat;
683 char* mod;
684 char* p;
685
Denis Vlasenko6398cf42007-04-11 17:04:29 +0000686 pat = xstrdup(pat_src);
687 mod = xstrdup(mod_src);
Rob Landleybf30c692006-07-20 17:36:18 +0000688
Denis Vlasenkocf704332006-10-27 15:12:50 +0000689 for (p = pat; (p = strchr(p, '-')); *p++ = '_');
690 for (p = mod; (p = strchr(p, '-')); *p++ = '_');
Rob Landleybf30c692006-07-20 17:36:18 +0000691
Denis Vlasenkocf704332006-10-27 15:12:50 +0000692 ret = fnmatch(pat, mod, 0);
Rob Landleybf30c692006-07-20 17:36:18 +0000693
694 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkoff131b92007-04-10 15:42:06 +0000695 free(pat);
696 free(mod);
Rob Landleybf30c692006-07-20 17:36:18 +0000697 }
698
699 return ret;
Rob Landleybf30c692006-07-20 17:36:18 +0000700 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000701 return fnmatch(pat_src, mod_src, 0);
Rob Landleybf30c692006-07-20 17:36:18 +0000702}
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;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000713 struct mod_opt_t *opt = NULL;
714 char *path = NULL;
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;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000750 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000751 bb_error_msg("module %s not found", mod);
Robert Griebl70112da2002-07-29 20:28:38 +0000752 return;
Rob Landleye9190962005-12-12 19:38:44 +0000753 }
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000754 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000755 bb_error_msg("bad alias %s", dt->m_name);
Eric Andersen716ccb22004-01-10 11:29:31 +0000756 return;
Rob Landleye9190962005-12-12 19:38:44 +0000757 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000758 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000759
Denis Vlasenkocf704332006-10-27 15:12:50 +0000760 mod = dt->m_name;
761 path = dt->m_path;
762 opt = dt->m_options;
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000763
Robert Griebl52e8d062002-05-14 23:42:08 +0000764 // search for duplicates
Denis Vlasenkocf704332006-10-27 15:12:50 +0000765 for (find = *head; find; find = find->m_next) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000766 if (strcmp(mod, find->m_name) == 0) {
767 // found -> dequeue it
Robert Griebl52e8d062002-05-14 23:42:08 +0000768
Denis Vlasenkocf704332006-10-27 15:12:50 +0000769 if (find->m_prev)
770 find->m_prev->m_next = find->m_next;
Robert Griebl52e8d062002-05-14 23:42:08 +0000771 else
Denis Vlasenkocf704332006-10-27 15:12:50 +0000772 *head = find->m_next;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000773
Denis Vlasenkocf704332006-10-27 15:12:50 +0000774 if (find->m_next)
775 find->m_next->m_prev = find->m_prev;
Robert Griebl52e8d062002-05-14 23:42:08 +0000776 else
Denis Vlasenkocf704332006-10-27 15:12:50 +0000777 *tail = find->m_prev;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000778
Robert Griebl52e8d062002-05-14 23:42:08 +0000779 break; // there can be only one duplicate
Eric Andersen716ccb22004-01-10 11:29:31 +0000780 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000781 }
782
Denis Vlasenkocf704332006-10-27 15:12:50 +0000783 if (!find) { // did not find a duplicate
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000784 find = xzalloc(sizeof(struct mod_list_t));
Denis Vlasenkocf704332006-10-27 15:12:50 +0000785 find->m_name = mod;
786 find->m_path = path;
787 find->m_options = opt;
Robert Griebl52e8d062002-05-14 23:42:08 +0000788 }
789
Eric Andersen716ccb22004-01-10 11:29:31 +0000790 // enqueue at tail
Denis Vlasenkocf704332006-10-27 15:12:50 +0000791 if (*tail)
792 (*tail)->m_next = find;
793 find->m_prev = *tail;
Denis Vlasenkocb12cb22007-11-06 11:34:03 +0000794 find->m_next = NULL; /* possibly NOT done by xzalloc! */
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000795
Denis Vlasenkocf704332006-10-27 15:12:50 +0000796 if (!*head)
Robert Griebl52e8d062002-05-14 23:42:08 +0000797 *head = find;
798 *tail = find;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000799
Denis Vlasenkocf704332006-10-27 15:12:50 +0000800 if (dt) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000801 int i;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000802
Rob Landleye9190962005-12-12 19:38:44 +0000803 /* Add all dependable module for that new module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000804 for (i = 0; i < dt->m_depcnt; i++)
805 check_dep(dt->m_deparr[i], head, tail);
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000806 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000807}
808
Denis Vlasenkocf704332006-10-27 15:12:50 +0000809static int mod_insert(char *mod, int argc, char **argv)
Robert Griebl52e8d062002-05-14 23:42:08 +0000810{
Denis Vlasenko322661d2007-01-29 23:43:52 +0000811 struct mod_list_t *tail = NULL;
812 struct mod_list_t *head = NULL;
Eric Andersen908e3622003-06-20 09:56:37 +0000813 int rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000814
Robert Griebl52e8d062002-05-14 23:42:08 +0000815 // get dep list for module mod
Denis Vlasenkocf704332006-10-27 15:12:50 +0000816 check_dep(mod, &head, &tail);
Eric Andersen3b1a7442003-12-24 20:30:45 +0000817
Denis Vlasenko322661d2007-01-29 23:43:52 +0000818 rc = 1;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000819 if (head && tail) {
820 if (argc) {
Eric Andersen716ccb22004-01-10 11:29:31 +0000821 int i;
Robert Grieblaead70b2002-07-26 15:54:20 +0000822 // append module args
Denis Vlasenkocf704332006-10-27 15:12:50 +0000823 for (i = 0; i < argc; i++)
824 head->m_options = append_option(head->m_options, argv[i]);
Robert Griebl52e8d062002-05-14 23:42:08 +0000825 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000826
Robert Griebl52e8d062002-05-14 23:42:08 +0000827 // process tail ---> head
Denis Vlasenko322661d2007-01-29 23:43:52 +0000828 rc = mod_process(tail, 1);
829 if (rc) {
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 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000840 return rc;
841}
842
Denis Vlasenkocf704332006-10-27 15:12:50 +0000843static int mod_remove(char *mod)
Robert Griebl52e8d062002-05-14 23:42:08 +0000844{
Eric Andersen908e3622003-06-20 09:56:37 +0000845 int rc;
Denis Vlasenko322661d2007-01-29 23:43:52 +0000846 static const struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL };
Eric Andersen3b1a7442003-12-24 20:30:45 +0000847
Denis Vlasenko322661d2007-01-29 23:43:52 +0000848 struct mod_list_t *head = NULL;
849 struct mod_list_t *tail = NULL;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000850
Denis Vlasenkocf704332006-10-27 15:12:50 +0000851 if (mod)
852 check_dep(mod, &head, &tail);
Robert Griebl52e8d062002-05-14 23:42:08 +0000853 else // autoclean
Denis Vlasenko322661d2007-01-29 23:43:52 +0000854 head = tail = (struct mod_list_t*) &rm_a_dummy;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000855
Denis Vlasenko322661d2007-01-29 23:43:52 +0000856 rc = 1;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000857 if (head && tail)
858 rc = mod_process(head, 0); // process head ---> tail
Eric Andersen908e3622003-06-20 09:56:37 +0000859 return rc;
Robert Griebl52e8d062002-05-14 23:42:08 +0000860}
861
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000862int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
863int modprobe_main(int argc, char **argv)
Eric Andersen0139ca92001-07-22 23:01:03 +0000864{
Rob Landleye9190962005-12-12 19:38:44 +0000865 int rc = EXIT_SUCCESS;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000866 char *unused;
Eric Andersen0139ca92001-07-22 23:01:03 +0000867
Denis Vlasenkoe7fca512007-11-10 01:32:18 +0000868 opt_complementary = "q-v:v-q";
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000869 getopt32(argv, MAIN_OPT_STR, &unused, &unused);
870
871 if (option_mask32 & (DUMP_CONF_EXIT | LIST_ALL))
Denis Vlasenkocf704332006-10-27 15:12:50 +0000872 return EXIT_SUCCESS;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000873 if (option_mask32 & (RESTRICT_DIR | CONFIG_FILE))
Denis Vlasenkocf704332006-10-27 15:12:50 +0000874 bb_error_msg_and_die("-t and -C not supported");
Robert Griebl236abbf2002-05-22 23:34:35 +0000875
Denis Vlasenkocf704332006-10-27 15:12:50 +0000876 depend = build_dep();
Robert Griebl52e8d062002-05-14 23:42:08 +0000877
Denis Vlasenkocf704332006-10-27 15:12:50 +0000878 if (!depend)
879 bb_error_msg_and_die("cannot parse modules.dep");
Eric Andersen3b1a7442003-12-24 20:30:45 +0000880
Eric Andersen1b064192001-07-25 07:23:38 +0000881 if (remove_opt) {
Eric Andersen0139ca92001-07-22 23:01:03 +0000882 do {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000883 /* argv[optind] can be NULL here */
884 if (mod_remove(argv[optind])) {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000885 bb_error_msg("failed to remove module %s",
886 argv[optind]);
Eric Andersen908e3622003-06-20 09:56:37 +0000887 rc = EXIT_FAILURE;
888 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000889 } while (++optind < argc);
Rob Landleye9190962005-12-12 19:38:44 +0000890 } else {
891 if (optind >= argc)
Denis Vlasenkocf704332006-10-27 15:12:50 +0000892 bb_error_msg_and_die("no module or pattern provided");
Eric Andersen3b1a7442003-12-24 20:30:45 +0000893
Denis Vlasenkocf704332006-10-27 15:12:50 +0000894 if (mod_insert(argv[optind], argc - optind - 1, argv + optind + 1))
895 bb_error_msg_and_die("failed to load module %s", argv[optind]);
Eric Andersen0139ca92001-07-22 23:01:03 +0000896 }
897
Rob Landleye9190962005-12-12 19:38:44 +0000898 /* Here would be a good place to free up memory allocated during the dependencies build. */
Eric Andersen3b1a7442003-12-24 20:30:45 +0000899
Rob Landleye9190962005-12-12 19:38:44 +0000900 return rc;
Eric Andersen0139ca92001-07-22 23:01:03 +0000901}