4 * Copyright (C) 2011 Kay Sievers <kay.sievers@vrfy.org>
5 * Copyright (C) 2011 ProFUSION embedded systems
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 static struct kmod_ctx *ctx;
36 static int command_do(struct kmod_module *module, const char *type, const char *command, const char *cmdline_opts)
38 const char *modname = kmod_module_get_name(module);
40 size_t cmdlen, cmdline_opts_len, varlen;
43 if (cmdline_opts == NULL)
45 cmdline_opts_len = strlen(cmdline_opts);
47 cmd = strdup(command);
51 varlen = sizeof("$CMDLINE_OPTS") - 1;
52 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
53 size_t prefixlen = p - cmd;
54 size_t suffixlen = cmdlen - prefixlen - varlen;
55 size_t slen = cmdlen - varlen + cmdline_opts_len;
56 char *suffix = p + varlen;
57 char *s = malloc(slen + 1);
62 memcpy(s, cmd, p - cmd);
63 memcpy(s + prefixlen, cmdline_opts, cmdline_opts_len);
64 memcpy(s + prefixlen + cmdline_opts_len, suffix, suffixlen);
72 setenv("MODPROBE_MODULE", modname, 1);
74 unsetenv("MODPROBE_MODULE");
75 if (ret == -1 || WEXITSTATUS(ret)) {
76 //LOG("Error running %s command for %s\n", type, modname);
78 ret = -WEXITSTATUS(ret);
86 static int insmod_do_dependencies(struct kmod_module *parent);
87 static int insmod_do_soft_dependencies(struct kmod_module *mod, struct kmod_list *deps);
89 static int insmod_do_deps_list(struct kmod_module *parent, struct kmod_list *deps, unsigned stop_on_errors)
94 kmod_list_foreach(d, deps) {
95 struct kmod_module *dm = kmod_module_get_module(d);
96 struct kmod_list *pre = NULL, *post = NULL;
97 const char *cmd, *opts, *dmname = kmod_module_get_name(dm);
101 r = insmod_do_dependencies(dm);
103 //WRN("could not insert dependencies of '%s': %s\n", dmname, strerror(-r));
107 r = kmod_module_get_softdeps(dm, &pre, &post);
109 //WRN("could not get softdeps of '%s': %s\n", dmname, strerror(-r));
113 r = insmod_do_soft_dependencies(dm, pre);
115 //WRN("could not insert pre softdeps of '%s': %s\n", dmname, strerror(-r));
119 state = kmod_module_get_initstate(dm);
120 if (state == KMOD_MODULE_LIVE ||
121 state == KMOD_MODULE_COMING ||
122 state == KMOD_MODULE_BUILTIN)
125 cmd = kmod_module_get_install_commands(dm);
127 r = command_do(dm, "install", cmd, NULL);
129 //WRN("failed to execute install command of '%s':" " %s\n", dmname, strerror(-r));
135 opts = kmod_module_get_options(dm);
137 r = kmod_module_insert_module(dm, 0, opts);
139 //WRN("could not insert '%s': %s\n", dmname, strerror(-r));
144 r = insmod_do_soft_dependencies(dm, post);
146 //WRN("could not insert post softdeps of '%s': %s\n", dmname, strerror(-r));
150 kmod_module_unref_list(pre);
151 kmod_module_unref_list(post);
152 kmod_module_unref(dm);
157 kmod_module_unref_list(pre);
158 kmod_module_unref_list(post);
159 kmod_module_unref(dm);
169 static int insmod_do_soft_dependencies(struct kmod_module *mod, struct kmod_list *deps)
171 return insmod_do_deps_list(mod, deps, 0);
174 static int insmod_do_dependencies(struct kmod_module *parent)
176 struct kmod_list *deps = kmod_module_get_dependencies(parent);
177 int err = insmod_do_deps_list(parent, deps, 1);
178 kmod_module_unref_list(deps);
182 static int insmod_do(struct kmod_module *mod, const char *extra_opts)
184 const char *modname = kmod_module_get_name(mod);
185 const char *conf_opts = kmod_module_get_options(mod);
186 struct kmod_list *pre = NULL, *post = NULL;
192 err = kmod_module_get_softdeps(mod, &pre, &post);
194 //WRN("could not get softdeps of '%s': %s\n", modname, strerror(-err));
198 err = insmod_do_soft_dependencies(mod, pre);
200 //WRN("could not insert pre softdeps of '%s': %s\n", modname, strerror(-err));
204 cmd = kmod_module_get_install_commands(mod);
206 err = command_do(mod, "install", cmd, extra_opts);
210 state = kmod_module_get_initstate(mod);
211 if (state == KMOD_MODULE_BUILTIN || state == KMOD_MODULE_LIVE)
215 * At this point it's not possible to be a install/remove command
216 * anymore. So if we can't get module's path, it's because it was
217 * really intended to be a module and it doesn't exist
219 if (kmod_module_get_path(mod) == NULL) {
220 //LOG("Module %s not found.\n", modname);
224 err = insmod_do_dependencies(mod);
228 if (conf_opts || extra_opts) {
229 if (conf_opts == NULL)
230 opts = strdup(extra_opts);
231 else if (extra_opts == NULL)
232 opts = strdup(conf_opts);
233 else if (asprintf(&opts, "%s %s", conf_opts, extra_opts) < 0)
242 err = kmod_module_insert_module(mod, 0, opts);
247 err = insmod_do_soft_dependencies(mod, post);
249 //WRN("could not insert post softdeps of '%s': %s\n", modname, strerror(-err));
254 kmod_module_unref_list(pre);
255 kmod_module_unref_list(post);
260 static int insmod_path(struct kmod_ctx *ctx, const char *path, const char *extra_options)
262 struct kmod_module *mod;
265 err = kmod_module_new_from_path(ctx, path, &mod);
267 //LOG("Module %s not found.\n", path);
270 err = insmod_do(mod, extra_options);
271 kmod_module_unref(mod);
275 static int insmod_alias(struct kmod_ctx *ctx, const char *alias, const char *extra_options)
277 struct kmod_list *l, *list = NULL;
278 struct kmod_list *filtered = NULL;
281 err = kmod_module_new_from_lookup(ctx, alias, &list);
286 //LOG("Module %s not found.\n", alias);
290 err = kmod_module_get_filtered_blacklist(ctx, list, &filtered);
291 kmod_module_unref_list(list);
293 //LOG("Could not filter alias list!\n");
298 kmod_list_foreach(l, list) {
299 struct kmod_module *mod = kmod_module_get_module(l);
300 err = insmod_do(mod, extra_options);
301 kmod_module_unref(mod);
306 kmod_module_unref_list(list);
310 static int insmod(struct kmod_ctx *ctx, const char *name, const char *extra_options)
313 if (stat(name, &st) == 0)
314 return insmod_path(ctx, name, extra_options);
316 return insmod_alias(ctx, name, extra_options);
319 static int builtin_kmod(struct udev_device *dev, int argc, char *argv[], bool test)
321 struct udev *udev = udev_device_get_udev(dev);
328 err(udev, "missing command + argument\n");
332 for (i = 2; argv[i]; i++) {
333 info(udev, "%s '%s'\n", argv[1], argv[i]);
334 insmod(ctx, argv[i], NULL);
340 static int builtin_kmod_init(struct udev *udev)
343 ctx = kmod_new(NULL, NULL);
347 info(udev, "load module index\n");
351 static int builtin_kmod_exit(struct udev *udev)
353 ctx = kmod_unref(ctx);
354 info(udev, "unload module index\n");
358 const struct udev_builtin udev_builtin_kmod = {
361 .init = builtin_kmod_init,
362 .exit = builtin_kmod_exit,
363 .help = "kernel module loader",