blob: f6d47dc863359a639af99cae5ce40972cdc816f6 [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 Marchia66a6a92012-01-09 00:40:50 -02004 * Copyright (C) 2011-2012 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>
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020021#include <stdio.h>
22#include <stdlib.h>
Lucas De Marchi0cf28322012-01-12 02:21:26 -020023#include <stdbool.h>
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020024#include <getopt.h>
25#include <errno.h>
26#include <string.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/utsname.h>
Thierry Vignaudeff917c2012-01-17 17:32:48 -020030#include <sys/wait.h>
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020031#include <unistd.h>
32#include <syslog.h>
33#include <limits.h>
34
35#include "libkmod.h"
Lucas De Marchi8b013762012-01-12 17:14:30 -020036#include "libkmod-array.h"
Lucas De Marchibc434962012-01-13 02:35:34 -020037#include "macro.h"
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020038
39static int log_priority = LOG_CRIT;
40static int use_syslog = 0;
41
42#define DEFAULT_VERBOSE LOG_WARNING
43static int verbose = DEFAULT_VERBOSE;
Gustavo Sverzut Barbieri525fa072012-01-08 13:58:28 -020044static int do_show = 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020045static int dry_run = 0;
46static int ignore_loaded = 0;
Lucas De Marchi81229852011-12-16 02:58:48 -020047static int lookup_only = 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020048static int first_time = 0;
49static int ignore_commands = 0;
50static int use_blacklist = 0;
51static int force = 0;
52static int strip_modversion = 0;
53static int strip_vermagic = 0;
54static int remove_dependencies = 0;
Lucas De Marchid8a6c0c2012-01-01 06:07:46 -020055static int quiet_inuse = 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020056
Dave Reisnercc988302012-01-26 11:36:35 -050057static const char cmdopts_s[] = "arRibfDcnC:d:S:sqvVh";
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020058static const struct option cmdopts[] = {
59 {"all", no_argument, 0, 'a'},
60 {"remove", no_argument, 0, 'r'},
61 {"remove-dependencies", no_argument, 0, 5},
62 {"resolve-alias", no_argument, 0, 'R'},
63 {"first-time", no_argument, 0, 3},
64 {"ignore-install", no_argument, 0, 'i'},
65 {"ignore-remove", no_argument, 0, 'i'},
66 {"use-blacklist", no_argument, 0, 'b'},
67 {"force", no_argument, 0, 'f'},
68 {"force-modversion", no_argument, 0, 2},
69 {"force-vermagic", no_argument, 0, 1},
70
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020071 {"show-depends", no_argument, 0, 'D'},
72 {"showconfig", no_argument, 0, 'c'},
73 {"show-config", no_argument, 0, 'c'},
74 {"show-modversions", no_argument, 0, 4},
75 {"dump-modversions", no_argument, 0, 4},
76
77 {"dry-run", no_argument, 0, 'n'},
78 {"show", no_argument, 0, 'n'},
79
80 {"config", required_argument, 0, 'C'},
81 {"dirname", required_argument, 0, 'd'},
82 {"set-version", required_argument, 0, 'S'},
83
84 {"syslog", no_argument, 0, 's'},
85 {"quiet", no_argument, 0, 'q'},
86 {"verbose", no_argument, 0, 'v'},
87 {"version", no_argument, 0, 'V'},
88 {"help", no_argument, 0, 'h'},
89 {NULL, 0, 0, 0}
90};
91
92static void help(const char *progname)
93{
94 fprintf(stderr,
95 "Usage:\n"
96 "\t%s [options] [-i] [-b] modulename\n"
97 "\t%s [options] -a [-i] [-b] modulename [modulename...]\n"
98 "\t%s [options] -r [-i] modulename\n"
99 "\t%s [options] -r -a [-i] modulename [modulename...]\n"
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200100 "\t%s [options] -c\n"
101 "\t%s [options] --dump-modversions filename\n"
102 "Management Options:\n"
Gustavo Sverzut Barbieriab70dce2011-12-19 13:02:15 -0200103 "\t-a, --all Consider every non-argument to\n"
104 "\t be a module name to be inserted\n"
105 "\t or removed (-r)\n"
106 "\t-r, --remove Remove modules instead of inserting\n"
107 "\t --remove-dependencies Also remove modules depending on it\n"
108 "\t-R, --resolve-alias Only lookup and print alias and exit\n"
109 "\t --first-time Fail if module already inserted or removed\n"
110 "\t-i, --ignore-install Ignore install commands\n"
111 "\t-i, --ignore-remove Ignore remove commands\n"
112 "\t-b, --use-blacklist Apply blacklist to resolved alias.\n"
113 "\t-f, --force Force module insertion or removal.\n"
114 "\t implies --force-modversions and\n"
115 "\t --force-vermagic\n"
116 "\t --force-modversion Ignore module's version\n"
117 "\t --force-vermagic Ignore module's version magic\n"
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200118 "\n"
119 "Query Options:\n"
Gustavo Sverzut Barbieriab70dce2011-12-19 13:02:15 -0200120 "\t-D, --show-depends Only print module dependencies and exit\n"
121 "\t-c, --showconfig Print out known configuration and exit\n"
122 "\t-c, --show-config Same as --showconfig\n"
123 "\t --show-modversions Dump module symbol version and exit\n"
124 "\t --dump-modversions Same as --show-modversions\n"
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200125 "\n"
126 "General Options:\n"
Gustavo Sverzut Barbieriab70dce2011-12-19 13:02:15 -0200127 "\t-n, --dry-run Do not execute operations, just print out\n"
128 "\t-n, --show Same as --dry-run\n"
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200129
Lucas De Marchi2c966932011-12-20 16:39:59 -0200130 "\t-C, --config=FILE Use FILE instead of default search paths\n"
Kay Sieversa308abe2011-12-20 17:58:05 +0100131 "\t-d, --dirname=DIR Use DIR as filesystem root for " ROOTPREFIX "/lib/modules\n"
Gustavo Sverzut Barbieriab70dce2011-12-19 13:02:15 -0200132 "\t-S, --set-version=VERSION Use VERSION instead of `uname -r`\n"
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200133
134 "\t-s, --syslog print to syslog, not stderr\n"
135 "\t-q, --quiet disable messages\n"
136 "\t-v, --verbose enables more messages\n"
137 "\t-V, --version show version\n"
138 "\t-h, --help show this help\n",
Lucas De Marchi3ef848b2012-01-26 16:01:34 -0200139 progname, progname, progname, progname, progname, progname);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200140}
141
142static inline void _show(const char *fmt, ...)
143{
144 va_list args;
145
Gustavo Sverzut Barbieri525fa072012-01-08 13:58:28 -0200146 if (!do_show && verbose <= DEFAULT_VERBOSE)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200147 return;
148
149 va_start(args, fmt);
150 vfprintf(stdout, fmt, args);
151 fflush(stdout);
152 va_end(args);
153}
154
155static inline void _log(int prio, const char *fmt, ...)
156{
157 const char *prioname;
158 char buf[32], *msg;
159 va_list args;
160
161 if (prio > verbose)
162 return;
163
164 va_start(args, fmt);
165 if (vasprintf(&msg, fmt, args) < 0)
166 msg = NULL;
167 va_end(args);
168 if (msg == NULL)
169 return;
170
171 switch (prio) {
172 case LOG_CRIT:
173 prioname = "FATAL";
174 break;
175 case LOG_ERR:
176 prioname = "ERROR";
177 break;
178 case LOG_WARNING:
179 prioname = "WARNING";
180 break;
181 case LOG_NOTICE:
182 prioname = "NOTICE";
183 break;
184 case LOG_INFO:
185 prioname = "INFO";
186 break;
187 case LOG_DEBUG:
188 prioname = "DEBUG";
189 break;
190 default:
191 snprintf(buf, sizeof(buf), "LOG-%03d", prio);
192 prioname = buf;
193 }
194
195 if (use_syslog)
196 syslog(LOG_NOTICE, "%s: %s", prioname, msg);
197 else
198 fprintf(stderr, "%s: %s", prioname, msg);
199 free(msg);
200
201 if (prio <= LOG_CRIT)
202 exit(EXIT_FAILURE);
203}
204#define ERR(...) _log(LOG_ERR, __VA_ARGS__)
205#define WRN(...) _log(LOG_WARNING, __VA_ARGS__)
206#define INF(...) _log(LOG_INFO, __VA_ARGS__)
207#define DBG(...) _log(LOG_DEBUG, __VA_ARGS__)
208#define LOG(...) _log(log_priority, __VA_ARGS__)
209#define SHOW(...) _show(__VA_ARGS__)
210
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200211static int show_config(struct kmod_ctx *ctx)
212{
Lucas De Marchibc434962012-01-13 02:35:34 -0200213 struct config_iterators {
214 const char *name;
215 struct kmod_config_iter *(*get_iter)(const struct kmod_ctx *ctx);
216 } ci[] = {
217 { "blacklist", kmod_config_get_blacklists },
218 { "install", kmod_config_get_install_commands },
219 { "remove", kmod_config_get_remove_commands },
220 { "alias", kmod_config_get_aliases },
Elan Ruusamäe02629fa2012-03-22 12:00:16 -0300221 { "options", kmod_config_get_options },
Lucas De Marchibc434962012-01-13 02:35:34 -0200222 { "softdep", kmod_config_get_softdeps },
223 };
224 size_t i;
225
226 for (i = 0; i < ARRAY_SIZE(ci); i++) {
227 struct kmod_config_iter *iter = ci[i].get_iter(ctx);
228
229 if (iter == NULL)
230 continue;
231
232 while (kmod_config_iter_next(iter)) {
233 const char *val;
234
235 printf("%s %s", ci[i].name,
236 kmod_config_iter_get_key(iter));
237 val = kmod_config_iter_get_value(iter);
238 if (val != NULL) {
239 putchar(' ');
240 puts(val);
241 } else
242 putchar('\n');
243 }
244
245 kmod_config_iter_free_iter(iter);
246 }
247
Lucas De Marchi28f32c62012-01-27 23:56:46 -0200248 puts("\n# End of configuration files. Dumping indexes now:\n");
Lucas De Marchi09e9ae52012-01-17 10:05:02 -0200249 fflush(stdout);
250
Lucas De Marchi49a16372012-01-16 16:00:35 -0200251 kmod_dump_index(ctx, KMOD_INDEX_MODULES_ALIAS, STDOUT_FILENO);
252 kmod_dump_index(ctx, KMOD_INDEX_MODULES_SYMBOL, STDOUT_FILENO);
253
Lucas De Marchibc434962012-01-13 02:35:34 -0200254 return 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200255}
256
257static int show_modversions(struct kmod_ctx *ctx, const char *filename)
258{
Gustavo Sverzut Barbieri0e3e2f42011-12-19 12:45:22 -0200259 struct kmod_list *l, *list = NULL;
260 struct kmod_module *mod;
261 int err = kmod_module_new_from_path(ctx, filename, &mod);
262 if (err < 0) {
263 LOG("Module %s not found.\n", filename);
264 return err;
265 }
266
267 err = kmod_module_get_versions(mod, &list);
268 if (err < 0) {
Dave Reisner63698372012-01-04 10:41:50 -0500269 LOG("could not get modversions of %s: %s\n",
Gustavo Sverzut Barbieri0e3e2f42011-12-19 12:45:22 -0200270 filename, strerror(-err));
271 kmod_module_unref(mod);
272 return err;
273 }
274
275 kmod_list_foreach(l, list) {
276 const char *symbol = kmod_module_version_get_symbol(l);
277 uint64_t crc = kmod_module_version_get_crc(l);
278 printf("0x%08"PRIx64"\t%s\n", crc, symbol);
279 }
280 kmod_module_versions_free_list(list);
281 kmod_module_unref(mod);
282 return 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200283}
284
Lucas De Marchi8f192212012-01-11 15:38:50 -0200285static int command_do(struct kmod_module *module, const char *type,
286 const char *command, const char *cmdline_opts)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200287{
288 const char *modname = kmod_module_get_name(module);
289 char *p, *cmd = NULL;
290 size_t cmdlen, cmdline_opts_len, varlen;
291 int ret = 0;
292
293 if (cmdline_opts == NULL)
294 cmdline_opts = "";
295 cmdline_opts_len = strlen(cmdline_opts);
296
297 cmd = strdup(command);
298 if (cmd == NULL)
299 return -ENOMEM;
300 cmdlen = strlen(cmd);
301 varlen = sizeof("$CMDLINE_OPTS") - 1;
302 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
303 size_t prefixlen = p - cmd;
304 size_t suffixlen = cmdlen - prefixlen - varlen;
305 size_t slen = cmdlen - varlen + cmdline_opts_len;
306 char *suffix = p + varlen;
307 char *s = malloc(slen + 1);
308 if (s == NULL) {
309 free(cmd);
310 return -ENOMEM;
311 }
312 memcpy(s, cmd, p - cmd);
313 memcpy(s + prefixlen, cmdline_opts, cmdline_opts_len);
314 memcpy(s + prefixlen + cmdline_opts_len, suffix, suffixlen);
315 s[slen] = '\0';
316
317 free(cmd);
318 cmd = s;
319 cmdlen = slen;
320 }
321
322 SHOW("%s %s\n", type, cmd);
323 if (dry_run)
324 goto end;
325
326 setenv("MODPROBE_MODULE", modname, 1);
327 ret = system(cmd);
328 unsetenv("MODPROBE_MODULE");
329 if (ret == -1 || WEXITSTATUS(ret)) {
330 LOG("Error running %s command for %s\n", type, modname);
331 if (ret != -1)
332 ret = -WEXITSTATUS(ret);
333 }
334
335end:
336 free(cmd);
337 return ret;
338}
339
Lucas De Marchia872bba2012-01-12 15:23:51 -0200340static int rmmod_do_remove_module(struct kmod_module *mod)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200341{
Lucas De Marchia872bba2012-01-12 15:23:51 -0200342 const char *modname = kmod_module_get_name(mod);
Dave Reisner69a19742012-01-30 17:16:50 -0500343 struct kmod_list *deps, *itr;
Lucas De Marchia872bba2012-01-12 15:23:51 -0200344 int flags = 0, err;
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200345
Lucas De Marchia872bba2012-01-12 15:23:51 -0200346 SHOW("rmmod %s\n", kmod_module_get_name(mod));
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200347
Lucas De Marchia872bba2012-01-12 15:23:51 -0200348 if (dry_run)
349 return 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200350
Lucas De Marchia872bba2012-01-12 15:23:51 -0200351 if (force)
352 flags |= KMOD_REMOVE_FORCE;
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200353
Lucas De Marchia872bba2012-01-12 15:23:51 -0200354 err = kmod_module_remove_module(mod, flags);
355 if (err == -EEXIST) {
356 if (!first_time)
357 err = 0;
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200358 else
Lucas De Marchia872bba2012-01-12 15:23:51 -0200359 LOG("Module %s is not in kernel.\n", modname);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200360 }
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200361
Dave Reisner69a19742012-01-30 17:16:50 -0500362 deps = kmod_module_get_dependencies(mod);
363 if (deps != NULL) {
364 kmod_list_foreach(itr, deps) {
365 struct kmod_module *dep = kmod_module_get_module(itr);
366 if (kmod_module_get_refcnt(dep) == 0)
367 rmmod_do_remove_module(dep);
368 kmod_module_unref(dep);
369 }
370 kmod_module_unref_list(deps);
371 }
372
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200373 return err;
374}
375
Lucas De Marchia872bba2012-01-12 15:23:51 -0200376static int rmmod_do_module(struct kmod_module *mod, bool do_dependencies);
377
378static int rmmod_do_deps_list(struct kmod_list *list, bool stop_on_errors)
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200379{
Lucas De Marchia872bba2012-01-12 15:23:51 -0200380 struct kmod_list *l;
381
382 kmod_list_foreach_reverse(l, list) {
383 struct kmod_module *m = kmod_module_get_module(l);
384 int r = rmmod_do_module(m, false);
385 kmod_module_unref(m);
386
387 if (r < 0 && stop_on_errors)
388 return r;
389 }
390
391 return 0;
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200392}
393
Lucas De Marchia872bba2012-01-12 15:23:51 -0200394static int rmmod_do_module(struct kmod_module *mod, bool do_dependencies)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200395{
396 const char *modname = kmod_module_get_name(mod);
Lucas De Marchia872bba2012-01-12 15:23:51 -0200397 struct kmod_list *pre = NULL, *post = NULL;
398 const char *cmd = NULL;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200399 int err;
400
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200401 if (!ignore_commands) {
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200402 err = kmod_module_get_softdeps(mod, &pre, &post);
403 if (err < 0) {
404 WRN("could not get softdeps of '%s': %s\n",
Lucas De Marchi8f192212012-01-11 15:38:50 -0200405 modname, strerror(-err));
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200406 return err;
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200407 }
408
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200409 cmd = kmod_module_get_remove_commands(mod);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200410 }
411
Lucas De Marchia872bba2012-01-12 15:23:51 -0200412 if (cmd == NULL && !ignore_loaded) {
Lucas De Marchi9bf60d22011-12-19 02:18:14 -0200413 int state = kmod_module_get_initstate(mod);
414
Lucas De Marchie5e2a682011-12-19 02:26:34 -0200415 if (state < 0) {
Dave Reisnerf758caf2012-03-14 22:15:21 -0400416 if (first_time) {
417 LOG("Module %s is not in kernel.\n", modname);
418 err = -ENOENT;
419 } else {
420 err = 0;
421 }
Lucas De Marchie4e1e642012-01-12 15:36:54 -0200422 goto error;
Lucas De Marchie5e2a682011-12-19 02:26:34 -0200423 } else if (state == KMOD_MODULE_BUILTIN) {
Lucas De Marchi9bf60d22011-12-19 02:18:14 -0200424 LOG("Module %s is builtin.\n", modname);
Lucas De Marchie4e1e642012-01-12 15:36:54 -0200425 err = -ENOENT;
426 goto error;
Lucas De Marchi9bf60d22011-12-19 02:18:14 -0200427 }
428 }
429
Lucas De Marchia872bba2012-01-12 15:23:51 -0200430 rmmod_do_deps_list(post, false);
431
432 if (do_dependencies && remove_dependencies) {
433 struct kmod_list *deps = kmod_module_get_dependencies(mod);
434
435 err = rmmod_do_deps_list(deps, true);
Lucas De Marchi9bf60d22011-12-19 02:18:14 -0200436 if (err < 0)
Lucas De Marchia872bba2012-01-12 15:23:51 -0200437 goto error;
Lucas De Marchi9bf60d22011-12-19 02:18:14 -0200438 }
439
440 if (!ignore_loaded) {
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200441 int usage = kmod_module_get_refcnt(mod);
Lucas De Marchid8a6c0c2012-01-01 06:07:46 -0200442
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200443 if (usage > 0) {
Lucas De Marchid8a6c0c2012-01-01 06:07:46 -0200444 if (!quiet_inuse)
445 LOG("Module %s is in use.\n", modname);
446
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200447 err = -EBUSY;
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200448 goto error;
449 }
450 }
451
Lucas De Marchia872bba2012-01-12 15:23:51 -0200452 if (cmd == NULL)
453 err = rmmod_do_remove_module(mod);
454 else
455 err = command_do(mod, "remove", cmd, NULL);
Lucas De Marchid8a6c0c2012-01-01 06:07:46 -0200456
Lucas De Marchia872bba2012-01-12 15:23:51 -0200457 if (err < 0)
458 goto error;
Lucas De Marchid8a6c0c2012-01-01 06:07:46 -0200459
Lucas De Marchia872bba2012-01-12 15:23:51 -0200460 rmmod_do_deps_list(pre, false);
Dave Reisner0e9bd2d2011-12-31 18:02:45 -0500461
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200462error:
463 kmod_module_unref_list(pre);
464 kmod_module_unref_list(post);
Lucas De Marchia872bba2012-01-12 15:23:51 -0200465
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200466 return err;
467}
468
Lucas De Marchi569f1602012-01-21 02:45:06 -0200469static int rmmod(struct kmod_ctx *ctx, const char *alias)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200470{
471 struct kmod_list *l, *list = NULL;
472 int err;
473
474 err = kmod_module_new_from_lookup(ctx, alias, &list);
Lucas De Marchie5e2a682011-12-19 02:26:34 -0200475 if (err < 0)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200476 return err;
Lucas De Marchie5e2a682011-12-19 02:26:34 -0200477
Dave Reisnerf758caf2012-03-14 22:15:21 -0400478 if (list == NULL) {
Lucas De Marchie5e2a682011-12-19 02:26:34 -0200479 LOG("Module %s not found.\n", alias);
Dave Reisnerf758caf2012-03-14 22:15:21 -0400480 err = -ENOENT;
481 }
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200482
483 kmod_list_foreach(l, list) {
484 struct kmod_module *mod = kmod_module_get_module(l);
Lucas De Marchia872bba2012-01-12 15:23:51 -0200485 err = rmmod_do_module(mod, true);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200486 kmod_module_unref(mod);
487 if (err < 0)
488 break;
489 }
490
491 kmod_module_unref_list(list);
492 return err;
493}
494
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200495static int rmmod_all(struct kmod_ctx *ctx, char **args, int nargs)
496{
497 int i, err = 0;
498
499 for (i = 0; i < nargs; i++) {
500 int r = rmmod(ctx, args[i]);
501 if (r < 0)
502 err = r;
503 }
504
505 return err;
506}
507
Lucas De Marchi92122612012-01-11 21:48:08 -0200508static int handle_failed_lookup(struct kmod_ctx *ctx, const char *alias)
509{
510 struct kmod_module *mod;
511 int state, err;
512
513 DBG("lookup failed - trying to check if it's builtin\n");
514
515 err = kmod_module_new_from_name(ctx, alias, &mod);
516 if (err < 0)
517 return err;
518
519 state = kmod_module_get_initstate(mod);
520 kmod_module_unref(mod);
521
522 if (state != KMOD_MODULE_BUILTIN) {
523 LOG("Module %s not found.\n", alias);
524 return -ENOENT;
525 }
526
527 if (first_time) {
528 LOG("Module %s already in kernel (builtin).\n", alias);
529 return -ENOENT;
530 }
531
532 SHOW("builtin %s\n", alias);
533 return 0;
534}
535
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200536static void print_action(struct kmod_module *m, bool install,
537 const char *options)
538{
Lucas De Marchi4744ebc2012-03-15 00:14:35 -0300539 const char *path;
540
541 if (install) {
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200542 printf("install %s %s\n", kmod_module_get_install_commands(m),
543 options);
Lucas De Marchi4744ebc2012-03-15 00:14:35 -0300544 return;
545 }
546
547 path = kmod_module_get_path(m);
548
549 if (path == NULL) {
550 assert(kmod_module_get_initstate(m) == KMOD_MODULE_BUILTIN);
551 printf("builtin %s\n", kmod_module_get_name(m));
552 } else
553 printf("insmod %s %s\n", kmod_module_get_path(m), options);
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200554}
555
Lucas De Marchi569f1602012-01-21 02:45:06 -0200556static int insmod(struct kmod_ctx *ctx, const char *alias,
Lucas De Marchi8f192212012-01-11 15:38:50 -0200557 const char *extra_options)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200558{
559 struct kmod_list *l, *list = NULL;
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200560 int err, flags = 0;
561
562 void (*show)(struct kmod_module *m, bool install,
563 const char *options) = NULL;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200564
565 err = kmod_module_new_from_lookup(ctx, alias, &list);
Lucas De Marchie5e2a682011-12-19 02:26:34 -0200566 if (err < 0)
567 return err;
568
Lucas De Marchi92122612012-01-11 21:48:08 -0200569 if (list == NULL)
570 return handle_failed_lookup(ctx, alias);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200571
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200572 if (strip_modversion || force)
573 flags |= KMOD_PROBE_FORCE_MODVERSION;
574 if (strip_vermagic || force)
575 flags |= KMOD_PROBE_FORCE_VERMAGIC;
576 if (ignore_commands)
577 flags |= KMOD_PROBE_IGNORE_COMMAND;
578 if (ignore_loaded)
579 flags |= KMOD_PROBE_IGNORE_LOADED;
580 if (dry_run)
581 flags |= KMOD_PROBE_DRY_RUN;
582 if (do_show || verbose > DEFAULT_VERBOSE)
583 show = &print_action;
584
585
586 if (use_blacklist)
587 flags |= KMOD_PROBE_APPLY_BLACKLIST;
588 if (first_time)
Lucas De Marchi814a57b2012-02-06 12:46:39 -0200589 flags |= KMOD_PROBE_FAIL_ON_LOADED;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200590
591 kmod_list_foreach(l, list) {
592 struct kmod_module *mod = kmod_module_get_module(l);
Lucas De Marchi8b013762012-01-12 17:14:30 -0200593
Lucas De Marchi81229852011-12-16 02:58:48 -0200594 if (lookup_only)
595 printf("%s\n", kmod_module_get_name(mod));
Lucas De Marchi8b013762012-01-12 17:14:30 -0200596 else {
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200597 err = kmod_module_probe_insert_module(mod, flags,
598 extra_options, NULL, NULL, show);
Lucas De Marchi8b013762012-01-12 17:14:30 -0200599 }
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200600
Dave Reisner297a3182012-01-30 20:57:36 -0500601 if (err >= 0)
602 /* ignore flag return values such as a mod being blacklisted */
603 err = 0;
604 else {
605 switch (err) {
606 case -EEXIST:
607 ERR("could not insert '%s': Module already in kernel\n",
608 kmod_module_get_name(mod));
609 break;
610 default:
611 ERR("could not insert '%s': %s\n",
612 kmod_module_get_name(mod),
613 strerror(-err));
614 break;
615 }
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200616 }
617
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200618 kmod_module_unref(mod);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200619 }
620
621 kmod_module_unref_list(list);
622 return err;
623}
624
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200625static int insmod_all(struct kmod_ctx *ctx, char **args, int nargs)
626{
627 int i, err = 0;
628
629 for (i = 0; i < nargs; i++) {
630 int r = insmod(ctx, args[i], NULL);
631 if (r < 0)
632 err = r;
633 }
634
635 return err;
636}
637
638static void env_modprobe_options_append(const char *value)
639{
640 const char *old = getenv("MODPROBE_OPTIONS");
641 char *env;
642
643 if (old == NULL) {
644 setenv("MODPROBE_OPTIONS", value, 1);
645 return;
646 }
647
648 if (asprintf(&env, "%s %s", old, value) < 0) {
649 ERR("could not append value to $MODPROBE_OPTIONS\n");
650 return;
651 }
652
653 if (setenv("MODPROBE_OPTIONS", env, 1) < 0)
654 ERR("could not setenv(MODPROBE_OPTIONS, \"%s\")\n", env);
655 free(env);
656}
657
658static int options_from_array(char **args, int nargs, char **output)
659{
660 char *opts = NULL;
661 size_t optslen = 0;
662 int i, err = 0;
663
664 for (i = 1; i < nargs; i++) {
665 size_t len = strlen(args[i]);
666 size_t qlen = 0;
667 const char *value;
668 void *tmp;
669
670 value = strchr(args[i], '=');
671 if (value) {
672 value++;
673 if (*value != '"' && *value != '\'') {
674 if (strchr(value, ' '))
675 qlen = 2;
676 }
677 }
678
679 tmp = realloc(opts, optslen + len + qlen + 2);
680 if (!tmp) {
681 err = -errno;
682 free(opts);
683 opts = NULL;
684 ERR("could not gather module options: out-of-memory\n");
685 break;
686 }
687 opts = tmp;
688 if (optslen > 0) {
689 opts[optslen] = ' ';
690 optslen++;
691 }
692 if (qlen == 0) {
693 memcpy(opts + optslen, args[i], len + 1);
694 optslen += len;
695 } else {
696 size_t keylen = value - args[i];
697 size_t valuelen = len - keylen;
698 memcpy(opts + optslen, args[i], keylen);
699 optslen += keylen;
700 opts[optslen] = '"';
701 optslen++;
702 memcpy(opts + optslen, value, valuelen);
703 optslen += valuelen;
704 opts[optslen] = '"';
705 optslen++;
706 opts[optslen] = '\0';
707 }
708 }
709
710 *output = opts;
711 return err;
712}
713
714static char **prepend_options_from_env(int *p_argc, char **orig_argv)
715{
716 const char *p, *env = getenv("MODPROBE_OPTIONS");
717 char **new_argv, *str_start, *str_end, *str, *s, *quote;
718 int i, argc = *p_argc;
719 size_t envlen, space_count = 0;
720
721 if (env == NULL)
722 return orig_argv;
723
724 for (p = env; *p != '\0'; p++) {
725 if (*p == ' ')
726 space_count++;
727 }
728
729 envlen = p - env;
730 new_argv = malloc(sizeof(char *) * (argc + space_count + 3 + envlen));
731 if (new_argv == NULL)
732 return NULL;
733
734 new_argv[0] = orig_argv[0];
735 str_start = str = (char *) (new_argv + argc + space_count + 3);
736 memcpy(str, env, envlen + 1);
737
738 str_end = str_start + envlen;
739
740 quote = NULL;
741 for (i = 1, s = str; *s != '\0'; s++) {
742 if (quote == NULL) {
743 if (*s == ' ') {
744 new_argv[i] = str;
745 i++;
746 *s = '\0';
747 str = s + 1;
748 } else if (*s == '"' || *s == '\'')
749 quote = s;
750 } else {
751 if (*s == *quote) {
752 if (quote == str) {
753 new_argv[i] = str + 1;
754 i++;
755 *s = '\0';
756 str = s + 1;
757 } else {
758 char *it;
759 for (it = quote; it < s - 1; it++)
760 it[0] = it[1];
761 for (it = s - 1; it < str_end - 2; it++)
762 it[0] = it[2];
763 str_end -= 2;
764 *str_end = '\0';
765 s -= 2;
766 }
767 quote = NULL;
768 }
769 }
770 }
771 if (str < s) {
772 new_argv[i] = str;
773 i++;
774 }
775
776 memcpy(new_argv + i, orig_argv + 1, sizeof(char *) * (argc - 1));
777 new_argv[i + argc] = NULL;
778 *p_argc = i + argc - 1;
779
780 return new_argv;
781}
782
783static void log_syslog(void *data, int priority, const char *file, int line,
Lucas De Marchi8f192212012-01-11 15:38:50 -0200784 const char *fn, const char *format, va_list args)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200785{
786 char *str, buf[32];
787 const char *prioname;
788
789 switch (priority) {
790 case LOG_CRIT:
791 prioname = "FATAL";
792 break;
793 case LOG_ERR:
794 prioname = "ERROR";
795 break;
796 case LOG_WARNING:
797 prioname = "WARNING";
798 break;
799 case LOG_NOTICE:
800 prioname = "NOTICE";
801 break;
802 case LOG_INFO:
803 prioname = "INFO";
804 break;
805 case LOG_DEBUG:
806 prioname = "DEBUG";
807 break;
808 default:
809 snprintf(buf, sizeof(buf), "LOG-%03d", priority);
810 prioname = buf;
811 }
812
813 if (vasprintf(&str, format, args) < 0)
814 return;
815#ifdef ENABLE_DEBUG
816 syslog(LOG_NOTICE, "%s: %s:%d %s() %s", prioname, file, line, fn, str);
817#else
818 syslog(LOG_NOTICE, "%s: %s", prioname, str);
819#endif
820 free(str);
821 (void)data;
822}
823
Lucas De Marchifa29c0e2011-12-22 03:54:46 -0200824static int do_modprobe(int argc, char **orig_argv)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200825{
826 struct kmod_ctx *ctx;
827 char **args = NULL, **argv;
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200828 const char **config_paths = NULL;
829 int nargs = 0, n_config_paths = 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200830 char dirname_buf[PATH_MAX];
831 const char *dirname = NULL;
832 const char *root = NULL;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200833 const char *kversion = NULL;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200834 int use_all = 0;
835 int do_remove = 0;
836 int do_show_config = 0;
837 int do_show_modversions = 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200838 int err;
839
840 argv = prepend_options_from_env(&argc, orig_argv);
841 if (argv == NULL) {
842 fputs("Error: could not prepend options from command line\n",
843 stderr);
844 return EXIT_FAILURE;
845 }
846
847 for (;;) {
848 int c, idx = 0;
849 c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
850 if (c == -1)
851 break;
852 switch (c) {
853 case 'a':
854 log_priority = LOG_WARNING;
855 use_all = 1;
856 break;
857 case 'r':
858 do_remove = 1;
859 break;
860 case 5:
861 remove_dependencies = 1;
862 break;
863 case 'R':
Lucas De Marchi81229852011-12-16 02:58:48 -0200864 lookup_only = 1;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200865 break;
866 case 3:
867 first_time = 1;
868 break;
869 case 'i':
870 ignore_commands = 1;
871 break;
872 case 'b':
873 use_blacklist = 1;
874 break;
875 case 'f':
876 force = 1;
877 break;
878 case 2:
879 strip_modversion = 1;
880 break;
881 case 1:
882 strip_vermagic = 1;
883 break;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200884 case 'D':
885 ignore_loaded = 1;
886 dry_run = 1;
Gustavo Sverzut Barbieri525fa072012-01-08 13:58:28 -0200887 do_show = 1;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200888 break;
889 case 'c':
890 do_show_config = 1;
891 break;
892 case 4:
893 do_show_modversions = 1;
894 break;
895 case 'n':
896 dry_run = 1;
897 break;
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200898 case 'C': {
899 size_t bytes = sizeof(char *) * (n_config_paths + 2);
900 void *tmp = realloc(config_paths, bytes);
901 if (!tmp) {
902 fputs("Error: out-of-memory\n", stderr);
903 goto cmdline_failed;
904 }
905 config_paths = tmp;
906 config_paths[n_config_paths] = optarg;
907 n_config_paths++;
908 config_paths[n_config_paths] = NULL;
909
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200910 env_modprobe_options_append("-C");
911 env_modprobe_options_append(optarg);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200912 break;
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200913 }
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200914 case 'd':
915 root = optarg;
916 break;
917 case 'S':
918 kversion = optarg;
919 break;
920 case 's':
921 env_modprobe_options_append("-s");
922 use_syslog = 1;
923 break;
924 case 'q':
925 env_modprobe_options_append("-q");
Lucas De Marchiae7ebe82012-03-15 01:11:10 -0300926 verbose = LOG_EMERG;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200927 break;
928 case 'v':
929 env_modprobe_options_append("-v");
930 verbose++;
931 break;
932 case 'V':
933 puts(PACKAGE " version " VERSION);
934 if (argv != orig_argv)
935 free(argv);
Gustavo Sverzut Barbieri5f9f58f2011-12-21 23:54:35 -0200936 free(config_paths);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200937 return EXIT_SUCCESS;
938 case 'h':
Lucas De Marchi3e8de632011-12-23 01:36:10 -0200939 help(basename(argv[0]));
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200940 if (argv != orig_argv)
941 free(argv);
Gustavo Sverzut Barbieri5f9f58f2011-12-21 23:54:35 -0200942 free(config_paths);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200943 return EXIT_SUCCESS;
944 case '?':
945 goto cmdline_failed;
946 default:
Lucas De Marchi8f192212012-01-11 15:38:50 -0200947 fprintf(stderr, "Error: unexpected getopt_long() value '%c'.\n",
948 c);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200949 goto cmdline_failed;
950 }
951 }
952
953 args = argv + optind;
954 nargs = argc - optind;
955
Dave Reisnerb09668c2011-12-31 16:51:40 -0500956 if (!do_show_config) {
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200957 if (nargs == 0) {
958 fputs("Error: missing parameters. See -h.\n", stderr);
959 goto cmdline_failed;
960 }
961 }
962
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200963 if (root != NULL || kversion != NULL) {
964 struct utsname u;
965 if (root == NULL)
966 root = "";
967 if (kversion == NULL) {
968 if (uname(&u) < 0) {
969 fprintf(stderr, "Error: uname() failed: %s\n",
970 strerror(errno));
971 goto cmdline_failed;
972 }
973 kversion = u.release;
974 }
Lucas De Marchi8f192212012-01-11 15:38:50 -0200975 snprintf(dirname_buf, sizeof(dirname_buf),
976 "%s" ROOTPREFIX "/lib/modules/%s", root,
977 kversion);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200978 dirname = dirname_buf;
979 }
980
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200981 ctx = kmod_new(dirname, config_paths);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200982 if (!ctx) {
983 fputs("Error: kmod_new() failed!\n", stderr);
984 goto cmdline_failed;
985 }
986 kmod_load_resources(ctx);
987
988 kmod_set_log_priority(ctx, verbose);
989 if (use_syslog) {
990 openlog("modprobe", LOG_CONS, LOG_DAEMON);
991 kmod_set_log_fn(ctx, log_syslog, NULL);
992 }
993
Dave Reisnerb09668c2011-12-31 16:51:40 -0500994 if (do_show_config)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200995 err = show_config(ctx);
996 else if (do_show_modversions)
997 err = show_modversions(ctx, args[0]);
998 else if (do_remove)
Lucas De Marchic1b84542012-03-15 00:27:18 -0300999 err = rmmod_all(ctx, args, nargs);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -02001000 else if (use_all)
1001 err = insmod_all(ctx, args, nargs);
1002 else {
1003 char *opts;
1004 err = options_from_array(args, nargs, &opts);
1005 if (err == 0) {
1006 err = insmod(ctx, args[0], opts);
1007 free(opts);
1008 }
1009 }
1010
1011 kmod_unref(ctx);
1012
1013 if (use_syslog)
1014 closelog();
1015
1016 if (argv != orig_argv)
1017 free(argv);
Lucas De Marchi8f192212012-01-11 15:38:50 -02001018
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -02001019 free(config_paths);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -02001020 return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
1021
1022cmdline_failed:
1023 if (argv != orig_argv)
1024 free(argv);
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -02001025 free(config_paths);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -02001026 return EXIT_FAILURE;
1027}
Lucas De Marchifa29c0e2011-12-22 03:54:46 -02001028
1029#ifndef KMOD_BUNDLE_TOOL
1030int main(int argc, char *argv[])
1031{
1032 return do_modprobe(argc, argv);
1033}
1034
1035#else
1036#include "kmod.h"
1037
1038const struct kmod_cmd kmod_cmd_compat_modprobe = {
1039 .name = "modprobe",
1040 .cmd = do_modprobe,
1041 .help = "compat modprobe command",
1042};
1043
1044#endif