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 #define RMMOD_FLAG_DO_DEPENDENCIES 0x1
357 #define RMMOD_FLAG_IGNORE_BUILTIN 0x2
358 static int rmmod_do_module(struct kmod_module *mod, int flags);
359
rmmod_do_deps_list(struct kmod_list * list,bool stop_on_errors)360 static int rmmod_do_deps_list(struct kmod_list *list, bool stop_on_errors)
361 {
362 struct kmod_list *l;
363
364 kmod_list_foreach_reverse(l, list) {
365 struct kmod_module *m = kmod_module_get_module(l);
366 int r = rmmod_do_module(m, RMMOD_FLAG_IGNORE_BUILTIN);
367 kmod_module_unref(m);
368
369 if (r < 0 && stop_on_errors)
370 return r;
371 }
372
373 return 0;
374 }
375
rmmod_do_module(struct kmod_module * mod,int flags)376 static int rmmod_do_module(struct kmod_module *mod, int flags)
377 {
378 const char *modname = kmod_module_get_name(mod);
379 struct kmod_list *pre = NULL, *post = NULL;
380 const char *cmd = NULL;
381 int err;
382
383 if (!ignore_commands) {
384 err = kmod_module_get_softdeps(mod, &pre, &post);
385 if (err < 0) {
386 WRN("could not get softdeps of '%s': %s\n",
387 modname, strerror(-err));
388 return err;
389 }
390
391 cmd = kmod_module_get_remove_commands(mod);
392 }
393
394 if (cmd == NULL && !ignore_loaded) {
395 int state = kmod_module_get_initstate(mod);
396
397 if (state < 0) {
398 if (first_time) {
399 LOG("Module %s is not in kernel.\n", modname);
400 err = -ENOENT;
401 } else {
402 err = 0;
403 }
404 goto error;
405 } else if (state == KMOD_MODULE_BUILTIN) {
406 if (flags & RMMOD_FLAG_IGNORE_BUILTIN) {
407 err = 0;
408 } else {
409 LOG("Module %s is builtin.\n", modname);
410 err = -ENOENT;
411 }
412 goto error;
413 }
414 }
415
416 rmmod_do_deps_list(post, false);
417
418 if ((flags & RMMOD_FLAG_DO_DEPENDENCIES) && remove_dependencies) {
419 struct kmod_list *deps = kmod_module_get_dependencies(mod);
420
421 err = rmmod_do_deps_list(deps, true);
422 if (err < 0)
423 goto error;
424 }
425
426 if (!ignore_loaded && !cmd) {
427 int usage = kmod_module_get_refcnt(mod);
428
429 if (usage > 0) {
430 if (!quiet_inuse)
431 LOG("Module %s is in use.\n", modname);
432
433 err = -EBUSY;
434 goto error;
435 }
436 }
437
438 if (cmd == NULL)
439 err = rmmod_do_remove_module(mod);
440 else
441 err = command_do(mod, "remove", cmd, NULL);
442
443 if (err < 0)
444 goto error;
445
446 rmmod_do_deps_list(pre, false);
447
448 error:
449 kmod_module_unref_list(pre);
450 kmod_module_unref_list(post);
451
452 return err;
453 }
454
rmmod(struct kmod_ctx * ctx,const char * alias)455 static int rmmod(struct kmod_ctx *ctx, const char *alias)
456 {
457 struct kmod_list *l, *list = NULL;
458 int err;
459
460 err = kmod_module_new_from_lookup(ctx, alias, &list);
461 if (err < 0)
462 return err;
463
464 if (list == NULL) {
465 LOG("Module %s not found.\n", alias);
466 err = -ENOENT;
467 }
468
469 kmod_list_foreach(l, list) {
470 struct kmod_module *mod = kmod_module_get_module(l);
471 err = rmmod_do_module(mod, RMMOD_FLAG_DO_DEPENDENCIES);
472 kmod_module_unref(mod);
473 if (err < 0)
474 break;
475 }
476
477 kmod_module_unref_list(list);
478 return err;
479 }
480
rmmod_all(struct kmod_ctx * ctx,char ** args,int nargs)481 static int rmmod_all(struct kmod_ctx *ctx, char **args, int nargs)
482 {
483 int i, err = 0;
484
485 for (i = 0; i < nargs; i++) {
486 int r = rmmod(ctx, args[i]);
487 if (r < 0)
488 err = r;
489 }
490
491 return err;
492 }
493
print_action(struct kmod_module * m,bool install,const char * options)494 static void print_action(struct kmod_module *m, bool install,
495 const char *options)
496 {
497 const char *path;
498
499 if (install) {
500 printf("install %s %s\n", kmod_module_get_install_commands(m),
501 options);
502 return;
503 }
504
505 path = kmod_module_get_path(m);
506
507 if (path == NULL) {
508 /*
509 * Either a builtin module, or an alias, print only for
510 * builtin
511 */
512 if (kmod_module_get_initstate(m) == KMOD_MODULE_BUILTIN)
513 printf("builtin %s\n", kmod_module_get_name(m));
514 } else
515 printf("insmod %s %s\n", kmod_module_get_path(m), options);
516 }
517
insmod(struct kmod_ctx * ctx,const char * alias,const char * extra_options)518 static int insmod(struct kmod_ctx *ctx, const char *alias,
519 const char *extra_options)
520 {
521 struct kmod_list *l, *list = NULL;
522 int err, flags = 0;
523
524 void (*show)(struct kmod_module *m, bool install,
525 const char *options) = NULL;
526
527 err = kmod_module_new_from_lookup(ctx, alias, &list);
528
529 if (list == NULL || err < 0) {
530 LOG("Module %s not found in directory %s\n", alias,
531 ctx ? kmod_get_dirname(ctx) : "(missing)");
532 return -ENOENT;
533 }
534
535 if (strip_modversion || force)
536 flags |= KMOD_PROBE_FORCE_MODVERSION;
537 if (strip_vermagic || force)
538 flags |= KMOD_PROBE_FORCE_VERMAGIC;
539 if (ignore_commands)
540 flags |= KMOD_PROBE_IGNORE_COMMAND;
541 if (ignore_loaded)
542 flags |= KMOD_PROBE_IGNORE_LOADED;
543 if (dry_run)
544 flags |= KMOD_PROBE_DRY_RUN;
545 if (do_show || verbose > DEFAULT_VERBOSE)
546 show = &print_action;
547
548 flags |= KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY;
549
550 if (use_blacklist)
551 flags |= KMOD_PROBE_APPLY_BLACKLIST;
552 if (first_time)
553 flags |= KMOD_PROBE_FAIL_ON_LOADED;
554
555 kmod_list_foreach(l, list) {
556 struct kmod_module *mod = kmod_module_get_module(l);
557
558 if (lookup_only)
559 printf("%s\n", kmod_module_get_name(mod));
560 else {
561 err = kmod_module_probe_insert_module(mod, flags,
562 extra_options, NULL, NULL, show);
563 }
564
565 if (err >= 0)
566 /* ignore flag return values such as a mod being blacklisted */
567 err = 0;
568 else {
569 switch (err) {
570 case -EEXIST:
571 ERR("could not insert '%s': Module already in kernel\n",
572 kmod_module_get_name(mod));
573 break;
574 case -ENOENT:
575 ERR("could not insert '%s': Unknown symbol in module, "
576 "or unknown parameter (see dmesg)\n",
577 kmod_module_get_name(mod));
578 break;
579 default:
580 ERR("could not insert '%s': %s\n",
581 kmod_module_get_name(mod),
582 strerror(-err));
583 break;
584 }
585 }
586
587 kmod_module_unref(mod);
588 }
589
590 kmod_module_unref_list(list);
591 return err;
592 }
593
insmod_all(struct kmod_ctx * ctx,char ** args,int nargs)594 static int insmod_all(struct kmod_ctx *ctx, char **args, int nargs)
595 {
596 int i, err = 0;
597
598 for (i = 0; i < nargs; i++) {
599 int r = insmod(ctx, args[i], NULL);
600 if (r < 0)
601 err = r;
602 }
603
604 return err;
605 }
606
env_modprobe_options_append(const char * value)607 static void env_modprobe_options_append(const char *value)
608 {
609 const char *old = getenv("MODPROBE_OPTIONS");
610 char *env;
611
612 if (old == NULL) {
613 setenv("MODPROBE_OPTIONS", value, 1);
614 return;
615 }
616
617 if (asprintf(&env, "%s %s", old, value) < 0) {
618 ERR("could not append value to $MODPROBE_OPTIONS\n");
619 return;
620 }
621
622 if (setenv("MODPROBE_OPTIONS", env, 1) < 0)
623 ERR("could not setenv(MODPROBE_OPTIONS, \"%s\")\n", env);
624 free(env);
625 }
626
options_from_array(char ** args,int nargs,char ** output)627 static int options_from_array(char **args, int nargs, char **output)
628 {
629 char *opts = NULL;
630 size_t optslen = 0;
631 int i, err = 0;
632
633 for (i = 1; i < nargs; i++) {
634 size_t len = strlen(args[i]);
635 size_t qlen = 0;
636 const char *value;
637 void *tmp;
638
639 value = strchr(args[i], '=');
640 if (value) {
641 value++;
642 if (*value != '"' && *value != '\'') {
643 if (strchr(value, ' '))
644 qlen = 2;
645 }
646 }
647
648 tmp = realloc(opts, optslen + len + qlen + 2);
649 if (!tmp) {
650 err = -errno;
651 free(opts);
652 opts = NULL;
653 ERR("could not gather module options: out-of-memory\n");
654 break;
655 }
656 opts = tmp;
657 if (optslen > 0) {
658 opts[optslen] = ' ';
659 optslen++;
660 }
661 if (qlen == 0) {
662 memcpy(opts + optslen, args[i], len + 1);
663 optslen += len;
664 } else {
665 size_t keylen = value - args[i];
666 size_t valuelen = len - keylen;
667 memcpy(opts + optslen, args[i], keylen);
668 optslen += keylen;
669 opts[optslen] = '"';
670 optslen++;
671 memcpy(opts + optslen, value, valuelen);
672 optslen += valuelen;
673 opts[optslen] = '"';
674 optslen++;
675 opts[optslen] = '\0';
676 }
677 }
678
679 *output = opts;
680 return err;
681 }
682
prepend_options_from_env(int * p_argc,char ** orig_argv)683 static char **prepend_options_from_env(int *p_argc, char **orig_argv)
684 {
685 const char *p, *env = getenv("MODPROBE_OPTIONS");
686 char **new_argv, *str_start, *str_end, *str, *s, *quote;
687 int i, argc = *p_argc;
688 size_t envlen, space_count = 0;
689
690 if (env == NULL)
691 return orig_argv;
692
693 for (p = env; *p != '\0'; p++) {
694 if (*p == ' ')
695 space_count++;
696 }
697
698 envlen = p - env;
699 new_argv = malloc(sizeof(char *) * (argc + space_count + 3 + envlen));
700 if (new_argv == NULL)
701 return NULL;
702
703 new_argv[0] = orig_argv[0];
704 str_start = str = (char *) (new_argv + argc + space_count + 3);
705 memcpy(str, env, envlen + 1);
706
707 str_end = str_start + envlen;
708
709 quote = NULL;
710 for (i = 1, s = str; *s != '\0'; s++) {
711 if (quote == NULL) {
712 if (*s == ' ') {
713 new_argv[i] = str;
714 i++;
715 *s = '\0';
716 str = s + 1;
717 } else if (*s == '"' || *s == '\'')
718 quote = s;
719 } else {
720 if (*s == *quote) {
721 if (quote == str) {
722 new_argv[i] = str + 1;
723 i++;
724 *s = '\0';
725 str = s + 1;
726 } else {
727 char *it;
728 for (it = quote; it < s - 1; it++)
729 it[0] = it[1];
730 for (it = s - 1; it < str_end - 2; it++)
731 it[0] = it[2];
732 str_end -= 2;
733 *str_end = '\0';
734 s -= 2;
735 }
736 quote = NULL;
737 }
738 }
739 }
740 if (str < s) {
741 new_argv[i] = str;
742 i++;
743 }
744
745 memcpy(new_argv + i, orig_argv + 1, sizeof(char *) * (argc - 1));
746 new_argv[i + argc] = NULL;
747 *p_argc = i + argc - 1;
748
749 return new_argv;
750 }
751
do_modprobe(int argc,char ** orig_argv)752 static int do_modprobe(int argc, char **orig_argv)
753 {
754 struct kmod_ctx *ctx;
755 char **args = NULL, **argv;
756 const char **config_paths = NULL;
757 int nargs = 0, n_config_paths = 0;
758 char dirname_buf[PATH_MAX];
759 const char *dirname = NULL;
760 const char *root = NULL;
761 const char *kversion = NULL;
762 int use_all = 0;
763 int do_remove = 0;
764 int do_show_config = 0;
765 int do_show_modversions = 0;
766 int do_show_exports = 0;
767 int err;
768
769 argv = prepend_options_from_env(&argc, orig_argv);
770 if (argv == NULL) {
771 ERR("Could not prepend options from command line\n");
772 return EXIT_FAILURE;
773 }
774
775 for (;;) {
776 int c, idx = 0;
777 c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
778 if (c == -1)
779 break;
780 switch (c) {
781 case 'a':
782 log_priority = LOG_WARNING;
783 use_all = 1;
784 break;
785 case 'r':
786 do_remove = 1;
787 break;
788 case 5:
789 remove_dependencies = 1;
790 break;
791 case 'R':
792 lookup_only = 1;
793 break;
794 case 3:
795 first_time = 1;
796 break;
797 case 'i':
798 ignore_commands = 1;
799 break;
800 case 'b':
801 use_blacklist = 1;
802 break;
803 case 'f':
804 force = 1;
805 break;
806 case 2:
807 strip_modversion = 1;
808 break;
809 case 1:
810 strip_vermagic = 1;
811 break;
812 case 'D':
813 ignore_loaded = 1;
814 dry_run = 1;
815 do_show = 1;
816 break;
817 case 'c':
818 do_show_config = 1;
819 break;
820 case 4:
821 do_show_modversions = 1;
822 break;
823 case 6:
824 do_show_exports = 1;
825 break;
826 case 'n':
827 dry_run = 1;
828 break;
829 case 'C': {
830 size_t bytes = sizeof(char *) * (n_config_paths + 2);
831 void *tmp = realloc(config_paths, bytes);
832 if (!tmp) {
833 ERR("out-of-memory\n");
834 err = -1;
835 goto done;
836 }
837 config_paths = tmp;
838 config_paths[n_config_paths] = optarg;
839 n_config_paths++;
840 config_paths[n_config_paths] = NULL;
841
842 env_modprobe_options_append("-C");
843 env_modprobe_options_append(optarg);
844 break;
845 }
846 case 'd':
847 root = optarg;
848 break;
849 case 'S':
850 kversion = optarg;
851 break;
852 case 's':
853 env_modprobe_options_append("-s");
854 use_syslog = 1;
855 break;
856 case 'q':
857 env_modprobe_options_append("-q");
858 verbose = LOG_EMERG;
859 break;
860 case 'v':
861 env_modprobe_options_append("-v");
862 verbose++;
863 break;
864 case 'V':
865 puts(PACKAGE " version " VERSION);
866 puts(KMOD_FEATURES);
867 err = 0;
868 goto done;
869 case 'h':
870 help();
871 err = 0;
872 goto done;
873 case '?':
874 err = -1;
875 goto done;
876 default:
877 ERR("unexpected getopt_long() value '%c'.\n", c);
878 err = -1;
879 goto done;
880 }
881 }
882
883 args = argv + optind;
884 nargs = argc - optind;
885
886 log_open(use_syslog);
887
888 if (!do_show_config) {
889 if (nargs == 0) {
890 ERR("missing parameters. See -h.\n");
891 err = -1;
892 goto done;
893 }
894 }
895
896 if (root != NULL || kversion != NULL) {
897 struct utsname u;
898 if (root == NULL)
899 root = "";
900 if (kversion == NULL) {
901 if (uname(&u) < 0) {
902 ERR("uname() failed: %m\n");
903 err = -1;
904 goto done;
905 }
906 kversion = u.release;
907 }
908 snprintf(dirname_buf, sizeof(dirname_buf),
909 "%s/lib/modules/%s", root,
910 kversion);
911 dirname = dirname_buf;
912 }
913
914 ctx = kmod_new(dirname, config_paths);
915 if (!ctx) {
916 ERR("kmod_new() failed!\n");
917 err = -1;
918 goto done;
919 }
920
921 log_setup_kmod_log(ctx, verbose);
922
923 kmod_load_resources(ctx);
924
925 if (do_show_config)
926 err = show_config(ctx);
927 else if (do_show_modversions)
928 err = show_modversions(ctx, args[0]);
929 else if (do_show_exports)
930 err = show_exports(ctx, args[0]);
931 else if (do_remove)
932 err = rmmod_all(ctx, args, nargs);
933 else if (use_all)
934 err = insmod_all(ctx, args, nargs);
935 else {
936 char *opts;
937 err = options_from_array(args, nargs, &opts);
938 if (err == 0) {
939 err = insmod(ctx, args[0], opts);
940 free(opts);
941 }
942 }
943
944 kmod_unref(ctx);
945
946 done:
947 log_close();
948
949 if (argv != orig_argv)
950 free(argv);
951
952 free(config_paths);
953
954 return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
955 }
956
957 const struct kmod_cmd kmod_cmd_compat_modprobe = {
958 .name = "modprobe",
959 .cmd = do_modprobe,
960 .help = "compat modprobe command",
961 };
962