tools: add basic versions of insert and remove

This commit is contained in:
Caio Marcelo de Oliveira Filho 2015-03-07 10:32:53 -03:00 committed by Lucas De Marchi
parent f4f408f9f3
commit 037618816c
33 changed files with 376 additions and 1 deletions

View File

@ -126,6 +126,8 @@ tools_kmod_SOURCES = \
tools/rmmod.c tools/insmod.c \
tools/modinfo.c tools/modprobe.c \
tools/depmod.c tools/log.h tools/log.c \
tools/insert.c \
tools/remove.c \
tools/static-nodes.c
tools_kmod_LDADD = \
shared/libshared.la \
@ -315,7 +317,8 @@ TESTSUITE = \
testsuite/test-modinfo testsuite/test-util testsuite/test-new-module \
testsuite/test-modprobe testsuite/test-blacklist \
testsuite/test-dependencies testsuite/test-depmod \
testsuite/test-list
testsuite/test-list \
testsuite/test-tools
check_PROGRAMS = $(TESTSUITE)
TESTS = $(TESTSUITE)
@ -355,6 +358,8 @@ testsuite_test_depmod_LDADD = $(TESTSUITE_LDADD)
testsuite_test_depmod_CPPFLAGS = $(TESTSUITE_CPPFLAGS)
testsuite_test_list_LDADD = $(TESTSUITE_LDADD)
testsuite_test_list_CPPFLAGS = $(TESTSUITE_CPPFLAGS)
testsuite_test_tools_LDADD = $(TESTSUITE_LDADD)
testsuite_test_tools_CPPFLAGS = $(TESTSUITE_CPPFLAGS)
testsuite-distclean:
$(RM) -r $(ROOTFS)

View File

@ -17,6 +17,7 @@
/test-modprobe
/test-hash
/test-list
/test-tools
/rootfs
/stamp-rootfs
/test-strbuf.log
@ -49,3 +50,5 @@
/test-testsuite.trs
/test-list.log
/test-list.trs
/test-tools.log
/test-tools.trs

View File

@ -42,6 +42,8 @@ map=(
["test-modinfo/mod-simple-sparc64.ko"]="mod-simple-sparc64.ko"
["test-modinfo/mod-simple-sha1.ko"]="mod-simple.ko"
["test-modinfo/mod-simple-sha256.ko"]="mod-simple.ko"
["test-tools/insert/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
["test-tools/remove/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
)
gzip_array=(

View File

@ -0,0 +1 @@
# Aliases extracted from modules themselves.

View File

@ -0,0 +1 @@
kernel/mod-simple.ko:

View File

@ -0,0 +1 @@
# Device nodes to trigger on-demand module loading.

View File

@ -0,0 +1 @@
# Soft dependencies extracted from modules themselves.

View File

@ -0,0 +1 @@
# Aliases for symbols, used by symbol_request().

View File

@ -0,0 +1 @@
# Aliases extracted from modules themselves.

View File

@ -0,0 +1 @@
kernel/mod-simple.ko:

View File

@ -0,0 +1 @@
# Device nodes to trigger on-demand module loading.

View File

@ -0,0 +1 @@
# Soft dependencies extracted from modules themselves.

View File

@ -0,0 +1 @@
# Aliases for symbols, used by symbol_request().

71
testsuite/test-tools.c Normal file
View File

@ -0,0 +1,71 @@
/*
* Copyright (C) 2015 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <inttypes.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "testsuite.h"
static noreturn int kmod_tool_insert(const struct test *t)
{
const char *progname = ABS_TOP_BUILDDIR "/tools/kmod";
const char *const args[] = {
progname,
"insert", "mod-simple",
NULL,
};
test_spawn_prog(progname, args);
exit(EXIT_FAILURE);
}
DEFINE_TEST(kmod_tool_insert,
.description = "check kmod insert",
.config = {
[TC_UNAME_R] = "4.4.4",
[TC_ROOTFS] = TESTSUITE_ROOTFS "test-tools/insert",
[TC_INIT_MODULE_RETCODES] = "",
},
.modules_loaded = "mod-simple",
);
static noreturn int kmod_tool_remove(const struct test *t)
{
const char *progname = ABS_TOP_BUILDDIR "/tools/kmod";
const char *const args[] = {
progname,
"remove", "mod-simple",
NULL,
};
test_spawn_prog(progname, args);
exit(EXIT_FAILURE);
}
DEFINE_TEST(kmod_tool_remove,
.description = "check kmod remove",
.config = {
[TC_UNAME_R] = "4.4.4",
[TC_ROOTFS] = TESTSUITE_ROOTFS "test-tools/remove",
[TC_DELETE_MODULE_RETCODES] = "",
},
);
TESTSUITE_MAIN();

128
tools/insert.c Normal file
View File

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

View File

@ -40,7 +40,9 @@ static const struct kmod_cmd kmod_cmd_help;
static const struct kmod_cmd *kmod_cmds[] = {
&kmod_cmd_help,
&kmod_cmd_insert,
&kmod_cmd_list,
&kmod_cmd_remove,
&kmod_cmd_static_nodes,
};

View File

@ -34,7 +34,9 @@ extern const struct kmod_cmd kmod_cmd_compat_modinfo;
extern const struct kmod_cmd kmod_cmd_compat_modprobe;
extern const struct kmod_cmd kmod_cmd_compat_depmod;
extern const struct kmod_cmd kmod_cmd_insert;
extern const struct kmod_cmd kmod_cmd_list;
extern const struct kmod_cmd kmod_cmd_static_nodes;
extern const struct kmod_cmd kmod_cmd_remove;
#include "log.h"

150
tools/remove.c Normal file
View File

@ -0,0 +1,150 @@
/*
* kmod-remove - remove modules from the kernel.
*
* Copyright (C) 2015 Intel Corporation. All rights reserved.
* Copyright (C) 2011-2013 ProFUSION embedded systems
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <libkmod/libkmod.h>
#include "kmod.h"
static const char cmdopts_s[] = "h";
static const struct option cmdopts[] = {
{"help", no_argument, 0, 'h'},
{ }
};
static void help(void)
{
printf("Usage:\n"
"\t%s remove [options] module\n"
"Options:\n"
"\t-h, --help show this help\n",
program_invocation_short_name);
}
static int check_module_inuse(struct kmod_module *mod) {
struct kmod_list *holders;
int state;
state = kmod_module_get_initstate(mod);
if (state == KMOD_MODULE_BUILTIN) {
ERR("Module %s is builtin.\n", kmod_module_get_name(mod));
return -ENOENT;
} else if (state < 0) {
ERR("Module %s is not currently loaded\n",
kmod_module_get_name(mod));
return -ENOENT;
}
holders = kmod_module_get_holders(mod);
if (holders != NULL) {
struct kmod_list *itr;
ERR("Module %s is in use by:", kmod_module_get_name(mod));
kmod_list_foreach(itr, holders) {
struct kmod_module *hm = kmod_module_get_module(itr);
fprintf(stderr, " %s", kmod_module_get_name(hm));
kmod_module_unref(hm);
}
fputc('\n', stderr);
kmod_module_unref_list(holders);
return -EBUSY;
}
if (kmod_module_get_refcnt(mod) != 0) {
ERR("Module %s is in use\n", kmod_module_get_name(mod));
return -EBUSY;
}
return 0;
}
static int do_remove(int argc, char *argv[])
{
struct kmod_ctx *ctx;
struct kmod_module *mod;
const char *name;
int err, r = EXIT_SUCCESS;
for (;;) {
int c, idx =0;
c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
if (c == -1)
break;
switch (c) {
case 'h':
help();
return EXIT_SUCCESS;
default:
ERR("Unexpected getopt_long() value '%c'.\n", c);
return EXIT_FAILURE;
}
}
if (optind >= argc) {
ERR("Missing module name\n");
return EXIT_FAILURE;
}
ctx = kmod_new(NULL, NULL);
if (!ctx) {
ERR("kmod_new() failed!\n");
return EXIT_FAILURE;
}
name = argv[optind];
err = kmod_module_new_from_name(ctx, name, &mod);
if (err < 0) {
ERR("Could not remove module %s: %s\n", name, strerror(-err));
goto end;
}
err = check_module_inuse(mod);
if (err < 0)
goto unref;
err = kmod_module_remove_module(mod, 0);
if (err < 0)
goto unref;
unref:
kmod_module_unref(mod);
end:
kmod_unref(ctx);
if (err < 0) {
r = EXIT_FAILURE;
ERR("Could not remove module %s: %s\n", name, strerror(-err));
}
return r;
}
const struct kmod_cmd kmod_cmd_remove = {
.name = "remove",
.cmd = do_remove,
.help = "remove module from kernel",
};