blob: 94500cf602195b536f4f766e8cf4e6faa404c006 [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
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020020#include <stdio.h>
21#include <stdlib.h>
Lucas De Marchi0cf28322012-01-12 02:21:26 -020022#include <stdbool.h>
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020023#include <getopt.h>
24#include <errno.h>
25#include <string.h>
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <sys/utsname.h>
Thierry Vignaudeff917c2012-01-17 17:32:48 -020029#include <sys/wait.h>
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020030#include <unistd.h>
31#include <syslog.h>
32#include <limits.h>
33
34#include "libkmod.h"
Lucas De Marchi8b013762012-01-12 17:14:30 -020035#include "libkmod-array.h"
Lucas De Marchibc434962012-01-13 02:35:34 -020036#include "macro.h"
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020037
38static int log_priority = LOG_CRIT;
39static int use_syslog = 0;
40
41#define DEFAULT_VERBOSE LOG_WARNING
42static int verbose = DEFAULT_VERBOSE;
Gustavo Sverzut Barbieri525fa072012-01-08 13:58:28 -020043static int do_show = 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020044static int dry_run = 0;
45static int ignore_loaded = 0;
Lucas De Marchi81229852011-12-16 02:58:48 -020046static int lookup_only = 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020047static int first_time = 0;
48static int ignore_commands = 0;
49static int use_blacklist = 0;
50static int force = 0;
51static int strip_modversion = 0;
52static int strip_vermagic = 0;
53static int remove_dependencies = 0;
Lucas De Marchid8a6c0c2012-01-01 06:07:46 -020054static int quiet_inuse = 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020055
Dave Reisnercc988302012-01-26 11:36:35 -050056static const char cmdopts_s[] = "arRibfDcnC:d:S:sqvVh";
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020057static const struct option cmdopts[] = {
58 {"all", no_argument, 0, 'a'},
59 {"remove", no_argument, 0, 'r'},
60 {"remove-dependencies", no_argument, 0, 5},
61 {"resolve-alias", no_argument, 0, 'R'},
62 {"first-time", no_argument, 0, 3},
63 {"ignore-install", no_argument, 0, 'i'},
64 {"ignore-remove", no_argument, 0, 'i'},
65 {"use-blacklist", no_argument, 0, 'b'},
66 {"force", no_argument, 0, 'f'},
67 {"force-modversion", no_argument, 0, 2},
68 {"force-vermagic", no_argument, 0, 1},
69
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020070 {"show-depends", no_argument, 0, 'D'},
71 {"showconfig", no_argument, 0, 'c'},
72 {"show-config", no_argument, 0, 'c'},
73 {"show-modversions", no_argument, 0, 4},
74 {"dump-modversions", no_argument, 0, 4},
75
76 {"dry-run", no_argument, 0, 'n'},
77 {"show", no_argument, 0, 'n'},
78
79 {"config", required_argument, 0, 'C'},
80 {"dirname", required_argument, 0, 'd'},
81 {"set-version", required_argument, 0, 'S'},
82
83 {"syslog", no_argument, 0, 's'},
84 {"quiet", no_argument, 0, 'q'},
85 {"verbose", no_argument, 0, 'v'},
86 {"version", no_argument, 0, 'V'},
87 {"help", no_argument, 0, 'h'},
88 {NULL, 0, 0, 0}
89};
90
91static void help(const char *progname)
92{
93 fprintf(stderr,
94 "Usage:\n"
95 "\t%s [options] [-i] [-b] modulename\n"
96 "\t%s [options] -a [-i] [-b] modulename [modulename...]\n"
97 "\t%s [options] -r [-i] modulename\n"
98 "\t%s [options] -r -a [-i] modulename [modulename...]\n"
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -020099 "\t%s [options] -c\n"
100 "\t%s [options] --dump-modversions filename\n"
101 "Management Options:\n"
Gustavo Sverzut Barbieriab70dce2011-12-19 13:02:15 -0200102 "\t-a, --all Consider every non-argument to\n"
103 "\t be a module name to be inserted\n"
104 "\t or removed (-r)\n"
105 "\t-r, --remove Remove modules instead of inserting\n"
106 "\t --remove-dependencies Also remove modules depending on it\n"
107 "\t-R, --resolve-alias Only lookup and print alias and exit\n"
108 "\t --first-time Fail if module already inserted or removed\n"
109 "\t-i, --ignore-install Ignore install commands\n"
110 "\t-i, --ignore-remove Ignore remove commands\n"
111 "\t-b, --use-blacklist Apply blacklist to resolved alias.\n"
112 "\t-f, --force Force module insertion or removal.\n"
113 "\t implies --force-modversions and\n"
114 "\t --force-vermagic\n"
115 "\t --force-modversion Ignore module's version\n"
116 "\t --force-vermagic Ignore module's version magic\n"
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200117 "\n"
118 "Query Options:\n"
Gustavo Sverzut Barbieriab70dce2011-12-19 13:02:15 -0200119 "\t-D, --show-depends Only print module dependencies and exit\n"
120 "\t-c, --showconfig Print out known configuration and exit\n"
121 "\t-c, --show-config Same as --showconfig\n"
122 "\t --show-modversions Dump module symbol version and exit\n"
123 "\t --dump-modversions Same as --show-modversions\n"
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200124 "\n"
125 "General Options:\n"
Gustavo Sverzut Barbieriab70dce2011-12-19 13:02:15 -0200126 "\t-n, --dry-run Do not execute operations, just print out\n"
127 "\t-n, --show Same as --dry-run\n"
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200128
Lucas De Marchi2c966932011-12-20 16:39:59 -0200129 "\t-C, --config=FILE Use FILE instead of default search paths\n"
Kay Sieversa308abe2011-12-20 17:58:05 +0100130 "\t-d, --dirname=DIR Use DIR as filesystem root for " ROOTPREFIX "/lib/modules\n"
Gustavo Sverzut Barbieriab70dce2011-12-19 13:02:15 -0200131 "\t-S, --set-version=VERSION Use VERSION instead of `uname -r`\n"
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200132
133 "\t-s, --syslog print to syslog, not stderr\n"
134 "\t-q, --quiet disable messages\n"
135 "\t-v, --verbose enables more messages\n"
136 "\t-V, --version show version\n"
137 "\t-h, --help show this help\n",
Lucas De Marchi3ef848b2012-01-26 16:01:34 -0200138 progname, progname, progname, progname, progname, progname);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200139}
140
141static inline void _show(const char *fmt, ...)
142{
143 va_list args;
144
Gustavo Sverzut Barbieri525fa072012-01-08 13:58:28 -0200145 if (!do_show && verbose <= DEFAULT_VERBOSE)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200146 return;
147
148 va_start(args, fmt);
149 vfprintf(stdout, fmt, args);
150 fflush(stdout);
151 va_end(args);
152}
153
154static inline void _log(int prio, const char *fmt, ...)
155{
156 const char *prioname;
157 char buf[32], *msg;
158 va_list args;
159
160 if (prio > verbose)
161 return;
162
163 va_start(args, fmt);
164 if (vasprintf(&msg, fmt, args) < 0)
165 msg = NULL;
166 va_end(args);
167 if (msg == NULL)
168 return;
169
170 switch (prio) {
171 case LOG_CRIT:
172 prioname = "FATAL";
173 break;
174 case LOG_ERR:
175 prioname = "ERROR";
176 break;
177 case LOG_WARNING:
178 prioname = "WARNING";
179 break;
180 case LOG_NOTICE:
181 prioname = "NOTICE";
182 break;
183 case LOG_INFO:
184 prioname = "INFO";
185 break;
186 case LOG_DEBUG:
187 prioname = "DEBUG";
188 break;
189 default:
190 snprintf(buf, sizeof(buf), "LOG-%03d", prio);
191 prioname = buf;
192 }
193
194 if (use_syslog)
195 syslog(LOG_NOTICE, "%s: %s", prioname, msg);
196 else
197 fprintf(stderr, "%s: %s", prioname, msg);
198 free(msg);
199
200 if (prio <= LOG_CRIT)
201 exit(EXIT_FAILURE);
202}
203#define ERR(...) _log(LOG_ERR, __VA_ARGS__)
204#define WRN(...) _log(LOG_WARNING, __VA_ARGS__)
205#define INF(...) _log(LOG_INFO, __VA_ARGS__)
206#define DBG(...) _log(LOG_DEBUG, __VA_ARGS__)
207#define LOG(...) _log(log_priority, __VA_ARGS__)
208#define SHOW(...) _show(__VA_ARGS__)
209
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200210static int show_config(struct kmod_ctx *ctx)
211{
Lucas De Marchibc434962012-01-13 02:35:34 -0200212 struct config_iterators {
213 const char *name;
214 struct kmod_config_iter *(*get_iter)(const struct kmod_ctx *ctx);
215 } ci[] = {
216 { "blacklist", kmod_config_get_blacklists },
217 { "install", kmod_config_get_install_commands },
218 { "remove", kmod_config_get_remove_commands },
219 { "alias", kmod_config_get_aliases },
220 { "option", kmod_config_get_options },
221 { "softdep", kmod_config_get_softdeps },
222 };
223 size_t i;
224
225 for (i = 0; i < ARRAY_SIZE(ci); i++) {
226 struct kmod_config_iter *iter = ci[i].get_iter(ctx);
227
228 if (iter == NULL)
229 continue;
230
231 while (kmod_config_iter_next(iter)) {
232 const char *val;
233
234 printf("%s %s", ci[i].name,
235 kmod_config_iter_get_key(iter));
236 val = kmod_config_iter_get_value(iter);
237 if (val != NULL) {
238 putchar(' ');
239 puts(val);
240 } else
241 putchar('\n');
242 }
243
244 kmod_config_iter_free_iter(iter);
245 }
246
Lucas De Marchi28f32c62012-01-27 23:56:46 -0200247 puts("\n# End of configuration files. Dumping indexes now:\n");
Lucas De Marchi09e9ae52012-01-17 10:05:02 -0200248 fflush(stdout);
249
Lucas De Marchi49a16372012-01-16 16:00:35 -0200250 kmod_dump_index(ctx, KMOD_INDEX_MODULES_ALIAS, STDOUT_FILENO);
251 kmod_dump_index(ctx, KMOD_INDEX_MODULES_SYMBOL, STDOUT_FILENO);
252
Lucas De Marchibc434962012-01-13 02:35:34 -0200253 return 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200254}
255
256static int show_modversions(struct kmod_ctx *ctx, const char *filename)
257{
Gustavo Sverzut Barbieri0e3e2f42011-12-19 12:45:22 -0200258 struct kmod_list *l, *list = NULL;
259 struct kmod_module *mod;
260 int err = kmod_module_new_from_path(ctx, filename, &mod);
261 if (err < 0) {
262 LOG("Module %s not found.\n", filename);
263 return err;
264 }
265
266 err = kmod_module_get_versions(mod, &list);
267 if (err < 0) {
Dave Reisner63698372012-01-04 10:41:50 -0500268 LOG("could not get modversions of %s: %s\n",
Gustavo Sverzut Barbieri0e3e2f42011-12-19 12:45:22 -0200269 filename, strerror(-err));
270 kmod_module_unref(mod);
271 return err;
272 }
273
274 kmod_list_foreach(l, list) {
275 const char *symbol = kmod_module_version_get_symbol(l);
276 uint64_t crc = kmod_module_version_get_crc(l);
277 printf("0x%08"PRIx64"\t%s\n", crc, symbol);
278 }
279 kmod_module_versions_free_list(list);
280 kmod_module_unref(mod);
281 return 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200282}
283
Lucas De Marchi8f192212012-01-11 15:38:50 -0200284static int command_do(struct kmod_module *module, const char *type,
285 const char *command, const char *cmdline_opts)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200286{
287 const char *modname = kmod_module_get_name(module);
288 char *p, *cmd = NULL;
289 size_t cmdlen, cmdline_opts_len, varlen;
290 int ret = 0;
291
292 if (cmdline_opts == NULL)
293 cmdline_opts = "";
294 cmdline_opts_len = strlen(cmdline_opts);
295
296 cmd = strdup(command);
297 if (cmd == NULL)
298 return -ENOMEM;
299 cmdlen = strlen(cmd);
300 varlen = sizeof("$CMDLINE_OPTS") - 1;
301 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
302 size_t prefixlen = p - cmd;
303 size_t suffixlen = cmdlen - prefixlen - varlen;
304 size_t slen = cmdlen - varlen + cmdline_opts_len;
305 char *suffix = p + varlen;
306 char *s = malloc(slen + 1);
307 if (s == NULL) {
308 free(cmd);
309 return -ENOMEM;
310 }
311 memcpy(s, cmd, p - cmd);
312 memcpy(s + prefixlen, cmdline_opts, cmdline_opts_len);
313 memcpy(s + prefixlen + cmdline_opts_len, suffix, suffixlen);
314 s[slen] = '\0';
315
316 free(cmd);
317 cmd = s;
318 cmdlen = slen;
319 }
320
321 SHOW("%s %s\n", type, cmd);
322 if (dry_run)
323 goto end;
324
325 setenv("MODPROBE_MODULE", modname, 1);
326 ret = system(cmd);
327 unsetenv("MODPROBE_MODULE");
328 if (ret == -1 || WEXITSTATUS(ret)) {
329 LOG("Error running %s command for %s\n", type, modname);
330 if (ret != -1)
331 ret = -WEXITSTATUS(ret);
332 }
333
334end:
335 free(cmd);
336 return ret;
337}
338
Lucas De Marchia872bba2012-01-12 15:23:51 -0200339static int rmmod_do_remove_module(struct kmod_module *mod)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200340{
Lucas De Marchia872bba2012-01-12 15:23:51 -0200341 const char *modname = kmod_module_get_name(mod);
Dave Reisner69a19742012-01-30 17:16:50 -0500342 struct kmod_list *deps, *itr;
Lucas De Marchia872bba2012-01-12 15:23:51 -0200343 int flags = 0, err;
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200344
Lucas De Marchia872bba2012-01-12 15:23:51 -0200345 SHOW("rmmod %s\n", kmod_module_get_name(mod));
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200346
Lucas De Marchia872bba2012-01-12 15:23:51 -0200347 if (dry_run)
348 return 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200349
Lucas De Marchia872bba2012-01-12 15:23:51 -0200350 if (force)
351 flags |= KMOD_REMOVE_FORCE;
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200352
Lucas De Marchia872bba2012-01-12 15:23:51 -0200353 err = kmod_module_remove_module(mod, flags);
354 if (err == -EEXIST) {
355 if (!first_time)
356 err = 0;
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200357 else
Lucas De Marchia872bba2012-01-12 15:23:51 -0200358 LOG("Module %s is not in kernel.\n", modname);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200359 }
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200360
Dave Reisner69a19742012-01-30 17:16:50 -0500361 deps = kmod_module_get_dependencies(mod);
362 if (deps != NULL) {
363 kmod_list_foreach(itr, deps) {
364 struct kmod_module *dep = kmod_module_get_module(itr);
365 if (kmod_module_get_refcnt(dep) == 0)
366 rmmod_do_remove_module(dep);
367 kmod_module_unref(dep);
368 }
369 kmod_module_unref_list(deps);
370 }
371
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200372 return err;
373}
374
Lucas De Marchia872bba2012-01-12 15:23:51 -0200375static int rmmod_do_module(struct kmod_module *mod, bool do_dependencies);
376
377static int rmmod_do_deps_list(struct kmod_list *list, bool stop_on_errors)
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200378{
Lucas De Marchia872bba2012-01-12 15:23:51 -0200379 struct kmod_list *l;
380
381 kmod_list_foreach_reverse(l, list) {
382 struct kmod_module *m = kmod_module_get_module(l);
383 int r = rmmod_do_module(m, false);
384 kmod_module_unref(m);
385
386 if (r < 0 && stop_on_errors)
387 return r;
388 }
389
390 return 0;
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200391}
392
Lucas De Marchia872bba2012-01-12 15:23:51 -0200393static int rmmod_do_module(struct kmod_module *mod, bool do_dependencies)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200394{
395 const char *modname = kmod_module_get_name(mod);
Lucas De Marchia872bba2012-01-12 15:23:51 -0200396 struct kmod_list *pre = NULL, *post = NULL;
397 const char *cmd = NULL;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200398 int err;
399
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200400 if (!ignore_commands) {
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200401 err = kmod_module_get_softdeps(mod, &pre, &post);
402 if (err < 0) {
403 WRN("could not get softdeps of '%s': %s\n",
Lucas De Marchi8f192212012-01-11 15:38:50 -0200404 modname, strerror(-err));
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200405 return err;
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200406 }
407
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200408 cmd = kmod_module_get_remove_commands(mod);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200409 }
410
Lucas De Marchia872bba2012-01-12 15:23:51 -0200411 if (cmd == NULL && !ignore_loaded) {
Lucas De Marchi9bf60d22011-12-19 02:18:14 -0200412 int state = kmod_module_get_initstate(mod);
413
Lucas De Marchie5e2a682011-12-19 02:26:34 -0200414 if (state < 0) {
415 LOG ("Module %s not found.\n", modname);
Lucas De Marchie4e1e642012-01-12 15:36:54 -0200416 err = -ENOENT;
417 goto error;
Lucas De Marchie5e2a682011-12-19 02:26:34 -0200418 } else if (state == KMOD_MODULE_BUILTIN) {
Lucas De Marchi9bf60d22011-12-19 02:18:14 -0200419 LOG("Module %s is builtin.\n", modname);
Lucas De Marchie4e1e642012-01-12 15:36:54 -0200420 err = -ENOENT;
421 goto error;
Lucas De Marchi9bf60d22011-12-19 02:18:14 -0200422 } else if (state != KMOD_MODULE_LIVE) {
423 if (first_time) {
424 LOG("Module %s is not in kernel.\n", modname);
Lucas De Marchie4e1e642012-01-12 15:36:54 -0200425 err = -ENOENT;
426 goto error;
427 } else {
428 err = 0;
429 goto error;
430 }
Lucas De Marchi9bf60d22011-12-19 02:18:14 -0200431 }
432 }
433
Lucas De Marchia872bba2012-01-12 15:23:51 -0200434 rmmod_do_deps_list(post, false);
435
436 if (do_dependencies && remove_dependencies) {
437 struct kmod_list *deps = kmod_module_get_dependencies(mod);
438
439 err = rmmod_do_deps_list(deps, true);
Lucas De Marchi9bf60d22011-12-19 02:18:14 -0200440 if (err < 0)
Lucas De Marchia872bba2012-01-12 15:23:51 -0200441 goto error;
Lucas De Marchi9bf60d22011-12-19 02:18:14 -0200442 }
443
444 if (!ignore_loaded) {
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200445 int usage = kmod_module_get_refcnt(mod);
Lucas De Marchid8a6c0c2012-01-01 06:07:46 -0200446
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200447 if (usage > 0) {
Lucas De Marchid8a6c0c2012-01-01 06:07:46 -0200448 if (!quiet_inuse)
449 LOG("Module %s is in use.\n", modname);
450
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200451 err = -EBUSY;
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200452 goto error;
453 }
454 }
455
Lucas De Marchia872bba2012-01-12 15:23:51 -0200456 if (cmd == NULL)
457 err = rmmod_do_remove_module(mod);
458 else
459 err = command_do(mod, "remove", cmd, NULL);
Lucas De Marchid8a6c0c2012-01-01 06:07:46 -0200460
Lucas De Marchia872bba2012-01-12 15:23:51 -0200461 if (err < 0)
462 goto error;
Lucas De Marchid8a6c0c2012-01-01 06:07:46 -0200463
Lucas De Marchia872bba2012-01-12 15:23:51 -0200464 rmmod_do_deps_list(pre, false);
Dave Reisner0e9bd2d2011-12-31 18:02:45 -0500465
Gustavo Sverzut Barbierie793f1e2011-12-16 22:35:28 -0200466error:
467 kmod_module_unref_list(pre);
468 kmod_module_unref_list(post);
Lucas De Marchia872bba2012-01-12 15:23:51 -0200469
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200470 return err;
471}
472
Lucas De Marchi569f1602012-01-21 02:45:06 -0200473static int rmmod(struct kmod_ctx *ctx, const char *alias)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200474{
475 struct kmod_list *l, *list = NULL;
476 int err;
477
478 err = kmod_module_new_from_lookup(ctx, alias, &list);
Lucas De Marchie5e2a682011-12-19 02:26:34 -0200479 if (err < 0)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200480 return err;
Lucas De Marchie5e2a682011-12-19 02:26:34 -0200481
482 if (list == NULL)
483 LOG("Module %s not found.\n", alias);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200484
485 kmod_list_foreach(l, list) {
486 struct kmod_module *mod = kmod_module_get_module(l);
Lucas De Marchia872bba2012-01-12 15:23:51 -0200487 err = rmmod_do_module(mod, true);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200488 kmod_module_unref(mod);
489 if (err < 0)
490 break;
491 }
492
493 kmod_module_unref_list(list);
494 return err;
495}
496
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200497static int rmmod_all(struct kmod_ctx *ctx, char **args, int nargs)
498{
499 int i, err = 0;
500
501 for (i = 0; i < nargs; i++) {
502 int r = rmmod(ctx, args[i]);
503 if (r < 0)
504 err = r;
505 }
506
507 return err;
508}
509
Lucas De Marchi92122612012-01-11 21:48:08 -0200510static int handle_failed_lookup(struct kmod_ctx *ctx, const char *alias)
511{
512 struct kmod_module *mod;
513 int state, err;
514
515 DBG("lookup failed - trying to check if it's builtin\n");
516
517 err = kmod_module_new_from_name(ctx, alias, &mod);
518 if (err < 0)
519 return err;
520
521 state = kmod_module_get_initstate(mod);
522 kmod_module_unref(mod);
523
524 if (state != KMOD_MODULE_BUILTIN) {
525 LOG("Module %s not found.\n", alias);
526 return -ENOENT;
527 }
528
529 if (first_time) {
530 LOG("Module %s already in kernel (builtin).\n", alias);
531 return -ENOENT;
532 }
533
534 SHOW("builtin %s\n", alias);
535 return 0;
536}
537
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200538static void print_action(struct kmod_module *m, bool install,
539 const char *options)
540{
541 if (install)
542 printf("install %s %s\n", kmod_module_get_install_commands(m),
543 options);
544 else
545 printf("insmod %s %s\n", kmod_module_get_path(m), options);
546}
547
Lucas De Marchi569f1602012-01-21 02:45:06 -0200548static int insmod(struct kmod_ctx *ctx, const char *alias,
Lucas De Marchi8f192212012-01-11 15:38:50 -0200549 const char *extra_options)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200550{
551 struct kmod_list *l, *list = NULL;
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200552 int err, flags = 0;
553
554 void (*show)(struct kmod_module *m, bool install,
555 const char *options) = NULL;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200556
557 err = kmod_module_new_from_lookup(ctx, alias, &list);
Lucas De Marchie5e2a682011-12-19 02:26:34 -0200558 if (err < 0)
559 return err;
560
Lucas De Marchi92122612012-01-11 21:48:08 -0200561 if (list == NULL)
562 return handle_failed_lookup(ctx, alias);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200563
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200564 if (strip_modversion || force)
565 flags |= KMOD_PROBE_FORCE_MODVERSION;
566 if (strip_vermagic || force)
567 flags |= KMOD_PROBE_FORCE_VERMAGIC;
568 if (ignore_commands)
569 flags |= KMOD_PROBE_IGNORE_COMMAND;
570 if (ignore_loaded)
571 flags |= KMOD_PROBE_IGNORE_LOADED;
572 if (dry_run)
573 flags |= KMOD_PROBE_DRY_RUN;
574 if (do_show || verbose > DEFAULT_VERBOSE)
575 show = &print_action;
576
577
578 if (use_blacklist)
579 flags |= KMOD_PROBE_APPLY_BLACKLIST;
580 if (first_time)
581 flags |= KMOD_PROBE_STOP_ON_ALREADY_LOADED;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200582
583 kmod_list_foreach(l, list) {
584 struct kmod_module *mod = kmod_module_get_module(l);
Lucas De Marchi8b013762012-01-12 17:14:30 -0200585
Lucas De Marchi81229852011-12-16 02:58:48 -0200586 if (lookup_only)
587 printf("%s\n", kmod_module_get_name(mod));
Lucas De Marchi8b013762012-01-12 17:14:30 -0200588 else {
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200589 err = kmod_module_probe_insert_module(mod, flags,
590 extra_options, NULL, NULL, show);
Lucas De Marchi8b013762012-01-12 17:14:30 -0200591 }
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200592
Dave Reisner297a3182012-01-30 20:57:36 -0500593 if (err >= 0)
594 /* ignore flag return values such as a mod being blacklisted */
595 err = 0;
596 else {
597 switch (err) {
598 case -EEXIST:
599 ERR("could not insert '%s': Module already in kernel\n",
600 kmod_module_get_name(mod));
601 break;
602 default:
603 ERR("could not insert '%s': %s\n",
604 kmod_module_get_name(mod),
605 strerror(-err));
606 break;
607 }
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200608 }
609
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200610 kmod_module_unref(mod);
Lucas De Marchi2e9dcd72012-01-30 19:01:24 -0200611 if (err != 0)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200612 break;
613 }
614
615 kmod_module_unref_list(list);
616 return err;
617}
618
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200619static int insmod_all(struct kmod_ctx *ctx, char **args, int nargs)
620{
621 int i, err = 0;
622
623 for (i = 0; i < nargs; i++) {
624 int r = insmod(ctx, args[i], NULL);
625 if (r < 0)
626 err = r;
627 }
628
629 return err;
630}
631
632static void env_modprobe_options_append(const char *value)
633{
634 const char *old = getenv("MODPROBE_OPTIONS");
635 char *env;
636
637 if (old == NULL) {
638 setenv("MODPROBE_OPTIONS", value, 1);
639 return;
640 }
641
642 if (asprintf(&env, "%s %s", old, value) < 0) {
643 ERR("could not append value to $MODPROBE_OPTIONS\n");
644 return;
645 }
646
647 if (setenv("MODPROBE_OPTIONS", env, 1) < 0)
648 ERR("could not setenv(MODPROBE_OPTIONS, \"%s\")\n", env);
649 free(env);
650}
651
652static int options_from_array(char **args, int nargs, char **output)
653{
654 char *opts = NULL;
655 size_t optslen = 0;
656 int i, err = 0;
657
658 for (i = 1; i < nargs; i++) {
659 size_t len = strlen(args[i]);
660 size_t qlen = 0;
661 const char *value;
662 void *tmp;
663
664 value = strchr(args[i], '=');
665 if (value) {
666 value++;
667 if (*value != '"' && *value != '\'') {
668 if (strchr(value, ' '))
669 qlen = 2;
670 }
671 }
672
673 tmp = realloc(opts, optslen + len + qlen + 2);
674 if (!tmp) {
675 err = -errno;
676 free(opts);
677 opts = NULL;
678 ERR("could not gather module options: out-of-memory\n");
679 break;
680 }
681 opts = tmp;
682 if (optslen > 0) {
683 opts[optslen] = ' ';
684 optslen++;
685 }
686 if (qlen == 0) {
687 memcpy(opts + optslen, args[i], len + 1);
688 optslen += len;
689 } else {
690 size_t keylen = value - args[i];
691 size_t valuelen = len - keylen;
692 memcpy(opts + optslen, args[i], keylen);
693 optslen += keylen;
694 opts[optslen] = '"';
695 optslen++;
696 memcpy(opts + optslen, value, valuelen);
697 optslen += valuelen;
698 opts[optslen] = '"';
699 optslen++;
700 opts[optslen] = '\0';
701 }
702 }
703
704 *output = opts;
705 return err;
706}
707
708static char **prepend_options_from_env(int *p_argc, char **orig_argv)
709{
710 const char *p, *env = getenv("MODPROBE_OPTIONS");
711 char **new_argv, *str_start, *str_end, *str, *s, *quote;
712 int i, argc = *p_argc;
713 size_t envlen, space_count = 0;
714
715 if (env == NULL)
716 return orig_argv;
717
718 for (p = env; *p != '\0'; p++) {
719 if (*p == ' ')
720 space_count++;
721 }
722
723 envlen = p - env;
724 new_argv = malloc(sizeof(char *) * (argc + space_count + 3 + envlen));
725 if (new_argv == NULL)
726 return NULL;
727
728 new_argv[0] = orig_argv[0];
729 str_start = str = (char *) (new_argv + argc + space_count + 3);
730 memcpy(str, env, envlen + 1);
731
732 str_end = str_start + envlen;
733
734 quote = NULL;
735 for (i = 1, s = str; *s != '\0'; s++) {
736 if (quote == NULL) {
737 if (*s == ' ') {
738 new_argv[i] = str;
739 i++;
740 *s = '\0';
741 str = s + 1;
742 } else if (*s == '"' || *s == '\'')
743 quote = s;
744 } else {
745 if (*s == *quote) {
746 if (quote == str) {
747 new_argv[i] = str + 1;
748 i++;
749 *s = '\0';
750 str = s + 1;
751 } else {
752 char *it;
753 for (it = quote; it < s - 1; it++)
754 it[0] = it[1];
755 for (it = s - 1; it < str_end - 2; it++)
756 it[0] = it[2];
757 str_end -= 2;
758 *str_end = '\0';
759 s -= 2;
760 }
761 quote = NULL;
762 }
763 }
764 }
765 if (str < s) {
766 new_argv[i] = str;
767 i++;
768 }
769
770 memcpy(new_argv + i, orig_argv + 1, sizeof(char *) * (argc - 1));
771 new_argv[i + argc] = NULL;
772 *p_argc = i + argc - 1;
773
774 return new_argv;
775}
776
777static void log_syslog(void *data, int priority, const char *file, int line,
Lucas De Marchi8f192212012-01-11 15:38:50 -0200778 const char *fn, const char *format, va_list args)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200779{
780 char *str, buf[32];
781 const char *prioname;
782
783 switch (priority) {
784 case LOG_CRIT:
785 prioname = "FATAL";
786 break;
787 case LOG_ERR:
788 prioname = "ERROR";
789 break;
790 case LOG_WARNING:
791 prioname = "WARNING";
792 break;
793 case LOG_NOTICE:
794 prioname = "NOTICE";
795 break;
796 case LOG_INFO:
797 prioname = "INFO";
798 break;
799 case LOG_DEBUG:
800 prioname = "DEBUG";
801 break;
802 default:
803 snprintf(buf, sizeof(buf), "LOG-%03d", priority);
804 prioname = buf;
805 }
806
807 if (vasprintf(&str, format, args) < 0)
808 return;
809#ifdef ENABLE_DEBUG
810 syslog(LOG_NOTICE, "%s: %s:%d %s() %s", prioname, file, line, fn, str);
811#else
812 syslog(LOG_NOTICE, "%s: %s", prioname, str);
813#endif
814 free(str);
815 (void)data;
816}
817
Lucas De Marchifa29c0e2011-12-22 03:54:46 -0200818static int do_modprobe(int argc, char **orig_argv)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200819{
820 struct kmod_ctx *ctx;
821 char **args = NULL, **argv;
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200822 const char **config_paths = NULL;
823 int nargs = 0, n_config_paths = 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200824 char dirname_buf[PATH_MAX];
825 const char *dirname = NULL;
826 const char *root = NULL;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200827 const char *kversion = NULL;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200828 int use_all = 0;
829 int do_remove = 0;
830 int do_show_config = 0;
831 int do_show_modversions = 0;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200832 int err;
833
834 argv = prepend_options_from_env(&argc, orig_argv);
835 if (argv == NULL) {
836 fputs("Error: could not prepend options from command line\n",
837 stderr);
838 return EXIT_FAILURE;
839 }
840
841 for (;;) {
842 int c, idx = 0;
843 c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
844 if (c == -1)
845 break;
846 switch (c) {
847 case 'a':
848 log_priority = LOG_WARNING;
849 use_all = 1;
850 break;
851 case 'r':
852 do_remove = 1;
853 break;
854 case 5:
855 remove_dependencies = 1;
856 break;
857 case 'R':
Lucas De Marchi81229852011-12-16 02:58:48 -0200858 lookup_only = 1;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200859 break;
860 case 3:
861 first_time = 1;
862 break;
863 case 'i':
864 ignore_commands = 1;
865 break;
866 case 'b':
867 use_blacklist = 1;
868 break;
869 case 'f':
870 force = 1;
871 break;
872 case 2:
873 strip_modversion = 1;
874 break;
875 case 1:
876 strip_vermagic = 1;
877 break;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200878 case 'D':
879 ignore_loaded = 1;
880 dry_run = 1;
Gustavo Sverzut Barbieri525fa072012-01-08 13:58:28 -0200881 do_show = 1;
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200882 break;
883 case 'c':
884 do_show_config = 1;
885 break;
886 case 4:
887 do_show_modversions = 1;
888 break;
889 case 'n':
890 dry_run = 1;
891 break;
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200892 case 'C': {
893 size_t bytes = sizeof(char *) * (n_config_paths + 2);
894 void *tmp = realloc(config_paths, bytes);
895 if (!tmp) {
896 fputs("Error: out-of-memory\n", stderr);
897 goto cmdline_failed;
898 }
899 config_paths = tmp;
900 config_paths[n_config_paths] = optarg;
901 n_config_paths++;
902 config_paths[n_config_paths] = NULL;
903
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200904 env_modprobe_options_append("-C");
905 env_modprobe_options_append(optarg);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200906 break;
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200907 }
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200908 case 'd':
909 root = optarg;
910 break;
911 case 'S':
912 kversion = optarg;
913 break;
914 case 's':
915 env_modprobe_options_append("-s");
916 use_syslog = 1;
917 break;
918 case 'q':
919 env_modprobe_options_append("-q");
920 verbose--;
921 break;
922 case 'v':
923 env_modprobe_options_append("-v");
924 verbose++;
925 break;
926 case 'V':
927 puts(PACKAGE " version " VERSION);
928 if (argv != orig_argv)
929 free(argv);
Gustavo Sverzut Barbieri5f9f58f2011-12-21 23:54:35 -0200930 free(config_paths);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200931 return EXIT_SUCCESS;
932 case 'h':
Lucas De Marchi3e8de632011-12-23 01:36:10 -0200933 help(basename(argv[0]));
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200934 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 '?':
939 goto cmdline_failed;
940 default:
Lucas De Marchi8f192212012-01-11 15:38:50 -0200941 fprintf(stderr, "Error: unexpected getopt_long() value '%c'.\n",
942 c);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200943 goto cmdline_failed;
944 }
945 }
946
947 args = argv + optind;
948 nargs = argc - optind;
949
Dave Reisnerb09668c2011-12-31 16:51:40 -0500950 if (!do_show_config) {
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200951 if (nargs == 0) {
952 fputs("Error: missing parameters. See -h.\n", stderr);
953 goto cmdline_failed;
954 }
955 }
956
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200957 if (root != NULL || kversion != NULL) {
958 struct utsname u;
959 if (root == NULL)
960 root = "";
961 if (kversion == NULL) {
962 if (uname(&u) < 0) {
963 fprintf(stderr, "Error: uname() failed: %s\n",
964 strerror(errno));
965 goto cmdline_failed;
966 }
967 kversion = u.release;
968 }
Lucas De Marchi8f192212012-01-11 15:38:50 -0200969 snprintf(dirname_buf, sizeof(dirname_buf),
970 "%s" ROOTPREFIX "/lib/modules/%s", root,
971 kversion);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200972 dirname = dirname_buf;
973 }
974
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -0200975 ctx = kmod_new(dirname, config_paths);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200976 if (!ctx) {
977 fputs("Error: kmod_new() failed!\n", stderr);
978 goto cmdline_failed;
979 }
980 kmod_load_resources(ctx);
981
982 kmod_set_log_priority(ctx, verbose);
983 if (use_syslog) {
984 openlog("modprobe", LOG_CONS, LOG_DAEMON);
985 kmod_set_log_fn(ctx, log_syslog, NULL);
986 }
987
Dave Reisnerb09668c2011-12-31 16:51:40 -0500988 if (do_show_config)
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -0200989 err = show_config(ctx);
990 else if (do_show_modversions)
991 err = show_modversions(ctx, args[0]);
992 else if (do_remove)
993 err = rmmod_all(ctx, args, use_all ? nargs : 1);
994 else if (use_all)
995 err = insmod_all(ctx, args, nargs);
996 else {
997 char *opts;
998 err = options_from_array(args, nargs, &opts);
999 if (err == 0) {
1000 err = insmod(ctx, args[0], opts);
1001 free(opts);
1002 }
1003 }
1004
1005 kmod_unref(ctx);
1006
1007 if (use_syslog)
1008 closelog();
1009
1010 if (argv != orig_argv)
1011 free(argv);
Lucas De Marchi8f192212012-01-11 15:38:50 -02001012
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -02001013 free(config_paths);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -02001014 return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
1015
1016cmdline_failed:
1017 if (argv != orig_argv)
1018 free(argv);
Gustavo Sverzut Barbiericb8d4d32011-12-11 20:37:01 -02001019 free(config_paths);
Gustavo Sverzut Barbieric3d0a5f2011-12-11 19:36:18 -02001020 return EXIT_FAILURE;
1021}
Lucas De Marchifa29c0e2011-12-22 03:54:46 -02001022
1023#ifndef KMOD_BUNDLE_TOOL
1024int main(int argc, char *argv[])
1025{
1026 return do_modprobe(argc, argv);
1027}
1028
1029#else
1030#include "kmod.h"
1031
1032const struct kmod_cmd kmod_cmd_compat_modprobe = {
1033 .name = "modprobe",
1034 .cmd = do_modprobe,
1035 .help = "compat modprobe command",
1036};
1037
1038#endif