blob: 412e71d16b7535809432c37b90186f7552679ae4 [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
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +000051struct include_conf_t {
52 struct dep_t *first;
53 struct dep_t *current;
54};
Robert Griebl52e8d062002-05-14 23:42:08 +000055
Robert Griebl236abbf2002-05-22 23:34:35 +000056static struct dep_t *depend;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +000057
Denis Vlasenkob68979a2007-11-02 23:31:10 +000058#define MAIN_OPT_STR "acdklnqrst:vVC:"
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +000059#define INSERT_ALL 1 /* a */
60#define DUMP_CONF_EXIT 2 /* c */
61#define D_OPT_IGNORED 4 /* d */
62#define AUTOCLEAN_FLG 8 /* k */
63#define LIST_ALL 16 /* l */
64#define SHOW_ONLY 32 /* n */
65#define QUIET 64 /* q */
66#define REMOVE_OPT 128 /* r */
67#define DO_SYSLOG 256 /* s */
68#define RESTRICT_DIR 512 /* t */
69#define VERBOSE 1024 /* v */
70#define VERSION_ONLY 2048 /* V */
71#define CONFIG_FILE 4096 /* C */
72
Denis Vlasenkob68979a2007-11-02 23:31:10 +000073#define autoclean (option_mask32 & AUTOCLEAN_FLG)
74#define show_only (option_mask32 & SHOW_ONLY)
75#define quiet (option_mask32 & QUIET)
76#define remove_opt (option_mask32 & REMOVE_OPT)
77#define do_syslog (option_mask32 & DO_SYSLOG)
78#define verbose (option_mask32 & VERBOSE)
Robert Griebl52e8d062002-05-14 23:42:08 +000079
Denis Vlasenkocf704332006-10-27 15:12:50 +000080static int parse_tag_value(char *buffer, char **ptag, char **pvalue)
Robert Grieblaead70b2002-07-26 15:54:20 +000081{
82 char *tag, *value;
83
Denis Vlasenkocf704332006-10-27 15:12:50 +000084 buffer = skip_whitespace(buffer);
Robert Grieblaead70b2002-07-26 15:54:20 +000085 tag = value = buffer;
Denis Vlasenkob68979a2007-11-02 23:31:10 +000086 while (!isspace(*value)) {
87 if (!*value)
88 return 0;
89 value++;
90 }
91 *value++ = '\0';
Denis Vlasenkocf704332006-10-27 15:12:50 +000092 value = skip_whitespace(value);
Denis Vlasenkob68979a2007-11-02 23:31:10 +000093 if (!*value)
94 return 0;
Robert Grieblaead70b2002-07-26 15:54:20 +000095
96 *ptag = tag;
97 *pvalue = value;
Eric Andersen3b1a7442003-12-24 20:30:45 +000098
Eric Andersen7e496a72004-04-06 12:06:03 +000099 return 1;
Robert Grieblaead70b2002-07-26 15:54:20 +0000100}
Eric Andersen60e56f52002-04-26 06:04:01 +0000101
Rob Landleye9190962005-12-12 19:38:44 +0000102/*
103 * This function appends an option to a list
104 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000105static struct mod_opt_t *append_option(struct mod_opt_t *opt_list, char *opt)
Eric Andersen60e56f52002-04-26 06:04:01 +0000106{
Rob Landleye9190962005-12-12 19:38:44 +0000107 struct mod_opt_t *ol = opt_list;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000108
Denis Vlasenkocf704332006-10-27 15:12:50 +0000109 if (ol) {
110 while (ol->m_next) {
111 ol = ol->m_next;
Rob Landleye9190962005-12-12 19:38:44 +0000112 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000113 ol->m_next = xzalloc(sizeof(struct mod_opt_t));
Denis Vlasenkocf704332006-10-27 15:12:50 +0000114 ol = ol->m_next;
Rob Landleye9190962005-12-12 19:38:44 +0000115 } else {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000116 ol = opt_list = xzalloc(sizeof(struct mod_opt_t));
Eric Andersen03d80912003-12-19 21:04:19 +0000117 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000118
Denis Vlasenkocf704332006-10-27 15:12:50 +0000119 ol->m_opt_val = xstrdup(opt);
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000120 /*ol->m_next = NULL; - done by xzalloc*/
Eric Andersen60e56f52002-04-26 06:04:01 +0000121
Rob Landleye9190962005-12-12 19:38:44 +0000122 return opt_list;
Eric Andersen60e56f52002-04-26 06:04:01 +0000123}
124
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000125#if ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS
Denis Vlasenkocf704332006-10-27 15:12:50 +0000126/* static char* parse_command_string(char* src, char **dst);
Rob Landley79e1cab2005-11-15 00:08:29 +0000127 * src: pointer to string containing argument
128 * dst: pointer to where to store the parsed argument
129 * return value: the pointer to the first char after the parsed argument,
130 * NULL if there was no argument parsed (only trailing spaces).
Rob Landleyd921b2e2006-08-03 15:41:12 +0000131 * Note that memory is allocated with xstrdup when a new argument was
Rob Landley79e1cab2005-11-15 00:08:29 +0000132 * parsed. Don't forget to free it!
133 */
134#define ARG_EMPTY 0x00
135#define ARG_IN_DQUOTES 0x01
136#define ARG_IN_SQUOTES 0x02
Denis Vlasenkocf704332006-10-27 15:12:50 +0000137static char *parse_command_string(char *src, char **dst)
Rob Landley79e1cab2005-11-15 00:08:29 +0000138{
139 int opt_status = ARG_EMPTY;
140 char* tmp_str;
141
142 /* Dumb you, I have nothing to do... */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000143 if (src == NULL) return src;
Rob Landley79e1cab2005-11-15 00:08:29 +0000144
145 /* Skip leading spaces */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000146 while (*src == ' ') {
Rob Landley79e1cab2005-11-15 00:08:29 +0000147 src++;
148 }
149 /* Is the end of string reached? */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000150 if (*src == '\0') {
Rob Landley79e1cab2005-11-15 00:08:29 +0000151 return NULL;
152 }
153 /* Reached the start of an argument
Rob Landleye9190962005-12-12 19:38:44 +0000154 * By the way, we duplicate a little too much
155 * here but what is too much is freed later. */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000156 *dst = tmp_str = xstrdup(src);
Rob Landley79e1cab2005-11-15 00:08:29 +0000157 /* Get to the end of that argument */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000158 while (*tmp_str != '\0'
159 && (*tmp_str != ' ' || (opt_status & (ARG_IN_DQUOTES | ARG_IN_SQUOTES)))
160 ) {
161 switch (*tmp_str) {
162 case '\'':
163 if (opt_status & ARG_IN_DQUOTES) {
164 /* Already in double quotes, keep current char as is */
165 } else {
166 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
167 memmove(tmp_str, tmp_str + 1, strlen(tmp_str));
168 /* mark me: we enter or leave single quotes */
169 opt_status ^= ARG_IN_SQUOTES;
170 /* Back one char, as we need to re-scan the new char there. */
171 tmp_str--;
172 }
Rob Landley79e1cab2005-11-15 00:08:29 +0000173 break;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000174 case '"':
175 if (opt_status & ARG_IN_SQUOTES) {
176 /* Already in single quotes, keep current char as is */
177 } else {
178 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
179 memmove(tmp_str, tmp_str + 1, strlen(tmp_str));
180 /* mark me: we enter or leave double quotes */
181 opt_status ^= ARG_IN_DQUOTES;
182 /* Back one char, as we need to re-scan the new char there. */
183 tmp_str--;
184 }
Rob Landley79e1cab2005-11-15 00:08:29 +0000185 break;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000186 case '\\':
187 if (opt_status & ARG_IN_SQUOTES) {
188 /* Between single quotes: keep as is. */
189 } else {
190 switch (*(tmp_str+1)) {
191 case 'a':
192 case 'b':
193 case 't':
194 case 'n':
195 case 'v':
196 case 'f':
197 case 'r':
198 case '0':
199 /* We escaped a special character. For now, keep
200 * both the back-slash and the following char. */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000201 tmp_str++;
202 src++;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000203 break;
204 default:
205 /* We escaped a space or a single or double quote,
206 * or a back-slash, or a non-escapable char. Remove
207 * the '\' and keep the new current char as is. */
208 memmove(tmp_str, tmp_str + 1, strlen(tmp_str));
209 break;
Rob Landley79e1cab2005-11-15 00:08:29 +0000210 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000211 }
Rob Landley79e1cab2005-11-15 00:08:29 +0000212 break;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000213 /* Any other char that is special shall appear here.
214 * Example: $ starts a variable
215 case '$':
216 do_variable_expansion();
217 break;
218 * */
219 default:
220 /* any other char is kept as is. */
221 break;
Rob Landley79e1cab2005-11-15 00:08:29 +0000222 }
223 tmp_str++; /* Go to next char */
224 src++; /* Go to next char to find the end of the argument. */
225 }
226 /* End of string, but still no ending quote */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000227 if (opt_status & (ARG_IN_DQUOTES | ARG_IN_SQUOTES)) {
228 bb_error_msg_and_die("unterminated (single or double) quote in options list: %s", src);
Rob Landley79e1cab2005-11-15 00:08:29 +0000229 }
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000230 *tmp_str++ = '\0';
Denis Vlasenkocf704332006-10-27 15:12:50 +0000231 *dst = xrealloc(*dst, (tmp_str - *dst));
Rob Landley79e1cab2005-11-15 00:08:29 +0000232 return src;
233}
Rob Landleyae50c6d2005-12-15 07:42:13 +0000234#else
235#define parse_command_string(src, dst) (0)
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000236#endif /* ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
Rob Landley79e1cab2005-11-15 00:08:29 +0000237
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000238static int is_conf_command(char *buffer, const char *command)
Denis Vlasenkof45c4f42008-06-16 04:09:25 +0000239{
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000240 int len = strlen(command);
241 return ((strstr(buffer, command) == buffer) &&
Denis Vlasenkof45c4f42008-06-16 04:09:25 +0000242 isspace(buffer[len]));
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000243}
244
Rob Landleye9190962005-12-12 19:38:44 +0000245/*
Rob Landley72615752006-04-10 16:09:52 +0000246 * This function reads aliases and default module options from a configuration file
247 * (/etc/modprobe.conf syntax). It supports includes (only files, no directories).
248 */
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000249
250static int FAST_FUNC include_conf_file_act(const char *filename,
251 struct stat *statbuf UNUSED_PARAM,
252 void *userdata,
253 int depth UNUSED_PARAM);
254
255static int FAST_FUNC include_conf_dir_act(const char *filename UNUSED_PARAM,
256 struct stat *statbuf UNUSED_PARAM,
257 void *userdata UNUSED_PARAM,
258 int depth)
Rob Landley72615752006-04-10 16:09:52 +0000259{
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000260 if (depth > 1)
261 return SKIP;
262
263 return TRUE;
264}
265
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000266static int include_conf_recursive(struct include_conf_t *conf, const char *filename)
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000267{
268 return recursive_action(filename, ACTION_RECURSE,
269 include_conf_file_act,
270 include_conf_dir_act,
271 conf, 1);
272}
273
274static int FAST_FUNC include_conf_file_act(const char *filename,
275 struct stat *statbuf UNUSED_PARAM,
276 void *userdata,
277 int depth UNUSED_PARAM)
278{
279 struct include_conf_t *conf = (struct include_conf_t *) userdata;
280 struct dep_t **first = &conf->first;
281 struct dep_t **current = &conf->current;
Rob Landley72615752006-04-10 16:09:52 +0000282 int continuation_line = 0;
Denis Vlasenko855ff6f2008-08-04 21:16:46 +0000283 FILE *f;
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000284
285 if (bb_basename(filename)[0] == '.')
286 return TRUE;
287
Denis Vlasenko855ff6f2008-08-04 21:16:46 +0000288 f = fopen_for_read(filename);
289 if (f == NULL)
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000290 return FALSE;
Rob Landley72615752006-04-10 16:09:52 +0000291
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000292 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias)!
Rob Landley72615752006-04-10 16:09:52 +0000293
Denis Vlasenko855ff6f2008-08-04 21:16:46 +0000294 while (fgets(line_buffer, sizeof(line_buffer), f)) {
Rob Landley72615752006-04-10 16:09:52 +0000295 int l;
Rob Landley72615752006-04-10 16:09:52 +0000296
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000297 *strchrnul(line_buffer, '#') = '\0';
Rob Landley72615752006-04-10 16:09:52 +0000298
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000299 l = strlen(line_buffer);
Rob Landley72615752006-04-10 16:09:52 +0000300
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000301 while (l && isspace(line_buffer[l-1])) {
302 line_buffer[l-1] = '\0';
Rob Landley72615752006-04-10 16:09:52 +0000303 l--;
304 }
305
Denis Vlasenkocf704332006-10-27 15:12:50 +0000306 if (l == 0) {
Rob Landley72615752006-04-10 16:09:52 +0000307 continuation_line = 0;
308 continue;
309 }
310
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000311 if (continuation_line)
312 continue;
Rob Landley72615752006-04-10 16:09:52 +0000313
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000314 if (is_conf_command(line_buffer, "alias")) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000315 char *alias, *mod;
Rob Landley72615752006-04-10 16:09:52 +0000316
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000317 if (parse_tag_value(line_buffer + 6, &alias, &mod)) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000318 /* handle alias as a module dependent on the aliased module */
319 if (!*current) {
320 (*first) = (*current) = xzalloc(sizeof(struct dep_t));
321 } else {
322 (*current)->m_next = xzalloc(sizeof(struct dep_t));
323 (*current) = (*current)->m_next;
Rob Landley72615752006-04-10 16:09:52 +0000324 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000325 (*current)->m_name = xstrdup(alias);
326 (*current)->m_isalias = 1;
Rob Landley72615752006-04-10 16:09:52 +0000327
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000328 if ((strcmp(mod, "off") == 0) || (strcmp(mod, "null") == 0)) {
329 /*(*current)->m_depcnt = 0; - done by xzalloc */
330 /*(*current)->m_deparr = 0;*/
331 } else {
332 (*current)->m_depcnt = 1;
333 (*current)->m_deparr = xmalloc(sizeof(char *));
334 (*current)->m_deparr[0] = xstrdup(mod);
335 }
336 /*(*current)->m_next = NULL; - done by xzalloc */
337 }
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000338 } else if (is_conf_command(line_buffer, "options")) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000339 char *mod, *opt;
Rob Landley72615752006-04-10 16:09:52 +0000340
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000341 /* split the line in the module/alias name, and options */
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000342 if (parse_tag_value(line_buffer + 8, &mod, &opt)) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000343 struct dep_t *dt;
344
345 /* find the corresponding module */
346 for (dt = *first; dt; dt = dt->m_next) {
347 if (strcmp(dt->m_name, mod) == 0)
348 break;
349 }
350 if (dt) {
351 if (ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS) {
352 char* new_opt = NULL;
353 while ((opt = parse_command_string(opt, &new_opt))) {
354 dt->m_options = append_option(dt->m_options, new_opt);
Rob Landley72615752006-04-10 16:09:52 +0000355 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000356 } else {
357 dt->m_options = append_option(dt->m_options, opt);
Rob Landley72615752006-04-10 16:09:52 +0000358 }
359 }
Rob Landley72615752006-04-10 16:09:52 +0000360 }
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000361 } else if (is_conf_command(line_buffer, "include")) {
362 char *includefile;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000363
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000364 includefile = skip_whitespace(line_buffer + 8);
365 include_conf_recursive(conf, includefile);
Denis Vlasenkof45c4f42008-06-16 04:09:25 +0000366 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST &&
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000367 (is_conf_command(line_buffer, "blacklist"))) {
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000368 char *mod;
369 struct dep_t *dt;
370
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000371 mod = skip_whitespace(line_buffer + 10);
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000372 for (dt = *first; dt; dt = dt->m_next) {
Denis Vlasenkoae84b112008-05-22 17:37:38 +0000373 if (strcmp(dt->m_name, mod) == 0)
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000374 break;
375 }
376 if (dt)
377 dt->m_isblacklisted = 1;
Rob Landley72615752006-04-10 16:09:52 +0000378 }
Denis Vlasenko855ff6f2008-08-04 21:16:46 +0000379 } /* while (fgets(...)) */
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000380
Denis Vlasenko855ff6f2008-08-04 21:16:46 +0000381 fclose(f);
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000382 return TRUE;
383}
384
385static int include_conf_file(struct include_conf_t *conf,
386 const char *filename)
387{
388 return include_conf_file_act(filename, NULL, conf, 0);
389}
390
391static int include_conf_file2(struct include_conf_t *conf,
392 const char *filename, const char *oldname)
393{
394 if (include_conf_file(conf, filename) == TRUE)
395 return TRUE;
396 return include_conf_file(conf, oldname);
Rob Landley72615752006-04-10 16:09:52 +0000397}
398
399/*
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000400 * This function builds a list of dependency rules from /lib/modules/`uname -r`/modules.dep.
Bernhard Reutner-Fischere3c150b2006-05-19 11:24:28 +0000401 * It then fills every modules and aliases with their default options, found by parsing
Rob Landleye9190962005-12-12 19:38:44 +0000402 * modprobe.conf (or modules.conf, or conf.modules).
403 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000404static struct dep_t *build_dep(void)
Rob Landleye9190962005-12-12 19:38:44 +0000405{
Denis Vlasenko855ff6f2008-08-04 21:16:46 +0000406 FILE *f;
Rob Landleye9190962005-12-12 19:38:44 +0000407 struct utsname un;
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000408 struct include_conf_t conf = { NULL, NULL };
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000409 char *filename;
Rob Landleye9190962005-12-12 19:38:44 +0000410 int continuation_line = 0;
411 int k_version;
412
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000413 uname(&un); /* never fails */
Rob Landleye9190962005-12-12 19:38:44 +0000414
Denis Vlasenkocf704332006-10-27 15:12:50 +0000415 k_version = 0;
Rob Landleye9190962005-12-12 19:38:44 +0000416 if (un.release[0] == '2') {
417 k_version = un.release[2] - '0';
418 }
419
Bernhard Reutner-Fischerb85fb692008-05-27 10:55:34 +0000420 filename = xasprintf(CONFIG_DEFAULT_MODULES_DIR"/%s/"CONFIG_DEFAULT_DEPMOD_FILE, un.release);
Denis Vlasenko855ff6f2008-08-04 21:16:46 +0000421 f = fopen_for_read(filename);
Bernhard Reutner-Fischere3c150b2006-05-19 11:24:28 +0000422 if (ENABLE_FEATURE_CLEAN_UP)
423 free(filename);
Denis Vlasenko855ff6f2008-08-04 21:16:46 +0000424 if (f == NULL) {
Rob Landleye9190962005-12-12 19:38:44 +0000425 /* Ok, that didn't work. Fall back to looking in /lib/modules */
Denis Vlasenko855ff6f2008-08-04 21:16:46 +0000426 f = fopen_for_read(CONFIG_DEFAULT_MODULES_DIR"/"CONFIG_DEFAULT_DEPMOD_FILE);
427 if (f == NULL) {
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000428 bb_error_msg_and_die("cannot parse "CONFIG_DEFAULT_DEPMOD_FILE);
Rob Landleye9190962005-12-12 19:38:44 +0000429 }
430 }
431
Denis Vlasenko855ff6f2008-08-04 21:16:46 +0000432 while (fgets(line_buffer, sizeof(line_buffer), f)) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000433 int l = strlen(line_buffer);
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000434 char *p = NULL;
Rob Landleye9190962005-12-12 19:38:44 +0000435
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000436 while (l > 0 && isspace(line_buffer[l-1])) {
437 line_buffer[l-1] = '\0';
Rob Landleye9190962005-12-12 19:38:44 +0000438 l--;
439 }
440
Denis Vlasenkocf704332006-10-27 15:12:50 +0000441 if (l == 0) {
Rob Landleye9190962005-12-12 19:38:44 +0000442 continuation_line = 0;
443 continue;
444 }
445
446 /* Is this a new module dep description? */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000447 if (!continuation_line) {
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000448 /* find the dep beginning */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000449 char *col = strchr(line_buffer, ':');
Rob Landleye9190962005-12-12 19:38:44 +0000450 char *dot = col;
451
Denis Vlasenkocf704332006-10-27 15:12:50 +0000452 if (col) {
Rob Landleye9190962005-12-12 19:38:44 +0000453 /* This line is a dep description */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000454 const char *mods;
Rob Landleye9190962005-12-12 19:38:44 +0000455 char *modpath;
456 char *mod;
457
458 /* Find the beginning of the module file name */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000459 *col = '\0';
460 mods = bb_basename(line_buffer);
Rob Landleye9190962005-12-12 19:38:44 +0000461
462 /* find the path of the module */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000463 modpath = strchr(line_buffer, '/'); /* ... and this is the path */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000464 if (!modpath)
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000465 modpath = line_buffer; /* module with no path */
Rob Landleye9190962005-12-12 19:38:44 +0000466 /* find the end of the module name in the file name */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000467 if (ENABLE_FEATURE_2_6_MODULES &&
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000468 (k_version > 4) && (col[-3] == '.') &&
469 (col[-2] == 'k') && (col[-1] == 'o'))
Rob Landleye9190962005-12-12 19:38:44 +0000470 dot = col - 3;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000471 else if ((col[-2] == '.') && (col[-1] == 'o'))
472 dot = col - 2;
Rob Landleye9190962005-12-12 19:38:44 +0000473
Denis Vlasenkocf704332006-10-27 15:12:50 +0000474 mod = xstrndup(mods, dot - mods);
Rob Landleye9190962005-12-12 19:38:44 +0000475
476 /* enqueue new module */
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000477 if (!conf.current) {
478 conf.first = conf.current = xzalloc(sizeof(struct dep_t));
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000479 } else {
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000480 conf.current->m_next = xzalloc(sizeof(struct dep_t));
481 conf.current = conf.current->m_next;
Rob Landleye9190962005-12-12 19:38:44 +0000482 }
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000483 conf.current->m_name = mod;
484 conf.current->m_path = xstrdup(modpath);
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000485 /*current->m_options = NULL; - xzalloc did it*/
486 /*current->m_isalias = 0;*/
487 /*current->m_depcnt = 0;*/
488 /*current->m_deparr = 0;*/
489 /*current->m_next = 0;*/
Rob Landleye9190962005-12-12 19:38:44 +0000490
491 p = col + 1;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000492 } else
Rob Landleye9190962005-12-12 19:38:44 +0000493 /* this line is not a dep description */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000494 p = NULL;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000495 } else
Rob Landleye9190962005-12-12 19:38:44 +0000496 /* It's a dep description continuation */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000497 p = line_buffer;
Rob Landleye9190962005-12-12 19:38:44 +0000498
Rob Landleye9190962005-12-12 19:38:44 +0000499 /* p points to the first dependable module; if NULL, no dependable module */
Denis Vlasenko8124a962008-06-22 16:59:46 +0000500 if (p && (p = skip_whitespace(p))[0] != '\0') {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000501 char *end = &line_buffer[l-1];
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000502 const char *deps;
Rob Landleye9190962005-12-12 19:38:44 +0000503 char *dep;
504 char *next;
505 int ext = 0;
506
Denis Vlasenkocf704332006-10-27 15:12:50 +0000507 while (isblank(*end) || (*end == '\\'))
Rob Landleye9190962005-12-12 19:38:44 +0000508 end--;
509
Denis Vlasenkocf704332006-10-27 15:12:50 +0000510 do {
Rob Landleye9190962005-12-12 19:38:44 +0000511 /* search the end of the dependency */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000512 next = strchr(p, ' ');
513 if (next) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000514 *next = '\0';
Rob Landleye9190962005-12-12 19:38:44 +0000515 next--;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000516 } else
Rob Landleye9190962005-12-12 19:38:44 +0000517 next = end;
518
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000519 /* find the beginning of the module file name */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000520 deps = bb_basename(p);
Bernhard Reutner-Fischere0fd13e2008-05-31 18:50:17 +0000521 if (deps == p)
522 deps = skip_whitespace(deps);
Rob Landleye9190962005-12-12 19:38:44 +0000523
524 /* find the end of the module name in the file name */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000525 if (ENABLE_FEATURE_2_6_MODULES
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000526 && (k_version > 4) && (next[-2] == '.')
527 && (next[-1] == 'k') && (next[0] == 'o'))
Rob Landleye9190962005-12-12 19:38:44 +0000528 ext = 3;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000529 else if ((next[-1] == '.') && (next[0] == 'o'))
530 ext = 2;
Rob Landleye9190962005-12-12 19:38:44 +0000531
532 /* Cope with blank lines */
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000533 if ((next - deps - ext + 1) <= 0)
Rob Landleye9190962005-12-12 19:38:44 +0000534 continue;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000535 dep = xstrndup(deps, next - deps - ext + 1);
Rob Landleye9190962005-12-12 19:38:44 +0000536
537 /* Add the new dependable module name */
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000538 conf.current->m_deparr = xrealloc_vector(conf.current->m_deparr, 2, conf.current->m_depcnt);
539 conf.current->m_deparr[conf.current->m_depcnt++] = dep;
Rob Landleye9190962005-12-12 19:38:44 +0000540
541 p = next + 2;
542 } while (next < end);
543 }
544
545 /* is there other dependable module(s) ? */
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000546 continuation_line = (line_buffer[l-1] == '\\');
Denis Vlasenko855ff6f2008-08-04 21:16:46 +0000547 } /* while (fgets(...)) */
548 fclose(f);
Rob Landleye9190962005-12-12 19:38:44 +0000549
Rob Landley3b0cfb42006-07-19 21:33:42 +0000550 /*
551 * First parse system-specific options and aliases
552 * as they take precedence over the kernel ones.
Mike Frysinger4423e5b2007-02-08 07:03:44 +0000553 * >=2.6: we only care about modprobe.conf
554 * <=2.4: we care about modules.conf and conf.modules
Rob Landley3b0cfb42006-07-19 21:33:42 +0000555 */
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000556 {
557 int r = FALSE;
Rob Landleye9190962005-12-12 19:38:44 +0000558
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000559 if (ENABLE_FEATURE_2_6_MODULES) {
560 if (include_conf_file(&conf, "/etc/modprobe.conf"))
561 r = TRUE;
562 if (include_conf_recursive(&conf, "/etc/modprobe.d"))
563 r = TRUE;
564 }
565 if (ENABLE_FEATURE_2_4_MODULES && !r)
566 include_conf_file2(&conf,
567 "/etc/modules.conf",
568 "/etc/conf.modules");
Rob Landleye9190962005-12-12 19:38:44 +0000569 }
Rob Landley72615752006-04-10 16:09:52 +0000570
Rob Landley3b0cfb42006-07-19 21:33:42 +0000571 /* Only 2.6 has a modules.alias file */
572 if (ENABLE_FEATURE_2_6_MODULES) {
Denis Vlasenkof8483052007-08-16 10:40:06 +0000573 /* Parse kernel-declared module aliases */
Bernhard Reutner-Fischerb85fb692008-05-27 10:55:34 +0000574 filename = xasprintf(CONFIG_DEFAULT_MODULES_DIR"/%s/modules.alias", un.release);
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000575 include_conf_file2(&conf,
576 filename,
577 CONFIG_DEFAULT_MODULES_DIR"/modules.alias");
Rob Landley3b0cfb42006-07-19 21:33:42 +0000578 if (ENABLE_FEATURE_CLEAN_UP)
579 free(filename);
580
Denis Vlasenkof8483052007-08-16 10:40:06 +0000581 /* Parse kernel-declared symbol aliases */
Bernhard Reutner-Fischerb85fb692008-05-27 10:55:34 +0000582 filename = xasprintf(CONFIG_DEFAULT_MODULES_DIR"/%s/modules.symbols", un.release);
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000583 include_conf_file2(&conf,
584 filename,
585 CONFIG_DEFAULT_MODULES_DIR"/modules.symbols");
Denis Vlasenkof8483052007-08-16 10:40:06 +0000586 if (ENABLE_FEATURE_CLEAN_UP)
587 free(filename);
Rob Landley3b0cfb42006-07-19 21:33:42 +0000588 }
Rob Landleye9190962005-12-12 19:38:44 +0000589
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000590 return conf.first;
Rob Landleye9190962005-12-12 19:38:44 +0000591}
592
593/* return 1 = loaded, 0 = not loaded, -1 = can't tell */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000594static int already_loaded(const char *name)
Rob Landleye9190962005-12-12 19:38:44 +0000595{
Denis Vlasenko855ff6f2008-08-04 21:16:46 +0000596 FILE *f;
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000597 int ret;
Rob Landleye9190962005-12-12 19:38:44 +0000598
Denis Vlasenko855ff6f2008-08-04 21:16:46 +0000599 f = fopen_for_read("/proc/modules");
600 if (f == NULL)
Rob Landleye9190962005-12-12 19:38:44 +0000601 return -1;
602
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000603 ret = 0;
Denis Vlasenko855ff6f2008-08-04 21:16:46 +0000604 while (fgets(line_buffer, sizeof(line_buffer), f)) {
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000605 char *p = line_buffer;
606 const char *n = name;
Rob Landleye9190962005-12-12 19:38:44 +0000607
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000608 while (1) {
609 char cn = *n;
610 char cp = *p;
611 if (cp == ' ' || cp == '\0') {
612 if (cn == '\0') {
613 ret = 1; /* match! */
Rob Landleyd7605602006-06-14 01:51:16 +0000614 goto done;
615 }
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000616 break; /* no match on this line, take next one */
Rob Landleye9190962005-12-12 19:38:44 +0000617 }
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000618 if (cn == '-') cn = '_';
619 if (cp == '-') cp = '_';
620 if (cp != cn)
621 break; /* no match on this line, take next one */
622 n++;
623 p++;
Rob Landleye9190962005-12-12 19:38:44 +0000624 }
625 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000626 done:
Denis Vlasenko855ff6f2008-08-04 21:16:46 +0000627 fclose(f);
Mike Frysinger135cee32006-06-21 23:03:37 +0000628 return ret;
Rob Landleye9190962005-12-12 19:38:44 +0000629}
630
Denis Vlasenko322661d2007-01-29 23:43:52 +0000631static int mod_process(const struct mod_list_t *list, int do_insert)
Eric Andersen60e56f52002-04-26 06:04:01 +0000632{
Eric Andersen44b57582004-08-03 08:23:33 +0000633 int rc = 0;
Rob Landley79e1cab2005-11-15 00:08:29 +0000634 char **argv = NULL;
Rob Landleye9190962005-12-12 19:38:44 +0000635 struct mod_opt_t *opts;
636 int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */
Paul Fox8eeb6552005-08-04 18:33:36 +0000637 int argc;
Eric Andersen864b7972002-05-03 15:48:26 +0000638
Denis Vlasenkocf704332006-10-27 15:12:50 +0000639 while (list) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000640 argc = 0;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000641 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleye9190962005-12-12 19:38:44 +0000642 argc_malloc = 0;
643 /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory
644 * each time we allocate memory for argv.
645 * But it is (quite) small amounts of memory that leak each
646 * time a module is loaded, and it is reclaimed when modprobe
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000647 * exits anyway (even when standalone shell? Yes --vda).
Rob Landleye9190962005-12-12 19:38:44 +0000648 * This could become a problem when loading a module with LOTS of
649 * dependencies, with LOTS of options for each dependencies, with
650 * very little memory on the target... But in that case, the module
651 * would not load because there is no more memory, so there's no
652 * problem. */
653 /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000654 argv = xmalloc(6 * sizeof(char*));
655 if (do_insert) {
656 if (already_loaded(list->m_name) != 1) {
Denis Vlasenko322661d2007-01-29 23:43:52 +0000657 argv[argc++] = (char*)"insmod";
Rob Landleyd7605602006-06-14 01:51:16 +0000658 if (ENABLE_FEATURE_2_4_MODULES) {
659 if (do_syslog)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000660 argv[argc++] = (char*)"-s";
Rob Landleyd7605602006-06-14 01:51:16 +0000661 if (autoclean)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000662 argv[argc++] = (char*)"-k";
Rob Landleyd7605602006-06-14 01:51:16 +0000663 if (quiet)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000664 argv[argc++] = (char*)"-q";
Denis Vlasenkocf704332006-10-27 15:12:50 +0000665 else if (verbose) /* verbose and quiet are mutually exclusive */
Denis Vlasenko322661d2007-01-29 23:43:52 +0000666 argv[argc++] = (char*)"-v";
Rob Landleyd7605602006-06-14 01:51:16 +0000667 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000668 argv[argc++] = list->m_path;
669 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleye9190962005-12-12 19:38:44 +0000670 argc_malloc = argc;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000671 opts = list->m_options;
672 while (opts) {
Rob Landleye9190962005-12-12 19:38:44 +0000673 /* Add one more option */
Rob Landley79e1cab2005-11-15 00:08:29 +0000674 argc++;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000675 argv = xrealloc(argv, (argc + 1) * sizeof(char*));
Denis Vlasenkocf704332006-10-27 15:12:50 +0000676 argv[argc-1] = opts->m_opt_val;
677 opts = opts->m_next;
Rob Landley79e1cab2005-11-15 00:08:29 +0000678 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000679 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000680 } else {
Eric Andersen807bd842004-08-19 18:30:31 +0000681 /* modutils uses short name for removal */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000682 if (already_loaded(list->m_name) != 0) {
Denis Vlasenko322661d2007-01-29 23:43:52 +0000683 argv[argc++] = (char*)"rmmod";
Paul Fox8eeb6552005-08-04 18:33:36 +0000684 if (do_syslog)
Denis Vlasenko322661d2007-01-29 23:43:52 +0000685 argv[argc++] = (char*)"-s";
686 argv[argc++] = (char*)list->m_name;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000687 if (ENABLE_FEATURE_CLEAN_UP)
Rob Landleye9190962005-12-12 19:38:44 +0000688 argc_malloc = argc;
Paul Fox8eeb6552005-08-04 18:33:36 +0000689 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000690 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000691 argv[argc] = NULL;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000692
Paul Fox8eeb6552005-08-04 18:33:36 +0000693 if (argc) {
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000694 if (verbose) {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000695 printf("%s module %s\n", do_insert?"Loading":"Unloading", list->m_name);
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000696 }
697 if (!show_only) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000698 int rc2 = wait4pid(spawn(argv));
Rob Landley4b5827a2006-08-22 23:50:11 +0000699
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000700 if (do_insert) {
701 rc = rc2; /* only last module matters */
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000702 } else if (!rc2) {
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000703 rc = 0; /* success if remove any mod */
704 }
705 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000706 if (ENABLE_FEATURE_CLEAN_UP) {
Rob Landleye9190962005-12-12 19:38:44 +0000707 /* the last value in the array has index == argc, but
708 * it is the terminating NULL, so we must not free it. */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000709 while (argc_malloc < argc) {
710 free(argv[argc_malloc++]);
711 }
Rob Landley79e1cab2005-11-15 00:08:29 +0000712 }
Eric Andersen908e3622003-06-20 09:56:37 +0000713 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000714 if (ENABLE_FEATURE_CLEAN_UP) {
715 free(argv);
Rob Landleye9190962005-12-12 19:38:44 +0000716 argv = NULL;
Rob Landley79e1cab2005-11-15 00:08:29 +0000717 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000718 list = do_insert ? list->m_prev : list->m_next;
Eric Andersen60e56f52002-04-26 06:04:01 +0000719 }
Eric Andersen908e3622003-06-20 09:56:37 +0000720 return (show_only) ? 0 : rc;
Eric Andersen60e56f52002-04-26 06:04:01 +0000721}
722
Rob Landleye9190962005-12-12 19:38:44 +0000723/*
Rob Landleybf30c692006-07-20 17:36:18 +0000724 * Check the matching between a pattern and a module name.
725 * We need this as *_* is equivalent to *-*, even in pattern matching.
726 */
Denis Vlasenkoac678ec2007-04-16 22:32:04 +0000727static int check_pattern(const char* pat_src, const char* mod_src)
728{
Rob Landleybf30c692006-07-20 17:36:18 +0000729 int ret;
730
731 if (ENABLE_FEATURE_MODPROBE_FANCY_ALIAS) {
732 char* pat;
733 char* mod;
734 char* p;
735
Denis Vlasenko6398cf42007-04-11 17:04:29 +0000736 pat = xstrdup(pat_src);
737 mod = xstrdup(mod_src);
Rob Landleybf30c692006-07-20 17:36:18 +0000738
Denis Vlasenkocf704332006-10-27 15:12:50 +0000739 for (p = pat; (p = strchr(p, '-')); *p++ = '_');
740 for (p = mod; (p = strchr(p, '-')); *p++ = '_');
Rob Landleybf30c692006-07-20 17:36:18 +0000741
Denis Vlasenkocf704332006-10-27 15:12:50 +0000742 ret = fnmatch(pat, mod, 0);
Rob Landleybf30c692006-07-20 17:36:18 +0000743
744 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkoff131b92007-04-10 15:42:06 +0000745 free(pat);
746 free(mod);
Rob Landleybf30c692006-07-20 17:36:18 +0000747 }
748
749 return ret;
Rob Landleybf30c692006-07-20 17:36:18 +0000750 }
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000751 return fnmatch(pat_src, mod_src, 0);
Rob Landleybf30c692006-07-20 17:36:18 +0000752}
753
754/*
Rob Landleye9190962005-12-12 19:38:44 +0000755 * Builds the dependency list (aka stack) of a module.
756 * head: the highest module in the stack (last to insmod, first to rmmod)
757 * tail: the lowest module in the stack (first to insmod, last to rmmod)
758 */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000759static void check_dep(char *mod, struct mod_list_t **head, struct mod_list_t **tail)
Eric Andersen60e56f52002-04-26 06:04:01 +0000760{
Robert Griebl52e8d062002-05-14 23:42:08 +0000761 struct mod_list_t *find;
Eric Andersen60e56f52002-04-26 06:04:01 +0000762 struct dep_t *dt;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000763 struct mod_opt_t *opt = NULL;
764 char *path = NULL;
Robert Griebl52e8d062002-05-14 23:42:08 +0000765
Bernhard Reutner-Fischer2c351a82006-06-03 19:08:49 +0000766 /* Search for the given module name amongst all dependency rules.
767 * The module name in a dependency rule can be a shell pattern,
768 * so try to match the given module name against such a pattern.
769 * Of course if the name in the dependency rule is a plain string,
770 * then we consider it a pattern, and matching will still work. */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000771 for (dt = depend; dt; dt = dt->m_next) {
772 if (check_pattern(dt->m_name, mod) == 0) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000773 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000774 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000775 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000776
Denis Vlasenkocf704332006-10-27 15:12:50 +0000777 if (!dt) {
778 bb_error_msg("module %s not found", mod);
Rob Landleye9190962005-12-12 19:38:44 +0000779 return;
780 }
781
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000782 // resolve alias names
Denis Vlasenkocf704332006-10-27 15:12:50 +0000783 while (dt->m_isalias) {
Denis Vlasenkoae84b112008-05-22 17:37:38 +0000784 if (dt->m_depcnt == 1) {
Robert Grieblaead70b2002-07-26 15:54:20 +0000785 struct dep_t *adt;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000786
Denis Vlasenkocf704332006-10-27 15:12:50 +0000787 for (adt = depend; adt; adt = adt->m_next) {
Denis Vlasenkoae84b112008-05-22 17:37:38 +0000788 if (check_pattern(adt->m_name, dt->m_deparr[0]) == 0 &&
789 !(ENABLE_FEATURE_MODPROBE_BLACKLIST &&
790 adt->m_isblacklisted))
Robert Grieblaead70b2002-07-26 15:54:20 +0000791 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000792 }
Denis Vlasenkocf704332006-10-27 15:12:50 +0000793 if (adt) {
Rob Landleye9190962005-12-12 19:38:44 +0000794 /* This is the module we are aliased to */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000795 struct mod_opt_t *opts = dt->m_options;
Rob Landleye9190962005-12-12 19:38:44 +0000796 /* Option of the alias are appended to the options of the module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000797 while (opts) {
798 adt->m_options = append_option(adt->m_options, opts->m_opt_val);
799 opts = opts->m_next;
Rob Landleye9190962005-12-12 19:38:44 +0000800 }
Eric Andersen716ccb22004-01-10 11:29:31 +0000801 dt = adt;
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000802 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000803 bb_error_msg("module %s not found", mod);
Robert Griebl70112da2002-07-29 20:28:38 +0000804 return;
Rob Landleye9190962005-12-12 19:38:44 +0000805 }
Mike Frysingerc5d9e8f2007-02-08 06:30:58 +0000806 } else {
Denis Vlasenkocf704332006-10-27 15:12:50 +0000807 bb_error_msg("bad alias %s", dt->m_name);
Eric Andersen716ccb22004-01-10 11:29:31 +0000808 return;
Rob Landleye9190962005-12-12 19:38:44 +0000809 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000810 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000811
Denis Vlasenkocf704332006-10-27 15:12:50 +0000812 mod = dt->m_name;
813 path = dt->m_path;
814 opt = dt->m_options;
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000815
Robert Griebl52e8d062002-05-14 23:42:08 +0000816 // search for duplicates
Denis Vlasenkocf704332006-10-27 15:12:50 +0000817 for (find = *head; find; find = find->m_next) {
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000818 if (strcmp(mod, find->m_name) == 0) {
819 // found -> dequeue it
Robert Griebl52e8d062002-05-14 23:42:08 +0000820
Denis Vlasenkocf704332006-10-27 15:12:50 +0000821 if (find->m_prev)
822 find->m_prev->m_next = find->m_next;
Robert Griebl52e8d062002-05-14 23:42:08 +0000823 else
Denis Vlasenkocf704332006-10-27 15:12:50 +0000824 *head = find->m_next;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000825
Denis Vlasenkocf704332006-10-27 15:12:50 +0000826 if (find->m_next)
827 find->m_next->m_prev = find->m_prev;
Robert Griebl52e8d062002-05-14 23:42:08 +0000828 else
Denis Vlasenkocf704332006-10-27 15:12:50 +0000829 *tail = find->m_prev;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000830
Robert Griebl52e8d062002-05-14 23:42:08 +0000831 break; // there can be only one duplicate
Eric Andersen716ccb22004-01-10 11:29:31 +0000832 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000833 }
834
Denis Vlasenkocf704332006-10-27 15:12:50 +0000835 if (!find) { // did not find a duplicate
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000836 find = xzalloc(sizeof(struct mod_list_t));
Denis Vlasenkocf704332006-10-27 15:12:50 +0000837 find->m_name = mod;
838 find->m_path = path;
839 find->m_options = opt;
Robert Griebl52e8d062002-05-14 23:42:08 +0000840 }
841
Eric Andersen716ccb22004-01-10 11:29:31 +0000842 // enqueue at tail
Denis Vlasenkocf704332006-10-27 15:12:50 +0000843 if (*tail)
844 (*tail)->m_next = find;
845 find->m_prev = *tail;
Denis Vlasenkocb12cb22007-11-06 11:34:03 +0000846 find->m_next = NULL; /* possibly NOT done by xzalloc! */
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000847
Denis Vlasenkocf704332006-10-27 15:12:50 +0000848 if (!*head)
Robert Griebl52e8d062002-05-14 23:42:08 +0000849 *head = find;
850 *tail = find;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000851
Denis Vlasenkocf704332006-10-27 15:12:50 +0000852 if (dt) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000853 int i;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000854
Rob Landleye9190962005-12-12 19:38:44 +0000855 /* Add all dependable module for that new module */
Denis Vlasenkocf704332006-10-27 15:12:50 +0000856 for (i = 0; i < dt->m_depcnt; i++)
857 check_dep(dt->m_deparr[i], head, tail);
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000858 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000859}
860
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000861static int mod_insert(char **argv)
Robert Griebl52e8d062002-05-14 23:42:08 +0000862{
Denis Vlasenko322661d2007-01-29 23:43:52 +0000863 struct mod_list_t *tail = NULL;
864 struct mod_list_t *head = NULL;
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000865 char *modname = *argv++;
Eric Andersen908e3622003-06-20 09:56:37 +0000866 int rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000867
Robert Griebl52e8d062002-05-14 23:42:08 +0000868 // get dep list for module mod
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000869 check_dep(modname, &head, &tail);
Eric Andersen3b1a7442003-12-24 20:30:45 +0000870
Denis Vlasenko322661d2007-01-29 23:43:52 +0000871 rc = 1;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000872 if (head && tail) {
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000873 while (*argv)
874 head->m_options = append_option(head->m_options, *argv++);
Eric Andersen3b1a7442003-12-24 20:30:45 +0000875
Robert Griebl52e8d062002-05-14 23:42:08 +0000876 // process tail ---> head
Denis Vlasenko322661d2007-01-29 23:43:52 +0000877 rc = mod_process(tail, 1);
878 if (rc) {
Rob Landley4b5827a2006-08-22 23:50:11 +0000879 /*
880 * In case of using udev, multiple instances of modprobe can be
881 * spawned to load the same module (think of two same usb devices,
882 * for example; or cold-plugging at boot time). Thus we shouldn't
883 * fail if the module was loaded, and not by us.
884 */
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000885 if (already_loaded(modname))
Rob Landley4b5827a2006-08-22 23:50:11 +0000886 rc = 0;
887 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000888 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000889 return rc;
890}
891
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000892static int mod_remove(char *modname)
Robert Griebl52e8d062002-05-14 23:42:08 +0000893{
Denis Vlasenko322661d2007-01-29 23:43:52 +0000894 static const struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL };
Eric Andersen3b1a7442003-12-24 20:30:45 +0000895
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000896 int rc;
Denis Vlasenko322661d2007-01-29 23:43:52 +0000897 struct mod_list_t *head = NULL;
898 struct mod_list_t *tail = NULL;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000899
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000900 if (modname)
901 check_dep(modname, &head, &tail);
Robert Griebl52e8d062002-05-14 23:42:08 +0000902 else // autoclean
Denis Vlasenko322661d2007-01-29 23:43:52 +0000903 head = tail = (struct mod_list_t*) &rm_a_dummy;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000904
Denis Vlasenko322661d2007-01-29 23:43:52 +0000905 rc = 1;
Denis Vlasenkocf704332006-10-27 15:12:50 +0000906 if (head && tail)
907 rc = mod_process(head, 0); // process head ---> tail
Eric Andersen908e3622003-06-20 09:56:37 +0000908 return rc;
Robert Griebl52e8d062002-05-14 23:42:08 +0000909}
910
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000911int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000912int modprobe_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen0139ca92001-07-22 23:01:03 +0000913{
Rob Landleye9190962005-12-12 19:38:44 +0000914 int rc = EXIT_SUCCESS;
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000915 unsigned opt;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000916 char *unused;
Eric Andersen0139ca92001-07-22 23:01:03 +0000917
Denis Vlasenkoe7fca512007-11-10 01:32:18 +0000918 opt_complementary = "q-v:v-q";
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000919 opt = getopt32(argv, MAIN_OPT_STR, &unused, &unused);
920 argv += optind;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000921
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000922 if (opt & (DUMP_CONF_EXIT | LIST_ALL))
Denis Vlasenkocf704332006-10-27 15:12:50 +0000923 return EXIT_SUCCESS;
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000924 if (opt & (RESTRICT_DIR | CONFIG_FILE))
Denis Vlasenkocf704332006-10-27 15:12:50 +0000925 bb_error_msg_and_die("-t and -C not supported");
Robert Griebl236abbf2002-05-22 23:34:35 +0000926
Denis Vlasenkocf704332006-10-27 15:12:50 +0000927 depend = build_dep();
Robert Griebl52e8d062002-05-14 23:42:08 +0000928
Denis Vlasenkocf704332006-10-27 15:12:50 +0000929 if (!depend)
Bernhard Reutner-Fischerb85fb692008-05-27 10:55:34 +0000930 bb_error_msg_and_die("cannot parse "CONFIG_DEFAULT_DEPMOD_FILE);
Eric Andersen3b1a7442003-12-24 20:30:45 +0000931
Eric Andersen1b064192001-07-25 07:23:38 +0000932 if (remove_opt) {
Eric Andersen0139ca92001-07-22 23:01:03 +0000933 do {
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000934 /* (*argv) can be NULL here */
935 if (mod_remove(*argv)) {
936 bb_perror_msg("failed to %s module %s", "remove",
937 *argv);
Eric Andersen908e3622003-06-20 09:56:37 +0000938 rc = EXIT_FAILURE;
939 }
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000940 } while (*argv && *++argv);
Rob Landleye9190962005-12-12 19:38:44 +0000941 } else {
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000942 if (!*argv)
Denis Vlasenkocf704332006-10-27 15:12:50 +0000943 bb_error_msg_and_die("no module or pattern provided");
Eric Andersen3b1a7442003-12-24 20:30:45 +0000944
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000945 if (mod_insert(argv))
946 bb_perror_msg_and_die("failed to %s module %s", "load", *argv);
Eric Andersen0139ca92001-07-22 23:01:03 +0000947 }
948
Rob Landleye9190962005-12-12 19:38:44 +0000949 /* Here would be a good place to free up memory allocated during the dependencies build. */
Eric Andersen3b1a7442003-12-24 20:30:45 +0000950
Rob Landleye9190962005-12-12 19:38:44 +0000951 return rc;
Eric Andersen0139ca92001-07-22 23:01:03 +0000952}