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