]> git.ipfire.org Git - thirdparty/kmod.git/blob - libkmod/libkmod-config.c
depmod: add missing "else" clause
[thirdparty/kmod.git] / libkmod / libkmod-config.c
1 /*
2 * libkmod - interface to kernel module operations
3 *
4 * Copyright (C) 2011-2013 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; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stddef.h>
24 #include <stdarg.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <dirent.h>
32
33 #include "libkmod.h"
34 #include "libkmod-internal.h"
35
36 struct kmod_alias {
37 char *name;
38 char modname[];
39 };
40
41 struct kmod_options {
42 char *options;
43 char modname[];
44 };
45
46 struct kmod_command {
47 char *command;
48 char modname[];
49 };
50
51 struct kmod_softdep {
52 char *name;
53 const char **pre;
54 const char **post;
55 unsigned int n_pre;
56 unsigned int n_post;
57 };
58
59 const char *kmod_blacklist_get_modname(const struct kmod_list *l)
60 {
61 return l->data;
62 }
63
64 const char *kmod_alias_get_name(const struct kmod_list *l) {
65 const struct kmod_alias *alias = l->data;
66 return alias->name;
67 }
68
69 const char *kmod_alias_get_modname(const struct kmod_list *l) {
70 const struct kmod_alias *alias = l->data;
71 return alias->modname;
72 }
73
74 const char *kmod_option_get_options(const struct kmod_list *l) {
75 const struct kmod_options *alias = l->data;
76 return alias->options;
77 }
78
79 const char *kmod_option_get_modname(const struct kmod_list *l) {
80 const struct kmod_options *alias = l->data;
81 return alias->modname;
82 }
83
84 const char *kmod_command_get_command(const struct kmod_list *l) {
85 const struct kmod_command *alias = l->data;
86 return alias->command;
87 }
88
89 const char *kmod_command_get_modname(const struct kmod_list *l) {
90 const struct kmod_command *alias = l->data;
91 return alias->modname;
92 }
93
94 const char *kmod_softdep_get_name(const struct kmod_list *l) {
95 const struct kmod_softdep *dep = l->data;
96 return dep->name;
97 }
98
99 const char * const *kmod_softdep_get_pre(const struct kmod_list *l, unsigned int *count) {
100 const struct kmod_softdep *dep = l->data;
101 *count = dep->n_pre;
102 return dep->pre;
103 }
104
105 const char * const *kmod_softdep_get_post(const struct kmod_list *l, unsigned int *count) {
106 const struct kmod_softdep *dep = l->data;
107 *count = dep->n_post;
108 return dep->post;
109 }
110
111 /*
112 * Replace dashes with underscores.
113 * Dashes inside character range patterns (e.g. [0-9]) are left unchanged.
114 */
115 static char *underscores(struct kmod_ctx *ctx, char *s)
116 {
117 unsigned int i;
118
119 if (!s)
120 return NULL;
121
122 for (i = 0; s[i]; i++) {
123 switch (s[i]) {
124 case '-':
125 s[i] = '_';
126 break;
127
128 case ']':
129 INFO(ctx, "Unmatched bracket in %s\n", s);
130 break;
131
132 case '[':
133 i += strcspn(&s[i], "]");
134 if (!s[i])
135 INFO(ctx, "Unmatched bracket in %s\n", s);
136 break;
137 }
138 }
139 return s;
140 }
141
142 static int kmod_config_add_command(struct kmod_config *config,
143 const char *modname,
144 const char *command,
145 const char *command_name,
146 struct kmod_list **list)
147 {
148 struct kmod_command *cmd;
149 struct kmod_list *l;
150 size_t modnamelen = strlen(modname) + 1;
151 size_t commandlen = strlen(command) + 1;
152
153 DBG(config->ctx, "modname='%s' cmd='%s %s'\n", modname, command_name,
154 command);
155
156 cmd = malloc(sizeof(*cmd) + modnamelen + commandlen);
157 if (cmd == NULL)
158 goto oom_error_init;
159
160 cmd->command = sizeof(*cmd) + modnamelen + (char *)cmd;
161 memcpy(cmd->modname, modname, modnamelen);
162 memcpy(cmd->command, command, commandlen);
163
164 l = kmod_list_append(*list, cmd);
165 if (l == NULL)
166 goto oom_error;
167
168 *list = l;
169 return 0;
170
171 oom_error:
172 free(cmd);
173 oom_error_init:
174 ERR(config->ctx, "out-of-memory\n");
175 return -ENOMEM;
176 }
177
178 static void kmod_config_free_command(struct kmod_config *config,
179 struct kmod_list *l,
180 struct kmod_list **list)
181 {
182 struct kmod_command *cmd = l->data;
183
184 free(cmd);
185 *list = kmod_list_remove(l);
186 }
187
188 static int kmod_config_add_options(struct kmod_config *config,
189 const char *modname, const char *options)
190 {
191 struct kmod_options *opt;
192 struct kmod_list *list;
193 size_t modnamelen = strlen(modname) + 1;
194 size_t optionslen = strlen(options) + 1;
195
196 DBG(config->ctx, "modname='%s' options='%s'\n", modname, options);
197
198 opt = malloc(sizeof(*opt) + modnamelen + optionslen);
199 if (opt == NULL)
200 goto oom_error_init;
201
202 opt->options = sizeof(*opt) + modnamelen + (char *)opt;
203
204 memcpy(opt->modname, modname, modnamelen);
205 memcpy(opt->options, options, optionslen);
206 strchr_replace(opt->options, '\t', ' ');
207
208 list = kmod_list_append(config->options, opt);
209 if (list == NULL)
210 goto oom_error;
211
212 config->options = list;
213 return 0;
214
215 oom_error:
216 free(opt);
217 oom_error_init:
218 ERR(config->ctx, "out-of-memory\n");
219 return -ENOMEM;
220 }
221
222 static void kmod_config_free_options(struct kmod_config *config,
223 struct kmod_list *l)
224 {
225 struct kmod_options *opt = l->data;
226
227 free(opt);
228
229 config->options = kmod_list_remove(l);
230 }
231
232 static int kmod_config_add_alias(struct kmod_config *config,
233 const char *name, const char *modname)
234 {
235 struct kmod_alias *alias;
236 struct kmod_list *list;
237 size_t namelen = strlen(name) + 1, modnamelen = strlen(modname) + 1;
238
239 DBG(config->ctx, "name=%s modname=%s\n", name, modname);
240
241 alias = malloc(sizeof(*alias) + namelen + modnamelen);
242 if (!alias)
243 goto oom_error_init;
244
245 alias->name = sizeof(*alias) + modnamelen + (char *)alias;
246
247 memcpy(alias->modname, modname, modnamelen);
248 memcpy(alias->name, name, namelen);
249
250 list = kmod_list_append(config->aliases, alias);
251 if (!list)
252 goto oom_error;
253
254 config->aliases = list;
255 return 0;
256
257 oom_error:
258 free(alias);
259 oom_error_init:
260 ERR(config->ctx, "out-of-memory name=%s modname=%s\n", name, modname);
261 return -ENOMEM;
262 }
263
264 static void kmod_config_free_alias(struct kmod_config *config,
265 struct kmod_list *l)
266 {
267 struct kmod_alias *alias = l->data;
268
269 free(alias);
270
271 config->aliases = kmod_list_remove(l);
272 }
273
274 static int kmod_config_add_blacklist(struct kmod_config *config,
275 const char *modname)
276 {
277 char *p;
278 struct kmod_list *list;
279
280 DBG(config->ctx, "modname=%s\n", modname);
281
282 p = strdup(modname);
283 if (!p)
284 goto oom_error_init;
285
286 list = kmod_list_append(config->blacklists, p);
287 if (!list)
288 goto oom_error;
289 config->blacklists = list;
290 return 0;
291
292 oom_error:
293 free(p);
294 oom_error_init:
295 ERR(config->ctx, "out-of-memory modname=%s\n", modname);
296 return -ENOMEM;
297 }
298
299 static void kmod_config_free_blacklist(struct kmod_config *config,
300 struct kmod_list *l)
301 {
302 free(l->data);
303 config->blacklists = kmod_list_remove(l);
304 }
305
306 static int kmod_config_add_softdep(struct kmod_config *config,
307 const char *modname,
308 const char *line)
309 {
310 struct kmod_list *list;
311 struct kmod_softdep *dep;
312 const char *s, *p;
313 char *itr;
314 unsigned int n_pre = 0, n_post = 0;
315 size_t modnamelen = strlen(modname) + 1;
316 size_t buflen = 0;
317 bool was_space = false;
318 enum { S_NONE, S_PRE, S_POST } mode = S_NONE;
319
320 DBG(config->ctx, "modname=%s\n", modname);
321
322 /* analyze and count */
323 for (p = s = line; ; s++) {
324 size_t plen;
325
326 if (*s != '\0') {
327 if (!isspace(*s)) {
328 was_space = false;
329 continue;
330 }
331
332 if (was_space) {
333 p = s + 1;
334 continue;
335 }
336 was_space = true;
337
338 if (p >= s)
339 continue;
340 }
341 plen = s - p;
342
343 if (plen == sizeof("pre:") - 1 &&
344 memcmp(p, "pre:", sizeof("pre:") - 1) == 0)
345 mode = S_PRE;
346 else if (plen == sizeof("post:") - 1 &&
347 memcmp(p, "post:", sizeof("post:") - 1) == 0)
348 mode = S_POST;
349 else if (*s != '\0' || (*s == '\0' && !was_space)) {
350 if (mode == S_PRE) {
351 buflen += plen + 1;
352 n_pre++;
353 } else if (mode == S_POST) {
354 buflen += plen + 1;
355 n_post++;
356 }
357 }
358 p = s + 1;
359 if (*s == '\0')
360 break;
361 }
362
363 DBG(config->ctx, "%u pre, %u post\n", n_pre, n_post);
364
365 dep = malloc(sizeof(struct kmod_softdep) + modnamelen +
366 n_pre * sizeof(const char *) +
367 n_post * sizeof(const char *) +
368 buflen);
369 if (dep == NULL) {
370 ERR(config->ctx, "out-of-memory modname=%s\n", modname);
371 return -ENOMEM;
372 }
373 dep->n_pre = n_pre;
374 dep->n_post = n_post;
375 dep->pre = (const char **)((char *)dep + sizeof(struct kmod_softdep));
376 dep->post = dep->pre + n_pre;
377 dep->name = (char *)(dep->post + n_post);
378
379 memcpy(dep->name, modname, modnamelen);
380
381 /* copy strings */
382 itr = dep->name + modnamelen;
383 n_pre = 0;
384 n_post = 0;
385 mode = S_NONE;
386 for (p = s = line; ; s++) {
387 size_t plen;
388
389 if (*s != '\0') {
390 if (!isspace(*s)) {
391 was_space = false;
392 continue;
393 }
394
395 if (was_space) {
396 p = s + 1;
397 continue;
398 }
399 was_space = true;
400
401 if (p >= s)
402 continue;
403 }
404 plen = s - p;
405
406 if (plen == sizeof("pre:") - 1 &&
407 memcmp(p, "pre:", sizeof("pre:") - 1) == 0)
408 mode = S_PRE;
409 else if (plen == sizeof("post:") - 1 &&
410 memcmp(p, "post:", sizeof("post:") - 1) == 0)
411 mode = S_POST;
412 else if (*s != '\0' || (*s == '\0' && !was_space)) {
413 if (mode == S_PRE) {
414 dep->pre[n_pre] = itr;
415 memcpy(itr, p, plen);
416 itr[plen] = '\0';
417 itr += plen + 1;
418 n_pre++;
419 } else if (mode == S_POST) {
420 dep->post[n_post] = itr;
421 memcpy(itr, p, plen);
422 itr[plen] = '\0';
423 itr += plen + 1;
424 n_post++;
425 }
426 }
427 p = s + 1;
428 if (*s == '\0')
429 break;
430 }
431
432 list = kmod_list_append(config->softdeps, dep);
433 if (list == NULL) {
434 free(dep);
435 return -ENOMEM;
436 }
437 config->softdeps = list;
438
439 return 0;
440 }
441
442 static char *softdep_to_char(struct kmod_softdep *dep) {
443 const size_t sz_preprefix = sizeof("pre: ") - 1;
444 const size_t sz_postprefix = sizeof("post: ") - 1;
445 size_t sz = 1; /* at least '\0' */
446 size_t sz_pre, sz_post;
447 const char *start, *end;
448 char *s, *itr;
449
450 /*
451 * Rely on the fact that dep->pre[] and dep->post[] are strv's that
452 * point to a contiguous buffer
453 */
454 if (dep->n_pre > 0) {
455 start = dep->pre[0];
456 end = dep->pre[dep->n_pre - 1]
457 + strlen(dep->pre[dep->n_pre - 1]);
458 sz_pre = end - start;
459 sz += sz_pre + sz_preprefix;
460 } else
461 sz_pre = 0;
462
463 if (dep->n_post > 0) {
464 start = dep->post[0];
465 end = dep->post[dep->n_post - 1]
466 + strlen(dep->post[dep->n_post - 1]);
467 sz_post = end - start;
468 sz += sz_post + sz_postprefix;
469 } else
470 sz_post = 0;
471
472 itr = s = malloc(sz);
473 if (s == NULL)
474 return NULL;
475
476 if (sz_pre) {
477 char *p;
478
479 memcpy(itr, "pre: ", sz_preprefix);
480 itr += sz_preprefix;
481
482 /* include last '\0' */
483 memcpy(itr, dep->pre[0], sz_pre + 1);
484 for (p = itr; p < itr + sz_pre; p++) {
485 if (*p == '\0')
486 *p = ' ';
487 }
488 itr = p;
489 }
490
491 if (sz_post) {
492 char *p;
493
494 memcpy(itr, "post: ", sz_postprefix);
495 itr += sz_postprefix;
496
497 /* include last '\0' */
498 memcpy(itr, dep->post[0], sz_post + 1);
499 for (p = itr; p < itr + sz_post; p++) {
500 if (*p == '\0')
501 *p = ' ';
502 }
503 itr = p;
504 }
505
506 *itr = '\0';
507
508 return s;
509 }
510
511 static void kmod_config_free_softdep(struct kmod_config *config,
512 struct kmod_list *l)
513 {
514 free(l->data);
515 config->softdeps = kmod_list_remove(l);
516 }
517
518 static void kcmdline_parse_result(struct kmod_config *config, char *modname,
519 char *param, char *value)
520 {
521 if (modname == NULL || param == NULL || value == NULL)
522 return;
523
524 DBG(config->ctx, "%s %s\n", modname, param);
525
526 if (streq(modname, "modprobe") && !strncmp(param, "blacklist=", 10)) {
527 for (;;) {
528 char *t = strsep(&value, ",");
529 if (t == NULL)
530 break;
531
532 kmod_config_add_blacklist(config, t);
533 }
534 } else {
535 kmod_config_add_options(config,
536 underscores(config->ctx, modname), param);
537 }
538 }
539
540 static int kmod_config_parse_kcmdline(struct kmod_config *config)
541 {
542 char buf[KCMD_LINE_SIZE];
543 int fd, err;
544 char *p, *modname, *param = NULL, *value = NULL;
545
546 fd = open("/proc/cmdline", O_RDONLY|O_CLOEXEC);
547 if (fd < 0) {
548 err = -errno;
549 DBG(config->ctx, "could not open '/proc/cmdline' for reading: %m\n");
550 return err;
551 }
552
553 err = read_str_safe(fd, buf, sizeof(buf));
554 close(fd);
555 if (err < 0) {
556 ERR(config->ctx, "could not read from '/proc/cmdline': %s\n",
557 strerror(-err));
558 return err;
559 }
560
561 for (p = buf, modname = buf; *p != '\0' && *p != '\n'; p++) {
562 switch (*p) {
563 case ' ':
564 *p = '\0';
565 kcmdline_parse_result(config, modname, param, value);
566 param = value = NULL;
567 modname = p + 1;
568 break;
569 case '.':
570 if (param == NULL) {
571 *p = '\0';
572 param = p + 1;
573 }
574 break;
575 case '=':
576 if (param != NULL)
577 value = p + 1;
578 break;
579 }
580 }
581
582 *p = '\0';
583 kcmdline_parse_result(config, modname, param, value);
584
585 return 0;
586 }
587
588 /*
589 * Take an fd and own it. It will be closed on return. filename is used only
590 * for debug messages
591 */
592 static int kmod_config_parse(struct kmod_config *config, int fd,
593 const char *filename)
594 {
595 struct kmod_ctx *ctx = config->ctx;
596 char *line;
597 FILE *fp;
598 unsigned int linenum = 0;
599 int err;
600
601 fp = fdopen(fd, "r");
602 if (fp == NULL) {
603 err = -errno;
604 ERR(config->ctx, "fd %d: %m\n", fd);
605 close(fd);
606 return err;
607 }
608
609 while ((line = getline_wrapped(fp, &linenum)) != NULL) {
610 char *cmd, *saveptr;
611
612 if (line[0] == '\0' || line[0] == '#')
613 goto done_next;
614
615 cmd = strtok_r(line, "\t ", &saveptr);
616 if (cmd == NULL)
617 goto done_next;
618
619 if (streq(cmd, "alias")) {
620 char *alias = strtok_r(NULL, "\t ", &saveptr);
621 char *modname = strtok_r(NULL, "\t ", &saveptr);
622
623 if (alias == NULL || modname == NULL)
624 goto syntax_error;
625
626 kmod_config_add_alias(config,
627 underscores(ctx, alias),
628 underscores(ctx, modname));
629 } else if (streq(cmd, "blacklist")) {
630 char *modname = strtok_r(NULL, "\t ", &saveptr);
631
632 if (modname == NULL)
633 goto syntax_error;
634
635 kmod_config_add_blacklist(config,
636 underscores(ctx, modname));
637 } else if (streq(cmd, "options")) {
638 char *modname = strtok_r(NULL, "\t ", &saveptr);
639 char *options = strtok_r(NULL, "\0", &saveptr);
640
641 if (modname == NULL || options == NULL)
642 goto syntax_error;
643
644 kmod_config_add_options(config,
645 underscores(ctx, modname),
646 options);
647 } else if (streq(cmd, "install")) {
648 char *modname = strtok_r(NULL, "\t ", &saveptr);
649 char *installcmd = strtok_r(NULL, "\0", &saveptr);
650
651 if (modname == NULL || installcmd == NULL)
652 goto syntax_error;
653
654 kmod_config_add_command(config,
655 underscores(ctx, modname),
656 installcmd,
657 cmd, &config->install_commands);
658 } else if (streq(cmd, "remove")) {
659 char *modname = strtok_r(NULL, "\t ", &saveptr);
660 char *removecmd = strtok_r(NULL, "\0", &saveptr);
661
662 if (modname == NULL || removecmd == NULL)
663 goto syntax_error;
664
665 kmod_config_add_command(config,
666 underscores(ctx, modname),
667 removecmd,
668 cmd, &config->remove_commands);
669 } else if (streq(cmd, "softdep")) {
670 char *modname = strtok_r(NULL, "\t ", &saveptr);
671 char *softdeps = strtok_r(NULL, "\0", &saveptr);
672
673 if (modname == NULL || softdeps == NULL)
674 goto syntax_error;
675
676 kmod_config_add_softdep(config,
677 underscores(ctx, modname),
678 softdeps);
679 } else if (streq(cmd, "include")
680 || streq(cmd, "config")) {
681 ERR(ctx, "%s: command %s is deprecated and not parsed anymore\n",
682 filename, cmd);
683 } else {
684 syntax_error:
685 ERR(ctx, "%s line %u: ignoring bad line starting with '%s'\n",
686 filename, linenum, cmd);
687 }
688
689 done_next:
690 free(line);
691 }
692
693 fclose(fp);
694
695 return 0;
696 }
697
698 void kmod_config_free(struct kmod_config *config)
699 {
700 while (config->aliases)
701 kmod_config_free_alias(config, config->aliases);
702
703 while (config->blacklists)
704 kmod_config_free_blacklist(config, config->blacklists);
705
706 while (config->options)
707 kmod_config_free_options(config, config->options);
708
709 while (config->install_commands) {
710 kmod_config_free_command(config, config->install_commands,
711 &config->install_commands);
712 }
713
714 while (config->remove_commands) {
715 kmod_config_free_command(config, config->remove_commands,
716 &config->remove_commands);
717 }
718
719 while (config->softdeps)
720 kmod_config_free_softdep(config, config->softdeps);
721
722 for (; config->paths != NULL;
723 config->paths = kmod_list_remove(config->paths))
724 free(config->paths->data);
725
726 free(config);
727 }
728
729 static bool conf_files_filter_out(struct kmod_ctx *ctx, DIR *d,
730 const char *path, const char *fn)
731 {
732 size_t len = strlen(fn);
733 struct stat st;
734
735 if (fn[0] == '.')
736 return true;
737
738 if (len < 6 || (!streq(&fn[len - 5], ".conf")
739 && !streq(&fn[len - 6], ".alias")))
740 return true;
741
742 fstatat(dirfd(d), fn, &st, 0);
743
744 if (S_ISDIR(st.st_mode)) {
745 ERR(ctx, "Directories inside directories are not supported: "
746 "%s/%s\n", path, fn);
747 return true;
748 }
749
750 return false;
751 }
752
753 struct conf_file {
754 const char *path;
755 bool is_single;
756 char name[];
757 };
758
759 static int conf_files_insert_sorted(struct kmod_ctx *ctx,
760 struct kmod_list **list,
761 const char *path, const char *name)
762 {
763 struct kmod_list *lpos, *tmp;
764 struct conf_file *cf;
765 size_t namelen;
766 int cmp = -1;
767 bool is_single = false;
768
769 if (name == NULL) {
770 name = basename(path);
771 is_single = true;
772 }
773
774 kmod_list_foreach(lpos, *list) {
775 cf = lpos->data;
776
777 if ((cmp = strcmp(name, cf->name)) <= 0)
778 break;
779 }
780
781 if (cmp == 0) {
782 DBG(ctx, "Ignoring duplicate config file: %s/%s\n", path,
783 name);
784 return -EEXIST;
785 }
786
787 namelen = strlen(name);
788 cf = malloc(sizeof(*cf) + namelen + 1);
789 if (cf == NULL)
790 return -ENOMEM;
791
792 memcpy(cf->name, name, namelen + 1);
793 cf->path = path;
794 cf->is_single = is_single;
795
796 if (lpos == NULL)
797 tmp = kmod_list_append(*list, cf);
798 else if (lpos == *list)
799 tmp = kmod_list_prepend(*list, cf);
800 else
801 tmp = kmod_list_insert_before(lpos, cf);
802
803 if (tmp == NULL) {
804 free(cf);
805 return -ENOMEM;
806 }
807
808 if (lpos == NULL || lpos == *list)
809 *list = tmp;
810
811 return 0;
812 }
813
814 /*
815 * Insert configuration files in @list, ignoring duplicates
816 */
817 static int conf_files_list(struct kmod_ctx *ctx, struct kmod_list **list,
818 const char *path,
819 unsigned long long *path_stamp)
820 {
821 DIR *d;
822 int err;
823 struct stat st;
824
825 if (stat(path, &st) != 0) {
826 err = -errno;
827 DBG(ctx, "could not stat '%s': %m\n", path);
828 return err;
829 }
830
831 *path_stamp = stat_mstamp(&st);
832
833 if (S_ISREG(st.st_mode)) {
834 conf_files_insert_sorted(ctx, list, path, NULL);
835 return 0;
836 } else if (!S_ISDIR(st.st_mode)) {
837 ERR(ctx, "unsupported file mode %s: %#x\n",
838 path, st.st_mode);
839 return -EINVAL;
840 }
841
842 d = opendir(path);
843 if (d == NULL) {
844 ERR(ctx, "opendir(%s): %m\n", path);
845 return -EINVAL;
846 }
847
848 for (;;) {
849 struct dirent ent, *entp;
850
851 err = readdir_r(d, &ent, &entp);
852 if (err != 0) {
853 ERR(ctx, "reading entry %s\n", strerror(-err));
854 goto fail_read;
855 }
856
857 if (entp == NULL)
858 break;
859
860 if (conf_files_filter_out(ctx, d, path, entp->d_name))
861 continue;
862
863 conf_files_insert_sorted(ctx, list, path, entp->d_name);
864 }
865
866 closedir(d);
867 return 0;
868
869 fail_read:
870 closedir(d);
871 return err;
872 }
873
874 int kmod_config_new(struct kmod_ctx *ctx, struct kmod_config **p_config,
875 const char * const *config_paths)
876 {
877 struct kmod_config *config;
878 struct kmod_list *list = NULL;
879 struct kmod_list *path_list = NULL;
880 size_t i;
881
882 for (i = 0; config_paths[i] != NULL; i++) {
883 const char *path = config_paths[i];
884 unsigned long long path_stamp = 0;
885 size_t pathlen;
886 struct kmod_list *tmp;
887 struct kmod_config_path *cf;
888
889 if (conf_files_list(ctx, &list, path, &path_stamp) < 0)
890 continue;
891
892 pathlen = strlen(path) + 1;
893 cf = malloc(sizeof(*cf) + pathlen);
894 if (cf == NULL)
895 goto oom;
896
897 cf->stamp = path_stamp;
898 memcpy(cf->path, path, pathlen);
899
900 tmp = kmod_list_append(path_list, cf);
901 if (tmp == NULL)
902 goto oom;
903 path_list = tmp;
904 }
905
906 *p_config = config = calloc(1, sizeof(struct kmod_config));
907 if (config == NULL)
908 goto oom;
909
910 config->paths = path_list;
911 config->ctx = ctx;
912
913 for (; list != NULL; list = kmod_list_remove(list)) {
914 char fn[PATH_MAX];
915 struct conf_file *cf = list->data;
916 int fd;
917
918 if (cf->is_single)
919 strcpy(fn, cf->path);
920 else
921 snprintf(fn, sizeof(fn),"%s/%s", cf->path,
922 cf->name);
923
924 fd = open(fn, O_RDONLY|O_CLOEXEC);
925 DBG(ctx, "parsing file '%s' fd=%d\n", fn, fd);
926
927 if (fd >= 0)
928 kmod_config_parse(config, fd, fn);
929
930 free(cf);
931 }
932
933 kmod_config_parse_kcmdline(config);
934
935 return 0;
936
937 oom:
938 for (; list != NULL; list = kmod_list_remove(list))
939 free(list->data);
940
941 for (; path_list != NULL; path_list = kmod_list_remove(path_list))
942 free(path_list->data);
943
944 return -ENOMEM;
945 }
946
947 /**********************************************************************
948 * struct kmod_config_iter functions
949 **********************************************************************/
950
951 enum config_type {
952 CONFIG_TYPE_BLACKLIST = 0,
953 CONFIG_TYPE_INSTALL,
954 CONFIG_TYPE_REMOVE,
955 CONFIG_TYPE_ALIAS,
956 CONFIG_TYPE_OPTION,
957 CONFIG_TYPE_SOFTDEP,
958 };
959
960 struct kmod_config_iter {
961 enum config_type type;
962 bool intermediate;
963 const struct kmod_list *list;
964 const struct kmod_list *curr;
965 void *data;
966 const char *(*get_key)(const struct kmod_list *l);
967 const char *(*get_value)(const struct kmod_list *l);
968 };
969
970 static const char *softdep_get_plain_softdep(const struct kmod_list *l)
971 {
972 char *s = softdep_to_char(l->data);
973 return s;
974 }
975
976 static struct kmod_config_iter *kmod_config_iter_new(const struct kmod_ctx* ctx,
977 enum config_type type)
978 {
979 struct kmod_config_iter *iter = calloc(1, sizeof(*iter));
980 const struct kmod_config *config = kmod_get_config(ctx);
981
982 if (iter == NULL)
983 return NULL;
984
985 iter->type = type;
986
987 switch (type) {
988 case CONFIG_TYPE_BLACKLIST:
989 iter->list = config->blacklists;
990 iter->get_key = kmod_blacklist_get_modname;
991 break;
992 case CONFIG_TYPE_INSTALL:
993 iter->list = config->install_commands;
994 iter->get_key = kmod_command_get_modname;
995 iter->get_value = kmod_command_get_command;
996 break;
997 case CONFIG_TYPE_REMOVE:
998 iter->list = config->remove_commands;
999 iter->get_key = kmod_command_get_modname;
1000 iter->get_value = kmod_command_get_command;
1001 break;
1002 case CONFIG_TYPE_ALIAS:
1003 iter->list = config->aliases;
1004 iter->get_key = kmod_alias_get_name;
1005 iter->get_value = kmod_alias_get_modname;
1006 break;
1007 case CONFIG_TYPE_OPTION:
1008 iter->list = config->options;
1009 iter->get_key = kmod_option_get_modname;
1010 iter->get_value = kmod_option_get_options;
1011 break;
1012 case CONFIG_TYPE_SOFTDEP:
1013 iter->list = config->softdeps;
1014 iter->get_key = kmod_softdep_get_name;
1015 iter->get_value = softdep_get_plain_softdep;
1016 iter->intermediate = true;
1017 break;
1018 }
1019
1020 return iter;
1021 }
1022
1023 /**
1024 * SECTION:libkmod-config
1025 * @short_description: retrieve current libkmod configuration
1026 */
1027
1028 /**
1029 * kmod_config_get_blacklists:
1030 * @ctx: kmod library context
1031 *
1032 * Retrieve an iterator to deal with the blacklist maintained inside the
1033 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1034 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1035 * be made to initialize the iterator and check if it's valid.
1036 *
1037 * Returns: a new iterator over the blacklists or NULL on failure. Free it
1038 * with kmod_config_iter_free_iter().
1039 */
1040 KMOD_EXPORT struct kmod_config_iter *kmod_config_get_blacklists(const struct kmod_ctx *ctx)
1041 {
1042 if (ctx == NULL)
1043 return NULL;;
1044
1045 return kmod_config_iter_new(ctx, CONFIG_TYPE_BLACKLIST);
1046 }
1047
1048 /**
1049 * kmod_config_get_install_commands:
1050 * @ctx: kmod library context
1051 *
1052 * Retrieve an iterator to deal with the install commands maintained inside the
1053 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1054 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1055 * be made to initialize the iterator and check if it's valid.
1056 *
1057 * Returns: a new iterator over the install commands or NULL on failure. Free
1058 * it with kmod_config_iter_free_iter().
1059 */
1060 KMOD_EXPORT struct kmod_config_iter *kmod_config_get_install_commands(const struct kmod_ctx *ctx)
1061 {
1062 if (ctx == NULL)
1063 return NULL;;
1064
1065 return kmod_config_iter_new(ctx, CONFIG_TYPE_INSTALL);
1066 }
1067
1068 /**
1069 * kmod_config_get_remove_commands:
1070 * @ctx: kmod library context
1071 *
1072 * Retrieve an iterator to deal with the remove commands maintained inside the
1073 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1074 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1075 * be made to initialize the iterator and check if it's valid.
1076 *
1077 * Returns: a new iterator over the remove commands or NULL on failure. Free
1078 * it with kmod_config_iter_free_iter().
1079 */
1080 KMOD_EXPORT struct kmod_config_iter *kmod_config_get_remove_commands(const struct kmod_ctx *ctx)
1081 {
1082 if (ctx == NULL)
1083 return NULL;;
1084
1085 return kmod_config_iter_new(ctx, CONFIG_TYPE_REMOVE);
1086 }
1087
1088 /**
1089 * kmod_config_get_aliases:
1090 * @ctx: kmod library context
1091 *
1092 * Retrieve an iterator to deal with the aliases maintained inside the
1093 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1094 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1095 * be made to initialize the iterator and check if it's valid.
1096 *
1097 * Returns: a new iterator over the aliases or NULL on failure. Free it with
1098 * kmod_config_iter_free_iter().
1099 */
1100 KMOD_EXPORT struct kmod_config_iter *kmod_config_get_aliases(const struct kmod_ctx *ctx)
1101 {
1102 if (ctx == NULL)
1103 return NULL;;
1104
1105 return kmod_config_iter_new(ctx, CONFIG_TYPE_ALIAS);
1106 }
1107
1108 /**
1109 * kmod_config_get_options:
1110 * @ctx: kmod library context
1111 *
1112 * Retrieve an iterator to deal with the options maintained inside the
1113 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1114 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1115 * be made to initialize the iterator and check if it's valid.
1116 *
1117 * Returns: a new iterator over the options or NULL on failure. Free it with
1118 * kmod_config_iter_free_iter().
1119 */
1120 KMOD_EXPORT struct kmod_config_iter *kmod_config_get_options(const struct kmod_ctx *ctx)
1121 {
1122 if (ctx == NULL)
1123 return NULL;;
1124
1125 return kmod_config_iter_new(ctx, CONFIG_TYPE_OPTION);
1126 }
1127
1128 /**
1129 * kmod_config_get_softdeps:
1130 * @ctx: kmod library context
1131 *
1132 * Retrieve an iterator to deal with the softdeps maintained inside the
1133 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1134 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1135 * be made to initialize the iterator and check if it's valid.
1136 *
1137 * Returns: a new iterator over the softdeps or NULL on failure. Free it with
1138 * kmod_config_iter_free_iter().
1139 */
1140 KMOD_EXPORT struct kmod_config_iter *kmod_config_get_softdeps(const struct kmod_ctx *ctx)
1141 {
1142 if (ctx == NULL)
1143 return NULL;;
1144
1145 return kmod_config_iter_new(ctx, CONFIG_TYPE_SOFTDEP);
1146 }
1147
1148 /**
1149 * kmod_config_iter_get_key:
1150 * @iter: iterator over a certain configuration
1151 *
1152 * When using a new allocated iterator, user must perform a call to
1153 * kmod_config_iter_next() to initialize iterator's position and check if it's
1154 * valid.
1155 *
1156 * Returns: the key of the current configuration pointed by @iter.
1157 */
1158 KMOD_EXPORT const char *kmod_config_iter_get_key(const struct kmod_config_iter *iter)
1159 {
1160 if (iter == NULL || iter->curr == NULL)
1161 return NULL;
1162
1163 return iter->get_key(iter->curr);
1164 }
1165
1166 /**
1167 * kmod_config_iter_get_value:
1168 * @iter: iterator over a certain configuration
1169 *
1170 * When using a new allocated iterator, user must perform a call to
1171 * kmod_config_iter_next() to initialize iterator's position and check if it's
1172 * valid.
1173 *
1174 * Returns: the value of the current configuration pointed by @iter.
1175 */
1176 KMOD_EXPORT const char *kmod_config_iter_get_value(const struct kmod_config_iter *iter)
1177 {
1178 const char *s;
1179
1180 if (iter == NULL || iter->curr == NULL)
1181 return NULL;
1182
1183 if (iter->get_value == NULL)
1184 return NULL;
1185
1186 if (iter->intermediate) {
1187 struct kmod_config_iter *i = (struct kmod_config_iter *)iter;
1188
1189 free(i->data);
1190 s = i->data = (void *) iter->get_value(iter->curr);
1191 } else
1192 s = iter->get_value(iter->curr);
1193
1194 return s;
1195 }
1196
1197 /**
1198 * kmod_config_iter_next:
1199 * @iter: iterator over a certain configuration
1200 *
1201 * Make @iter point to the next item of a certain configuration. It's an
1202 * automatically recycling iterator. When it reaches the end, false is
1203 * returned; then if user wants to iterate again, it's sufficient to call this
1204 * function once more.
1205 *
1206 * Returns: true if next position of @iter is valid or false if its end is
1207 * reached.
1208 */
1209 KMOD_EXPORT bool kmod_config_iter_next(struct kmod_config_iter *iter)
1210 {
1211 if (iter == NULL)
1212 return false;
1213
1214 if (iter->curr == NULL) {
1215 iter->curr = iter->list;
1216 return iter->curr != NULL;
1217 }
1218
1219 iter->curr = kmod_list_next(iter->list, iter->curr);
1220
1221 return iter->curr != NULL;
1222 }
1223
1224 /**
1225 * kmod_config_iter_free_iter:
1226 * @iter: iterator over a certain configuration
1227 *
1228 * Free resources used by the iterator.
1229 */
1230 KMOD_EXPORT void kmod_config_iter_free_iter(struct kmod_config_iter *iter)
1231 {
1232 free(iter->data);
1233 free(iter);
1234 }