From 8900b9166b650d22a93d020889a35382f70fc9bf Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Thu, 22 Dec 2011 02:33:36 -0200 Subject: [PATCH] tools: add skeleton of kmod tool If using libtool 2.4.2, running the script generated by libtool will not work because libtool changes argv[0] to lt-progname. To test this is necessary to either fix the installed build-aux/ltmain.sh file or run the binary directly like in: $ export LD_LIBRARY_PATH=$PWD/libkmod/.libs/ $ ./tools/.libs/kmod help --- Makefile.am | 5 +++- tools/.gitignore | 1 + tools/kmod.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/kmod.h | 30 ++++++++++++++++++++ 4 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 tools/kmod.c create mode 100644 tools/kmod.h diff --git a/Makefile.am b/Makefile.am index 6d598e3..3eaef81 100644 --- a/Makefile.am +++ b/Makefile.am @@ -82,13 +82,16 @@ uninstall-hook: if BUILD_TOOLS bin_PROGRAMS = tools/kmod-insmod tools/kmod-rmmod tools/kmod-lsmod \ - tools/kmod-modprobe tools/kmod-modinfo + tools/kmod-modprobe tools/kmod-modinfo tools/kmod tools_kmod_insmod_LDADD = libkmod/libkmod.la tools_kmod_rmmod_LDADD = libkmod/libkmod.la tools_kmod_lsmod_LDADD = libkmod/libkmod.la tools_kmod_modprobe_LDADD = libkmod/libkmod.la tools_kmod_modinfo_LDADD = libkmod/libkmod.la + +tools_kmod_SOURCES = tools/kmod.c tools/kmod.h +tools_kmod_LDADD = libkmod/libkmod.la endif TESTS = test/test-init test/test-loaded diff --git a/tools/.gitignore b/tools/.gitignore index b24cfd2..63aba1d 100644 --- a/tools/.gitignore +++ b/tools/.gitignore @@ -4,3 +4,4 @@ kmod-rmmod kmod-lsmod kmod-modprobe kmod-modinfo +kmod diff --git a/tools/kmod.c b/tools/kmod.c new file mode 100644 index 0000000..ec0f573 --- /dev/null +++ b/tools/kmod.c @@ -0,0 +1,84 @@ +/* + * kmod - one tool to rule them all + * + * Copyright (C) 2011 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 . + */ + +#include +#include +#include +#include +#include +#include "kmod.h" + +static const struct kmod_cmd kmod_cmd_help; + +static const struct kmod_cmd *kmod_cmds[] = { + &kmod_cmd_help, +}; + +static int kmod_help(int argc, char *argv[]) +{ + size_t i; + + puts("Manage kernel modules: list, load, unload, etc\n" + "Usage: kmod COMMAND [COMMAND_OPTIONS]\n\n" + "Available commands:"); + + for (i = 0; i < ARRAY_SIZE(kmod_cmds); i++) { + if (kmod_cmds[i]->help != NULL) { + printf(" %-12s %s\n", kmod_cmds[i]->name, + kmod_cmds[i]->help); + } + } + + return EXIT_SUCCESS; +} + +static const struct kmod_cmd kmod_cmd_help = { + .name = "help", + .cmd = kmod_help, + .help = "Show help message", +}; + +int main(int argc, char *argv[]) +{ + const char *cmd; + int err = 0; + size_t i; + + if (argc < 2) { + err = -ENOENT; + goto finish; + } + + cmd = argv[1]; + + for (i = 0; i < ARRAY_SIZE(kmod_cmds); i++) { + if (strcmp(kmod_cmds[i]->name, cmd) != 0) + continue; + + err = kmod_cmds[i]->cmd(--argc, ++argv); + } + +finish: + if (err < 0) { + fputs("missing or unknown command; " + "see 'kmod help' for a list of available commands\n", stderr); + } + + return err; +} diff --git a/tools/kmod.h b/tools/kmod.h new file mode 100644 index 0000000..27cb71a --- /dev/null +++ b/tools/kmod.h @@ -0,0 +1,30 @@ +/* + * kmod - one tool to rule them all + * + * Copyright (C) 2011 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 . + */ + +#ifndef _KMOD_H_ + +#include "macro.h" + +struct kmod_cmd { + const char *name; + int (*cmd)(int argc, char *argv[]); + const char *help; +}; + +#endif -- 2.7.4