* [PATCH v3] tools: add static-nodes tool
@ 2013-04-16 13:12 Tom Gundersen
2013-04-16 13:49 ` Thomas Bächler
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Tom Gundersen @ 2013-04-16 13:12 UTC (permalink / raw)
To: linux-hotplug
This tool reads modules.devname from the current kernel directory and outputs
the information. By default in a human-readable format, and optionally in
machine-readable formats.
For now only the tmpfiles.d(5) format is supported, but more could easily be
added in the future if there is a need.
This means nothing but kmod needs to reads the private files under
/lib/modules/. In particular systemd-udevd can stop reading modules.devname.
Cc: <linux-hotplug@vger.kernel.org>
Cc: <systemd-devel@lists.freedesktop.org>tools: static-nodes
---
v3: dropped the systemd integration for now, we can decide on that separately
added human-readable format and use this by default
added a --format= switch to get the tmpfiles format
Makefile.am | 3 +-
tools/kmod.c | 1 +
tools/kmod.h | 1 +
tools/static-nodes.c | 176 +++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 180 insertions(+), 1 deletion(-)
create mode 100644 tools/static-nodes.c
diff --git a/Makefile.am b/Makefile.am
index fe4c769..b1bfd59 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -110,7 +110,8 @@ noinst_SCRIPTS = tools/insmod tools/rmmod tools/lsmod \
tools_kmod_SOURCES = tools/kmod.c tools/kmod.h tools/lsmod.c \
tools/rmmod.c tools/insmod.c \
tools/modinfo.c tools/modprobe.c \
- tools/depmod.c tools/log.h tools/log.c
+ tools/depmod.c tools/log.h tools/log.c \
+ tools/static-nodes.c
tools_kmod_LDADD = libkmod/libkmod-util.la \
libkmod/libkmod.la
diff --git a/tools/kmod.c b/tools/kmod.c
index ebb8875..347bb7d 100644
--- a/tools/kmod.c
+++ b/tools/kmod.c
@@ -37,6 +37,7 @@ static const struct kmod_cmd kmod_cmd_help;
static const struct kmod_cmd *kmod_cmds[] = {
&kmod_cmd_help,
&kmod_cmd_list,
+ &kmod_cmd_static_nodes,
};
static const struct kmod_cmd *kmod_compat_cmds[] = {
diff --git a/tools/kmod.h b/tools/kmod.h
index 80fa4c2..68a646a 100644
--- a/tools/kmod.h
+++ b/tools/kmod.h
@@ -35,5 +35,6 @@ 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_list;
+extern const struct kmod_cmd kmod_cmd_static_nodes;
#include "log.h"
diff --git a/tools/static-nodes.c b/tools/static-nodes.c
new file mode 100644
index 0000000..3d0582a
--- /dev/null
+++ b/tools/static-nodes.c
@@ -0,0 +1,176 @@
+/*
+ * kmod-static-nodes - manage modules.devname
+ *
+ * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
+ * Copyright (C) 2011-2013 ProFUSION embedded systems
+ * Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
+ *
+ * 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 <stddef.h>
+#include <getopt.h>
+#include <errno.h>
+#include <unistd.h>
+#include <string.h>
+#include <limits.h>
+#include <sys/utsname.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include "libkmod-util.h"
+
+#include "kmod.h"
+
+static const char cmdopts_s[] = "o:f:h";
+static const struct option cmdopts[] = {
+ { "output", required_argument, 0, 'o'},
+ { "format", required_argument, 0, 'f'},
+ { "help", no_argument, 0, 'h'},
+ { },
+};
+
+static void help(void)
+{
+ printf("Usage:\n"
+ "\t%s static-nodes [options]\n"
+ "\n"
+ "kmod static-nodes outputs the static-node information of the currently running kernel.\n"
+ "\n"
+ "Options:\n"
+ "\t-f, --format=FORMAT use a machine-readable format\n"
+ "\t-o, --output=FILE write output to file\n"
+ "\t-h, --help show this help\n"
+ "\n"
+ "Formats:\n"
+ " tmpfiles the tmpfiles.d(5) format used by systemd-tmpfiles.\n",
+ program_invocation_short_name);
+}
+
+static void write_human(FILE *out, char module[], char devname[], char type, unsigned int maj, unsigned int min)
+{
+ fprintf(out,
+ "Module: %s\n"
+ "\tDevice node: /dev/%s\n"
+ "\t\tType: %s device\n"
+ "\t\tMajor: %u\n"
+ "\t\tMinor: %u\n",
+ module, devname, (type = 'c') ? "character" : "block", maj, min);
+ return;
+}
+
+static void write_tmpfile(FILE *out, char devname[], char type, unsigned int maj, unsigned int min)
+{
+ fprintf(out, "%c /dev/%s 0600 - - - %u:%u\n", type, devname, maj, min);
+ return;
+}
+
+static int do_static_nodes(int argc, char *argv[])
+{
+ struct utsname kernel;
+ char modules[PATH_MAX];
+ FILE *in = NULL, *out = stdout;
+ bool human_readable = 1;
+ char buf[4096];
+ int ret = EXIT_SUCCESS;
+
+ for (;;) {
+ int c, idx = 0;
+
+ c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
+ if (c = -1) {
+ break;
+ }
+ switch (c) {
+ case 'o':
+ out = fopen(optarg, "we");
+ if (out = NULL) {
+ fprintf(stderr, "Error: could not create %s!\n", optarg);
+ ret = EXIT_FAILURE;
+ goto finish;
+ }
+ break;
+ case 'f':
+ if (!streq(optarg, "tmpfiles")) {
+ fprintf(stderr, "Unknown format: '%s'.\n", argv[1]);
+ help();
+ ret = EXIT_FAILURE;
+ goto finish;
+ }
+ human_readable = 0;
+ break;
+ case 'h':
+ help();
+ goto finish;
+ case '?':
+ ret = EXIT_FAILURE;
+ goto finish;
+ default:
+ fprintf(stderr, "Unexpected commandline option '%c'.\n", c);
+ help();
+ ret = EXIT_FAILURE;
+ goto finish;
+ }
+ }
+
+ if (uname(&kernel) < 0) {
+ fputs("Error: uname failed!\n", stderr);
+ ret = EXIT_FAILURE;
+ goto finish;
+ }
+ snprintf(modules, sizeof(modules), "/lib/modules/%s/modules.devname", kernel.release);
+ in = fopen(modules, "re");
+ if (in = NULL && errno != ENOENT) {
+ fprintf(stderr, "Error: could not open /lib/modules/%s/modules.devname!\n", kernel.release);
+ ret = EXIT_FAILURE;
+ goto finish;
+ }
+
+ while (fgets(buf, sizeof(buf), in) != NULL) {
+ char module[PATH_MAX];
+ char devname[PATH_MAX];
+ char type;
+ unsigned int maj, min;
+ int matches;
+
+ if (buf[0] = '#')
+ continue;
+
+ matches = sscanf(buf, "%s %s %c%u:%u", module, devname, &type, &maj, &min);
+ if (matches != 5 || (type != 'c' && type != 'b')) {
+ fprintf(stderr, "Error: invalid devname entry: %s", buf);
+ ret = EXIT_FAILURE;
+ continue;
+ }
+
+ if (human_readable)
+ write_human(out, module, devname, type, maj, min);
+ else
+ write_tmpfile(out, devname, type, maj, min);
+ }
+
+finish:
+ if (in)
+ fclose(in);
+ if (out)
+ fclose(out);
+ return ret;
+}
+
+const struct kmod_cmd kmod_cmd_static_nodes = {
+ .name = "static-nodes",
+ .cmd = do_static_nodes,
+ .help = "outputs the static-node information of the currently running kernel",
+};
--
1.8.2.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v3] tools: add static-nodes tool
2013-04-16 13:12 [PATCH v3] tools: add static-nodes tool Tom Gundersen
@ 2013-04-16 13:49 ` Thomas Bächler
2013-04-16 18:51 ` Lucas De Marchi
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Thomas Bächler @ 2013-04-16 13:49 UTC (permalink / raw)
To: linux-hotplug
[-- Attachment #1: Type: text/plain, Size: 1971 bytes --]
Am 16.04.2013 15:12, schrieb Tom Gundersen:
> +static void write_human(FILE *out, char module[], char devname[], char type, unsigned int maj, unsigned int min)
[...]
> +static void write_tmpfile(FILE *out, char devname[], char type, unsigned int maj, unsigned int min)
[...]
> +static int do_static_nodes(int argc, char *argv[])
> +{
> + struct utsname kernel;
> + char modules[PATH_MAX];
> + FILE *in = NULL, *out = stdout;
> + bool human_readable = 1;
This code emphasizes that there is actually only one format available
and needs to be changed again when another one is added. Why not
void (*write_output)((FILE *, char[], char[], char, unsigned int,
unsigned int) = write_human;
? Then ...
> + case 'f':
> + if (!streq(optarg, "tmpfiles")) {
> + fprintf(stderr, "Unknown format: '%s'.\n", argv[1]);
> + help();
> + ret = EXIT_FAILURE;
> + goto finish;
> + }
> + human_readable = 0;
> + break;
case 'f':
if (streq(optarg, "tmpfiles")) {
write_output = write_tmpfiles;
}
else {
fprintf(stderr, "Unknown format: '%s'.\n", argv[1]);
[...]
}
break;
And in the end:
> + if (human_readable)
> + write_human(out, module, devname, type, maj, min);
> + else
> + write_tmpfile(out, devname, type, maj, min);
write_output(out, module, devname, type, maj, min);
Maybe even add an array with output name and function pointer pairs, so
that we could get a list of available formats using --format=?. For
consistency, --format=human should also work. Just seems nicer to me, in
case someone actually plans to extend this later.
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 901 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v3] tools: add static-nodes tool
2013-04-16 13:12 [PATCH v3] tools: add static-nodes tool Tom Gundersen
2013-04-16 13:49 ` Thomas Bächler
@ 2013-04-16 18:51 ` Lucas De Marchi
2013-04-16 19:08 ` Kay Sievers
2013-04-16 20:32 ` Tom Gundersen
3 siblings, 0 replies; 5+ messages in thread
From: Lucas De Marchi @ 2013-04-16 18:51 UTC (permalink / raw)
To: linux-hotplug
On Tue, Apr 16, 2013 at 10:49 AM, Thomas Bächler <thomas@archlinux.org> wrote:
> Am 16.04.2013 15:12, schrieb Tom Gundersen:
>> +static void write_human(FILE *out, char module[], char devname[], char type, unsigned int maj, unsigned int min)
>
> [...]
>
>> +static void write_tmpfile(FILE *out, char devname[], char type, unsigned int maj, unsigned int min)
>
> [...]
>
>> +static int do_static_nodes(int argc, char *argv[])
>> +{
>> + struct utsname kernel;
>> + char modules[PATH_MAX];
>> + FILE *in = NULL, *out = stdout;
>> + bool human_readable = 1;
>
> This code emphasizes that there is actually only one format available
> and needs to be changed again when another one is added. Why not
>
> void (*write_output)((FILE *, char[], char[], char, unsigned int,
> unsigned int) = write_human;
>
> ? Then ...
>
>> + case 'f':
>> + if (!streq(optarg, "tmpfiles")) {
>> + fprintf(stderr, "Unknown format: '%s'.\n", argv[1]);
>> + help();
>> + ret = EXIT_FAILURE;
>> + goto finish;
>> + }
>> + human_readable = 0;
>> + break;
>
> case 'f':
> if (streq(optarg, "tmpfiles")) {
> write_output = write_tmpfiles;
> }
> else {
> fprintf(stderr, "Unknown format: '%s'.\n", argv[1]);
> [...]
> }
> break;
>
> And in the end:
>
>> + if (human_readable)
>> + write_human(out, module, devname, type, maj, min);
>> + else
>> + write_tmpfile(out, devname, type, maj, min);
>
> write_output(out, module, devname, type, maj, min);
>
> Maybe even add an array with output name and function pointer pairs, so
> that we could get a list of available formats using --format=?. For
> consistency, --format=human should also work. Just seems nicer to me, in
> case someone actually plans to extend this later.
>
Agree. Otherwise looks good.
Kay, what's your opinion regarding this command? Is this sufficient to
drop CAP_MKNOD from udev?
Lucas De Marchi
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v3] tools: add static-nodes tool
2013-04-16 13:12 [PATCH v3] tools: add static-nodes tool Tom Gundersen
2013-04-16 13:49 ` Thomas Bächler
2013-04-16 18:51 ` Lucas De Marchi
@ 2013-04-16 19:08 ` Kay Sievers
2013-04-16 20:32 ` Tom Gundersen
3 siblings, 0 replies; 5+ messages in thread
From: Kay Sievers @ 2013-04-16 19:08 UTC (permalink / raw)
To: linux-hotplug
On Tue, Apr 16, 2013 at 8:51 PM, Lucas De Marchi
<lucas.demarchi@profusion.mobi> wrote:
> Kay, what's your opinion regarding this command? Is this sufficient to
> drop CAP_MKNOD from udev?
Sounds all good to me.
Kay
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v3] tools: add static-nodes tool
2013-04-16 13:12 [PATCH v3] tools: add static-nodes tool Tom Gundersen
` (2 preceding siblings ...)
2013-04-16 19:08 ` Kay Sievers
@ 2013-04-16 20:32 ` Tom Gundersen
3 siblings, 0 replies; 5+ messages in thread
From: Tom Gundersen @ 2013-04-16 20:32 UTC (permalink / raw)
To: linux-hotplug
On Tue, Apr 16, 2013 at 3:49 PM, Thomas Bächler <thomas@archlinux.org> wrote:
> Am 16.04.2013 15:12, schrieb Tom Gundersen:
>> +static void write_human(FILE *out, char module[], char devname[], char type, unsigned int maj, unsigned int min)
>
> [...]
>
>> +static void write_tmpfile(FILE *out, char devname[], char type, unsigned int maj, unsigned int min)
>
> [...]
>
>> +static int do_static_nodes(int argc, char *argv[])
>> +{
>> + struct utsname kernel;
>> + char modules[PATH_MAX];
>> + FILE *in = NULL, *out = stdout;
>> + bool human_readable = 1;
>
> This code emphasizes that there is actually only one format available
> and needs to be changed again when another one is added. Why not
>
> void (*write_output)((FILE *, char[], char[], char, unsigned int,
> unsigned int) = write_human;
>
> ? Then ...
>
>> + case 'f':
>> + if (!streq(optarg, "tmpfiles")) {
>> + fprintf(stderr, "Unknown format: '%s'.\n", argv[1]);
>> + help();
>> + ret = EXIT_FAILURE;
>> + goto finish;
>> + }
>> + human_readable = 0;
>> + break;
>
> case 'f':
> if (streq(optarg, "tmpfiles")) {
> write_output = write_tmpfiles;
> }
> else {
> fprintf(stderr, "Unknown format: '%s'.\n", argv[1]);
> [...]
> }
> break;
>
> And in the end:
>
>> + if (human_readable)
>> + write_human(out, module, devname, type, maj, min);
>> + else
>> + write_tmpfile(out, devname, type, maj, min);
>
> write_output(out, module, devname, type, maj, min);
>
> Maybe even add an array with output name and function pointer pairs, so
> that we could get a list of available formats using --format=?. For
> consistency, --format=human should also work. Just seems nicer to me, in
> case someone actually plans to extend this later.
Thanks for the suggestions Thomas. I implemented most of them, so now
it should be a lot simpler to extend this in the future.
The only thing I skipped was the --format=? idea, as this info is
already in the help output, and I'm not sure how useful it is to
parse. That said, it could easily be added follow-up patch if there is
a need for it.
-t
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2013-04-16 20:32 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-04-16 13:12 [PATCH v3] tools: add static-nodes tool Tom Gundersen
2013-04-16 13:49 ` Thomas Bächler
2013-04-16 18:51 ` Lucas De Marchi
2013-04-16 19:08 ` Kay Sievers
2013-04-16 20:32 ` Tom Gundersen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).