2013-04-17 03:39:55 +07:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2014-10-03 11:41:42 +07:00
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stddef.h>
|
2013-04-17 03:39:55 +07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-10-03 11:41:42 +07:00
|
|
|
#include <unistd.h>
|
2013-04-17 03:39:55 +07:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2014-10-03 11:41:42 +07:00
|
|
|
#include <sys/utsname.h>
|
2014-10-03 10:01:35 +07:00
|
|
|
|
|
|
|
#include <shared/util.h>
|
2013-04-17 03:39:55 +07:00
|
|
|
|
|
|
|
#include "kmod.h"
|
|
|
|
|
|
|
|
struct static_nodes_format {
|
2013-04-20 05:08:43 +07:00
|
|
|
const char *name;
|
|
|
|
int (*write)(FILE *, char[], char[], char, unsigned int, unsigned int);
|
|
|
|
const char *description;
|
2013-04-17 03:39:55 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct static_nodes_format static_nodes_format_human;
|
|
|
|
static const struct static_nodes_format static_nodes_format_tmpfiles;
|
|
|
|
static const struct static_nodes_format static_nodes_format_devname;
|
|
|
|
|
|
|
|
static const struct static_nodes_format *static_nodes_formats[] = {
|
2013-04-20 05:08:43 +07:00
|
|
|
&static_nodes_format_human,
|
|
|
|
&static_nodes_format_tmpfiles,
|
|
|
|
&static_nodes_format_devname,
|
2013-04-17 03:39:55 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char cmdopts_s[] = "o:f:h";
|
|
|
|
static const struct option cmdopts[] = {
|
2013-04-20 05:08:43 +07:00
|
|
|
{ "output", required_argument, 0, 'o'},
|
|
|
|
{ "format", required_argument, 0, 'f'},
|
|
|
|
{ "help", no_argument, 0, 'h'},
|
|
|
|
{ },
|
2013-04-17 03:39:55 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
static int write_human(FILE *out, char modname[], char devname[], char type, unsigned int maj, unsigned int min)
|
|
|
|
{
|
2013-04-20 05:08:43 +07:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = fprintf(out,
|
|
|
|
"Module: %s\n"
|
|
|
|
"\tDevice node: /dev/%s\n"
|
|
|
|
"\t\tType: %s device\n"
|
|
|
|
"\t\tMajor: %u\n"
|
|
|
|
"\t\tMinor: %u\n",
|
|
|
|
modname, devname,
|
|
|
|
(type == 'c') ? "character" : "block", maj, min);
|
|
|
|
if (ret >= 0)
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
else
|
|
|
|
return EXIT_FAILURE;
|
2013-04-17 03:39:55 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct static_nodes_format static_nodes_format_human = {
|
|
|
|
.name = "human",
|
|
|
|
.write = write_human,
|
2013-04-20 05:08:43 +07:00
|
|
|
.description = "(default) a human readable format. Do not parse.",
|
2013-04-17 03:39:55 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
static int write_tmpfiles(FILE *out, char modname[], char devname[], char type, unsigned int maj, unsigned int min)
|
|
|
|
{
|
2013-04-20 05:08:43 +07:00
|
|
|
const char *dir;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
dir = strrchr(devname, '/');
|
|
|
|
if (dir) {
|
|
|
|
ret = fprintf(out, "d /dev/%.*s 0755 - - -\n",
|
|
|
|
(int)(dir - devname), devname);
|
|
|
|
if (ret < 0)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-10-27 23:55:03 +07:00
|
|
|
ret = fprintf(out, "%c! /dev/%s 0600 - - - %u:%u\n",
|
2013-04-20 05:08:43 +07:00
|
|
|
type, devname, maj, min);
|
|
|
|
if (ret < 0)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2013-04-17 03:39:55 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct static_nodes_format static_nodes_format_tmpfiles = {
|
|
|
|
.name = "tmpfiles",
|
|
|
|
.write = write_tmpfiles,
|
2013-04-20 05:08:43 +07:00
|
|
|
.description = "the tmpfiles.d(5) format used by systemd-tmpfiles.",
|
2013-04-17 03:39:55 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
static int write_devname(FILE *out, char modname[], char devname[], char type, unsigned int maj, unsigned int min)
|
|
|
|
{
|
2013-04-20 05:08:43 +07:00
|
|
|
int ret;
|
2013-04-17 03:39:55 +07:00
|
|
|
|
2013-04-20 05:08:43 +07:00
|
|
|
ret = fprintf(out, "%s %s %c%u:%u\n", modname, devname, type, maj, min);
|
|
|
|
if (ret >= 0)
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
else
|
|
|
|
return EXIT_FAILURE;
|
2013-04-17 03:39:55 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct static_nodes_format static_nodes_format_devname = {
|
|
|
|
.name = "devname",
|
|
|
|
.write = write_devname,
|
2013-04-20 05:08:43 +07:00
|
|
|
.description = "the modules.devname format.",
|
2013-04-17 03:39:55 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
static void help(void)
|
|
|
|
{
|
2013-04-20 05:08:43 +07:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
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"
|
2013-07-02 09:00:20 +07:00
|
|
|
"\t-f, --format=FORMAT choose format to use: see \"Formats\"\n"
|
2013-04-20 05:08:43 +07:00
|
|
|
"\t-o, --output=FILE write output to file\n"
|
|
|
|
"\t-h, --help show this help\n"
|
|
|
|
"\n"
|
|
|
|
"Formats:\n",
|
|
|
|
program_invocation_short_name);
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(static_nodes_formats); i++) {
|
|
|
|
if (static_nodes_formats[i]->description != NULL) {
|
|
|
|
printf("\t%-12s %s\n", static_nodes_formats[i]->name,
|
|
|
|
static_nodes_formats[i]->description);
|
|
|
|
}
|
|
|
|
}
|
2013-04-17 03:39:55 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int do_static_nodes(int argc, char *argv[])
|
|
|
|
{
|
2013-04-20 05:08:43 +07:00
|
|
|
struct utsname kernel;
|
2013-07-14 20:13:34 +07:00
|
|
|
char modules[PATH_MAX], buf[4096];
|
2013-07-14 20:13:33 +07:00
|
|
|
const char *output = "/dev/stdout";
|
|
|
|
FILE *in = NULL, *out = NULL;
|
2013-04-20 05:08:43 +07:00
|
|
|
const struct static_nodes_format *format = &static_nodes_format_human;
|
2013-07-14 20:13:34 +07:00
|
|
|
int r, ret = EXIT_SUCCESS;
|
2013-04-20 05:08:43 +07:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int c, idx = 0, valid;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
|
|
|
|
if (c == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (c) {
|
|
|
|
case 'o':
|
2013-07-14 20:13:33 +07:00
|
|
|
output = optarg;
|
2013-04-20 05:08:43 +07:00
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
valid = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(static_nodes_formats); i++) {
|
|
|
|
if (streq(static_nodes_formats[i]->name, optarg)) {
|
|
|
|
format = static_nodes_formats[i];
|
|
|
|
valid = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!valid) {
|
|
|
|
fprintf(stderr, "Unknown format: '%s'.\n",
|
|
|
|
optarg);
|
|
|
|
help();
|
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-11-10 19:13:54 +07:00
|
|
|
snprintf(modules, sizeof(modules), MODULE_DIRECTORY "/%s/modules.devname", kernel.release);
|
2013-04-20 05:08:43 +07:00
|
|
|
in = fopen(modules, "re");
|
|
|
|
if (in == NULL) {
|
2013-07-14 20:13:33 +07:00
|
|
|
if (errno == ENOENT) {
|
2023-11-10 19:13:54 +07:00
|
|
|
fprintf(stderr, "Warning: " MODULE_DIRECTORY "/%s/modules.devname not found - ignoring\n",
|
2013-07-14 20:13:33 +07:00
|
|
|
kernel.release);
|
|
|
|
ret = EXIT_SUCCESS;
|
|
|
|
} else {
|
2023-11-10 19:13:54 +07:00
|
|
|
fprintf(stderr, "Error: could not open " MODULE_DIRECTORY "/%s/modules.devname - %m\n",
|
2013-07-14 20:13:33 +07:00
|
|
|
kernel.release);
|
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2013-07-14 20:13:34 +07:00
|
|
|
r = mkdir_parents(output, 0755);
|
|
|
|
if (r < 0) {
|
|
|
|
fprintf(stderr, "Error: could not create parent directory for %s - %m.\n", output);
|
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2013-07-14 20:13:33 +07:00
|
|
|
out = fopen(output, "we");
|
|
|
|
if (out == NULL) {
|
|
|
|
fprintf(stderr, "Error: could not create %s - %m\n", output);
|
2013-04-20 05:08:43 +07:00
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets(buf, sizeof(buf), in) != NULL) {
|
|
|
|
char modname[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", modname, devname,
|
|
|
|
&type, &maj, &min);
|
|
|
|
if (matches != 5 || (type != 'c' && type != 'b')) {
|
|
|
|
fprintf(stderr, "Error: invalid devname entry: %s", buf);
|
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
format->write(out, modname, devname, type, maj, min);
|
|
|
|
}
|
2013-04-17 03:39:55 +07:00
|
|
|
|
|
|
|
finish:
|
2013-04-20 05:08:43 +07:00
|
|
|
if (in)
|
|
|
|
fclose(in);
|
|
|
|
if (out)
|
|
|
|
fclose(out);
|
|
|
|
return ret;
|
2013-04-17 03:39:55 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct kmod_cmd kmod_cmd_static_nodes = {
|
|
|
|
.name = "static-nodes",
|
|
|
|
.cmd = do_static_nodes,
|
|
|
|
.help = "outputs the static-node information installed with the currently running kernel",
|
|
|
|
};
|