blob: 3ac5a81a552fbc4d106331d18643a97a54b533b8 [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 */
Bernhard Reutner-Fischerdb508e32008-05-28 15:57:31 +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
Bernhard Reutner-Fischerdb508e32008-05-28 15:57:31 +000031 unsigned int m_isalias :1; /* the module is an alias */
32 unsigned int m_isblacklisted:1; /* the module is blacklisted */
33 unsigned int m_reserved :14; /* stuffin' */
Rob Landleye9190962005-12-12 19:38:44 +000034
Bernhard Reutner-Fischerdb508e32008-05-28 15:57:31 +000035 unsigned int m_depcnt :16; /* the number of dependable module(s) */
36 char ** m_deparr; /* the list of dependable module(s) */
Rob Landleye9190962005-12-12 19:38:44 +000037
Bernhard Reutner-Fischerdb508e32008-05-28 15:57:31 +000038 struct dep_t * m_next; /* the next dependency rule */
Rob Landleye9190962005-12-12 19:38:44 +000039};
40
41struct mod_list_t { /* two-way list of modules to process */
42 /* a module description */
Denis Vlasenko322661d2007-01-29 23:43:52 +000043 const char * m_name;
44 char * m_path;
45 struct mod_opt_t * m_options;
Eric Andersen3b1a7442003-12-24 20:30:45 +000046
Robert Griebl52e8d062002-05-14 23:42:08 +000047 struct mod_list_t * m_prev;
48 struct mod_list_t * m_next;
49};
50
51
Robert Griebl236abbf2002-05-22 23:34:35 +000052static struct dep_t *depend;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +000053
Denis Vlasenkob68979a2007-11-02 23:31:10 +000054#define MAIN_OPT_STR "acdklnqrst:vVC:"
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +000055#define INSERT_ALL 1 /* a */
56#define DUMP_CONF_EXIT 2 /* c */
57#define D_OPT_IGNORED 4 /* d */
58#define AUTOCLEAN_FLG 8 /* k */
59#define LIST_ALL 16 /* l */
60#define SHOW_ONLY 32 /* n */
61#define QUIET 64 /* q */
62#define REMOVE_OPT 128 /* r */
63#define DO_SYSLOG 256 /* s */
64#define RESTRICT_DIR 512 /* t */
65#define VERBOSE 1024 /* v */
66#define VERSION_ONLY 2048 /* V */
67#define CONFIG_FILE 4096 /* C */
68
Denis Vlasenkob68979a2007-11-02 23:31:10 +000069#define autoclean (option_mask32 & AUTOCLEAN_FLG)
70#define show_only (option_mask32 & SHOW_ONLY)
71#define quiet (option_mask32 & QUIET)
72#define remove_opt (option_mask32 & REMOVE_OPT)
73#define do_syslog (option_mask32 & DO_SYSLOG)
74#define verbose (option_mask32 & VERBOSE)
Robert Griebl52e8d062002-05-14 23:42:08 +000075
Denis Vlasenkocf704332006-10-27 15:12:50 +000076static int parse_tag_value(char *buffer, char **ptag, char **pvalue)
Robert Grieblaead70b2002-07-26 15:54:20 +000077{
78 char *tag, *value;
79
Denis Vlasenkocf704332006-10-27 15:12:50 +000080 buffer = skip_whitespace(buffer);
Robert Grieblaead70b2002-07-26 15:54:20 +000081 tag = value = buffer;
Denis Vlasenkob68979a2007-11-02 23:31:10 +000082 while (!isspace(*value)) {
83 if (!*value)
84 return 0;
85 value++;
86 }
87 *value++ = '\0';
Denis Vlasenkocf704332006-10-27 15:12:50 +000088 value = skip_whitespace(value);
Denis Vlasenkob68979a2007-11-02 23:31:10 +000089 if (!*value)
90 return 0;
Robert Grieblaead70b2002-07-26 15:54:20 +000091
92 *ptag = tag;
93 *pvalue = value;
Eric Andersen3b1a7442003-12-24 20:30:45 +000094
Eric Andersen7e496a72004-04-06 12:06:03 +000095 return 1;
Robert Grieblaead70b2002-07-26 15:54:20 +000096}
Eric Andersen60e56f52002-04-26 06:04:01 +000097
Rob Landleye9190962005-12-12 19:38:44 +000098/*
99 * This function appends an option to a list
100 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000101static struct mod_opt_t *append_option(struct mod_opt_t *opt_list, char *opt)
Eric Andersen60e56f52002-04-26 06:04:01 +0000102{
Rob Landleye9190962005-12-12 19:38:44 +0000103 struct mod_opt_t *ol = opt_list;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000104
Denis Vlasenkocf704332006-10-27 15:12:50 +0000105 if (ol) {
106 while (ol->m_next) {
107 ol = ol->m_next;
Rob Landleye9190962005-12-12 19:38:44 +0000108 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000109 ol->m_next = xzalloc(sizeof(struct mod_opt_t));
Denis Vlasenkocf704332006-10-27 15:12:50 +0000110 ol = ol->m_next;
Rob Landleye9190962005-12-12 19:38:44 +0000111 } else {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000112 ol = opt_list = xzalloc(sizeof(struct mod_opt_t));
Eric Andersen03d80912003-12-19 21:04:19 +0000113 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000114
Denis Vlasenkocf704332006-10-27 15:12:50 +0000115 ol->m_opt_val = xstrdup(opt);
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000116 /*ol->m_next = NULL; - done by xzalloc*/
Eric Andersen60e56f52002-04-26 06:04:01 +0000117
Rob Landleye9190962005-12-12 19:38:44 +0000118 return opt_list;
Eric Andersen60e56f52002-04-26 06:04:01 +0000119}
120
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000121#if ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS
Denis Vlasenkocf704332006-10-27 15:12:50 +0000122/* static char* parse_command_string(char* src, char **dst);
Rob Landley79e1cab2005-11-15 00:08:29 +0000123 * src: pointer to string containing argument
124 * dst: pointer to where to store the parsed argument
125 * return value: the pointer to the first char after the parsed argument,
126 * NULL if there was no argument parsed (only trailing spaces).
Rob Landleyd921b2e2006-08-03 15:41:12 +0000127 * Note that memory is allocated with xstrdup when a new argument was
Rob Landley79e1cab2005-11-15 00:08:29 +0000128 * parsed. Don't forget to free it!
129 */
130#define ARG_EMPTY 0x00
131#define ARG_IN_DQUOTES 0x01
132#define ARG_IN_SQUOTES 0x02
Denis Vlasenkocf704332006-10-27 15:12:50 +0000133static char *parse_command_string(char *src, char **dst)
Rob Landley79e1cab2005-11-15 00:08:29 +0000134{
135 int opt_status = ARG_EMPTY;
136 char* tmp_str;
137
138 /* Dumb you, I have nothing to do... */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000139 if (src == NULL) return src;
Rob Landley79e1cab2005-11-15 00:08:29 +0000140
141 /* Skip leading spaces */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000142 while (*src == ' ') {
Rob Landley79e1cab2005-11-15 00:08:29 +0000143 src++;
144 }
145 /* Is the end of string reached? */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000146 if (*src == '\0') {
Rob Landley79e1cab2005-11-15 00:08:29 +0000147 return NULL;
148 }
149 /* Reached the start of an argument
Rob Landleye9190962005-12-12 19:38:44 +0000150 * By the way, we duplicate a little too much
151 * here but what is too much is freed later. */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000152 *dst = tmp_str = xstrdup(src);
Rob Landley79e1cab2005-11-15 00:08:29 +0000153 /* Get to the end of that argument */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000154 while (*tmp_str != '\0'
155 && (*tmp_str != ' ' || (opt_status & (ARG_IN_DQUOTES | ARG_IN_SQUOTES)))
156 ) {
157 switch (*tmp_str) {
158 case '\'':
159 if (opt_status & ARG_IN_DQUOTES) {
160 /* Already in double quotes, keep current char as is */
161 } else {
162 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
163 memmove(tmp_str, tmp_str + 1, strlen(tmp_str));
164 /* mark me: we enter or leave single quotes */
165 opt_status ^= ARG_IN_SQUOTES;
166 /* Back one char, as we need to re-scan the new char there. */
167 tmp_str--;
168 }
Rob Landley79e1cab2005-11-15 00:08:29 +0000169 break;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000170 case '"':
171 if (opt_status & ARG_IN_SQUOTES) {
172 /* Already in single quotes, keep current char as is */
173 } else {
174 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
175 memmove(tmp_str, tmp_str + 1, strlen(tmp_str));
176 /* mark me: we enter or leave double quotes */
177 opt_status ^= ARG_IN_DQUOTES;
178 /* Back one char, as we need to re-scan the new char there. */
179 tmp_str--;
180 }
Rob Landley79e1cab2005-11-15 00:08:29 +0000181 break;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000182 case '\\':
183 if (opt_status & ARG_IN_SQUOTES) {
184 /* Between single quotes: keep as is. */
185 } else {
186 switch (*(tmp_str+1)) {
187 case 'a':
188 case 'b':
189 case 't':
190 case 'n':
191 case 'v':
192 case 'f':
193 case 'r':
194 case '0':
195 /* We escaped a special character. For now, keep
196 * both the back-slash and the following char. */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000197 tmp_str++;
198 src++;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000199 break;
200 default:
201 /* We escaped a space or a single or double quote,
202 * or a back-slash, or a non-escapable char. Remove
203 * the '\' and keep the new current char as is. */
204 memmove(tmp_str, tmp_str + 1, strlen(tmp_str));
205 break;
Rob Landley79e1cab2005-11-15 00:08:29 +0000206 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000207 }
Rob Landley79e1cab2005-11-15 00:08:29 +0000208 break;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000209 /* Any other char that is special shall appear here.
210 * Example: $ starts a variable
211 case '$':
212 do_variable_expansion();
213 break;
214 * */
215 default:
216 /* any other char is kept as is. */
217 break;
Rob Landley79e1cab2005-11-15 00:08:29 +0000218 }
219 tmp_str++; /* Go to next char */
220 src++; /* Go to next char to find the end of the argument. */
221 }
222 /* End of string, but still no ending quote */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000223 if (opt_status & (ARG_IN_DQUOTES | ARG_IN_SQUOTES)) {
224 bb_error_msg_and_die("unterminated (single or double) quote in options list: %s", src);
Rob Landley79e1cab2005-11-15 00:08:29 +0000225 }
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000226 *tmp_str++ = '\0';
Denis Vlasenkocf704332006-10-27 15:12:50 +0000227 *dst = xrealloc(*dst, (tmp_str - *dst));
Rob Landley79e1cab2005-11-15 00:08:29 +0000228 return src;
229}
Rob Landleyae50c6d2005-12-15 07:42:13 +0000230#else
231#define parse_command_string(src, dst) (0)
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000232#endif /* ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
Rob Landley79e1cab2005-11-15 00:08:29 +0000233
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000234static int is_conf_command(char *buffer, const char *command)
Denis Vlasenkof45c4f42008-06-16 04:09:25 +0000235{
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000236 int len = strlen(command);
237 return ((strstr(buffer, command) == buffer) &&
Denis Vlasenkof45c4f42008-06-16 04:09:25 +0000238 isspace(buffer[len]));
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000239}
240
Rob Landleye9190962005-12-12 19:38:44 +0000241/*
Rob Landley72615752006-04-10 16:09:52 +0000242 * This function reads aliases and default module options from a configuration file
243 * (/etc/modprobe.conf syntax). It supports includes (only files, no directories).
244 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000245static void include_conf(struct dep_t **first, struct dep_t **current, char *buffer, int buflen, int fd)
Rob Landley72615752006-04-10 16:09:52 +0000246{
247 int continuation_line = 0;
248
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000249 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias)!
Rob Landley72615752006-04-10 16:09:52 +0000250
Denis Vlasenkocf704332006-10-27 15:12:50 +0000251 while (reads(fd, buffer, buflen)) {
Rob Landley72615752006-04-10 16:09:52 +0000252 int l;
Rob Landley72615752006-04-10 16:09:52 +0000253
Denis Vlasenkoc03e8722007-12-26 20:56:55 +0000254 *strchrnul(buffer, '#') = '\0';
Rob Landley72615752006-04-10 16:09:52 +0000255
Denis Vlasenkocf704332006-10-27 15:12:50 +0000256 l = strlen(buffer);
Rob Landley72615752006-04-10 16:09:52 +0000257
Denis Vlasenkocf704332006-10-27 15:12:50 +0000258 while (l && isspace(buffer[l-1])) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000259 buffer[l-1] = '\0';
Rob Landley72615752006-04-10 16:09:52 +0000260 l--;
261 }
262
Denis Vlasenkocf704332006-10-27 15:12:50 +0000263 if (l == 0) {
Rob Landley72615752006-04-10 16:09:52 +0000264 continuation_line = 0;
265 continue;
266 }
267
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000268 if (continuation_line)
269 continue;
Rob Landley72615752006-04-10 16:09:52 +0000270
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000271 if (is_conf_command(buffer, "alias")) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000272 char *alias, *mod;
Rob Landley72615752006-04-10 16:09:52 +0000273
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000274 if (parse_tag_value(buffer + 6, &alias, &mod)) {
275 /* handle alias as a module dependent on the aliased module */
276 if (!*current) {
277 (*first) = (*current) = xzalloc(sizeof(struct dep_t));
278 } else {
279 (*current)->m_next = xzalloc(sizeof(struct dep_t));
280 (*current) = (*current)->m_next;
Rob Landley72615752006-04-10 16:09:52 +0000281 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000282 (*current)->m_name = xstrdup(alias);
283 (*current)->m_isalias = 1;
Rob Landley72615752006-04-10 16:09:52 +0000284
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000285 if ((strcmp(mod, "off") == 0) || (strcmp(mod, "null") == 0)) {
286 /*(*current)->m_depcnt = 0; - done by xzalloc */
287 /*(*current)->m_deparr = 0;*/
288 } else {
289 (*current)->m_depcnt = 1;
290 (*current)->m_deparr = xmalloc(sizeof(char *));
291 (*current)->m_deparr[0] = xstrdup(mod);
292 }
293 /*(*current)->m_next = NULL; - done by xzalloc */
294 }
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000295 } else if (is_conf_command(buffer, "options")) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000296 char *mod, *opt;
Rob Landley72615752006-04-10 16:09:52 +0000297
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000298 /* split the line in the module/alias name, and options */
299 if (parse_tag_value(buffer + 8, &mod, &opt)) {
300 struct dep_t *dt;
301
302 /* find the corresponding module */
303 for (dt = *first; dt; dt = dt->m_next) {
304 if (strcmp(dt->m_name, mod) == 0)
305 break;
306 }
307 if (dt) {
308 if (ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS) {
309 char* new_opt = NULL;
310 while ((opt = parse_command_string(opt, &new_opt))) {
311 dt->m_options = append_option(dt->m_options, new_opt);
Rob Landley72615752006-04-10 16:09:52 +0000312 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000313 } else {
314 dt->m_options = append_option(dt->m_options, opt);
Rob Landley72615752006-04-10 16:09:52 +0000315 }
316 }
Rob Landley72615752006-04-10 16:09:52 +0000317 }
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000318 } else if (is_conf_command(buffer, "include")) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000319 int fdi;
320 char *filename;
321
322 filename = skip_whitespace(buffer + 8);
323 fdi = open(filename, O_RDONLY);
324 if (fdi >= 0) {
325 include_conf(first, current, buffer, buflen, fdi);
326 close(fdi);
327 }
Denis Vlasenkof45c4f42008-06-16 04:09:25 +0000328 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST &&
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000329 (is_conf_command(buffer, "blacklist"))) {
330 char *mod;
331 struct dep_t *dt;
332
333 mod = skip_whitespace(buffer + 10);
334 for (dt = *first; dt; dt = dt->m_next) {
Denis Vlasenkoae84b112008-05-22 17:37:38 +0000335 if (strcmp(dt->m_name, mod) == 0)
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000336 break;
337 }
338 if (dt)
339 dt->m_isblacklisted = 1;
Rob Landley72615752006-04-10 16:09:52 +0000340 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000341 } /* while (reads(...)) */
Rob Landley72615752006-04-10 16:09:52 +0000342}
343
344/*
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000345 * This function builds a list of dependency rules from /lib/modules/`uname -r`/modules.dep.
Bernhard Reutner-Fischere3c150b2006-05-19 11:24:28 +0000346 * It then fills every modules and aliases with their default options, found by parsing
Rob Landleye9190962005-12-12 19:38:44 +0000347 * modprobe.conf (or modules.conf, or conf.modules).
348 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000349static struct dep_t *build_dep(void)
Rob Landleye9190962005-12-12 19:38:44 +0000350{
351 int fd;
352 struct utsname un;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000353 struct dep_t *first = NULL;
354 struct dep_t *current = NULL;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000355 char *filename;
Rob Landleye9190962005-12-12 19:38:44 +0000356 int continuation_line = 0;
357 int k_version;
358
Denis Vlasenkocf704332006-10-27 15:12:50 +0000359 if (uname(&un))
Rob Landleye9190962005-12-12 19:38:44 +0000360 bb_error_msg_and_die("can't determine kernel version");
361
Denis Vlasenkocf704332006-10-27 15:12:50 +0000362 k_version = 0;
Rob Landleye9190962005-12-12 19:38:44 +0000363 if (un.release[0] == '2') {
364 k_version = un.release[2] - '0';
365 }
366
Bernhard Reutner-Fischerb85fb692008-05-27 10:55:34 +0000367 filename = xasprintf(CONFIG_DEFAULT_MODULES_DIR"/%s/"CONFIG_DEFAULT_DEPMOD_FILE, un.release);
Denis Vlasenkocf704332006-10-27 15:12:50 +0000368 fd = open(filename, O_RDONLY);
Bernhard Reutner-Fischere3c150b2006-05-19 11:24:28 +0000369 if (ENABLE_FEATURE_CLEAN_UP)
370 free(filename);
Rob Landley3afb0702006-05-18 20:41:43 +0000371 if (fd < 0) {
Rob Landleye9190962005-12-12 19:38:44 +0000372 /* Ok, that didn't work. Fall back to looking in /lib/modules */
Bernhard Reutner-Fischerb85fb692008-05-27 10:55:34 +0000373 fd = open(CONFIG_DEFAULT_MODULES_DIR"/"CONFIG_DEFAULT_DEPMOD_FILE, O_RDONLY);
Denis Vlasenkocf704332006-10-27 15:12:50 +0000374 if (fd < 0) {
Bernhard Reutner-Fischerb85fb692008-05-27 10:55:34 +0000375 bb_error_msg_and_die("cannot parse " CONFIG_DEFAULT_DEPMOD_FILE);
Rob Landleye9190962005-12-12 19:38:44 +0000376 }
377 }
378
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000379 while (reads(fd, line_buffer, sizeof(line_buffer))) {
380 int l = strlen(line_buffer);
Rob Landleye9190962005-12-12 19:38:44 +0000381 char *p = 0;
382
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000383 while (l > 0 && isspace(line_buffer[l-1])) {
384 line_buffer[l-1] = '\0';
Rob Landleye9190962005-12-12 19:38:44 +0000385 l--;
386 }
387
Denis Vlasenkocf704332006-10-27 15:12:50 +0000388 if (l == 0) {
Rob Landleye9190962005-12-12 19:38:44 +0000389 continuation_line = 0;
390 continue;
391 }
392
393 /* Is this a new module dep description? */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000394 if (!continuation_line) {
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000395 /* find the dep beginning */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000396 char *col = strchr(line_buffer, ':');
Rob Landleye9190962005-12-12 19:38:44 +0000397 char *dot = col;
398
Denis Vlasenkocf704332006-10-27 15:12:50 +0000399 if (col) {
Rob Landleye9190962005-12-12 19:38:44 +0000400 /* This line is a dep description */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000401 const char *mods;
Rob Landleye9190962005-12-12 19:38:44 +0000402 char *modpath;
403 char *mod;
404
405 /* Find the beginning of the module file name */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000406 *col = '\0';
407 mods = bb_basename(line_buffer);
Rob Landleye9190962005-12-12 19:38:44 +0000408
409 /* find the path of the module */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000410 modpath = strchr(line_buffer, '/'); /* ... and this is the path */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000411 if (!modpath)
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000412 modpath = line_buffer; /* module with no path */
Rob Landleye9190962005-12-12 19:38:44 +0000413 /* find the end of the module name in the file name */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000414 if (ENABLE_FEATURE_2_6_MODULES &&
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000415 (k_version > 4) && (col[-3] == '.') &&
416 (col[-2] == 'k') && (col[-1] == 'o'))
Rob Landleye9190962005-12-12 19:38:44 +0000417 dot = col - 3;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000418 else if ((col[-2] == '.') && (col[-1] == 'o'))
419 dot = col - 2;
Rob Landleye9190962005-12-12 19:38:44 +0000420
Denis Vlasenkocf704332006-10-27 15:12:50 +0000421 mod = xstrndup(mods, dot - mods);
Rob Landleye9190962005-12-12 19:38:44 +0000422
423 /* enqueue new module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000424 if (!current) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000425 first = current = xzalloc(sizeof(struct dep_t));
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000426 } else {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000427 current->m_next = xzalloc(sizeof(struct dep_t));
Denis Vlasenkocf704332006-10-27 15:12:50 +0000428 current = current->m_next;
Rob Landleye9190962005-12-12 19:38:44 +0000429 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000430 current->m_name = mod;
431 current->m_path = xstrdup(modpath);
432 /*current->m_options = NULL; - xzalloc did it*/
433 /*current->m_isalias = 0;*/
434 /*current->m_depcnt = 0;*/
435 /*current->m_deparr = 0;*/
436 /*current->m_next = 0;*/
Rob Landleye9190962005-12-12 19:38:44 +0000437
438 p = col + 1;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000439 } else
Rob Landleye9190962005-12-12 19:38:44 +0000440 /* this line is not a dep description */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000441 p = NULL;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000442 } else
Rob Landleye9190962005-12-12 19:38:44 +0000443 /* It's a dep description continuation */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000444 p = line_buffer;
Rob Landleye9190962005-12-12 19:38:44 +0000445
Rob Landleye9190962005-12-12 19:38:44 +0000446 /* p points to the first dependable module; if NULL, no dependable module */
Denis Vlasenko8124a962008-06-22 16:59:46 +0000447 if (p && (p = skip_whitespace(p))[0] != '\0') {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000448 char *end = &line_buffer[l-1];
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000449 const char *deps;
Rob Landleye9190962005-12-12 19:38:44 +0000450 char *dep;
451 char *next;
452 int ext = 0;
453
Denis Vlasenkocf704332006-10-27 15:12:50 +0000454 while (isblank(*end) || (*end == '\\'))
Rob Landleye9190962005-12-12 19:38:44 +0000455 end--;
456
Denis Vlasenkocf704332006-10-27 15:12:50 +0000457 do {
Rob Landleye9190962005-12-12 19:38:44 +0000458 /* search the end of the dependency */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000459 next = strchr(p, ' ');
460 if (next) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000461 *next = '\0';
Rob Landleye9190962005-12-12 19:38:44 +0000462 next--;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000463 } else
Rob Landleye9190962005-12-12 19:38:44 +0000464 next = end;
465
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000466 /* find the beginning of the module file name */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000467 deps = bb_basename(p);
Bernhard Reutner-Fischere0fd13e2008-05-31 18:50:17 +0000468 if (deps == p)
469 deps = skip_whitespace(deps);
Rob Landleye9190962005-12-12 19:38:44 +0000470
471 /* find the end of the module name in the file name */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000472 if (ENABLE_FEATURE_2_6_MODULES
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000473 && (k_version > 4) && (next[-2] == '.')
474 && (next[-1] == 'k') && (next[0] == 'o'))
Rob Landleye9190962005-12-12 19:38:44 +0000475 ext = 3;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000476 else if ((next[-1] == '.') && (next[0] == 'o'))
477 ext = 2;
Rob Landleye9190962005-12-12 19:38:44 +0000478
479 /* Cope with blank lines */
480 if ((next-deps-ext+1) <= 0)
481 continue;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000482 dep = xstrndup(deps, next - deps - ext + 1);
Rob Landleye9190962005-12-12 19:38:44 +0000483
484 /* Add the new dependable module name */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000485 current->m_depcnt++;
486 current->m_deparr = xrealloc(current->m_deparr,
487 sizeof(char *) * current->m_depcnt);
488 current->m_deparr[current->m_depcnt - 1] = dep;
Rob Landleye9190962005-12-12 19:38:44 +0000489
490 p = next + 2;
491 } while (next < end);
492 }
493
494 /* is there other dependable module(s) ? */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000495 continuation_line = (line_buffer[l-1] == '\\');
496 } /* while (reads(...)) */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000497 close(fd);
Rob Landleye9190962005-12-12 19:38:44 +0000498
Rob Landley3b0cfb42006-07-19 21:33:42 +0000499 /*
500 * First parse system-specific options and aliases
501 * as they take precedence over the kernel ones.
Mike Frysinger4423e5b2007-02-08 07:03:44 +0000502 * >=2.6: we only care about modprobe.conf
503 * <=2.4: we care about modules.conf and conf.modules
Rob Landley3b0cfb42006-07-19 21:33:42 +0000504 */
Mike Frysinger4423e5b2007-02-08 07:03:44 +0000505 if (ENABLE_FEATURE_2_6_MODULES
506 && (fd = open("/etc/modprobe.conf", O_RDONLY)) < 0)
507 if (ENABLE_FEATURE_2_4_MODULES
508 && (fd = open("/etc/modules.conf", O_RDONLY)) < 0)
509 if (ENABLE_FEATURE_2_4_MODULES)
510 fd = open("/etc/conf.modules", O_RDONLY);
Rob Landleye9190962005-12-12 19:38:44 +0000511
Rob Landley3b0cfb42006-07-19 21:33:42 +0000512 if (fd >= 0) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000513 include_conf(&first, &current, line_buffer, sizeof(line_buffer), fd);
Rob Landley3b0cfb42006-07-19 21:33:42 +0000514 close(fd);
Rob Landleye9190962005-12-12 19:38:44 +0000515 }
Rob Landley72615752006-04-10 16:09:52 +0000516
Rob Landley3b0cfb42006-07-19 21:33:42 +0000517 /* Only 2.6 has a modules.alias file */
518 if (ENABLE_FEATURE_2_6_MODULES) {
Denis Vlasenkof8483052007-08-16 10:40:06 +0000519 /* Parse kernel-declared module aliases */
Bernhard Reutner-Fischerb85fb692008-05-27 10:55:34 +0000520 filename = xasprintf(CONFIG_DEFAULT_MODULES_DIR"/%s/modules.alias", un.release);
Denis Vlasenkocf704332006-10-27 15:12:50 +0000521 fd = open(filename, O_RDONLY);
522 if (fd < 0) {
Rob Landley3b0cfb42006-07-19 21:33:42 +0000523 /* Ok, that didn't work. Fall back to looking in /lib/modules */
Bernhard Reutner-Fischerb85fb692008-05-27 10:55:34 +0000524 fd = open(CONFIG_DEFAULT_MODULES_DIR"/modules.alias", O_RDONLY);
Rob Landley3b0cfb42006-07-19 21:33:42 +0000525 }
526 if (ENABLE_FEATURE_CLEAN_UP)
527 free(filename);
528
529 if (fd >= 0) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000530 include_conf(&first, &current, line_buffer, sizeof(line_buffer), fd);
Rob Landley3b0cfb42006-07-19 21:33:42 +0000531 close(fd);
532 }
Denis Vlasenkof8483052007-08-16 10:40:06 +0000533
534 /* Parse kernel-declared symbol aliases */
Bernhard Reutner-Fischerb85fb692008-05-27 10:55:34 +0000535 filename = xasprintf(CONFIG_DEFAULT_MODULES_DIR"/%s/modules.symbols", un.release);
Denis Vlasenkof8483052007-08-16 10:40:06 +0000536 fd = open(filename, O_RDONLY);
537 if (fd < 0) {
538 /* Ok, that didn't work. Fall back to looking in /lib/modules */
Bernhard Reutner-Fischerb85fb692008-05-27 10:55:34 +0000539 fd = open(CONFIG_DEFAULT_MODULES_DIR"/modules.symbols", O_RDONLY);
Denis Vlasenkof8483052007-08-16 10:40:06 +0000540 }
541 if (ENABLE_FEATURE_CLEAN_UP)
542 free(filename);
543
544 if (fd >= 0) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000545 include_conf(&first, &current, line_buffer, sizeof(line_buffer), fd);
Denis Vlasenkof8483052007-08-16 10:40:06 +0000546 close(fd);
547 }
Rob Landley3b0cfb42006-07-19 21:33:42 +0000548 }
Rob Landleye9190962005-12-12 19:38:44 +0000549
550 return first;
551}
552
553/* return 1 = loaded, 0 = not loaded, -1 = can't tell */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000554static int already_loaded(const char *name)
Rob Landleye9190962005-12-12 19:38:44 +0000555{
Rob Landleyd7605602006-06-14 01:51:16 +0000556 int fd, ret = 0;
Rob Landleye9190962005-12-12 19:38:44 +0000557
Denis Vlasenkocf704332006-10-27 15:12:50 +0000558 fd = open("/proc/modules", O_RDONLY);
Rob Landleye9190962005-12-12 19:38:44 +0000559 if (fd < 0)
560 return -1;
561
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000562 while (reads(fd, line_buffer, sizeof(line_buffer))) {
Rob Landleye9190962005-12-12 19:38:44 +0000563 char *p;
564
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000565 p = strchr(line_buffer, ' ');
Rob Landleye9190962005-12-12 19:38:44 +0000566 if (p) {
Rob Landleyd7605602006-06-14 01:51:16 +0000567 const char *n;
568
569 // Truncate buffer at first space and check for matches, with
570 // the idiosyncrasy that _ and - are interchangeable because the
571 // 2.6 kernel does weird things.
572
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000573 *p = '\0';
574 for (p = line_buffer, n = name; ; p++, n++) {
Rob Landleyd7605602006-06-14 01:51:16 +0000575 if (*p != *n) {
576 if ((*p == '_' || *p == '-') && (*n == '_' || *n == '-'))
577 continue;
578 break;
579 }
580 // If we made it to the end, that's a match.
581 if (!*p) {
582 ret = 1;
583 goto done;
584 }
Rob Landleye9190962005-12-12 19:38:44 +0000585 }
586 }
587 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000588 done:
589 close(fd);
Mike Frysinger135cee32006-06-21 23:03:37 +0000590 return ret;
Rob Landleye9190962005-12-12 19:38:44 +0000591}
592
Denis Vlasenko322661d2007-01-29 23:43:52 +0000593static int mod_process(const struct mod_list_t *list, int do_insert)
Eric Andersen60e56f52002-04-26 06:04:01 +0000594{
Eric Andersen44b57582004-08-03 08:23:33 +0000595 int rc = 0;
Rob Landley79e1cab2005-11-15 00:08:29 +0000596 char **argv = NULL;
Rob Landleye9190962005-12-12 19:38:44 +0000597 struct mod_opt_t *opts;
598 int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */
Paul Fox8eeb6552005-08-04 18:33:36 +0000599 int argc;
Eric Andersen864b7972002-05-03 15:48:26 +0000600
Denis Vlasenkocf704332006-10-27 15:12:50 +0000601 while (list) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000602 argc = 0;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000603 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleye9190962005-12-12 19:38:44 +0000604 argc_malloc = 0;
605 /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory
606 * each time we allocate memory for argv.
607 * But it is (quite) small amounts of memory that leak each
608 * time a module is loaded, and it is reclaimed when modprobe
609 * exits anyway (even when standalone shell?).
610 * This could become a problem when loading a module with LOTS of
611 * dependencies, with LOTS of options for each dependencies, with
612 * very little memory on the target... But in that case, the module
613 * would not load because there is no more memory, so there's no
614 * problem. */
615 /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000616 argv = xmalloc(6 * sizeof(char*));
617 if (do_insert) {
618 if (already_loaded(list->m_name) != 1) {
Denis Vlasenko322661d2007-01-29 23:43:52 +0000619 argv[argc++] = (char*)"insmod";
Rob Landleyd7605602006-06-14 01:51:16 +0000620 if (ENABLE_FEATURE_2_4_MODULES) {
621 if (do_syslog)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000622 argv[argc++] = (char*)"-s";
Rob Landleyd7605602006-06-14 01:51:16 +0000623 if (autoclean)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000624 argv[argc++] = (char*)"-k";
Rob Landleyd7605602006-06-14 01:51:16 +0000625 if (quiet)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000626 argv[argc++] = (char*)"-q";
Denis Vlasenkocf704332006-10-27 15:12:50 +0000627 else if (verbose) /* verbose and quiet are mutually exclusive */
Denis Vlasenko322661d2007-01-29 23:43:52 +0000628 argv[argc++] = (char*)"-v";
Rob Landleyd7605602006-06-14 01:51:16 +0000629 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000630 argv[argc++] = list->m_path;
631 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleye9190962005-12-12 19:38:44 +0000632 argc_malloc = argc;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000633 opts = list->m_options;
634 while (opts) {
Rob Landleye9190962005-12-12 19:38:44 +0000635 /* Add one more option */
Rob Landley79e1cab2005-11-15 00:08:29 +0000636 argc++;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000637 argv = xrealloc(argv, (argc + 1) * sizeof(char*));
Denis Vlasenkocf704332006-10-27 15:12:50 +0000638 argv[argc-1] = opts->m_opt_val;
639 opts = opts->m_next;
Rob Landley79e1cab2005-11-15 00:08:29 +0000640 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000641 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000642 } else {
Eric Andersen807bd842004-08-19 18:30:31 +0000643 /* modutils uses short name for removal */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000644 if (already_loaded(list->m_name) != 0) {
Denis Vlasenko322661d2007-01-29 23:43:52 +0000645 argv[argc++] = (char*)"rmmod";
Paul Fox8eeb6552005-08-04 18:33:36 +0000646 if (do_syslog)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000647 argv[argc++] = (char*)"-s";
648 argv[argc++] = (char*)list->m_name;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000649 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleye9190962005-12-12 19:38:44 +0000650 argc_malloc = argc;
Paul Fox8eeb6552005-08-04 18:33:36 +0000651 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000652 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000653 argv[argc] = NULL;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000654
Paul Fox8eeb6552005-08-04 18:33:36 +0000655 if (argc) {
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000656 if (verbose) {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000657 printf("%s module %s\n", do_insert?"Loading":"Unloading", list->m_name);
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000658 }
659 if (!show_only) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000660 int rc2 = wait4pid(spawn(argv));
Rob Landley4b5827a2006-08-22 23:50:11 +0000661
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000662 if (do_insert) {
663 rc = rc2; /* only last module matters */
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000664 } else if (!rc2) {
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000665 rc = 0; /* success if remove any mod */
666 }
667 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000668 if (ENABLE_FEATURE_CLEAN_UP) {
Rob Landleye9190962005-12-12 19:38:44 +0000669 /* the last value in the array has index == argc, but
670 * it is the terminating NULL, so we must not free it. */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000671 while (argc_malloc < argc) {
672 free(argv[argc_malloc++]);
673 }
Rob Landley79e1cab2005-11-15 00:08:29 +0000674 }
Eric Andersen908e3622003-06-20 09:56:37 +0000675 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000676 if (ENABLE_FEATURE_CLEAN_UP) {
677 free(argv);
Rob Landleye9190962005-12-12 19:38:44 +0000678 argv = NULL;
Rob Landley79e1cab2005-11-15 00:08:29 +0000679 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000680 list = do_insert ? list->m_prev : list->m_next;
Eric Andersen60e56f52002-04-26 06:04:01 +0000681 }
Eric Andersen908e3622003-06-20 09:56:37 +0000682 return (show_only) ? 0 : rc;
Eric Andersen60e56f52002-04-26 06:04:01 +0000683}
684
Rob Landleye9190962005-12-12 19:38:44 +0000685/*
Rob Landleybf30c692006-07-20 17:36:18 +0000686 * Check the matching between a pattern and a module name.
687 * We need this as *_* is equivalent to *-*, even in pattern matching.
688 */
Denis Vlasenkoac678ec2007-04-16 22:32:04 +0000689static int check_pattern(const char* pat_src, const char* mod_src)
690{
Rob Landleybf30c692006-07-20 17:36:18 +0000691 int ret;
692
693 if (ENABLE_FEATURE_MODPROBE_FANCY_ALIAS) {
694 char* pat;
695 char* mod;
696 char* p;
697
Denis Vlasenko6398cf42007-04-11 17:04:29 +0000698 pat = xstrdup(pat_src);
699 mod = xstrdup(mod_src);
Rob Landleybf30c692006-07-20 17:36:18 +0000700
Denis Vlasenkocf704332006-10-27 15:12:50 +0000701 for (p = pat; (p = strchr(p, '-')); *p++ = '_');
702 for (p = mod; (p = strchr(p, '-')); *p++ = '_');
Rob Landleybf30c692006-07-20 17:36:18 +0000703
Denis Vlasenkocf704332006-10-27 15:12:50 +0000704 ret = fnmatch(pat, mod, 0);
Rob Landleybf30c692006-07-20 17:36:18 +0000705
706 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkoff131b92007-04-10 15:42:06 +0000707 free(pat);
708 free(mod);
Rob Landleybf30c692006-07-20 17:36:18 +0000709 }
710
711 return ret;
Rob Landleybf30c692006-07-20 17:36:18 +0000712 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000713 return fnmatch(pat_src, mod_src, 0);
Rob Landleybf30c692006-07-20 17:36:18 +0000714}
715
716/*
Rob Landleye9190962005-12-12 19:38:44 +0000717 * Builds the dependency list (aka stack) of a module.
718 * head: the highest module in the stack (last to insmod, first to rmmod)
719 * tail: the lowest module in the stack (first to insmod, last to rmmod)
720 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000721static void check_dep(char *mod, struct mod_list_t **head, struct mod_list_t **tail)
Eric Andersen60e56f52002-04-26 06:04:01 +0000722{
Robert Griebl52e8d062002-05-14 23:42:08 +0000723 struct mod_list_t *find;
Eric Andersen60e56f52002-04-26 06:04:01 +0000724 struct dep_t *dt;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000725 struct mod_opt_t *opt = NULL;
726 char *path = NULL;
Robert Griebl52e8d062002-05-14 23:42:08 +0000727
Bernhard Reutner-Fischer2c351a82006-06-03 19:08:49 +0000728 /* Search for the given module name amongst all dependency rules.
729 * The module name in a dependency rule can be a shell pattern,
730 * so try to match the given module name against such a pattern.
731 * Of course if the name in the dependency rule is a plain string,
732 * then we consider it a pattern, and matching will still work. */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000733 for (dt = depend; dt; dt = dt->m_next) {
734 if (check_pattern(dt->m_name, mod) == 0) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000735 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000736 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000737 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000738
Denis Vlasenkocf704332006-10-27 15:12:50 +0000739 if (!dt) {
740 bb_error_msg("module %s not found", mod);
Rob Landleye9190962005-12-12 19:38:44 +0000741 return;
742 }
743
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000744 // resolve alias names
Denis Vlasenkocf704332006-10-27 15:12:50 +0000745 while (dt->m_isalias) {
Denis Vlasenkoae84b112008-05-22 17:37:38 +0000746 if (dt->m_depcnt == 1) {
Robert Grieblaead70b2002-07-26 15:54:20 +0000747 struct dep_t *adt;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000748
Denis Vlasenkocf704332006-10-27 15:12:50 +0000749 for (adt = depend; adt; adt = adt->m_next) {
Denis Vlasenkoae84b112008-05-22 17:37:38 +0000750 if (check_pattern(adt->m_name, dt->m_deparr[0]) == 0 &&
751 !(ENABLE_FEATURE_MODPROBE_BLACKLIST &&
752 adt->m_isblacklisted))
Robert Grieblaead70b2002-07-26 15:54:20 +0000753 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000754 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000755 if (adt) {
Rob Landleye9190962005-12-12 19:38:44 +0000756 /* This is the module we are aliased to */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000757 struct mod_opt_t *opts = dt->m_options;
Rob Landleye9190962005-12-12 19:38:44 +0000758 /* Option of the alias are appended to the options of the module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000759 while (opts) {
760 adt->m_options = append_option(adt->m_options, opts->m_opt_val);
761 opts = opts->m_next;
Rob Landleye9190962005-12-12 19:38:44 +0000762 }
Eric Andersen716ccb22004-01-10 11:29:31 +0000763 dt = adt;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000764 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000765 bb_error_msg("module %s not found", mod);
Robert Griebl70112da2002-07-29 20:28:38 +0000766 return;
Rob Landleye9190962005-12-12 19:38:44 +0000767 }
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000768 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000769 bb_error_msg("bad alias %s", dt->m_name);
Eric Andersen716ccb22004-01-10 11:29:31 +0000770 return;
Rob Landleye9190962005-12-12 19:38:44 +0000771 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000772 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000773
Denis Vlasenkocf704332006-10-27 15:12:50 +0000774 mod = dt->m_name;
775 path = dt->m_path;
776 opt = dt->m_options;
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000777
Robert Griebl52e8d062002-05-14 23:42:08 +0000778 // search for duplicates
Denis Vlasenkocf704332006-10-27 15:12:50 +0000779 for (find = *head; find; find = find->m_next) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000780 if (strcmp(mod, find->m_name) == 0) {
781 // found -> dequeue it
Robert Griebl52e8d062002-05-14 23:42:08 +0000782
Denis Vlasenkocf704332006-10-27 15:12:50 +0000783 if (find->m_prev)
784 find->m_prev->m_next = find->m_next;
Robert Griebl52e8d062002-05-14 23:42:08 +0000785 else
Denis Vlasenkocf704332006-10-27 15:12:50 +0000786 *head = find->m_next;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000787
Denis Vlasenkocf704332006-10-27 15:12:50 +0000788 if (find->m_next)
789 find->m_next->m_prev = find->m_prev;
Robert Griebl52e8d062002-05-14 23:42:08 +0000790 else
Denis Vlasenkocf704332006-10-27 15:12:50 +0000791 *tail = find->m_prev;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000792
Robert Griebl52e8d062002-05-14 23:42:08 +0000793 break; // there can be only one duplicate
Eric Andersen716ccb22004-01-10 11:29:31 +0000794 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000795 }
796
Denis Vlasenkocf704332006-10-27 15:12:50 +0000797 if (!find) { // did not find a duplicate
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000798 find = xzalloc(sizeof(struct mod_list_t));
Denis Vlasenkocf704332006-10-27 15:12:50 +0000799 find->m_name = mod;
800 find->m_path = path;
801 find->m_options = opt;
Robert Griebl52e8d062002-05-14 23:42:08 +0000802 }
803
Eric Andersen716ccb22004-01-10 11:29:31 +0000804 // enqueue at tail
Denis Vlasenkocf704332006-10-27 15:12:50 +0000805 if (*tail)
806 (*tail)->m_next = find;
807 find->m_prev = *tail;
Denis Vlasenkocb12cb22007-11-06 11:34:03 +0000808 find->m_next = NULL; /* possibly NOT done by xzalloc! */
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000809
Denis Vlasenkocf704332006-10-27 15:12:50 +0000810 if (!*head)
Robert Griebl52e8d062002-05-14 23:42:08 +0000811 *head = find;
812 *tail = find;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000813
Denis Vlasenkocf704332006-10-27 15:12:50 +0000814 if (dt) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000815 int i;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000816
Rob Landleye9190962005-12-12 19:38:44 +0000817 /* Add all dependable module for that new module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000818 for (i = 0; i < dt->m_depcnt; i++)
819 check_dep(dt->m_deparr[i], head, tail);
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000820 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000821}
822
Denis Vlasenkocf704332006-10-27 15:12:50 +0000823static int mod_insert(char *mod, int argc, char **argv)
Robert Griebl52e8d062002-05-14 23:42:08 +0000824{
Denis Vlasenko322661d2007-01-29 23:43:52 +0000825 struct mod_list_t *tail = NULL;
826 struct mod_list_t *head = NULL;
Eric Andersen908e3622003-06-20 09:56:37 +0000827 int rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000828
Robert Griebl52e8d062002-05-14 23:42:08 +0000829 // get dep list for module mod
Denis Vlasenkocf704332006-10-27 15:12:50 +0000830 check_dep(mod, &head, &tail);
Eric Andersen3b1a7442003-12-24 20:30:45 +0000831
Denis Vlasenko322661d2007-01-29 23:43:52 +0000832 rc = 1;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000833 if (head && tail) {
834 if (argc) {
Eric Andersen716ccb22004-01-10 11:29:31 +0000835 int i;
Robert Grieblaead70b2002-07-26 15:54:20 +0000836 // append module args
Denis Vlasenkocf704332006-10-27 15:12:50 +0000837 for (i = 0; i < argc; i++)
838 head->m_options = append_option(head->m_options, argv[i]);
Robert Griebl52e8d062002-05-14 23:42:08 +0000839 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000840
Robert Griebl52e8d062002-05-14 23:42:08 +0000841 // process tail ---> head
Denis Vlasenko322661d2007-01-29 23:43:52 +0000842 rc = mod_process(tail, 1);
843 if (rc) {
Rob Landley4b5827a2006-08-22 23:50:11 +0000844 /*
845 * In case of using udev, multiple instances of modprobe can be
846 * spawned to load the same module (think of two same usb devices,
847 * for example; or cold-plugging at boot time). Thus we shouldn't
848 * fail if the module was loaded, and not by us.
849 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000850 if (already_loaded(mod))
Rob Landley4b5827a2006-08-22 23:50:11 +0000851 rc = 0;
852 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000853 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000854 return rc;
855}
856
Denis Vlasenkocf704332006-10-27 15:12:50 +0000857static int mod_remove(char *mod)
Robert Griebl52e8d062002-05-14 23:42:08 +0000858{
Eric Andersen908e3622003-06-20 09:56:37 +0000859 int rc;
Denis Vlasenko322661d2007-01-29 23:43:52 +0000860 static const struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL };
Eric Andersen3b1a7442003-12-24 20:30:45 +0000861
Denis Vlasenko322661d2007-01-29 23:43:52 +0000862 struct mod_list_t *head = NULL;
863 struct mod_list_t *tail = NULL;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000864
Denis Vlasenkocf704332006-10-27 15:12:50 +0000865 if (mod)
866 check_dep(mod, &head, &tail);
Robert Griebl52e8d062002-05-14 23:42:08 +0000867 else // autoclean
Denis Vlasenko322661d2007-01-29 23:43:52 +0000868 head = tail = (struct mod_list_t*) &rm_a_dummy;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000869
Denis Vlasenko322661d2007-01-29 23:43:52 +0000870 rc = 1;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000871 if (head && tail)
872 rc = mod_process(head, 0); // process head ---> tail
Eric Andersen908e3622003-06-20 09:56:37 +0000873 return rc;
Robert Griebl52e8d062002-05-14 23:42:08 +0000874}
875
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000876int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
877int modprobe_main(int argc, char **argv)
Eric Andersen0139ca92001-07-22 23:01:03 +0000878{
Rob Landleye9190962005-12-12 19:38:44 +0000879 int rc = EXIT_SUCCESS;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000880 char *unused;
Eric Andersen0139ca92001-07-22 23:01:03 +0000881
Denis Vlasenkoe7fca512007-11-10 01:32:18 +0000882 opt_complementary = "q-v:v-q";
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000883 getopt32(argv, MAIN_OPT_STR, &unused, &unused);
884
885 if (option_mask32 & (DUMP_CONF_EXIT | LIST_ALL))
Denis Vlasenkocf704332006-10-27 15:12:50 +0000886 return EXIT_SUCCESS;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000887 if (option_mask32 & (RESTRICT_DIR | CONFIG_FILE))
Denis Vlasenkocf704332006-10-27 15:12:50 +0000888 bb_error_msg_and_die("-t and -C not supported");
Robert Griebl236abbf2002-05-22 23:34:35 +0000889
Denis Vlasenkocf704332006-10-27 15:12:50 +0000890 depend = build_dep();
Robert Griebl52e8d062002-05-14 23:42:08 +0000891
Denis Vlasenkocf704332006-10-27 15:12:50 +0000892 if (!depend)
Bernhard Reutner-Fischerb85fb692008-05-27 10:55:34 +0000893 bb_error_msg_and_die("cannot parse "CONFIG_DEFAULT_DEPMOD_FILE);
Eric Andersen3b1a7442003-12-24 20:30:45 +0000894
Eric Andersen1b064192001-07-25 07:23:38 +0000895 if (remove_opt) {
Eric Andersen0139ca92001-07-22 23:01:03 +0000896 do {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000897 /* argv[optind] can be NULL here */
898 if (mod_remove(argv[optind])) {
Bernhard Reutner-Fischere0fd13e2008-05-31 18:50:17 +0000899 bb_error_msg("failed to %s module %s", "remove",
Denis Vlasenkocf704332006-10-27 15:12:50 +0000900 argv[optind]);
Eric Andersen908e3622003-06-20 09:56:37 +0000901 rc = EXIT_FAILURE;
902 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000903 } while (++optind < argc);
Rob Landleye9190962005-12-12 19:38:44 +0000904 } else {
905 if (optind >= argc)
Denis Vlasenkocf704332006-10-27 15:12:50 +0000906 bb_error_msg_and_die("no module or pattern provided");
Eric Andersen3b1a7442003-12-24 20:30:45 +0000907
Denis Vlasenkocf704332006-10-27 15:12:50 +0000908 if (mod_insert(argv[optind], argc - optind - 1, argv + optind + 1))
Bernhard Reutner-Fischere0fd13e2008-05-31 18:50:17 +0000909 bb_error_msg_and_die("failed to %s module %s", "load", argv[optind]);
Eric Andersen0139ca92001-07-22 23:01:03 +0000910 }
911
Rob Landleye9190962005-12-12 19:38:44 +0000912 /* Here would be a good place to free up memory allocated during the dependencies build. */
Eric Andersen3b1a7442003-12-24 20:30:45 +0000913
Rob Landleye9190962005-12-12 19:38:44 +0000914 return rc;
Eric Andersen0139ca92001-07-22 23:01:03 +0000915}