2011-12-11 07:19:41 +07:00
|
|
|
/*
|
|
|
|
* kmod-insmod - insert modules into linux kernel using libkmod.
|
|
|
|
*
|
2013-01-16 20:27:21 +07:00
|
|
|
* Copyright (C) 2011-2013 ProFUSION embedded systems
|
2011-12-11 07:19:41 +07:00
|
|
|
*
|
2011-12-13 03:24:35 +07:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2011-12-11 07:19:41 +07:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2011-12-13 03:24:35 +07:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2011-12-11 07:19:41 +07:00
|
|
|
*
|
2011-12-13 03:24:35 +07:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-12-11 07:19:41 +07:00
|
|
|
*/
|
2011-12-13 03:24:35 +07:00
|
|
|
|
2014-10-03 11:41:42 +07:00
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
2011-12-11 07:19:41 +07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-10-03 11:41:42 +07:00
|
|
|
|
|
|
|
#include <libkmod.h>
|
2011-12-11 07:19:41 +07:00
|
|
|
|
2012-11-07 01:54:17 +07:00
|
|
|
#include "kmod.h"
|
|
|
|
|
2011-12-11 07:19:41 +07:00
|
|
|
static const char cmdopts_s[] = "psfVh";
|
|
|
|
static const struct option cmdopts[] = {
|
|
|
|
{"version", no_argument, 0, 'V'},
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{NULL, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2012-11-07 01:54:17 +07:00
|
|
|
static void help(void)
|
2011-12-11 07:19:41 +07:00
|
|
|
{
|
2012-11-07 02:32:41 +07:00
|
|
|
printf("Usage:\n"
|
2011-12-11 07:19:41 +07:00
|
|
|
"\t%s [options] filename [args]\n"
|
|
|
|
"Options:\n"
|
|
|
|
"\t-V, --version show version\n"
|
|
|
|
"\t-h, --help show this help\n",
|
2012-11-07 04:20:09 +07:00
|
|
|
program_invocation_short_name);
|
2011-12-11 07:19:41 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *mod_strerror(int err)
|
|
|
|
{
|
|
|
|
switch (err) {
|
|
|
|
case ENOEXEC:
|
|
|
|
return "Invalid module format";
|
|
|
|
case ENOENT:
|
|
|
|
return "Unknown symbol in module";
|
|
|
|
case ESRCH:
|
|
|
|
return "Module has wrong symbol version";
|
|
|
|
case EINVAL:
|
|
|
|
return "Invalid parameters";
|
|
|
|
default:
|
|
|
|
return strerror(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 12:45:07 +07:00
|
|
|
static int do_insmod(int argc, char *argv[])
|
2011-12-11 07:19:41 +07:00
|
|
|
{
|
|
|
|
struct kmod_ctx *ctx;
|
|
|
|
struct kmod_module *mod;
|
|
|
|
const char *filename;
|
|
|
|
char *opts = NULL;
|
|
|
|
size_t optslen = 0;
|
|
|
|
int i, err;
|
2011-12-13 00:41:02 +07:00
|
|
|
const char *null_config = NULL;
|
2011-12-11 07:19:41 +07:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int c, idx = 0;
|
|
|
|
c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
|
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
switch (c) {
|
|
|
|
case 'p':
|
|
|
|
case 's':
|
|
|
|
case 'f':
|
|
|
|
/* ignored, for compatibility only */
|
|
|
|
break;
|
|
|
|
case 'h':
|
2012-11-07 01:54:17 +07:00
|
|
|
help();
|
2011-12-11 07:19:41 +07:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
case 'V':
|
|
|
|
puts(PACKAGE " version " VERSION);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
case '?':
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
default:
|
2012-11-06 00:08:54 +07:00
|
|
|
ERR("unexpected getopt_long() value '%c'.\n",
|
2011-12-11 07:19:41 +07:00
|
|
|
c);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind >= argc) {
|
2012-11-06 00:08:54 +07:00
|
|
|
ERR("missing filename.\n");
|
2011-12-11 07:19:41 +07:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = argv[optind];
|
|
|
|
if (strcmp(filename, "-") == 0) {
|
2012-11-06 00:08:54 +07:00
|
|
|
ERR("this tool does not support loading from stdin!\n");
|
2011-12-11 07:19:41 +07:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = optind + 1; i < argc; i++) {
|
|
|
|
size_t len = strlen(argv[i]);
|
|
|
|
void *tmp = realloc(opts, optslen + len + 2);
|
|
|
|
if (tmp == NULL) {
|
2012-11-06 00:08:54 +07:00
|
|
|
ERR("out of memory\n");
|
2011-12-11 07:19:41 +07:00
|
|
|
free(opts);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
opts = tmp;
|
|
|
|
if (optslen > 0) {
|
|
|
|
opts[optslen] = ' ';
|
|
|
|
optslen++;
|
|
|
|
}
|
|
|
|
memcpy(opts + optslen, argv[i], len);
|
|
|
|
optslen += len;
|
|
|
|
opts[optslen] = '\0';
|
|
|
|
}
|
|
|
|
|
2011-12-13 00:41:02 +07:00
|
|
|
ctx = kmod_new(NULL, &null_config);
|
2011-12-11 07:19:41 +07:00
|
|
|
if (!ctx) {
|
2012-11-06 00:08:54 +07:00
|
|
|
ERR("kmod_new() failed!\n");
|
2011-12-11 07:19:41 +07:00
|
|
|
free(opts);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = kmod_module_new_from_path(ctx, filename, &mod);
|
|
|
|
if (err < 0) {
|
2012-11-06 00:08:54 +07:00
|
|
|
ERR("could not load module %s: %s\n", filename,
|
|
|
|
strerror(-err));
|
2011-12-11 07:19:41 +07:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = kmod_module_insert_module(mod, 0, opts);
|
|
|
|
if (err < 0) {
|
2012-11-06 00:08:54 +07:00
|
|
|
ERR("could not insert module %s: %s\n", filename,
|
|
|
|
mod_strerror(-err));
|
2011-12-11 07:19:41 +07:00
|
|
|
}
|
|
|
|
kmod_module_unref(mod);
|
|
|
|
|
|
|
|
end:
|
|
|
|
kmod_unref(ctx);
|
|
|
|
free(opts);
|
|
|
|
return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
}
|
2011-12-22 12:45:07 +07:00
|
|
|
|
|
|
|
const struct kmod_cmd kmod_cmd_compat_insmod = {
|
|
|
|
.name = "insmod",
|
|
|
|
.cmd = do_insmod,
|
|
|
|
.help = "compat insmod command",
|
|
|
|
};
|