summaryrefslogtreecommitdiff
path: root/libkmod/libkmod-builtin.c
blob: 65334a8a4e0eed4c608d69178f6ea92ed10016a2 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * libkmod - interface to kernel built-in modules
 *
 * Copyright (C) 2019  Alexey Gladkov <gladkov.alexey@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include <sys/types.h>
#include <sys/stat.h>

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "libkmod.h"
#include "libkmod-internal.h"

#define MODULES_BUILTIN_MODINFO "modules.builtin.modinfo"

struct kmod_builtin_iter {
	struct kmod_ctx *ctx;

	// The file descriptor.
	int file;

	// The total size in bytes.
	ssize_t size;

	// The offset of current module.
	off_t pos;

	// The offset at which the next module is located.
	off_t next;

	// Number of strings in the current block.
	ssize_t nstrings;

	// Internal buffer and its size.
	size_t bufsz;
	char *buf;
};

static struct kmod_builtin_iter *kmod_builtin_iter_new(struct kmod_ctx *ctx)
{
	char path[PATH_MAX];
	int file, sv_errno;
	struct stat sb;
	struct kmod_builtin_iter *iter = NULL;
	const char *dirname = kmod_get_dirname(ctx);
	size_t len = strlen(dirname);

	file = -1;

	if ((len + 1 + strlen(MODULES_BUILTIN_MODINFO) + 1) >= PATH_MAX) {
		sv_errno = ENAMETOOLONG;
		goto fail;
	}

	snprintf(path, PATH_MAX, "%s/%s", dirname, MODULES_BUILTIN_MODINFO);

	file = open(path, O_RDONLY|O_CLOEXEC);
	if (file < 0) {
		sv_errno = errno;
		goto fail;
	}

	if (fstat(file, &sb) < 0) {
		sv_errno = errno;
		goto fail;
	}

	iter = malloc(sizeof(*iter));
	if (!iter) {
		sv_errno = ENOMEM;
		goto fail;
	}

	iter->ctx = ctx;
	iter->file = file;
	iter->size = sb.st_size;
	iter->nstrings = 0;
	iter->pos = 0;
	iter->next = 0;
	iter->bufsz = 0;
	iter->buf = NULL;

	return iter;
fail:
	if (file >= 0)
		close(file);

	errno = sv_errno;

	return iter;
}

static void kmod_builtin_iter_free(struct kmod_builtin_iter *iter)
{
	close(iter->file);
	free(iter->buf);
	free(iter);
}

static off_t get_string(struct kmod_builtin_iter *iter, off_t offset,
			char **line, size_t *size)
{
	int sv_errno;
	char *nullp = NULL;
	size_t linesz = 0;

	while (!nullp) {
		char buf[BUFSIZ];
		ssize_t sz;
		size_t partsz;

		sz = pread(iter->file, buf, BUFSIZ, offset);
		if (sz < 0) {
			sv_errno = errno;
			goto fail;
		} else if (sz == 0) {
			offset = 0;
			break;
		}

		nullp = memchr(buf, '\0', (size_t) sz);
		partsz = (size_t)((nullp) ? (nullp - buf) + 1 : sz);
		offset += (off_t) partsz;

		if (iter->bufsz < linesz + partsz) {
			iter->bufsz = linesz + partsz;
			iter->buf = realloc(iter->buf, iter->bufsz);

			if (!iter->buf) {
				sv_errno = errno;
				goto fail;
			}
		}

		strncpy(iter->buf + linesz, buf, partsz);
		linesz += partsz;
	}

	if (linesz) {
		*line = iter->buf;
		*size = linesz;
	}

	return offset;
fail:
	errno = sv_errno;
	return -1;
}

static bool kmod_builtin_iter_next(struct kmod_builtin_iter *iter)
{
	char *line,  *modname;
	size_t linesz;
	off_t pos, offset, modlen;

	modname = NULL;

	iter->nstrings = 0;
	offset = pos = iter->next;

	while (offset < iter->size) {
		char *dot;
		off_t len;

		offset = get_string(iter, pos, &line, &linesz);
		if (offset <= 0) {
			if (offset)
				ERR(iter->ctx, "get_string: %s\n", strerror(errno));
			pos = iter->size;
			break;
		}

		dot = strchr(line, '.');
		if (!dot) {
			ERR(iter->ctx, "kmod_builtin_iter_next: unexpected string without modname prefix\n");
			pos = iter->size;
			break;
		}

		len = dot - line;

		if (!modname) {
			modname = strdup(line);
			modlen = len;
		} else if (modlen != len || strncmp(modname, line, len)) {
			break;
		}

		iter->nstrings++;
		pos = offset;
	}

	iter->pos = iter->next;
	iter->next = pos;

	free(modname);

	return (iter->pos < iter->size);
}

static bool kmod_builtin_iter_get_modname(struct kmod_builtin_iter *iter,
				char modname[static PATH_MAX])
{
	int sv_errno;
	char *line, *dot;
	size_t linesz, len;
	off_t offset;

	if (iter->pos == iter->size)
		return false;

	line = NULL;

	offset = get_string(iter, iter->pos, &line, &linesz);
	if (offset <= 0) {
		sv_errno = errno;
		if (offset)
			ERR(iter->ctx, "get_string: %s\n", strerror(errno));
		goto fail;
	}

	dot = strchr(line, '.');
	if (!dot) {
		sv_errno = errno;
		ERR(iter->ctx, "kmod_builtin_iter_get_modname: unexpected string without modname prefix\n");
		goto fail;
	}

	len = dot - line;

	if (len >= PATH_MAX) {
		sv_errno = ENAMETOOLONG;
		goto fail;
	}

	strncpy(modname, line, len);
	modname[len] = '\0';

	return true;
fail:
	errno = sv_errno;
	return false;
}

/* array will be allocated with strings in a single malloc, just free *array */
ssize_t kmod_builtin_get_modinfo(struct kmod_ctx *ctx, const char *modname,
				char ***modinfo)
{
	ssize_t count = 0;
	char *s, *line = NULL;
	size_t i, n, linesz, modlen, size;
	off_t pos, offset;

	char *name = NULL;
	char buf[PATH_MAX];

	struct kmod_builtin_iter *iter = kmod_builtin_iter_new(ctx);

	if (!iter)
		return -errno;

	while (!name && kmod_builtin_iter_next(iter)) {
		if (!kmod_builtin_iter_get_modname(iter, buf)) {
			count = -errno;
			goto fail;
		}

		if (strcmp(modname, buf))
			continue;

		name = buf;
	}

	if (!name) {
		count = -ENOSYS;
		goto fail;
	}

	modlen = strlen(modname) + 1;
	count = iter->nstrings;
	size = iter->next - iter->pos - (modlen * count);

	*modinfo = malloc(size + sizeof(char *) * (count + 1));
	if (!*modinfo) {
		count = -errno;
		goto fail;
	}

	s = (char *)(*modinfo + count + 1);
	i = 0;

	n = 0;
	offset = pos = iter->pos;

	while (offset < iter->next) {
		offset = get_string(iter, pos, &line, &linesz);
		if (offset <= 0) {
			count = (offset) ? -errno : -EINVAL;
			free(*modinfo);
			goto fail;
		}

		strcpy(s + i, line + modlen);
		(*modinfo)[n++] = s + i;
		i += linesz - modlen;

		pos = offset;
	}
fail:
	kmod_builtin_iter_free(iter);
	return count;
}