]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-kmod.c
Merge pull request #57 from pwithnall/wip/pwithnall/udev-virtualbox-rules
[thirdparty/systemd.git] / src / udev / udev-builtin-kmod.c
1 /*
2 * load kernel modules
3 *
4 * Copyright (C) 2011-2012 Kay Sievers <kay@vrfy.org>
5 * Copyright (C) 2011 ProFUSION embedded systems
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <errno.h>
25 #include <libkmod.h>
26
27 #include "udev.h"
28
29 static struct kmod_ctx *ctx = NULL;
30
31 static int load_module(struct udev *udev, const char *alias) {
32 struct kmod_list *list = NULL;
33 struct kmod_list *l;
34 int err;
35
36 err = kmod_module_new_from_lookup(ctx, alias, &list);
37 if (err < 0)
38 return err;
39
40 if (list == NULL)
41 log_debug("No module matches '%s'", alias);
42
43 kmod_list_foreach(l, list) {
44 struct kmod_module *mod = kmod_module_get_module(l);
45
46 err = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL);
47 if (err == KMOD_PROBE_APPLY_BLACKLIST)
48 log_debug("Module '%s' is blacklisted", kmod_module_get_name(mod));
49 else if (err == 0)
50 log_debug("Inserted '%s'", kmod_module_get_name(mod));
51 else
52 log_debug("Failed to insert '%s'", kmod_module_get_name(mod));
53
54 kmod_module_unref(mod);
55 }
56
57 kmod_module_unref_list(list);
58 return err;
59 }
60
61 _printf_(6,0) static void udev_kmod_log(void *data, int priority, const char *file, int line, const char *fn, const char *format, va_list args) {
62 log_internalv(priority, 0, file, line, fn, format, args);
63 }
64
65 static int builtin_kmod(struct udev_device *dev, int argc, char *argv[], bool test) {
66 struct udev *udev = udev_device_get_udev(dev);
67 int i;
68
69 if (!ctx)
70 return 0;
71
72 if (argc < 3 || !streq(argv[1], "load")) {
73 log_error("expect: %s load <module>", argv[0]);
74 return EXIT_FAILURE;
75 }
76
77 for (i = 2; argv[i]; i++) {
78 log_debug("Execute '%s' '%s'", argv[1], argv[i]);
79 load_module(udev, argv[i]);
80 }
81
82 return EXIT_SUCCESS;
83 }
84
85 /* called at udev startup and reload */
86 static int builtin_kmod_init(struct udev *udev) {
87 if (ctx)
88 return 0;
89
90 ctx = kmod_new(NULL, NULL);
91 if (!ctx)
92 return -ENOMEM;
93
94 log_debug("Load module index");
95 kmod_set_log_fn(ctx, udev_kmod_log, udev);
96 kmod_load_resources(ctx);
97 return 0;
98 }
99
100 /* called on udev shutdown and reload request */
101 static void builtin_kmod_exit(struct udev *udev) {
102 log_debug("Unload module index");
103 ctx = kmod_unref(ctx);
104 }
105
106 /* called every couple of seconds during event activity; 'true' if config has changed */
107 static bool builtin_kmod_validate(struct udev *udev) {
108 log_debug("Validate module index");
109 if (!ctx)
110 return false;
111 return (kmod_validate_resources(ctx) != KMOD_RESOURCES_OK);
112 }
113
114 const struct udev_builtin udev_builtin_kmod = {
115 .name = "kmod",
116 .cmd = builtin_kmod,
117 .init = builtin_kmod_init,
118 .exit = builtin_kmod_exit,
119 .validate = builtin_kmod_validate,
120 .help = "Kernel module loader",
121 .run_once = false,
122 };