]> git.ipfire.org Git - thirdparty/kmod.git/blob - libkmod/libkmod-config.c
config: let softdeps dump their data
[thirdparty/kmod.git] / libkmod / libkmod-config.c
1 /*
2 * libkmod - interface to kernel module operations
3 *
4 * Copyright (C) 2011-2012 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-private.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 static int kmod_config_add_command(struct kmod_config *config,
112 const char *modname,
113 const char *command,
114 const char *command_name,
115 struct kmod_list **list)
116 {
117 struct kmod_command *cmd;
118 struct kmod_list *l;
119 size_t modnamelen = strlen(modname) + 1;
120 size_t commandlen = strlen(command) + 1;
121
122 DBG(config->ctx, "modname='%s' cmd='%s %s'\n", modname, command_name,
123 command);
124
125 cmd = malloc(sizeof(*cmd) + modnamelen + commandlen);
126 if (cmd == NULL)
127 goto oom_error_init;
128
129 cmd->command = sizeof(*cmd) + modnamelen + (char *)cmd;
130 memcpy(cmd->modname, modname, modnamelen);
131 memcpy(cmd->command, command, commandlen);
132
133 l = kmod_list_append(*list, cmd);
134 if (l == NULL)
135 goto oom_error;
136
137 *list = l;
138 return 0;
139
140 oom_error:
141 free(cmd);
142 oom_error_init:
143 ERR(config->ctx, "out-of-memory\n");
144 return -ENOMEM;
145 }
146
147 static void kmod_config_free_command(struct kmod_config *config,
148 struct kmod_list *l,
149 struct kmod_list **list)
150 {
151 struct kmod_command *cmd = l->data;
152
153 free(cmd);
154 *list = kmod_list_remove(l);
155 }
156
157 static int kmod_config_add_options(struct kmod_config *config,
158 const char *modname, const char *options)
159 {
160 struct kmod_options *opt;
161 struct kmod_list *list;
162 size_t modnamelen = strlen(modname) + 1;
163 size_t optionslen = strlen(options) + 1;
164
165 DBG(config->ctx, "modname='%s' options='%s'\n", modname, options);
166
167 opt = malloc(sizeof(*opt) + modnamelen + optionslen);
168 if (opt == NULL)
169 goto oom_error_init;
170
171 opt->options = sizeof(*opt) + modnamelen + (char *)opt;
172
173 memcpy(opt->modname, modname, modnamelen);
174 memcpy(opt->options, options, optionslen);
175 strchr_replace(opt->options, '\t', ' ');
176
177 list = kmod_list_append(config->options, opt);
178 if (list == NULL)
179 goto oom_error;
180
181 config->options = list;
182 return 0;
183
184 oom_error:
185 free(opt);
186 oom_error_init:
187 ERR(config->ctx, "out-of-memory\n");
188 return -ENOMEM;
189 }
190
191 static void kmod_config_free_options(struct kmod_config *config,
192 struct kmod_list *l)
193 {
194 struct kmod_options *opt = l->data;
195
196 free(opt);
197
198 config->options = kmod_list_remove(l);
199 }
200
201 static int kmod_config_add_alias(struct kmod_config *config,
202 const char *name, const char *modname)
203 {
204 struct kmod_alias *alias;
205 struct kmod_list *list;
206 size_t namelen = strlen(name) + 1, modnamelen = strlen(modname) + 1;
207
208 DBG(config->ctx, "name=%s modname=%s\n", name, modname);
209
210 alias = malloc(sizeof(*alias) + namelen + modnamelen);
211 if (!alias)
212 goto oom_error_init;
213
214 alias->name = sizeof(*alias) + modnamelen + (char *)alias;
215
216 memcpy(alias->modname, modname, modnamelen);
217 memcpy(alias->name, name, namelen);
218
219 list = kmod_list_append(config->aliases, alias);
220 if (!list)
221 goto oom_error;
222
223 config->aliases = list;
224 return 0;
225
226 oom_error:
227 free(alias);
228 oom_error_init:
229 ERR(config->ctx, "out-of-memory name=%s modname=%s\n", name, modname);
230 return -ENOMEM;
231 }
232
233 static void kmod_config_free_alias(struct kmod_config *config,
234 struct kmod_list *l)
235 {
236 struct kmod_alias *alias = l->data;
237
238 free(alias);
239
240 config->aliases = kmod_list_remove(l);
241 }
242
243 static int kmod_config_add_blacklist(struct kmod_config *config,
244 const char *modname)
245 {
246 char *p;
247 struct kmod_list *list;
248
249 DBG(config->ctx, "modname=%s\n", modname);
250
251 p = strdup(modname);
252 if (!p)
253 goto oom_error_init;
254
255 list = kmod_list_append(config->blacklists, p);
256 if (!list)
257 goto oom_error;
258 config->blacklists = list;
259 return 0;
260
261 oom_error:
262 free(p);
263 oom_error_init:
264 ERR(config->ctx, "out-of-memory modname=%s\n", modname);
265 return -ENOMEM;
266 }
267
268 static void kmod_config_free_blacklist(struct kmod_config *config,
269 struct kmod_list *l)
270 {
271 free(l->data);
272 config->blacklists = kmod_list_remove(l);
273 }
274
275 static int kmod_config_add_softdep(struct kmod_config *config,
276 const char *modname,
277 const char *line)
278 {
279 struct kmod_list *list;
280 struct kmod_softdep *dep;
281 const char *s, *p;
282 char *itr;
283 unsigned int n_pre = 0, n_post = 0;
284 size_t modnamelen = strlen(modname) + 1;
285 size_t buflen = 0;
286 bool was_space = false;
287 enum { S_NONE, S_PRE, S_POST } mode = S_NONE;
288
289 DBG(config->ctx, "modname=%s\n", modname);
290
291 /* analyze and count */
292 for (p = s = line; ; s++) {
293 size_t plen;
294
295 if (*s != '\0') {
296 if (!isspace(*s)) {
297 was_space = false;
298 continue;
299 }
300
301 if (was_space) {
302 p = s + 1;
303 continue;
304 }
305 was_space = true;
306
307 if (p >= s)
308 continue;
309 }
310 plen = s - p;
311
312 if (plen == sizeof("pre:") - 1 &&
313 memcmp(p, "pre:", sizeof("pre:") - 1) == 0)
314 mode = S_PRE;
315 else if (plen == sizeof("post:") - 1 &&
316 memcmp(p, "post:", sizeof("post:") - 1) == 0)
317 mode = S_POST;
318 else if (*s != '\0' || (*s == '\0' && !was_space)) {
319 if (mode == S_PRE) {
320 buflen += plen + 1;
321 n_pre++;
322 } else if (mode == S_POST) {
323 buflen += plen + 1;
324 n_post++;
325 }
326 }
327 p = s + 1;
328 if (*s == '\0')
329 break;
330 }
331
332 DBG(config->ctx, "%u pre, %u post\n", n_pre, n_post);
333
334 dep = malloc(sizeof(struct kmod_softdep) + modnamelen +
335 n_pre * sizeof(const char *) +
336 n_post * sizeof(const char *) +
337 buflen);
338 if (dep == NULL) {
339 ERR(config->ctx, "out-of-memory modname=%s\n", modname);
340 return -ENOMEM;
341 }
342 dep->n_pre = n_pre;
343 dep->n_post = n_post;
344 dep->pre = (const char **)((char *)dep + sizeof(struct kmod_softdep));
345 dep->post = dep->pre + n_pre;
346 dep->name = (char *)(dep->post + n_post);
347
348 memcpy(dep->name, modname, modnamelen);
349
350 /* copy strings */
351 itr = dep->name + modnamelen;
352 n_pre = 0;
353 n_post = 0;
354 mode = S_NONE;
355 for (p = s = line; ; s++) {
356 size_t plen;
357
358 if (*s != '\0') {
359 if (!isspace(*s)) {
360 was_space = false;
361 continue;
362 }
363
364 if (was_space) {
365 p = s + 1;
366 continue;
367 }
368 was_space = true;
369
370 if (p >= s)
371 continue;
372 }
373 plen = s - p;
374
375 if (plen == sizeof("pre:") - 1 &&
376 memcmp(p, "pre:", sizeof("pre:") - 1) == 0)
377 mode = S_PRE;
378 else if (plen == sizeof("post:") - 1 &&
379 memcmp(p, "post:", sizeof("post:") - 1) == 0)
380 mode = S_POST;
381 else if (*s != '\0' || (*s == '\0' && !was_space)) {
382 if (mode == S_PRE) {
383 dep->pre[n_pre] = itr;
384 memcpy(itr, p, plen);
385 itr[plen] = '\0';
386 itr += plen + 1;
387 n_pre++;
388 } else if (mode == S_POST) {
389 dep->post[n_post] = itr;
390 memcpy(itr, p, plen);
391 itr[plen] = '\0';
392 itr += plen + 1;
393 n_post++;
394 }
395 }
396 p = s + 1;
397 if (*s == '\0')
398 break;
399 }
400
401 list = kmod_list_append(config->softdeps, dep);
402 if (list == NULL) {
403 free(dep);
404 return -ENOMEM;
405 }
406 config->softdeps = list;
407
408 return 0;
409 }
410
411 static char *softdep_to_char(struct kmod_softdep *dep) {
412 const size_t sz_preprefix = sizeof("pre: ") - 1;
413 const size_t sz_postprefix = sizeof("post: ") - 1;
414 size_t sz = 1; /* at least '\0' */
415 size_t sz_pre, sz_post;
416 const char *start, *end;
417 char *s, *itr;
418
419 /*
420 * Rely on the fact that dep->pre[] and dep->post[] are strv's that
421 * point to a contiguous buffer
422 */
423 if (dep->n_pre > 0) {
424 start = dep->pre[0];
425 end = dep->pre[dep->n_pre - 1]
426 + strlen(dep->pre[dep->n_pre - 1]);
427 sz_pre = end - start;
428 sz += sz_pre + sz_preprefix;
429 } else
430 sz_pre = 0;
431
432 if (dep->n_post > 0) {
433 start = dep->post[0];
434 end = dep->post[dep->n_post - 1]
435 + strlen(dep->post[dep->n_post - 1]);
436 sz_post = end - start;
437 sz += sz_post + sz_postprefix;
438 } else
439 sz_post = 0;
440
441 itr = s = malloc(sz);
442 if (s == NULL)
443 return NULL;
444
445 if (sz_pre) {
446 char *p;
447
448 memcpy(itr, "pre: ", sz_preprefix);
449 itr += sz_preprefix;
450
451 /* include last '\0' */
452 memcpy(itr, dep->pre[0], sz_pre + 1);
453 for (p = itr; p < itr + sz_pre; p++) {
454 if (*p == '\0')
455 *p = ' ';
456 }
457 itr = p;
458 }
459
460 if (sz_post) {
461 char *p;
462
463 memcpy(itr, "post: ", sz_postprefix);
464 itr += sz_postprefix;
465
466 /* include last '\0' */
467 memcpy(itr, dep->post[0], sz_post + 1);
468 for (p = itr; p < itr + sz_post; p++) {
469 if (*p == '\0')
470 *p = ' ';
471 }
472 itr = p;
473 }
474
475 *itr = '\0';
476
477 return s;
478 }
479
480 static void kmod_config_free_softdep(struct kmod_config *config,
481 struct kmod_list *l)
482 {
483 free(l->data);
484 config->softdeps = kmod_list_remove(l);
485 }
486
487 static void kcmdline_parse_result(struct kmod_config *config, char *modname,
488 char *param, char *value)
489 {
490 if (modname == NULL || param == NULL || value == NULL)
491 return;
492
493 DBG(config->ctx, "%s %s\n", modname, param);
494
495 if (streq(modname, "modprobe") && !strncmp(param, "blacklist=", 10)) {
496 for (;;) {
497 char *t = strsep(&value, ",");
498 if (t == NULL)
499 break;
500
501 kmod_config_add_blacklist(config, t);
502 }
503 } else {
504 kmod_config_add_options(config,
505 underscores(config->ctx, modname), param);
506 }
507 }
508
509 static int kmod_config_parse_kcmdline(struct kmod_config *config)
510 {
511 char buf[KCMD_LINE_SIZE];
512 int fd, err;
513 char *p, *modname, *param = NULL, *value = NULL;
514
515 fd = open("/proc/cmdline", O_RDONLY|O_CLOEXEC);
516 if (fd < 0) {
517 err = -errno;
518 DBG(config->ctx, "could not open '/proc/cmdline' for reading: %m\n");
519 return err;
520 }
521
522 err = read_str_safe(fd, buf, sizeof(buf));
523 close(fd);
524 if (err < 0) {
525 ERR(config->ctx, "could not read from '/proc/cmdline': %s\n",
526 strerror(-err));
527 return err;
528 }
529
530 for (p = buf, modname = buf; *p != '\0' && *p != '\n'; p++) {
531 switch (*p) {
532 case ' ':
533 *p = '\0';
534 kcmdline_parse_result(config, modname, param, value);
535 param = value = NULL;
536 modname = p + 1;
537 break;
538 case '.':
539 *p = '\0';
540 param = p + 1;
541 break;
542 case '=':
543 if (param != NULL)
544 value = p + 1;
545 break;
546 }
547 }
548
549 *p = '\0';
550 kcmdline_parse_result(config, modname, param, value);
551
552 return 0;
553 }
554
555 /*
556 * Take an fd and own it. It will be closed on return. filename is used only
557 * for debug messages
558 */
559 static int kmod_config_parse(struct kmod_config *config, int fd,
560 const char *filename)
561 {
562 struct kmod_ctx *ctx = config->ctx;
563 char *line;
564 FILE *fp;
565 unsigned int linenum = 0;
566 int err;
567
568 fp = fdopen(fd, "r");
569 if (fp == NULL) {
570 err = -errno;
571 ERR(config->ctx, "fd %d: %m", fd);
572 close(fd);
573 return err;
574 }
575
576 while ((line = getline_wrapped(fp, &linenum)) != NULL) {
577 char *cmd, *saveptr;
578
579 if (line[0] == '\0' || line[0] == '#')
580 goto done_next;
581
582 cmd = strtok_r(line, "\t ", &saveptr);
583 if (cmd == NULL)
584 goto done_next;
585
586 if (streq(cmd, "alias")) {
587 char *alias = strtok_r(NULL, "\t ", &saveptr);
588 char *modname = strtok_r(NULL, "\t ", &saveptr);
589
590 if (alias == NULL || modname == NULL)
591 goto syntax_error;
592
593 kmod_config_add_alias(config,
594 underscores(ctx, alias),
595 underscores(ctx, modname));
596 } else if (streq(cmd, "blacklist")) {
597 char *modname = strtok_r(NULL, "\t ", &saveptr);
598
599 if (modname == NULL)
600 goto syntax_error;
601
602 kmod_config_add_blacklist(config,
603 underscores(ctx, modname));
604 } else if (streq(cmd, "options")) {
605 char *modname = strtok_r(NULL, "\t ", &saveptr);
606 char *options = strtok_r(NULL, "\0", &saveptr);
607
608 if (modname == NULL || options == NULL)
609 goto syntax_error;
610
611 kmod_config_add_options(config,
612 underscores(ctx, modname),
613 options);
614 } else if (streq(cmd, "install")) {
615 char *modname = strtok_r(NULL, "\t ", &saveptr);
616 char *installcmd = strtok_r(NULL, "\0", &saveptr);
617
618 if (modname == NULL || installcmd == NULL)
619 goto syntax_error;
620
621 kmod_config_add_command(config,
622 underscores(ctx, modname),
623 installcmd,
624 cmd, &config->install_commands);
625 } else if (streq(cmd, "remove")) {
626 char *modname = strtok_r(NULL, "\t ", &saveptr);
627 char *removecmd = strtok_r(NULL, "\0", &saveptr);
628
629 if (modname == NULL || removecmd == NULL)
630 goto syntax_error;
631
632 kmod_config_add_command(config,
633 underscores(ctx, modname),
634 removecmd,
635 cmd, &config->remove_commands);
636 } else if (streq(cmd, "softdep")) {
637 char *modname = strtok_r(NULL, "\t ", &saveptr);
638 char *softdeps = strtok_r(NULL, "\0", &saveptr);
639
640 if (modname == NULL || softdeps == NULL)
641 goto syntax_error;
642
643 kmod_config_add_softdep(config,
644 underscores(ctx, modname),
645 softdeps);
646 } else if (streq(cmd, "include")
647 || streq(cmd, "config")) {
648 ERR(ctx, "%s: command %s is deprecated and not parsed anymore\n",
649 filename, cmd);
650 } else {
651 syntax_error:
652 ERR(ctx, "%s line %u: ignoring bad line starting with '%s'\n",
653 filename, linenum, cmd);
654 }
655
656 done_next:
657 free(line);
658 }
659
660 fclose(fp);
661
662 return 0;
663 }
664
665 void kmod_config_free(struct kmod_config *config)
666 {
667 while (config->aliases)
668 kmod_config_free_alias(config, config->aliases);
669
670 while (config->blacklists)
671 kmod_config_free_blacklist(config, config->blacklists);
672
673 while (config->options)
674 kmod_config_free_options(config, config->options);
675
676 while (config->install_commands) {
677 kmod_config_free_command(config, config->install_commands,
678 &config->install_commands);
679 }
680
681 while (config->remove_commands) {
682 kmod_config_free_command(config, config->remove_commands,
683 &config->remove_commands);
684 }
685
686 while (config->softdeps)
687 kmod_config_free_softdep(config, config->softdeps);
688
689 for (; config->paths != NULL;
690 config->paths = kmod_list_remove(config->paths))
691 free(config->paths->data);
692
693 free(config);
694 }
695
696 static bool conf_files_filter_out(struct kmod_ctx *ctx, DIR *d,
697 const char *path, const char *fn)
698 {
699 size_t len = strlen(fn);
700 struct stat st;
701
702 if (fn[0] == '.')
703 return true;
704
705 if (len < 6 || (!streq(&fn[len - 5], ".conf")
706 && !streq(&fn[len - 6], ".alias")))
707 return true;
708
709 fstatat(dirfd(d), fn, &st, 0);
710
711 if (S_ISDIR(st.st_mode)) {
712 ERR(ctx, "Directories inside directories are not supported: "
713 "%s/%s\n", path, fn);
714 return true;
715 }
716
717 return false;
718 }
719
720 struct conf_file {
721 const char *path;
722 bool is_single;
723 char name[];
724 };
725
726 static int conf_files_insert_sorted(struct kmod_ctx *ctx,
727 struct kmod_list **list,
728 const char *path, const char *name)
729 {
730 struct kmod_list *lpos, *tmp;
731 struct conf_file *cf;
732 size_t namelen;
733 int cmp = -1;
734 bool is_single = false;
735
736 if (name == NULL) {
737 name = basename(path);
738 is_single = true;
739 }
740
741 kmod_list_foreach(lpos, *list) {
742 cf = lpos->data;
743
744 if ((cmp = strcmp(name, cf->name)) <= 0)
745 break;
746 }
747
748 if (cmp == 0) {
749 DBG(ctx, "Ignoring duplicate config file: %s/%s\n", path,
750 name);
751 return -EEXIST;
752 }
753
754 namelen = strlen(name);
755 cf = malloc(sizeof(*cf) + namelen + 1);
756 if (cf == NULL)
757 return -ENOMEM;
758
759 memcpy(cf->name, name, namelen + 1);
760 cf->path = path;
761 cf->is_single = is_single;
762
763 if (lpos == NULL)
764 tmp = kmod_list_append(*list, cf);
765 else if (lpos == *list)
766 tmp = kmod_list_prepend(*list, cf);
767 else
768 tmp = kmod_list_insert_before(lpos, cf);
769
770 if (tmp == NULL) {
771 free(cf);
772 return -ENOMEM;
773 }
774
775 if (lpos == NULL || lpos == *list)
776 *list = tmp;
777
778 return 0;
779 }
780
781 /*
782 * Insert configuration files in @list, ignoring duplicates
783 */
784 static int conf_files_list(struct kmod_ctx *ctx, struct kmod_list **list,
785 const char *path,
786 unsigned long long *path_stamp)
787 {
788 DIR *d;
789 int err;
790 struct stat st;
791
792 if (stat(path, &st) != 0) {
793 err = -errno;
794 DBG(ctx, "could not stat '%s': %m\n", path);
795 return err;
796 }
797
798 *path_stamp = ts_usec(&st.st_mtim);
799
800 if (S_ISREG(st.st_mode)) {
801 conf_files_insert_sorted(ctx, list, path, NULL);
802 return 0;
803 } if (!S_ISDIR(st.st_mode)) {
804 ERR(ctx, "unsupported file mode %s: %#x\n",
805 path, st.st_mode);
806 return -EINVAL;
807 }
808
809 d = opendir(path);
810 if (d == NULL) {
811 ERR(ctx, "%m\n");
812 return -EINVAL;
813 }
814
815 for (;;) {
816 struct dirent ent, *entp;
817
818 err = readdir_r(d, &ent, &entp);
819 if (err != 0) {
820 ERR(ctx, "reading entry %s\n", strerror(-err));
821 goto fail_read;
822 }
823
824 if (entp == NULL)
825 break;
826
827 if (conf_files_filter_out(ctx, d, path, entp->d_name))
828 continue;
829
830 conf_files_insert_sorted(ctx, list, path, entp->d_name);
831 }
832
833 closedir(d);
834 return 0;
835
836 fail_read:
837 closedir(d);
838 return err;
839 }
840
841 int kmod_config_new(struct kmod_ctx *ctx, struct kmod_config **p_config,
842 const char * const *config_paths)
843 {
844 struct kmod_config *config;
845 struct kmod_list *list = NULL;
846 struct kmod_list *path_list = NULL;
847 size_t i;
848
849 for (i = 0; config_paths[i] != NULL; i++) {
850 const char *path = config_paths[i];
851 unsigned long long path_stamp = 0;
852 size_t pathlen;
853 struct kmod_list *tmp;
854 struct kmod_config_path *cf;
855
856 if (conf_files_list(ctx, &list, path, &path_stamp) < 0)
857 continue;
858
859 pathlen = strlen(path) + 1;
860 cf = malloc(sizeof(*cf) + pathlen);
861 if (cf == NULL)
862 goto oom;
863
864 cf->stamp = path_stamp;
865 memcpy(cf->path, path, pathlen);
866
867 tmp = kmod_list_append(path_list, cf);
868 if (tmp == NULL)
869 goto oom;
870 path_list = tmp;
871 }
872
873 *p_config = config = calloc(1, sizeof(struct kmod_config));
874 if (config == NULL)
875 goto oom;
876
877 config->paths = path_list;
878 config->ctx = ctx;
879
880 for (; list != NULL; list = kmod_list_remove(list)) {
881 char fn[PATH_MAX];
882 struct conf_file *cf = list->data;
883 int fd;
884
885 if (cf->is_single)
886 strcpy(fn, cf->path);
887 else
888 snprintf(fn, sizeof(fn),"%s/%s", cf->path,
889 cf->name);
890
891 fd = open(fn, O_RDONLY|O_CLOEXEC);
892 DBG(ctx, "parsing file '%s' fd=%d\n", fn, fd);
893
894 if (fd >= 0)
895 kmod_config_parse(config, fd, fn);
896
897 free(cf);
898 }
899
900 kmod_config_parse_kcmdline(config);
901
902 return 0;
903
904 oom:
905 for (; list != NULL; list = kmod_list_remove(list))
906 free(list->data);
907
908 for (; path_list != NULL; path_list = kmod_list_remove(path_list))
909 free(path_list->data);
910
911 return -ENOMEM;
912 }
913
914 /**********************************************************************
915 * struct kmod_config_iter functions
916 **********************************************************************/
917
918 enum config_type {
919 CONFIG_TYPE_BLACKLIST = 0,
920 CONFIG_TYPE_INSTALL,
921 CONFIG_TYPE_REMOVE,
922 CONFIG_TYPE_ALIAS,
923 CONFIG_TYPE_OPTION,
924 CONFIG_TYPE_SOFTDEP,
925 };
926
927 struct kmod_config_iter {
928 enum config_type type;
929 bool intermediate;
930 const struct kmod_list *list;
931 const struct kmod_list *curr;
932 void *data;
933 const char *(*get_key)(const struct kmod_list *l);
934 const char *(*get_value)(const struct kmod_list *l);
935 };
936
937 static const char *softdep_get_plain_softdep(const struct kmod_list *l)
938 {
939 char *s = softdep_to_char(l->data);
940 return s;
941 }
942
943 static struct kmod_config_iter *kmod_config_iter_new(const struct kmod_ctx* ctx,
944 enum config_type type)
945 {
946 struct kmod_config_iter *iter = calloc(1, sizeof(*iter));
947
948 if (iter == NULL)
949 return NULL;
950
951 iter->type = type;
952
953 switch (type) {
954 case CONFIG_TYPE_BLACKLIST:
955 iter->list = kmod_get_blacklists(ctx);
956 iter->get_key = kmod_blacklist_get_modname;
957 break;
958 case CONFIG_TYPE_INSTALL:
959 iter->list = kmod_get_install_commands(ctx);
960 iter->get_key = kmod_command_get_modname;
961 iter->get_value = kmod_command_get_command;
962 break;
963 case CONFIG_TYPE_REMOVE:
964 iter->list = kmod_get_remove_commands(ctx);
965 iter->get_key = kmod_command_get_modname;
966 iter->get_value = kmod_command_get_command;
967 break;
968 case CONFIG_TYPE_ALIAS:
969 iter->list = kmod_get_aliases(ctx);
970 iter->get_key = kmod_alias_get_name;
971 iter->get_value = kmod_alias_get_modname;
972 break;
973 case CONFIG_TYPE_OPTION:
974 iter->list = kmod_get_options(ctx);
975 iter->get_key = kmod_option_get_modname;
976 iter->get_value = kmod_option_get_options;
977 break;
978 case CONFIG_TYPE_SOFTDEP:
979 iter->list = kmod_get_softdeps(ctx);
980 iter->get_key = kmod_softdep_get_name;
981 iter->get_value = softdep_get_plain_softdep;
982 iter->intermediate = true;
983 break;
984 }
985
986 return iter;
987 }
988
989 KMOD_EXPORT struct kmod_config_iter *kmod_config_get_blacklists(const struct kmod_ctx *ctx)
990 {
991 if (ctx == NULL)
992 return NULL;;
993
994 return kmod_config_iter_new(ctx, CONFIG_TYPE_BLACKLIST);
995 }
996
997 KMOD_EXPORT struct kmod_config_iter *kmod_config_get_install_commands(const struct kmod_ctx *ctx)
998 {
999 if (ctx == NULL)
1000 return NULL;;
1001
1002 return kmod_config_iter_new(ctx, CONFIG_TYPE_INSTALL);
1003 }
1004
1005 KMOD_EXPORT struct kmod_config_iter *kmod_config_get_remove_commands(const struct kmod_ctx *ctx)
1006 {
1007 if (ctx == NULL)
1008 return NULL;;
1009
1010 return kmod_config_iter_new(ctx, CONFIG_TYPE_REMOVE);
1011 }
1012
1013 KMOD_EXPORT struct kmod_config_iter *kmod_config_get_aliases(const struct kmod_ctx *ctx)
1014 {
1015 if (ctx == NULL)
1016 return NULL;;
1017
1018 return kmod_config_iter_new(ctx, CONFIG_TYPE_ALIAS);
1019 }
1020
1021 KMOD_EXPORT struct kmod_config_iter *kmod_config_get_options(const struct kmod_ctx *ctx)
1022 {
1023 if (ctx == NULL)
1024 return NULL;;
1025
1026 return kmod_config_iter_new(ctx, CONFIG_TYPE_OPTION);
1027 }
1028
1029 KMOD_EXPORT struct kmod_config_iter *kmod_config_get_softdeps(const struct kmod_ctx *ctx)
1030 {
1031 if (ctx == NULL)
1032 return NULL;;
1033
1034 return kmod_config_iter_new(ctx, CONFIG_TYPE_SOFTDEP);
1035 }
1036
1037 KMOD_EXPORT const char *kmod_config_iter_get_key(const struct kmod_config_iter *iter)
1038 {
1039 if (iter == NULL || iter->curr == NULL)
1040 return NULL;
1041
1042 return iter->get_key(iter->curr);
1043 }
1044
1045 KMOD_EXPORT const char *kmod_config_iter_get_value(const struct kmod_config_iter *iter)
1046 {
1047 const char *s;
1048
1049 if (iter == NULL || iter->curr == NULL)
1050 return NULL;
1051
1052 if (iter->get_value == NULL)
1053 return NULL;
1054
1055 if (iter->intermediate) {
1056 struct kmod_config_iter *i = (struct kmod_config_iter *)iter;
1057
1058 free(i->data);
1059 s = i->data = (void *) iter->get_value(iter->curr);
1060 } else
1061 s = iter->get_value(iter->curr);
1062
1063 return s;
1064 }
1065
1066 KMOD_EXPORT bool kmod_config_iter_next(struct kmod_config_iter *iter)
1067 {
1068 if (iter == NULL)
1069 return false;
1070
1071 if (iter->curr == NULL) {
1072 iter->curr = iter->list;
1073 return iter->curr != NULL;
1074 }
1075
1076 iter->curr = kmod_list_next(iter->list, iter->curr);
1077
1078 return iter->curr != NULL;
1079 }
1080
1081 KMOD_EXPORT void kmod_config_iter_free_iter(struct kmod_config_iter *iter)
1082 {
1083 free(iter->data);
1084 free(iter);
1085 }