2 * kmod-static-nodes - manage modules.devname
4 * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
5 * Copyright (C) 2011-2013 ProFUSION embedded systems
6 * Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include <sys/types.h>
32 #include <sys/utsname.h>
34 #include <shared/util.h>
38 struct static_nodes_format {
40 int (*write)(FILE *, char[], char[], char, unsigned int, unsigned int);
41 const char *description;
44 static const struct static_nodes_format static_nodes_format_human;
45 static const struct static_nodes_format static_nodes_format_tmpfiles;
46 static const struct static_nodes_format static_nodes_format_devname;
48 static const struct static_nodes_format *static_nodes_formats[] = {
49 &static_nodes_format_human,
50 &static_nodes_format_tmpfiles,
51 &static_nodes_format_devname,
54 static const char cmdopts_s[] = "o:f:h";
55 static const struct option cmdopts[] = {
56 { "output", required_argument, 0, 'o'},
57 { "format", required_argument, 0, 'f'},
58 { "help", no_argument, 0, 'h'},
62 static int write_human(FILE *out, char modname[], char devname[], char type, unsigned int maj, unsigned int min)
68 "\tDevice node: /dev/%s\n"
69 "\t\tType: %s device\n"
73 (type == 'c') ? "character" : "block", maj, min);
80 static const struct static_nodes_format static_nodes_format_human = {
83 .description = "(default) a human readable format. Do not parse.",
86 static int write_tmpfiles(FILE *out, char modname[], char devname[], char type, unsigned int maj, unsigned int min)
91 dir = strrchr(devname, '/');
93 ret = fprintf(out, "d /dev/%.*s 0755 - - -\n",
94 (int)(dir - devname), devname);
99 ret = fprintf(out, "%c! /dev/%s 0600 - - - %u:%u\n",
100 type, devname, maj, min);
107 static const struct static_nodes_format static_nodes_format_tmpfiles = {
109 .write = write_tmpfiles,
110 .description = "the tmpfiles.d(5) format used by systemd-tmpfiles.",
113 static int write_devname(FILE *out, char modname[], char devname[], char type, unsigned int maj, unsigned int min)
117 ret = fprintf(out, "%s %s %c%u:%u\n", modname, devname, type, maj, min);
124 static const struct static_nodes_format static_nodes_format_devname = {
126 .write = write_devname,
127 .description = "the modules.devname format.",
130 static void help(void)
135 "\t%s static-nodes [options]\n"
137 "kmod static-nodes outputs the static-node information of the currently running kernel.\n"
140 "\t-f, --format=FORMAT choose format to use: see \"Formats\"\n"
141 "\t-o, --output=FILE write output to file\n"
142 "\t-h, --help show this help\n"
145 program_invocation_short_name);
147 for (i = 0; i < ARRAY_SIZE(static_nodes_formats); i++) {
148 if (static_nodes_formats[i]->description != NULL) {
149 printf("\t%-12s %s\n", static_nodes_formats[i]->name,
150 static_nodes_formats[i]->description);
155 static int do_static_nodes(int argc, char *argv[])
157 struct utsname kernel;
158 char modules[PATH_MAX], buf[4096];
159 const char *output = "/dev/stdout";
160 FILE *in = NULL, *out = NULL;
161 const struct static_nodes_format *format = &static_nodes_format_human;
162 int r, ret = EXIT_SUCCESS;
165 int c, idx = 0, valid;
168 c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
179 for (i = 0; i < ARRAY_SIZE(static_nodes_formats); i++) {
180 if (streq(static_nodes_formats[i]->name, optarg)) {
181 format = static_nodes_formats[i];
187 fprintf(stderr, "Unknown format: '%s'.\n",
201 fprintf(stderr, "Unexpected commandline option '%c'.\n",
209 if (uname(&kernel) < 0) {
210 fputs("Error: uname failed!\n", stderr);
215 snprintf(modules, sizeof(modules), "/lib/modules/%s/modules.devname", kernel.release);
216 in = fopen(modules, "re");
218 if (errno == ENOENT) {
219 fprintf(stderr, "Warning: /lib/modules/%s/modules.devname not found - ignoring\n",
223 fprintf(stderr, "Error: could not open /lib/modules/%s/modules.devname - %m\n",
230 r = mkdir_parents(output, 0755);
232 fprintf(stderr, "Error: could not create parent directory for %s - %m.\n", output);
237 out = fopen(output, "we");
239 fprintf(stderr, "Error: could not create %s - %m\n", output);
244 while (fgets(buf, sizeof(buf), in) != NULL) {
245 char modname[PATH_MAX];
246 char devname[PATH_MAX];
248 unsigned int maj, min;
254 matches = sscanf(buf, "%s %s %c%u:%u", modname, devname,
256 if (matches != 5 || (type != 'c' && type != 'b')) {
257 fprintf(stderr, "Error: invalid devname entry: %s", buf);
262 format->write(out, modname, devname, type, maj, min);
273 const struct kmod_cmd kmod_cmd_static_nodes = {
274 .name = "static-nodes",
275 .cmd = do_static_nodes,
276 .help = "outputs the static-node information installed with the currently running kernel",