blob: 43605ccaf0f0a6ba4e218e3d2f59296182a1e841 [file] [log] [blame]
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -02001/*
Lucas De Marchi8b013762012-01-12 17:14:30 -02002 * kmod-modprobe - manage linux kernel modules using libkmod.
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -02003 *
Lucas De Marchie6b0e492013-01-16 11:27:21 -02004 * Copyright (C) 2011-2013 ProFUSION embedded systems
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -02005 *
Lucas De Marchicb451f32011-12-12 18:24:35 -02006 * 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.
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020010 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Lucas De Marchicb451f32011-12-12 18:24:35 -020013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020015 *
Lucas De Marchicb451f32011-12-12 18:24:35 -020016 * 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/>.
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020018 */
Lucas De Marchicb451f32011-12-12 18:24:35 -020019
Lucas De Marchi4744ebc2012-03-15 00:14:35 -030020#include <assert.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030021#include <errno.h>
22#include <getopt.h>
23#include <limits.h>
24#include <stdbool.h>
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020025#include <stdio.h>
26#include <stdlib.h>
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020027#include <string.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030028#include <unistd.h>
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020029#include <sys/stat.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030030#include <sys/types.h>
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020031#include <sys/utsname.h>
Thierry Vignaudeff917c2012-01-17 17:32:48 -020032#include <sys/wait.h>
Lucas De Marchi74d1df62014-10-03 00:22:36 -030033
34#include <shared/array.h>
Lucas De Marchi576dd432014-10-02 22:03:19 -030035#include <shared/macro.h>
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020036
Lucas De Marchif3578662015-01-02 12:38:27 -020037#include <libkmod/libkmod.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030038
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -020039#include "kmod.h"
40
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020041static int log_priority = LOG_CRIT;
42static int use_syslog = 0;
Lucas De Marchifcb0ce92012-11-06 19:01:59 -020043#define LOG(...) log_printf(log_priority, __VA_ARGS__)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020044
45#define DEFAULT_VERBOSE LOG_WARNING
46static int verbose = DEFAULT_VERBOSE;
Gustavo Sverzut Barbieri525fa072012-01-08 13:58:28 -020047static int do_show = 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020048static int dry_run = 0;
49static int ignore_loaded = 0;
Lucas De Marchi81229852011-12-16 02:58:48 -020050static int lookup_only = 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020051static int first_time = 0;
52static int ignore_commands = 0;
53static int use_blacklist = 0;
54static int force = 0;
55static int strip_modversion = 0;
56static int strip_vermagic = 0;
57static int remove_dependencies = 0;
Lucas De Marchid8a6c0c2012-01-01 06:07:46 -020058static int quiet_inuse = 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020059
Dave Reisnercc988302012-01-26 11:36:35 -050060static const char cmdopts_s[] = "arRibfDcnC:d:S:sqvVh";
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020061static 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
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020074 {"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
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -020095static void help(void)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020096{
Lucas De Marchi34e06bf2012-11-06 17:32:41 -020097 printf("Usage:\n"
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020098 "\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"
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200102 "\t%s [options] -c\n"
103 "\t%s [options] --dump-modversions filename\n"
104 "Management Options:\n"
Gustavo Sverzut Barbieriab70dce2011-12-19 13:02:15 -0200105 "\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"
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200120 "\n"
121 "Query Options:\n"
Gustavo Sverzut Barbieriab70dce2011-12-19 13:02:15 -0200122 "\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"
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200127 "\n"
128 "General Options:\n"
Gustavo Sverzut Barbieriab70dce2011-12-19 13:02:15 -0200129 "\t-n, --dry-run Do not execute operations, just print out\n"
130 "\t-n, --show Same as --dry-run\n"
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200131
Lucas De Marchi2c966932011-12-20 16:39:59 -0200132 "\t-C, --config=FILE Use FILE instead of default search paths\n"
Dave Reisnerc5b37db2012-09-27 11:00:42 -0400133 "\t-d, --dirname=DIR Use DIR as filesystem root for /lib/modules\n"
Gustavo Sverzut Barbieriab70dce2011-12-19 13:02:15 -0200134 "\t-S, --set-version=VERSION Use VERSION instead of `uname -r`\n"
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200135
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",
Lucas De Marchi7c04aee2012-11-06 19:20:09 -0200141 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);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200144}
145
Lucas De Marchi1958af82013-04-21 16:16:18 -0300146_printf_format_(1, 2)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200147static inline void _show(const char *fmt, ...)
148{
149 va_list args;
150
Gustavo Sverzut Barbieri525fa072012-01-08 13:58:28 -0200151 if (!do_show && verbose <= DEFAULT_VERBOSE)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200152 return;
153
154 va_start(args, fmt);
155 vfprintf(stdout, fmt, args);
156 fflush(stdout);
157 va_end(args);
158}
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200159#define SHOW(...) _show(__VA_ARGS__)
160
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200161static int show_config(struct kmod_ctx *ctx)
162{
Lucas De Marchibc434962012-01-13 02:35:34 -0200163 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 },
Elan Ruusamäe02629fa2012-03-22 12:00:16 -0300171 { "options", kmod_config_get_options },
Lucas De Marchibc434962012-01-13 02:35:34 -0200172 { "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
Lucas De Marchi28f32c62012-01-27 23:56:46 -0200198 puts("\n# End of configuration files. Dumping indexes now:\n");
Lucas De Marchi09e9ae52012-01-17 10:05:02 -0200199 fflush(stdout);
200
Lucas De Marchi49a16372012-01-16 16:00:35 -0200201 kmod_dump_index(ctx, KMOD_INDEX_MODULES_ALIAS, STDOUT_FILENO);
202 kmod_dump_index(ctx, KMOD_INDEX_MODULES_SYMBOL, STDOUT_FILENO);
203
Lucas De Marchibc434962012-01-13 02:35:34 -0200204 return 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200205}
206
207static int show_modversions(struct kmod_ctx *ctx, const char *filename)
208{
Gustavo Sverzut Barbieri0e3e2f42011-12-19 12:45:22 -0200209 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) {
Dave Reisner63698372012-01-04 10:41:50 -0500219 LOG("could not get modversions of %s: %s\n",
Gustavo Sverzut Barbieri0e3e2f42011-12-19 12:45:22 -0200220 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;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200233}
234
Lucas De Marchi8f192212012-01-11 15:38:50 -0200235static int command_do(struct kmod_module *module, const char *type,
236 const char *command, const char *cmdline_opts)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200237{
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
285end:
286 free(cmd);
287 return ret;
288}
289
Lucas De Marchia872bba2012-01-12 15:23:51 -0200290static int rmmod_do_remove_module(struct kmod_module *mod)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200291{
Lucas De Marchia872bba2012-01-12 15:23:51 -0200292 const char *modname = kmod_module_get_name(mod);
Dave Reisner69a19742012-01-30 17:16:50 -0500293 struct kmod_list *deps, *itr;
Lucas De Marchia872bba2012-01-12 15:23:51 -0200294 int flags = 0, err;
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200295
Lucas De Marchia872bba2012-01-12 15:23:51 -0200296 SHOW("rmmod %s\n", kmod_module_get_name(mod));
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200297
Lucas De Marchia872bba2012-01-12 15:23:51 -0200298 if (dry_run)
299 return 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200300
Lucas De Marchia872bba2012-01-12 15:23:51 -0200301 if (force)
302 flags |= KMOD_REMOVE_FORCE;
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200303
Lucas De Marchia872bba2012-01-12 15:23:51 -0200304 err = kmod_module_remove_module(mod, flags);
305 if (err == -EEXIST) {
306 if (!first_time)
307 err = 0;
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200308 else
Lucas De Marchia872bba2012-01-12 15:23:51 -0200309 LOG("Module %s is not in kernel.\n", modname);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200310 }
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200311
Dave Reisner69a19742012-01-30 17:16:50 -0500312 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
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200323 return err;
324}
325
Lucas De Marchia872bba2012-01-12 15:23:51 -0200326static int rmmod_do_module(struct kmod_module *mod, bool do_dependencies);
327
328static int rmmod_do_deps_list(struct kmod_list *list, bool stop_on_errors)
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200329{
Lucas De Marchia872bba2012-01-12 15:23:51 -0200330 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;
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200342}
343
Lucas De Marchia872bba2012-01-12 15:23:51 -0200344static int rmmod_do_module(struct kmod_module *mod, bool do_dependencies)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200345{
346 const char *modname = kmod_module_get_name(mod);
Lucas De Marchia872bba2012-01-12 15:23:51 -0200347 struct kmod_list *pre = NULL, *post = NULL;
348 const char *cmd = NULL;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200349 int err;
350
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200351 if (!ignore_commands) {
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200352 err = kmod_module_get_softdeps(mod, &pre, &post);
353 if (err < 0) {
354 WRN("could not get softdeps of '%s': %s\n",
Lucas De Marchi8f192212012-01-11 15:38:50 -0200355 modname, strerror(-err));
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200356 return err;
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200357 }
358
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200359 cmd = kmod_module_get_remove_commands(mod);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200360 }
361
Lucas De Marchia872bba2012-01-12 15:23:51 -0200362 if (cmd == NULL && !ignore_loaded) {
Lucas De Marchi9bf60d22011-12-19 02:18:14 -0200363 int state = kmod_module_get_initstate(mod);
364
Lucas De Marchie5e2a682011-12-19 02:26:34 -0200365 if (state < 0) {
Dave Reisnerf758caf2012-03-14 22:15:21 -0400366 if (first_time) {
367 LOG("Module %s is not in kernel.\n", modname);
368 err = -ENOENT;
369 } else {
370 err = 0;
371 }
Lucas De Marchie4e1e642012-01-12 15:36:54 -0200372 goto error;
Lucas De Marchie5e2a682011-12-19 02:26:34 -0200373 } else if (state == KMOD_MODULE_BUILTIN) {
Lucas De Marchi9bf60d22011-12-19 02:18:14 -0200374 LOG("Module %s is builtin.\n", modname);
Lucas De Marchie4e1e642012-01-12 15:36:54 -0200375 err = -ENOENT;
376 goto error;
Lucas De Marchi9bf60d22011-12-19 02:18:14 -0200377 }
378 }
379
Lucas De Marchia872bba2012-01-12 15:23:51 -0200380 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);
Lucas De Marchi9bf60d22011-12-19 02:18:14 -0200386 if (err < 0)
Lucas De Marchia872bba2012-01-12 15:23:51 -0200387 goto error;
Lucas De Marchi9bf60d22011-12-19 02:18:14 -0200388 }
389
Johannes Bergc010f022013-05-02 15:23:28 +0200390 if (!ignore_loaded && !cmd) {
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200391 int usage = kmod_module_get_refcnt(mod);
Lucas De Marchid8a6c0c2012-01-01 06:07:46 -0200392
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200393 if (usage > 0) {
Lucas De Marchid8a6c0c2012-01-01 06:07:46 -0200394 if (!quiet_inuse)
395 LOG("Module %s is in use.\n", modname);
396
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200397 err = -EBUSY;
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200398 goto error;
399 }
400 }
401
Lucas De Marchia872bba2012-01-12 15:23:51 -0200402 if (cmd == NULL)
403 err = rmmod_do_remove_module(mod);
404 else
405 err = command_do(mod, "remove", cmd, NULL);
Lucas De Marchid8a6c0c2012-01-01 06:07:46 -0200406
Lucas De Marchia872bba2012-01-12 15:23:51 -0200407 if (err < 0)
408 goto error;
Lucas De Marchid8a6c0c2012-01-01 06:07:46 -0200409
Lucas De Marchia872bba2012-01-12 15:23:51 -0200410 rmmod_do_deps_list(pre, false);
Dave Reisner0e9bd2d2011-12-31 18:02:45 -0500411
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200412error:
413 kmod_module_unref_list(pre);
414 kmod_module_unref_list(post);
Lucas De Marchia872bba2012-01-12 15:23:51 -0200415
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200416 return err;
417}
418
Lucas De Marchi569f1602012-01-21 02:45:06 -0200419static int rmmod(struct kmod_ctx *ctx, const char *alias)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200420{
421 struct kmod_list *l, *list = NULL;
422 int err;
423
424 err = kmod_module_new_from_lookup(ctx, alias, &list);
Lucas De Marchie5e2a682011-12-19 02:26:34 -0200425 if (err < 0)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200426 return err;
Lucas De Marchie5e2a682011-12-19 02:26:34 -0200427
Dave Reisnerf758caf2012-03-14 22:15:21 -0400428 if (list == NULL) {
Lucas De Marchie5e2a682011-12-19 02:26:34 -0200429 LOG("Module %s not found.\n", alias);
Dave Reisnerf758caf2012-03-14 22:15:21 -0400430 err = -ENOENT;
431 }
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200432
433 kmod_list_foreach(l, list) {
434 struct kmod_module *mod = kmod_module_get_module(l);
Lucas De Marchia872bba2012-01-12 15:23:51 -0200435 err = rmmod_do_module(mod, true);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200436 kmod_module_unref(mod);
437 if (err < 0)
438 break;
439 }
440
441 kmod_module_unref_list(list);
442 return err;
443}
444
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200445static 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
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200458static void print_action(struct kmod_module *m, bool install,
459 const char *options)
460{
Lucas De Marchi4744ebc2012-03-15 00:14:35 -0300461 const char *path;
462
463 if (install) {
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200464 printf("install %s %s\n", kmod_module_get_install_commands(m),
465 options);
Lucas De Marchi4744ebc2012-03-15 00:14:35 -0300466 return;
467 }
468
469 path = kmod_module_get_path(m);
470
471 if (path == NULL) {
Lucas De Marchiace742f2013-03-21 02:33:25 -0300472 /*
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));
Lucas De Marchi4744ebc2012-03-15 00:14:35 -0300478 } else
479 printf("insmod %s %s\n", kmod_module_get_path(m), options);
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200480}
481
Lucas De Marchi569f1602012-01-21 02:45:06 -0200482static int insmod(struct kmod_ctx *ctx, const char *alias,
Lucas De Marchi8f192212012-01-11 15:38:50 -0200483 const char *extra_options)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200484{
485 struct kmod_list *l, *list = NULL;
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200486 int err, flags = 0;
487
488 void (*show)(struct kmod_module *m, bool install,
489 const char *options) = NULL;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200490
491 err = kmod_module_new_from_lookup(ctx, alias, &list);
Lucas De Marchie5e2a682011-12-19 02:26:34 -0200492
Laura Abbottb87d01d2015-09-28 15:39:11 -0700493 if (list == NULL || err < 0) {
494 LOG("Module %s not found in directory %s\n", alias,
495 ctx ? kmod_get_dirname(ctx) : "(missing)");
Lucas De Marchie2719b32015-02-27 15:39:36 -0300496 return -ENOENT;
497 }
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200498
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200499 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
Lucas De Marchi36ddee62012-08-17 09:42:47 -0300512 flags |= KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY;
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200513
514 if (use_blacklist)
515 flags |= KMOD_PROBE_APPLY_BLACKLIST;
516 if (first_time)
Lucas De Marchi814a57b2012-02-06 12:46:39 -0200517 flags |= KMOD_PROBE_FAIL_ON_LOADED;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200518
519 kmod_list_foreach(l, list) {
520 struct kmod_module *mod = kmod_module_get_module(l);
Lucas De Marchi8b013762012-01-12 17:14:30 -0200521
Lucas De Marchi81229852011-12-16 02:58:48 -0200522 if (lookup_only)
523 printf("%s\n", kmod_module_get_name(mod));
Lucas De Marchi8b013762012-01-12 17:14:30 -0200524 else {
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200525 err = kmod_module_probe_insert_module(mod, flags,
526 extra_options, NULL, NULL, show);
Lucas De Marchi8b013762012-01-12 17:14:30 -0200527 }
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200528
Dave Reisner297a3182012-01-30 20:57:36 -0500529 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;
Dave Reisnerccb64d12012-04-16 10:37:32 -0400538 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;
Dave Reisner297a3182012-01-30 20:57:36 -0500543 default:
544 ERR("could not insert '%s': %s\n",
545 kmod_module_get_name(mod),
546 strerror(-err));
547 break;
548 }
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200549 }
550
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200551 kmod_module_unref(mod);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200552 }
553
554 kmod_module_unref_list(list);
555 return err;
556}
557
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200558static 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
571static 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
591static 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
647static 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
Lucas De Marchifa29c0e2011-12-22 03:54:46 -0200716static int do_modprobe(int argc, char **orig_argv)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200717{
718 struct kmod_ctx *ctx;
719 char **args = NULL, **argv;
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200720 const char **config_paths = NULL;
721 int nargs = 0, n_config_paths = 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200722 char dirname_buf[PATH_MAX];
723 const char *dirname = NULL;
724 const char *root = NULL;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200725 const char *kversion = NULL;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200726 int use_all = 0;
727 int do_remove = 0;
728 int do_show_config = 0;
729 int do_show_modversions = 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200730 int err;
731
732 argv = prepend_options_from_env(&argc, orig_argv);
733 if (argv == NULL) {
Lucas De Marchid9a2e152012-11-01 13:02:10 -0200734 ERR("Could not prepend options from command line\n");
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200735 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':
Lucas De Marchi81229852011-12-16 02:58:48 -0200755 lookup_only = 1;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200756 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;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200775 case 'D':
776 ignore_loaded = 1;
777 dry_run = 1;
Gustavo Sverzut Barbieri525fa072012-01-08 13:58:28 -0200778 do_show = 1;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200779 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;
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200789 case 'C': {
790 size_t bytes = sizeof(char *) * (n_config_paths + 2);
791 void *tmp = realloc(config_paths, bytes);
792 if (!tmp) {
Lucas De Marchid9a2e152012-11-01 13:02:10 -0200793 ERR("out-of-memory\n");
794 err = -1;
795 goto done;
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200796 }
797 config_paths = tmp;
798 config_paths[n_config_paths] = optarg;
799 n_config_paths++;
800 config_paths[n_config_paths] = NULL;
801
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200802 env_modprobe_options_append("-C");
803 env_modprobe_options_append(optarg);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200804 break;
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200805 }
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200806 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");
Lucas De Marchiae7ebe82012-03-15 01:11:10 -0300818 verbose = LOG_EMERG;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200819 break;
820 case 'v':
821 env_modprobe_options_append("-v");
822 verbose++;
823 break;
824 case 'V':
825 puts(PACKAGE " version " VERSION);
Lucas De Marchi655de272015-06-07 02:46:22 -0300826 puts(KMOD_FEATURES);
Lucas De Marchid9a2e152012-11-01 13:02:10 -0200827 err = 0;
Lucas De Marchi4434d8b2012-10-31 21:29:54 -0200828 goto done;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200829 case 'h':
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -0200830 help();
Lucas De Marchid9a2e152012-11-01 13:02:10 -0200831 err = 0;
Lucas De Marchi4434d8b2012-10-31 21:29:54 -0200832 goto done;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200833 case '?':
Lucas De Marchid9a2e152012-11-01 13:02:10 -0200834 err = -1;
835 goto done;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200836 default:
Lucas De Marchid9a2e152012-11-01 13:02:10 -0200837 ERR("unexpected getopt_long() value '%c'.\n", c);
838 err = -1;
839 goto done;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200840 }
841 }
842
843 args = argv + optind;
844 nargs = argc - optind;
845
Lucas De Marchi92aad742012-11-06 18:04:09 -0200846 log_open(use_syslog);
Lucas De Marchid9a2e152012-11-01 13:02:10 -0200847
Dave Reisnerb09668c2011-12-31 16:51:40 -0500848 if (!do_show_config) {
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200849 if (nargs == 0) {
Lucas De Marchid9a2e152012-11-01 13:02:10 -0200850 ERR("missing parameters. See -h.\n");
851 err = -1;
852 goto done;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200853 }
854 }
855
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200856 if (root != NULL || kversion != NULL) {
857 struct utsname u;
858 if (root == NULL)
859 root = "";
860 if (kversion == NULL) {
861 if (uname(&u) < 0) {
Lucas De Marchid9a2e152012-11-01 13:02:10 -0200862 ERR("uname() failed: %m\n");
863 err = -1;
864 goto done;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200865 }
866 kversion = u.release;
867 }
Lucas De Marchi8f192212012-01-11 15:38:50 -0200868 snprintf(dirname_buf, sizeof(dirname_buf),
Dave Reisnerc5b37db2012-09-27 11:00:42 -0400869 "%s/lib/modules/%s", root,
Lucas De Marchi8f192212012-01-11 15:38:50 -0200870 kversion);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200871 dirname = dirname_buf;
872 }
873
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200874 ctx = kmod_new(dirname, config_paths);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200875 if (!ctx) {
Lucas De Marchid9a2e152012-11-01 13:02:10 -0200876 ERR("kmod_new() failed!\n");
877 err = -1;
878 goto done;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200879 }
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200880
Lucas De Marchi52a50fe2012-11-06 18:26:34 -0200881 log_setup_kmod_log(ctx, verbose);
Lucas De Marchid9a2e152012-11-01 13:02:10 -0200882
883 kmod_load_resources(ctx);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200884
Dave Reisnerb09668c2011-12-31 16:51:40 -0500885 if (do_show_config)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200886 err = show_config(ctx);
887 else if (do_show_modversions)
888 err = show_modversions(ctx, args[0]);
889 else if (do_remove)
Lucas De Marchic1b84542012-03-15 00:27:18 -0300890 err = rmmod_all(ctx, args, nargs);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200891 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
Lucas De Marchid9a2e152012-11-01 13:02:10 -0200904done:
Lucas De Marchi92aad742012-11-06 18:04:09 -0200905 log_close();
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200906
907 if (argv != orig_argv)
908 free(argv);
Lucas De Marchi8f192212012-01-11 15:38:50 -0200909
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200910 free(config_paths);
Lucas De Marchid9a2e152012-11-01 13:02:10 -0200911
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200912 return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200913}
Lucas De Marchifa29c0e2011-12-22 03:54:46 -0200914
Lucas De Marchifa29c0e2011-12-22 03:54:46 -0200915const struct kmod_cmd kmod_cmd_compat_modprobe = {
916 .name = "modprobe",
917 .cmd = do_modprobe,
918 .help = "compat modprobe command",
919};