]> git.ipfire.org Git - thirdparty/kmod.git/blob - libkmod/libkmod-config.c
2873f061dc9eef378380592d8aeb99700a2f4a8d
[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 was_space = false;
339 for (p = s = line; ; s++) {
340 size_t plen;
341
342 if (*s != '\0') {
343 if (!isspace(*s)) {
344 was_space = false;
345 continue;
346 }
347
348 if (was_space) {
349 p = s + 1;
350 continue;
351 }
352 was_space = true;
353
354 if (p >= s)
355 continue;
356 }
357 plen = s - p;
358
359 if (plen == sizeof("pre:") - 1 &&
360 memcmp(p, "pre:", sizeof("pre:") - 1) == 0)
361 mode = S_PRE;
362 else if (plen == sizeof("post:") - 1 &&
363 memcmp(p, "post:", sizeof("post:") - 1) == 0)
364 mode = S_POST;
365 else if (*s != '\0' || (*s == '\0' && !was_space)) {
366 if (mode == S_PRE) {
367 dep->pre[n_pre] = itr;
368 memcpy(itr, p, plen);
369 itr[plen] = '\0';
370 itr += plen + 1;
371 n_pre++;
372 } else if (mode == S_POST) {
373 dep->post[n_post] = itr;
374 memcpy(itr, p, plen);
375 itr[plen] = '\0';
376 itr += plen + 1;
377 n_post++;
378 }
379 }
380 p = s + 1;
381 if (*s == '\0')
382 break;
383 }
384
385 list = kmod_list_append(config->softdeps, dep);
386 if (list == NULL) {
387 free(dep);
388 return -ENOMEM;
389 }
390 config->softdeps = list;
391
392 return 0;
393 }
394
395 static char *softdep_to_char(struct kmod_softdep *dep) {
396 const size_t sz_preprefix = sizeof("pre: ") - 1;
397 const size_t sz_postprefix = sizeof("post: ") - 1;
398 size_t sz = 1; /* at least '\0' */
399 size_t sz_pre, sz_post;
400 const char *start, *end;
401 char *s, *itr;
402
403 /*
404 * Rely on the fact that dep->pre[] and dep->post[] are strv's that
405 * point to a contiguous buffer
406 */
407 if (dep->n_pre > 0) {
408 start = dep->pre[0];
409 end = dep->pre[dep->n_pre - 1]
410 + strlen(dep->pre[dep->n_pre - 1]);
411 sz_pre = end - start;
412 sz += sz_pre + sz_preprefix;
413 } else
414 sz_pre = 0;
415
416 if (dep->n_post > 0) {
417 start = dep->post[0];
418 end = dep->post[dep->n_post - 1]
419 + strlen(dep->post[dep->n_post - 1]);
420 sz_post = end - start;
421 sz += sz_post + sz_postprefix;
422 } else
423 sz_post = 0;
424
425 itr = s = malloc(sz);
426 if (s == NULL)
427 return NULL;
428
429 if (sz_pre) {
430 char *p;
431
432 memcpy(itr, "pre: ", sz_preprefix);
433 itr += sz_preprefix;
434
435 /* include last '\0' */
436 memcpy(itr, dep->pre[0], sz_pre + 1);
437 for (p = itr; p < itr + sz_pre; p++) {
438 if (*p == '\0')
439 *p = ' ';
440 }
441 itr = p;
442 }
443
444 if (sz_post) {
445 char *p;
446
447 memcpy(itr, "post: ", sz_postprefix);
448 itr += sz_postprefix;
449
450 /* include last '\0' */
451 memcpy(itr, dep->post[0], sz_post + 1);
452 for (p = itr; p < itr + sz_post; p++) {
453 if (*p == '\0')
454 *p = ' ';
455 }
456 itr = p;
457 }
458
459 *itr = '\0';
460
461 return s;
462 }
463
464 static void kmod_config_free_softdep(struct kmod_config *config,
465 struct kmod_list *l)
466 {
467 free(l->data);
468 config->softdeps = kmod_list_remove(l);
469 }
470
471 static void kcmdline_parse_result(struct kmod_config *config, char *modname,
472 char *param, char *value)
473 {
474 if (modname == NULL || param == NULL)
475 return;
476
477 DBG(config->ctx, "%s %s\n", modname, param);
478
479 if (streq(modname, "modprobe") && !strncmp(param, "blacklist=", 10)) {
480 for (;;) {
481 char *t = strsep(&value, ",");
482 if (t == NULL)
483 break;
484
485 kmod_config_add_blacklist(config, t);
486 }
487 } else {
488 if (underscores(modname) < 0) {
489 ERR(config->ctx, "Ignoring bad option on kernel command line while parsing module name: '%s'\n",
490 modname);
491 } else {
492 kmod_config_add_options(config, modname, param);
493 }
494 }
495 }
496
497 static int kmod_config_parse_kcmdline(struct kmod_config *config)
498 {
499 char buf[KCMD_LINE_SIZE];
500 int fd, err;
501 char *p, *p_quote_start, *modname, *param = NULL, *value = NULL;
502 bool is_quoted = false, iter = true;
503 enum state {
504 STATE_IGNORE,
505 STATE_MODNAME,
506 STATE_PARAM,
507 STATE_VALUE,
508 STATE_COMPLETE,
509 } state;
510
511 fd = open("/proc/cmdline", O_RDONLY|O_CLOEXEC);
512 if (fd < 0) {
513 err = -errno;
514 DBG(config->ctx, "could not open '/proc/cmdline' for reading: %m\n");
515 return err;
516 }
517
518 err = read_str_safe(fd, buf, sizeof(buf));
519 close(fd);
520 if (err < 0) {
521 ERR(config->ctx, "could not read from '/proc/cmdline': %s\n",
522 strerror(-err));
523 return err;
524 }
525
526 state = STATE_MODNAME;
527 p_quote_start = NULL;
528 for (p = buf, modname = buf; iter; p++) {
529 switch (*p) {
530 case '"':
531 is_quoted = !is_quoted;
532
533 /*
534 * only allow starting quote as first char when looking
535 * for a modname: anything else is considered ill-formed
536 */
537 if (is_quoted && state == STATE_MODNAME && p == modname) {
538 p_quote_start = p;
539 modname = p + 1;
540 } else if (state != STATE_VALUE) {
541 state = STATE_IGNORE;
542 }
543
544 break;
545 case '\0':
546 case '\n':
547 /* Stop iterating on new chars */
548 iter = false;
549 /* fall-through */
550 case ' ':
551 if (is_quoted && state == STATE_VALUE) {
552 /* no state change*/;
553 } else if (is_quoted) {
554 /* spaces are only allowed in the value part */
555 state = STATE_IGNORE;
556 } else if (state == STATE_VALUE || state == STATE_PARAM) {
557 *p = '\0';
558 state = STATE_COMPLETE;
559 } else {
560 /*
561 * go to next option, ignoring any possible
562 * partial match we have
563 */
564 modname = p + 1;
565 state = STATE_MODNAME;
566 p_quote_start = NULL;
567 }
568 break;
569 case '.':
570 if (state == STATE_MODNAME) {
571 *p = '\0';
572 param = p + 1;
573 state = STATE_PARAM;
574 } else if (state == STATE_PARAM) {
575 state = STATE_IGNORE;
576 }
577 break;
578 case '=':
579 if (state == STATE_PARAM) {
580 /*
581 * Don't set *p to '\0': the value var shadows
582 * param
583 */
584 value = p + 1;
585 state = STATE_VALUE;
586 } else if (state == STATE_MODNAME) {
587 state = STATE_IGNORE;
588 }
589 break;
590 }
591
592 if (state == STATE_COMPLETE) {
593 /*
594 * We may need to re-quote to unmangle what the
595 * bootloader passed. Example: grub passes the option as
596 * "parport.dyndbg=file drivers/parport/ieee1284_ops.c +mpf"
597 * instead of
598 * parport.dyndbg="file drivers/parport/ieee1284_ops.c +mpf"
599 */
600 if (p_quote_start && p_quote_start < modname) {
601 /*
602 * p_quote_start
603 * |
604 * |modname param value
605 * || | |
606 * vv v v
607 * "parport\0dyndbg=file drivers/parport/ieee1284_ops.c +mpf" */
608 memmove(p_quote_start, modname, value - modname);
609 value--; modname--; param--;
610 *value = '"';
611 }
612 kcmdline_parse_result(config, modname, param, value);
613 /* start over on next iteration */
614 modname = p + 1;
615 state = STATE_MODNAME;
616 p_quote_start = NULL;
617 }
618 }
619
620 return 0;
621 }
622
623 /*
624 * Take an fd and own it. It will be closed on return. filename is used only
625 * for debug messages
626 */
627 static int kmod_config_parse(struct kmod_config *config, int fd,
628 const char *filename)
629 {
630 struct kmod_ctx *ctx = config->ctx;
631 char *line;
632 FILE *fp;
633 unsigned int linenum = 0;
634 int err;
635
636 fp = fdopen(fd, "r");
637 if (fp == NULL) {
638 err = -errno;
639 ERR(config->ctx, "fd %d: %m\n", fd);
640 close(fd);
641 return err;
642 }
643
644 while ((line = freadline_wrapped(fp, &linenum)) != NULL) {
645 char *cmd, *saveptr;
646
647 if (line[0] == '\0' || line[0] == '#')
648 goto done_next;
649
650 cmd = strtok_r(line, "\t ", &saveptr);
651 if (cmd == NULL)
652 goto done_next;
653
654 if (streq(cmd, "alias")) {
655 char *alias = strtok_r(NULL, "\t ", &saveptr);
656 char *modname = strtok_r(NULL, "\t ", &saveptr);
657
658 if (underscores(alias) < 0 || underscores(modname) < 0)
659 goto syntax_error;
660
661 kmod_config_add_alias(config, alias, modname);
662 } else if (streq(cmd, "blacklist")) {
663 char *modname = strtok_r(NULL, "\t ", &saveptr);
664
665 if (underscores(modname) < 0)
666 goto syntax_error;
667
668 kmod_config_add_blacklist(config, modname);
669 } else if (streq(cmd, "options")) {
670 char *modname = strtok_r(NULL, "\t ", &saveptr);
671 char *options = strtok_r(NULL, "\0", &saveptr);
672
673 if (underscores(modname) < 0 || options == NULL)
674 goto syntax_error;
675
676 kmod_config_add_options(config, modname, options);
677 } else if (streq(cmd, "install")) {
678 char *modname = strtok_r(NULL, "\t ", &saveptr);
679 char *installcmd = strtok_r(NULL, "\0", &saveptr);
680
681 if (underscores(modname) < 0 || installcmd == NULL)
682 goto syntax_error;
683
684 kmod_config_add_command(config, modname, installcmd,
685 cmd, &config->install_commands);
686 } else if (streq(cmd, "remove")) {
687 char *modname = strtok_r(NULL, "\t ", &saveptr);
688 char *removecmd = strtok_r(NULL, "\0", &saveptr);
689
690 if (underscores(modname) < 0 || removecmd == NULL)
691 goto syntax_error;
692
693 kmod_config_add_command(config, modname, removecmd,
694 cmd, &config->remove_commands);
695 } else if (streq(cmd, "softdep")) {
696 char *modname = strtok_r(NULL, "\t ", &saveptr);
697 char *softdeps = strtok_r(NULL, "\0", &saveptr);
698
699 if (underscores(modname) < 0 || softdeps == NULL)
700 goto syntax_error;
701
702 kmod_config_add_softdep(config, modname, softdeps);
703 } else if (streq(cmd, "include")
704 || streq(cmd, "config")) {
705 ERR(ctx, "%s: command %s is deprecated and not parsed anymore\n",
706 filename, cmd);
707 } else {
708 syntax_error:
709 ERR(ctx, "%s line %u: ignoring bad line starting with '%s'\n",
710 filename, linenum, cmd);
711 }
712
713 done_next:
714 free(line);
715 }
716
717 fclose(fp);
718
719 return 0;
720 }
721
722 void kmod_config_free(struct kmod_config *config)
723 {
724 while (config->aliases)
725 kmod_config_free_alias(config, config->aliases);
726
727 while (config->blacklists)
728 kmod_config_free_blacklist(config, config->blacklists);
729
730 while (config->options)
731 kmod_config_free_options(config, config->options);
732
733 while (config->install_commands) {
734 kmod_config_free_command(config, config->install_commands,
735 &config->install_commands);
736 }
737
738 while (config->remove_commands) {
739 kmod_config_free_command(config, config->remove_commands,
740 &config->remove_commands);
741 }
742
743 while (config->softdeps)
744 kmod_config_free_softdep(config, config->softdeps);
745
746 for (; config->paths != NULL;
747 config->paths = kmod_list_remove(config->paths))
748 free(config->paths->data);
749
750 free(config);
751 }
752
753 static bool conf_files_filter_out(struct kmod_ctx *ctx, DIR *d,
754 const char *path, const char *fn)
755 {
756 size_t len = strlen(fn);
757 struct stat st;
758
759 if (fn[0] == '.')
760 return true;
761
762 if (len < 6 || (!streq(&fn[len - 5], ".conf")
763 && !streq(&fn[len - 6], ".alias")))
764 return true;
765
766 fstatat(dirfd(d), fn, &st, 0);
767
768 if (S_ISDIR(st.st_mode)) {
769 ERR(ctx, "Directories inside directories are not supported: "
770 "%s/%s\n", path, fn);
771 return true;
772 }
773
774 return false;
775 }
776
777 struct conf_file {
778 const char *path;
779 bool is_single;
780 char name[];
781 };
782
783 static int conf_files_insert_sorted(struct kmod_ctx *ctx,
784 struct kmod_list **list,
785 const char *path, const char *name)
786 {
787 struct kmod_list *lpos, *tmp;
788 struct conf_file *cf;
789 size_t namelen;
790 int cmp = -1;
791 bool is_single = false;
792
793 if (name == NULL) {
794 name = basename(path);
795 is_single = true;
796 }
797
798 kmod_list_foreach(lpos, *list) {
799 cf = lpos->data;
800
801 if ((cmp = strcmp(name, cf->name)) <= 0)
802 break;
803 }
804
805 if (cmp == 0) {
806 DBG(ctx, "Ignoring duplicate config file: %s/%s\n", path,
807 name);
808 return -EEXIST;
809 }
810
811 namelen = strlen(name);
812 cf = malloc(sizeof(*cf) + namelen + 1);
813 if (cf == NULL)
814 return -ENOMEM;
815
816 memcpy(cf->name, name, namelen + 1);
817 cf->path = path;
818 cf->is_single = is_single;
819
820 if (lpos == NULL)
821 tmp = kmod_list_append(*list, cf);
822 else if (lpos == *list)
823 tmp = kmod_list_prepend(*list, cf);
824 else
825 tmp = kmod_list_insert_before(lpos, cf);
826
827 if (tmp == NULL) {
828 free(cf);
829 return -ENOMEM;
830 }
831
832 if (lpos == NULL || lpos == *list)
833 *list = tmp;
834
835 return 0;
836 }
837
838 /*
839 * Insert configuration files in @list, ignoring duplicates
840 */
841 static int conf_files_list(struct kmod_ctx *ctx, struct kmod_list **list,
842 const char *path,
843 unsigned long long *path_stamp)
844 {
845 DIR *d;
846 int err;
847 struct stat st;
848 struct dirent *dent;
849
850 if (stat(path, &st) != 0) {
851 err = -errno;
852 DBG(ctx, "could not stat '%s': %m\n", path);
853 return err;
854 }
855
856 *path_stamp = stat_mstamp(&st);
857
858 if (!S_ISDIR(st.st_mode)) {
859 conf_files_insert_sorted(ctx, list, path, NULL);
860 return 0;
861 }
862
863 d = opendir(path);
864 if (d == NULL) {
865 ERR(ctx, "opendir(%s): %m\n", path);
866 return -EINVAL;
867 }
868
869 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
870 if (conf_files_filter_out(ctx, d, path, dent->d_name))
871 continue;
872
873 conf_files_insert_sorted(ctx, list, path, dent->d_name);
874 }
875
876 closedir(d);
877 return 0;
878 }
879
880 int kmod_config_new(struct kmod_ctx *ctx, struct kmod_config **p_config,
881 const char * const *config_paths)
882 {
883 struct kmod_config *config;
884 struct kmod_list *list = NULL;
885 struct kmod_list *path_list = NULL;
886 size_t i;
887
888 conf_files_insert_sorted(ctx, &list, kmod_get_dirname(ctx), "modules.softdep");
889
890 for (i = 0; config_paths[i] != NULL; i++) {
891 const char *path = config_paths[i];
892 unsigned long long path_stamp = 0;
893 size_t pathlen;
894 struct kmod_list *tmp;
895 struct kmod_config_path *cf;
896
897 if (conf_files_list(ctx, &list, path, &path_stamp) < 0)
898 continue;
899
900 pathlen = strlen(path) + 1;
901 cf = malloc(sizeof(*cf) + pathlen);
902 if (cf == NULL)
903 goto oom;
904
905 cf->stamp = path_stamp;
906 memcpy(cf->path, path, pathlen);
907
908 tmp = kmod_list_append(path_list, cf);
909 if (tmp == NULL)
910 goto oom;
911 path_list = tmp;
912 }
913
914 *p_config = config = calloc(1, sizeof(struct kmod_config));
915 if (config == NULL)
916 goto oom;
917
918 config->paths = path_list;
919 config->ctx = ctx;
920
921 for (; list != NULL; list = kmod_list_remove(list)) {
922 char buf[PATH_MAX];
923 const char *fn = buf;
924 struct conf_file *cf = list->data;
925 int fd;
926
927 if (cf->is_single) {
928 fn = cf->path;
929 } else if (snprintf(buf, sizeof(buf), "%s/%s",
930 cf->path, cf->name) >= (int)sizeof(buf)) {
931 ERR(ctx, "Error parsing %s/%s: path too long\n",
932 cf->path, cf->name);
933 free(cf);
934 continue;
935 }
936
937 fd = open(fn, O_RDONLY|O_CLOEXEC);
938 DBG(ctx, "parsing file '%s' fd=%d\n", fn, fd);
939
940 if (fd >= 0)
941 kmod_config_parse(config, fd, fn);
942
943 free(cf);
944 }
945
946 kmod_config_parse_kcmdline(config);
947
948 return 0;
949
950 oom:
951 for (; list != NULL; list = kmod_list_remove(list))
952 free(list->data);
953
954 for (; path_list != NULL; path_list = kmod_list_remove(path_list))
955 free(path_list->data);
956
957 return -ENOMEM;
958 }
959
960 /**********************************************************************
961 * struct kmod_config_iter functions
962 **********************************************************************/
963
964 enum config_type {
965 CONFIG_TYPE_BLACKLIST = 0,
966 CONFIG_TYPE_INSTALL,
967 CONFIG_TYPE_REMOVE,
968 CONFIG_TYPE_ALIAS,
969 CONFIG_TYPE_OPTION,
970 CONFIG_TYPE_SOFTDEP,
971 };
972
973 struct kmod_config_iter {
974 enum config_type type;
975 bool intermediate;
976 const struct kmod_list *list;
977 const struct kmod_list *curr;
978 void *data;
979 const char *(*get_key)(const struct kmod_list *l);
980 const char *(*get_value)(const struct kmod_list *l);
981 };
982
983 static const char *softdep_get_plain_softdep(const struct kmod_list *l)
984 {
985 char *s = softdep_to_char(l->data);
986 return s;
987 }
988
989 static struct kmod_config_iter *kmod_config_iter_new(const struct kmod_ctx* ctx,
990 enum config_type type)
991 {
992 struct kmod_config_iter *iter = calloc(1, sizeof(*iter));
993 const struct kmod_config *config = kmod_get_config(ctx);
994
995 if (iter == NULL)
996 return NULL;
997
998 iter->type = type;
999
1000 switch (type) {
1001 case CONFIG_TYPE_BLACKLIST:
1002 iter->list = config->blacklists;
1003 iter->get_key = kmod_blacklist_get_modname;
1004 break;
1005 case CONFIG_TYPE_INSTALL:
1006 iter->list = config->install_commands;
1007 iter->get_key = kmod_command_get_modname;
1008 iter->get_value = kmod_command_get_command;
1009 break;
1010 case CONFIG_TYPE_REMOVE:
1011 iter->list = config->remove_commands;
1012 iter->get_key = kmod_command_get_modname;
1013 iter->get_value = kmod_command_get_command;
1014 break;
1015 case CONFIG_TYPE_ALIAS:
1016 iter->list = config->aliases;
1017 iter->get_key = kmod_alias_get_name;
1018 iter->get_value = kmod_alias_get_modname;
1019 break;
1020 case CONFIG_TYPE_OPTION:
1021 iter->list = config->options;
1022 iter->get_key = kmod_option_get_modname;
1023 iter->get_value = kmod_option_get_options;
1024 break;
1025 case CONFIG_TYPE_SOFTDEP:
1026 iter->list = config->softdeps;
1027 iter->get_key = kmod_softdep_get_name;
1028 iter->get_value = softdep_get_plain_softdep;
1029 iter->intermediate = true;
1030 break;
1031 }
1032
1033 return iter;
1034 }
1035
1036 /**
1037 * SECTION:libkmod-config
1038 * @short_description: retrieve current libkmod configuration
1039 */
1040
1041 /**
1042 * kmod_config_get_blacklists:
1043 * @ctx: kmod library context
1044 *
1045 * Retrieve an iterator to deal with the blacklist maintained inside the
1046 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1047 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1048 * be made to initialize the iterator and check if it's valid.
1049 *
1050 * Returns: a new iterator over the blacklists or NULL on failure. Free it
1051 * with kmod_config_iter_free_iter().
1052 */
1053 KMOD_EXPORT struct kmod_config_iter *kmod_config_get_blacklists(const struct kmod_ctx *ctx)
1054 {
1055 if (ctx == NULL)
1056 return NULL;;
1057
1058 return kmod_config_iter_new(ctx, CONFIG_TYPE_BLACKLIST);
1059 }
1060
1061 /**
1062 * kmod_config_get_install_commands:
1063 * @ctx: kmod library context
1064 *
1065 * Retrieve an iterator to deal with the install commands maintained inside the
1066 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1067 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1068 * be made to initialize the iterator and check if it's valid.
1069 *
1070 * Returns: a new iterator over the install commands or NULL on failure. Free
1071 * it with kmod_config_iter_free_iter().
1072 */
1073 KMOD_EXPORT struct kmod_config_iter *kmod_config_get_install_commands(const struct kmod_ctx *ctx)
1074 {
1075 if (ctx == NULL)
1076 return NULL;;
1077
1078 return kmod_config_iter_new(ctx, CONFIG_TYPE_INSTALL);
1079 }
1080
1081 /**
1082 * kmod_config_get_remove_commands:
1083 * @ctx: kmod library context
1084 *
1085 * Retrieve an iterator to deal with the remove commands maintained inside the
1086 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1087 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1088 * be made to initialize the iterator and check if it's valid.
1089 *
1090 * Returns: a new iterator over the remove commands or NULL on failure. Free
1091 * it with kmod_config_iter_free_iter().
1092 */
1093 KMOD_EXPORT struct kmod_config_iter *kmod_config_get_remove_commands(const struct kmod_ctx *ctx)
1094 {
1095 if (ctx == NULL)
1096 return NULL;;
1097
1098 return kmod_config_iter_new(ctx, CONFIG_TYPE_REMOVE);
1099 }
1100
1101 /**
1102 * kmod_config_get_aliases:
1103 * @ctx: kmod library context
1104 *
1105 * Retrieve an iterator to deal with the aliases maintained inside the
1106 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1107 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1108 * be made to initialize the iterator and check if it's valid.
1109 *
1110 * Returns: a new iterator over the aliases or NULL on failure. Free it with
1111 * kmod_config_iter_free_iter().
1112 */
1113 KMOD_EXPORT struct kmod_config_iter *kmod_config_get_aliases(const struct kmod_ctx *ctx)
1114 {
1115 if (ctx == NULL)
1116 return NULL;;
1117
1118 return kmod_config_iter_new(ctx, CONFIG_TYPE_ALIAS);
1119 }
1120
1121 /**
1122 * kmod_config_get_options:
1123 * @ctx: kmod library context
1124 *
1125 * Retrieve an iterator to deal with the options maintained inside the
1126 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1127 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1128 * be made to initialize the iterator and check if it's valid.
1129 *
1130 * Returns: a new iterator over the options or NULL on failure. Free it with
1131 * kmod_config_iter_free_iter().
1132 */
1133 KMOD_EXPORT struct kmod_config_iter *kmod_config_get_options(const struct kmod_ctx *ctx)
1134 {
1135 if (ctx == NULL)
1136 return NULL;;
1137
1138 return kmod_config_iter_new(ctx, CONFIG_TYPE_OPTION);
1139 }
1140
1141 /**
1142 * kmod_config_get_softdeps:
1143 * @ctx: kmod library context
1144 *
1145 * Retrieve an iterator to deal with the softdeps maintained inside the
1146 * library. See kmod_config_iter_get_key(), kmod_config_iter_get_value() and
1147 * kmod_config_iter_next(). At least one call to kmod_config_iter_next() must
1148 * be made to initialize the iterator and check if it's valid.
1149 *
1150 * Returns: a new iterator over the softdeps or NULL on failure. Free it with
1151 * kmod_config_iter_free_iter().
1152 */
1153 KMOD_EXPORT struct kmod_config_iter *kmod_config_get_softdeps(const struct kmod_ctx *ctx)
1154 {
1155 if (ctx == NULL)
1156 return NULL;;
1157
1158 return kmod_config_iter_new(ctx, CONFIG_TYPE_SOFTDEP);
1159 }
1160
1161 /**
1162 * kmod_config_iter_get_key:
1163 * @iter: iterator over a certain configuration
1164 *
1165 * When using a new allocated iterator, user must perform a call to
1166 * kmod_config_iter_next() to initialize iterator's position and check if it's
1167 * valid.
1168 *
1169 * Returns: the key of the current configuration pointed by @iter.
1170 */
1171 KMOD_EXPORT const char *kmod_config_iter_get_key(const struct kmod_config_iter *iter)
1172 {
1173 if (iter == NULL || iter->curr == NULL)
1174 return NULL;
1175
1176 return iter->get_key(iter->curr);
1177 }
1178
1179 /**
1180 * kmod_config_iter_get_value:
1181 * @iter: iterator over a certain configuration
1182 *
1183 * When using a new allocated iterator, user must perform a call to
1184 * kmod_config_iter_next() to initialize iterator's position and check if it's
1185 * valid.
1186 *
1187 * Returns: the value of the current configuration pointed by @iter.
1188 */
1189 KMOD_EXPORT const char *kmod_config_iter_get_value(const struct kmod_config_iter *iter)
1190 {
1191 const char *s;
1192
1193 if (iter == NULL || iter->curr == NULL)
1194 return NULL;
1195
1196 if (iter->get_value == NULL)
1197 return NULL;
1198
1199 if (iter->intermediate) {
1200 struct kmod_config_iter *i = (struct kmod_config_iter *)iter;
1201
1202 free(i->data);
1203 s = i->data = (void *) iter->get_value(iter->curr);
1204 } else
1205 s = iter->get_value(iter->curr);
1206
1207 return s;
1208 }
1209
1210 /**
1211 * kmod_config_iter_next:
1212 * @iter: iterator over a certain configuration
1213 *
1214 * Make @iter point to the next item of a certain configuration. It's an
1215 * automatically recycling iterator. When it reaches the end, false is
1216 * returned; then if user wants to iterate again, it's sufficient to call this
1217 * function once more.
1218 *
1219 * Returns: true if next position of @iter is valid or false if its end is
1220 * reached.
1221 */
1222 KMOD_EXPORT bool kmod_config_iter_next(struct kmod_config_iter *iter)
1223 {
1224 if (iter == NULL)
1225 return false;
1226
1227 if (iter->curr == NULL) {
1228 iter->curr = iter->list;
1229 return iter->curr != NULL;
1230 }
1231
1232 iter->curr = kmod_list_next(iter->list, iter->curr);
1233
1234 return iter->curr != NULL;
1235 }
1236
1237 /**
1238 * kmod_config_iter_free_iter:
1239 * @iter: iterator over a certain configuration
1240 *
1241 * Free resources used by the iterator.
1242 */
1243 KMOD_EXPORT void kmod_config_iter_free_iter(struct kmod_config_iter *iter)
1244 {
1245 free(iter->data);
1246 free(iter);
1247 }