tools: add skeleton of kmod tool
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Thu, 22 Dec 2011 04:33:36 +0000 (02:33 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Fri, 23 Dec 2011 05:01:58 +0000 (03:01 -0200)
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
tools/.gitignore
tools/kmod.c [new file with mode: 0644]
tools/kmod.h [new file with mode: 0644]

index 6d598e3..3eaef81 100644 (file)
@@ -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
index b24cfd2..63aba1d 100644 (file)
@@ -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 (file)
index 0000000..ec0f573
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <libkmod.h>
+#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 (file)
index 0000000..27cb71a
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _KMOD_H_
+
+#include "macro.h"
+
+struct kmod_cmd {
+       const char *name;
+       int (*cmd)(int argc, char *argv[]);
+       const char *help;
+};
+
+#endif