chiark / gitweb /
keymap: Fix rfkill button on Hewlett-Packard HP ProBook
[elogind.git] / src / udev-builtin-kmod.c
1 /*
2  * load kernel modules
3  *
4  * Copyright (C) 2011 Kay Sievers <kay.sievers@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 *listb = NULL;
40         struct kmod_list *l;
41         int err;
42
43         err = kmod_module_new_from_lookup(ctx, alias, &list);
44         if (err < 0)
45                 return err;
46
47         err = kmod_module_get_filtered_blacklist(ctx, list, &listb);
48         if (err < 0)
49                 return err;
50
51         if (list == NULL)
52                 info(udev, "no module matches '%s'\n", alias);
53         else if (listb == NULL)
54                 info(udev, "modules matching '%s' are blacklisted\n", alias);
55
56         kmod_list_foreach(l, listb) {
57                 struct kmod_module *mod = kmod_module_get_module(l);
58
59                 err = kmod_module_probe_insert_module(mod, 0, NULL, NULL, NULL);
60                 if (err >=0 )
61                         info(udev, "inserted '%s'\n", kmod_module_get_name(mod));
62                 else
63                         info(udev, "failed to insert '%s'\n", kmod_module_get_name(mod));
64
65                 kmod_module_unref(mod);
66         }
67
68         kmod_module_unref_list(list);
69         kmod_module_unref_list(listb);
70         return err;
71 }
72
73 static void udev_kmod_log(void *data, int priority, const char *file, int line,
74                           const char *fn, const char *format, va_list args)
75 {
76         udev_main_log(data, priority, file, line, fn, format, args);
77 }
78
79 /* needs to re-instantiate the context after a reload */
80 static int builtin_kmod(struct udev_device *dev, int argc, char *argv[], bool test)
81 {
82         struct udev *udev = udev_device_get_udev(dev);
83         int i;
84
85         if (!ctx) {
86                 ctx = kmod_new(NULL, NULL);
87                 if (!ctx)
88                         return -ENOMEM;
89
90                 info(udev, "load module index\n");
91                 kmod_set_log_fn(ctx, udev_kmod_log, udev);
92                 kmod_load_resources(ctx);
93         }
94
95         if (argc < 3 || strcmp(argv[1], "load")) {
96                 err(udev, "expect: %s load <module>\n", argv[0]);
97                 return EXIT_FAILURE;
98         }
99
100         for (i = 2; argv[i]; i++) {
101                 info(udev, "execute '%s' '%s'\n", argv[1], argv[i]);
102                 load_module(udev, argv[i]);
103         }
104
105         return EXIT_SUCCESS;
106 }
107
108 /* called at udev startup */
109 static int builtin_kmod_init(struct udev *udev)
110 {
111         if (ctx)
112                 return 0;
113
114         ctx = kmod_new(NULL, NULL);
115         if (!ctx)
116                 return -ENOMEM;
117
118         info(udev, "load module index\n");
119         kmod_set_log_fn(ctx, udev_kmod_log, udev);
120         kmod_load_resources(ctx);
121         return 0;
122 }
123
124 /* called on udev shutdown and reload request */
125 static void builtin_kmod_exit(struct udev *udev)
126 {
127         info(udev, "unload module index\n");
128         ctx = kmod_unref(ctx);
129 }
130
131 /* called every couple of seconds during event activity; 'true' if config has changed */
132 static bool builtin_kmod_validate(struct udev *udev)
133 {
134         info(udev, "validate module index\n");
135         if (kmod_validate_resources(ctx) != KMOD_RESOURCES_OK)
136                 return true;
137         return false;
138 }
139
140 const struct udev_builtin udev_builtin_kmod = {
141         .name = "kmod",
142         .cmd = builtin_kmod,
143         .init = builtin_kmod_init,
144         .exit = builtin_kmod_exit,
145         .validate = builtin_kmod_validate,
146         .help = "kernel module loader",
147         .run_once = false,
148 };