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