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