udev-builtin-kmod.c revision 238b53a024a47cda38be5d9c11b54256e316f0ca
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani/*
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * load kernel modules
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani *
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * Copyright (C) 2011-2012 Kay Sievers <kay.sievers@vrfy.org>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * Copyright (C) 2011 ProFUSION embedded systems
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani *
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * This program is free software: you can redistribute it and/or modify
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * it under the terms of the GNU General Public License as published by
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * the Free Software Foundation, either version 2 of the License, or
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * (at your option) any later version.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani *
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * This program is distributed in the hope that it will be useful,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * but WITHOUT ANY WARRANTY; without even the implied warranty of
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * GNU General Public License for more details.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani *
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * You should have received a copy of the GNU General Public License
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * along with this program. If not, see <http://www.gnu.org/licenses/>.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <stdio.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <stdlib.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <stdarg.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <unistd.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <string.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <errno.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <fcntl.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <sys/stat.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <sys/wait.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <libkmod.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include "udev.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic struct kmod_ctx *ctx;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic int load_module(struct udev *udev, const char *alias)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani{
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani struct kmod_list *list = NULL;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani struct kmod_list *l;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani int err;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert(ctx);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani err = kmod_module_new_from_lookup(ctx, alias, &list);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (err < 0)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return err;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (list == NULL)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_debug("no module matches '%s'\n", alias);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani kmod_list_foreach(l, list) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani struct kmod_module *mod = kmod_module_get_module(l);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani err = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (err == KMOD_PROBE_APPLY_BLACKLIST)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_debug("module '%s' is blacklisted\n", kmod_module_get_name(mod));
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani else if (err == 0)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_debug("inserted '%s'\n", kmod_module_get_name(mod));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani else
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_debug("failed to insert '%s'\n", kmod_module_get_name(mod));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani kmod_module_unref(mod);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
176c355b43e616b61552566303ad59d5fd910333Beniamino Galvani
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani kmod_module_unref_list(list);
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani return err;
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani}
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvanistatic void udev_kmod_log(void *data, int priority, const char *file, int line,
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani const char *fn, const char *format, va_list args)
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani{
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani udev_main_log(data, priority, file, line, fn, format, args);
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani}
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani
176c355b43e616b61552566303ad59d5fd910333Beniamino Galvanistatic int builtin_kmod(struct udev_device *dev, int argc, char *argv[], bool test)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani{
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani struct udev *udev = udev_device_get_udev(dev);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani int i;
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani if (argc < 3 || strcmp(argv[1], "load")) {
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani log_error("expect: %s load <module>\n", argv[0]);
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani return EXIT_FAILURE;
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani }
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani for (i = 2; argv[i]; i++) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_debug("execute '%s' '%s'\n", argv[1], argv[i]);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani load_module(udev, argv[i]);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return EXIT_SUCCESS;
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani/* called at udev startup and reload */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic int builtin_kmod_init(struct udev *udev)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani{
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (ctx)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return 0;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani ctx = kmod_new(NULL, NULL);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (!ctx)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return -ENOMEM;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_debug("load module index\n");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani kmod_set_log_fn(ctx, udev_kmod_log, udev);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani kmod_load_resources(ctx);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return 0;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani/* called on udev shutdown and reload request */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic void builtin_kmod_exit(struct udev *udev)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani{
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_debug("unload module index\n");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani ctx = kmod_unref(ctx);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani/* called every couple of seconds during event activity; 'true' if config has changed */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic bool builtin_kmod_validate(struct udev *udev)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani{
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_debug("validate module index\n");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (!ctx)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return false;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return (kmod_validate_resources(ctx) != KMOD_RESOURCES_OK);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahaniconst struct udev_builtin udev_builtin_kmod = {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani .name = "kmod",
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani .cmd = builtin_kmod,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani .init = builtin_kmod_init,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani .exit = builtin_kmod_exit,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani .validate = builtin_kmod_validate,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani .help = "kernel module loader",
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani .run_once = false,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani};
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani