]> git.ipfire.org Git - thirdparty/kmod.git/blob - libkmod/libkmod-config.c
API-BREAK: kmod_new() takes a second parameter for configuration directory.
[thirdparty/kmod.git] / libkmod / libkmod-config.c
1 /*
2 * libkmod - interface to kernel module operations
3 *
4 * Copyright (C) 2011 ProFUSION embedded systems
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation version 2.1.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <stdarg.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <dirent.h>
31
32 #include "libkmod.h"
33 #include "libkmod-private.h"
34
35 struct kmod_alias {
36 char *name;
37 char modname[];
38 };
39
40 struct kmod_options {
41 char *options;
42 char modname[];
43 };
44
45 struct kmod_command {
46 char *command;
47 char modname[];
48 };
49
50 const char *kmod_alias_get_name(const struct kmod_list *l) {
51 const struct kmod_alias *alias = l->data;
52 return alias->name;
53 }
54
55 const char *kmod_alias_get_modname(const struct kmod_list *l) {
56 const struct kmod_alias *alias = l->data;
57 return alias->modname;
58 }
59
60 const char *kmod_option_get_options(const struct kmod_list *l) {
61 const struct kmod_options *alias = l->data;
62 return alias->options;
63 }
64
65 const char *kmod_option_get_modname(const struct kmod_list *l) {
66 const struct kmod_options *alias = l->data;
67 return alias->modname;
68 }
69
70 const char *kmod_command_get_command(const struct kmod_list *l) {
71 const struct kmod_command *alias = l->data;
72 return alias->command;
73 }
74
75 const char *kmod_command_get_modname(const struct kmod_list *l) {
76 const struct kmod_command *alias = l->data;
77 return alias->modname;
78 }
79
80 static int kmod_config_add_command(struct kmod_config *config,
81 const char *modname,
82 const char *command,
83 const char *command_name,
84 struct kmod_list **list)
85 {
86 struct kmod_command *cmd;
87 struct kmod_list *l;
88 size_t modnamelen = strlen(modname) + 1;
89 size_t commandlen = strlen(command) + 1;
90
91 DBG(config->ctx, "modname'%s' cmd='%s %s'\n", modname, command_name,
92 command);
93
94 cmd = malloc(sizeof(*cmd) + modnamelen + commandlen);
95 if (cmd == NULL)
96 goto oom_error_init;
97
98 cmd->command = sizeof(*cmd) + modnamelen + (char *)cmd;
99 memcpy(cmd->modname, modname, modnamelen);
100 memcpy(cmd->command, command, commandlen);
101
102 l = kmod_list_append(*list, cmd);
103 if (l == NULL)
104 goto oom_error;
105
106 *list = l;
107 return 0;
108
109 oom_error:
110 free(cmd);
111 oom_error_init:
112 ERR(config->ctx, "out-of-memory\n");
113 return -ENOMEM;
114 }
115
116 static void kmod_config_free_command(struct kmod_config *config,
117 struct kmod_list *l,
118 struct kmod_list **list)
119 {
120 struct kmod_command *cmd = l->data;
121
122 free(cmd);
123 *list = kmod_list_remove(l);
124 }
125
126 static int kmod_config_add_options(struct kmod_config *config,
127 const char *modname, const char *options)
128 {
129 struct kmod_options *opt;
130 struct kmod_list *list;
131 size_t modnamelen = strlen(modname) + 1;
132 size_t optionslen = strlen(options) + 1;
133
134 DBG(config->ctx, "modname'%s' options='%s'\n", modname, options);
135
136 opt = malloc(sizeof(*opt) + modnamelen + optionslen);
137 if (opt == NULL)
138 goto oom_error_init;
139
140 opt->options = sizeof(*opt) + modnamelen + (char *)opt;
141
142 memcpy(opt->modname, modname, modnamelen);
143 memcpy(opt->options, options, optionslen);
144 strchr_replace(opt->options, '\t', ' ');
145
146 list = kmod_list_append(config->options, opt);
147 if (list == NULL)
148 goto oom_error;
149
150 config->options = list;
151 return 0;
152
153 oom_error:
154 free(opt);
155 oom_error_init:
156 ERR(config->ctx, "out-of-memory\n");
157 return -ENOMEM;
158 }
159
160 static void kmod_config_free_options(struct kmod_config *config, struct kmod_list *l)
161 {
162 struct kmod_options *opt = l->data;
163
164 free(opt);
165
166 config->options = kmod_list_remove(l);
167 }
168
169 static int kmod_config_add_alias(struct kmod_config *config,
170 const char *name, const char *modname)
171 {
172 struct kmod_alias *alias;
173 struct kmod_list *list;
174 size_t namelen = strlen(name) + 1, modnamelen = strlen(modname) + 1;
175
176 DBG(config->ctx, "name=%s modname=%s\n", name, modname);
177
178 alias = malloc(sizeof(*alias) + namelen + modnamelen);
179 if (!alias)
180 goto oom_error_init;
181 alias->name = sizeof(*alias) + modnamelen + (char *)alias;
182
183 memcpy(alias->modname, modname, modnamelen);
184 memcpy(alias->name, name, namelen);
185
186 list = kmod_list_append(config->aliases, alias);
187 if (!list)
188 goto oom_error;
189 config->aliases = list;
190 return 0;
191
192 oom_error:
193 free(alias);
194 oom_error_init:
195 ERR(config->ctx, "out-of-memory name=%s modname=%s\n", name, modname);
196 return -ENOMEM;
197 }
198
199 static void kmod_config_free_alias(struct kmod_config *config, struct kmod_list *l)
200 {
201 struct kmod_alias *alias = l->data;
202
203 free(alias);
204
205 config->aliases = kmod_list_remove(l);
206 }
207
208 static int kmod_config_add_blacklist(struct kmod_config *config,
209 const char *modname)
210 {
211 char *p;
212 struct kmod_list *list;
213
214 DBG(config->ctx, "modname=%s\n", modname);
215
216 p = strdup(modname);
217 if (!p)
218 goto oom_error_init;
219
220 list = kmod_list_append(config->blacklists, p);
221 if (!list)
222 goto oom_error;
223 config->blacklists = list;
224 return 0;
225
226 oom_error:
227 free(p);
228 oom_error_init:
229 ERR(config->ctx, "out-of-memory modname=%s\n", modname);
230 return -ENOMEM;
231 }
232
233 static void kmod_config_free_blacklist(struct kmod_config *config,
234 struct kmod_list *l)
235 {
236 free(l->data);
237 config->blacklists = kmod_list_remove(l);
238 }
239
240 /*
241 * Take an fd and own it. It will be closed on return. filename is used only
242 * for debug messages
243 */
244 static int kmod_config_parse(struct kmod_config *config, int fd,
245 const char *filename)
246 {
247 struct kmod_ctx *ctx = config->ctx;
248 char *line;
249 FILE *fp;
250 unsigned int linenum;
251 int err;
252
253 fp = fdopen(fd, "r");
254 if (fp == NULL) {
255 err = -errno;
256 ERR(config->ctx, "fd %d: %m", fd);
257 close(fd);
258 return err;
259 }
260
261 while ((line = getline_wrapped(fp, &linenum)) != NULL) {
262 char *cmd, *saveptr;
263
264 if (line[0] == '\0' || line[0] == '#')
265 goto done_next;
266
267 cmd = strtok_r(line, "\t ", &saveptr);
268 if (cmd == NULL)
269 goto done_next;
270
271 if (streq(cmd, "alias")) {
272 char *alias = strtok_r(NULL, "\t ", &saveptr);
273 char *modname = strtok_r(NULL, "\t ", &saveptr);
274
275 if (alias == NULL || modname == NULL)
276 goto syntax_error;
277
278 kmod_config_add_alias(config,
279 underscores(ctx, alias),
280 underscores(ctx, modname));
281 } else if (streq(cmd, "blacklist")) {
282 char *modname = strtok_r(NULL, "\t ", &saveptr);
283
284 if (modname == NULL)
285 goto syntax_error;
286
287 kmod_config_add_blacklist(config,
288 underscores(ctx, modname));
289 } else if (streq(cmd, "options")) {
290 char *modname = strtok_r(NULL, "\t ", &saveptr);
291
292 if (modname == NULL)
293 goto syntax_error;
294
295 kmod_config_add_options(config,
296 underscores(ctx, modname),
297 strtok_r(NULL, "\0", &saveptr));
298 } else if streq(cmd, "install") {
299 char *modname = strtok_r(NULL, "\t ", &saveptr);
300
301 if (modname == NULL)
302 goto syntax_error;
303
304 kmod_config_add_command(config,
305 underscores(ctx, modname),
306 strtok_r(NULL, "\0", &saveptr),
307 cmd, &config->install_commands);
308 } else if streq(cmd, "remove") {
309 char *modname = strtok_r(NULL, "\t ", &saveptr);
310
311 if (modname == NULL)
312 goto syntax_error;
313
314 kmod_config_add_command(config,
315 underscores(ctx, modname),
316 strtok_r(NULL, "\0", &saveptr),
317 cmd, &config->remove_commands);
318 } else if (streq(cmd, "include")
319 || streq(cmd, "softdep")
320 || streq(cmd, "config")) {
321 INFO(ctx, "%s: command %s not implemented yet\n",
322 filename, cmd);
323 } else {
324 syntax_error:
325 ERR(ctx, "%s line %u: ignoring bad line starting with '%s'\n",
326 filename, linenum, cmd);
327 }
328
329 done_next:
330 free(line);
331 }
332
333 fclose(fp);
334
335 return 0;
336 }
337
338 void kmod_config_free(struct kmod_config *config)
339 {
340 while (config->aliases)
341 kmod_config_free_alias(config, config->aliases);
342
343 while (config->blacklists)
344 kmod_config_free_blacklist(config, config->blacklists);
345
346 while (config->options)
347 kmod_config_free_options(config, config->options);
348
349 while (config->install_commands) {
350 kmod_config_free_command(config, config->install_commands,
351 &config->install_commands);
352 }
353
354 while (config->remove_commands) {
355 kmod_config_free_command(config, config->remove_commands,
356 &config->remove_commands);
357 }
358
359 free(config);
360 }
361
362 static bool conf_files_filter_out(struct kmod_ctx *ctx, const char *path,
363 const char *fn)
364 {
365 size_t len = strlen(fn);
366
367 if (fn[0] == '.')
368 return 1;
369
370 if (len < 6 || (!streq(&fn[len - 5], ".conf")
371 && !streq(&fn[len - 6], ".alias"))) {
372 INFO(ctx, "All config files need .conf: %s/%s, "
373 "it will be ignored in a future release\n",
374 path, fn);
375 return 1;
376 }
377
378 return 0;
379 }
380
381 static DIR *conf_files_list(struct kmod_ctx *ctx, struct kmod_list **list,
382 const char *path)
383 {
384 struct stat st;
385 DIR *d;
386 int err;
387
388 if (stat(path, &st) < 0)
389 return NULL;
390
391 if (!S_ISDIR(st.st_mode)) {
392 *list = kmod_list_append(*list, path);
393 return NULL;
394 }
395
396 d = opendir(path);
397 if (d == NULL) {
398 err = errno;
399 ERR(ctx, "%m\n");
400 return NULL;
401 }
402
403 for (;;) {
404 struct dirent ent, *entp;
405 struct kmod_list *l, *tmp;
406 const char *dname;
407
408 err = readdir_r(d, &ent, &entp);
409 if (err != 0) {
410 ERR(ctx, "reading entry %s\n", strerror(-err));
411 goto fail_read;
412 }
413
414 if (entp == NULL)
415 break;
416
417 if (conf_files_filter_out(ctx, path, entp->d_name) == 1)
418 continue;
419
420 /* insert sorted */
421 kmod_list_foreach(l, *list) {
422 if (strcmp(entp->d_name, l->data) < 0)
423 break;
424 }
425
426 dname = strdup(entp->d_name);
427 if (dname == NULL)
428 goto fail_oom;
429
430 if (l == NULL)
431 tmp = kmod_list_append(*list, dname);
432 else if (l == *list)
433 tmp = kmod_list_prepend(*list, dname);
434 else
435 tmp = kmod_list_insert_before(l, dname);
436
437 if (tmp == NULL)
438 goto fail_oom;
439
440 if (l == NULL || l == *list)
441 *list = tmp;
442 }
443
444 return d;
445
446 fail_oom:
447 ERR(ctx, "out of memory while scanning '%s'\n", path);
448 fail_read:
449 for (; *list != NULL; *list = kmod_list_remove(*list))
450 free((*list)->data);
451 closedir(d);
452 return NULL;
453 }
454
455 int kmod_config_new(struct kmod_ctx *ctx, struct kmod_config **p_config, const char * const *config_paths)
456 {
457 struct kmod_config *config;
458 size_t i;
459
460 *p_config = config = calloc(1, sizeof(struct kmod_config));
461 if (config == NULL)
462 return -ENOMEM;
463
464 config->ctx = ctx;
465
466 for (i = 0; config_paths[i] != NULL; i++) {
467 const char *path = config_paths[i];
468 struct kmod_list *list = NULL;
469 struct stat st;
470 DIR *d;
471
472 if (stat(path, &st) != 0) {
473 DBG(ctx, "could not load '%s': %s\n",
474 path, strerror(errno));
475 continue;
476 }
477
478 if (S_ISREG(st.st_mode)) {
479 int fd = open(path, O_RDONLY);
480 DBG(ctx, "parsing file '%s': %d\n", path, fd);
481 if (fd >= 0)
482 kmod_config_parse(config, fd, path);
483 continue;
484 } else if (!S_ISDIR(st.st_mode)) {
485 ERR(ctx, "unsupported file mode %s: %#x\n",
486 path, st.st_mode);
487 continue;
488 }
489
490 d = conf_files_list(ctx, &list, path);
491
492 /* there's no entry */
493 if (list == NULL)
494 continue;
495 if (d == NULL) {
496 ERR(ctx, "returned list but no directory?\n");
497 while (list) {
498 free(list->data);
499 kmod_list_remove(list);
500 }
501 continue;
502 }
503
504 /* treat all the entries in that dir */
505 for (; list != NULL; list = kmod_list_remove(list)) {
506 int fd = openat(dirfd(d), list->data, O_RDONLY);
507 DBG(ctx, "parsing file '%s/%s': %d\n", path,
508 (const char *) list->data, fd);
509 if (fd >= 0)
510 kmod_config_parse(config, fd, list->data);
511
512 free(list->data);
513 }
514
515 closedir(d);
516 }
517
518 return 0;
519 }