chiark / gitweb /
udev: really exclude device-mapper from block device ownership event locking
[elogind.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 <unistd.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <sys/stat.h>
29 #include <sys/wait.h>
30 #include <libkmod.h>
31
32 #include "udev.h"
33
34 static struct kmod_ctx *ctx;
35
36 static int load_module(struct udev *udev, const char *alias)
37 {
38         struct kmod_list *list = NULL;
39         struct kmod_list *l;
40         int err;
41
42         err = kmod_module_new_from_lookup(ctx, alias, &list);
43         if (err < 0)
44                 return err;
45
46         if (list == NULL)
47                 log_debug("no module matches '%s'", alias);
48
49         kmod_list_foreach(l, list) {
50                 struct kmod_module *mod = kmod_module_get_module(l);
51
52                 err = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL);
53                 if (err == KMOD_PROBE_APPLY_BLACKLIST)
54                         log_debug("module '%s' is blacklisted", kmod_module_get_name(mod));
55                 else if (err == 0)
56                         log_debug("inserted '%s'", kmod_module_get_name(mod));
57                 else
58                         log_debug("failed to insert '%s'", kmod_module_get_name(mod));
59
60                 kmod_module_unref(mod);
61         }
62
63         kmod_module_unref_list(list);
64         return err;
65 }
66
67 _printf_(6,0)
68 static void udev_kmod_log(void *data, int priority, const char *file, int line,
69                           const char *fn, const char *format, va_list args)
70 {
71         udev_main_log(data, priority, file, line, fn, format, args);
72 }
73
74 static int builtin_kmod(struct udev_device *dev, int argc, char *argv[], bool test)
75 {
76         struct udev *udev = udev_device_get_udev(dev);
77         int i;
78
79         if (!ctx)
80                 return 0;
81
82         if (argc < 3 || !streq(argv[1], "load")) {
83                 log_error("expect: %s load <module>", argv[0]);
84                 return EXIT_FAILURE;
85         }
86
87         for (i = 2; argv[i]; i++) {
88                 log_debug("execute '%s' '%s'", argv[1], argv[i]);
89                 load_module(udev, argv[i]);
90         }
91
92         return EXIT_SUCCESS;
93 }
94
95 /* called at udev startup and reload */
96 static int builtin_kmod_init(struct udev *udev)
97 {
98         if (ctx)
99                 return 0;
100
101         ctx = kmod_new(NULL, NULL);
102         if (!ctx)
103                 return -ENOMEM;
104
105         log_debug("load module index");
106         kmod_set_log_fn(ctx, udev_kmod_log, udev);
107         kmod_load_resources(ctx);
108         return 0;
109 }
110
111 /* called on udev shutdown and reload request */
112 static void builtin_kmod_exit(struct udev *udev)
113 {
114         log_debug("unload module index");
115         ctx = kmod_unref(ctx);
116 }
117
118 /* called every couple of seconds during event activity; 'true' if config has changed */
119 static bool builtin_kmod_validate(struct udev *udev)
120 {
121         log_debug("validate module index");
122         if (!ctx)
123                 return false;
124         return (kmod_validate_resources(ctx) != KMOD_RESOURCES_OK);
125 }
126
127 const struct udev_builtin udev_builtin_kmod = {
128         .name = "kmod",
129         .cmd = builtin_kmod,
130         .init = builtin_kmod_init,
131         .exit = builtin_kmod_exit,
132         .validate = builtin_kmod_validate,
133         .help = "kernel module loader",
134         .run_once = false,
135 };