]> git.ipfire.org Git - thirdparty/kmod.git/blame - tools/modprobe.c
libkmod: add weak dependecies
[thirdparty/kmod.git] / tools / modprobe.c
CommitLineData
c3d0a5f2 1/*
8b013767 2 * kmod-modprobe - manage linux kernel modules using libkmod.
c3d0a5f2 3 *
e6b0e49b 4 * Copyright (C) 2011-2013 ProFUSION embedded systems
c3d0a5f2 5 *
cb451f35
LDM
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
c3d0a5f2
GSB
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cb451f35
LDM
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
c3d0a5f2 15 *
cb451f35
LDM
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
c3d0a5f2 18 */
cb451f35 19
4744ebce 20#include <assert.h>
c2e4286b
LDM
21#include <errno.h>
22#include <getopt.h>
23#include <limits.h>
24#include <stdbool.h>
c3d0a5f2
GSB
25#include <stdio.h>
26#include <stdlib.h>
c3d0a5f2 27#include <string.h>
c2e4286b 28#include <unistd.h>
c3d0a5f2 29#include <sys/stat.h>
c2e4286b 30#include <sys/types.h>
c3d0a5f2 31#include <sys/utsname.h>
eff917c0 32#include <sys/wait.h>
74d1df66
LDM
33
34#include <shared/array.h>
2b98ed88 35#include <shared/util.h>
576dd439 36#include <shared/macro.h>
c3d0a5f2 37
f357866d 38#include <libkmod/libkmod.h>
c2e4286b 39
4a2e20df
LDM
40#include "kmod.h"
41
c3d0a5f2
GSB
42static int log_priority = LOG_CRIT;
43static int use_syslog = 0;
fcb0ce94 44#define LOG(...) log_printf(log_priority, __VA_ARGS__)
c3d0a5f2
GSB
45
46#define DEFAULT_VERBOSE LOG_WARNING
47static int verbose = DEFAULT_VERBOSE;
525fa07b 48static int do_show = 0;
c3d0a5f2
GSB
49static int dry_run = 0;
50static int ignore_loaded = 0;
8122985d 51static int lookup_only = 0;
c3d0a5f2
GSB
52static int first_time = 0;
53static int ignore_commands = 0;
54static int use_blacklist = 0;
55static int force = 0;
56static int strip_modversion = 0;
57static int strip_vermagic = 0;
42b32d30 58static int remove_holders = 0;
2b98ed88 59static unsigned long long wait_msec = 0;
d8a6c0cc 60static int quiet_inuse = 0;
c3d0a5f2 61
2b98ed88 62static const char cmdopts_s[] = "arw:RibfDcnC:d:S:sqvVh";
c3d0a5f2
GSB
63static const struct option cmdopts[] = {
64 {"all", no_argument, 0, 'a'},
2b98ed88 65
c3d0a5f2
GSB
66 {"remove", no_argument, 0, 'r'},
67 {"remove-dependencies", no_argument, 0, 5},
42b32d30 68 {"remove-holders", no_argument, 0, 5},
2b98ed88
LDM
69 {"wait", required_argument, 0, 'w'},
70
c3d0a5f2
GSB
71 {"resolve-alias", no_argument, 0, 'R'},
72 {"first-time", no_argument, 0, 3},
73 {"ignore-install", no_argument, 0, 'i'},
74 {"ignore-remove", no_argument, 0, 'i'},
75 {"use-blacklist", no_argument, 0, 'b'},
76 {"force", no_argument, 0, 'f'},
77 {"force-modversion", no_argument, 0, 2},
78 {"force-vermagic", no_argument, 0, 1},
79
c3d0a5f2
GSB
80 {"show-depends", no_argument, 0, 'D'},
81 {"showconfig", no_argument, 0, 'c'},
82 {"show-config", no_argument, 0, 'c'},
83 {"show-modversions", no_argument, 0, 4},
84 {"dump-modversions", no_argument, 0, 4},
3ada8df8 85 {"show-exports", no_argument, 0, 6},
c3d0a5f2
GSB
86
87 {"dry-run", no_argument, 0, 'n'},
88 {"show", no_argument, 0, 'n'},
89
90 {"config", required_argument, 0, 'C'},
91 {"dirname", required_argument, 0, 'd'},
92 {"set-version", required_argument, 0, 'S'},
93
94 {"syslog", no_argument, 0, 's'},
95 {"quiet", no_argument, 0, 'q'},
96 {"verbose", no_argument, 0, 'v'},
97 {"version", no_argument, 0, 'V'},
98 {"help", no_argument, 0, 'h'},
99 {NULL, 0, 0, 0}
100};
101
4a2e20df 102static void help(void)
c3d0a5f2 103{
34e06bfb 104 printf("Usage:\n"
c3d0a5f2
GSB
105 "\t%s [options] [-i] [-b] modulename\n"
106 "\t%s [options] -a [-i] [-b] modulename [modulename...]\n"
107 "\t%s [options] -r [-i] modulename\n"
108 "\t%s [options] -r -a [-i] modulename [modulename...]\n"
c3d0a5f2
GSB
109 "\t%s [options] -c\n"
110 "\t%s [options] --dump-modversions filename\n"
111 "Management Options:\n"
ab70dce1
GSB
112 "\t-a, --all Consider every non-argument to\n"
113 "\t be a module name to be inserted\n"
114 "\t or removed (-r)\n"
115 "\t-r, --remove Remove modules instead of inserting\n"
42b32d30
LDM
116 "\t --remove-dependencies Deprecated: use --remove-holders\n"
117 "\t --remove-holders Also remove module holders (use together with -r)\n"
2b98ed88
LDM
118 "\t-w, --wait <MSEC> When removing a module, wait up to MSEC for\n"
119 "\t module's refcount to become 0 so it can be\n"
120 "\t removed (use together with -r)\n"
ab70dce1
GSB
121 "\t --first-time Fail if module already inserted or removed\n"
122 "\t-i, --ignore-install Ignore install commands\n"
123 "\t-i, --ignore-remove Ignore remove commands\n"
124 "\t-b, --use-blacklist Apply blacklist to resolved alias.\n"
125 "\t-f, --force Force module insertion or removal.\n"
126 "\t implies --force-modversions and\n"
127 "\t --force-vermagic\n"
128 "\t --force-modversion Ignore module's version\n"
129 "\t --force-vermagic Ignore module's version magic\n"
c3d0a5f2
GSB
130 "\n"
131 "Query Options:\n"
081fff2c 132 "\t-R, --resolve-alias Only lookup and print alias and exit\n"
ab70dce1
GSB
133 "\t-D, --show-depends Only print module dependencies and exit\n"
134 "\t-c, --showconfig Print out known configuration and exit\n"
135 "\t-c, --show-config Same as --showconfig\n"
136 "\t --show-modversions Dump module symbol version and exit\n"
137 "\t --dump-modversions Same as --show-modversions\n"
3ada8df8 138 "\t --show-exports Only print module exported symbol versions and exit\n"
c3d0a5f2
GSB
139 "\n"
140 "General Options:\n"
ab70dce1
GSB
141 "\t-n, --dry-run Do not execute operations, just print out\n"
142 "\t-n, --show Same as --dry-run\n"
c3d0a5f2 143
2c96693e 144 "\t-C, --config=FILE Use FILE instead of default search paths\n"
9d1fb317 145 "\t-d, --dirname=DIR Use DIR as filesystem root for " MODULE_DIRECTORY "\n"
ab70dce1 146 "\t-S, --set-version=VERSION Use VERSION instead of `uname -r`\n"
c3d0a5f2
GSB
147
148 "\t-s, --syslog print to syslog, not stderr\n"
149 "\t-q, --quiet disable messages\n"
150 "\t-v, --verbose enables more messages\n"
151 "\t-V, --version show version\n"
152 "\t-h, --help show this help\n",
7c04aeee
LDM
153 program_invocation_short_name, program_invocation_short_name,
154 program_invocation_short_name, program_invocation_short_name,
155 program_invocation_short_name, program_invocation_short_name);
c3d0a5f2
GSB
156}
157
1958af88 158_printf_format_(1, 2)
c3d0a5f2
GSB
159static inline void _show(const char *fmt, ...)
160{
161 va_list args;
162
525fa07b 163 if (!do_show && verbose <= DEFAULT_VERBOSE)
c3d0a5f2
GSB
164 return;
165
166 va_start(args, fmt);
167 vfprintf(stdout, fmt, args);
168 fflush(stdout);
169 va_end(args);
170}
c3d0a5f2
GSB
171#define SHOW(...) _show(__VA_ARGS__)
172
c3d0a5f2
GSB
173static int show_config(struct kmod_ctx *ctx)
174{
bc43496a
LDM
175 struct config_iterators {
176 const char *name;
177 struct kmod_config_iter *(*get_iter)(const struct kmod_ctx *ctx);
178 } ci[] = {
179 { "blacklist", kmod_config_get_blacklists },
180 { "install", kmod_config_get_install_commands },
181 { "remove", kmod_config_get_remove_commands },
182 { "alias", kmod_config_get_aliases },
02629fa0 183 { "options", kmod_config_get_options },
bc43496a 184 { "softdep", kmod_config_get_softdeps },
05828b4a 185 { "weakdep", kmod_config_get_weakdeps },
bc43496a
LDM
186 };
187 size_t i;
188
189 for (i = 0; i < ARRAY_SIZE(ci); i++) {
190 struct kmod_config_iter *iter = ci[i].get_iter(ctx);
191
192 if (iter == NULL)
193 continue;
194
195 while (kmod_config_iter_next(iter)) {
196 const char *val;
197
198 printf("%s %s", ci[i].name,
199 kmod_config_iter_get_key(iter));
200 val = kmod_config_iter_get_value(iter);
201 if (val != NULL) {
202 putchar(' ');
203 puts(val);
204 } else
205 putchar('\n');
206 }
207
208 kmod_config_iter_free_iter(iter);
209 }
210
28f32c65 211 puts("\n# End of configuration files. Dumping indexes now:\n");
09e9ae58
LDM
212 fflush(stdout);
213
49a16375
LDM
214 kmod_dump_index(ctx, KMOD_INDEX_MODULES_ALIAS, STDOUT_FILENO);
215 kmod_dump_index(ctx, KMOD_INDEX_MODULES_SYMBOL, STDOUT_FILENO);
216
bc43496a 217 return 0;
c3d0a5f2
GSB
218}
219
220static int show_modversions(struct kmod_ctx *ctx, const char *filename)
221{
0e3e2f43
GSB
222 struct kmod_list *l, *list = NULL;
223 struct kmod_module *mod;
224 int err = kmod_module_new_from_path(ctx, filename, &mod);
225 if (err < 0) {
226 LOG("Module %s not found.\n", filename);
227 return err;
228 }
229
230 err = kmod_module_get_versions(mod, &list);
231 if (err < 0) {
63698377 232 LOG("could not get modversions of %s: %s\n",
0e3e2f43
GSB
233 filename, strerror(-err));
234 kmod_module_unref(mod);
235 return err;
236 }
237
238 kmod_list_foreach(l, list) {
239 const char *symbol = kmod_module_version_get_symbol(l);
240 uint64_t crc = kmod_module_version_get_crc(l);
241 printf("0x%08"PRIx64"\t%s\n", crc, symbol);
242 }
243 kmod_module_versions_free_list(list);
244 kmod_module_unref(mod);
245 return 0;
c3d0a5f2
GSB
246}
247
3ada8df8
YK
248static int show_exports(struct kmod_ctx *ctx, const char *filename)
249{
250 struct kmod_list *l, *list = NULL;
251 struct kmod_module *mod;
252 int err = kmod_module_new_from_path(ctx, filename, &mod);
253 if (err < 0) {
254 LOG("Module %s not found.\n", filename);
255 return err;
256 }
257
258 err = kmod_module_get_symbols(mod, &list);
259 if (err < 0) {
260 LOG("could not get symbols of %s: %s\n",
261 filename, strerror(-err));
262 kmod_module_unref(mod);
263 return err;
264 }
265
266 kmod_list_foreach(l, list) {
267 const char *symbol = kmod_module_symbol_get_symbol(l);
268 uint64_t crc = kmod_module_symbol_get_crc(l);
269 printf("0x%08"PRIx64"\t%s\n", crc, symbol);
270 }
271 kmod_module_symbols_free_list(list);
272 kmod_module_unref(mod);
273 return 0;
274}
275
8f192210
LDM
276static int command_do(struct kmod_module *module, const char *type,
277 const char *command, const char *cmdline_opts)
c3d0a5f2
GSB
278{
279 const char *modname = kmod_module_get_name(module);
280 char *p, *cmd = NULL;
281 size_t cmdlen, cmdline_opts_len, varlen;
282 int ret = 0;
283
284 if (cmdline_opts == NULL)
285 cmdline_opts = "";
286 cmdline_opts_len = strlen(cmdline_opts);
287
288 cmd = strdup(command);
289 if (cmd == NULL)
290 return -ENOMEM;
291 cmdlen = strlen(cmd);
292 varlen = sizeof("$CMDLINE_OPTS") - 1;
293 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
294 size_t prefixlen = p - cmd;
295 size_t suffixlen = cmdlen - prefixlen - varlen;
296 size_t slen = cmdlen - varlen + cmdline_opts_len;
297 char *suffix = p + varlen;
298 char *s = malloc(slen + 1);
299 if (s == NULL) {
300 free(cmd);
301 return -ENOMEM;
302 }
303 memcpy(s, cmd, p - cmd);
304 memcpy(s + prefixlen, cmdline_opts, cmdline_opts_len);
305 memcpy(s + prefixlen + cmdline_opts_len, suffix, suffixlen);
306 s[slen] = '\0';
307
308 free(cmd);
309 cmd = s;
310 cmdlen = slen;
311 }
312
313 SHOW("%s %s\n", type, cmd);
314 if (dry_run)
315 goto end;
316
317 setenv("MODPROBE_MODULE", modname, 1);
318 ret = system(cmd);
319 unsetenv("MODPROBE_MODULE");
320 if (ret == -1 || WEXITSTATUS(ret)) {
321 LOG("Error running %s command for %s\n", type, modname);
322 if (ret != -1)
323 ret = -WEXITSTATUS(ret);
324 }
325
326end:
327 free(cmd);
328 return ret;
329}
330
a872bba2 331static int rmmod_do_remove_module(struct kmod_module *mod)
c3d0a5f2 332{
a872bba2 333 const char *modname = kmod_module_get_name(mod);
2b98ed88
LDM
334 unsigned long long interval_msec = 0, t0_msec = 0,
335 tend_msec = 0;
a872bba2 336 int flags = 0, err;
e793f1ea 337
6f7ab216 338 SHOW("rmmod %s\n", modname);
c3d0a5f2 339
a872bba2
LDM
340 if (dry_run)
341 return 0;
c3d0a5f2 342
a872bba2
LDM
343 if (force)
344 flags |= KMOD_REMOVE_FORCE;
c3d0a5f2 345
2b98ed88
LDM
346 if (wait_msec)
347 flags |= KMOD_REMOVE_NOLOG;
348
349 do {
350 err = kmod_module_remove_module(mod, flags);
351 if (err == -EEXIST) {
352 if (!first_time)
353 err = 0;
354 else
355 LOG("Module %s is not in kernel.\n", modname);
356 break;
357 } else if (err == -EAGAIN && wait_msec) {
358 unsigned long long until_msec;
359
360 if (!t0_msec) {
361 t0_msec = now_msec();
362 tend_msec = t0_msec + wait_msec;
363 interval_msec = 1;
364 }
365
366 until_msec = get_backoff_delta_msec(t0_msec, tend_msec,
367 &interval_msec);
368 err = sleep_until_msec(until_msec);
369
370 if (!t0_msec)
371 err = -ENOTSUP;
372
373 if (err < 0) {
374 ERR("Failed to sleep: %s\n", strerror(-err));
375 err = -EAGAIN;
376 break;
377 }
378 } else {
379 break;
380 }
381 } while (interval_msec);
382
383 if (err < 0 && wait_msec)
384 ERR("could not remove '%s': %s\n", modname, strerror(-err));
e793f1ea
GSB
385
386 return err;
387}
388
42b32d30 389#define RMMOD_FLAG_REMOVE_HOLDERS 0x1
a6b540de
LDM
390#define RMMOD_FLAG_IGNORE_BUILTIN 0x2
391static int rmmod_do_module(struct kmod_module *mod, int flags);
e793f1ea 392
7089386e
LDM
393/* Remove modules in reverse order */
394static int rmmod_do_modlist(struct kmod_list *list, bool stop_on_errors)
e793f1ea 395{
a872bba2
LDM
396 struct kmod_list *l;
397
398 kmod_list_foreach_reverse(l, list) {
399 struct kmod_module *m = kmod_module_get_module(l);
a6b540de 400 int r = rmmod_do_module(m, RMMOD_FLAG_IGNORE_BUILTIN);
a872bba2
LDM
401 kmod_module_unref(m);
402
403 if (r < 0 && stop_on_errors)
404 return r;
405 }
406
407 return 0;
c3d0a5f2
GSB
408}
409
a6b540de 410static int rmmod_do_module(struct kmod_module *mod, int flags)
c3d0a5f2
GSB
411{
412 const char *modname = kmod_module_get_name(mod);
a872bba2
LDM
413 struct kmod_list *pre = NULL, *post = NULL;
414 const char *cmd = NULL;
c3d0a5f2
GSB
415 int err;
416
c3d0a5f2 417 if (!ignore_commands) {
e793f1ea
GSB
418 err = kmod_module_get_softdeps(mod, &pre, &post);
419 if (err < 0) {
420 WRN("could not get softdeps of '%s': %s\n",
8f192210 421 modname, strerror(-err));
c3d0a5f2 422 return err;
e793f1ea
GSB
423 }
424
c3d0a5f2 425 cmd = kmod_module_get_remove_commands(mod);
c3d0a5f2
GSB
426 }
427
ea3c8adf
LDM
428 /* Quick check if module is loaded, otherwise there's nothing to do */
429 if (!cmd && !ignore_loaded) {
9bf60d21
LDM
430 int state = kmod_module_get_initstate(mod);
431
e5e2a683 432 if (state < 0) {
9bf60d21
LDM
433 if (first_time) {
434 LOG("Module %s is not in kernel.\n", modname);
e4e1e64a 435 err = -ENOENT;
e4e1e64a
LDM
436 } else {
437 err = 0;
e4e1e64a 438 }
f758caf5
DR
439 goto error;
440 } else if (state == KMOD_MODULE_BUILTIN) {
a6b540de 441 if (flags & RMMOD_FLAG_IGNORE_BUILTIN) {
52a0ba82
YK
442 err = 0;
443 } else {
444 LOG("Module %s is builtin.\n", modname);
445 err = -ENOENT;
446 }
f758caf5 447 goto error;
9bf60d21
LDM
448 }
449 }
450
ea3c8adf 451 /* 1. @mod's post-softdeps in reverse order */
7089386e 452 rmmod_do_modlist(post, false);
a872bba2 453
ea3c8adf 454 /* 2. Other modules holding @mod */
d29ed6ef 455 if (flags & RMMOD_FLAG_REMOVE_HOLDERS) {
42b32d30 456 struct kmod_list *holders = kmod_module_get_holders(mod);
a872bba2 457
42b32d30 458 err = rmmod_do_modlist(holders, true);
116e7c7f 459 kmod_module_unref_list(holders);
9bf60d21 460 if (err < 0)
a872bba2 461 goto error;
9bf60d21
LDM
462 }
463
ea3c8adf 464 /* 3. @mod itself, but check for refcnt first */
2b98ed88 465 if (!cmd && !ignore_loaded && !wait_msec) {
c3d0a5f2 466 int usage = kmod_module_get_refcnt(mod);
d8a6c0cc 467
c3d0a5f2 468 if (usage > 0) {
d8a6c0cc
LDM
469 if (!quiet_inuse)
470 LOG("Module %s is in use.\n", modname);
471
e793f1ea 472 err = -EBUSY;
e793f1ea
GSB
473 goto error;
474 }
475 }
476
ea3c8adf 477 if (!cmd)
a872bba2
LDM
478 err = rmmod_do_remove_module(mod);
479 else
480 err = command_do(mod, "remove", cmd, NULL);
d8a6c0cc 481
a872bba2
LDM
482 if (err < 0)
483 goto error;
c9a14448 484
ea3c8adf
LDM
485 /* 4. Other modules that became unused: errors are non-fatal */
486 if (!cmd) {
487 struct kmod_list *deps, *itr;
488
489 deps = kmod_module_get_dependencies(mod);
490 kmod_list_foreach(itr, deps) {
491 struct kmod_module *dep = kmod_module_get_module(itr);
492 if (kmod_module_get_refcnt(dep) == 0)
493 rmmod_do_remove_module(dep);
494 kmod_module_unref(dep);
495 }
496 kmod_module_unref_list(deps);
497 }
498
499 /* 5. @mod's pre-softdeps in reverse order: errors are non-fatal */
7089386e 500 rmmod_do_modlist(pre, false);
0e9bd2d1 501
e793f1ea
GSB
502error:
503 kmod_module_unref_list(pre);
504 kmod_module_unref_list(post);
a872bba2 505
c3d0a5f2
GSB
506 return err;
507}
508
569f1609 509static int rmmod(struct kmod_ctx *ctx, const char *alias)
c3d0a5f2
GSB
510{
511 struct kmod_list *l, *list = NULL;
512 int err;
513
514 err = kmod_module_new_from_lookup(ctx, alias, &list);
e5e2a683 515 if (err < 0)
c3d0a5f2 516 return err;
e5e2a683 517
f758caf5 518 if (list == NULL) {
e5e2a683 519 LOG("Module %s not found.\n", alias);
f758caf5
DR
520 err = -ENOENT;
521 }
c3d0a5f2
GSB
522
523 kmod_list_foreach(l, list) {
524 struct kmod_module *mod = kmod_module_get_module(l);
d29ed6ef
LDM
525 int flags = remove_holders ? RMMOD_FLAG_REMOVE_HOLDERS : 0;
526
527 err = rmmod_do_module(mod, flags);
c3d0a5f2
GSB
528 kmod_module_unref(mod);
529 if (err < 0)
530 break;
531 }
532
533 kmod_module_unref_list(list);
534 return err;
535}
536
c3d0a5f2
GSB
537static int rmmod_all(struct kmod_ctx *ctx, char **args, int nargs)
538{
539 int i, err = 0;
540
541 for (i = 0; i < nargs; i++) {
542 int r = rmmod(ctx, args[i]);
543 if (r < 0)
544 err = r;
545 }
546
547 return err;
548}
549
2e9dcd74
LDM
550static void print_action(struct kmod_module *m, bool install,
551 const char *options)
552{
4744ebce
LDM
553 const char *path;
554
555 if (install) {
2e9dcd74
LDM
556 printf("install %s %s\n", kmod_module_get_install_commands(m),
557 options);
4744ebce
LDM
558 return;
559 }
560
561 path = kmod_module_get_path(m);
562
563 if (path == NULL) {
ace742fa
LDM
564 /*
565 * Either a builtin module, or an alias, print only for
566 * builtin
567 */
568 if (kmod_module_get_initstate(m) == KMOD_MODULE_BUILTIN)
569 printf("builtin %s\n", kmod_module_get_name(m));
4744ebce
LDM
570 } else
571 printf("insmod %s %s\n", kmod_module_get_path(m), options);
2e9dcd74
LDM
572}
573
f3db15e9
GS
574static int insmod_insert(struct kmod_module *mod, int flags,
575 const char *extra_options)
576{
577 int err = 0;
578 void (*show)(struct kmod_module *m, bool install,
579 const char *options) = NULL;
580
581 if (do_show || verbose > DEFAULT_VERBOSE)
582 show = &print_action;
583
584 if (lookup_only)
585 printf("%s\n", kmod_module_get_name(mod));
586 else
587 err = kmod_module_probe_insert_module(mod, flags,
588 extra_options, NULL, NULL, show);
589
590 if (err >= 0)
591 /* ignore flag return values such as a mod being blacklisted */
592 err = 0;
593 else {
594 switch (err) {
595 case -EEXIST:
596 ERR("could not insert '%s': Module already in kernel\n",
597 kmod_module_get_name(mod));
598 break;
599 case -ENOENT:
600 ERR("could not insert '%s': Unknown symbol in module, "
601 "or unknown parameter (see dmesg)\n",
602 kmod_module_get_name(mod));
603 break;
604 default:
605 ERR("could not insert '%s': %s\n",
606 kmod_module_get_name(mod),
607 strerror(-err));
608 break;
609 }
610 }
611
612 return err;
613}
614
569f1609 615static int insmod(struct kmod_ctx *ctx, const char *alias,
8f192210 616 const char *extra_options)
c3d0a5f2
GSB
617{
618 struct kmod_list *l, *list = NULL;
883d931d 619 struct kmod_module *mod = NULL;
2e9dcd74
LDM
620 int err, flags = 0;
621
883d931d
GS
622 if (strncmp(alias, "/", 1) == 0 || strncmp(alias, "./", 2) == 0) {
623 err = kmod_module_new_from_path(ctx, alias, &mod);
624 if (err < 0) {
625 LOG("Failed to get module from path %s: %s\n", alias,
626 strerror(-err));
627 return -ENOENT;
628 }
629 } else {
630 err = kmod_module_new_from_lookup(ctx, alias, &list);
631 if (list == NULL || err < 0) {
632 LOG("Module %s not found in directory %s\n", alias,
633 ctx ? kmod_get_dirname(ctx) : "(missing)");
634 return -ENOENT;
635 }
e2719b32 636 }
c3d0a5f2 637
2e9dcd74
LDM
638 if (strip_modversion || force)
639 flags |= KMOD_PROBE_FORCE_MODVERSION;
640 if (strip_vermagic || force)
641 flags |= KMOD_PROBE_FORCE_VERMAGIC;
642 if (ignore_commands)
643 flags |= KMOD_PROBE_IGNORE_COMMAND;
644 if (ignore_loaded)
645 flags |= KMOD_PROBE_IGNORE_LOADED;
646 if (dry_run)
647 flags |= KMOD_PROBE_DRY_RUN;
2e9dcd74 648
36ddee65 649 flags |= KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY;
2e9dcd74
LDM
650
651 if (use_blacklist)
652 flags |= KMOD_PROBE_APPLY_BLACKLIST;
653 if (first_time)
814a57ba 654 flags |= KMOD_PROBE_FAIL_ON_LOADED;
c3d0a5f2 655
883d931d
GS
656 /* If module is loaded from path */
657 if (mod != NULL) {
f3db15e9 658 err = insmod_insert(mod, flags, extra_options);
c3d0a5f2 659 kmod_module_unref(mod);
883d931d
GS
660 } else {
661 kmod_list_foreach(l, list) {
662 mod = kmod_module_get_module(l);
663 err = insmod_insert(mod, flags, extra_options);
664 kmod_module_unref(mod);
665 }
666 kmod_module_unref_list(list);
c3d0a5f2 667 }
c3d0a5f2
GSB
668 return err;
669}
670
c3d0a5f2
GSB
671static int insmod_all(struct kmod_ctx *ctx, char **args, int nargs)
672{
673 int i, err = 0;
674
675 for (i = 0; i < nargs; i++) {
676 int r = insmod(ctx, args[i], NULL);
677 if (r < 0)
678 err = r;
679 }
680
681 return err;
682}
683
684static void env_modprobe_options_append(const char *value)
685{
686 const char *old = getenv("MODPROBE_OPTIONS");
687 char *env;
688
689 if (old == NULL) {
690 setenv("MODPROBE_OPTIONS", value, 1);
691 return;
692 }
693
694 if (asprintf(&env, "%s %s", old, value) < 0) {
695 ERR("could not append value to $MODPROBE_OPTIONS\n");
696 return;
697 }
698
699 if (setenv("MODPROBE_OPTIONS", env, 1) < 0)
700 ERR("could not setenv(MODPROBE_OPTIONS, \"%s\")\n", env);
701 free(env);
702}
703
704static int options_from_array(char **args, int nargs, char **output)
705{
706 char *opts = NULL;
707 size_t optslen = 0;
708 int i, err = 0;
709
710 for (i = 1; i < nargs; i++) {
711 size_t len = strlen(args[i]);
712 size_t qlen = 0;
713 const char *value;
714 void *tmp;
715
716 value = strchr(args[i], '=');
717 if (value) {
718 value++;
719 if (*value != '"' && *value != '\'') {
720 if (strchr(value, ' '))
721 qlen = 2;
722 }
723 }
724
725 tmp = realloc(opts, optslen + len + qlen + 2);
726 if (!tmp) {
727 err = -errno;
728 free(opts);
729 opts = NULL;
730 ERR("could not gather module options: out-of-memory\n");
731 break;
732 }
733 opts = tmp;
734 if (optslen > 0) {
735 opts[optslen] = ' ';
736 optslen++;
737 }
738 if (qlen == 0) {
739 memcpy(opts + optslen, args[i], len + 1);
740 optslen += len;
741 } else {
742 size_t keylen = value - args[i];
743 size_t valuelen = len - keylen;
744 memcpy(opts + optslen, args[i], keylen);
745 optslen += keylen;
746 opts[optslen] = '"';
747 optslen++;
748 memcpy(opts + optslen, value, valuelen);
749 optslen += valuelen;
750 opts[optslen] = '"';
751 optslen++;
752 opts[optslen] = '\0';
753 }
754 }
755
756 *output = opts;
757 return err;
758}
759
760static char **prepend_options_from_env(int *p_argc, char **orig_argv)
761{
762 const char *p, *env = getenv("MODPROBE_OPTIONS");
d890179b 763 char **new_argv, *str_end, *str, *s, *quote;
c3d0a5f2
GSB
764 int i, argc = *p_argc;
765 size_t envlen, space_count = 0;
766
767 if (env == NULL)
768 return orig_argv;
769
770 for (p = env; *p != '\0'; p++) {
771 if (*p == ' ')
772 space_count++;
773 }
774
775 envlen = p - env;
776 new_argv = malloc(sizeof(char *) * (argc + space_count + 3 + envlen));
777 if (new_argv == NULL)
778 return NULL;
779
780 new_argv[0] = orig_argv[0];
d890179b 781 str = (char *) (new_argv + argc + space_count + 3);
c3d0a5f2
GSB
782 memcpy(str, env, envlen + 1);
783
d890179b 784 str_end = str + envlen;
c3d0a5f2
GSB
785
786 quote = NULL;
787 for (i = 1, s = str; *s != '\0'; s++) {
788 if (quote == NULL) {
789 if (*s == ' ') {
790 new_argv[i] = str;
791 i++;
792 *s = '\0';
793 str = s + 1;
794 } else if (*s == '"' || *s == '\'')
795 quote = s;
796 } else {
797 if (*s == *quote) {
798 if (quote == str) {
799 new_argv[i] = str + 1;
800 i++;
801 *s = '\0';
802 str = s + 1;
803 } else {
804 char *it;
805 for (it = quote; it < s - 1; it++)
806 it[0] = it[1];
807 for (it = s - 1; it < str_end - 2; it++)
808 it[0] = it[2];
809 str_end -= 2;
810 *str_end = '\0';
811 s -= 2;
812 }
813 quote = NULL;
814 }
815 }
816 }
817 if (str < s) {
818 new_argv[i] = str;
819 i++;
820 }
821
822 memcpy(new_argv + i, orig_argv + 1, sizeof(char *) * (argc - 1));
757b3599 823 new_argv[i + argc - 1] = NULL;
c3d0a5f2
GSB
824 *p_argc = i + argc - 1;
825
826 return new_argv;
827}
828
fa29c0ee 829static int do_modprobe(int argc, char **orig_argv)
c3d0a5f2
GSB
830{
831 struct kmod_ctx *ctx;
832 char **args = NULL, **argv;
cb8d4d3e
GSB
833 const char **config_paths = NULL;
834 int nargs = 0, n_config_paths = 0;
c3d0a5f2
GSB
835 char dirname_buf[PATH_MAX];
836 const char *dirname = NULL;
837 const char *root = NULL;
c3d0a5f2 838 const char *kversion = NULL;
c3d0a5f2
GSB
839 int use_all = 0;
840 int do_remove = 0;
841 int do_show_config = 0;
842 int do_show_modversions = 0;
3ada8df8 843 int do_show_exports = 0;
c3d0a5f2 844 int err;
c1fb98a3 845 struct stat stat_buf;
c3d0a5f2
GSB
846
847 argv = prepend_options_from_env(&argc, orig_argv);
848 if (argv == NULL) {
d9a2e155 849 ERR("Could not prepend options from command line\n");
c3d0a5f2
GSB
850 return EXIT_FAILURE;
851 }
852
853 for (;;) {
854 int c, idx = 0;
855 c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
856 if (c == -1)
857 break;
858 switch (c) {
859 case 'a':
860 log_priority = LOG_WARNING;
861 use_all = 1;
862 break;
863 case 'r':
864 do_remove = 1;
865 break;
866 case 5:
42b32d30 867 remove_holders = 1;
c3d0a5f2 868 break;
2b98ed88
LDM
869 case 'w': {
870 char *endptr = NULL;
871 wait_msec = strtoul(optarg, &endptr, 0);
872 if (!*optarg || *endptr) {
873 ERR("unexpected wait value '%s'.\n", optarg);
874 err = -1;
875 goto done;
876 }
877 break;
878 }
c3d0a5f2
GSB
879 case 3:
880 first_time = 1;
881 break;
882 case 'i':
883 ignore_commands = 1;
884 break;
885 case 'b':
886 use_blacklist = 1;
887 break;
888 case 'f':
889 force = 1;
890 break;
891 case 2:
892 strip_modversion = 1;
893 break;
894 case 1:
895 strip_vermagic = 1;
896 break;
c3d0a5f2
GSB
897 case 'D':
898 ignore_loaded = 1;
899 dry_run = 1;
525fa07b 900 do_show = 1;
c3d0a5f2 901 break;
081fff2c
LDM
902 case 'R':
903 lookup_only = 1;
904 break;
c3d0a5f2
GSB
905 case 'c':
906 do_show_config = 1;
907 break;
908 case 4:
909 do_show_modversions = 1;
910 break;
3ada8df8
YK
911 case 6:
912 do_show_exports = 1;
913 break;
c3d0a5f2
GSB
914 case 'n':
915 dry_run = 1;
916 break;
cb8d4d3e
GSB
917 case 'C': {
918 size_t bytes = sizeof(char *) * (n_config_paths + 2);
919 void *tmp = realloc(config_paths, bytes);
920 if (!tmp) {
d9a2e155
LDM
921 ERR("out-of-memory\n");
922 err = -1;
923 goto done;
cb8d4d3e
GSB
924 }
925 config_paths = tmp;
926 config_paths[n_config_paths] = optarg;
927 n_config_paths++;
928 config_paths[n_config_paths] = NULL;
929
c3d0a5f2
GSB
930 env_modprobe_options_append("-C");
931 env_modprobe_options_append(optarg);
c3d0a5f2 932 break;
cb8d4d3e 933 }
c3d0a5f2
GSB
934 case 'd':
935 root = optarg;
936 break;
937 case 'S':
938 kversion = optarg;
939 break;
940 case 's':
941 env_modprobe_options_append("-s");
942 use_syslog = 1;
943 break;
944 case 'q':
945 env_modprobe_options_append("-q");
ae7ebe87 946 verbose = LOG_EMERG;
c3d0a5f2
GSB
947 break;
948 case 'v':
949 env_modprobe_options_append("-v");
950 verbose++;
951 break;
952 case 'V':
953 puts(PACKAGE " version " VERSION);
655de275 954 puts(KMOD_FEATURES);
d9a2e155 955 err = 0;
4434d8ba 956 goto done;
c3d0a5f2 957 case 'h':
4a2e20df 958 help();
d9a2e155 959 err = 0;
4434d8ba 960 goto done;
c3d0a5f2 961 case '?':
d9a2e155
LDM
962 err = -1;
963 goto done;
c3d0a5f2 964 default:
d9a2e155
LDM
965 ERR("unexpected getopt_long() value '%c'.\n", c);
966 err = -1;
967 goto done;
c3d0a5f2
GSB
968 }
969 }
970
971 args = argv + optind;
972 nargs = argc - optind;
973
c1fb98a3
QA
974 if (!use_syslog &&
975 (!stderr ||
976 fileno(stderr) == -1 ||
977 fstat(fileno(stderr), &stat_buf)))
978 use_syslog = 1;
979
92aad749 980 log_open(use_syslog);
d9a2e155 981
b09668cf 982 if (!do_show_config) {
c3d0a5f2 983 if (nargs == 0) {
d9a2e155
LDM
984 ERR("missing parameters. See -h.\n");
985 err = -1;
986 goto done;
c3d0a5f2
GSB
987 }
988 }
989
c3d0a5f2
GSB
990 if (root != NULL || kversion != NULL) {
991 struct utsname u;
992 if (root == NULL)
993 root = "";
994 if (kversion == NULL) {
995 if (uname(&u) < 0) {
d9a2e155
LDM
996 ERR("uname() failed: %m\n");
997 err = -1;
998 goto done;
c3d0a5f2
GSB
999 }
1000 kversion = u.release;
1001 }
8f192210 1002 snprintf(dirname_buf, sizeof(dirname_buf),
9d1fb317 1003 "%s" MODULE_DIRECTORY "/%s", root,
8f192210 1004 kversion);
c3d0a5f2
GSB
1005 dirname = dirname_buf;
1006 }
1007
cb8d4d3e 1008 ctx = kmod_new(dirname, config_paths);
c3d0a5f2 1009 if (!ctx) {
d9a2e155
LDM
1010 ERR("kmod_new() failed!\n");
1011 err = -1;
1012 goto done;
c3d0a5f2 1013 }
c3d0a5f2 1014
52a50fe2 1015 log_setup_kmod_log(ctx, verbose);
d9a2e155
LDM
1016
1017 kmod_load_resources(ctx);
c3d0a5f2 1018
b09668cf 1019 if (do_show_config)
c3d0a5f2
GSB
1020 err = show_config(ctx);
1021 else if (do_show_modversions)
1022 err = show_modversions(ctx, args[0]);
3ada8df8
YK
1023 else if (do_show_exports)
1024 err = show_exports(ctx, args[0]);
c3d0a5f2 1025 else if (do_remove)
c1b84540 1026 err = rmmod_all(ctx, args, nargs);
c3d0a5f2
GSB
1027 else if (use_all)
1028 err = insmod_all(ctx, args, nargs);
1029 else {
1030 char *opts;
1031 err = options_from_array(args, nargs, &opts);
1032 if (err == 0) {
1033 err = insmod(ctx, args[0], opts);
1034 free(opts);
1035 }
1036 }
1037
1038 kmod_unref(ctx);
1039
d9a2e155 1040done:
92aad749 1041 log_close();
c3d0a5f2
GSB
1042
1043 if (argv != orig_argv)
1044 free(argv);
8f192210 1045
cb8d4d3e 1046 free(config_paths);
c3d0a5f2 1047
d9a2e155 1048 return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
c3d0a5f2 1049}
fa29c0ee 1050
fa29c0ee
LDM
1051const struct kmod_cmd kmod_cmd_compat_modprobe = {
1052 .name = "modprobe",
1053 .cmd = do_modprobe,
1054 .help = "compat modprobe command",
1055};