summaryrefslogtreecommitdiffstats
path: root/tools/static-nodes.c
blob: 5ef3743e967bf2f7d00ad122a9166f363ab66d35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * 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 <errno.h>
#include <getopt.h>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/utsname.h>

#include <shared/util.h>

#include "kmod.h"

struct static_nodes_format {
	const char *name;
	int (*write)(FILE *, char[], char[], char, unsigned int, unsigned int);
	const char *description;
};

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[] = {
	&static_nodes_format_human,
	&static_nodes_format_tmpfiles,
	&static_nodes_format_devname,
};

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 int write_human(FILE *out, char modname[], char devname[], char type, unsigned int maj, unsigned int min)
{
	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;
}

static const struct static_nodes_format static_nodes_format_human = {
	.name = "human",
	.write = write_human,
	.description = "(default) a human readable format. Do not parse.",
};

static int write_tmpfiles(FILE *out, char modname[], char devname[], char type, unsigned int maj, unsigned int min)
{
	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;
	}

	ret = fprintf(out, "%c! /dev/%s 0600 - - - %u:%u\n",
		      type, devname, maj, min);
	if (ret < 0)
		return EXIT_FAILURE;

	return EXIT_SUCCESS;
}

static const struct static_nodes_format static_nodes_format_tmpfiles = {
	.name = "tmpfiles",
	.write = write_tmpfiles,
	.description = "the tmpfiles.d(5) format used by systemd-tmpfiles.",
};

static int write_devname(FILE *out, char modname[], char devname[], char type, unsigned int maj, unsigned int min)
{
	int ret;

	ret = fprintf(out, "%s %s %c%u:%u\n", modname, devname, type, maj, min);
	if (ret >= 0)
		return EXIT_SUCCESS;
	else
		return EXIT_FAILURE;
}

static const struct static_nodes_format static_nodes_format_devname = {
	.name = "devname",
	.write = write_devname,
	.description = "the modules.devname format.",
};

static void help(void)
{
	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"
	       "\t-f, --format=FORMAT  choose format to use: see \"Formats\"\n"
	       "\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);
		}
	}
}

static int do_static_nodes(int argc, char *argv[])
{
	struct utsname kernel;
	char modules[PATH_MAX], buf[4096];
	const char *output = "/dev/stdout";
	FILE *in = NULL, *out = NULL;
	const struct static_nodes_format *format = &static_nodes_format_human;
	int r, ret = EXIT_SUCCESS;

	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':
			output = optarg;
			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;
	}

	snprintf(modules, sizeof(modules), MODULE_DIRECTORY "/%s/modules.devname", kernel.release);
	in = fopen(modules, "re");
	if (in == NULL) {
		if (errno == ENOENT) {
			fprintf(stderr, "Warning: " MODULE_DIRECTORY "/%s/modules.devname not found - ignoring\n",
				kernel.release);
			ret = EXIT_SUCCESS;
		} else {
			fprintf(stderr, "Error: could not open " MODULE_DIRECTORY "/%s/modules.devname - %m\n",
				kernel.release);
			ret = EXIT_FAILURE;
		}
		goto finish;
	}

	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;
	}

	out = fopen(output, "we");
	if (out == NULL) {
		fprintf(stderr, "Error: could not create %s - %m\n", output);
		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);
	}

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 installed with the currently running kernel",
};