]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-kmod.c
udev: add emacs header line
[thirdparty/systemd.git] / src / udev / udev-builtin-kmod.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /*
4 * load kernel modules
5 *
6 * Copyright (C) 2011-2012 Kay Sievers <kay@vrfy.org>
7 * Copyright (C) 2011 ProFUSION embedded systems
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include <errno.h>
24 #include <libkmod.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include "string-util.h"
30 #include "udev.h"
31
32 static struct kmod_ctx *ctx = NULL;
33
34 static int load_module(struct udev *udev, const char *alias) {
35 struct kmod_list *list = NULL;
36 struct kmod_list *l;
37 int err;
38
39 err = kmod_module_new_from_lookup(ctx, alias, &list);
40 if (err < 0)
41 return err;
42
43 if (list == NULL)
44 log_debug("No module matches '%s'", alias);
45
46 kmod_list_foreach(l, list) {
47 struct kmod_module *mod = kmod_module_get_module(l);
48
49 err = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL);
50 if (err == KMOD_PROBE_APPLY_BLACKLIST)
51 log_debug("Module '%s' is blacklisted", kmod_module_get_name(mod));
52 else if (err == 0)
53 log_debug("Inserted '%s'", kmod_module_get_name(mod));
54 else
55 log_debug("Failed to insert '%s'", kmod_module_get_name(mod));
56
57 kmod_module_unref(mod);
58 }
59
60 kmod_module_unref_list(list);
61 return err;
62 }
63
64 _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) {
65 log_internalv(priority, 0, file, line, fn, format, args);
66 }
67
68 static int builtin_kmod(struct udev_device *dev, int argc, char *argv[], bool test) {
69 struct udev *udev = udev_device_get_udev(dev);
70 int i;
71
72 if (!ctx)
73 return 0;
74
75 if (argc < 3 || !streq(argv[1], "load")) {
76 log_error("expect: %s load <module>", argv[0]);
77 return EXIT_FAILURE;
78 }
79
80 for (i = 2; argv[i]; i++) {
81 log_debug("Execute '%s' '%s'", argv[1], argv[i]);
82 load_module(udev, argv[i]);
83 }
84
85 return EXIT_SUCCESS;
86 }
87
88 /* called at udev startup and reload */
89 static int builtin_kmod_init(struct udev *udev) {
90 if (ctx)
91 return 0;
92
93 ctx = kmod_new(NULL, NULL);
94 if (!ctx)
95 return -ENOMEM;
96
97 log_debug("Load module index");
98 kmod_set_log_fn(ctx, udev_kmod_log, udev);
99 kmod_load_resources(ctx);
100 return 0;
101 }
102
103 /* called on udev shutdown and reload request */
104 static void builtin_kmod_exit(struct udev *udev) {
105 log_debug("Unload module index");
106 ctx = kmod_unref(ctx);
107 }
108
109 /* called every couple of seconds during event activity; 'true' if config has changed */
110 static bool builtin_kmod_validate(struct udev *udev) {
111 log_debug("Validate module index");
112 if (!ctx)
113 return false;
114 return (kmod_validate_resources(ctx) != KMOD_RESOURCES_OK);
115 }
116
117 const struct udev_builtin udev_builtin_kmod = {
118 .name = "kmod",
119 .cmd = builtin_kmod,
120 .init = builtin_kmod_init,
121 .exit = builtin_kmod_exit,
122 .validate = builtin_kmod_validate,
123 .help = "Kernel module loader",
124 .run_once = false,
125 };