~registry/kmod/master

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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
 * libkmod - interface to kernel module operations
 *
 * Copyright (C) 2011  ProFUSION embedded systems
 *
 * 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 version 2.1.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>

#include "libkmod.h"
#include "libkmod-private.h"

struct kmod_alias {
	char *name;
	char modname[];
};

struct kmod_options {
	char *options;
	char modname[];
};

struct kmod_command {
	char *command;
	char modname[];
};

const char *kmod_alias_get_name(const struct kmod_list *l) {
	const struct kmod_alias *alias = l->data;
	return alias->name;
}

const char *kmod_alias_get_modname(const struct kmod_list *l) {
	const struct kmod_alias *alias = l->data;
	return alias->modname;
}

const char *kmod_option_get_options(const struct kmod_list *l) {
	const struct kmod_options *alias = l->data;
	return alias->options;
}

const char *kmod_option_get_modname(const struct kmod_list *l) {
	const struct kmod_options *alias = l->data;
	return alias->modname;
}

const char *kmod_command_get_command(const struct kmod_list *l) {
	const struct kmod_command *alias = l->data;
	return alias->command;
}

const char *kmod_command_get_modname(const struct kmod_list *l) {
	const struct kmod_command *alias = l->data;
	return alias->modname;
}

static int kmod_config_add_command(struct kmod_config *config,
						const char *modname,
						const char *command,
						const char *command_name,
						struct kmod_list **list)
{
	struct kmod_command *cmd;
	struct kmod_list *l;
	size_t modnamelen = strlen(modname) + 1;
	size_t commandlen = strlen(command) + 1;

	DBG(config->ctx, "modname'%s' cmd='%s %s'\n", modname, command_name,
								command);

	cmd = malloc(sizeof(*cmd) + modnamelen + commandlen);
	if (cmd == NULL)
		goto oom_error_init;

	cmd->command = sizeof(*cmd) + modnamelen + (char *)cmd;
	memcpy(cmd->modname, modname, modnamelen);
	memcpy(cmd->command, command, commandlen);

	l = kmod_list_append(*list, cmd);
	if (l == NULL)
		goto oom_error;

	*list = l;
	return 0;

oom_error:
	free(cmd);
oom_error_init:
	ERR(config->ctx, "out-of-memory\n");
	return -ENOMEM;
}

static void kmod_config_free_command(struct kmod_config *config,
					struct kmod_list *l,
					struct kmod_list **list)
{
	struct kmod_command *cmd = l->data;

	free(cmd);
	*list = kmod_list_remove(l);
}

static int kmod_config_add_options(struct kmod_config *config,
				const char *modname, const char *options)
{
	struct kmod_options *opt;
	struct kmod_list *list;
	size_t modnamelen = strlen(modname) + 1;
	size_t optionslen = strlen(options) + 1;

	DBG(config->ctx, "modname'%s' options='%s'\n", modname, options);

	opt = malloc(sizeof(*opt) + modnamelen + optionslen);
	if (opt == NULL)
		goto oom_error_init;

	opt->options = sizeof(*opt) + modnamelen + (char *)opt;

	memcpy(opt->modname, modname, modnamelen);
	memcpy(opt->options, options, optionslen);
	strchr_replace(opt->options, '\t', ' ');

	list = kmod_list_append(config->options, opt);
	if (list == NULL)
		goto oom_error;

	config->options = list;
	return 0;

oom_error:
	free(opt);
oom_error_init:
	ERR(config->ctx, "out-of-memory\n");
	return -ENOMEM;
}

static void kmod_config_free_options(struct kmod_config *config, struct kmod_list *l)
{
	struct kmod_options *opt = l->data;

	free(opt);

	config->options = kmod_list_remove(l);
}

static int kmod_config_add_alias(struct kmod_config *config,
				const char *name, const char *modname)
{
	struct kmod_alias *alias;
	struct kmod_list *list;
	size_t namelen = strlen(name) + 1, modnamelen = strlen(modname) + 1;

	DBG(config->ctx, "name=%s modname=%s\n", name, modname);

	alias = malloc(sizeof(*alias) + namelen + modnamelen);
	if (!alias)
		goto oom_error_init;
	alias->name = sizeof(*alias) + modnamelen + (char *)alias;

	memcpy(alias->modname, modname, modnamelen);
	memcpy(alias->name, name, namelen);

	list = kmod_list_append(config->aliases, alias);
	if (!list)
		goto oom_error;
	config->aliases = list;
	return 0;

oom_error:
	free(alias);
oom_error_init:
	ERR(config->ctx, "out-of-memory name=%s modname=%s\n", name, modname);
	return -ENOMEM;
}

static void kmod_config_free_alias(struct kmod_config *config, struct kmod_list *l)
{
	struct kmod_alias *alias = l->data;

	free(alias);

	config->aliases = kmod_list_remove(l);
}

static int kmod_config_add_blacklist(struct kmod_config *config,
					const char *modname)
{
	char *p;
	struct kmod_list *list;

	DBG(config->ctx, "modname=%s\n", modname);

	p = strdup(modname);
	if (!p)
		goto oom_error_init;

	list = kmod_list_append(config->blacklists, p);
	if (!list)
		goto oom_error;
	config->blacklists = list;
	return 0;

oom_error:
	free(p);
oom_error_init:
	ERR(config->ctx, "out-of-memory modname=%s\n", modname);
	return -ENOMEM;
}

static void kmod_config_free_blacklist(struct kmod_config *config,
							struct kmod_list *l)
{
	free(l->data);
	config->blacklists = kmod_list_remove(l);
}

/*
 * Take an fd and own it. It will be closed on return. filename is used only
 * for debug messages
 */
static int kmod_config_parse(struct kmod_config *config, int fd,
							const char *filename)
{
	struct kmod_ctx *ctx = config->ctx;
	char *line;
	FILE *fp;
	unsigned int linenum;
	int err;

	fp = fdopen(fd, "r");
	if (fp == NULL) {
		err = -errno;
		ERR(config->ctx, "fd %d: %m", fd);
		close(fd);
		return err;
	}

	while ((line = getline_wrapped(fp, &linenum)) != NULL) {
		char *cmd, *saveptr;

		if (line[0] == '\0' || line[0] == '#')
			goto done_next;

		cmd = strtok_r(line, "\t ", &saveptr);
		if (cmd == NULL)
			goto done_next;

		if (streq(cmd, "alias")) {
			char *alias = strtok_r(NULL, "\t ", &saveptr);
			char *modname = strtok_r(NULL, "\t ", &saveptr);

			if (alias == NULL || modname == NULL)
				goto syntax_error;

			kmod_config_add_alias(config,
						underscores(ctx, alias),
						underscores(ctx, modname));
		} else if (streq(cmd, "blacklist")) {
			char *modname = strtok_r(NULL, "\t ", &saveptr);

			if (modname == NULL)
				goto syntax_error;

			kmod_config_add_blacklist(config,
						underscores(ctx, modname));
		} else if (streq(cmd, "options")) {
			char *modname = strtok_r(NULL, "\t ", &saveptr);

			if (modname == NULL)
				goto syntax_error;

			kmod_config_add_options(config,
						underscores(ctx, modname),
						strtok_r(NULL, "\0", &saveptr));
		} else if streq(cmd, "install") {
			char *modname = strtok_r(NULL, "\t ", &saveptr);

			if (modname == NULL)
				goto syntax_error;

			kmod_config_add_command(config,
					underscores(ctx, modname),
					strtok_r(NULL, "\0", &saveptr),
					cmd, &config->install_commands);
		} else if streq(cmd, "remove") {
			char *modname = strtok_r(NULL, "\t ", &saveptr);

			if (modname == NULL)
				goto syntax_error;

			kmod_config_add_command(config,
					underscores(ctx, modname),
					strtok_r(NULL, "\0", &saveptr),
					cmd, &config->remove_commands);
		} else if (streq(cmd, "include")
				|| streq(cmd, "softdep")
				|| streq(cmd, "config")) {
			INFO(ctx, "%s: command %s not implemented yet\n",
								filename, cmd);
		} else {
syntax_error:
			ERR(ctx, "%s line %u: ignoring bad line starting with '%s'\n",
						filename, linenum, cmd);
		}

done_next:
		free(line);
	}

	fclose(fp);

	return 0;
}

void kmod_config_free(struct kmod_config *config)
{
	while (config->aliases)
		kmod_config_free_alias(config, config->aliases);

	while (config->blacklists)
		kmod_config_free_blacklist(config, config->blacklists);

	while (config->options)
		kmod_config_free_options(config, config->options);

	while (config->install_commands) {
		kmod_config_free_command(config, config->install_commands,
						&config->install_commands);
	}

	while (config->remove_commands) {
		kmod_config_free_command(config, config->remove_commands,
						&config->remove_commands);
	}

	free(config);
}

static bool conf_files_filter_out(struct kmod_ctx *ctx, const char *path,
								const char *fn)
{
	size_t len = strlen(fn);

	if (fn[0] == '.')
		return 1;

	if (len < 6 || (!streq(&fn[len - 5], ".conf")
				&& !streq(&fn[len - 6], ".alias"))) {
		INFO(ctx, "All config files need .conf: %s/%s, "
				"it will be ignored in a future release\n",
				path, fn);
		return 1;
	}

	return 0;
}

static DIR *conf_files_list(struct kmod_ctx *ctx, struct kmod_list **list,
							const char *path)
{
	struct stat st;
	DIR *d;
	int err;

	if (stat(path, &st) < 0)
		return NULL;

	if (!S_ISDIR(st.st_mode)) {
		*list = kmod_list_append(*list, path);
		return NULL;
	}

	d = opendir(path);
	if (d == NULL) {
		err = errno;
		ERR(ctx, "%m\n");
		return NULL;
	}

	for (;;) {
		struct dirent ent, *entp;
		struct kmod_list *l, *tmp;
		const char *dname;

		err = readdir_r(d, &ent, &entp);
		if (err != 0) {
			ERR(ctx, "reading entry %s\n", strerror(-err));
			goto fail_read;
		}

		if (entp == NULL)
			break;

		if (conf_files_filter_out(ctx, path, entp->d_name) == 1)
			continue;

		/* insert sorted */
		kmod_list_foreach(l, *list) {
			if (strcmp(entp->d_name, l->data) < 0)
				break;
		}

		dname = strdup(entp->d_name);
		if (dname == NULL)
			goto fail_oom;

		if (l == NULL)
			tmp = kmod_list_append(*list, dname);
		else if (l == *list)
			tmp = kmod_list_prepend(*list, dname);
		else
			tmp = kmod_list_insert_before(l, dname);

		if (tmp == NULL)
			goto fail_oom;

		if (l == NULL || l == *list)
			*list = tmp;
	}

	return d;

fail_oom:
	ERR(ctx, "out of memory while scanning '%s'\n", path);
fail_read:
	for (; *list != NULL; *list = kmod_list_remove(*list))
		free((*list)->data);
	closedir(d);
	return NULL;
}

int kmod_config_new(struct kmod_ctx *ctx, struct kmod_config **p_config, const char * const *config_paths)
{
	struct kmod_config *config;
	size_t i;

	*p_config = config = calloc(1, sizeof(struct kmod_config));
	if (config == NULL)
		return -ENOMEM;

	config->ctx = ctx;

	for (i = 0; config_paths[i] != NULL; i++) {
		const char *path = config_paths[i];
		struct kmod_list *list = NULL;
		struct stat st;
		DIR *d;

		if (stat(path, &st) != 0) {
			DBG(ctx, "could not load '%s': %s\n",
				path, strerror(errno));
			continue;
		}

		if (S_ISREG(st.st_mode)) {
			int fd = open(path, O_RDONLY);
			DBG(ctx, "parsing file '%s': %d\n", path, fd);
			if (fd >= 0)
				kmod_config_parse(config, fd, path);
			continue;
		} else if (!S_ISDIR(st.st_mode)) {
			ERR(ctx, "unsupported file mode %s: %#x\n",
				path, st.st_mode);
			continue;
		}

		d = conf_files_list(ctx, &list, path);

		/* there's no entry */
		if (list == NULL)
			continue;
		if (d == NULL) {
			ERR(ctx, "returned list but no directory?\n");
			while (list) {
				free(list->data);
				kmod_list_remove(list);
			}
			continue;
		}

		/* treat all the entries in that dir */
		for (; list != NULL; list = kmod_list_remove(list)) {
			int fd = openat(dirfd(d), list->data, O_RDONLY);
			DBG(ctx, "parsing file '%s/%s': %d\n", path,
				(const char *) list->data, fd);
			if (fd >= 0)
				kmod_config_parse(config, fd, list->data);

			free(list->data);
		}

		closedir(d);
	}

	return 0;
}