Blame tools/insert.c

Packit Service 68b988
/*
Packit Service 68b988
 * kmod-insert - insert a module into the kernel.
Packit Service 68b988
 *
Packit Service 68b988
 * Copyright (C) 2015 Intel Corporation. All rights reserved.
Packit Service 68b988
 * Copyright (C) 2011-2013  ProFUSION embedded systems
Packit Service 68b988
 *
Packit Service 68b988
 * This program is free software: you can redistribute it and/or modify
Packit Service 68b988
 * it under the terms of the GNU General Public License as published by
Packit Service 68b988
 * the Free Software Foundation, either version 2 of the License, or
Packit Service 68b988
 * (at your option) any later version.
Packit Service 68b988
 *
Packit Service 68b988
 * This program is distributed in the hope that it will be useful,
Packit Service 68b988
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 68b988
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 68b988
 * GNU General Public License for more details.
Packit Service 68b988
 *
Packit Service 68b988
 * You should have received a copy of the GNU General Public License
Packit Service 68b988
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit Service 68b988
 */
Packit Service 68b988
Packit Service 68b988
#include <errno.h>
Packit Service 68b988
#include <getopt.h>
Packit Service 68b988
#include <stdlib.h>
Packit Service 68b988
#include <string.h>
Packit Service 68b988
Packit Service 68b988
#include <libkmod/libkmod.h>
Packit Service 68b988
Packit Service 68b988
#include "kmod.h"
Packit Service 68b988
Packit Service 68b988
static const char cmdopts_s[] = "h";
Packit Service 68b988
static const struct option cmdopts[] = {
Packit Service 68b988
	{"help", no_argument, 0, 'h'},
Packit Service 68b988
	{ }
Packit Service 68b988
};
Packit Service 68b988
Packit Service 68b988
static void help(void)
Packit Service 68b988
{
Packit Service 68b988
	printf("Usage:\n"
Packit Service 68b988
	       "\t%s insert [options] module\n"
Packit Service 68b988
	       "Options:\n"
Packit Service 68b988
	       "\t-h, --help        show this help\n",
Packit Service 68b988
	       program_invocation_short_name);
Packit Service 68b988
}
Packit Service 68b988
Packit Service 68b988
static const char *mod_strerror(int err)
Packit Service 68b988
{
Packit Service 68b988
	switch (err) {
Packit Service 68b988
	case KMOD_PROBE_APPLY_BLACKLIST:
Packit Service 68b988
		return "Module is blacklisted";
Packit Service 68b988
	case -EEXIST:
Packit Service 68b988
		return "Module already in kernel";
Packit Service 68b988
	case -ENOENT:
Packit Service 68b988
		return "Unknown symbol in module or unknown parameter (see dmesg)";
Packit Service 68b988
	default:
Packit Service 68b988
		return strerror(-err);
Packit Service 68b988
	}
Packit Service 68b988
}
Packit Service 68b988
Packit Service 68b988
static int do_insert(int argc, char *argv[])
Packit Service 68b988
{
Packit Service 68b988
	struct kmod_ctx *ctx;
Packit Service 68b988
	struct kmod_list *list = NULL, *l;
Packit Service 68b988
	const char *name;
Packit Service 68b988
	int err, r = EXIT_SUCCESS;
Packit Service 68b988
Packit Service 68b988
	for (;;) {
Packit Service 68b988
		int c, idx = 0;
Packit Service 68b988
		c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
Packit Service 68b988
		if (c == -1)
Packit Service 68b988
			break;
Packit Service 68b988
		switch (c) {
Packit Service 68b988
		case 'h':
Packit Service 68b988
			help();
Packit Service 68b988
			return EXIT_SUCCESS;
Packit Service 68b988
		default:
Packit Service 68b988
			ERR("Unexpected getopt_long() value '%c'.\n", c);
Packit Service 68b988
			return EXIT_FAILURE;
Packit Service 68b988
		}
Packit Service 68b988
	}
Packit Service 68b988
Packit Service 68b988
	if (optind >= argc) {
Packit Service 68b988
		ERR("Missing module name\n");
Packit Service 68b988
		return EXIT_FAILURE;
Packit Service 68b988
	}
Packit Service 68b988
Packit Service 68b988
	ctx = kmod_new(NULL, NULL);
Packit Service 68b988
	if (!ctx) {
Packit Service 68b988
		ERR("kmod_new() failed!\n");
Packit Service 68b988
		return EXIT_FAILURE;
Packit Service 68b988
	}
Packit Service 68b988
Packit Service 68b988
	name = argv[optind];
Packit Service 68b988
	err = kmod_module_new_from_lookup(ctx, name, &list);
Packit Service 68b988
	if (err < 0) {
Packit Service 68b988
		ERR("Could not lookup module matching '%s': %s\n", name, strerror(-err));
Packit Service 68b988
		r = EXIT_FAILURE;
Packit Service 68b988
		goto end;
Packit Service 68b988
	}
Packit Service 68b988
Packit Service 68b988
	if (list == NULL) {
Packit Service 68b988
		ERR("No module matches '%s'\n", name);
Packit Service 68b988
		r = EXIT_FAILURE;
Packit Service 68b988
		goto end;
Packit Service 68b988
	}
Packit Service 68b988
Packit Service 68b988
	kmod_list_foreach(l, list) {
Packit Service 68b988
		struct kmod_module *mod = kmod_module_get_module(l);
Packit Service 68b988
Packit Service 68b988
		err = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL);
Packit Service 68b988
		if (err != 0) {
Packit Service 68b988
			r = EXIT_FAILURE;
Packit Service 68b988
			ERR("Could not insert '%s': %s\n", kmod_module_get_name(mod), mod_strerror(err));
Packit Service 68b988
		}
Packit Service 68b988
Packit Service 68b988
		kmod_module_unref(mod);
Packit Service 68b988
	}
Packit Service 68b988
Packit Service 68b988
	kmod_module_unref_list(list);
Packit Service 68b988
end:
Packit Service 68b988
	kmod_unref(ctx);
Packit Service 68b988
	return r;
Packit Service 68b988
}
Packit Service 68b988
Packit Service 68b988
const struct kmod_cmd kmod_cmd_insert = {
Packit Service 68b988
	.name = "insert",
Packit Service 68b988
	.cmd = do_insert,
Packit Service 68b988
	.help = "insert a module into the kernel",
Packit Service 68b988
};