4 * Copyright (C) 2011-2012 Kay Sievers <kay@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 load_module(struct udev
*udev
, const char *alias
) {
37 struct kmod_list
*list
= NULL
;
41 err
= kmod_module_new_from_lookup(ctx
, alias
, &list
);
46 log_debug("no module matches '%s'", alias
);
48 kmod_list_foreach(l
, list
) {
49 struct kmod_module
*mod
= kmod_module_get_module(l
);
51 err
= kmod_module_probe_insert_module(mod
, KMOD_PROBE_APPLY_BLACKLIST
, NULL
, NULL
, NULL
, NULL
);
52 if (err
== KMOD_PROBE_APPLY_BLACKLIST
)
53 log_debug("module '%s' is blacklisted", kmod_module_get_name(mod
));
55 log_debug("inserted '%s'", kmod_module_get_name(mod
));
57 log_debug("failed to insert '%s'", kmod_module_get_name(mod
));
59 kmod_module_unref(mod
);
62 kmod_module_unref_list(list
);
67 static void udev_kmod_log(void *data
, int priority
, const char *file
, int line
,
68 const char *fn
, const char *format
, va_list args
) {
69 udev_main_log(data
, priority
, file
, line
, fn
, format
, args
);
72 static int builtin_kmod(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
73 struct udev
*udev
= udev_device_get_udev(dev
);
79 if (argc
< 3 || !streq(argv
[1], "load")) {
80 log_error("expect: %s load <module>", argv
[0]);
84 for (i
= 2; argv
[i
]; i
++) {
85 log_debug("execute '%s' '%s'", argv
[1], argv
[i
]);
86 load_module(udev
, argv
[i
]);
92 /* called at udev startup and reload */
93 static int builtin_kmod_init(struct udev
*udev
) {
97 ctx
= kmod_new(NULL
, NULL
);
101 log_debug("load module index");
102 kmod_set_log_fn(ctx
, udev_kmod_log
, udev
);
103 kmod_load_resources(ctx
);
107 /* called on udev shutdown and reload request */
108 static void builtin_kmod_exit(struct udev
*udev
) {
109 log_debug("unload module index");
110 ctx
= kmod_unref(ctx
);
113 /* called every couple of seconds during event activity; 'true' if config has changed */
114 static bool builtin_kmod_validate(struct udev
*udev
) {
115 log_debug("validate module index");
118 return (kmod_validate_resources(ctx
) != KMOD_RESOURCES_OK
);
121 const struct udev_builtin udev_builtin_kmod
= {
124 .init
= builtin_kmod_init
,
125 .exit
= builtin_kmod_exit
,
126 .validate
= builtin_kmod_validate
,
127 .help
= "kernel module loader",