1 /*
2 * kmod-modprobe - manage linux kernel modules using libkmod.
3 *
4 * Copyright (C) 2011-2013 ProFUSION embedded systems
5 *
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.
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
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
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/>.
18 */
19
20 #include <assert.h>
21 #include <errno.h>
22 #include <getopt.h>
23 #include <limits.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <sys/utsname.h>
32 #include <sys/wait.h>
33
34 #include <shared/array.h>
35 #include <shared/macro.h>
36
37 #include <libkmod/libkmod.h>
38
39 #include "kmod.h"
40
41 static int log_priority = LOG_CRIT;
42 static int use_syslog = 0;
43 #define LOG(...) log_printf(log_priority, __VA_ARGS__)
44
45 #define DEFAULT_VERBOSE LOG_WARNING
46 static int verbose = DEFAULT_VERBOSE;
47 static int do_show = 0;
48 static int dry_run = 0;
49 static int ignore_loaded = 0;
50 static int lookup_only = 0;
51 static int first_time = 0;
52 static int ignore_commands = 0;
53 static int use_blacklist = 0;
54 static int force = 0;
55 static int strip_modversion = 0;
56 static int strip_vermagic = 0;
57 static int remove_dependencies = 0;
58 static int quiet_inuse = 0;
59
60 static const char cmdopts_s[] = "arRibfDcnC:d:S:sqvVh";
61 static const struct option cmdopts[] = {
62 {"all", no_argument, 0, 'a'},
63 {"remove", no_argument, 0, 'r'},
64 {"remove-dependencies", no_argument, 0, 5},
65 {"resolve-alias", no_argument, 0, 'R'},
66 {"first-time", no_argument, 0, 3},
67 {"ignore-install", no_argument, 0, 'i'},
68 {"ignore-remove", no_argument, 0, 'i'},
69 {"use-blacklist", no_argument, 0, 'b'},
70 {"force", no_argument, 0, 'f'},
71 {"force-modversion", no_argument, 0, 2},
72 {"force-vermagic", no_argument, 0, 1},
73
74 {"show-depends", no_argument, 0, 'D'},
75 {"showconfig", no_argument, 0, 'c'},
76 {"show-config", no_argument, 0, 'c'},
77 {"show-modversions", no_argument, 0, 4},
78 {"dump-modversions", no_argument, 0, 4},
79
80 {"dry-run", no_argument, 0, 'n'},
81 {"show", no_argument, 0, 'n'},
82
83 {"config", required_argument, 0, 'C'},
84 {"dirname", required_argument, 0, 'd'},
85 {"set-version", required_argument, 0, 'S'},
86
87 {"syslog", no_argument, 0, 's'},
88 {"quiet", no_argument, 0, 'q'},
89 {"verbose", no_argument, 0, 'v'},
90 {"version", no_argument, 0, 'V'},
91 {"help", no_argument, 0, 'h'},
92 {NULL, 0, 0, 0}
93 };
94
help(void)95 static void help(void)
96 {
97 printf("Usage:\n"
98 "\t%s [options] [-i] [-b] modulename\n"
99 "\t%s [options] -a [-i] [-b] modulename [modulename...]\n"
100 "\t%s [options] -r [-i] modulename\n"
101 "\t%s [options] -r -a [-i] modulename [modulename...]\n"
102 "\t%s [options] -c\n"
103 "\t%s [options] --dump-modversions filename\n"
104 "Management Options:\n"
105 "\t-a, --all Consider every non-argument to\n"
106 "\t be a module name to be inserted\n"
107 "\t or removed (-r)\n"
108 "\t-r, --remove Remove modules instead of inserting\n"
109 "\t --remove-dependencies Also remove modules depending on it\n"
110 "\t-R, --resolve-alias Only lookup and print alias and exit\n"
111 "\t --first-time Fail if module already inserted or removed\n"
112 "\t-i, --ignore-install Ignore install commands\n"
113 "\t-i, --ignore-remove Ignore remove commands\n"
114 "\t-b, --use-blacklist Apply blacklist to resolved alias.\n"
115 "\t-f, --force Force module insertion or removal.\n"
116 "\t implies --force-modversions and\n"
117 "\t --force-vermagic\n"
118 "\t --force-modversion Ignore module's version\n"
119 "\t --force-vermagic Ignore module's version magic\n"
120 "\n"
121 "Query Options:\n"
122 "\t-D, --show-depends Only print module dependencies and exit\n"
123 "\t-c, --showconfig Print out known configuration and exit\n"
124 "\t-c, --show-config Same as --showconfig\n"
125 "\t --show-modversions Dump module symbol version and exit\n"
126 "\t --dump-modversions Same as --show-modversions\n"
127 "\n"
128 "General Options:\n"
129 "\t-n, --dry-run Do not execute operations, just print out\n"
130 "\t-n, --show Same as --dry-run\n"
131
132 "\t-C, --config=FILE Use FILE instead of default search paths\n"
133 "\t-d, --dirname=DIR Use DIR as filesystem root for /lib/modules\n"
134 "\t-S, --set-version=VERSION Use VERSION instead of `uname -r`\n"
135
136 "\t-s, --syslog print to syslog, not stderr\n"
137 "\t-q, --quiet disable messages\n"
138 "\t-v, --verbose enables more messages\n"
139 "\t-V, --version show version\n"
140 "\t-h, --help show this help\n",
141 program_invocation_short_name, program_invocation_short_name,
142 program_invocation_short_name, program_invocation_short_name,
143 program_invocation_short_name, program_invocation_short_name);
144 }
145
146 _printf_format_(1, 2)
_show(const char * fmt,...)147 static inline void _show(const char *fmt, ...)
148 {
149 va_list args;
150
151 if (!do_show && verbose <= DEFAULT_VERBOSE)
152 return;
153
154 va_start(args, fmt);
155 vfprintf(stdout, fmt, args);
156 fflush(stdout);
157 va_end(args);
158 }
159 #define SHOW(...) _show(__VA_ARGS__)
160
show_config(struct kmod_ctx * ctx)161 static int show_config(struct kmod_ctx *ctx)
162 {
163 struct config_iterators {
164 const char *name;
165 struct kmod_config_iter *(*get_iter)(const struct kmod_ctx *ctx);
166 } ci[] = {
167 { "blacklist", kmod_config_get_blacklists },
168 { "install", kmod_config_get_install_commands },
169 { "remove", kmod_config_get_remove_commands },
170 { "alias", kmod_config_get_aliases },
171 { "options", kmod_config_get_options },
172 { "softdep", kmod_config_get_softdeps },
173 };
174 size_t i;
175
176 for (i = 0; i < ARRAY_SIZE(ci); i++) {
177 struct kmod_config_iter *iter = ci[i].get_iter(ctx);
178
179 if (iter == NULL)
180 continue;
181
182 while (kmod_config_iter_next(iter)) {
183 const char *val;
184
185 printf("%s %s", ci[i].name,
186 kmod_config_iter_get_key(iter));
187 val = kmod_config_iter_get_value(iter);
188 if (val != NULL) {
189 putchar(' ');
190 puts(val);
191 } else
192 putchar('\n');
193 }
194
195 kmod_config_iter_free_iter(iter);
196 }
197
198 puts("\n# End of configuration files. Dumping indexes now:\n");
199 fflush(stdout);
200
201 kmod_dump_index(ctx, KMOD_INDEX_MODULES_ALIAS, STDOUT_FILENO);
202 kmod_dump_index(ctx, KMOD_INDEX_MODULES_SYMBOL, STDOUT_FILENO);
203
204 return 0;
205 }
206
show_modversions(struct kmod_ctx * ctx,const char * filename)207 static int show_modversions(struct kmod_ctx *ctx, const char *filename)
208 {
209 struct kmod_list *l, *list = NULL;
210 struct kmod_module *mod;
211 int err = kmod_module_new_from_path(ctx, filename, &mod);
212 if (err < 0) {
213 LOG("Module %s not found.\n", filename);
214 return err;
215 }
216
217 err = kmod_module_get_versions(mod, &list);
218 if (err < 0) {
219 LOG("could not get modversions of %s: %s\n",
220 filename, strerror(-err));
221 kmod_module_unref(mod);
222 return err;
223 }
224
225 kmod_list_foreach(l, list) {
226 const char *symbol = kmod_module_version_get_symbol(l);
227 uint64_t crc = kmod_module_version_get_crc(l);
228 printf("0x%08"PRIx64"\t%s\n", crc, symbol);
229 }
230 kmod_module_versions_free_list(list);
231 kmod_module_unref(mod);
232 return 0;
233 }
234
command_do(struct kmod_module * module,const char * type,const char * command,const char * cmdline_opts)235 static int command_do(struct kmod_module *module, const char *type,
236 const char *command, const char *cmdline_opts)
237 {
238 const char *modname = kmod_module_get_name(module);
239 char *p, *cmd = NULL;
240 size_t cmdlen, cmdline_opts_len, varlen;
241 int ret = 0;
242
243 if (cmdline_opts == NULL)
244 cmdline_opts = "";
245 cmdline_opts_len = strlen(cmdline_opts);
246
247 cmd = strdup(command);
248 if (cmd == NULL)
249 return -ENOMEM;
250 cmdlen = strlen(cmd);
251 varlen = sizeof("$CMDLINE_OPTS") - 1;
252 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
253 size_t prefixlen = p - cmd;
254 size_t suffixlen = cmdlen - prefixlen - varlen;
255 size_t slen = cmdlen - varlen + cmdline_opts_len;
256 char *suffix = p + varlen;
257 char *s = malloc(slen + 1);
258 if (s == NULL) {
259 free(cmd);
260 return -ENOMEM;
261 }
262 memcpy(s, cmd, p - cmd);
263 memcpy(s + prefixlen, cmdline_opts, cmdline_opts_len);
264 memcpy(s + prefixlen + cmdline_opts_len, suffix, suffixlen);
265 s[slen] = '\0';
266
267 free(cmd);
268 cmd = s;
269 cmdlen = slen;
270 }
271
272 SHOW("%s %s\n", type, cmd);
273 if (dry_run)
274 goto end;
275
276 setenv("MODPROBE_MODULE", modname, 1);
277 ret = system(cmd);
278 unsetenv("MODPROBE_MODULE");
279 if (ret == -1 || WEXITSTATUS(ret)) {
280 LOG("Error running %s command for %s\n", type, modname);
281 if (ret != -1)
282 ret = -WEXITSTATUS(ret);
283 }
284
285 end:
286 free(cmd);
287 return ret;
288 }
289
rmmod_do_remove_module(struct kmod_module * mod)290 static int rmmod_do_remove_module(struct kmod_module *mod)
291 {
292 const char *modname = kmod_module_get_name(mod);
293 struct kmod_list *deps, *itr;
294 int flags = 0, err;
295
296 SHOW("rmmod %s\n", kmod_module_get_name(mod));
297
298 if (dry_run)
299 return 0;
300
301 if (force)
302 flags |= KMOD_REMOVE_FORCE;
303
304 err = kmod_module_remove_module(mod, flags);
305 if (err == -EEXIST) {
306 if (!first_time)
307 err = 0;
308 else
309 LOG("Module %s is not in kernel.\n", modname);
310 }
311
312 deps = kmod_module_get_dependencies(mod);
313 if (deps != NULL) {
314 kmod_list_foreach(itr, deps) {
315 struct kmod_module *dep = kmod_module_get_module(itr);
316 if (kmod_module_get_refcnt(dep) == 0)
317 rmmod_do_remove_module(dep);
318 kmod_module_unref(dep);
319 }
320 kmod_module_unref_list(deps);
321 }
322
323 return err;
324 }
325
326 static int rmmod_do_module(struct kmod_module *mod, bool do_dependencies);
327
rmmod_do_deps_list(struct kmod_list * list,bool stop_on_errors)328 static int rmmod_do_deps_list(struct kmod_list *list, bool stop_on_errors)
329 {
330 struct kmod_list *l;
331
332 kmod_list_foreach_reverse(l, list) {
333 struct kmod_module *m = kmod_module_get_module(l);
334 int r = rmmod_do_module(m, false);
335 kmod_module_unref(m);
336
337 if (r < 0 && stop_on_errors)
338 return r;
339 }
340
341 return 0;
342 }
343
rmmod_do_module(struct kmod_module * mod,bool do_dependencies)344 static int rmmod_do_module(struct kmod_module *mod, bool do_dependencies)
345 {
346 const char *modname = kmod_module_get_name(mod);
347 struct kmod_list *pre = NULL, *post = NULL;
348 const char *cmd = NULL;
349 int err;
350
351 if (!ignore_commands) {
352 err = kmod_module_get_softdeps(mod, &pre, &post);
353 if (err < 0) {
354 WRN("could not get softdeps of '%s': %s\n",
355 modname, strerror(-err));
356 return err;
357 }
358
359 cmd = kmod_module_get_remove_commands(mod);
360 }
361
362 if (cmd == NULL && !ignore_loaded) {
363 int state = kmod_module_get_initstate(mod);
364
365 if (state < 0) {
366 if (first_time) {
367 LOG("Module %s is not in kernel.\n", modname);
368 err = -ENOENT;
369 } else {
370 err = 0;
371 }
372 goto error;
373 } else if (state == KMOD_MODULE_BUILTIN) {
374 LOG("Module %s is builtin.\n", modname);
375 err = -ENOENT;
376 goto error;
377 }
378 }
379
380 rmmod_do_deps_list(post, false);
381
382 if (do_dependencies && remove_dependencies) {
383 struct kmod_list *deps = kmod_module_get_dependencies(mod);
384
385 err = rmmod_do_deps_list(deps, true);
386 if (err < 0)
387 goto error;
388 }
389
390 if (!ignore_loaded && !cmd) {
391 int usage = kmod_module_get_refcnt(mod);
392
393 if (usage > 0) {
394 if (!quiet_inuse)
395 LOG("Module %s is in use.\n", modname);
396
397 err = -EBUSY;
398 goto error;
399 }
400 }
401
402 if (cmd == NULL)
403 err = rmmod_do_remove_module(mod);
404 else
405 err = command_do(mod, "remove", cmd, NULL);
406
407 if (err < 0)
408 goto error;
409
410 rmmod_do_deps_list(pre, false);
411
412 error:
413 kmod_module_unref_list(pre);
414 kmod_module_unref_list(post);
415
416 return err;
417 }
418
rmmod(struct kmod_ctx * ctx,const char * alias)419 static int rmmod(struct kmod_ctx *ctx, const char *alias)
420 {
421 struct kmod_list *l, *list = NULL;
422 int err;
423
424 err = kmod_module_new_from_lookup(ctx, alias, &list);
425 if (err < 0)
426 return err;
427
428 if (list == NULL) {
429 LOG("Module %s not found.\n", alias);
430 err = -ENOENT;
431 }
432
433 kmod_list_foreach(l, list) {
434 struct kmod_module *mod = kmod_module_get_module(l);
435 err = rmmod_do_module(mod, true);
436 kmod_module_unref(mod);
437 if (err < 0)
438 break;
439 }
440
441 kmod_module_unref_list(list);
442 return err;
443 }
444
rmmod_all(struct kmod_ctx * ctx,char ** args,int nargs)445 static int rmmod_all(struct kmod_ctx *ctx, char **args, int nargs)
446 {
447 int i, err = 0;
448
449 for (i = 0; i < nargs; i++) {
450 int r = rmmod(ctx, args[i]);
451 if (r < 0)
452 err = r;
453 }
454
455 return err;
456 }
457
print_action(struct kmod_module * m,bool install,const char * options)458 static void print_action(struct kmod_module *m, bool install,
459 const char *options)
460 {
461 const char *path;
462
463 if (install) {
464 printf("install %s %s\n", kmod_module_get_install_commands(m),
465 options);
466 return;
467 }
468
469 path = kmod_module_get_path(m);
470
471 if (path == NULL) {
472 /*
473 * Either a builtin module, or an alias, print only for
474 * builtin
475 */
476 if (kmod_module_get_initstate(m) == KMOD_MODULE_BUILTIN)
477 printf("builtin %s\n", kmod_module_get_name(m));
478 } else
479 printf("insmod %s %s\n", kmod_module_get_path(m), options);
480 }
481
insmod(struct kmod_ctx * ctx,const char * alias,const char * extra_options)482 static int insmod(struct kmod_ctx *ctx, const char *alias,
483 const char *extra_options)
484 {
485 struct kmod_list *l, *list = NULL;
486 int err, flags = 0;
487
488 void (*show)(struct kmod_module *m, bool install,
489 const char *options) = NULL;
490
491 err = kmod_module_new_from_lookup(ctx, alias, &list);
492
493 if (list == NULL || err < 0) {
494 LOG("Module %s not found in directory %s\n", alias,
495 ctx ? kmod_get_dirname(ctx) : "(missing)");
496 return -ENOENT;
497 }
498
499 if (strip_modversion || force)
500 flags |= KMOD_PROBE_FORCE_MODVERSION;
501 if (strip_vermagic || force)
502 flags |= KMOD_PROBE_FORCE_VERMAGIC;
503 if (ignore_commands)
504 flags |= KMOD_PROBE_IGNORE_COMMAND;
505 if (ignore_loaded)
506 flags |= KMOD_PROBE_IGNORE_LOADED;
507 if (dry_run)
508 flags |= KMOD_PROBE_DRY_RUN;
509 if (do_show || verbose > DEFAULT_VERBOSE)
510 show = &print_action;
511
512 flags |= KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY;
513
514 if (use_blacklist)
515 flags |= KMOD_PROBE_APPLY_BLACKLIST;
516 if (first_time)
517 flags |= KMOD_PROBE_FAIL_ON_LOADED;
518
519 kmod_list_foreach(l, list) {
520 struct kmod_module *mod = kmod_module_get_module(l);
521
522 if (lookup_only)
523 printf("%s\n", kmod_module_get_name(mod));
524 else {
525 err = kmod_module_probe_insert_module(mod, flags,
526 extra_options, NULL, NULL, show);
527 }
528
529 if (err >= 0)
530 /* ignore flag return values such as a mod being blacklisted */
531 err = 0;
532 else {
533 switch (err) {
534 case -EEXIST:
535 ERR("could not insert '%s': Module already in kernel\n",
536 kmod_module_get_name(mod));
537 break;
538 case -ENOENT:
539 ERR("could not insert '%s': Unknown symbol in module, "
540 "or unknown parameter (see dmesg)\n",
541 kmod_module_get_name(mod));
542 break;
543 default:
544 ERR("could not insert '%s': %s\n",
545 kmod_module_get_name(mod),
546 strerror(-err));
547 break;
548 }
549 }
550
551 kmod_module_unref(mod);
552 }
553
554 kmod_module_unref_list(list);
555 return err;
556 }
557
insmod_all(struct kmod_ctx * ctx,char ** args,int nargs)558 static int insmod_all(struct kmod_ctx *ctx, char **args, int nargs)
559 {
560 int i, err = 0;
561
562 for (i = 0; i < nargs; i++) {
563 int r = insmod(ctx, args[i], NULL);
564 if (r < 0)
565 err = r;
566 }
567
568 return err;
569 }
570
env_modprobe_options_append(const char * value)571 static void env_modprobe_options_append(const char *value)
572 {
573 const char *old = getenv("MODPROBE_OPTIONS");
574 char *env;
575
576 if (old == NULL) {
577 setenv("MODPROBE_OPTIONS", value, 1);
578 return;
579 }
580
581 if (asprintf(&env, "%s %s", old, value) < 0) {
582 ERR("could not append value to $MODPROBE_OPTIONS\n");
583 return;
584 }
585
586 if (setenv("MODPROBE_OPTIONS", env, 1) < 0)
587 ERR("could not setenv(MODPROBE_OPTIONS, \"%s\")\n", env);
588 free(env);
589 }
590
options_from_array(char ** args,int nargs,char ** output)591 static int options_from_array(char **args, int nargs, char **output)
592 {
593 char *opts = NULL;
594 size_t optslen = 0;
595 int i, err = 0;
596
597 for (i = 1; i < nargs; i++) {
598 size_t len = strlen(args[i]);
599 size_t qlen = 0;
600 const char *value;
601 void *tmp;
602
603 value = strchr(args[i], '=');
604 if (value) {
605 value++;
606 if (*value != '"' && *value != '\'') {
607 if (strchr(value, ' '))
608 qlen = 2;
609 }
610 }
611
612 tmp = realloc(opts, optslen + len + qlen + 2);
613 if (!tmp) {
614 err = -errno;
615 free(opts);
616 opts = NULL;
617 ERR("could not gather module options: out-of-memory\n");
618 break;
619 }
620 opts = tmp;
621 if (optslen > 0) {
622 opts[optslen] = ' ';
623 optslen++;
624 }
625 if (qlen == 0) {
626 memcpy(opts + optslen, args[i], len + 1);
627 optslen += len;
628 } else {
629 size_t keylen = value - args[i];
630 size_t valuelen = len - keylen;
631 memcpy(opts + optslen, args[i], keylen);
632 optslen += keylen;
633 opts[optslen] = '"';
634 optslen++;
635 memcpy(opts + optslen, value, valuelen);
636 optslen += valuelen;
637 opts[optslen] = '"';
638 optslen++;
639 opts[optslen] = '\0';
640 }
641 }
642
643 *output = opts;
644 return err;
645 }
646
prepend_options_from_env(int * p_argc,char ** orig_argv)647 static char **prepend_options_from_env(int *p_argc, char **orig_argv)
648 {
649 const char *p, *env = getenv("MODPROBE_OPTIONS");
650 char **new_argv, *str_start, *str_end, *str, *s, *quote;
651 int i, argc = *p_argc;
652 size_t envlen, space_count = 0;
653
654 if (env == NULL)
655 return orig_argv;
656
657 for (p = env; *p != '\0'; p++) {
658 if (*p == ' ')
659 space_count++;
660 }
661
662 envlen = p - env;
663 new_argv = malloc(sizeof(char *) * (argc + space_count + 3 + envlen));
664 if (new_argv == NULL)
665 return NULL;
666
667 new_argv[0] = orig_argv[0];
668 str_start = str = (char *) (new_argv + argc + space_count + 3);
669 memcpy(str, env, envlen + 1);
670
671 str_end = str_start + envlen;
672
673 quote = NULL;
674 for (i = 1, s = str; *s != '\0'; s++) {
675 if (quote == NULL) {
676 if (*s == ' ') {
677 new_argv[i] = str;
678 i++;
679 *s = '\0';
680 str = s + 1;
681 } else if (*s == '"' || *s == '\'')
682 quote = s;
683 } else {
684 if (*s == *quote) {
685 if (quote == str) {
686 new_argv[i] = str + 1;
687 i++;
688 *s = '\0';
689 str = s + 1;
690 } else {
691 char *it;
692 for (it = quote; it < s - 1; it++)
693 it[0] = it[1];
694 for (it = s - 1; it < str_end - 2; it++)
695 it[0] = it[2];
696 str_end -= 2;
697 *str_end = '\0';
698 s -= 2;
699 }
700 quote = NULL;
701 }
702 }
703 }
704 if (str < s) {
705 new_argv[i] = str;
706 i++;
707 }
708
709 memcpy(new_argv + i, orig_argv + 1, sizeof(char *) * (argc - 1));
710 new_argv[i + argc] = NULL;
711 *p_argc = i + argc - 1;
712
713 return new_argv;
714 }
715
do_modprobe(int argc,char ** orig_argv)716 static int do_modprobe(int argc, char **orig_argv)
717 {
718 struct kmod_ctx *ctx;
719 char **args = NULL, **argv;
720 const char **config_paths = NULL;
721 int nargs = 0, n_config_paths = 0;
722 char dirname_buf[PATH_MAX];
723 const char *dirname = NULL;
724 const char *root = NULL;
725 const char *kversion = NULL;
726 int use_all = 0;
727 int do_remove = 0;
728 int do_show_config = 0;
729 int do_show_modversions = 0;
730 int err;
731
732 argv = prepend_options_from_env(&argc, orig_argv);
733 if (argv == NULL) {
734 ERR("Could not prepend options from command line\n");
735 return EXIT_FAILURE;
736 }
737
738 for (;;) {
739 int c, idx = 0;
740 c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
741 if (c == -1)
742 break;
743 switch (c) {
744 case 'a':
745 log_priority = LOG_WARNING;
746 use_all = 1;
747 break;
748 case 'r':
749 do_remove = 1;
750 break;
751 case 5:
752 remove_dependencies = 1;
753 break;
754 case 'R':
755 lookup_only = 1;
756 break;
757 case 3:
758 first_time = 1;
759 break;
760 case 'i':
761 ignore_commands = 1;
762 break;
763 case 'b':
764 use_blacklist = 1;
765 break;
766 case 'f':
767 force = 1;
768 break;
769 case 2:
770 strip_modversion = 1;
771 break;
772 case 1:
773 strip_vermagic = 1;
774 break;
775 case 'D':
776 ignore_loaded = 1;
777 dry_run = 1;
778 do_show = 1;
779 break;
780 case 'c':
781 do_show_config = 1;
782 break;
783 case 4:
784 do_show_modversions = 1;
785 break;
786 case 'n':
787 dry_run = 1;
788 break;
789 case 'C': {
790 size_t bytes = sizeof(char *) * (n_config_paths + 2);
791 void *tmp = realloc(config_paths, bytes);
792 if (!tmp) {
793 ERR("out-of-memory\n");
794 err = -1;
795 goto done;
796 }
797 config_paths = tmp;
798 config_paths[n_config_paths] = optarg;
799 n_config_paths++;
800 config_paths[n_config_paths] = NULL;
801
802 env_modprobe_options_append("-C");
803 env_modprobe_options_append(optarg);
804 break;
805 }
806 case 'd':
807 root = optarg;
808 break;
809 case 'S':
810 kversion = optarg;
811 break;
812 case 's':
813 env_modprobe_options_append("-s");
814 use_syslog = 1;
815 break;
816 case 'q':
817 env_modprobe_options_append("-q");
818 verbose = LOG_EMERG;
819 break;
820 case 'v':
821 env_modprobe_options_append("-v");
822 verbose++;
823 break;
824 case 'V':
825 puts(PACKAGE " version " VERSION);
826 puts(KMOD_FEATURES);
827 err = 0;
828 goto done;
829 case 'h':
830 help();
831 err = 0;
832 goto done;
833 case '?':
834 err = -1;
835 goto done;
836 default:
837 ERR("unexpected getopt_long() value '%c'.\n", c);
838 err = -1;
839 goto done;
840 }
841 }
842
843 args = argv + optind;
844 nargs = argc - optind;
845
846 log_open(use_syslog);
847
848 if (!do_show_config) {
849 if (nargs == 0) {
850 ERR("missing parameters. See -h.\n");
851 err = -1;
852 goto done;
853 }
854 }
855
856 if (root != NULL || kversion != NULL) {
857 struct utsname u;
858 if (root == NULL)
859 root = "";
860 if (kversion == NULL) {
861 if (uname(&u) < 0) {
862 ERR("uname() failed: %m\n");
863 err = -1;
864 goto done;
865 }
866 kversion = u.release;
867 }
868 snprintf(dirname_buf, sizeof(dirname_buf),
869 "%s/lib/modules/%s", root,
870 kversion);
871 dirname = dirname_buf;
872 }
873
874 ctx = kmod_new(dirname, config_paths);
875 if (!ctx) {
876 ERR("kmod_new() failed!\n");
877 err = -1;
878 goto done;
879 }
880
881 log_setup_kmod_log(ctx, verbose);
882
883 kmod_load_resources(ctx);
884
885 if (do_show_config)
886 err = show_config(ctx);
887 else if (do_show_modversions)
888 err = show_modversions(ctx, args[0]);
889 else if (do_remove)
890 err = rmmod_all(ctx, args, nargs);
891 else if (use_all)
892 err = insmod_all(ctx, args, nargs);
893 else {
894 char *opts;
895 err = options_from_array(args, nargs, &opts);
896 if (err == 0) {
897 err = insmod(ctx, args[0], opts);
898 free(opts);
899 }
900 }
901
902 kmod_unref(ctx);
903
904 done:
905 log_close();
906
907 if (argv != orig_argv)
908 free(argv);
909
910 free(config_paths);
911
912 return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
913 }
914
915 const struct kmod_cmd kmod_cmd_compat_modprobe = {
916 .name = "modprobe",
917 .cmd = do_modprobe,
918 .help = "compat modprobe command",
919 };
920